please help Wriic: a C propramning la consolle: program of he: vector muliply ad
ID: 3739867 • Letter: P
Question
please help
Wriic: a C propramning la consolle: program of he: vector muliply ad "axpy" algorithm for double precision floating data types: a) Execution should be scalable and be Your C program should be single threaded, and sequential. able to handle the number of vector elements, N, from 1 to 1,000,000. Set the vector/array dimension using a DEFINE statement. Use a random number generator which every time the program is executed fills the random data into the scalar and vector elements with random double precision floating point values ranging from 1 to 100 The console window should execute and remain open until manually closed. Comment your code to explain what it is doing. You should use the time.h header file library, and either the "time_t or clock t timer functions to capture the start and end execution times for your benchmark. You will need to use an outer accuracy improvement loop in your program. Adjust the total number of iterations of your inner and outer accuracy improvement loops so that total execution time is approximately constant as you vary the dimensions of your x, y and z arrays from 1 to 1,000,000.Explanation / Answer
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdint.h>
#define Size 30000
//o count the colcks
#ifdef _WIN32
//for windows
#include <intrin.h>
uint64_t rdtsc(){
return __rdtsc();
}
//for linux
#else
uint64_t rdtsc(){
unsigned int lo,hi;
__asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
return ((uint64_t)hi << 32) | lo;
}
#endif
//the function to fill the vector randomly
void populateVector(int *in)
{
int itr=0;
for(itr=0;itr<Size;itr++)
in[itr]=(rand()%100)+1;
}
//perform the axpy operation
void axpyOPeration(int *x,int *y, int a)
{
int itr=0;
for(itr=0;itr<Size;itr++)
y[itr]=a*x[itr] + y[itr];
}
//to perform the improvent
int improveaxPY(int *x,int *y, int a)
{
int itr=0;
for(itr=0;itr<Size;)
{
y[itr]=a*x[itr] + y[itr];
y[itr+1]=a*x[itr+1] + y[itr+1];
y[itr+2]=a*x[itr+2] + y[itr+2];
itr=itr+3;
}
return itr;
}
//The main
int main()
{
int scale=2;
clock_t Calctime;
int x[Size];
int y[Size];
//create the vectors
populateVector(x);
populateVector(y);
//record the start timee
Calctime=clock();
//The CPU cycle count
unsigned long long counbtCycle = rdtsc();
axpyOPeration(x,y,scale);
int loop=improveaxPY(x,y,scale);
//count the number of counbtCycle
counbtCycle = rdtsc() - counbtCycle;
//find the total time needed
Calctime=clock()-Calctime;
double time_taken_int = ((double)Calctime)/CLOCKS_PER_SEC;
printf("Vector length : %d", Size );
printf(" The number of accuracy impr loops: %d", loop/3 );
printf(" Total computation time: %f", Calctime);
double at=Calctime/Size;
printf(" Computation time per axpy vector: %f", at);
double avt=Calctime/(2*Size);
printf(" Computation time per vector component: %f", avt);
printf(" Number of machine counbtCycle per arithematic operation: %d", counbtCycle/(2*Size));
system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.