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

Last change on this file since 5314 was 5314, checked in by lindanl, 2 years ago

Extend icgrep to use multiple groups of thread on GPU.

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(cuMemsetD8(devBufferInput,0,startPoints[numOfGroups]));
102  checkCudaErrors(cuMemAlloc(&devStartPoints, sizeof(ulong) * (numOfGroups + 1)));
103  checkCudaErrors(cuMemAlloc(&devBufferSizes, sizeof(ulong) * numOfGroups));
104
105  int outputSize = 0;
106  if (CountOnly){
107    outputSize = sizeof(ulong) * GROUPTHREADS * numOfGroups;
108  }
109  else{
110    outputSize = startPoints[numOfGroups]/4;
111  }
112  checkCudaErrors(cuMemAlloc(&devBufferOutput, outputSize));
113
114  //Copy from host to device
115  for(unsigned i=0; i<numOfGroups; i++){
116    checkCudaErrors(cuMemcpyHtoD(devBufferInput+startPoints[i], fileBuffer+divPoints[i], bufferSizes[i]));
117  }
118  checkCudaErrors(cuMemcpyHtoD(devStartPoints, startPoints, sizeof(ulong) * (numOfGroups + 1)));
119  checkCudaErrors(cuMemcpyHtoD(devBufferSizes, bufferSizes, sizeof(ulong) * numOfGroups));
120
121  unsigned blockSizeX = GROUPTHREADS;
122  unsigned blockSizeY = 1;
123  unsigned blockSizeZ = 1;
124  unsigned gridSizeX  = numOfGroups;
125  unsigned gridSizeY  = 1;
126  unsigned gridSizeZ  = 1;
127
128  // Kernel parameters
129  void *KernelParams[] = { &devBufferInput, &devStartPoints, &devBufferSizes, &devBufferOutput};
130
131  // std::cout << "Launching kernel\n";
132
133  CUevent start;
134  CUevent stop;
135  float elapsedTime;
136
137  cuEventCreate(&start, CU_EVENT_BLOCKING_SYNC);
138  cuEventRecord(start,0);
139
140  // Kernel launch
141  checkCudaErrors(cuLaunchKernel(function, gridSizeX, gridSizeY, gridSizeZ,
142                                 blockSizeX, blockSizeY, blockSizeZ,
143                                 0, NULL, KernelParams, NULL));
144  // std::cout << "kernel success.\n";
145
146  cuEventCreate(&stop, CU_EVENT_BLOCKING_SYNC);
147  cuEventRecord(stop,0);
148  cuEventSynchronize(stop);
149
150  cuEventElapsedTime(&elapsedTime, start, stop);
151  printf("GPU Kernel time : %f ms\n" ,elapsedTime);
152
153  // Retrieve device data
154  ulong * matchRslt;
155  if (posix_memalign((void**)&matchRslt, 32, outputSize)) {
156    std::cerr << "Cannot allocate memory for output.\n";
157    exit(-1);
158  }
159  checkCudaErrors(cuMemcpyDtoH(matchRslt, devBufferOutput, outputSize));
160
161  if (CountOnly){
162    int count = 0;
163    for (unsigned i = 0; i < GROUPTHREADS * numOfGroups; ++i) {
164      // std::cout << i << ":" << matchRslt[i] << "\n";
165      count += matchRslt[i];
166    }
167    std::cout << count << "\n";
168  }
169  else{
170    for(unsigned i=0; i<=numOfGroups; i++){
171      accumBytes[i] = startPoints[i] - divPoints[i];
172    }
173  }
174
175
176  // Clean-up
177  checkCudaErrors(cuMemFree(devBufferInput));
178  checkCudaErrors(cuMemFree(devStartPoints));
179  checkCudaErrors(cuMemFree(devBufferSizes));
180  checkCudaErrors(cuMemFree(devBufferOutput));
181  checkCudaErrors(cuModuleUnload(cudaModule));
182  checkCudaErrors(cuCtxDestroy(context));
183
184  return matchRslt;
185}
Note: See TracBrowser for help on using the repository browser.