Using CUDA to compute vector addition a+b=c
Process:
1. Request the host memory. Vector A, vector b, calculation result C
2. Initialize the data. Initializes the vector a,b,c with a random number between 0-1
3.GPU memory request. GPU memory space required for A,B,C
4. Copy of data. Copy data from host memory to GPU memory
5. Calculate the required number of threads and the number of thread blocks.
6. Calling the GPU addition function
7. Copy of data. Copy the results from GPU memory to host memory.
8. Run again on the CPU, comparing with the GPU results.
9. Release the GPU memory.
10. Release the host memory.
11. Reset the GPU status.
Important: Memory management, data copying.
Code:
1#include <stdio.h>2#include <cuda_runtime.h>3__global__void4Vectoradd (Const float*a,Const float*b,float*c,intnumelements)5 {6 inti = blockdim.x * blockidx.x +threadidx.x;7 8 if(I <numelements)9 {TenC[i] = A[i] +B[i]; One } A } - - intMainvoid) the { - //detecting Cuda return Values -cudaerror_t err =cudasuccess; - + //Initializing vector dimensions - intNumelements =50000; + //Compute Memory Requirements Asize_t size = numelements *sizeof(float); at -printf"[Vector addition of%d elements]\n", numelements); - - // - //a,b,c Request memory space for the host - float*host_a = (float*)malloc(size); in float*host_b = (float*)malloc(size); - float*host_c = (float*)malloc(size); to //determine whether to apply for success + if(host_a = = NULL | | host_b = = NULL | | host_c = =NULL) - { thefprintf (stderr,"Failed to allocate host vectors!\n"); * exit (exit_failure); $ }Panax Notoginseng //Initialize host , A, b - for(inti =0; i < numelements; ++i) the { +Host_a[i] = rand ()/(float) Rand_max; AHost_b[i] = rand ()/(float) Rand_max; the } + - // $ //Apply Cuda memory space and Judge $ float*device_a =NULL; -Err = Cudamalloc ((void* *) &device_a, size); - if(Err! =cudasuccess) the { -fprintf (stderr,"failed to request Cuda memory space for vector A (error code%s)!\n", cudageterrorstring (err));Wuyi exit (exit_failure); the } - float*device_b =NULL; WuErr = Cudamalloc ((void* *) &device_b, size); - if(Err! =cudasuccess) About { $fprintf (stderr,"failed to request Cuda memory space for Vector B (error code%s)!\n", cudageterrorstring (err)); - exit (exit_failure); - } - float*device_c =NULL; AErr = Cudamalloc ((void* *) &Device_c, size); + if(Err! =cudasuccess) the { -fprintf (stderr,"failed to request Cuda memory space on vector C (error code%s)!\n", cudageterrorstring (err)); $ exit (exit_failure); the } the //copy data to Cuda memory and detect theprintf"Copy input data from the host memory to the CUDA device\n"); theErr =cudamemcpy (device_a, host_a, size, cudamemcpyhosttodevice); - if(Err! =cudasuccess) in { thefprintf (stderr,"Failed to copy vectors A from host to device (error code%s)!\n", cudageterrorstring (err)); the exit (exit_failure); About } theErr =cudamemcpy (Device_b, host_b, size, cudamemcpyhosttodevice); the if(Err! =cudasuccess) the { +fprintf (stderr,"Failed to copy vectors A from host to device (error code%s)!\n", cudageterrorstring (err)); - exit (exit_failure); the }Bayi the //Calculate line Cheng and Threads the //block threads per thread - intThreadsperblock = the; - //number of thread blocks per grid the intBlockspergrid = (numelements + threadsperblock-1)/Threadsperblock; theprintf"CUDA kernel launch with%d blocks of%d threads\n", Blockspergrid, threadsperblock); theVectoradd<<<blockspergrid, threadsperblock>>>(Device_a, Device_b, Device_c, numelements); the //determine the operation of Cuda program -Err =cudagetlasterror (); the if(Err! =cudasuccess) the { thefprintf (stderr,"Failed to launch Vectoradd kernel (error code%s)!\n", cudageterrorstring (err));94 exit (exit_failure); the } the the //Copy the results back to the host computer98printf"Copy output data from the CUDA device to the host memory\n"); AboutErr =cudamemcpy (Host_c, Device_c, size, cudamemcpydevicetohost); - if(Err! =cudasuccess)101 {102fprintf (stderr,"The result of the calculation is copied back to the host failed (error code:%s) \ n", cudageterrorstring (err));103 exit (exit_failure);104 } the 106 //Result Validation107 for(inti =0; i < numelements; ++i)108 {109 if(Fabs (Host_a[i] + host_b[i]-host_c[i]) > 1e-5) the {111fprintf (stderr,"validation failed%d\n", i); the exit (exit_failure);113 } the } theprintf"verification successful \ n"); the 117 //free CUDA Memory and host memory118Err =Cudafree (device_a);119Err =Cudafree (device_b); -Err =Cudafree (device_c);121 Free(host_a);122 Free(host_b);123 Free(host_c);124 the //To reset the cuda State126Err =Cudadevicereset ();127printf"End"); - return 0;129}
So annoying ... The GPU's got a real problem.
Cuda_c Learning Notes-vector addition