-
Notifications
You must be signed in to change notification settings - Fork 192
/
enqueue.cpp
830 lines (707 loc) · 26 KB
/
enqueue.cpp
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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
/* ************************************************************************
* Copyright 2013 Advanced Micro Devices, Inc.
*
* 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
*
* 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 "stdafx.h"
#include <math.h>
#include "private.h"
#include "repo.h"
#include "plan.h"
#include "generator.stockham.h"
#include "../include/convenienceFunctions.h"
#include "action.h"
#include "fft_binary_lookup.h"
#define FFT_CACHE_DEBUG 0
FFTCopyAction::FFTCopyAction(clfftPlanHandle plHandle, FFTPlan * plan, cl_command_queue queue, clfftStatus & err)
: FFTAction(plan, err)
{
if (err != CLFFT_SUCCESS)
{
// FFTAction() failed, exit constructor
return;
}
err = CLFFT_SUCCESS;
}
FFTTransposeGCNAction::FFTTransposeGCNAction(clfftPlanHandle plHandle, FFTPlan * plan, cl_command_queue queue, clfftStatus & err)
: FFTAction(plan, err)
{
if (err != CLFFT_SUCCESS)
{
// FFTAction() failed, exit constructor
return;
}
err = CLFFT_SUCCESS;
}
FFTTransposeSquareAction::FFTTransposeSquareAction(clfftPlanHandle plHandle, FFTPlan * plan, cl_command_queue queue, clfftStatus & err)
: FFTAction(plan, err)
{
if (err != CLFFT_SUCCESS)
{
// FFTAction() failed, exit constructor
return;
}
err = CLFFT_SUCCESS;
}
FFTTransposeNonSquareAction::FFTTransposeNonSquareAction(clfftPlanHandle plHandle, FFTPlan * plan, cl_command_queue queue, clfftStatus & err)
: FFTAction(plan, err)
{
if (err != CLFFT_SUCCESS)
{
// FFTAction() failed, exit constructor
return;
}
err = CLFFT_SUCCESS;
}
FFTStockhamAction::FFTStockhamAction(clfftPlanHandle plHandle, FFTPlan * plan, cl_command_queue queue, clfftStatus & err)
: FFTAction(plan, err)
{
if (err != CLFFT_SUCCESS)
{
// FFTAction() failed, exit constructor
return;
}
err = CLFFT_SUCCESS;
}
FFTAction::FFTAction(FFTPlan * fftPlan, clfftStatus & err)
: plan(fftPlan)
{
err = CLFFT_SUCCESS;
}
clfftStatus FFTAction::selectBufferArguments(FFTPlan * fftPlan,
cl_mem* clInputBuffers,
cl_mem* clOutputBuffers,
std::vector< cl_mem > &inputBuff,
std::vector< cl_mem > &outputBuff)
{
// 1d with normal length will fall into the below category
// add: 2d transpose kernel will fall into here too.
inputBuff.reserve( 2 );
outputBuff.reserve( 2 );
// Decode the relevant properties from the plan paramter to figure out how many input/output buffers we have
switch( fftPlan->inputLayout )
{
case CLFFT_COMPLEX_INTERLEAVED:
{
switch( fftPlan->outputLayout )
{
case CLFFT_COMPLEX_INTERLEAVED:
{
if( fftPlan->placeness == CLFFT_INPLACE )
{
inputBuff.push_back( clInputBuffers[ 0 ] );
}
else
{
inputBuff.push_back( clInputBuffers[ 0 ] );
outputBuff.push_back( clOutputBuffers[ 0 ] );
}
break;
}
case CLFFT_COMPLEX_PLANAR:
{
if( fftPlan->placeness == CLFFT_INPLACE )
{
// Invalid to be an inplace transform, and go from 1 to 2 buffers
return CLFFT_INVALID_ARG_VALUE;
}
else
{
inputBuff.push_back( clInputBuffers[ 0 ] );
outputBuff.push_back( clOutputBuffers[ 0 ] );
outputBuff.push_back( clOutputBuffers[ 1 ] );
}
break;
}
case CLFFT_HERMITIAN_INTERLEAVED:
{
if( fftPlan->placeness == CLFFT_INPLACE )
{
return CLFFT_INVALID_ARG_VALUE;
}
else
{
inputBuff.push_back( clInputBuffers[ 0 ] );
outputBuff.push_back( clOutputBuffers[ 0 ] );
}
break;
}
case CLFFT_HERMITIAN_PLANAR:
{
if( fftPlan->placeness == CLFFT_INPLACE )
{
return CLFFT_INVALID_ARG_VALUE;
}
else
{
inputBuff.push_back( clInputBuffers[ 0 ] );
outputBuff.push_back( clOutputBuffers[ 0 ] );
outputBuff.push_back( clOutputBuffers[ 1 ] );
}
break;
}
case CLFFT_REAL:
{
if( fftPlan->placeness == CLFFT_INPLACE )
{
inputBuff.push_back( clInputBuffers[ 0 ] );
}
else
{
inputBuff.push_back( clInputBuffers[ 0 ] );
outputBuff.push_back( clOutputBuffers[ 0 ] );
}
break;
}
default:
{
// Don't recognize output layout
return CLFFT_INVALID_ARG_VALUE;
}
}
break;
}
case CLFFT_COMPLEX_PLANAR:
{
switch( fftPlan->outputLayout )
{
case CLFFT_COMPLEX_INTERLEAVED:
{
if( fftPlan->placeness == CLFFT_INPLACE )
{
return CLFFT_INVALID_ARG_VALUE;
}
else
{
inputBuff.push_back( clInputBuffers[ 0 ] );
inputBuff.push_back( clInputBuffers[ 1 ] );
outputBuff.push_back( clOutputBuffers[ 0 ] );
}
break;
}
case CLFFT_COMPLEX_PLANAR:
{
if( fftPlan->placeness == CLFFT_INPLACE )
{
inputBuff.push_back( clInputBuffers[ 0 ] );
inputBuff.push_back( clInputBuffers[ 1 ] );
}
else
{
inputBuff.push_back( clInputBuffers[ 0 ] );
inputBuff.push_back( clInputBuffers[ 1 ] );
outputBuff.push_back( clOutputBuffers[ 0 ] );
outputBuff.push_back( clOutputBuffers[ 1 ] );
}
break;
}
case CLFFT_HERMITIAN_INTERLEAVED:
{
if( fftPlan->placeness == CLFFT_INPLACE )
{
return CLFFT_INVALID_ARG_VALUE;
}
else
{
inputBuff.push_back( clInputBuffers[ 0 ] );
inputBuff.push_back( clInputBuffers[ 1 ] );
outputBuff.push_back( clOutputBuffers[ 0 ] );
}
break;
}
case CLFFT_HERMITIAN_PLANAR:
{
if( fftPlan->placeness == CLFFT_INPLACE )
{
return CLFFT_INVALID_ARG_VALUE;
}
else
{
inputBuff.push_back( clInputBuffers[ 0 ] );
inputBuff.push_back( clInputBuffers[ 1 ] );
outputBuff.push_back( clOutputBuffers[ 0 ] );
outputBuff.push_back( clOutputBuffers[ 1 ] );
}
break;
}
case CLFFT_REAL:
{
if( fftPlan->placeness == CLFFT_INPLACE )
{
return CLFFT_INVALID_ARG_VALUE;
}
else
{
inputBuff.push_back( clInputBuffers[ 0 ] );
inputBuff.push_back( clInputBuffers[ 1 ] );
outputBuff.push_back( clOutputBuffers[ 0 ] );
}
break;
}
default:
{
// Don't recognize output layout
return CLFFT_INVALID_ARG_VALUE;
}
}
break;
}
case CLFFT_HERMITIAN_INTERLEAVED:
{
switch( fftPlan->outputLayout )
{
case CLFFT_COMPLEX_INTERLEAVED:
{
if( fftPlan->placeness == CLFFT_INPLACE )
{
return CLFFT_INVALID_ARG_VALUE;
}
else
{
inputBuff.push_back( clInputBuffers[ 0 ] );
outputBuff.push_back( clOutputBuffers[ 0 ] );
}
break;
}
case CLFFT_COMPLEX_PLANAR:
{
if( fftPlan->placeness == CLFFT_INPLACE )
{
return CLFFT_INVALID_ARG_VALUE;
}
else
{
inputBuff.push_back( clInputBuffers[ 0 ] );
outputBuff.push_back( clOutputBuffers[ 0 ] );
outputBuff.push_back( clOutputBuffers[ 1 ] );
}
break;
}
case CLFFT_HERMITIAN_INTERLEAVED:
{
return CLFFT_INVALID_ARG_VALUE;
}
case CLFFT_HERMITIAN_PLANAR:
{
return CLFFT_INVALID_ARG_VALUE;
}
case CLFFT_REAL:
{
if( fftPlan->placeness == CLFFT_INPLACE )
{
inputBuff.push_back( clInputBuffers[ 0 ] );
}
else
{
inputBuff.push_back( clInputBuffers[ 0 ] );
outputBuff.push_back( clOutputBuffers[ 0 ] );
}
break;
}
default:
{
// Don't recognize output layout
return CLFFT_INVALID_ARG_VALUE;
}
}
break;
}
case CLFFT_HERMITIAN_PLANAR:
{
switch( fftPlan->outputLayout )
{
case CLFFT_COMPLEX_INTERLEAVED:
{
if( fftPlan->placeness == CLFFT_INPLACE )
{
return CLFFT_INVALID_ARG_VALUE;
}
else
{
inputBuff.push_back( clInputBuffers[ 0 ] );
inputBuff.push_back( clInputBuffers[ 1 ] );
outputBuff.push_back( clOutputBuffers[ 0 ] );
}
break;
}
case CLFFT_COMPLEX_PLANAR:
{
if( fftPlan->placeness == CLFFT_INPLACE )
{
return CLFFT_INVALID_ARG_VALUE;
}
else
{
inputBuff.push_back( clInputBuffers[ 0 ] );
inputBuff.push_back( clInputBuffers[ 1 ] );
outputBuff.push_back( clOutputBuffers[ 0 ] );
outputBuff.push_back( clOutputBuffers[ 1 ] );
}
break;
}
case CLFFT_HERMITIAN_INTERLEAVED:
{
return CLFFT_INVALID_ARG_VALUE;
}
case CLFFT_HERMITIAN_PLANAR:
{
return CLFFT_INVALID_ARG_VALUE;
}
case CLFFT_REAL:
{
if( fftPlan->placeness == CLFFT_INPLACE )
{
return CLFFT_INVALID_ARG_VALUE;
}
else
{
inputBuff.push_back( clInputBuffers[ 0 ] );
inputBuff.push_back( clInputBuffers[ 1 ] );
outputBuff.push_back( clOutputBuffers[ 0 ] );
}
break;
}
default:
{
// Don't recognize output layout
return CLFFT_INVALID_ARG_VALUE;
}
}
break;
}
case CLFFT_REAL:
{
switch( fftPlan->outputLayout )
{
case CLFFT_COMPLEX_INTERLEAVED:
{
if( fftPlan->placeness == CLFFT_INPLACE )
{
inputBuff.push_back( clInputBuffers[ 0 ] );
}
else
{
inputBuff.push_back( clInputBuffers[ 0 ] );
outputBuff.push_back( clOutputBuffers[ 0 ] );
}
break;
}
case CLFFT_COMPLEX_PLANAR:
{
if( fftPlan->placeness == CLFFT_INPLACE )
{
return CLFFT_INVALID_ARG_VALUE;
}
else
{
inputBuff.push_back( clInputBuffers[ 0 ] );
outputBuff.push_back( clOutputBuffers[ 0 ] );
outputBuff.push_back( clOutputBuffers[ 1 ] );
}
break;
}
case CLFFT_HERMITIAN_INTERLEAVED:
{
if( fftPlan->placeness == CLFFT_INPLACE )
{
inputBuff.push_back( clInputBuffers[ 0 ] );
}
else
{
inputBuff.push_back( clInputBuffers[ 0 ] );
outputBuff.push_back( clOutputBuffers[ 0 ] );
}
break;
}
case CLFFT_HERMITIAN_PLANAR:
{
if( fftPlan->placeness == CLFFT_INPLACE )
{
return CLFFT_INVALID_ARG_VALUE;
}
else
{
inputBuff.push_back( clInputBuffers[ 0 ] );
outputBuff.push_back( clOutputBuffers[ 0 ] );
outputBuff.push_back( clOutputBuffers[ 1 ] );
}
break;
}
default:
{
if(fftPlan->transflag)
{
if( fftPlan->placeness == CLFFT_INPLACE )
{
return CLFFT_INVALID_ARG_VALUE;
}
else
{
inputBuff.push_back( clInputBuffers[ 0 ] );
outputBuff.push_back( clOutputBuffers[ 0 ] );
}
}
else
{
// Don't recognize output layout
return CLFFT_INVALID_ARG_VALUE;
}
}
}
break;
}
default:
{
// Don't recognize output layout
return CLFFT_INVALID_ARG_VALUE;
}
}
return CLFFT_SUCCESS;
}
clfftStatus FFTAction::enqueue(clfftPlanHandle plHandle,
clfftDirection dir,
cl_uint numQueuesAndEvents,
cl_command_queue* commQueues,
cl_uint numWaitEvents,
const cl_event* waitEvents,
cl_event* outEvents,
cl_mem* clInputBuffers,
cl_mem* clOutputBuffers)
{
FFTRepo & fftRepo = FFTRepo::getInstance();
std::vector< cl_mem > inputBuff;
std::vector< cl_mem > outputBuff;
clfftStatus status = selectBufferArguments(this->plan,
clInputBuffers, clOutputBuffers,
inputBuff, outputBuff);
if (status != CLFFT_SUCCESS)
{
return status;
}
// TODO: In the case of length == 1, FFT is a trivial NOP, but we still need to apply the forward and backwards tranforms
// TODO: Are map lookups expensive to call here? We can cache a pointer to the cl_program/cl_kernel in the plan
// Translate the user plan into the structure that we use to map plans to clPrograms
cl_program prog;
cl_kernel kern;
lockRAII* kernelLock;
OPENCL_V( fftRepo.getclProgram( this->getGenerator(), this->getSignatureData(), prog, this->plan->bakeDevice, this->plan->context ), _T( "fftRepo.getclProgram failed" ) );
OPENCL_V( fftRepo.getclKernel( prog, dir, kern, kernelLock), _T( "fftRepo.getclKernels failed" ) );
scopedLock sLock(*kernelLock, _T("FFTAction::enqueue"));
cl_uint uarg = 0;
if (!this->plan->transflag && !(this->plan->gen == Copy))
{
// ::clSetKernelArg() is not thread safe, according to the openCL spec for the same cl_kernel object
// TODO: Need to verify that two different plans (which would get through our lock above) with exactly the same
// parameters would NOT share the same cl_kernel objects
/* constant buffer */
OPENCL_V( clSetKernelArg( kern, uarg++, sizeof( cl_mem ), (void*)&this->plan->const_buffer ), _T( "clSetKernelArg failed" ) );
}
// Input buffer(s)
// Input may be 1 buffer (CLFFT_COMPLEX_INTERLEAVED)
// or 2 buffers (CLFFT_COMPLEX_PLANAR)
for (size_t i = 0; i < inputBuff.size(); ++i)
{
OPENCL_V( clSetKernelArg( kern, uarg++, sizeof( cl_mem ), (void*)&inputBuff[i] ), _T( "clSetKernelArg failed" ) );
}
// Output buffer(s)
// Output may be 0 buffers (CLFFT_INPLACE)
// or 1 buffer (CLFFT_COMPLEX_INTERLEAVED)
// or 2 buffers (CLFFT_COMPLEX_PLANAR)
for (size_t o = 0; o < outputBuff.size(); ++o)
{
OPENCL_V( clSetKernelArg( kern, uarg++, sizeof( cl_mem ), (void*)&outputBuff[o] ), _T( "clSetKernelArg failed" ) );
}
//If callback function is set for the plan, pass the appropriate aruments
if (this->plan->hasPreCallback || this->plan->hasPostCallback)
{
if (this->plan->hasPreCallback)
{
OPENCL_V( clSetKernelArg( kern, uarg++, sizeof( cl_mem ), (void*)&this->plan->precallUserData ), _T( "clSetKernelArg failed" ) );
}
//If post-callback function is set for the plan, pass the appropriate aruments
if (this->plan->hasPostCallback)
{
OPENCL_V( clSetKernelArg( kern, uarg++, sizeof( cl_mem ), (void*)&this->plan->postcallUserData ), _T( "clSetKernelArg failed" ) );
}
//Pass LDS size arument if set
if ((this->plan->hasPreCallback && this->plan->preCallback.localMemSize > 0) ||
(this->plan->hasPostCallback && this->plan->postCallbackParam.localMemSize > 0))
{
int localmemSize = 0;
if (this->plan->hasPreCallback && this->plan->preCallback.localMemSize > 0)
localmemSize = this->plan->preCallback.localMemSize;
if (this->plan->hasPostCallback && this->plan->postCallbackParam.localMemSize > 0)
localmemSize += this->plan->postCallbackParam.localMemSize;
OPENCL_V( clSetKernelArg( kern, uarg++, localmemSize, NULL ), _T( "clSetKernelArg failed" ) );
}
}
std::vector< size_t > gWorkSize;
std::vector< size_t > lWorkSize;
clfftStatus result = this->getWorkSizes (gWorkSize, lWorkSize);
//std::cout << "work sizes are " << gWorkSize[0] << ", " << lWorkSize[0] << std::endl;
/*
std::cout << "work sizes are ";
for (auto itor = gWorkSize.begin(); itor != gWorkSize.end(); itor++)
std::cout << *itor << " ";
std::cout << ", ";
for (auto itor = lWorkSize.begin(); itor != lWorkSize.end(); itor++)
std::cout << *itor << " ";
std::cout << std::endl;
*/
// TODO: if getWorkSizes returns CLFFT_INVALID_GLOBAL_WORK_SIZE, that means
// that this multidimensional input data array is too large to be transformed
// with a single call to clEnqueueNDRangeKernel. For now, we will just return
// the error code back up the call stack.
// The *correct* course of action would be to split the work into mutliple
// calls to clEnqueueNDRangeKernel.
if (CLFFT_INVALID_GLOBAL_WORK_SIZE == result)
{
OPENCL_V( result, _T("Work size too large for clEnqueNDRangeKernel()"));
}
else
{
OPENCL_V( result, _T("FFTAction::getWorkSizes failed"));
}
BUG_CHECK (gWorkSize.size() == lWorkSize.size());
cl_int call_status = clEnqueueNDRangeKernel( *commQueues, kern, static_cast< cl_uint >( gWorkSize.size( ) ),
NULL, &gWorkSize[ 0 ], &lWorkSize[ 0 ], numWaitEvents, waitEvents, outEvents );
OPENCL_V( call_status, _T( "clEnqueueNDRangeKernel failed" ) );
if( fftRepo.pStatTimer )
{
fftRepo.pStatTimer->AddSample( plHandle, this->plan, kern, numQueuesAndEvents, outEvents, gWorkSize, lWorkSize );
}
return CLFFT_SUCCESS;
}
// Read the kernels that this plan uses from file, and store into the plan
clfftStatus FFTAction::writeKernel( const clfftPlanHandle plHandle, const clfftGenerators gen, const FFTKernelSignatureHeader* data, const cl_context& context, const cl_device_id &device )
{
FFTRepo& fftRepo = FFTRepo::getInstance( );
std::string kernelPath = getKernelName(gen, plHandle, true);
// Logic to write string contents out to file
tofstreamRAII< std::ofstream, std::string > kernelFile( kernelPath.c_str( ) );
if( !kernelFile.get( ) )
{
std::cerr << "Failed to open kernel file for writing: " << kernelPath.c_str( ) << std::endl;
return CLFFT_FILE_CREATE_FAILURE;
}
std::string kernel;
OPENCL_V( fftRepo.getProgramCode( gen, data, kernel, device, context ), _T( "fftRepo.getProgramCode failed." ) );
kernelFile.get( ) << kernel << std::endl;
return CLFFT_SUCCESS;
}
// **************** TODO TODO TODO ***********************
// Making compileKernels function take in command queue parameter so we can build for 1 particular device only;
// this may not be desirable for persistent plans, where we may have to compile for all devices in the context;
// make changes appropriately before enabling persistent plans and then remove this comment
// Compile the kernels that this plan uses, and store into the plan
clfftStatus FFTAction::compileKernels( const cl_command_queue commQueueFFT, const clfftPlanHandle plHandle, FFTPlan* fftPlan )
{
cl_int status = 0;
size_t deviceListSize = 0;
FFTRepo& fftRepo = FFTRepo::getInstance( );
// create a cl program executable for the device associated with command queue
// Get the device
cl_device_id &q_device = fftPlan->bakeDevice;
cl_program program;
if( fftRepo.getclProgram( this->getGenerator(), this->getSignatureData(), program, q_device, fftPlan->context ) == CLFFT_INVALID_PROGRAM )
{
FFTBinaryLookup lookup (this->getGenerator(), plHandle, fftPlan->context, q_device);
lookup.variantRaw(this->getSignatureData(), this->getSignatureData()->datasize);
if (lookup.found())
{
#if FFT_CACHE_DEBUG
// debug message in debug mode to ensure that the cache is used
fprintf(stderr, "Kernel loaded from cache\n");
#endif
program = lookup.getProgram();
}
else
{
#if FFT_CACHE_DEBUG
fprintf(stderr, "Kernel built from source\n");
#endif
// If the user wishes us to write the kernels out to disk, we do so
if( fftRepo.setupData.debugFlags & CLFFT_DUMP_PROGRAMS )
{
OPENCL_V( writeKernel( plHandle, this->getGenerator(), this->getSignatureData(), fftPlan->context, fftPlan->bakeDevice ), _T( "writeKernel failed." ) );
}
std::string programCode;
OPENCL_V( fftRepo.getProgramCode( this->getGenerator(), this->getSignatureData(), programCode, q_device, fftPlan->context ), _T( "fftRepo.getProgramCode failed." ) );
const char* source = programCode.c_str();
program = clCreateProgramWithSource( fftPlan->context, 1, &source, NULL, &status );
OPENCL_V( status, _T( "clCreateProgramWithSource failed." ) );
// create a cl program executable for the device associated with command queue
#if defined(DEBUGGING)
status = clBuildProgram( program, 1, &q_device, "-g -cl-opt-disable", NULL, NULL); // good for debugging kernels
// if you have trouble creating smbols that GDB can pick up to set a breakpoint after kernels are loaded into memory
// this can be used to stop execution to allow you to set a breakpoint in a kernel after kernel symbols are in memory.
#ifdef DEBUG_BREAK_GDB
__debugbreak();
#endif
#else
status = clBuildProgram( program, 1, &q_device, "", NULL, NULL);
#endif
if( status != CL_SUCCESS )
{
if( status == CL_BUILD_PROGRAM_FAILURE )
{
size_t buildLogSize = 0;
OPENCL_V( clGetProgramBuildInfo( program, q_device, CL_PROGRAM_BUILD_LOG, 0, NULL, &buildLogSize ),
_T( "clGetProgramBuildInfo failed" ) );
std::vector< char > buildLog( buildLogSize );
::memset( &buildLog[ 0 ], 0x0, buildLogSize );
OPENCL_V( clGetProgramBuildInfo( program, q_device, CL_PROGRAM_BUILD_LOG, buildLogSize, &buildLog[ 0 ], NULL ),
_T( "clGetProgramBuildInfo failed" ) );
std::cerr << "\n\t\t\tBUILD LOG\n";
std::cerr << "************************************************\n";
std::cerr << &buildLog[ 0 ] << std::endl;
std::cerr << "************************************************\n";
}
OPENCL_V( status, _T( "clBuildProgram failed" ) );
}
lookup.setProgram(program, source);
lookup.populateCache();
}
fftRepo.setclProgram( this->getGenerator(), this->getSignatureData(), program, q_device, fftPlan->context );
// For real transforms we compile either forward or backward kernel
bool buildFwdKernel = buildForwardKernel();
bool buildBwdKernel = buildBackwardKernel();
// get a kernel object handle for a kernel with the given name
cl_kernel kernel;
if( buildFwdKernel )
{
lockRAII *kernelLock;
if( fftRepo.getclKernel( program, CLFFT_FORWARD, kernel, kernelLock) == CLFFT_INVALID_KERNEL )
{
std::string entryPoint;
OPENCL_V( fftRepo.getProgramEntryPoint( this->getGenerator(), this->getSignatureData(), CLFFT_FORWARD, entryPoint, q_device, fftPlan->context ), _T( "fftRepo.getProgramEntryPoint failed." ) );
kernel = clCreateKernel( program, entryPoint.c_str( ), &status );
OPENCL_V( status, _T( "clCreateKernel failed" ) );
fftRepo.setclKernel( program, CLFFT_FORWARD, kernel );
}
}
if( buildBwdKernel )
{
lockRAII *kernelLock;
if( fftRepo.getclKernel( program, CLFFT_BACKWARD, kernel, kernelLock ) == CLFFT_INVALID_KERNEL )
{
std::string entryPoint;
OPENCL_V( fftRepo.getProgramEntryPoint( this->getGenerator(), this->getSignatureData(), CLFFT_BACKWARD, entryPoint, q_device, fftPlan->context ), _T( "fftRepo.getProgramEntryPoint failed." ) );
kernel = clCreateKernel( program, entryPoint.c_str( ), &status );
OPENCL_V( status, _T( "clCreateKernel failed" ) );
fftRepo.setclKernel( program, CLFFT_BACKWARD, kernel );
}
}
}
return CLFFT_SUCCESS;
}