/* ====================================================== cpu-add-vector.c: add 2 vectors of 10000000 elements ====================================================== */ #include #include #include #include struct timeval start_time, stop_time; int elapsed; int main(int argc, char *argv[]) { int i, N; if ( argc == 1 ) { printf("Usage: %s #elements_in_vector\n", argv[0]); exit(1); } N = atoi( argv[1] ); // N = # elements in vector float *x, *y, *z; /* ========================================== Allocate arrays to store vector x and y ========================================== */ x = malloc( N*sizeof(float) ); y = malloc( N*sizeof(float) ); z = malloc( N*sizeof(float) ); // initialize x and y arrays on the host for (i = 0; i < N; i++) { x[i] = 1.0f; y[i] = 2.0f; } gettimeofday(&start_time, NULL); // Record current sys time as start_time /* =============================================== CPU version of the vector addition algorithm =============================================== */ for (i = 0; i < N; i++) z[i] = x[i] + y[i]; gettimeofday(&stop_time, NULL); // Record current sys time as stop_time elapsed = (stop_time.tv_sec*1000000 + stop_time.tv_usec) - (start_time.tv_sec*1000000 + start_time.tv_usec); printf("Elasped time = %d micro secs\n", elapsed); /* ================================================== Check for errors (all values should be 3.0f) ================================================== */ int nErrors = 0; for (int i = 0; i < N; i++) { if ( fabs(z[i]-3.0f) > 0.001 ) nErrors++; } printf("#errors = %d\n", nErrors); if ( N < 10 ) { printf("Vector sum = "); for (i = 0; i < N; i++) printf("%f ", y[i]); printf("\n\n"); } // Free memory free(x); free(y); return 0; }