source: icGREP/icgrep-devel/icgrep/IR_Gen/CudaDriver.h @ 5345

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

Remove initialization of GPU memory.

File size: 5.4 KB
Line 
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 <toolchain.h>
9#include "cuda.h"
10
11#define GROUPTHREADS 64
12
13void checkCudaErrors(CUresult err) {
14  assert(err == CUDA_SUCCESS);
15}
16
17/// main - Program entry point
18ulong * RunPTX(std::string PTXFilename, char * fileBuffer, ulong filesize, bool CountOnly, std::vector<size_t> LFPositions, ulong * startPoints, ulong * accumBytes) {
19 
20  CUdevice    device;
21  CUmodule    cudaModule;
22  CUcontext   context;
23  CUfunction  function;
24  int         devCount;
25
26  // CUDA initialization
27  checkCudaErrors(cuInit(0));
28  checkCudaErrors(cuDeviceGetCount(&devCount));
29  checkCudaErrors(cuDeviceGet(&device, 0));
30
31  char name[128];
32  checkCudaErrors(cuDeviceGetName(name, 128, device));
33  // std::cout << "Using CUDA Device [0]: " << name << "\n";
34
35  int devMajor, devMinor;
36  checkCudaErrors(cuDeviceComputeCapability(&devMajor, &devMinor, device));
37  // std::cout << "Device Compute Capability: " << devMajor << "." << devMinor << "\n";
38  if (devMajor < 2) {
39    std::cerr << "ERROR: Device 0 is not SM 2.0 or greater\n";
40    exit(-1);
41  }
42
43  std::ifstream t(PTXFilename);
44  if (!t.is_open()) {
45    std::cerr << "Error: cannot open " << PTXFilename << " for processing. Skipped.\n";
46    exit(-1);
47  }
48 
49  std::string ptx_str((std::istreambuf_iterator<char>(t)), std::istreambuf_iterator<char>());
50
51  // Create driver context
52  checkCudaErrors(cuCtxCreate(&context, 0, device));
53
54  // Create module for object
55  checkCudaErrors(cuModuleLoadDataEx(&cudaModule, ptx_str.c_str(), 0, 0, 0));
56
57  // Get kernel function
58  checkCudaErrors(cuModuleGetFunction(&function, cudaModule, "GPU_Main"));
59
60  // Device data
61  CUdeviceptr devBufferInput;
62  CUdeviceptr devStartPoints;
63  CUdeviceptr devBufferSizes;
64  CUdeviceptr devBufferOutput;
65
66  int groupSize = GROUPTHREADS * sizeof(ulong) * 8;
67  const unsigned numOfGroups = codegen::GroupNum;
68
69  if(LFPositions.size() < numOfGroups){
70    std::cerr << "Line Breaks less than " << numOfGroups << std::endl;
71    exit(-1);
72  }
73
74  unsigned avg = LFPositions.size()/numOfGroups;
75  unsigned left = LFPositions.size()%numOfGroups;
76
77  size_t divPoints[numOfGroups + 1];
78  size_t bufferSizes[numOfGroups];
79  divPoints[0] = 0;
80  startPoints[0] = 0;
81  unsigned i = 1;
82  unsigned pos = 0;
83  while (i < numOfGroups){
84    if (i < left)
85      pos += avg + 1;
86    else
87      pos += avg;
88
89    divPoints[i] = LFPositions[pos]+1;
90    bufferSizes[i-1] = divPoints[i]-divPoints[i-1];
91    startPoints[i] = startPoints[i-1] + ((bufferSizes[i-1]-1)/groupSize+1)*groupSize;
92
93    i++;
94  }
95
96  divPoints[numOfGroups] = filesize;
97  bufferSizes[i-1] = divPoints[i]-divPoints[i-1];
98  startPoints[i] = startPoints[i-1] + ((bufferSizes[i-1]-1)/groupSize+1)*groupSize;
99   
100  checkCudaErrors(cuMemAlloc(&devBufferInput, startPoints[numOfGroups]));
101  checkCudaErrors(cuMemAlloc(&devStartPoints, sizeof(ulong) * (numOfGroups + 1)));
102  checkCudaErrors(cuMemAlloc(&devBufferSizes, sizeof(ulong) * numOfGroups));
103
104  int outputSize = 0;
105  if (CountOnly){
106    outputSize = sizeof(ulong) * GROUPTHREADS * numOfGroups;
107  }
108  else{
109    outputSize = startPoints[numOfGroups]/4;
110  }
111  checkCudaErrors(cuMemAlloc(&devBufferOutput, outputSize));
112
113  //Copy from host to device
114  for(unsigned i=0; i<numOfGroups; i++){
115    checkCudaErrors(cuMemcpyHtoD(devBufferInput+startPoints[i], fileBuffer+divPoints[i], bufferSizes[i]));
116  }
117  checkCudaErrors(cuMemcpyHtoD(devStartPoints, startPoints, sizeof(ulong) * (numOfGroups + 1)));
118  checkCudaErrors(cuMemcpyHtoD(devBufferSizes, bufferSizes, sizeof(ulong) * numOfGroups));
119
120  unsigned blockSizeX = GROUPTHREADS;
121  unsigned blockSizeY = 1;
122  unsigned blockSizeZ = 1;
123  unsigned gridSizeX  = numOfGroups;
124  unsigned gridSizeY  = 1;
125  unsigned gridSizeZ  = 1;
126
127  // Kernel parameters
128  void *KernelParams[] = { &devBufferInput, &devStartPoints, &devBufferSizes, &devBufferOutput};
129
130  // std::cout << "Launching kernel\n";
131
132  CUevent start;
133  CUevent stop;
134  float elapsedTime;
135
136  cuEventCreate(&start, CU_EVENT_BLOCKING_SYNC);
137  cuEventRecord(start,0);
138
139  // Kernel launch
140  checkCudaErrors(cuLaunchKernel(function, gridSizeX, gridSizeY, gridSizeZ,
141                                 blockSizeX, blockSizeY, blockSizeZ,
142                                 0, NULL, KernelParams, NULL));
143  // std::cout << "kernel success.\n";
144
145  cuEventCreate(&stop, CU_EVENT_BLOCKING_SYNC);
146  cuEventRecord(stop,0);
147  cuEventSynchronize(stop);
148
149  cuEventElapsedTime(&elapsedTime, start, stop);
150  printf("GPU Kernel time : %f ms\n" ,elapsedTime);
151
152  // Retrieve device data
153  ulong * matchRslt;
154  if (posix_memalign((void**)&matchRslt, 32, outputSize)) {
155    std::cerr << "Cannot allocate memory for output.\n";
156    exit(-1);
157  }
158  checkCudaErrors(cuMemcpyDtoH(matchRslt, devBufferOutput, outputSize));
159
160  if (CountOnly){
161    int count = 0;
162    for (unsigned i = 0; i < GROUPTHREADS * numOfGroups; ++i) {
163      // std::cout << i << ":" << matchRslt[i] << "\n";
164      count += matchRslt[i];
165    }
166    std::cout << count << "\n";
167  }
168  else{
169    for(unsigned i=0; i<=numOfGroups; i++){
170      accumBytes[i] = startPoints[i] - divPoints[i];
171    }
172  }
173
174
175  // Clean-up
176  checkCudaErrors(cuMemFree(devBufferInput));
177  checkCudaErrors(cuMemFree(devStartPoints));
178  checkCudaErrors(cuMemFree(devBufferSizes));
179  checkCudaErrors(cuMemFree(devBufferOutput));
180  checkCudaErrors(cuModuleUnload(cudaModule));
181  checkCudaErrors(cuCtxDestroy(context));
182
183  return matchRslt;
184}
Note: See TracBrowser for help on using the repository browser.