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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
|
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of NVIDIA CORPORATION nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Copyright (c) 2018 NVIDIA Corporation. All rights reserved.
#ifndef APEX_CUDA_DEFS_H
#define APEX_CUDA_DEFS_H
#include <cuda.h>
const unsigned int MAX_CONST_MEM_SIZE = 65536;
const unsigned int APEX_CUDA_MEM_ALIGNMENT = 256;
const unsigned int APEX_CUDA_TEX_MEM_ALIGNMENT = 512;
const unsigned int MAX_SMEM_BANKS = 32;
#define APEX_CUDA_ALIGN_UP(value, alignment) (((value) + (alignment)-1) & ~((alignment)-1))
#define APEX_CUDA_MEM_ALIGN_UP_32BIT(count) APEX_CUDA_ALIGN_UP(count, APEX_CUDA_MEM_ALIGNMENT >> 2)
const unsigned int LOG2_WARP_SIZE = 5;
const unsigned int WARP_SIZE = (1U << LOG2_WARP_SIZE);
//if you would like to make this value larger than 32 for future GPUs,
//then you'll need to fix some kernels (like reduce and scan) to support more than 32 warps per block!!!
const unsigned int MAX_WARPS_PER_BLOCK = 32;
const unsigned int MAX_THREADS_PER_BLOCK = (MAX_WARPS_PER_BLOCK << LOG2_WARP_SIZE);
//uncomment this line to force bound kernels to use defined number of CTAs
//#define APEX_CUDA_FORCED_BLOCKS 80
namespace nvidia
{
namespace apex
{
struct ApexCudaMemFlags
{
enum Enum
{
UNUSED = 0,
IN = 0x01,
OUT = 0x02,
IN_OUT = IN | OUT
};
};
#ifndef __CUDACC__
class ApexCudaArray : public UserAllocated
{
PX_NOCOPY(ApexCudaArray)
void init()
{
switch (mDesc.Format)
{
case CU_AD_FORMAT_UNSIGNED_INT8:
case CU_AD_FORMAT_SIGNED_INT8:
mElemSize = 1;
break;
case CU_AD_FORMAT_UNSIGNED_INT16:
case CU_AD_FORMAT_SIGNED_INT16:
case CU_AD_FORMAT_HALF:
mElemSize = 2;
break;
case CU_AD_FORMAT_UNSIGNED_INT32:
case CU_AD_FORMAT_SIGNED_INT32:
case CU_AD_FORMAT_FLOAT:
mElemSize = 4;
break;
default:
PX_ALWAYS_ASSERT();
mElemSize = 0;
break;
};
mElemSize *= mDesc.NumChannels;
}
public:
ApexCudaArray() : mCuArray(NULL), mHasOwnership(false), mElemSize(0) {}
~ApexCudaArray() { release(); }
void assign(CUarray cuArray, bool bTakeOwnership)
{
release();
mCuArray = cuArray;
mHasOwnership = bTakeOwnership;
CUT_SAFE_CALL(cuArray3DGetDescriptor(&mDesc, mCuArray));
init();
}
void create(CUDA_ARRAY3D_DESCRIPTOR desc)
{
if (mCuArray != NULL && mHasOwnership &&
mDesc.Width == desc.Width && mDesc.Height == desc.Height && mDesc.Depth == desc.Depth &&
mDesc.Format == desc.Format && mDesc.NumChannels == desc.NumChannels && mDesc.Flags == desc.Flags)
{
return;
}
release();
// Allocate CUDA 3d array in device memory
mDesc = desc;
CUT_SAFE_CALL(cuArray3DCreate(&mCuArray, &mDesc));
mHasOwnership = true;
init();
}
void create(CUarray_format format, unsigned int numChannels, unsigned int width, unsigned int height, unsigned int depth = 0, bool surfUsage = false)
{
CUDA_ARRAY3D_DESCRIPTOR desc;
desc.Format = format;
desc.NumChannels = numChannels;
desc.Width = width;
desc.Height = height;
desc.Depth = depth;
desc.Flags = surfUsage ? CUDA_ARRAY3D_SURFACE_LDST : 0u;
create(desc);
}
void release()
{
if (mCuArray != NULL)
{
if (mHasOwnership)
{
CUT_SAFE_CALL(cuArrayDestroy(mCuArray));
}
mCuArray = NULL;
mHasOwnership = false;
mElemSize = 0;
}
}
void copyToHost(CUstream stream, void* dstHost, size_t dstPitch = 0, size_t dstHeight = 0,
size_t copyWidth = 0, size_t copyHeight = 0, size_t copyDepth = 0)
{
if (mDesc.Width > 0)
{
if (mDesc.Height > 0)
{
if (mDesc.Depth > 0)
{
//3D
CUDA_MEMCPY3D copyDesc;
copyDesc.WidthInBytes = size_t(copyWidth ? copyWidth : mDesc.Width) * mElemSize;
copyDesc.Height = copyHeight ? copyHeight : mDesc.Height;
copyDesc.Depth = copyDepth ? copyDepth : mDesc.Depth;
copyDesc.srcXInBytes = copyDesc.srcY = copyDesc.srcZ = copyDesc.srcLOD = 0;
copyDesc.srcMemoryType = CU_MEMORYTYPE_ARRAY;
copyDesc.srcArray = mCuArray;
copyDesc.dstXInBytes = copyDesc.dstY = copyDesc.dstZ = copyDesc.dstLOD = 0;
copyDesc.dstMemoryType = CU_MEMORYTYPE_HOST;
copyDesc.dstHost = dstHost;
copyDesc.dstPitch = (dstPitch > 0) ? dstPitch : copyDesc.WidthInBytes;
copyDesc.dstHeight = (dstHeight > 0) ? dstHeight : copyDesc.Height;
CUT_SAFE_CALL(cuMemcpy3DAsync(©Desc, stream));
}
else
{
//2D
CUDA_MEMCPY2D copyDesc;
copyDesc.WidthInBytes = size_t(copyWidth ? copyWidth : mDesc.Width) * mElemSize;
copyDesc.Height = copyHeight ? copyHeight : mDesc.Height;
copyDesc.srcXInBytes = copyDesc.srcY = 0;
copyDesc.srcMemoryType = CU_MEMORYTYPE_ARRAY;
copyDesc.srcArray = mCuArray;
copyDesc.dstXInBytes = copyDesc.dstY = 0;
copyDesc.dstMemoryType = CU_MEMORYTYPE_HOST;
copyDesc.dstHost = dstHost;
copyDesc.dstPitch = (dstPitch > 0) ? dstPitch : copyDesc.WidthInBytes;
CUT_SAFE_CALL(cuMemcpy2DAsync(©Desc, stream));
}
}
else
{
//1D
CUT_SAFE_CALL(cuMemcpyAtoHAsync(dstHost, mCuArray, 0, size_t(copyWidth ? copyWidth : mDesc.Width) * mElemSize, stream));
}
}
}
void copyFromHost(CUstream stream, const void* srcHost, size_t srcPitch = 0, size_t srcHeight = 0)
{
if (mDesc.Width > 0)
{
if (mDesc.Height > 0)
{
if (mDesc.Depth > 0)
{
//3D
CUDA_MEMCPY3D copyDesc;
copyDesc.WidthInBytes = size_t(mDesc.Width) * mElemSize;
copyDesc.Height = mDesc.Height;
copyDesc.Depth = mDesc.Depth;
copyDesc.srcXInBytes = copyDesc.srcY = copyDesc.srcZ = copyDesc.srcLOD = 0;
copyDesc.srcMemoryType = CU_MEMORYTYPE_HOST;
copyDesc.srcHost = srcHost;
copyDesc.srcPitch = (srcPitch > 0) ? srcPitch : copyDesc.WidthInBytes;
copyDesc.srcHeight = (srcHeight > 0) ? srcHeight : copyDesc.Height;
copyDesc.dstXInBytes = copyDesc.dstY = copyDesc.dstZ = copyDesc.dstLOD = 0;
copyDesc.dstMemoryType = CU_MEMORYTYPE_ARRAY;
copyDesc.dstArray = mCuArray;
CUT_SAFE_CALL(cuMemcpy3DAsync(©Desc, stream));
}
else
{
//2D
CUDA_MEMCPY2D copyDesc;
copyDesc.WidthInBytes = size_t(mDesc.Width) * mElemSize;
copyDesc.Height = mDesc.Height;
copyDesc.srcXInBytes = copyDesc.srcY = 0;
copyDesc.srcMemoryType = CU_MEMORYTYPE_HOST;
copyDesc.srcHost = srcHost;
copyDesc.srcPitch = (srcPitch > 0) ? srcPitch : copyDesc.WidthInBytes;
copyDesc.dstXInBytes = copyDesc.dstY = 0;
copyDesc.dstMemoryType = CU_MEMORYTYPE_ARRAY;
copyDesc.dstArray = mCuArray;
CUT_SAFE_CALL(cuMemcpy2DAsync(©Desc, stream));
}
}
else
{
//1D
CUT_SAFE_CALL(cuMemcpyHtoAAsync(mCuArray, 0, srcHost, size_t(mDesc.Width) * mElemSize, stream));
}
}
}
void copyToArray(CUstream stream, CUarray dstArray)
{
//copy array to array
CUDA_MEMCPY3D desc;
desc.srcXInBytes = desc.srcY = desc.srcZ = desc.srcLOD = 0;
desc.srcMemoryType = CU_MEMORYTYPE_ARRAY;
desc.srcArray = mCuArray;
desc.dstXInBytes = desc.dstY = desc.dstZ = desc.dstLOD = 0;
desc.dstMemoryType = CU_MEMORYTYPE_ARRAY;
desc.dstArray = dstArray;
desc.WidthInBytes = size_t(mDesc.Width) * mElemSize;
desc.Height = mDesc.Height;
desc.Depth = mDesc.Depth;
CUT_SAFE_CALL(cuMemcpy3DAsync(&desc, stream));
}
PX_INLINE CUarray getCuArray() const
{
return mCuArray;
}
PX_INLINE bool isValid() const
{
return (mCuArray != NULL);
}
PX_INLINE unsigned int getWidth() const { return (unsigned int)mDesc.Width; }
PX_INLINE unsigned int getHeight() const { return (unsigned int)mDesc.Height; }
PX_INLINE unsigned int getDepth() const { return (unsigned int)mDesc.Depth; }
PX_INLINE CUarray_format getFormat() const { return mDesc.Format; }
PX_INLINE unsigned int getNumChannels() const { return mDesc.NumChannels; }
PX_INLINE bool hasOwnership() const { return mHasOwnership; }
PX_INLINE const CUDA_ARRAY3D_DESCRIPTOR& getDesc() const { return mDesc; }
PX_INLINE size_t getByteSize() const
{
size_t size = mDesc.Width * mElemSize;
if (mDesc.Height > 0) size *= mDesc.Height;
if (mDesc.Depth > 0) size *= mDesc.Depth;
return size;
}
private:
CUarray mCuArray;
bool mHasOwnership;
unsigned int mElemSize;
CUDA_ARRAY3D_DESCRIPTOR mDesc;
};
#endif //__CUDACC__
}
} // end namespace nvidia::apex
#endif //APEX_CUDA_DEFS_H
|