-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcaldgemm_opencl.cpp
2652 lines (2424 loc) · 106 KB
/
caldgemm_opencl.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
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* CPU side of CALDGEMM implementation.
*
* Copyright 2015:
* - David Rohr ([email protected])
* - Matthias Bach ([email protected])
* - Matthias Kretz ([email protected])
*
* This file is part of CALDGEMM.
*
* CALDGEMM is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CALDGEMM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with CALDGEMM. If not, see <http://www.gnu.org/licenses/>.
*/
#include "caldgemm_opencl.h"
#include <CL/cl_ext.h>
#include <algorithm>
#ifndef _WIN32
#include <syscall.h>
#include <unistd.h>
#include <sys/mman.h>
#ifndef MAP_HUGETLB
#define MAP_HUGETLB 0x40000 /* arch specific */
#endif
#ifndef MPOL_DEFAULT
#define MPOL_DEFAULT 0
#endif
#ifndef MPOL_PREFERRED
#define MPOL_PREFERRED 1
#endif
#ifndef MPOL_BIND
#define MPOL_BIND 2
#endif
#ifndef MPOL_INTERLEAVE
#define MPOL_INTERLEAVE 3
#endif
#endif
#define OCL_KERNEL_PRE \
"#ifdef cl_amd_fp64\n" \
"#pragma OPENCL EXTENSION cl_amd_fp64 : enable\n" \
"#else\n" \
"#pragma OPENCL EXTENSION cl_khr_fp64 : enable\n" \
"#endif\n" \
"//const sampler_t sampler = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_NONE | CLK_FILTER_NEAREST;\n" \
"\n"
#ifdef CALDGEMM_OPENCL_EMULATE_STRIDED
inline cl_int clEnqueueReadBufferRectUse (cl_command_queue command_queue, cl_mem buffer, cl_bool blocking_read, const size_t buffer_origin[3], const size_t host_origin[3], const size_t region[3], size_t buffer_row_pitch, size_t buffer_slice_pitch,
size_t host_row_pitch, size_t host_slice_pitch, void *ptr, cl_uint num_events_in_wait_list, const cl_event *event_wait_list, cl_event *event)
{
for (unsigned int i = 0;i < region[1];i++)
{
cl_int retVal = clEnqueueReadBuffer(command_queue, buffer, blocking_read, i * region[0], region[0], (char*) ptr + i * host_row_pitch, i == 0 ? num_events_in_wait_list : 0, i == 0 ? event_wait_list : NULL, i == region[1] - 1 ? event : NULL);
if (retVal != CL_SUCCESS) return(retVal);
}
return(CL_SUCCESS);
}
inline cl_int clEnqueueWriteBufferRectUse (cl_command_queue command_queue, cl_mem buffer, cl_bool blocking_read, const size_t buffer_origin[3], const size_t host_origin[3], const size_t region[3], size_t buffer_row_pitch, size_t buffer_slice_pitch,
size_t host_row_pitch, size_t host_slice_pitch, const void *ptr, cl_uint num_events_in_wait_list, const cl_event *event_wait_list, cl_event *event)
{
for (unsigned int i = 0;i < region[1];i++)
{
cl_int retVal = clEnqueueWriteBuffer(command_queue, buffer, blocking_read, i * region[0], region[0], (const char*) ptr + i * host_row_pitch, i == 0 ? num_events_in_wait_list : 0, i == 0 ? event_wait_list : NULL, i == region[1] - 1 ? event : NULL);
if (retVal != CL_SUCCESS) return(retVal);
}
return(CL_SUCCESS);
}
#elif defined(CALDGEMM_OPENCL_USE_ORIGINAL_POINTERS)
inline cl_int clEnqueueReadBufferRectUse (cl_command_queue command_queue, cl_mem buffer, cl_bool blocking_read, const size_t buffer_origin[3], const size_t host_origin[3], const size_t region[3], size_t buffer_row_pitch, size_t buffer_slice_pitch,
size_t host_row_pitch, size_t host_slice_pitch, void *ptr, cl_uint num_events_in_wait_list, const cl_event *event_wait_list, cl_event *event)
{
void* orig_ptr = NULL;
size_t offset = 0;
if (caldgemm_opencl::GetMemoryInfo(NULL, &orig_ptr, &offset, ptr)) return(CL_INVALID_MIP_LEVEL);
size_t offset_origin[3] = {offset % host_row_pitch, offset / host_row_pitch, 0};
return clEnqueueReadBufferRect(command_queue, buffer, blocking_read, buffer_origin, offset_origin, region, buffer_row_pitch, buffer_slice_pitch, host_row_pitch, host_slice_pitch, orig_ptr, num_events_in_wait_list, event_wait_list, event);
}
inline cl_int clEnqueueWriteBufferRectUse (cl_command_queue command_queue, cl_mem buffer, cl_bool blocking_read, const size_t buffer_origin[3], const size_t host_origin[3], const size_t region[3], size_t buffer_row_pitch, size_t buffer_slice_pitch,
size_t host_row_pitch, size_t host_slice_pitch, const void *ptr, cl_uint num_events_in_wait_list, const cl_event *event_wait_list, cl_event *event)
{
void* orig_ptr = NULL;
size_t offset = 0;
if (caldgemm_opencl::GetMemoryInfo(NULL, &orig_ptr, &offset, ptr)) return(CL_INVALID_MIP_LEVEL);
size_t offset_origin[3] = {offset % host_row_pitch, offset / host_row_pitch, 0};
return clEnqueueWriteBufferRect(command_queue, buffer, blocking_read, buffer_origin, offset_origin, region, buffer_row_pitch, buffer_slice_pitch, host_row_pitch, host_slice_pitch, orig_ptr, num_events_in_wait_list, event_wait_list, event);
}
#else
#define clEnqueueWriteBufferRectUse clEnqueueWriteBufferRect
#define clEnqueueReadBufferRectUse clEnqueueReadBufferRect
#endif
#define OCLKernelName OCLKernel
#include "caldgemm.cl"
#undef OCLKernelName
#define OCLKernelName OCLKernelALPHA1
#define CALDGEMM_ALPHA1
#include "caldgemm.cl"
#undef OCLKernelName
#define OCLKernelName OCLKernelLinpack
#define CALDGEMM_LINPACK_KERNEL
#include "caldgemm.cl"
#undef CALDGEMM_LINPACK_KERNEL
#undef CALDGEMM_ALPHA1
#undef OCLKernelName
#ifndef _WIN32
#include <dlfcn.h>
#include <sys/mman.h>
#endif
const char* caldgemm_opencl::OCLConvertKernel =
OCL_KERNEL_PRE
"__kernel void oclconkernel(__global const uint4* __restrict const iBuffer, __global uint4* const oBuffer, int width, int height, int transpose)\n"
"{\n"
" int i, j;\n"
" for (i = get_global_id(1);i < height / 2;i+=get_global_size(1))\n"
" {\n"
" for (j = get_global_id(0);j < width / 2;j+=get_global_size(0))\n"
" {\n"
" uint4 tmp, tmp2;\n"
" tmp = iBuffer[(2 * i) * (width / 2) + j];\n"
" tmp2 = iBuffer[(2 * i + 1) * (width / 2) + j];\n"
" uint4 val, val2;\n"
" if (transpose)\n"
" {\n"
" uint4 val, val2;\n"
" val.x = tmp.x;val.y = tmp.y;\n"
" val.z = tmp2.x;val.w = tmp2.y;\n"
" val2.x = tmp.z;val2.y = tmp.w;\n"
" val2.z = tmp2.z;val2.w = tmp2.w;\n"
" oBuffer[(2 * j) * (height / 2) + i] = val;\n"
" oBuffer[(2 * j + 1) * (height / 2) + i] = val2;\n"
" }\n"
" else\n"
" {\n"
" oBuffer[(2 * i) * (width / 2) + j] = tmp;\n"
" oBuffer[(2 * i + 1) * (width / 2) + j] = tmp2;\n"
" }\n"
" }\n"
" }\n"
"}\n"
;
const char* caldgemm_opencl::OCLConvertKernelTex =
OCL_KERNEL_PRE
"__kernel void oclconkernel(__global const uint4* iBuffer, __write_only image2d_t oBuffer, int width, int height, int transpose)\n"
"{\n"
" int i, j;\n"
" for (i = get_global_id(1);i < height / 2;i+=get_global_size(1))\n"
" {\n"
" for (j = get_global_id(0);j < width / 2;j+=get_global_size(0))\n"
" {\n"
" uint4 tmp, tmp2;\n"
" tmp = iBuffer[(2 * i) * (width / 2) + j];\n"
" tmp2 = iBuffer[(2 * i + 1) * (width / 2) + j];\n"
" int2 coord, coord2;\n"
" uint4 val, val2;\n"
" if (transpose)\n"
" {\n"
" val.x = tmp.x;val.y = tmp.y;\n"
" val.z = tmp2.x;val.w = tmp2.y;\n"
" val2.x = tmp.z;val2.y = tmp.w;\n"
" val2.z = tmp2.z;val2.w = tmp2.w;\n"
" coord.x = i;\n"
" coord.y = 2 * j;\n"
" coord2.x = i;\n"
" coord2.y = 2 * j + 1;\n"
" }\n"
" else\n"
" {\n"
" coord.x = j;\n"
" coord.y = 2 * i;\n"
" coord2.x = j;\n"
" coord2.y = 2 * i + 1;\n"
" val = tmp;\n"
" val2 = tmp2;\n"
" }\n"
" write_imageui(oBuffer, coord, val);\n"
" write_imageui(oBuffer, coord2, val2);\n"
" }\n"
" }\n"
"}\n"
;
static const char* opencl_error_string(int errorcode)
{
switch (errorcode)
{
case CL_SUCCESS: return "Success!";
case CL_DEVICE_NOT_FOUND: return "Device not found.";
case CL_DEVICE_NOT_AVAILABLE: return "Device not available";
case CL_COMPILER_NOT_AVAILABLE: return "Compiler not available";
case CL_MEM_OBJECT_ALLOCATION_FAILURE: return "Memory object allocation failure";
case CL_OUT_OF_RESOURCES: return "Out of resources";
case CL_OUT_OF_HOST_MEMORY: return "Out of host memory";
case CL_PROFILING_INFO_NOT_AVAILABLE: return "Profiling information not available";
case CL_MEM_COPY_OVERLAP: return "Memory copy overlap";
case CL_IMAGE_FORMAT_MISMATCH: return "Image format mismatch";
case CL_IMAGE_FORMAT_NOT_SUPPORTED: return "Image format not supported";
case CL_BUILD_PROGRAM_FAILURE: return "Program build failure";
case CL_MAP_FAILURE: return "Map failure";
case CL_INVALID_VALUE: return "Invalid value";
case CL_INVALID_DEVICE_TYPE: return "Invalid device type";
case CL_INVALID_PLATFORM: return "Invalid platform";
case CL_INVALID_DEVICE: return "Invalid device";
case CL_INVALID_CONTEXT: return "Invalid context";
case CL_INVALID_QUEUE_PROPERTIES: return "Invalid queue properties";
case CL_INVALID_COMMAND_QUEUE: return "Invalid command queue";
case CL_INVALID_HOST_PTR: return "Invalid host pointer";
case CL_INVALID_MEM_OBJECT: return "Invalid memory object";
case CL_INVALID_IMAGE_FORMAT_DESCRIPTOR: return "Invalid image format descriptor";
case CL_INVALID_IMAGE_SIZE: return "Invalid image size";
case CL_INVALID_SAMPLER: return "Invalid sampler";
case CL_INVALID_BINARY: return "Invalid binary";
case CL_INVALID_BUILD_OPTIONS: return "Invalid build options";
case CL_INVALID_PROGRAM: return "Invalid program";
case CL_INVALID_PROGRAM_EXECUTABLE: return "Invalid program executable";
case CL_INVALID_KERNEL_NAME: return "Invalid kernel name";
case CL_INVALID_KERNEL_DEFINITION: return "Invalid kernel definition";
case CL_INVALID_KERNEL: return "Invalid kernel";
case CL_INVALID_ARG_INDEX: return "Invalid argument index";
case CL_INVALID_ARG_VALUE: return "Invalid argument value";
case CL_INVALID_ARG_SIZE: return "Invalid argument size";
case CL_INVALID_KERNEL_ARGS: return "Invalid kernel arguments";
case CL_INVALID_WORK_DIMENSION: return "Invalid work dimension";
case CL_INVALID_WORK_GROUP_SIZE: return "Invalid work group size";
case CL_INVALID_WORK_ITEM_SIZE: return "Invalid work item size";
case CL_INVALID_GLOBAL_OFFSET: return "Invalid global offset";
case CL_INVALID_EVENT_WAIT_LIST: return "Invalid event wait list";
case CL_INVALID_EVENT: return "Invalid event";
case CL_INVALID_OPERATION: return "Invalid operation";
case CL_INVALID_GL_OBJECT: return "Invalid OpenGL object";
case CL_INVALID_BUFFER_SIZE: return "Invalid buffer size";
case CL_INVALID_MIP_LEVEL: return "Invalid mip-map level";
default: return "Unknown Errorcode";
}
}
#define ERRRET(...) {fprintf(STD_OUT, __VA_ARGS__);fprintf(STD_OUT, "\n");return(1);}
#define CHKRET(result, ...) \
{ \
const cl_int tmp_ocl_error = result; \
if (tmp_ocl_error != CL_SUCCESS) \
{ \
fprintf(STD_OUT, __VA_ARGS__); \
fprintf(STD_OUT, ":\n"); \
fprintf(STD_OUT, "OpenCL Error %d: (%s: %d) %s\n", tmp_ocl_error, __FILE__, __LINE__, opencl_error_string(tmp_ocl_error)); \
return(1); \
} \
}
caldgemm_opencl::caldgemm_opencl() : caldgemm()
{
}
caldgemm_opencl::~caldgemm_opencl()
{
}
caldgemm_opencl::caldgemm_config_backend_opencl::caldgemm_config_backend_opencl()
{
size = sizeof(*this);
kernelLib = NULL;
allowCPUDevice = false;
}
caldgemm::caldgemm_config_backend* caldgemm_opencl::create_caldgemm_config_backend()
{
return(new caldgemm_config_backend_opencl);
}
void caldgemm_opencl::caldgemm_config_backend_opencl::printConfig(caldgemm_config_backend* oldConfigA)
{
caldgemm_config_backend_opencl* oldConfig = (caldgemm_config_backend_opencl*) oldConfigA;
caldgemm_config_backend_opencl* const newConfig = this;
caldgemm_config_backend_opencl* const myConfig = this;
PRINT_CONFIG_STRING(kernelLib);
PRINT_CONFIG_INT(allowCPUDevice);
}
caldgemm_opencl::caldgemm_config_backend_opencl::~caldgemm_config_backend_opencl() {}
int caldgemm_opencl::caldgemm_config_backend_opencl::ParseBackendOptions(unsigned int argc, char** argv)
{
caldgemm::caldgemm_config* Config = NULL; //Should never be accessed if caldgemm_parse_parameterts.h is correct
#define CALDGEMM_PARAMETERS_BACKEND
#include "caldgemm_parse_parameters.h"
#undef CALDGEMM_PARAMETERS_BACKEND
return(0);
}
int caldgemm_opencl::WaitForEventAndRelease(cl_event* pEvent, int lock)
{
cl_int ocl_error;
if (lock == -1) lock = (Config->ThreadSaveDriver == -1);
if (Config->Debug) fprintf(STD_OUT, "\t\t\tOpenCL WaitForEventAndRelease: 0x%p\n", pEvent);
if (lock)
{
cl_int status;
do
{
if (Config->ThreadSaveDriver == -1) pthread_mutex_lock(&globalDriverLock);
if (clGetEventInfo(*pEvent, CL_EVENT_COMMAND_EXECUTION_STATUS, sizeof(status), &status, NULL) != CL_SUCCESS)
{
fprintf(STD_OUT, "Error querying event info\n");
return(1);
}
if (Config->ThreadSaveDriver == -1) pthread_mutex_unlock(&globalDriverLock);
} while (status != CL_COMPLETE);
}
else
{
if ((ocl_error = clWaitForEvents(1, pEvent)) != CL_SUCCESS)
{
fprintf(STD_OUT, "Error while waiting for event (%d: %s)\n", ocl_error, opencl_error_string(ocl_error));
return(1);
}
}
if (Config->ThreadSaveDriver == -1) pthread_mutex_lock(&globalDriverLock);
if ((ocl_error = clReleaseEvent(*pEvent)) != CL_SUCCESS)
{
fprintf(STD_OUT, "Error releasing event (%d: %s)\n", ocl_error, opencl_error_string(ocl_error));
return(1);
}
if (Config->ThreadSaveDriver == -1) pthread_mutex_unlock(&globalDriverLock);
return(0);
}
int caldgemm_opencl::WaitForEvent(int a, int b, int mustlock)
{
if (Config->SimpleGPUQueuing) return(0);
if (Config->Debug) fprintf(STD_OUT, "\tWaiting for event from device %d obuffer %d...\n", b, a);
return(WaitForEventAndRelease(&ocl_events[b][a], mustlock || Config->ThreadSaveDriver == -1));
}
int caldgemm_opencl::Initialize(bool nocalinit)
{
int deviceNum = Config->DeviceNum;
if (!Config->Quiet) fprintf(STD_OUT, "Initializing CALDGEMM (OpenCL Runtime)\n");
if (Config->Debug) fprintf(STD_OUT, "OPENCL Initialice\n");
cl_int ocl_error;
cl_uint num_devices;
CHKRET(clGetDeviceIDs(ocl_platform, CL_DEVICE_TYPE_ALL, 0, NULL, &num_devices), "Error getting device IDs");
nDevices = num_devices;
if (nDevices == 0) ERRRET("No OpenCL device for this platform found\n");
if (Config->Debug) fprintf(STD_OUT, "%d OpenCL devices found for this platform\n", nDevices);
cl_device_id* devices = new cl_device_id[nDevices];
if (devices == NULL) ERRRET("Memory allocation error\n");
CHKRET(clGetDeviceIDs(ocl_platform, CL_DEVICE_TYPE_ALL, nDevices, devices, NULL), "Error getting OpenCL devices");
int gooddevices = 0;
int cpu_found = 0;
cl_device_id cpu_device;
for (int i = 0;i < nDevices;i++)
{
char device_vendor[64], device_name[64];
cl_device_type device_type;
cl_uint nbits;
CHKRET(clGetDeviceInfo(devices[i], CL_DEVICE_NAME, 64, device_name, NULL), "Error getting device info");
CHKRET(clGetDeviceInfo(devices[i], CL_DEVICE_VENDOR, 64, device_vendor, NULL), "Error getting device info");
CHKRET(clGetDeviceInfo(devices[i], CL_DEVICE_TYPE, sizeof(cl_device_type), &device_type, NULL), "Error getting device info");
CHKRET(clGetDeviceInfo(devices[i], CL_DEVICE_ADDRESS_BITS, sizeof(nbits), &nbits, NULL), "Error getting device info");
int device_ok = config_backend->allowCPUDevice ?
(device_type & (CL_DEVICE_TYPE_CPU | CL_DEVICE_TYPE_GPU)) :
(device_type & CL_DEVICE_TYPE_GPU) && !(device_type & CL_DEVICE_TYPE_CPU);
if (Config->Debug) fprintf(STD_OUT, "Device %d -> %d: %s %s (%d bits)\n", i, device_ok ? gooddevices : -1, device_vendor, device_name, nbits);
if (device_ok)
{
devices[gooddevices++] = devices[i];
}
else if (device_type & CL_DEVICE_TYPE_CPU)
{
cpu_found = 1;
cpu_device = devices[i];
}
}
if (cpu_found == 0)
{
if (config_backend->allowCPUDevice == false && Config->CPUInContext)
{
ERRRET("No CPU OpenCL device found for mapping buffers\n");
}
else
{
Config->CPUInContext = false;
}
}
if (gooddevices == 0)
{
ERRRET("No OpenCL GPU device found\n");
}
nDevices = gooddevices;
if (nDevices > (signed) max_devices) nDevices = max_devices;
if (nDevices > Config->NumDevices) nDevices = Config->NumDevices;
if (deviceNum >= nDevices) ERRRET("OpenCL Device %d not available\n", deviceNum);
if (deviceNum >= 0) nDevices = 1;
if (!Config->UseGPU) nDevices = 0;
gpu_available = (nDevices > 0);
if (nDevices > 1 && !Config->MultiThread)
{
fprintf(STD_OUT, "Cannot use multiple devices without multithreading\n");
nDevices = 1;
}
for (int i = 0;i < nDevices;i++)
{
if (deviceNum >= 0) ocl_devices[i] = devices[deviceNum];
else ocl_devices[i] = devices[Config->DeviceNums[i]];
}
ocl_devices[nDevices] = cpu_device;
delete[] devices;
ocl_context = clCreateContext(NULL, nDevices + (Config->CPUInContext ? 1 : 0), ocl_devices, NULL, NULL, &ocl_error);
CHKRET(ocl_error, "Error creating OpenCL context");
for (int i = 0;i < nDevices;i++)
{
for (int j = 0;j < (Config->AlternateSimpleQueuing ? 3 : obuffercount);j++)
{
#ifdef CL_VERSION_2_0
cl_queue_properties flags[] = {CL_QUEUE_PROPERTIES, 0, 0};
if (Config->VerboseTiming || (Config->PipelinedOperation && CALDGEMM_OPENCL_PROFILED_PIPELINE)) flags[1] |= CL_QUEUE_PROFILING_ENABLE;
ocl_command_queues[i][j] = clCreateCommandQueueWithProperties(ocl_context, ocl_devices[i], flags, &ocl_error);
#else
cl_command_queue_properties flags = 0;
if (Config->VerboseTiming || (Config->PipelinedOperation && CALDGEMM_OPENCL_PROFILED_PIPELINE)) flags |= CL_QUEUE_PROFILING_ENABLE;
ocl_command_queues[i][j] = clCreateCommandQueue(ocl_context, ocl_devices[i], flags, &ocl_error);
#endif
CHKRET(ocl_error, "Error creating OpenCL command queue");
}
if (Config->AsyncSideQueue)
{
#ifdef CL_VERSION_2_0
ocl_async_queue[i] = clCreateCommandQueueWithProperties(ocl_context, ocl_devices[i], NULL, &ocl_error);
#else
ocl_async_queue[i] = clCreateCommandQueue(ocl_context, ocl_devices[i], 0, &ocl_error);
#endif
CHKRET(ocl_error, "Error creating async OpenCL command queue");
}
}
if (Config->CPUInContext)
{
#ifdef CL_VERSION_2_0
ocl_command_queue_cpu = clCreateCommandQueueWithProperties(ocl_context, ocl_devices[nDevices], NULL, &ocl_error);
#else
ocl_command_queue_cpu = clCreateCommandQueue(ocl_context, ocl_devices[nDevices], 0, &ocl_error);
#endif
CHKRET(ocl_error, "Error creating OpenCL CPU command queue");
}
AlternateLookaheadDoneMutexSQ.Lock();
return(0);
}
int caldgemm_opencl::ValidateRuntime()
{
if (Config->Debug) fprintf(STD_OUT, "OPENCL ValidateRuntime\n");
Config->MultiThreadDivide = false;
if (Config->ThreadSaveDriver != -1) Config->ThreadSaveDriver = 1;
if (Config->GPU_C == -1) Config->GPU_C = 1;
if (Config->SimpleGPUQueuing)
{
if (Config->GPU_C == 0)
{
fprintf(STD_OUT, "SimpleGPUQueuing works only in combination with GPU_C\n");
return(1);
}
if (Config->NoConcurrentKernels)
{
fprintf(STD_OUT, "Automatically disabling NoConcurrentKernels when SimpleGPUQueuing is active...\n");
Config->NoConcurrentKernels = 0;
}
}
cl_uint num_platforms;
CHKRET(clGetPlatformIDs(0, NULL, &num_platforms), "Error getting OpenCL Platform Count");
if (num_platforms == 0) ERRRET("No OpenCL Platform found\n");
if (Config->Debug) fprintf(STD_OUT, "%d OpenCL Platforms found\n", num_platforms);
cl_platform_id* platforms = new cl_platform_id[num_platforms];
if (platforms == NULL) ERRRET("Memory allocation error");
CHKRET(clGetPlatformIDs(num_platforms, platforms, NULL), "Error getting OpenCL Platforms");
for (unsigned int i = 0;i < num_platforms;i++)
{
char platform_profile[64], platform_version[64], platform_name[64], platform_vendor[64];
CHKRET(clGetPlatformInfo(platforms[i], CL_PLATFORM_PROFILE, 64, platform_profile, NULL), "Error getting platform info");
CHKRET(clGetPlatformInfo(platforms[i], CL_PLATFORM_VERSION, 64, platform_version, NULL), "Error getting platform info");
CHKRET(clGetPlatformInfo(platforms[i], CL_PLATFORM_NAME, 64, platform_name, NULL), "Error getting platform info");
CHKRET(clGetPlatformInfo(platforms[i], CL_PLATFORM_VENDOR, 64, platform_vendor, NULL), "Error getting platform info");
if (Config->Debug) fprintf(STD_OUT, "Platform %d: (%s %s) %s %s\n", i, platform_profile, platform_version, platform_vendor, platform_name);
}
if (Config->OpenCLPlatform >= (signed) num_platforms) ERRRET("OpenCL Platform %d not available\n", Config->OpenCLPlatform);
ocl_platform = platforms[Config->OpenCLPlatform];
delete[] platforms;
#ifdef CL_VERSION_2_0
{
char platform_version[64], info[64];
double version;
CHKRET(clGetPlatformInfo(ocl_platform, CL_PLATFORM_VERSION, 64, platform_version, NULL), "Error getting platform info");
sscanf(platform_version, "OpenCL %lf %s", &version, info);
if (version < 2.0)
{
fprintf(STD_OUT, "This binary was compiled with OpenCL 2.0 headers and uses OpenCL 2.0 features, but your runtime is only at version %.1lf\n", version);
return(1);
}
}
#endif
if (Config->config_backend == NULL) Config->config_backend = new caldgemm_config_backend_opencl;
config_backend = (caldgemm_config_backend_opencl*) Config->config_backend;
if (config_backend->kernelLib != NULL)
{
if (Config->PrintILKernel)
{
fprintf(STD_OUT, "Cannot print kernel from 3ed party library\n");
}
#ifdef _WIN32
kernelLib = LoadLibrary(config_backend->kernelLib);
#else
kernelLib = dlopen(config_backend->kernelLib, RTLD_LAZY|RTLD_GLOBAL);
#endif
if (kernelLib == NULL)
{
fprintf(STD_OUT, "Error opening kernel library: %s\n", config_backend->kernelLib);
return(1);
}
#ifdef _WIN32
kernelLibCreate = (cl_kernel (*) (cl_context*, int, cl_device_id*, int, int, int)) GetProcAddress(kernelLib, "kernelLibCreate");
kernelLibQuerySettings = (void (*) (int*, int*, bool*, bool*, bool*, int*, int*, int*, int*)) GetProcAddress(kernelLib, "kernelLibQuerySettings");
kernelLibTerminate = (void (*) ()) GetProcAddress(kernelLib, "kernelLibTerminate");
kernelLibSuggestedMaxHeight = (size_t (*) ()) GetProcAddress(kernelLib, "suggestedMaxHeight");
kernelLibGetAutoHeight = (size_t (*) (size_t, size_t, int, size_t)) GetProcAddress(kernelLib, "getAutoHeight");
kernelLibModHeight = (void (*) (size_t, size_t)) GetProcAddress(kernelLib, "modHeight");
kernelLibInitialize = (int (*) (cl_platform_id)) GetProcAddress(kernelLib, "kernelLibInitialize");
#else
kernelLibCreate = (cl_kernel (*)(cl_context*, int, cl_device_id*, int, int, int)) dlsym(kernelLib, "kernelLibCreate");
kernelLibQuerySettings = (void (*) (int*, int*, bool*, bool*, bool*, int*, int*, int*, int*)) dlsym(kernelLib, "kernelLibQuerySettings");
kernelLibTerminate = (void (*) ()) dlsym(kernelLib, "kernelLibTerminate");
kernelLibSuggestedMaxHeight = (size_t (*) ()) dlsym(kernelLib, "suggestedMaxHeight");
kernelLibGetAutoHeight = (size_t (*) (size_t, size_t, int, size_t)) dlsym(kernelLib, "getAutoHeight");
kernelLibModHeight = (void (*) (size_t, size_t)) dlsym(kernelLib, "modHeight");
kernelLibInitialize = (int (*) (cl_platform_id)) dlsym(kernelLib, "kernelLibInitialize");
#endif
if (kernelLibCreate == NULL || kernelLibQuerySettings == NULL || kernelLibTerminate == NULL || kernelLibSuggestedMaxHeight == NULL || kernelLibGetAutoHeight == NULL || kernelLibModHeight == NULL || kernelLibInitialize == NULL)
{
fprintf(STD_OUT, "Error getting function pointer from external library (%p %p %p)\n", kernelLibCreate, kernelLibQuerySettings, kernelLibTerminate);
return(1);
}
if (kernelLibInitialize(ocl_platform))
{
fprintf(STD_OUT, "3rd Party DGEMM Library failed to initialize\n");
return(1);
}
kernelLibQuerySettings(&KernelSettings.tiling_x, &KernelSettings.tiling_y, &KernelSettings.transposeA, &KernelSettings.transposeB, &KernelSettings.texture_buffers, &KernelSettings.group_size_x, &KernelSettings.group_size_y, &KernelSettings.min_tile_size, &KernelSettings.min_k);
if (Config->Height == 0)
{
Config->Height = kernelLibSuggestedMaxHeight();
}
}
else
{
//Do not set default kernel settings
#ifdef CALDGEMM_TRANSPOSED_A
KernelSettings.transposeA = true;
#else
KernelSettings.transposeA = false;
#endif
#ifdef CALDGEMM_TRANSPOSED_B
KernelSettings.transposeB = true;
#else
KernelSettings.transposeB = false;
#endif
#ifdef OCL_USE_SIMPLE_BUFFERS
KernelSettings.texture_buffers = false;
#else
KernelSettings.texture_buffers = true;
#endif
KernelSettings.tiling_x = OCL_TILING_X;
KernelSettings.tiling_y = OCL_TILING_Y;
KernelSettings.group_size_x = OCL_GROUP_SIZE_X;
KernelSettings.group_size_y = OCL_GROUP_SIZE_Y;
KernelSettings.min_tile_size = 32;
KernelSettings.min_k = 4;
}
if (!(KernelSettings.transposeA ^ KernelSettings.transposeB))
{
fprintf(STD_OUT, "Must set either transposed A or transposed B\n");
return(1);
}
return(0);
}
int caldgemm_opencl::CheckDevices()
{
return(0);
}
int caldgemm_opencl::InitDevices()
{
if (Config->Debug) fprintf(STD_OUT, "OPENCL InitDevices\n");
cl_int ocl_error;
int num_bbuffers;
if (Config->max_bbuffers) num_bbuffers = Config->max_bbuffers;
else if (Config->DstMemory == 'g') num_bbuffers = max_bbuffers_g;
else num_bbuffers = max_bbuffers;
if (num_bbuffers > max_bbuffers)
{
fprintf(STD_OUT, "Requested number of bbuffers (%d) larger than max_bbuffers constant (%d)!", num_bbuffers, max_bbuffers);
return(1);
}
SetupBufferSizes();
cl_image_format ocl_image_format;
ocl_image_format.image_channel_order = CL_RGBA;
ocl_image_format.image_channel_data_type = CL_UNSIGNED_INT32;
cpu_set_t tmpmask, oldtmpmask;
sched_getaffinity(0, sizeof(oldtmpmask), &oldtmpmask);
for (int i = 0;i < nDevices;i++)
{
if (Config->AllocMapping[i] != -1)
{
CPU_ZERO(&tmpmask);
CPU_SET(Config->AllocMapping[i] + Config->CPUCoreOffset, &tmpmask);
sched_setaffinity(0, sizeof(tmpmask), &tmpmask);
}
for (int k = 0;k < (Config->PipelineDoubleBuffer ? 2 : 1);k++)
{
for (int j = 0;j < ibuffercount;j++)
{
if (!KernelSettings.texture_buffers)
{
ocl_abuffers[k][i][j] = clCreateBuffer(ocl_context, CL_MEM_READ_WRITE, BufferWidth * BufferHeight * sizeof(double), NULL, &ocl_error);
}
else
{
cl_image_desc image_desc;
memset(&image_desc, 0, sizeof(image_desc));
image_desc.image_type = CL_MEM_OBJECT_IMAGE2D;
if (KernelSettings.transposeB)
{
image_desc.image_width = BufferWidth / 2;
image_desc.image_height = BufferHeight;
ocl_abuffers[k][i][j] = clCreateImage(ocl_context, CL_MEM_READ_WRITE, &ocl_image_format, &image_desc, NULL, &ocl_error);
}
else //must be transposeA
{
image_desc.image_width = BufferHeight / 2;
image_desc.image_height = BufferWidth;
ocl_abuffers[k][i][j] = clCreateImage(ocl_context, CL_MEM_READ_WRITE, &ocl_image_format, &image_desc, NULL, &ocl_error);
}
}
CHKRET(ocl_error, "Error allocating device memory (A)");
}
CHKRET(clEnqueueMigrateMemObjects(ocl_command_queues[i][0], ibuffercount, &ocl_abuffers[k][i][0], 0, 0, NULL, NULL), "Error migrating mem object");
for (int j = 0;j < obuffercount;j++)
{
if (Config->DstMemory == 'g')
{
ocl_cbuffers[k][i][j] = clCreateBuffer(ocl_context, CL_MEM_READ_WRITE, BufferHeight * BufferHeight * sizeof(double), NULL, &ocl_error);
CHKRET(ocl_error, "Error allocating device memory (C)");
}
if (Config->GPU_C == 0 && k == 0)
{
ocl_tmp_cbuffers[i][j] = clCreateBuffer(ocl_context, CL_MEM_READ_WRITE | CL_MEM_ALLOC_HOST_PTR, BufferHeight * BufferHeight * sizeof(double), NULL, &ocl_error);
CHKRET(ocl_error, "Error allocating host memory (C tmp)");
ocl_tmp_cbuffers_ptr[i][j] = (double*) clEnqueueMapBuffer(ocl_command_queues[i][0], ocl_tmp_cbuffers[i][j], CL_TRUE, CL_MAP_READ, 0, BufferHeight * BufferHeight * sizeof(double), 0, NULL, NULL, &ocl_error);
CHKRET(ocl_error, "Error mapping host memory (C tmp)");
memset(ocl_tmp_cbuffers_ptr[i][j], 0, BufferHeight * BufferHeight * sizeof(double));
if (Config->DstMemory == 'g')
{
CHKRET(clEnqueueWriteBuffer(ocl_command_queues[i][0], ocl_cbuffers[k][i][j], CL_TRUE, 0, BufferHeight * BufferHeight * sizeof(double), ocl_tmp_cbuffers_ptr[i][j], 0, NULL, NULL), "Error initializing GPU buffer with zero");
}
}
}
if (Config->DstMemory == 'g') CHKRET(clEnqueueMigrateMemObjects(ocl_command_queues[i][0], obuffercount, &ocl_cbuffers[k][i][0], 0, 0, NULL, NULL), "Error migrating mem object");
for (int j = 0;j < (Config->GPU_C ? obuffercount : ibuffercount);j++)
{
cl_int tmp_flags = Config->GPU_C ? CL_MEM_READ_ONLY : (CL_MEM_ALLOC_HOST_PTR | CL_MEM_READ_WRITE);
ocl_tmp_abuffers[k][i][j] = clCreateBuffer(ocl_context, tmp_flags, BufferWidth * BufferHeight * sizeof(double), NULL, &ocl_error);
CHKRET(ocl_error, "Error allocating device memory (A tmp - Width: %lld Height: %lld)", (long long int) BufferWidth, (long long int) BufferHeight);
ocl_tmp_bbuffers[k][i][j] = clCreateBuffer(ocl_context, tmp_flags, BufferWidth * BufferHeight * sizeof(double), NULL, &ocl_error);
CHKRET(ocl_error, "Error allocating device memory (B tmp)");
if (Config->GPU_C == 0 && k == 0)
{
ocl_tmp_abuffers_ptr[i][j] = (double*) clEnqueueMapBuffer(ocl_command_queues[i][0], ocl_tmp_abuffers[k][i][j], CL_TRUE, CL_MAP_WRITE, 0, BufferWidth * BufferHeight * sizeof(double), 0, NULL, NULL, &ocl_error);
CHKRET(ocl_error, "Error mapping buffer (A)");
ocl_tmp_bbuffers_ptr[i][j] = (double*) clEnqueueMapBuffer(ocl_command_queues[i][0], ocl_tmp_bbuffers[k][i][j], CL_TRUE, CL_MAP_WRITE, 0, BufferWidth * BufferHeight * sizeof(double), 0, NULL, NULL, &ocl_error);
CHKRET(ocl_error, "Error mapping buffer (B)");
}
}
if (Config->GPU_C)
{
CHKRET(clEnqueueMigrateMemObjects(ocl_command_queues[i][0], obuffercount, &ocl_tmp_abuffers[k][i][0], 0, 0, NULL, NULL), "Error migrating mem object");
CHKRET(clEnqueueMigrateMemObjects(ocl_command_queues[i][0], obuffercount, &ocl_tmp_bbuffers[k][i][0], 0, 0, NULL, NULL), "Error migrating mem object");
}
for (int j = 0;j < num_bbuffers;j++)
{
if (!KernelSettings.texture_buffers)
{
ocl_bbuffers[k][i][j] = clCreateBuffer(ocl_context, CL_MEM_READ_WRITE, BufferWidth * BufferHeight * sizeof(double), NULL, &ocl_error);
}
else
{
cl_image_desc image_desc;
memset(&image_desc, 0, sizeof(image_desc));
image_desc.image_type = CL_MEM_OBJECT_IMAGE2D;
if (KernelSettings.transposeB)
{
image_desc.image_width = BufferWidth / 2;
image_desc.image_height = BufferHeight;
ocl_bbuffers[k][i][j] = clCreateImage(ocl_context, CL_MEM_READ_WRITE, &ocl_image_format, &image_desc, NULL, &ocl_error);
}
else //must be transposeA
{
image_desc.image_width = BufferHeight / 2;
image_desc.image_height = BufferWidth;
ocl_bbuffers[k][i][j] = clCreateImage(ocl_context, CL_MEM_READ_WRITE, &ocl_image_format, &image_desc, NULL, &ocl_error);
}
}
if (ocl_error != CL_SUCCESS)
{
if (j < obuffercount)
{
CHKRET(ocl_error, "Error allocating device memory (B)");
}
else break;
}
ocl_error = clEnqueueMigrateMemObjects(ocl_command_queues[i][0], 1, &ocl_bbuffers[k][i][j], 0, 0, NULL, NULL);
if (ocl_error != CL_SUCCESS)
{
if (j < obuffercount)
{
CHKRET(ocl_error, "Error migrating device memory (B)");
}
else break;
}
bbuffers[i] = j + 1;
}
}
if (Config->Debug) fprintf(STD_OUT, "Allocated %d BBuffers on Device %d\n", bbuffers[i], i);
if (Config->AsyncSideQueue)
{
for (int j = 0;j < 4;j++)
{
ocl_async_buffers[i][j] = clCreateBuffer(ocl_context, CL_MEM_READ_WRITE, std::max<size_t>(BufferWidth, BufferHeight) * std::max<size_t>(BufferWidth, BufferHeight) * sizeof(double), NULL, &ocl_error);
CHKRET(ocl_error, "Error allocating async device memory %d/%d", i, j);
}
CHKRET(clEnqueueMigrateMemObjects(ocl_command_queues[i][0], 4, &ocl_async_buffers[i][0], 0, 0, NULL, NULL), "Error migrating async mem object");
}
clFinish(ocl_command_queues[i][0]); //Finish migrating memory objects
}
sched_setaffinity(0, sizeof(oldtmpmask), &oldtmpmask);
for (int j = 0;j < 3 + 1 + (Config->GPU_C ? 1 : 0) + (Config->AsyncDTRSM ? 2 : 0);j++)
{
if ((j != 3 || Config->Use3rdPartyTranspose) && config_backend->kernelLib != NULL)
{
if (Config->PrintILKernel)
{
fprintf(STD_OUT, "Cannot print kernel from 3ed party library\n");
}
for (int i = 0;i < nDevices;i++)
{
if (j < 5)
{
ocl_kernel[i][j] = kernelLibCreate(&ocl_context, nDevices, ocl_devices, j, Config->Width, Config->GPU_C == 0);
if (ocl_kernel[i][j] == 0)
{
fprintf(STD_OUT, "Error obtaining kernel from external library\n");
return(1);
}
}
if (Config->AsyncSideQueue && (j == 0 || j == 3 || j >= 5))
{
int num = j >= 5 ? j - 3 : (j == 3 ? 1 : 0);
ocl_async_kernel[i][num] = kernelLibCreate(&ocl_context, nDevices, ocl_devices, j, Config->Width, Config->GPU_C == 0);
if (ocl_async_kernel[i][num] == 0)
{
fprintf(STD_OUT, "Error obtaining async kernel from external library\n");
return(1);
}
}
}
}
else
{
if (j == 5)
{
fprintf(STD_OUT, "AsyncDTRSM only supported with 3rd party lib\n");
return(1);
}
const char* sourceCode;
switch (j)
{
case 0:
sourceCode = OCLKernel;
break;
case 1:
sourceCode = OCLKernelALPHA1;
break;
case 2:
sourceCode = OCLKernelLinpack;
break;
case 3:
sourceCode = KernelSettings.texture_buffers ? OCLConvertKernelTex : OCLConvertKernel;
break;
case 4:
sourceCode = OCLKernel;
break;
}
if (Config->PrintILKernel)
{
fprintf(STD_OUT, "OpenCL Kernel %d:\n%s\n\n", j, sourceCode);
}
ocl_program[j] = clCreateProgramWithSource(ocl_context, 1, &sourceCode, NULL, &ocl_error);
CHKRET(ocl_error, "Error creating program object");
ocl_error = clBuildProgram(ocl_program[j], nDevices, ocl_devices, Config->Disassemble ? "-save-temps=." : "", NULL, NULL);
if (ocl_error != CL_SUCCESS)
{
fprintf(STD_OUT, "OpenCL Error while building program: %d\n", ocl_error);
fprintf(STD_OUT, "OpenCL Kernel:\n\n%s\n\n", sourceCode);
char build_log[16384];
for (int i = 0;i < nDevices;i++)
{
clGetProgramBuildInfo(ocl_program[j], ocl_devices[i], CL_PROGRAM_BUILD_LOG, 16384, build_log, NULL);
fprintf(STD_OUT, "Build Log (device %d):\n\n%s\n\n", i, build_log);
}
return(1);
}
for (int i = 0;i < nDevices;i++)
{
ocl_kernel[i][j] = clCreateKernel(ocl_program[j], j == 3 ? "oclconkernel" : "oclkernel", &ocl_error);
CHKRET(ocl_error, "Error creating kernel");
if (Config->AsyncSideQueue && (j == 0 || j == 3))
{
ocl_async_kernel[i][j ? 1 : 0] = clCreateKernel(ocl_program[j], j == 3 ? "oclconkernel" : "oclkernel", &ocl_error);
CHKRET(ocl_error, "Error creating async kernel");
}
}
}
}
return(0);
}
int caldgemm_opencl::ReinitDevices()
{
if (Config->Debug) fprintf(STD_OUT, "OPENCL ReinitDevices\n");
fprintf(STD_OUT, "Reinit of OpenCL devices not supported yet\n");
return(1);
}
int caldgemm_opencl::InitConstantData(double alpha)
{
return(0);
}
int caldgemm_opencl::ExecuteKernels(caldgemm::DGEMMPrepareAndExecuteTask& Task, int blockm, int blockn)
{
if (Config->Debug) fprintf(STD_OUT, "OPENCL ExecuteKernels\n");
if (Config->ThreadSaveDriver == -1) pthread_mutex_lock(&globalDriverLock);
if (Config->Debug) fprintf(STD_OUT, "\tExecuting MM kernel (device %d obuffer %d, k=%lld m=%lld n=%lld)\n", Task.device, Task.j, (long long int) Task.k, (long long int) blockm, (long long int) blockn);
if (Config->GPU_C && Task.kernel_num == 2) Task.kernel_num = 4;
#ifdef REUSE_BBUFFERS
if (!DGEMM_favor_m && buffersSwitchable)
{
const int buffer_pos = buffer_pointers_A[Task.device][blockm] % (buffer_pointers_A[Task.device][blockm] < bbuffers[Task.device] ? bbuffers[Task.device] : ibuffercount);
CHKRET(clSetKernelArg(ocl_kernel[Task.device][Task.kernel_num], 1, sizeof(cl_mem), &ocl_bbuffers[pipelineBuffer][Task.device][buffer_pos]), "Error setting kernel memory A");
CHKRET(clSetKernelArg(ocl_kernel[Task.device][Task.kernel_num], 2, sizeof(cl_mem), &ocl_abuffers[pipelineBuffer][Task.device][buffer_pointers_B[Task.device][blockn] % ibuffercount]), "Error setting kernel memory B");
}
else
#endif
{
#ifdef REUSE_BBUFFERS
const bool buffersSufficiant = buffer_pointers_B[Task.device][blockn] < bbuffers[Task.device];
#else
const bool buffersSufficiant = false;
#endif
CHKRET(clSetKernelArg(ocl_kernel[Task.device][Task.kernel_num], 1, sizeof(cl_mem), &ocl_abuffers[pipelineBuffer][Task.device][buffer_pointers_A[Task.device][blockm] % ibuffercount]), "Error setting kernel memory A");
CHKRET(clSetKernelArg(ocl_kernel[Task.device][Task.kernel_num], 2, sizeof(cl_mem), &ocl_bbuffers[pipelineBuffer][Task.device][!buffersSufficiant ? (buffer_pointers_B[Task.device][blockn] % ibuffercount) : (buffer_pointers_B[Task.device][blockn] % bbuffers[Task.device])]), "Error setting kernel memory B");
}
int pitch;
cl_ulong offset; //uling should always be 64-bit in OpenCL
int height1 = (int) (((size_t) blockn == gpu_n / Config->Height) ? (gpu_n % Config->Height) : Config->Height);
int height2 = (int) (((size_t) blockm == gpu_m / Config->Height) ? (gpu_m % Config->Height) : Config->Height);
if (Config->DstMemory == 'g')
{
CHKRET(clSetKernelArg(ocl_kernel[Task.device][Task.kernel_num], 0, sizeof(cl_mem), &ocl_cbuffers[pipelineBuffer][Task.device][Task.j]), "Error setting kernel memory C");
pitch = height1;
offset = 0;
}
else if (Config->GPU_C == 0)
{
CHKRET(clSetKernelArg(ocl_kernel[Task.device][Task.kernel_num], 0, sizeof(cl_mem), &ocl_tmp_cbuffers[Task.device][Task.j]), "Error setting kernel memory C");
pitch = height1;
offset = 0;
}
else
{
cl_mem mem_c_matrix = 0;
size_t matrix_offset = 0;
if (GetMemoryInfo(&mem_c_matrix, NULL, &matrix_offset, C + blockn * Config->Height + blockm * Config->Height * C_pitch)) CHKRET(CL_INVALID_MIP_LEVEL, "Error obtaining memory info");
CHKRET(clSetKernelArg(ocl_kernel[Task.device][Task.kernel_num], 0, sizeof(cl_mem), &mem_c_matrix), "Error setting kernel memory C");
pitch = C_pitch;
offset = matrix_offset / sizeof(C[0]);
}
double beta = Config->GPU_C ? Beta : 0.;
double alpha = Task.kernel_num == 2 ? 1. : Alpha;
CHKRET(clSetKernelArg(ocl_kernel[Task.device][Task.kernel_num], 3, sizeof(int), &height1), "Error setting kernel arg height1");
CHKRET(clSetKernelArg(ocl_kernel[Task.device][Task.kernel_num], 4, sizeof(int), &height2), "Error setting kernel arg height2");
int width = Config->Width;
CHKRET(clSetKernelArg(ocl_kernel[Task.device][Task.kernel_num], 5, sizeof(int), &width), "Error setting kernel arg width");
CHKRET(clSetKernelArg(ocl_kernel[Task.device][Task.kernel_num], 6, sizeof(double), &alpha), "Error setting kernel arg alpha");
CHKRET(clSetKernelArg(ocl_kernel[Task.device][Task.kernel_num], 7, sizeof(double), &beta), "Error setting kernel arg beta");
CHKRET(clSetKernelArg(ocl_kernel[Task.device][Task.kernel_num], 8, sizeof(int), &pitch), "Error setting kernel arg pitch");
CHKRET(clSetKernelArg(ocl_kernel[Task.device][Task.kernel_num], 9, sizeof(cl_ulong), &offset), "Error setting kernel arg offset");
size_t local_size[2] = {(size_t) KernelSettings.group_size_x, (size_t) KernelSettings.group_size_y};
size_t global_size[2] = {(size_t) height1 / KernelSettings.tiling_x, (size_t) height2 / KernelSettings.tiling_y};
if (Config->Debug) fprintf(STD_OUT, "MM Kernel: height1 %d height2 %d width %d alpha %f beta %f pitch %d offset %lld\n", height1, height2, width, Alpha, Beta, pitch, (long long int) offset);
cl_event* kernel_event;
cl_event* need_retain_kernel_event = NULL;
int need_release_kernel_event_after_transfer = 0;
cl_event tmp_event;
int wait_num_events = 0;
cl_event* wait_event = NULL;
cl_event simple_queue_event[2];
cl_event* simple_queue_lookahead_event = NULL;
int use_queue = Config->AlternateSimpleQueuing ? 2 : Task.j;
if (Config->SimpleGPUQueuing)
{
if (ExecLinpack >= 2 && Config->AlternateLookahead > matrix_n && AlternateLookaheadTilesRemaining && blockm < AlternateLookaheadBlocksM)
{
simple_queue_lookahead_event = &AlternateLookaheadTilesRemainingSQ_events[AlternateLookaheadTilesRemaining - 1];
}
if (Config->DstMemory == 'g')
{
kernel_event = NULL;
}
else