/
itkVkCommon.cxx
580 lines (533 loc) · 22.5 KB
/
itkVkCommon.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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#include "itkVkCommon.h"
#include "itkVkDefinitions.h"
#include "vkFFT.h"
#include "itkMacro.h"
#include <complex>
#include <iostream>
#include <memory>
namespace itk
{
VkFFTResult
VkCommon::Run(const VkGPU & vkGPU, const VkParameters & vkParameters)
{
VkFFTResult resFFT{ VKFFT_SUCCESS };
m_VkGPU = vkGPU;
m_VkParameters = vkParameters;
if (m_MustConfigure || m_VkGPU != m_VkGPUPrevious || m_VkParameters != m_VkParametersPrevious)
{
resFFT = this->ReleaseBackend();
if (resFFT != VKFFT_SUCCESS)
{
return resFFT;
}
resFFT = this->ConfigureBackend();
if (resFFT != VKFFT_SUCCESS)
{
return resFFT;
}
this->m_MustConfigure = false;
}
resFFT = this->PerformFFT();
if (resFFT != VKFFT_SUCCESS)
{
return resFFT;
}
return resFFT;
}
VkFFTResult
VkCommon::ConfigureBackend()
{
VkFFTResult resFFT{ VKFFT_SUCCESS };
#if (VKFFT_BACKEND == CUDA)
CUresult res{ CUDA_SUCCESS };
cudaError_t res2{ cudaSuccess };
res = cuInit(0);
if (res != CUDA_SUCCESS)
return VkFFTResult{ VKFFT_ERROR_FAILED_TO_INITIALIZE };
res2 = cudaSetDevice((int)m_VkGPU.device_id);
if (res2 != cudaSuccess)
return VkFFTResult{ VKFFT_ERROR_FAILED_TO_SET_DEVICE_ID };
res = cuDeviceGet(&m_VkGPU.device, (int)m_VkGPU.device_id);
if (res != CUDA_SUCCESS)
return VkFFTResult{ VKFFT_ERROR_FAILED_TO_GET_DEVICE };
res = cuCtxCreate(&m_VkGPU.context, 0, (int)m_VkGPU.device);
if (res != CUDA_SUCCESS)
return VkFFTResult{ VKFFT_ERROR_FAILED_TO_CREATE_CONTEXT };
#elif (VKFFT_BACKEND == OPENCL)
cl_int resCL{ CL_SUCCESS };
// Begin code that mimics launchVkFFT from VkFFT/Vulkan_FFT.cpp, though just the OpenCL part.
cl_uint numPlatforms;
resCL = clGetPlatformIDs(0, nullptr, &numPlatforms);
if (resCL != CL_SUCCESS)
{
std::cerr << __FILE__ "(" << __LINE__ << "): clGetPlatformIDs returned " << resCL << std::endl;
return VkFFTResult{ VKFFT_ERROR_FAILED_TO_INITIALIZE };
}
std::unique_ptr<cl_platform_id[]> platformsArray{ std::make_unique<cl_platform_id[]>(numPlatforms) };
cl_platform_id * platforms{ &platformsArray[0] };
if (!platforms)
return VkFFTResult{ VKFFT_ERROR_MALLOC_FAILED };
resCL = clGetPlatformIDs(numPlatforms, platforms, nullptr);
if (resCL != CL_SUCCESS)
{
std::cerr << __FILE__ "(" << __LINE__ << "): clGetPlatformIDs returned " << resCL << std::endl;
return VkFFTResult{ VKFFT_ERROR_FAILED_TO_INITIALIZE };
}
uint64_t k{ 0 };
for (uint64_t j{ 0 }; j < numPlatforms; j++)
{
cl_uint numDevices;
resCL = clGetDeviceIDs(platforms[j], CL_DEVICE_TYPE_ALL, 0, nullptr, &numDevices);
std::unique_ptr<cl_device_id[]> deviceListArray{ std::make_unique<cl_device_id[]>(numDevices) };
cl_device_id * deviceList{ &deviceListArray[0] };
if (!deviceList)
return VkFFTResult{ VKFFT_ERROR_MALLOC_FAILED };
resCL = clGetDeviceIDs(platforms[j], CL_DEVICE_TYPE_ALL, numDevices, deviceList, nullptr);
if (resCL != CL_SUCCESS)
{
std::cerr << __FILE__ "(" << __LINE__ << "): clGetDeviceIDs returned " << resCL << std::endl;
return VkFFTResult{ VKFFT_ERROR_FAILED_TO_GET_DEVICE };
}
for (uint64_t i{ 0 }; i < numDevices; i++)
{
if (k == m_VkGPU.device_id)
{
m_VkGPU.platform = platforms[j];
m_VkGPU.device = deviceList[i];
m_VkGPU.context = clCreateContext(NULL, 1, &m_VkGPU.device, NULL, NULL, &resCL);
if (resCL != CL_SUCCESS)
{
std::cerr << __FILE__ "(" << __LINE__ << "): clCreateContext returned " << resCL << std::endl;
return VkFFTResult{ VKFFT_ERROR_FAILED_TO_CREATE_CONTEXT };
}
m_VkGPU.commandQueue = clCreateCommandQueue(m_VkGPU.context, m_VkGPU.device, 0, &resCL);
if (resCL != CL_SUCCESS)
{
std::cerr << __FILE__ "(" << __LINE__ << "): clCreateCommandQueue returned " << resCL << std::endl;
return VkFFTResult{ VKFFT_ERROR_FAILED_TO_CREATE_COMMAND_QUEUE };
}
k++;
}
else
{
k++;
}
}
}
#endif
// Proceed by doing something similar to user_benchmark_VkFFT from
// VkFFT/benchmark_scripts/vkFFT_scripts/src/user_benchmark_VkFFT.cpp, but without file_output and
// output.
m_VkFFTConfiguration.size[0] = std::max(m_VkParameters.X, (decltype(m_VkParameters.X))1);
m_VkFFTConfiguration.size[1] = std::max(m_VkParameters.Y, (decltype(m_VkParameters.Y))1);
m_VkFFTConfiguration.size[2] = std::max(m_VkParameters.Z, (decltype(m_VkParameters.Z))1);
m_VkFFTConfiguration.FFTdim = 3;
if (m_VkFFTConfiguration.size[2] == 1)
{
--m_VkFFTConfiguration.FFTdim;
if (m_VkFFTConfiguration.size[1] == 1)
{
--m_VkFFTConfiguration.FFTdim;
}
}
m_VkFFTConfiguration.numberBatches = m_VkParameters.B;
m_VkFFTConfiguration.performR2C = m_VkParameters.fft == FFTEnum::C2C ? 0 : 1;
if (m_VkParameters.P == PrecisionEnum::DOUBLE)
{
m_VkFFTConfiguration.doublePrecision = 1;
}
for (size_t dim{ 0 }; dim < 3; ++dim)
{
m_VkFFTConfiguration.omitDimension[dim] = m_VkParameters.omitDimension[dim];
}
// if (m_VkParameters.P == HALF)
// m_VkFFTConfiguration.halfPrecision = 1;
m_VkFFTConfiguration.normalize = m_VkParameters.normalized == NormalizationEnum::NORMALIZED ? 1 : 0;
// After this, configuration file contains pointers to Vulkan objects needed to work with the GPU: VkDevice* device
// - created device, [uint64_t *bufferSize, VkBuffer *buffer, VkDeviceMemory* bufferDeviceMemory] - allocated GPU
// memory FFT is performed on. [uint64_t *kernelSize, VkBuffer *kernel, VkDeviceMemory* kernelDeviceMemory] -
// allocated GPU memory, where kernel for convolution is stored.
m_VkFFTConfiguration.device = &m_VkGPU.device;
#if (VKFFT_BACKEND == CUDA)
// pass
#elif (VKFFT_BACKEND == OPENCL)
m_VkFFTConfiguration.platform = &m_VkGPU.platform;
m_VkFFTConfiguration.context = &m_VkGPU.context;
#endif
m_VkFFTConfiguration.makeInversePlanOnly = (m_VkParameters.I == DirectionEnum::INVERSE);
m_VkFFTConfiguration.makeForwardPlanOnly = (m_VkParameters.I == DirectionEnum::FORWARD);
if (m_VkParameters.fft == FFTEnum::C2C)
{
// For C2C computation we can do everything in the in-place-computation buffer.
m_VkFFTConfiguration.bufferNum = 1;
m_VkFFTConfiguration.bufferStride[0] = m_VkFFTConfiguration.size[0];
m_VkFFTConfiguration.bufferStride[1] = m_VkFFTConfiguration.bufferStride[0] * m_VkFFTConfiguration.size[1];
m_VkFFTConfiguration.bufferStride[2] = m_VkFFTConfiguration.bufferStride[1] * m_VkFFTConfiguration.size[2];
m_VkFFTConfiguration.bufferSize = &m_VkFFTConfiguration.bufferStride[2];
const uint64_t bufferBytes{ 2UL * m_VkParameters.PSize * *m_VkFFTConfiguration.bufferSize };
itkAssertOrThrowMacro(bufferBytes == m_VkParameters.inputBufferBytes,
"CPU and GPU input buffers are of different sizes.");
itkAssertOrThrowMacro(bufferBytes == m_VkParameters.outputBufferBytes,
"CPU and GPU output buffers are of different sizes.");
}
else
{
// Either R2HalfH or R2FullH computation. Either forward or inverse.
m_VkFFTConfiguration.bufferNum = 1;
if (m_VkParameters.fft == FFTEnum::R2HalfH)
{
// R2HalfH computation, either forward or inverse.
m_VkFFTConfiguration.bufferStride[0] = m_VkFFTConfiguration.size[0] / 2 + 1;
}
else
{
// R2FullH computation, either forward or inverse.
m_VkFFTConfiguration.bufferStride[0] = m_VkFFTConfiguration.size[0];
}
m_VkFFTConfiguration.bufferStride[1] = m_VkFFTConfiguration.bufferStride[0] * m_VkFFTConfiguration.size[1];
m_VkFFTConfiguration.bufferStride[2] = m_VkFFTConfiguration.bufferStride[1] * m_VkFFTConfiguration.size[2];
m_VkFFTConfiguration.bufferSize = &m_VkFFTConfiguration.bufferStride[2];
const uint64_t bufferBytes{ 2UL * m_VkParameters.PSize * *m_VkFFTConfiguration.bufferSize };
if (m_VkParameters.I == DirectionEnum::FORWARD)
{
// Either R2FullH or R2HalfH. For forward computation, we have a smaller input buffer.
m_VkFFTConfiguration.isInputFormatted = 1;
m_VkFFTConfiguration.inputBufferNum = 1;
m_VkFFTConfiguration.inputBufferStride[0] = m_VkFFTConfiguration.size[0];
m_VkFFTConfiguration.inputBufferStride[1] =
m_VkFFTConfiguration.inputBufferStride[0] * m_VkFFTConfiguration.size[1];
m_VkFFTConfiguration.inputBufferStride[2] =
m_VkFFTConfiguration.inputBufferStride[1] * m_VkFFTConfiguration.size[2];
m_VkFFTConfiguration.inputBufferSize = &m_VkFFTConfiguration.inputBufferStride[2];
const uint64_t inputBufferBytes{ 1UL * m_VkParameters.PSize * *m_VkFFTConfiguration.inputBufferSize };
itkAssertOrThrowMacro(inputBufferBytes == m_VkParameters.inputBufferBytes,
"CPU and GPU input buffers are of different sizes.");
itkAssertOrThrowMacro(bufferBytes == m_VkParameters.outputBufferBytes,
"CPU and GPU output buffers are of different sizes.");
}
else
{
// Either R2FullH or R2HalfH. For inverse computation, we have a smaller output buffer.
m_VkFFTConfiguration.isOutputFormatted = 1;
m_VkFFTConfiguration.outputBufferNum = 1;
m_VkFFTConfiguration.outputBufferStride[0] = m_VkFFTConfiguration.size[0];
m_VkFFTConfiguration.outputBufferStride[1] =
m_VkFFTConfiguration.outputBufferStride[0] * m_VkFFTConfiguration.size[1];
m_VkFFTConfiguration.outputBufferStride[2] =
m_VkFFTConfiguration.outputBufferStride[1] * m_VkFFTConfiguration.size[2];
m_VkFFTConfiguration.outputBufferSize = &m_VkFFTConfiguration.outputBufferStride[2];
uint64_t outputBufferBytes{ 1UL * m_VkParameters.PSize * *m_VkFFTConfiguration.outputBufferSize };
itkAssertOrThrowMacro(bufferBytes == m_VkParameters.inputBufferBytes,
"CPU and GPU input buffers are of different sizes.");
itkAssertOrThrowMacro(outputBufferBytes == m_VkParameters.outputBufferBytes,
"CPU and GPU output buffers are of different sizes.");
}
}
return resFFT;
}
VkFFTResult
VkCommon::PerformFFT()
{
VkFFTResult resFFT{ VKFFT_SUCCESS };
#if (VKFFT_BACKEND == CUDA)
cudaError resCu{ cudaSuccess };
cuFloatComplex * inputGPUBuffer{ nullptr };
cuFloatComplex * GPUBuffer{ nullptr };
cuFloatComplex * outputGPUBuffer{ nullptr };
// Allocate the in-place-computation buffer
const uint64_t bufferBytes{ 2UL * m_VkParameters.PSize * *m_VkFFTConfiguration.bufferSize };
resCu = cudaMalloc((void **)&GPUBuffer, bufferBytes);
if (resCu != cudaSuccess)
{
std::cerr << __FILE__ "(" << __LINE__ << "): cudaMalloc returned " << resCu << std::endl;
return VkFFTResult{ VKFFT_ERROR_FAILED_TO_ALLOCATE };
}
m_VkFFTConfiguration.buffer = reinterpret_cast<void **>(&GPUBuffer);
if (m_VkParameters.fft == FFTEnum::C2C)
{
// For C2C computation we can do everything in the in-place-computation buffer.
inputGPUBuffer = GPUBuffer;
outputGPUBuffer = GPUBuffer;
}
else
{
if (m_VkParameters.I == DirectionEnum::FORWARD)
{
outputGPUBuffer = GPUBuffer;
// Either R2FullH or R2HalfH. For forward computation, we have a smaller input buffer.
const uint64_t inputBufferBytes{ 1UL * m_VkParameters.PSize * *m_VkFFTConfiguration.inputBufferSize };
resCu = cudaMalloc((void **)&inputGPUBuffer, inputBufferBytes);
if (resCu != cudaSuccess)
{
std::cerr << __FILE__ "(" << __LINE__ << "): cudaMalloc returned " << resCu << std::endl;
return VkFFTResult{ VKFFT_ERROR_FAILED_TO_ALLOCATE };
}
m_VkFFTConfiguration.inputBuffer = reinterpret_cast<void **>(&inputGPUBuffer);
}
else
{
inputGPUBuffer = GPUBuffer;
// Either R2FullH or R2HalfH. For inverse computation, we have a smaller output buffer.
uint64_t outputBufferBytes{ 1UL * m_VkParameters.PSize * *m_VkFFTConfiguration.outputBufferSize };
resCu = cudaMalloc((void **)&outputGPUBuffer, outputBufferBytes);
if (resCu != cudaSuccess)
{
std::cerr << __FILE__ "(" << __LINE__ << "): cudaMalloc returned " << resCu << std::endl;
return VkFFTResult{ VKFFT_ERROR_FAILED_TO_ALLOCATE };
}
m_VkFFTConfiguration.outputBuffer = reinterpret_cast<void **>(&outputGPUBuffer);
}
}
// Copy input from CPU to GPU
resCu =
cudaMemcpy(inputGPUBuffer, m_VkParameters.inputCPUBuffer, m_VkParameters.inputBufferBytes, cudaMemcpyHostToDevice);
if (resCu != cudaSuccess)
{
std::cerr << __FILE__ "(" << __LINE__ << "): cudaMemcpy returned " << resCu << std::endl;
return VkFFTResult{ VKFFT_ERROR_FAILED_TO_COPY };
}
#elif (VKFFT_BACKEND == OPENCL)
cl_int resCL{ CL_SUCCESS };
// Configure the buffers. Some of these three pointers will be nullptr or be duplicates of each
// other, so don't release all of them at the end. All re-striding of data (for R2HalfH or R2FullH,
// regardless of forward vs. inverse) is done by VkFFT between the two GPU buffers it uses.
cl_mem inputGPUBuffer{ nullptr }; // Copy from CPU input buffer to this GPU buffer
cl_mem GPUBuffer{ nullptr }; // GPU buffer where main computation occurs
cl_mem outputGPUBuffer{ nullptr }; // Copy from this GPU buffer to CPU output buffer
if (m_VkParameters.fft == FFTEnum::C2C)
{
// For C2C computation we can do everything in the in-place-computation buffer.
const uint64_t bufferBytes{ 2UL * m_VkParameters.PSize * *m_VkFFTConfiguration.bufferSize };
GPUBuffer = clCreateBuffer(m_VkGPU.context, CL_MEM_READ_WRITE, bufferBytes, nullptr, &resCL);
inputGPUBuffer = GPUBuffer;
outputGPUBuffer = GPUBuffer;
if (resCL != CL_SUCCESS)
{
std::cerr << __FILE__ "(" << __LINE__ << "): clCreateBuffer returned " << resCL << std::endl;
return VkFFTResult{ VKFFT_ERROR_FAILED_TO_ALLOCATE };
}
m_VkFFTConfiguration.buffer = &GPUBuffer;
}
else
{
// Either R2HalfH or R2FullH computation. Either forward or inverse.
const uint64_t bufferBytes{ 2UL * m_VkParameters.PSize * *m_VkFFTConfiguration.bufferSize };
GPUBuffer = clCreateBuffer(m_VkGPU.context, CL_MEM_READ_WRITE, bufferBytes, nullptr, &resCL);
if (resCL != CL_SUCCESS)
{
std::cerr << __FILE__ "(" << __LINE__ << "): clCreateBuffer returned " << resCL << std::endl;
return VkFFTResult{ VKFFT_ERROR_FAILED_TO_ALLOCATE };
}
m_VkFFTConfiguration.buffer = &GPUBuffer;
if (m_VkParameters.I == DirectionEnum::FORWARD)
{
// Either R2FullH or R2HalfH. For forward computation, we have a smaller input buffer.
const uint64_t inputBufferBytes{ 1UL * m_VkParameters.PSize * *m_VkFFTConfiguration.inputBufferSize };
inputGPUBuffer = clCreateBuffer(m_VkGPU.context, CL_MEM_READ_WRITE, inputBufferBytes, nullptr, &resCL);
outputGPUBuffer = GPUBuffer;
if (resCL != CL_SUCCESS)
{
std::cerr << __FILE__ "(" << __LINE__ << "): clCreateBuffer returned " << resCL << std::endl;
return VkFFTResult{ VKFFT_ERROR_FAILED_TO_ALLOCATE };
}
m_VkFFTConfiguration.inputBuffer = &inputGPUBuffer;
}
else
{
// Either R2FullH or R2HalfH. For inverse computation, we have a smaller output buffer.
uint64_t outputBufferBytes{ 1UL * m_VkParameters.PSize * *m_VkFFTConfiguration.outputBufferSize };
inputGPUBuffer = GPUBuffer;
outputGPUBuffer = clCreateBuffer(m_VkGPU.context, CL_MEM_READ_WRITE, outputBufferBytes, nullptr, &resCL);
if (resCL != CL_SUCCESS)
{
std::cerr << __FILE__ "(" << __LINE__ << "): clCreateBuffer returned " << resCL << std::endl;
return VkFFTResult{ VKFFT_ERROR_FAILED_TO_ALLOCATE };
}
m_VkFFTConfiguration.outputBuffer = &outputGPUBuffer;
}
}
// Copy input from CPU to GPU
resCL = clEnqueueWriteBuffer(m_VkGPU.commandQueue,
inputGPUBuffer,
CL_TRUE,
0,
m_VkParameters.inputBufferBytes,
m_VkParameters.inputCPUBuffer,
0,
nullptr,
nullptr);
if (resCL != CL_SUCCESS)
{
std::cerr << __FILE__ "(" << __LINE__ << "): clEnqueueWriteBuffer returned " << resCL << std::endl;
return VkFFTResult{ VKFFT_ERROR_FAILED_TO_COPY };
}
#endif
// Initialize applications. This function loads shaders, creates pipeline and configures FFT based on configuration
// file. No buffer allocations inside VkFFT library.
VkFFTApplication app{};
resFFT = initializeVkFFT(&app, m_VkFFTConfiguration);
if (resFFT != VKFFT_SUCCESS)
return resFFT;
// Submit FFT or iFFT.
VkFFTLaunchParams launchParams{};
launchParams.inputBuffer = m_VkFFTConfiguration.inputBuffer;
launchParams.buffer = m_VkFFTConfiguration.buffer;
launchParams.outputBuffer = m_VkFFTConfiguration.outputBuffer;
#if (VKFFT_BACKEND == CUDA)
// pass
#elif (VKFFT_BACKEND == OPENCL)
launchParams.commandQueue = &m_VkGPU.commandQueue;
#endif
resFFT = VkFFTAppend(&app, m_VkParameters.I == DirectionEnum::INVERSE ? 1 : -1, &launchParams);
if (resFFT != VKFFT_SUCCESS)
return resFFT;
#if (VKFFT_BACKEND == CUDA)
resCu = cudaDeviceSynchronize();
if (resCu != cudaSuccess)
{
std::cerr << __FILE__ "(" << __LINE__ << "): cudaDeviceSynchronize returned " << resCu << std::endl;
return VkFFTResult{ VKFFT_ERROR_FAILED_TO_SYNCHRONIZE };
}
// Copy result from GPU to CPU
resCu = cudaMemcpy(
m_VkParameters.outputCPUBuffer, outputGPUBuffer, m_VkParameters.outputBufferBytes, cudaMemcpyDeviceToHost);
if (resCu != cudaSuccess)
{
std::cerr << __FILE__ "(" << __LINE__ << "): cudaMemcpy returned " << resCu << std::endl;
return VkFFTResult{ VKFFT_ERROR_FAILED_TO_COPY };
}
// Release mem buffers
cudaFree(inputGPUBuffer);
if (m_VkParameters.fft != FFTEnum::C2C)
{
cudaFree(outputGPUBuffer);
}
#elif (VKFFT_BACKEND == OPENCL)
resCL = clFinish(m_VkGPU.commandQueue);
if (resCL != CL_SUCCESS)
{
std::cerr << __FILE__ "(" << __LINE__ << "): clFinish returned " << resCL << std::endl;
return VkFFTResult{ VKFFT_ERROR_FAILED_TO_SYNCHRONIZE };
}
// Copy result from GPU to CPU
resCL = clEnqueueReadBuffer(m_VkGPU.commandQueue,
outputGPUBuffer,
CL_TRUE,
0,
m_VkParameters.outputBufferBytes,
m_VkParameters.outputCPUBuffer,
0,
nullptr,
nullptr);
if (resCL != CL_SUCCESS)
{
std::cerr << __FILE__ "(" << __LINE__ << "): clEnqueueReadBuffer returned " << resCL << std::endl;
return VkFFTResult{ VKFFT_ERROR_FAILED_TO_COPY };
}
clReleaseMemObject(inputGPUBuffer);
if (m_VkParameters.fft != FFTEnum::C2C)
{
// Release other buffer too
clReleaseMemObject(outputGPUBuffer);
}
#endif
if (m_VkParameters.fft == FFTEnum::R2FullH && m_VkParameters.I == DirectionEnum::FORWARD)
{
// Compute complex conjugates for the R2FullH forward computation
switch (m_VkParameters.P)
{
case PrecisionEnum::FLOAT:
{
using ComplexType = std::complex<float>;
ComplexType * const outputCPUFloat{ reinterpret_cast<ComplexType *>(m_VkParameters.outputCPUBuffer) };
for (uint64_t z{ 0 }; z < m_VkFFTConfiguration.size[2]; ++z)
{
for (uint64_t y{ 0 }; y < m_VkFFTConfiguration.size[1]; ++y)
{
const uint64_t offsetStart{ z * m_VkFFTConfiguration.bufferStride[1] +
y * m_VkFFTConfiguration.bufferStride[0] };
const uint64_t offsetEnd{ offsetStart + m_VkFFTConfiguration.bufferStride[0] };
for (uint64_t x = (m_VkFFTConfiguration.size[0] - 1) / 2; x >= 1; --x)
{
outputCPUFloat[offsetEnd - x] = std::conj(outputCPUFloat[offsetStart + x]);
}
}
}
}
break;
case PrecisionEnum::DOUBLE:
{
using ComplexType = std::complex<double>;
ComplexType * const outputCPUDouble{ reinterpret_cast<ComplexType *>(m_VkParameters.outputCPUBuffer) };
for (uint64_t z{ 0 }; z < m_VkFFTConfiguration.size[2]; ++z)
{
for (uint64_t y{ 0 }; y < m_VkFFTConfiguration.size[1]; ++y)
{
const uint64_t offsetStart{ z * m_VkFFTConfiguration.bufferStride[1] +
y * m_VkFFTConfiguration.bufferStride[0] };
const uint64_t offsetEnd{ offsetStart + m_VkFFTConfiguration.bufferStride[0] };
for (uint64_t x = (m_VkFFTConfiguration.size[0] - 1) / 2; x >= 1; --x)
{
outputCPUDouble[offsetEnd - x] = std::conj(outputCPUDouble[offsetStart + x]);
}
}
}
}
break;
} // end switch (m_VkParameters.P)
} // end if(m_VkParameters.fft == R2FullH && m_VkParameters.I == DirectionEnum::FORWARD)
deleteVkFFT(&app);
return resFFT;
}
VkFFTResult
VkCommon::ReleaseBackend()
{
VkFFTResult resFFT{ VKFFT_SUCCESS };
// Return to launchVkFFT code
#if (VKFFT_BACKEND == CUDA)
if (m_VkGPU.context)
{
cuCtxDestroy(m_VkGPU.context);
}
#elif (VKFFT_BACKEND == OPENCL)
cl_int resCL{ CL_SUCCESS };
if (m_VkGPU.commandQueue)
{
resCL = clReleaseCommandQueue(m_VkGPU.commandQueue);
if (resCL != CL_SUCCESS)
{
std::cerr << __FILE__ "(" << __LINE__ << "): clReleaseCommandQueue returned " << resCL << std::endl;
return VkFFTResult{ VKFFT_ERROR_FAILED_TO_RELEASE_COMMAND_QUEUE };
}
}
if (m_VkGPU.context)
{
resCL = clReleaseContext(m_VkGPU.context);
if (resCL != CL_SUCCESS)
{
std::cerr << __FILE__ "(" << __LINE__ << "): clReleaseContext returned " << resCL << std::endl;
return VkFFTResult{ VKFFT_ERROR_FAILED_TO_RELEASE_COMMAND_QUEUE };
}
}
#endif
// Invalidate cache description!!!
return resFFT;
}
} // end namespace itk