Advertisement
pai

CUDA PROGRAM TO ADD VECTOR

pai
Jun 30th, 2011
4,172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2.  
  3. /* vector elements */
  4. #define N 512
  5.  
  6. /* definition of an function executed on GPU */
  7. __global__ void vecAdd(int *A, int *B,int *C)
  8. {
  9.     int i = threadIdx.x;
  10.  
  11.     C[i] = A[i] + B[i];
  12. }
  13.  
  14. /* prints contents */
  15. void Print(const int *A, int size)
  16. {
  17.     for(int i=0;i<size;i++)
  18.     {
  19.         printf("\t%d", A[i]);
  20.     }
  21.     printf("\n");
  22. }
  23.  
  24. /* an entry point */
  25. int main(int argc, char *argv[])
  26. {
  27.     int A[N], B[N], C[N],i;
  28.     int *devA,*devB,*devC;
  29.  
  30.     /* device memory allocations */
  31.     cudaMalloc((void**)&devA, N*sizeof(int));
  32.     cudaMalloc((void**)&devB, N*sizeof(int));
  33.     cudaMalloc((void**)&devC, N*sizeof(int));
  34.  
  35.     /* setting initialize values */
  36.     for(i=0;i<N;i++)
  37.     {
  38.         A[i]=i;
  39.         B[i]=i;
  40.     }
  41.     for(i=0;i<N;i++)
  42.     {
  43.         C[i]=0;
  44.     }
  45.     printf("\nVecotr A is:\n");
  46.     Print( A, N);
  47.     printf("\nVector B is:\n");
  48.     Print(B, N);
  49.  
  50.     /* data copying from host to device */
  51.     cudaMemcpy(devA, A, N*sizeof(int), cudaMemcpyHostToDevice);
  52.     cudaMemcpy(devB, B, N*sizeof(int), cudaMemcpyHostToDevice);
  53.     cudaMemcpy(devC, C, N*sizeof(int), cudaMemcpyHostToDevice);
  54.  
  55.     /* execution on GPU */
  56.     vecAdd<<<1, N>>>(devA, devB, devC);
  57.  
  58.     /* data copying from device to host */
  59.     cudaMemcpy(C, devC, N*sizeof(int), cudaMemcpyDeviceToHost);
  60.     printf("\nVector A+B is\n\n");
  61.     Print(C, N);
  62.  
  63.     /* freeing device memory */
  64.     cudaFree((void**)&devA);
  65.     cudaFree((void**)&devB);
  66.     cudaFree((void**)&devC);
  67.  
  68.     return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement