-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathCallCubin.cpp
More file actions
91 lines (78 loc) · 2.12 KB
/
Copy pathCallCubin.cpp
File metadata and controls
91 lines (78 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// This file is a part of RCKangaroo software
// (c) 2024, RetiredCoder (RC)
// License: GPLv3, see "LICENSE.TXT" file
// https://github.com/RetiredC
#include "CallCubin.h"
#include <stdio.h>
#pragma comment(lib, "cuda.lib")
#pragma warning(disable : 4996)
TCubinCall::TCubinCall()
{
cuModule = NULL;
}
TCubinCall::~TCubinCall()
{
}
bool TCubinCall::LoadCubin(const char* fn)
{
CUresult res = ::cuModuleLoad(&cuModule, fn);
if (res != cudaSuccess)
{
printf("srv cuModuleLoad Error: %d\n", res);
return false;
}
return true;
}
bool TCubinCall::CallKernel(TCallKernelParams params)
{
dim3 gridDim, blockDim;
gridDim.x = params.blockCnt; gridDim.y = 1; gridDim.z = 1;
blockDim.x = params.blockSize; blockDim.y = 1; blockDim.z = 1;
int ArgCnt = 1;
void** args = (void**)malloc(8 * ArgCnt);
if (!args)
return false;
for (int i = 0; i < ArgCnt; i++)
args[i] = params.kernel_param_ptr;
CUfunction f = NULL;
CUresult res = ::cuModuleGetFunction(&f, cuModule, params.kernel_name);
if (res != CUDA_SUCCESS)
{
free(args);
printf("cuModuleGetFunction failed, err %d\r\n", res);
return false;
}
CUresult err = ::cuFuncSetAttribute(f, CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES, params.sharedSize);
if (err != cudaSuccess)
{
free(args);
printf("cudaFuncSetAttribute failed, err %d\r\n", err);
return false;
}
res = ::cuLaunchKernel(f, gridDim.x, gridDim.y, gridDim.z, blockDim.x, blockDim.y, blockDim.z, params.sharedSize, params.stream, args, NULL);
free(args);
if (res != CUDA_SUCCESS)
{
printf("cuLaunchKernel failed, err %d\r\n", res);
return false;
}
return true;
}
bool TCubinCall::CopyToSymbol(const char* sym_name, void* data, int size)
{
CUdeviceptr dptr = 0;
size_t symBytes = 0;
CUresult res = cuModuleGetGlobal(&dptr, &symBytes, cuModule, sym_name);
if (res != CUDA_SUCCESS)
{
printf("cuModuleGetGlobal failed, err %d\r\n", res);
return false;
}
res = cuMemcpyHtoD(dptr, data, size);
if (res != CUDA_SUCCESS)
{
printf("cuMemcpyHtoD failed, err %d\r\n", res);
return false;
}
return true;
}