source: icGREP/icgrep-devel/icgrep/editd/EditdCudaDriver.h @ 5402

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

edtid GPU optimization: merging result in GPU.

File size: 4.8 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 "cuda.h"
9
10#define GROUPTHREADS 64
11#define GROUPBLOCKS 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, const char * patternStr, unsigned patternLen, int dist) {
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 f_editd(PTXFilename);
44  if (!f_editd.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>(f_editd)), std::istreambuf_iterator<char>());
50
51  checkCudaErrors(cuCtxCreate(&context, 0, device));
52  checkCudaErrors(cuModuleLoadDataEx(&cudaModule, ptx_str.c_str(), 0, 0, 0));
53  checkCudaErrors(cuModuleGetFunction(&function, cudaModule, "GPU_Main"));
54
55  CUfunction  mergefunction;
56  CUmodule    mergeModule;
57  std::ifstream f_merge("merge.ptx");
58  if (!f_merge.is_open()) {
59    std::cerr << "Error: cannot open " << "merge.ptx" << " for processing. Skipped.\n";
60    exit(-1);
61  }
62 
63  std::string mergePTX((std::istreambuf_iterator<char>(f_merge)), std::istreambuf_iterator<char>());
64
65  checkCudaErrors(cuModuleLoadDataEx(&mergeModule, mergePTX.c_str(), 0, 0, 0));
66  checkCudaErrors(cuModuleGetFunction(&mergefunction, mergeModule, "mergeResult"));
67
68
69  // Device data
70  CUdeviceptr devBufferInput;
71  CUdeviceptr devInputSize;
72  CUdeviceptr devPatterns;
73  CUdeviceptr devBufferOutput;
74  CUdeviceptr devStrides;
75
76  int strideSize = GROUPTHREADS * sizeof(ulong) * 4;
77  int strides = filesize/(strideSize * 2) + 1;
78  int bufferSize = strides * strideSize;
79  int outputSize = sizeof(ulong) * GROUPTHREADS * strides * (dist + 1) * GROUPBLOCKS;
80
81  checkCudaErrors(cuMemAlloc(&devBufferInput, bufferSize));
82  checkCudaErrors(cuMemAlloc(&devInputSize, sizeof(ulong)));
83  checkCudaErrors(cuMemAlloc(&devPatterns, patternLen));
84  checkCudaErrors(cuMemAlloc(&devBufferOutput, outputSize));
85  // checkCudaErrors(cuMemsetD8(devBufferOutput, 0, outputSize));
86  checkCudaErrors(cuMemAlloc(&devStrides, sizeof(int)));
87
88  //Copy from host to device
89  checkCudaErrors(cuMemcpyHtoD(devBufferInput, fileBuffer, bufferSize));
90  checkCudaErrors(cuMemcpyHtoD(devInputSize, &filesize, sizeof(ulong)));
91  checkCudaErrors(cuMemcpyHtoD(devPatterns, patternStr, patternLen));
92  checkCudaErrors(cuMemcpyHtoD(devStrides, &strides, sizeof(int)));
93
94  unsigned blockSizeX = GROUPTHREADS;
95  unsigned blockSizeY = 1;
96  unsigned blockSizeZ = 1;
97  unsigned gridSizeX  = GROUPBLOCKS;
98  unsigned gridSizeY  = 1;
99  unsigned gridSizeZ  = 1;
100
101  // Kernel parameters
102  void *KernelParams[] = { &devBufferInput, &devInputSize, &devPatterns, &devBufferOutput, &devStrides};
103
104  void *MergeKernelParams[] = {&devBufferOutput, &devStrides};
105
106  // std::cout << "Launching kernel\n";
107
108  CUevent start;
109  CUevent stop;
110  float elapsedTime;
111
112  cuEventCreate(&start, CU_EVENT_BLOCKING_SYNC);
113  cuEventRecord(start,0);
114
115  // Kernel launch
116  checkCudaErrors(cuLaunchKernel(function, gridSizeX, gridSizeY, gridSizeZ,
117                                 blockSizeX, blockSizeY, blockSizeZ,
118                                 0, NULL, KernelParams, NULL));
119
120  cuCtxSynchronize();
121
122  checkCudaErrors(cuLaunchKernel(mergefunction, dist+1, gridSizeY, gridSizeZ,
123                                 blockSizeX, blockSizeY, blockSizeZ,
124                                 0, NULL, MergeKernelParams, NULL));
125
126  cuEventCreate(&stop, CU_EVENT_BLOCKING_SYNC);
127  cuEventRecord(stop,0);
128  cuEventSynchronize(stop);
129
130  cuEventElapsedTime(&elapsedTime, start, stop);
131  printf("Elapsed time : %f ms\n" ,elapsedTime);
132
133  // Retrieve device data
134  ulong * matchRslt = (ulong *) malloc(outputSize/GROUPBLOCKS);
135  checkCudaErrors(cuMemcpyDtoH(matchRslt, devBufferOutput, outputSize/GROUPBLOCKS));
136
137  // Clean-up
138  checkCudaErrors(cuMemFree(devBufferInput));
139  checkCudaErrors(cuMemFree(devInputSize));
140  checkCudaErrors(cuMemFree(devBufferOutput));
141  checkCudaErrors(cuMemFree(devPatterns));
142  checkCudaErrors(cuMemFree(devStrides));
143  checkCudaErrors(cuModuleUnload(cudaModule));
144  checkCudaErrors(cuCtxDestroy(context));
145
146  return matchRslt;
147}
Note: See TracBrowser for help on using the repository browser.