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

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

editd GPU (bug unresolved).

File size: 4.9 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, "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, "Main"));
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(cuMemsetD8(devBufferInput, 0, bufferSize));
83  checkCudaErrors(cuMemAlloc(&devInputSize, sizeof(ulong)));
84  checkCudaErrors(cuMemAlloc(&devPatterns, patternLen));
85  checkCudaErrors(cuMemAlloc(&devBufferOutput, outputSize));
86  // checkCudaErrors(cuMemsetD8(devBufferOutput, 0, outputSize));
87  checkCudaErrors(cuMemAlloc(&devStrides, sizeof(int)));
88
89  //Copy from host to device
90  checkCudaErrors(cuMemcpyHtoD(devBufferInput, fileBuffer, bufferSize));
91  checkCudaErrors(cuMemcpyHtoD(devInputSize, &filesize, sizeof(ulong)));
92  checkCudaErrors(cuMemcpyHtoD(devPatterns, patternStr, patternLen));
93  checkCudaErrors(cuMemcpyHtoD(devStrides, &strides, sizeof(int)));
94
95  unsigned blockSizeX = GROUPTHREADS;
96  unsigned blockSizeY = 1;
97  unsigned blockSizeZ = 1;
98  unsigned gridSizeX  = GROUPBLOCKS;
99  unsigned gridSizeY  = 1;
100  unsigned gridSizeZ  = 1;
101
102  // Kernel parameters
103  void *KernelParams[] = { &devBufferInput, &devInputSize, &devPatterns, &devBufferOutput, &devStrides};
104
105  void *MergeKernelParams[] = {&devBufferOutput, &devStrides};
106
107  // std::cout << "Launching kernel\n";
108
109  CUevent start;
110  CUevent stop;
111  float elapsedTime;
112
113  cuEventCreate(&start, CU_EVENT_BLOCKING_SYNC);
114  cuEventRecord(start,0);
115
116  // Kernel launch
117  checkCudaErrors(cuLaunchKernel(function, gridSizeX, gridSizeY, gridSizeZ,
118                                 blockSizeX, blockSizeY, blockSizeZ,
119                                 0, NULL, KernelParams, NULL));
120
121  cuCtxSynchronize();
122
123  checkCudaErrors(cuLaunchKernel(mergefunction, dist+1, gridSizeY, gridSizeZ,
124                                 blockSizeX, blockSizeY, blockSizeZ,
125                                 0, NULL, MergeKernelParams, NULL));
126
127  cuEventCreate(&stop, CU_EVENT_BLOCKING_SYNC);
128  cuEventRecord(stop,0);
129  cuEventSynchronize(stop);
130
131  cuEventElapsedTime(&elapsedTime, start, stop);
132  printf("Elapsed time : %f ms\n" ,elapsedTime);
133
134  // Retrieve device data
135  ulong * matchRslt = (ulong *) malloc(outputSize/GROUPBLOCKS);
136  checkCudaErrors(cuMemcpyDtoH(matchRslt, devBufferOutput, outputSize/GROUPBLOCKS));
137
138  // Clean-up
139  checkCudaErrors(cuMemFree(devBufferInput));
140  checkCudaErrors(cuMemFree(devInputSize));
141  checkCudaErrors(cuMemFree(devBufferOutput));
142  checkCudaErrors(cuMemFree(devPatterns));
143  checkCudaErrors(cuMemFree(devStrides));
144  checkCudaErrors(cuModuleUnload(cudaModule));
145  checkCudaErrors(cuCtxDestroy(context));
146
147  return matchRslt;
148}
Note: See TracBrowser for help on using the repository browser.