$25
Assignment #1: The Big Dot
The dot product of two vectors π = (π$, πβ, β¦, π()β ) and π = (π$, πβ, β¦, π()β ), written π β π, is simply the sum of the component-by-component products:
π β π = β(-/)$β π- Γ π-
Dot products are used extensively in computing and have a wide range of applications. For instance, in 3D graphics (n = 3), we often make use of the fact that π β π = |π||π|πππ π, where | | denotes vector length and π is the angle between the two vectors. In this assignment, you are expected to:
1. Write CUDA code to compute in parallel the dot product of two (possibly large N = 100,000, or N = 1024*1024) random single precision floating point vectors;
2. Write two functions to compute the results on the CPU and GPU, and compare the two results to check for correctness (1.0e-6);
β’ float *CPU_big_dot(float *A, float *B, int N);
β’ float *GPU_big_dot(float *A, float *B, int N);
3. Print performance statistics with timer function;
β’ CPU: Tcpu = Total computation time for CPU_big_dot();
β’ GPU: Tgpu = Total computation time for GPU_big_dot();
β’ Memory allocation and data transfer from CPU to GPU time
β’ Kernel execution time
β’ Data transfer from GPU to CPU time
β’ Speedup = CPU/GPU
4. Analyze the performance results in a few sentences.
β’ Which one runs faster?
β’ Whatβs the reason for that? Problem size, overhead, etc.
Timer functions #include <sys/time.h> long long start_timer() { struct timeval tv; gettimeofday(&tv, NULL); return tv.tv_sec * 1000000 + tv.tv_usec;
}
long long stop_timer(long long start_time, char *name) { struct timeval tv; gettimeofday(&tv, NULL);
long long end_time = tv.tv_sec * 1000000 + tv.tv_usec;
Printf(β%s: %.5f sec\nβ, name, ((float) (end_time β start_time)) /
(1000 * 1000)); return end_time β start_time;
}