source: icGREP/icgrep-devel/icgrep/IDISA/CudaDriver.h @ 5129

Last change on this file since 5129 was 5129, checked in by lindanl, 3 years ago

Bug fix.

File size: 3.4 KB
RevLine 
[5128]1#include <string>
2#include <iostream>
3#include <fstream>
4#include <sys/stat.h>
5#include <fcntl.h>
6#include <unistd.h>
7#include <cassert>
8#include "cuda.h"
9
10#define GROUPTHREADS 64
11
12void checkCudaErrors(CUresult err) {
13  assert(err == CUDA_SUCCESS);
14}
15
16/// main - Program entry point
17int RunPTX(std::string PTXFilename, char * fileBuffer, ulong filesize) {
18 
19  CUdevice    device;
20  CUmodule    cudaModule;
21  CUcontext   context;
22  CUfunction  function;
23  int         devCount;
24
25  // CUDA initialization
26  checkCudaErrors(cuInit(0));
27  checkCudaErrors(cuDeviceGetCount(&devCount));
28  checkCudaErrors(cuDeviceGet(&device, 0));
29
30  char name[128];
31  checkCudaErrors(cuDeviceGetName(name, 128, device));
32  // std::cout << "Using CUDA Device [0]: " << name << "\n";
33
34  int devMajor, devMinor;
35  checkCudaErrors(cuDeviceComputeCapability(&devMajor, &devMinor, device));
36  // std::cout << "Device Compute Capability: " << devMajor << "." << devMinor << "\n";
37  if (devMajor < 2) {
38    std::cerr << "ERROR: Device 0 is not SM 2.0 or greater\n";
39    return 1;
40  }
41
42  std::ifstream t(PTXFilename);
43  if (!t.is_open()) {
44    std::cerr << "Error: cannot open " << PTXFilename << " for processing. Skipped.\n";
45    exit(-1);
46  }
47 
48  std::string ptx_str((std::istreambuf_iterator<char>(t)), std::istreambuf_iterator<char>());
49
50  // Create driver context
51  checkCudaErrors(cuCtxCreate(&context, 0, device));
52
53  // Create module for object
54  checkCudaErrors(cuModuleLoadDataEx(&cudaModule, ptx_str.c_str(), 0, 0, 0));
55
56  // Get kernel function
57  checkCudaErrors(cuModuleGetFunction(&function, cudaModule, "kernel"));
58
59  // Device data
60  CUdeviceptr devBufferInput;
61  CUdeviceptr devBufferSize;
62  CUdeviceptr devBufferOutput;
63
64  int groupSize = GROUPTHREADS * sizeof(ulong) * 8;
65  int bufferSize = (filesize/groupSize + 1) * groupSize;
66
67  checkCudaErrors(cuMemAlloc(&devBufferInput, bufferSize));
68  // checkCudaErrors(cuMemsetD8(devBufferInput, 0, bufferSize));
69  checkCudaErrors(cuMemAlloc(&devBufferSize, sizeof(ulong)));
70  checkCudaErrors(cuMemAlloc(&devBufferOutput, sizeof(ulong)*GROUPTHREADS));
71
72  //Copy from host to device
[5129]73  checkCudaErrors(cuMemcpyHtoD(devBufferInput, fileBuffer, bufferSize));
[5128]74  checkCudaErrors(cuMemcpyHtoD(devBufferSize, &filesize, sizeof(ulong)));
75
76  unsigned blockSizeX = GROUPTHREADS;
77  unsigned blockSizeY = 1;
78  unsigned blockSizeZ = 1;
79  unsigned gridSizeX  = 1;
80  unsigned gridSizeY  = 1;
81  unsigned gridSizeZ  = 1;
82
83  // Kernel parameters
84  void *KernelParams[] = { &devBufferInput, &devBufferSize, &devBufferOutput};
85
86  // std::cout << "Launching kernel\n";
87
88  // Kernel launch
89  checkCudaErrors(cuLaunchKernel(function, gridSizeX, gridSizeY, gridSizeZ,
90                                 blockSizeX, blockSizeY, blockSizeZ,
91                                 0, NULL, KernelParams, NULL));
92  // std::cout << "kernel success.\n";
93  // Retrieve device data
94  ulong * matchCount = (ulong *) malloc(sizeof(ulong)*GROUPTHREADS);
95  checkCudaErrors(cuMemcpyDtoH(matchCount, devBufferOutput, sizeof(ulong)*GROUPTHREADS));
96
97  int count = 0;
98  for (unsigned i = 0; i < GROUPTHREADS; ++i) {
99    count += matchCount[i];
100    // std::cout << i << ":" << matchCount[i] << "\n";
101  }
102  std::cout << count << "\n";
103
104  // Clean-up
105  checkCudaErrors(cuMemFree(devBufferInput));
106  checkCudaErrors(cuMemFree(devBufferSize));
107  checkCudaErrors(cuMemFree(devBufferOutput));
108  checkCudaErrors(cuModuleUnload(cudaModule));
109  checkCudaErrors(cuCtxDestroy(context));
110
111  return 0;
112}
Note: See TracBrowser for help on using the repository browser.