-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransform.cpp
More file actions
419 lines (369 loc) · 16.8 KB
/
transform.cpp
File metadata and controls
419 lines (369 loc) · 16.8 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
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
#include "metalFFT.h"
metalfftStatus metalfftEnqueueTransform(
metalfftPlanHandle plHandle,
metalfftDirection dir,
unsigned int numQueuesAndEvents,
//cl_command_queue* commQueues,
unsigned int numWaitEvents//,
//const cl_event* waitEvents,
//cl_event* outEvents,
//cl_mem* inputBuffers,
//cl_mem* outputBuffers,
//cl_mem tmpBuffer
)
{
cl_int status = CLFFT_SUCCESS;
FFTRepo& fftRepo = FFTRepo::getInstance( );
FFTPlan* fftPlan = NULL;
lockRAII* planLock = NULL;
// At this point, the user wants to enqueue a plan to execute. We lock the plan down now, such that
// after we finish baking the plan (if the user did not do that explicitely before), the plan cannot
// change again through the action of other thread before we enqueue this plan for execution.
OPENCL_V( fftRepo.getPlan( plHandle, fftPlan, planLock ), _T( "fftRepo.getPlan failed" ) );
scopedLock sLock( *planLock, _T( "clfftGetPlanBatchSize" ) );
if( fftPlan->baked == false )
{
OPENCL_V( metalfftBakePlan( plHandle, numQueuesAndEvents, commQueues, NULL, NULL ), _T( "Failed to bake plan" ) );
}
if (fftPlan->inputLayout == METALFFT_REAL) dir = METALFFT_FORWARD;
else if (fftPlan->outputLayout == METALFFT_REAL) dir = METALFFT_BACKWARD;
// we do not check the user provided buffer at this release
cl_mem localIntBuffer = clTmpBuffers;
if( clTmpBuffers == NULL && fftPlan->tmpBufSize > 0 && fftPlan->intBuffer == NULL)
{
// create the intermediate buffers
// The intermediate buffer is always interleave and packed
// For outofplace operation, we have the choice not to create intermediate buffer
// input ->(col+Transpose) output ->(col) output
fftPlan->intBuffer = clCreateBuffer( fftPlan->context, CL_MEM_READ_WRITE,
fftPlan->tmpBufSize, 0, &status);
OPENCL_V( status, _T("Creating the intermediate buffer for large1D Failed") );
fftPlan->libCreatedIntBuffer = true;
#if defined(DEBUGGING)
std::cout << "One intermediate buffer is created" << std::endl;
#endif
}
if( localIntBuffer == NULL && fftPlan->intBuffer != NULL )
localIntBuffer = fftPlan->intBuffer;
if( fftPlan->intBufferRC == NULL && fftPlan->tmpBufSizeRC > 0 )
{
fftPlan->intBufferRC = clCreateBuffer( fftPlan->context, CL_MEM_READ_WRITE, fftPlan->tmpBufSizeRC, 0, &status);
OPENCL_V( status, _T("Creating the intermediate buffer for large1D RC Failed") );
}
if( fftPlan->intBufferC2R == NULL && fftPlan->tmpBufSizeC2R > 0 )
{
fftPlan->intBufferC2R = clCreateBuffer( fftPlan->context, CL_MEM_READ_WRITE, fftPlan->tmpBufSizeC2R, 0, &status);
OPENCL_V( status, _T("Creating the intermediate buffer for large1D YZ C2R Failed") );
}
// The largest vector we can transform in a single pass
// depends on the GPU caps -- especially the amount of LDS
// available
//
size_t Large1DThreshold = 0;
OPENCL_V(fftPlan->GetMax1DLength (&Large1DThreshold), _T("GetMax1DLength failed"));
BUG_CHECK (Large1DThreshold > 1);
//Large1DThreshold = 128;
if(fftPlan->gen != Copy)
switch( fftPlan->dim )
{
case CLFFT_1D:
{
if ( Is1DPossible(fftPlan->length[0], Large1DThreshold) )
break;
if( ( fftPlan->inputLayout == CLFFT_REAL ) && ( fftPlan->planTZ != 0) )
{
//First transpose
// Input->tmp
cl_event transTXOutEvents = NULL;
OPENCL_V( clfftEnqueueTransform( fftPlan->planTX, dir, numQueuesAndEvents, commQueues, numWaitEvents,
waitEvents, &transTXOutEvents, clInputBuffers, &localIntBuffer, NULL ),
_T("clfftEnqueueTransform for large1D transTX failed"));
cl_mem *mybuffers;
if (fftPlan->placeness==CLFFT_INPLACE)
mybuffers = clInputBuffers;
else
mybuffers = clOutputBuffers;
//First Row
//tmp->output
cl_event rowXOutEvents = NULL;
OPENCL_V( clfftEnqueueTransform( fftPlan->planX, dir, numQueuesAndEvents, commQueues, 1,
&transTXOutEvents, &rowXOutEvents, &localIntBuffer, &(fftPlan->intBufferRC), NULL ),
_T("clfftEnqueueTransform for large1D rowX failed"));
clReleaseEvent(transTXOutEvents);
//Second Transpose
// output->tmp
cl_event transTYOutEvents = NULL;
OPENCL_V( clfftEnqueueTransform( fftPlan->planTY, dir, numQueuesAndEvents, commQueues, 1,
&rowXOutEvents, &transTYOutEvents, &(fftPlan->intBufferRC), &localIntBuffer, NULL ),
_T("clfftEnqueueTransform for large1D transTY failed"));
clReleaseEvent(rowXOutEvents);
//Second Row
//tmp->tmp, inplace
cl_event rowYOutEvents = NULL;
OPENCL_V( clfftEnqueueTransform( fftPlan->planY, dir, numQueuesAndEvents, commQueues, 1,
&transTYOutEvents, &rowYOutEvents, &localIntBuffer, &(fftPlan->intBufferRC), NULL ),
_T("clfftEnqueueTransform for large1D rowY failed"));
clReleaseEvent(transTYOutEvents);
//Third Transpose
// tmp->output
OPENCL_V( clfftEnqueueTransform( fftPlan->planTZ, dir, numQueuesAndEvents, commQueues, 1,
&rowYOutEvents, outEvents, &(fftPlan->intBufferRC), mybuffers, NULL ),
_T("clfftEnqueueTransform for large1D transTZ failed"));
clReleaseEvent(rowYOutEvents);
}
else if ( fftPlan->inputLayout == CLFFT_REAL )
{
cl_event colOutEvents = NULL;
cl_event copyInEvents = NULL;
// First pass
// column with twiddle first, OUTOFPLACE, + transpose
OPENCL_V( clfftEnqueueTransform( fftPlan->planX, CLFFT_FORWARD, numQueuesAndEvents, commQueues, numWaitEvents,
waitEvents, &colOutEvents, clInputBuffers, &(fftPlan->intBufferRC), localIntBuffer),
_T("clfftEnqueueTransform large1D col pass failed"));
cl_mem *out_local;
out_local = (fftPlan->placeness==CLFFT_INPLACE) ? clInputBuffers : clOutputBuffers;
// another column FFT output, INPLACE
OPENCL_V(clfftEnqueueTransform(fftPlan->planY, CLFFT_FORWARD, numQueuesAndEvents, commQueues, 1, &colOutEvents,
©InEvents, &(fftPlan->intBufferRC), &(fftPlan->intBufferRC), localIntBuffer),
_T("clfftEnqueueTransform large1D second column failed"));
clReleaseEvent(colOutEvents);
// copy from full complex to hermitian
OPENCL_V(clfftEnqueueTransform(fftPlan->planRCcopy, CLFFT_FORWARD, numQueuesAndEvents, commQueues, 1, ©InEvents,
outEvents, &(fftPlan->intBufferRC), out_local, localIntBuffer),
_T("clfftEnqueueTransform large1D RC copy failed"));
clReleaseEvent(copyInEvents);
}
else if( fftPlan->outputLayout == CLFFT_REAL )
{
cl_event colOutEvents = NULL;
cl_event copyOutEvents = NULL;
if (fftPlan->planRCcopy)
{
// copy from hermitian to full complex
OPENCL_V(clfftEnqueueTransform(fftPlan->planRCcopy, CLFFT_BACKWARD, numQueuesAndEvents, commQueues, numWaitEvents,
waitEvents, ©OutEvents, clInputBuffers, &(fftPlan->intBufferRC), localIntBuffer),
_T("clfftEnqueueTransform large1D RC copy failed"));
// First pass
// column with twiddle first, INPLACE,
OPENCL_V(clfftEnqueueTransform(fftPlan->planX, CLFFT_BACKWARD, numQueuesAndEvents, commQueues, 1,
©OutEvents, &colOutEvents, &(fftPlan->intBufferRC), &(fftPlan->intBufferRC), localIntBuffer),
_T("clfftEnqueueTransform large1D col pass failed"));
clReleaseEvent(copyOutEvents);
}
else
{
// First pass
// column with twiddle first, INPLACE,
OPENCL_V(clfftEnqueueTransform(fftPlan->planX, CLFFT_BACKWARD, numQueuesAndEvents, commQueues, numWaitEvents,
waitEvents, &colOutEvents, clInputBuffers, &(fftPlan->intBufferRC), localIntBuffer),
_T("clfftEnqueueTransform large1D col pass failed"));
clReleaseEvent(copyOutEvents);
}
cl_mem *out_local;
out_local = (fftPlan->placeness==CLFFT_INPLACE) ? clInputBuffers : clOutputBuffers;
// another column FFT output, OUTOFPLACE + transpose
OPENCL_V( clfftEnqueueTransform( fftPlan->planY, CLFFT_BACKWARD, numQueuesAndEvents, commQueues, 1, &colOutEvents,
outEvents, &(fftPlan->intBufferRC), out_local, localIntBuffer ),
_T("clfftEnqueueTransform large1D second column failed"));
clReleaseEvent(colOutEvents);
}
else
{
if (fftPlan->transflag)
{
//First transpose
// Input->tmp
cl_event transTXOutEvents = NULL;
if(fftPlan->allOpsInplace)
{
OPENCL_V( clfftEnqueueTransform( fftPlan->planTX, dir, numQueuesAndEvents, commQueues, numWaitEvents,
waitEvents, &transTXOutEvents, clInputBuffers, NULL, NULL ),
_T("clfftEnqueueTransform for large1D transTX failed"));
}
else
{
OPENCL_V( clfftEnqueueTransform( fftPlan->planTX, dir, numQueuesAndEvents, commQueues, numWaitEvents,
waitEvents, &transTXOutEvents, clInputBuffers, &localIntBuffer, NULL ),
_T("clfftEnqueueTransform for large1D transTX failed"));
}
cl_mem *mybuffers;
if (fftPlan->placeness==CLFFT_INPLACE)
mybuffers = clInputBuffers;
else
mybuffers = clOutputBuffers;
//First Row
//tmp->output
cl_event rowXOutEvents = NULL;
if(fftPlan->allOpsInplace)
{
OPENCL_V( clfftEnqueueTransform( fftPlan->planX, dir, numQueuesAndEvents, commQueues, 1,
&transTXOutEvents, &rowXOutEvents, clInputBuffers, NULL, NULL ),
_T("clfftEnqueueTransform for large1D rowX failed"));
}
else
{
OPENCL_V( clfftEnqueueTransform( fftPlan->planX, dir, numQueuesAndEvents, commQueues, 1,
&transTXOutEvents, &rowXOutEvents, &localIntBuffer, mybuffers, NULL ),
_T("clfftEnqueueTransform for large1D rowX failed"));
}
clReleaseEvent(transTXOutEvents);
//Second Transpose
// output->tmp
cl_event transTYOutEvents = NULL;
if(fftPlan->allOpsInplace)
{
OPENCL_V( clfftEnqueueTransform( fftPlan->planTY, dir, numQueuesAndEvents, commQueues, 1,
&rowXOutEvents, &transTYOutEvents, clInputBuffers, NULL, NULL ),
_T("clfftEnqueueTransform for large1D transTY failed"));
}
else
{
OPENCL_V( clfftEnqueueTransform( fftPlan->planTY, dir, numQueuesAndEvents, commQueues, 1,
&rowXOutEvents, &transTYOutEvents, mybuffers, &localIntBuffer, NULL ),
_T("clfftEnqueueTransform for large1D transTY failed"));
}
clReleaseEvent(rowXOutEvents);
//Second Row
//tmp->tmp, inplace
cl_event rowYOutEvents = NULL;
if(fftPlan->allOpsInplace)
{
OPENCL_V( clfftEnqueueTransform( fftPlan->planY, dir, numQueuesAndEvents, commQueues, 1,
&transTYOutEvents, &rowYOutEvents, clInputBuffers, NULL, NULL ),
_T("clfftEnqueueTransform for large1D rowY failed"));
}
else
{
OPENCL_V( clfftEnqueueTransform( fftPlan->planY, dir, numQueuesAndEvents, commQueues, 1,
&transTYOutEvents, &rowYOutEvents, &localIntBuffer, NULL, NULL ),
_T("clfftEnqueueTransform for large1D rowY failed"));
}
clReleaseEvent(transTYOutEvents);
//Third Transpose
// tmp->output
if(fftPlan->allOpsInplace)
{
OPENCL_V( clfftEnqueueTransform( fftPlan->planTZ, dir, numQueuesAndEvents, commQueues, 1,
&rowYOutEvents, outEvents, clInputBuffers, NULL, NULL ),
_T("clfftEnqueueTransform for large1D transTZ failed"));
}
else
{
OPENCL_V( clfftEnqueueTransform( fftPlan->planTZ, dir, numQueuesAndEvents, commQueues, 1,
&rowYOutEvents, outEvents, &localIntBuffer, mybuffers, NULL ),
_T("clfftEnqueueTransform for large1D transTZ failed"));
}
clReleaseEvent(rowYOutEvents);
}
else
{
if (fftPlan->large1D == 0)
{
if(fftPlan->planCopy)
{
// Transpose OUTOFPLACE
cl_event transTXOutEvents = NULL;
OPENCL_V( clfftEnqueueTransform( fftPlan->planTX, dir, numQueuesAndEvents, commQueues, numWaitEvents,
waitEvents, &transTXOutEvents, clInputBuffers, &localIntBuffer, NULL ),
_T("clfftEnqueueTransform for large1D transTX failed"));
// FFT INPLACE
cl_event rowXOutEvents = NULL;
OPENCL_V( clfftEnqueueTransform( fftPlan->planX, dir, numQueuesAndEvents, commQueues, 1,
&transTXOutEvents, &rowXOutEvents, &localIntBuffer, NULL, NULL),
_T("clfftEnqueueTransform large1D first row pass failed"));
clReleaseEvent(transTXOutEvents);
// FFT INPLACE
cl_event colYOutEvents = NULL;
OPENCL_V( clfftEnqueueTransform( fftPlan->planY, dir, numQueuesAndEvents, commQueues, 1, &rowXOutEvents,
&colYOutEvents, &localIntBuffer, NULL, NULL ),
_T("clfftEnqueueTransform large1D second column failed"));
clReleaseEvent(rowXOutEvents);
cl_mem *mybuffers;
if (fftPlan->placeness==CLFFT_INPLACE)
mybuffers = clInputBuffers;
else
mybuffers = clOutputBuffers;
// Copy kernel
OPENCL_V( clfftEnqueueTransform( fftPlan->planCopy, dir, numQueuesAndEvents, commQueues, 1, &colYOutEvents,
outEvents, &localIntBuffer, mybuffers, NULL ),
_T("clfftEnqueueTransform large1D copy failed"));
clReleaseEvent(colYOutEvents);
}
else
{
cl_event colOutEvents = NULL;
// First pass
// column with twiddle first, OUTOFPLACE
OPENCL_V( clfftEnqueueTransform( fftPlan->planX, dir, numQueuesAndEvents, commQueues, numWaitEvents,
waitEvents, &colOutEvents, clInputBuffers, &localIntBuffer, NULL),
_T("clfftEnqueueTransform large1D col pass failed"));
if(fftPlan->planTZ)
{
cl_event rowYOutEvents = NULL;
OPENCL_V( clfftEnqueueTransform( fftPlan->planY, dir, numQueuesAndEvents, commQueues, 1, &colOutEvents,
&rowYOutEvents, &localIntBuffer, NULL, NULL ),
_T("clfftEnqueueTransform large1D second row failed"));
if (fftPlan->placeness == CLFFT_INPLACE)
{
OPENCL_V( clfftEnqueueTransform( fftPlan->planTZ, dir, numQueuesAndEvents, commQueues, 1, &rowYOutEvents,
outEvents, &localIntBuffer, clInputBuffers, NULL ),
_T("clfftEnqueueTransform large1D trans3 failed"));
}
else
{
OPENCL_V( clfftEnqueueTransform( fftPlan->planTZ, dir, numQueuesAndEvents, commQueues, 1, &rowYOutEvents,
outEvents, &localIntBuffer, clOutputBuffers, NULL ),
_T("clfftEnqueueTransform large1D trans3 failed"));
}
clReleaseEvent(rowYOutEvents);
}
else
{
//another column FFT output, OUTOFPLACE + transpose
if (fftPlan->placeness == CLFFT_INPLACE)
{
OPENCL_V( clfftEnqueueTransform( fftPlan->planY, dir, numQueuesAndEvents, commQueues, 1, &colOutEvents,
outEvents, &localIntBuffer, clInputBuffers, NULL ),
_T("clfftEnqueueTransform large1D second column failed"));
}
else
{
OPENCL_V( clfftEnqueueTransform( fftPlan->planY, dir, numQueuesAndEvents, commQueues, 1, &colOutEvents,
outEvents, &localIntBuffer, clOutputBuffers, NULL ),
_T("clfftEnqueueTransform large1D second column failed"));
}
}
clReleaseEvent(colOutEvents);
}
}
else
{
cl_event colOutEvents = NULL;
// second pass for huge 1D
// column with twiddle first, OUTOFPLACE, + transpose
OPENCL_V( clfftEnqueueTransform( fftPlan->planX, dir, numQueuesAndEvents, commQueues, numWaitEvents,
waitEvents, &colOutEvents, &localIntBuffer, clOutputBuffers, localIntBuffer),
_T("clfftEnqueueTransform Huge1D col pass failed"));
OPENCL_V( clfftEnqueueTransform( fftPlan->planY, dir, numQueuesAndEvents, commQueues, 1, &colOutEvents,
outEvents, clOutputBuffers, clOutputBuffers, localIntBuffer ),
_T("clfftEnqueueTransform large1D second column failed"));
clReleaseEvent(colOutEvents);
}
}
}
return CLFFT_SUCCESS;
}
case CLFFT_2D:
case CLFFT_3D:
return METALFFT_NOTIMPLEMENTED;
}
return fftPlan->action->enqueue(plHandle,
dir,
numQueuesAndEvents,
commQueues,
numWaitEvents,
waitEvents,
outEvents,
clInputBuffers,
clOutputBuffers);
}