99__global__ void saxpy (float * const z, const float * const x, const float * const y, const float a, const unsigned size)
1010{
1111 // TODO 9: Compute the global index for each thread.
12- unsigned idx = 0 ;
12+ unsigned idx = threadIdx . x ;
1313
1414 // TODO 10: Check if idx is out of bounds. If yes, return.
15- if (idx >= 0 )
16- return ;
15+ if (idx >= size )
16+ { return ;}
1717
1818 // TODO 11: Perform the SAXPY operation: z = a * x + y.
19+
20+ z[idx] = a * x[idx] + y[idx];
1921}
2022
2123int main (int argc, char *argv[])
2224{
2325 // TODO 1: Set the size. Start with something simple like 64.
2426 // TODO Optional: Try out these sizes: 256, 1024, 2048, 14, 103, 1025, 3127
25- const unsigned size = 0 ;
27+ const unsigned size = 64 ;
2628
2729 // Host arrays.
2830 float * x = new float [size];
@@ -51,11 +53,16 @@ int main(int argc, char *argv[])
5153 // Device arrays
5254 float *d_x, *d_y, *d_z;
5355
56+ int sizeInBytes = size * sizeof (float );
57+
5458 // TODO 2: Allocate memory on the device. Fill in the blanks for d_x, then do the same commands for d_y and d_z.
55- // CUDA(cudaMalloc((void **)& pointer, size in bytes)));
59+ CUDA (cudaMalloc ((void **)& d_x, sizeInBytes));
60+ CUDA (cudaMalloc ((void **)& d_y, sizeInBytes));
61+ CUDA (cudaMalloc ((void **)& d_z, sizeInBytes));
5662
5763 // TODO 3: Copy array contents of X and Y from the host (CPU) to the device (GPU). Follow what you did for 2,
58- // CUDA(cudaMemcpy(dest ptr, source ptr, size in bytes, direction enum));
64+ CUDA (cudaMemcpy (d_x, x, sizeInBytes, cudaMemcpyHostToDevice));
65+ CUDA (cudaMemcpy (d_y, y, sizeInBytes, cudaMemcpyHostToDevice));
5966
6067 CUDA (cudaDeviceSynchronize ());
6168
@@ -69,17 +76,20 @@ int main(int argc, char *argv[])
6976 // TODO 4: Setup threads and blocks.
7077 // Start threadPerBlock as 128, then try out differnt configurations: 32, 64, 256, 512, 1024
7178 // Use divup to get the number of blocks to launch.
72- const unsigned threadsPerBlock = 0 ;
79+ const unsigned threadsPerBlock = 128 ;
7380
7481 // TODO 5: Implement the divup function in common.cpp
7582 const unsigned blocks = divup (size, threadsPerBlock);
7683
7784 // TODO 6: Launch the GPU kernel with blocks and threadPerBlock as launch configuration
78- // saxpy<<< >>> (.... );
85+ saxpy<<<blocks, threadsPerBlock >>> (d_z, d_x, d_y, a, size );
7986
8087 // TODO 7: Copy the answer back to the host (CPU) from the device (GPU).
8188 // Copy what you did in 3, except for d_z -> z.
8289
90+ cudaMemcpy (z, d_z, sizeInBytes, cudaMemcpyDeviceToHost);
91+
92+
8393 // LOOK: Use postprocess to check the result
8494 compareReferenceAndResult (z_gold, z, size, 1e-6 );
8595 std::cout << " ****************************************************" << std::endl << std::endl;
@@ -88,6 +98,10 @@ int main(int argc, char *argv[])
8898 // TODO 8: free device memory using cudaFree
8999 // CUDA(cudaFree(device pointer));
90100
101+ CUDA (cudaFree (d_x));
102+ CUDA (cudaFree (d_y));
103+ CUDA (cudaFree (d_z));
104+
91105 // free host memory
92106 delete[] x;
93107 delete[] y;
0 commit comments