-
Notifications
You must be signed in to change notification settings - Fork 0
/
CUDAConvolutionDriver.cxx
555 lines (495 loc) · 14.6 KB
/
CUDAConvolutionDriver.cxx
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
/*
____ _ __ ____ __ ____
/ __/___(_) / ___ ____/ __ \__ _____ ___ / /_ / _/__ ____
_\ \/ __/ / _ \/ -_) __/ /_/ / // / -_|_-</ __/ _/ // _ \/ __/
/___/\__/_/_.__/\__/_/ \___\_\_,_/\__/___/\__/ /___/_//_/\__(_)
Copyright 2012 SciberQuest Inc.
*/
//#define CUDAConvolutionDriverDEBUG
#include "CUDAConvolutionDriver.h"
#include "CartesianExtent.h"
#include "SQMacros.h"
#include "postream.h"
#include "vtkDataArray.h"
#if defined SQTK_CUDA
#include "CUDAGlobalMemoryManager.hxx"
#include "CUDAConstMemoryManager.hxx"
#include "CUDANumerics.hxx"
#include "CUDAMacros.h"
#include <cuda.h>
#include <cuda_runtime.h>
#endif
#include <iostream>
using std::cerr;
using std::endl;
#include <vector>
using std::vector;
//-----------------------------------------------------------------------------
CUDAConvolutionDriver::CUDAConvolutionDriver()
:
NDevices(0),
DeviceId(0),
MaxThreads(1),
NThreads(1),
MaxBlocks(1),
NBlocks(1),
KernelMemoryType(CUDA_MEM_TYPE_GLOBAL),
InputMemoryType(CUDA_MEM_TYPE_GLOBAL),
WarpSize(32),
WarpsPerBlock(1),
MaxWarpsPerBlock(1)
{
int nDevs=0;
#if defined SQTK_CUDA
cudaGetDeviceCount(&nDevs);
#endif
this->NDevices=nDevs;
}
//-----------------------------------------------------------------------------
int CUDAConvolutionDriver::SetDeviceId(int deviceId)
{
#ifdef CUDAConvolutionDriverDEBUG
pCerr()
<< "===============CUDAConvolutionDriver::SetDeviceId" << endl
<< deviceId << endl;
#endif
#if defined SQTK_CUDA
this->DeviceId=deviceId;
cudaError_t ierr=cudaSetDevice(deviceId);
if (ierr)
{
CUDAErrorMacro(cerr,ierr,"Failed to select device " << deviceId);
return -1;
}
cudaDeviceProp props;
cudaGetDeviceProperties(&props, deviceId);
this->MaxThreads=props.maxThreadsPerBlock;
this->NThreads=props.maxThreadsPerBlock/2;
this->MaxBlocks=props.maxGridSize[0];
this->SetNumberOfBlocks(1024);
this->BlockGridMax[0]=props.maxGridSize[0];
this->BlockGridMax[1]=props.maxGridSize[1];
this->BlockGridMax[2]=props.maxGridSize[2];
this->WarpSize=props.warpSize;
this->MaxWarpsPerBlock=props.maxThreadsPerBlock/props.warpSize;
#endif
return 0;
}
//-----------------------------------------------------------------------------
int CUDAConvolutionDriver::Convolution(
CartesianExtent &extV,
CartesianExtent &extW,
CartesianExtent &extK,
int nGhost,
int mode,
vtkDataArray *V,
vtkDataArray *W,
float *K)
{
// TODO - make sure nothing is leaked if an error occurs!
#ifdef CUDAConvolutionDriverDEBUG
pCerr()
<< "===============CUDAConvolutionDriver::Convolution" << endl;
#endif
#if defined SQTK_CUDA
int nV[3];
extV.Size(nV);
unsigned long vnijk=extV.Size();
int nW[3];
extW.Size(nW);
unsigned long wnijk=extW.Size();
int nK[3];
extK.Size(nK);
unsigned long knijk=extK.Size();
CUDAMemoryManager<float> *devK;
if ( (this->KernelMemoryType==CUDA_MEM_TYPE_CONST)
&& (knijk*sizeof(float)<65536) )
{
#ifdef CUDAConvolutionDriverDEBUG
pCerr() << "Using constant memory for kernel" << endl;
#endif
devK=CUDAConstMemoryManager<float>::New("gK",K,knijk);
}
else
if (this->KernelMemoryType==CUDA_MEM_TYPE_TEX)
{
// TODO
sqErrorMacro(cerr,"Kernel texture memory is not implemented!");
return -1;
}
else
{
#ifdef CUDAConvolutionDriverDEBUG
pCerr() << "Using global memory for kernel" << endl;
#endif
devK=CUDAGlobalMemoryManager<float>::New(K,knijk);
}
unsigned long nComp=W->GetNumberOfComponents();
dim3 blockGrid(1,1,1);
dim3 threadGrid(1,1,1);
unsigned long nBlocks;
int ierr=PartitionBlocks(
wnijk,
this->WarpsPerBlock,
this->WarpSize,
this->BlockGridMax,
blockGrid,
nBlocks,
threadGrid);
if (ierr)
{
sqErrorMacro(pCerr(),"Failed to decompose domain for the GPU");
return -1;
}
int fastDim=0;
int slowDim=1;
switch (mode)
{
case CartesianExtent::DIM_MODE_2D_XY:
fastDim=0;
slowDim=1;
break;
case CartesianExtent::DIM_MODE_2D_XZ:
fastDim=0;
slowDim=2;
break;
case CartesianExtent::DIM_MODE_2D_YZ:
fastDim=1;
slowDim=2;
break;
case CartesianExtent::DIM_MODE_3D:
fastDim=0;
slowDim=1;
break;
default:
sqErrorMacro(cerr,"Bad dim mode.");
return -1;
}
#ifdef CUDAConvolutionDriverDEBUG
pCerr() << "wnijk=" << wnijk << endl;
pCerr() << "WarpsPerBlock=" << this->WarpsPerBlock << endl;
pCerr() << "WarpSize=" << this->WarpSize << endl;
pCerr() << "blockGridMaxMax=(" << this->BlockGridMax[0] << ", " << this->BlockGridMax[1] << ", " << this->BlockGridMax[2] << ")" << endl;
pCerr() << "blockGrid=(" << blockGrid.x << ", " << blockGrid.y << ", " << blockGrid.z << ")" << endl;
pCerr() << "nBlocks=" << nBlocks << endl;
pCerr() << "threadGrid=(" << threadGrid.x << ", " << threadGrid.y << ", " << threadGrid.z << ")" << endl;
pCerr() << "fastDim=" << fastDim << endl;
pCerr() << "slowDim=" << slowDim << endl;
pCerr() << "extV=" << extV << endl;
pCerr() << "nV=(" << nV[fastDim] << ", " << nV[slowDim] << ")" << endl;
pCerr() << "extW=" << extW << endl;
pCerr() << "nW=(" << nW[fastDim] << ", " << nW[slowDim] << ")" << endl;
#endif
switch (V->GetDataType())
{
// TODO -- replace with vtkTemplateMacro
case VTK_FLOAT:
{
//int worldRank;
//MPI_Comm_rank(MPI_COMM_WORLD,&worldRank);
cudaError_t uerr;
// allocate device memory for vector components
vector<float*> sV(nComp,0);
vector<CUDAGlobalMemoryManager<float>*> devV(nComp,0);
vector<CUDAGlobalMemoryManager<float>*> devW(nComp,0);
vector<float*> sW(nComp,0);
for (int q=0; q<nComp; ++q)
{
// input arrays
cudaHostAlloc(
&sV[q],
vnijk*sizeof(float),
cudaHostAllocDefault);
if (this->InputMemoryType==CUDA_MEM_TYPE_TEX)
{
// use texture memory for input
if ((mode==CartesianExtent::DIM_MODE_2D_XY)
||(mode==CartesianExtent::DIM_MODE_2D_XZ)
||(mode==CartesianExtent::DIM_MODE_2D_YZ))
{
devV[q]
= CUDAGlobalMemoryManager<float>::New(
nV[fastDim],
nV[slowDim]);
}
else
{
// TODO
sqErrorMacro(cerr,"3D Texture kernel is not implemented!");
return -1;
}
}
else
{
// use global memory for input
devV[q]=CUDAGlobalMemoryManager<float>::New(vnijk);
}
// output array
devW[q]=CUDAGlobalMemoryManager<float>::New(wnijk);
cudaHostAlloc(
&sW[q],
wnijk*sizeof(float),
cudaHostAllocDefault);
/*
// fill device data with falt index for lookup validation
for (int i=0; i<vnijk; ++i)
{
sV[q][i]=i;
}
*/
}
// convert vtk vectors/tensors into scalar component arrays
float *hV=(float*)V->GetVoidPointer(0);
Split<float>(vnijk,hV,sV);
// copy the input arrays to the device
for (int q=0; q<nComp; ++q)
{
// TODO-could this be streamed to overlap com w/ comp?
devV[q]->Push(sV[q]);
// execute the kernel
if (this->InputMemoryType==CUDA_MEM_TYPE_TEX)
{
#ifdef CUDAConvolutionDriverDEBUG
pCerr() << "Using texture memory for input" << endl;
#endif
if ((mode==CartesianExtent::DIM_MODE_2D_XY)
||(mode==CartesianExtent::DIM_MODE_2D_XZ)
||(mode==CartesianExtent::DIM_MODE_2D_YZ))
{
// setup texture
ierr=devV[q]->BindToTexture(&gV2);
CUDAScalarConvolution2D<<<blockGrid,threadGrid>>>(
//worldRank,
nV[fastDim],
nW[fastDim],
wnijk,
nK[fastDim],
nGhost,
devW[q]->GetDevicePointer(),
devK->GetDevicePointer());
}
else
{
// TODO
sqErrorMacro(cerr,"3D Texture kernel is not implemented!");
return -1;
}
uerr=cudaGetLastError();
if (uerr)
{
CUDAErrorMacro(pCerr(),uerr,"Error invoking 2D kernel.");
return -1;
}
// release texture
cudaDeviceSynchronize();
//cudaUnbindTexture(gV2);
}
else
{
#ifdef CUDAConvolutionDriverDEBUG
pCerr() << "Using global memory for input" << endl;
#endif
if ((mode==CartesianExtent::DIM_MODE_2D_XY)
||(mode==CartesianExtent::DIM_MODE_2D_XZ)
||(mode==CartesianExtent::DIM_MODE_2D_YZ))
{
CUDAScalarConvolution2D<<<blockGrid,threadGrid>>>(
//worldRank,
nV[fastDim],
nW[fastDim],
wnijk,
nK[fastDim],
nGhost,
devV[q]->GetDevicePointer(),
devW[q]->GetDevicePointer(),
devK->GetDevicePointer());
}
else
{
CUDAScalarConvolution3D<<<blockGrid,threadGrid>>>(
nV[fastDim],
nV[fastDim]*nV[slowDim],
nW[fastDim],
nW[fastDim]*nW[slowDim],
wnijk,
nK[fastDim],
nK[fastDim]*nK[slowDim],
nGhost,
devV[q]->GetDevicePointer(),
devW[q]->GetDevicePointer(),
devK->GetDevicePointer());
}
uerr=cudaGetLastError();
if (uerr)
{
CUDAErrorMacro(pCerr(),uerr,"Error invoking global memory kernel.");
return -1;
}
}
// retreive the output/result
devW[q]->Pull(sW[q]);
}
// put results in vtk order
float *hW=(float*)W->GetVoidPointer(0);
Interleave(wnijk,sW,hW);
// clean up
for (int q=0; q<nComp; ++q)
{
delete devW[q];
cudaFreeHost(sW[q]);
delete devV[q];
cudaFreeHost(sV[q]);
}
cudaUnbindTexture(gV2);
}
break;
default:
// TODO
sqErrorMacro(cerr,"Not currently using vtkTemplateMacro");
return -1;
}
delete devK;
// TODO if kernel is in texture mem, unbind
#endif
return 0;
}
/*
//-----------------------------------------------------------------------------
void CUDAConvolutionDriver::Convolution(
CartesianExtent &extV,
CartesianExtent &extW,
CartesianExtent &extK,
int ghostV,
int mode,
vtkDataArray *V,
vtkDataArray *W,
float *K)
{
pCerr()
<< "===============CUDAConvolutionDriver::Convolution" << endl
<< "NBlocks=" << this->NBlocks << endl
<< "NThreads=" << this->NThreads << endl;
CUDAFlatIndex idxV(extV,mode);
CUDATupleIndex tupW(extW,ghostV,mode);
CUDAFlatIndex idxK(extK,mode);
int compW=W->GetNumberOfComponents();
unsigned long sizeW=W->GetNumberOfTuples();
CUDAMemoryManager<int> *devExtK
= CUDAMemoryManager<int>::New(extK.GetData(),6);
devExtK->Push();
CUDAMemoryManager<float> *devK
= CUDAMemoryManager<float>::New(K,extK.Size());
devK->Push();
switch (V->GetDataType())
{
case VTK_FLOAT:
{
CUDAMemoryManager<float> *devV
= CUDAMemoryManager<float>::New(V);
devV->Push();
CUDAMemoryManager<float> *devW
= CUDAMemoryManager<float>::New(W);
//devW->Zero(); bug in cuda!
//devW->Push();
cerr << "Calling" << endl;
::CUDAConvolution<float><<<this->NBlocks,this->NThreads>>>(
idxV.GetDevicePointer(),
devV->GetDevicePointer(),
tupW.GetDevicePointer(),
sizeW,
compW,
devW->GetDevicePointer(),
devExtK->GetDevicePointer(),
idxK.GetDevicePointer(),
devK->GetDevicePointer());
cudaError_t ierr=cudaGetLastError();
if (ierr)
{
CUDAErrorMacro(pCerr(),ierr,"Kernel fialed to run.");
}
devW->Pull();
delete devW;
delete devV;
}
break;
};
delete devExtK;
delete devK;
cerr << "Finished." << endl;
}
//-----------------------------------------------------------------------------
void CUDAConvolutionDriver::Convolution3D(
CartesianExtent &extV,
CartesianExtent &extW,
CartesianExtent &extK,
int nGhost,
int mode,
vtkDataArray *V,
vtkDataArray *W,
float *K)
{
// block decomp
int wSize[3];
extW.Size(wSize);
dim3 blockDecomp(wSize[0],wSize[1],wSize[2]);
// thread decomp
int nCompV=V->GetNumberOfComponents();
// translation between output and input index tuple
int nGhostV[3]={0};
CartesianExtent::Shift(nGhostV,nGhost,mode);
CUDAMemoryManager<int> *devNGhostV
= CUDAMemoryManager<int>::New(nGhostV,3);
devNGhostV->Push();
//
CUDAFlatIndex idxV(extV,mode);
CUDAFlatIndex idxW(extW,mode);
CUDAFlatIndex idxK(extK,mode);
CUDAMemoryManager<int> *devExtK
= CUDAMemoryManager<int>::New(extK.GetData(),6);
devExtK->Push();
CUDAMemoryManager<float> *devK
= CUDAMemoryManager<float>::New(K,extK.Size());
devK->Push();
pCerr()
<< "===============CUDAConvolutionDriver::Convolution3D" << endl
<< "NBlocks=" << wSize[0] << ", " << wSize[1] << ", " << wSize[2] << endl
<< "NThreads=" << nCompV << endl;
switch (V->GetDataType())
{
case VTK_FLOAT:
{
CUDAMemoryManager<float> *devV
= CUDAMemoryManager<float>::New(V);
devV->Push();
CUDAMemoryManager<float> *devW
= CUDAMemoryManager<float>::New(W);
//devW->Zero(); bug in cuda! not all bytes are zerod
//devW->Push();
cerr << "Calling" << endl;
::CUDAConvolution<float><<<blockDecomp,nCompV>>>(
idxV.GetDevicePointer(),
nCompV,
devNGhostV->GetDevicePointer(),
devV->GetDevicePointer(),
idxW.GetDevicePointer(),
devW->GetDevicePointer(),
devExtK->GetDevicePointer(),
idxK.GetDevicePointer(),
devK->GetDevicePointer());
cudaThreadSynchronize();
cudaError_t ierr=cudaGetLastError();
if (ierr!=cudaSuccess)
{
CUDAErrorMacro(pCerr(),ierr,"Kernel fialed.");
}
devW->Pull();
delete devW;
delete devV;
}
break;
};
delete devK;
delete devExtK;
cerr << "Finished." << endl;
}
*/