Wir e a C console prram ot the vexior multiply add axpy algorihm ior lmie !er da
ID: 3588898 • Letter: W
Question
Wir e a C console prram ot the vexior multiply add axpy algorihm ior lmie !er data types. Instrument and monitor and measure execution time for the vector multiply add. a) Your C program should be single threaded, and sequential. Execution should be scalable and be able to handle the number of vector elements, N, from 1 to 1,000,000. Set the vector dimension, N, number of accuracy improvement loops, and system clock speed using DEFINE statements. Use a random number generator to fill the random data into the vector elements. Recompile & execute for each vector length (and # of accuracy loops) . Compiler optimizations should be off/defaulted. The console window should execute and remain open until manually closed. Comment your code to explain what it is doing. Make your code portable, and not dependent on development environment or properties settings, as I will run it on Linux, or a laptop using Cygwin or MS Visual Studio. We will inputting selected vector sizes to test your code b Your console program should print out ( using formatted printf commands) 1 Vector length (number of vector elements) 2) The number of accuracy improvement loops you run the axpy computation to improve accuracy. 3) Total computation time 4)Computation time per axpy vector 5) Computation time per vector element 6) The number of machine cycles per arithmetic operationExplanation / Answer
AXPY operation is to multiply a vector X to a scalar value (A) and add it to the another vector Y and store that value again to vector Y
i.e Y=A*X + Y
#include <stdio.h>
#include <stdlib.h> // for random number generator rand()
#include <time.h> // to calculate time works on all platform
#define DIM 5 // size or dimention of a vector
void fillIntVector(int *a)
{
int i=0;
for(i=0;i<DIM;i++)
a[i]=(rand()%100)+1; // generates the random number in the range of 1 to 100 you can change the range by changing the value in mod 100
}
void performAXPYOperationInt(int *x,int *y, int a) // passing the reference of an array so with every change occures in the function will reflect in original arrays
{
int i=0;
for(i=0;i<DIM;i++)
y[i]=a*x[i] + y[i]; // performs the axpy operation
}
void fillDoubleVector(double *a)
{
int i=0;
for(i=0;i<DIM;i++)
{
double d=(double)RAND_MAX / 100;
a[i]= 1.0 + (rand() / d);// generates the random numbers in double in the range of 1 to 100
}
}
void performAXPYOperationDouble(double *x, double *y, int a)
{
int i=0;
for(i=0;i<DIM;i++)
y[i]=a*x[i]+y[i]; // performs AXPY operation for double
}
int main()
{
int a=2;
clock_t t; // object of clock
int x[DIM]={0};
int y[DIM]={0};
double xd[DIM]={0};
double yd[DIM]={0};
fillIntVector(x);
fillIntVector(y);
t=clock(); // operation start time
performAXPYOperationInt(x,y,a);
t=clock()-t; // operation end time
double time_taken_int = ((double)t)/CLOCKS_PER_SEC; // total time in seconds
fillDoubleVector(xd);
fillDoubleVector(yd);
t=clock(); // operation start time
performAXPYOperationDouble(xd,yd,a);
t=clock()-t; //operation end time
double time_taken_double = ((double)t)/CLOCKS_PER_SEC; // total time in seconds
printf("Vector length : %d , execution time for int : %f seconds, execution time for double : %f seconds ", DIM, time_taken_int, time_taken_double);
return 0;
}
Now steps to setting up an visual studio environment for compiler option
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.