-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcuda_kernel.cpp
More file actions
186 lines (137 loc) · 4.19 KB
/
cuda_kernel.cpp
File metadata and controls
186 lines (137 loc) · 4.19 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include "cuda_kernel.h"
#include "cuda_event.h"
#include "cuda_program.h"
using namespace CUDA;
//
/*!
* Constructs a new kernel object from the given program with the given kernel name.
*
* @param program Pointer to the program which has built the kernel with the given
* name that is created.
*
* @param name The name of the kernel that is created.
*/
Kernel::Kernel(Program* program, const QString& name)
{
// Create a new CUDA kernel from the given program with the given name, storing
// its id to this object. If creating the kernel fails then throw an exception.
CUresult result = cuModuleGetFunction(&_kernel, program->id(), name.toLatin1().data());
if ( result != CUDA_SUCCESS )
{
E_MAKE_EXCEPTION(e);
throwError(&e,result);
}
}
/*!
* Executes this object's CUDA kernel on the given stream with the global sizes,
* local sizes, and arguments of this object, returning the event for the kernel
* command.
*
* @param stream CUDA stream on which to execute this kernel.
*
* @return The event for the kernel command running on the given stream.
*/
Event Kernel::execute(const Stream& stream)
{
// Launch this kernel on the given stream using this object's work sizes and
// kernel arguments. If the kernel launch fails then throw an exception.
CUresult result
{
cuLaunchKernel(_kernel
,_gridDim.x
,_gridDim.y
,_gridDim.z
,_blockDim.x
,_blockDim.y
,_blockDim.z
,_sharedMemBytes
,stream.id()
,_args.data()
,nullptr)
};
if ( result != CUDA_SUCCESS )
{
E_MAKE_EXCEPTION(e);
throwError(&e,result);
}
// Record the enqueued kernel launch to an event and return the event.
Event event;
event.record(stream);
return event;
}
/*!
* Returns an attribute of this CUDA kernel. Refer to the CUDA Toolkit Documentation
* for more information on the available kernel attributes.
*
* @param attribute Enumerated value for a CUDA kernel attribute.
*/
int Kernel::getAttribute(CUfunction_attribute attribute) const
{
// Get the value of the given kernel attribute. If this command fails then
// throw an exception.
int value;
CUresult result = cuFuncGetAttribute(&value, attribute, _kernel);
if ( result != CUDA_SUCCESS )
{
E_MAKE_EXCEPTION(e);
throwError(&e,result);
}
return value;
}
/*!
* Sets an attribute of this CUDA kernel. Refer to the CUDA Toolkit Documentation
* for more information on the available kernel attributes.
*
* @param attribute Enumerated value for a CUDA kernel attribute.
*
* @param value The value to set the given attribute to.
*/
void Kernel::setAttribute(CUfunction_attribute attribute, int value)
{
// Set the value of the given kernel attribute. If this command fails then
// throw an exception.
CUresult result = cuFuncSetAttribute(_kernel, attribute, value);
if ( result != CUDA_SUCCESS )
{
E_MAKE_EXCEPTION(e);
throwError(&e,result);
}
}
/*!
* Sets the global and local work sizes used for parallel execution of this kernel
* object. Each work size can be one-, two-, or three-dimensional.
*
* @param globalSize Global work size, also known as grid size.
*
* @param localSize Local work size. also known as block size.
*/
void Kernel::setSizes(dim3 globalSize, dim3 localSize)
{
_gridDim = globalSize;
_blockDim = localSize;
}
/*!
* Sets the amount of dynamic shared memory per thread block to allocate.
*
* @param size Dynamic shared memory size per thread block in bytes.
*/
void Kernel::setSharedMemory(unsigned int size)
{
_sharedMemBytes = size;
}
/*!
* Get the occupancy of this kernel for a given block size.
*
* @param blockSize Number of threads per block.
*/
int Kernel::getMaxActiveBlocksPerMultiprocessor(int blockSize) const
{
int numBlocks;
CUresult result = cuOccupancyMaxActiveBlocksPerMultiprocessor(&numBlocks, _kernel, blockSize, _sharedMemBytes);
if ( result != CUDA_SUCCESS )
{
E_MAKE_EXCEPTION(e);
throwError(&e,result);
}
return numBlocks;
}