Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.00 KB | None | 0 0
  1. #define BLOCK_SIZE 32
  2. #define MATRIX_SIZE 4096
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <cuda_runtime.h>
  7. #include <cutil.h>
  8. #include <sys/time.h>
  9.    
  10. bool InitCUDA(void)
  11. {
  12.         int count = 0;
  13.         int i = 0;
  14.         cudaGetDeviceCount(&count);
  15.         if(count == 0) {
  16.                 fprintf(stderr, "There is no device.\n");
  17.                 return false;
  18.         }
  19.         for(i = 0; i < count; i++) {
  20.                 cudaDeviceProp prop;
  21.                 if(cudaGetDeviceProperties(&prop, i) == cudaSuccess) {
  22.                         if(prop.major >= 1) {
  23.                                 break;
  24.                         }
  25.                 }
  26.         }
  27.         if(i == count) {
  28.                 fprintf(stderr, "There is no device supporting CUDA.\n");
  29.                 return false;
  30.         }
  31.         cudaSetDevice(i);
  32.         printf("CUDA initialized.\n");
  33.         return true;
  34. }
  35.  
  36. __global__ void MatMulKernel(float* Md, float* Nd, float* Pd, int Width)
  37. {
  38.     int tx = threadIdx.x;
  39.     int ty = threadIdx.y;
  40.  
  41.     float Pvalue = 0;
  42.  
  43.     for( int k = 0; k < Width; ++k)
  44.     {
  45.         float Mdelement = Md[ty * Width + k];
  46.         float Ndelement = Nd[k * Width + tx];
  47.         Pvalue += Mdelement * Ndelement;
  48.     }
  49.     Pd[ty * Width + tx] = Pvalue;
  50.  
  51. }
  52.  
  53. int main(int argc, char* argv[])
  54. {
  55.     struct timeval t0,t1;
  56.  
  57.     if(!InitCUDA()) {
  58.             return 0;
  59.     }
  60.  
  61.     unsigned int size = MATRIXSIZE * MATRIXSIZE;
  62.     unsigned int mem_size = sizeof(float) * size;
  63.  
  64.     float* A = (float*) malloc(mem_size);
  65.     float* B = (float*) malloc(mem_size);
  66.     float* C = (float*) malloc(mem_size);
  67.     float* Md;
  68.     float* Nd;
  69.     float* Pd;
  70.  
  71.     int row,col;
  72.  
  73.     A.width = A.height = MATRIXSIZE;
  74.     B.width = B.height = MATRIXSIZE;
  75.     C.width = C.height = MATRIXSIZE;
  76.  
  77.     for( int i=0; i<size; i++)
  78.     {
  79.         row = i/MATRIXSIZE;
  80.         col = i%MATRIXSIZE;
  81.         A[i] = ((row + 1.0)*(col + 1.0))/MATRIXSIZE;
  82.         B[i] = (col + 1.0)/(row + 1.0);
  83.     }
  84.  
  85.     gettimeofday(&t0,0);
  86.  
  87.     cudaMalloc((void**) &Md.elements, mem_size);
  88.     cudaMalloc((void**) &Nd.elements, mem_size);
  89.     cudaMalloc((void**) &Pd.elements, mem_size);
  90.  
  91.     cudaMemcpy(Md.elements, A.elements, mem_size, cudaMemcpyHostToDevice);
  92.     cudaMemcpy(Nd.elements, B.elements, mem_size, cudaMemcpyHostToDevice);
  93.  
  94.     dim3 dimBlock(MATRIXSIZE,MATRIXSIZE);
  95.     dim3 dimGrid(1,1);
  96.  
  97.     MatMulKernel<<<dimGrid, dimBlock>>>(Md,Nd,Pd,MATRIXSIZE);
  98.  
  99.     cudaMemcpy(C, Pd, mem_size, cudaMemcpyDeviceToHost);
  100.  
  101.     cudaFree(Md);
  102.     cudaFree(Nd);
  103.     cudaFree(Pd);
  104.  
  105.     gettimeofday(&t1,0);
  106.  
  107.     printf("\nMatrix C\n");
  108.     for(int i=0;i<size;i++)
  109.     {
  110.         printf("%6.2f ",C[i]);
  111.         if(((i+1)%MATRIXSIZE)==0)
  112.             printf("\n");
  113.     }
  114.  
  115.     printf("\nTime Results\n");
  116.     float totalInt = t1.tv_sec - t0.tv_sec + (t1.tv_usec - t0.tv_usec)*1.0E-06;
  117.     printf("Total Execution Time:\t%e\n",totalInt);
  118.  
  119.     free(A);
  120.     free(B);
  121.     free(C);
  122.  
  123.     return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement