Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write C console programs of the “axpy” algorithm for integer, and double precisi

ID: 3588174 • Letter: W

Question

Write C console programs of the “axpy” algorithm for integer, and double precision data types. Instrument and monitor and measure execution time as a function of problem size. For each of the 2 data types:

Write it as a straight forward, single processor, sequential execution program, which you can scale the size of the axpy computation for N = 1, 5, 10 , 50, 100, 500, 1000, 5000, 10000, 50000, 100000, 500000 and 1,000,000 vector elements . Use a random number generator to fill the vectors. Set the vector dimension using a DEFINE statement then recompile & execution each vector length.

Your program should print out the vector length, and wall clock execution time in the console window, and computation time per axpy vector for each vector length used in your program code, and the number of accuracy improvement loops you run the daxpy computation to improve accuracy.

For each t data for each data type and vector/array length combination, collect execution time data using the no compiler optimization setting (/Od, or disabled in Visual Studio) and the full optimization setting ( /Ox in Visual Studio)

Explanation / 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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote