Advertisement
Guest User

Untitled

a guest
Nov 28th, 2014
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.90 KB | None | 0 0
  1. #include "cuda.h"
  2. //#include "gcd.cu"
  3. #include <cstdio>
  4. #include <cmath>
  5. #include <iostream>
  6.  
  7.  
  8. #define BLOCK_DIM_X 16
  9. #define BLOCK_DIM_Y 32
  10.  
  11.  
  12. int main(){
  13.     int n, m;
  14.     scanf("%d%d", &n, &m);
  15.  
  16.     cuInit(0);
  17.     CUdevice cuDevice;
  18.     CUresult res = cuDeviceGet(&cuDevice, 0);
  19.  
  20.     CUcontext cuContext;
  21.     res = cuCtxCreate(&cuContext, 0, cuDevice);
  22.  
  23.     CUmodule cuModule = (CUmodule)0;
  24.     res = cuModuleLoad(&cuModule, "gcd.ptx");
  25.  
  26.     CUfunction gcd;
  27.     res = cuModuleGetFunction(&gcd, cuModule, "gcd");
  28.  
  29.  
  30.     int *pres = (int*) malloc(sizeof(int)*n*m);
  31.     int *qres = (int*) malloc(sizeof(int)*n*m);
  32.    
  33.     CUdeviceptr gpu_pres;
  34.     CUdeviceptr gpu_qres;
  35.  
  36.     res = cuMemAlloc(&gpu_pres, sizeof(int)*n*m);
  37.     res = cuMemAlloc(&gpu_qres, sizeof(int)*n*m);
  38.     cuCtxSynchronize();
  39.     //int blocks_per_grid = n*m;
  40.     //int threads_per_block = 16*32;
  41.  
  42.     int block_dim_x = BLOCK_DIM_X;
  43.     int block_dim_y = BLOCK_DIM_Y;
  44.  
  45.     int grid_dim_x = (n-1) / BLOCK_DIM_X + 1;
  46.     int grid_dim_y = (m-1) / BLOCK_DIM_Y + 1;
  47.    
  48.     int w = BLOCK_DIM_X * grid_dim_x;
  49.     int test;
  50.  
  51.  
  52.     void* args[] = {&w, &gpu_pres, &gpu_qres};
  53.     //cuLaunchKernel(gcd, n*m, 1, 1, n, 1, 1, 0, 0, args, 0);
  54.     res = cuLaunchKernel(gcd, grid_dim_x, grid_dim_y, 1, block_dim_x, block_dim_y, 1, 0, 0, args, 0);
  55.     cuCtxSynchronize();
  56.  
  57.     res = cuMemcpyDtoH( (void*)pres, gpu_pres, sizeof(int)*n*m);
  58.     res = cuMemcpyDtoH( (void*)qres, gpu_qres, sizeof(int)*n*m);
  59.  
  60.     cuCtxSynchronize();
  61.     std::cout << "N = " << n << " M = " << m << std::endl;
  62.     for(int i = 0; i<n; i++){
  63.         for(int j = 0; j<m; j++){
  64.             std::cout << i+1 << " " << j+1 << " " << pres[j*w + i] << " " << qres[j*n +i] << std::endl;
  65.         }
  66.     }
  67.  
  68.     cuMemFree(gpu_qres);
  69.     cuMemFree(gpu_pres);
  70.     cuCtxSynchronize();
  71.  
  72.     cuCtxDestroy(cuContext);
  73.     free(pres);
  74.     free(qres);
  75.     return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement