-
Notifications
You must be signed in to change notification settings - Fork 16
/
OpenCL.cpp
921 lines (793 loc) · 35 KB
/
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
#include "config.h"
#ifdef USE_OPENCL
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <memory>
#include <string>
#include <sstream>
#include <fstream>
#include <cmath>
#include <array>
#include <thread>
#include <boost/algorithm/string.hpp>
#include <boost/format.hpp>
#include "Utils.h"
#include "Timing.h"
#include "OpenCL.h"
#include "Network.h"
#include "GTP.h"
using namespace Utils;
static std::string sourceCode_convolve15 = R"(
__kernel
__attribute__((work_group_size_hint(8, 16, 1)))
void convolve5(
__global const float * in,
__global float * merge,
__global const float * weights,
__local float * channel_buff,
__local float * row_buff) {
// cl::NDRange global(channels, outputs, row);
const int c = get_global_id(0); // channel
const int o = get_global_id(1); // output
const int row = get_global_id(2); // row
const int channels = get_global_size(0);
const int outputs = get_global_size(1);
// cl::NDRange local(2, (1->32), 1);
const int lx = get_local_id(0);
const int ly = get_local_id(1);
const int chan_buff_size = 8;
const int out_buff_size = get_local_size(1);
const int row_buff_size = 7;
const int chan_shift = 3;
const int filter_size = 5;
const int filter_len = filter_size * filter_size;
const int mid = (filter_size / 2) + 1;
const int extent = mid - 1;
// input = channels * height * width
// output = outputs * height * width
// weights = output * channels * filter
// merge = channels * outputs * height * width
const int width = 19;
const int height = 19;
const int strip_size = filter_size * width;
// Copy the input channels (strips) locally
if (out_buff_size < 19 && ly == 0) {
// strip-row
for (int srow = 0; srow < filter_size; srow++) {
int in_row = row - extent + srow;
if ((unsigned)in_row >= height) {
for (int w = 0; w < width; w++) {
channel_buff[(lx * filter_size + srow) * width + w] = 0.0f;
}
} else {
for (int w = 0; w < width; w++) {
channel_buff[(lx * filter_size + srow) * width + w] =
in[(c * height + in_row) * width + w];
}
}
}
} else if (out_buff_size >= 19 && ly < 19) {
// Every thread copies a column
for (int srow = 0; srow < filter_size; srow++) {
int in_row = row - extent + srow;
float val = 0.0f;
if ((unsigned)in_row < height) {
val = in[(c * height + in_row) * width + ly];
}
channel_buff[(lx * filter_size + srow) * width + ly] = val;
}
}
__private float filter_buff[25];
// Copy the filter we are applying locally
// output * channel * filter_len
for (int f = 0; f < filter_len; f++) {
filter_buff[f] = weights[(o * channels + c) * filter_len + f];
}
barrier(CLK_LOCAL_MEM_FENCE);
int out_lane = 0;
int out_cw = 0;
#pragma unroll
for (int cw = 0; cw < width; cw++) {
int fwstart = cw - extent;
int fwend = cw + extent;
float out;
// Start filter
if (fwstart >= 0 && fwend < width) {
int fid = lx * strip_size + fwstart;
out = channel_buff[fid ] * filter_buff[0];
out += channel_buff[fid + 1] * filter_buff[1];
out += channel_buff[fid + 2] * filter_buff[2];
out += channel_buff[fid + 3] * filter_buff[3];
out += channel_buff[fid + 4] * filter_buff[4];
out += channel_buff[fid + width ] * filter_buff[5];
out += channel_buff[fid + width + 1] * filter_buff[6];
out += channel_buff[fid + width + 2] * filter_buff[7];
out += channel_buff[fid + width + 3] * filter_buff[8];
out += channel_buff[fid + width + 4] * filter_buff[9];
out += channel_buff[fid + width*2 ] * filter_buff[10];
out += channel_buff[fid + width*2 + 1] * filter_buff[11];
out += channel_buff[fid + width*2 + 2] * filter_buff[12];
out += channel_buff[fid + width*2 + 3] * filter_buff[13];
out += channel_buff[fid + width*2 + 4] * filter_buff[14];
out += channel_buff[fid + width*3 ] * filter_buff[15];
out += channel_buff[fid + width*3 + 1] * filter_buff[16];
out += channel_buff[fid + width*3 + 2] * filter_buff[17];
out += channel_buff[fid + width*3 + 3] * filter_buff[18];
out += channel_buff[fid + width*3 + 4] * filter_buff[19];
out += channel_buff[fid + width*4 ] * filter_buff[20];
out += channel_buff[fid + width*4 + 1] * filter_buff[21];
out += channel_buff[fid + width*4 + 2] * filter_buff[22];
out += channel_buff[fid + width*4 + 3] * filter_buff[23];
out += channel_buff[fid + width*4 + 4] * filter_buff[24];
} else {
const float * filter_idx = filter_buff;
out = 0.0f;
#pragma unroll
for (int fh = 0; fh < filter_size; fh++) {
for (int fw = fwstart; fw <= fwend; fw++) {
// "zero padding"
if ((unsigned)fw >= width) {
filter_idx++;
continue;
}
float input = channel_buff[(lx * filter_size + fh) * width + fw];
out += input * *filter_idx++;
}
}
}
// End filter
row_buff[(ly * chan_buff_size + lx) * row_buff_size + out_lane] = out;
out_lane++;
// Row buffer full or last lane?
if (out_lane == row_buff_size || (cw == width - 1)) {
barrier(CLK_LOCAL_MEM_FENCE);
if (lx < out_lane) {
float val;
val = row_buff[(ly * chan_buff_size + 0) * row_buff_size + lx];
val += row_buff[(ly * chan_buff_size + 1) * row_buff_size + lx];
val += row_buff[(ly * chan_buff_size + 2) * row_buff_size + lx];
val += row_buff[(ly * chan_buff_size + 3) * row_buff_size + lx];
val += row_buff[(ly * chan_buff_size + 4) * row_buff_size + lx];
val += row_buff[(ly * chan_buff_size + 5) * row_buff_size + lx];
val += row_buff[(ly * chan_buff_size + 6) * row_buff_size + lx];
val += row_buff[(ly * chan_buff_size + 7) * row_buff_size + lx];
merge[(((c >> chan_shift) * height + row) * width + out_cw + lx) * outputs + o] = val;
}
out_cw += row_buff_size;
out_lane = 0;
}
}
}
)";
static std::string sourceCode_convolve3 = R"(
__kernel
__attribute__((work_group_size_hint(8, 32, 1)))
void convolve3(
__global const float * in,
__global float * merge,
__global const float * weights,
__local float * channel_buff,
__local float * row_buff,
const int row_tile_size) {
// cl::NDRange global(channels, outputs, row);
const int c = get_global_id(0); // channel
const int o = get_global_id(1); // output
const int r = get_global_id(2); // row
const int channels = get_global_size(0);
const int outputs = get_global_size(1);
// cl::NDRange local(2, (1->32), 1);
const int lx = get_local_id(0);
const int ly = get_local_id(1);
const int chan_buff_size = 8;
const int out_buff_size = get_local_size(1);
const int row_buff_size = 7;
const int chan_shift = 3;
const int width = 19;
const int height = 19;
const int filter_size = 3;
const int filter_len = filter_size * filter_size;
const int mid = (filter_size / 2) + 1;
const int extent = mid - 1;
const int pad_width = width + filter_size - 1;
// input = channels * height * width
// output = outputs * height * width
// weights = output * channels * filter
// merge = channels * outputs * height * width
__private float filter_buff[9];
__private float chan_cache[2];
__private float stripe_cache[9];
// Copy the filter we are applying locally
// output * channel * filter_len
for (int f = 0; f < filter_len; f++) {
filter_buff[f] = weights[(o * channels + c) * filter_len + f];
}
for (int tile = 0; tile < row_tile_size; tile++) {
int row = r * row_tile_size + tile;
if (row > 18) break;
// Copy the input channels (strips) locally
if (out_buff_size < 21 && ly == 0) {
// strip-row
for (int srow = 0; srow < filter_size; srow++) {
int in_row = row - extent + srow;
channel_buff[(lx * pad_width + 0) * filter_size + srow] = 0.0f;
if ((unsigned)in_row < height) {
for (int w = 0; w < width; w++) {
float val = in[(c * height + in_row) * width + w];
channel_buff[(lx * pad_width + w + extent) * filter_size + srow] = val;
}
} else {
for (int w = 0; w < width; w++) {
channel_buff[(lx * pad_width + w + extent) * filter_size + srow] = 0.0f;
}
}
channel_buff[(lx * pad_width + pad_width - 1) * filter_size + srow] = 0.0f;
}
} else if (out_buff_size >= 21 && ly < 21) {
// Every thread copies a column
int copy_idx = (lx * pad_width + ly) * filter_size;
if (tile == 0 || row == 18) {
// Every thread copies a column
for (int srow = 0; srow < filter_size; srow++) {
int in_row = row - extent + srow;
float val = 0.0f;
if ((unsigned)in_row < height && ly >= 1 && ly <= 19) {
val = in[(c * height + in_row) * width + ly - 1];
}
channel_buff[copy_idx + srow] = val;
if (srow > 0) {
chan_cache[srow - 1] = val;
}
}
} else {
int in_row = row - extent + 2;
float val = 0.0f;
if (ly >= 1 && ly <= 19) {
val = in[(c * height + in_row) * width + ly - 1];
}
channel_buff[copy_idx + 0] = chan_cache[0];
channel_buff[copy_idx + 1] = chan_cache[1];
channel_buff[copy_idx + 2] = val;
chan_cache[0] = chan_cache[1];
chan_cache[1] = val;
}
}
int out_lane = 0;
int out_cw = 0;
__local float * out_row_buff = &row_buff[(ly * chan_buff_size + lx) * row_buff_size];
int fid = (lx * pad_width) * filter_size;
barrier(CLK_LOCAL_MEM_FENCE);
for (int rc = 0; rc < 9; rc++) {
stripe_cache[rc] = channel_buff[fid + rc];
}
#pragma unroll
for (int cw = 0; cw < width; cw++) {
// Start filter
float out = stripe_cache[ 0] * filter_buff[0]
+ stripe_cache[ 1] * filter_buff[3]
+ stripe_cache[ 2] * filter_buff[6]
+ stripe_cache[ 3] * filter_buff[1]
+ stripe_cache[ 4] * filter_buff[4]
+ stripe_cache[ 5] * filter_buff[7]
+ stripe_cache[ 6] * filter_buff[2]
+ stripe_cache[ 7] * filter_buff[5]
+ stripe_cache[ 8] * filter_buff[8];
// End filter
out_row_buff[out_lane++] = out;
fid += filter_size;
for (int rc = 0; rc < 6; rc++) {
stripe_cache[rc] = stripe_cache[rc + 3];
}
stripe_cache[6] = channel_buff[fid + 6];
stripe_cache[7] = channel_buff[fid + 7];
stripe_cache[8] = channel_buff[fid + 8];
// Row buffer full or last lane?
if (out_lane == row_buff_size || (cw == width - 1)) {
barrier(CLK_LOCAL_MEM_FENCE);
if (lx < out_lane) {
// lx = channels 2 or 8, ly = outputs 32
// repurpose the lx threads over columns now
float val;
val = row_buff[(ly * chan_buff_size + 0) * row_buff_size + lx];
val += row_buff[(ly * chan_buff_size + 1) * row_buff_size + lx];
val += row_buff[(ly * chan_buff_size + 2) * row_buff_size + lx];
val += row_buff[(ly * chan_buff_size + 3) * row_buff_size + lx];
val += row_buff[(ly * chan_buff_size + 4) * row_buff_size + lx];
val += row_buff[(ly * chan_buff_size + 5) * row_buff_size + lx];
val += row_buff[(ly * chan_buff_size + 6) * row_buff_size + lx];
val += row_buff[(ly * chan_buff_size + 7) * row_buff_size + lx];
merge[(((c >> chan_shift) * height + row) * width + out_cw + lx) * outputs + o] = val;
}
out_cw += row_buff_size;
out_lane = 0;
}
}
}
}
)";
static std::string sourceCode_utility = R"(
__kernel void merge(
__global const float * in,
__global float * out,
__constant const float * biases,
__private const int channels) {
// cl::NDRange global(outputs, 19*19);
const int gx = get_global_id(0);
const int gy = get_global_id(1);
const int output = gx;
const int b = gy;
const int outputs = get_global_size(0);
const int width = 19;
const int height = 19;
const int boardsize = width * height;
const int o = output;
const float bias = biases[o];
float sum = bias;
for (int c = 0; c < channels; c++) {
sum += in[(c * boardsize + b) * outputs + o];
}
// ELU
sum = sum > 0 ? sum : 1.0f * (half_exp(sum) - 1.0f);
out[o * boardsize + b] = sum;
}
__kernel void batchnorm(
__global const float * in,
__global float * out,
__constant const float * means,
__constant const float * variances,
__constant const float * scale) {
// cl::NDRange global(outputs, 19*19);
const int gx = get_global_id(0);
const int gy = get_global_id(1);
const int output = gx;
const int outputs = get_global_size(0);
const int channel_size = get_global_size(1);
const unsigned int o = output;
const unsigned int b = gy;
const float epsilon = 1e-5;
const float mean = means[o] / scale[0];
const float variance = epsilon + variances[o] / scale[0];
const float scale_stddiv = 1.0f / sqrt(variance);
out[o * channel_size + b] = scale_stddiv
* (in[o * channel_size + b] - mean);
}
__kernel void innerproduct(
__private const int inputs,
__global const float * in,
__global float * out,
__global const float * weights,
__constant const float * biases) {
const int gx = get_global_id(0);
const int output = gx;
const int outputs = get_global_size(0);
const unsigned int o = output;
unsigned int i;
float16 val16 = (float16)(0.0f);
for (i = 0; i + 16 < inputs; i += 16) {
val16 += vload16(0, &in[i]) * vload16(0, &weights[o * inputs + i]);
}
float val = val16.s0 + val16.s1 + val16.s2 + val16.s3
+ val16.s4 + val16.s5 + val16.s6 + val16.s7
+ val16.s8 + val16.s9 + val16.sa + val16.sb
+ val16.sc + val16.sd + val16.se + val16.sf;
for (; i < inputs; i++) {
val += in[i] * weights[o * inputs + i];
}
val += biases[o];
if (outputs > 1) {
val = val > 0 ? val : 1.0f * (half_exp(val) - 1.0f);
}
out[o] = val;
}
)";
OpenCL opencl;
OpenCL_Network opencl_policy_net;
OpenCL_Network opencl_value_net;
thread_local ThreadData opencl_thread_data;
bool OpenCL::thread_can_issue() {
static std::atomic<int> max_queue_size{0};
int current_queue = opencl_thread_data.m_results_outstanding;
if (current_queue > max_queue_size) {
max_queue_size = current_queue;
//myprintf("qsz: %d\n", max_queue_size);
}
return current_queue < 2;
}
std::atomic<int> * OpenCL::get_thread_results_outstanding() {
return &opencl_thread_data.m_results_outstanding;
}
void OpenCL::ensure_thread_initialized() {
if (!opencl_thread_data.m_is_initialized) {
// Make kernels
opencl_thread_data.m_convolve3_kernel = cl::Kernel(m_program, "convolve3");
opencl_thread_data.m_convolve5_kernel = cl::Kernel(m_program, "convolve5");
opencl_thread_data.m_merge_kernel = cl::Kernel(m_program, "merge");
opencl_thread_data.m_batchnorm_kernel = cl::Kernel(m_program, "batchnorm");
opencl_thread_data.m_innerproduct_kernel = cl::Kernel(m_program, "innerproduct");
opencl_thread_data.m_commandqueue = cl::CommandQueue(cl::Context::getDefault(),
cl::Device::getDefault());
opencl_thread_data.m_is_initialized = true;
}
}
void OpenCL_Network::add_weights(size_t layer,
size_t size,
const float * weights) {
if (layer >= m_layers.size()) {
m_layers.push_back(Layer());
}
size_t weightSize = size *
sizeof(std::remove_pointer<decltype(weights)>::type);
cl::Buffer bufferWeights = cl::Buffer(CL_MEM_COPY_HOST_PTR | CL_MEM_READ_ONLY,
weightSize, const_cast<float*>(weights));
m_layers.back().weights.push_back(bufferWeights);
}
void OpenCL_Network::forward(std::vector<float>& input,
std::vector<float>& output,
event_callback cb, void * data) {
constexpr int width = 19;
constexpr int height = 19;
constexpr size_t one_plane = width * height * sizeof(float);
opencl.ensure_thread_initialized();
opencl_thread_data.m_results_outstanding.fetch_add(1, std::memory_order_release);
size_t inSize = sizeof(float) * input.size();
size_t outSize = sizeof(float) * output.size();
size_t finalSize = m_layers.back().outputs * 19 * 19 * sizeof(float);
if (!opencl_thread_data.m_buffers_allocated) {
size_t alloc_inSize = one_plane * Network::MAX_CHANNELS;
size_t alloc_outSize = one_plane * Network::MAX_CHANNELS;
size_t alloc_finalSize = one_plane * Network::MAX_CHANNELS;
size_t alloc_mergeSize = one_plane *
Network::MAX_CHANNELS * (Network::MAX_CHANNELS / 8);
opencl_thread_data.m_inBuffer = cl::Buffer(
CL_MEM_READ_WRITE, alloc_inSize);
opencl_thread_data.m_tmpBuffer = cl::Buffer(
CL_MEM_READ_WRITE, alloc_outSize);
opencl_thread_data.m_mergeBuffer = cl::Buffer(
CL_MEM_READ_WRITE | CL_MEM_HOST_NO_ACCESS, alloc_mergeSize);
opencl_thread_data.m_outBuffer = cl::Buffer(
CL_MEM_WRITE_ONLY, alloc_finalSize);
opencl_thread_data.m_buffers_allocated = true;
}
cl::Buffer & inBuffer = opencl_thread_data.m_inBuffer;
cl::Buffer & outBuffer = opencl_thread_data.m_outBuffer;
cl::Buffer & tmpBuffer = opencl_thread_data.m_tmpBuffer;
cl::Buffer & mergeBuffer = opencl_thread_data.m_mergeBuffer;
cl::CommandQueue & queue = opencl_thread_data.m_commandqueue;
queue.enqueueWriteBuffer(inBuffer, CL_FALSE, 0, inSize, input.data());
for (auto & layer : m_layers) {
if (layer.is_batchnorm) {
batchnorm(layer.outputs,
layer.filter_size,
inBuffer,
tmpBuffer,
layer.weights);
std::swap(inBuffer, tmpBuffer);
} else if (layer.is_innerproduct) {
innerproduct(layer.channels,
layer.outputs,
inBuffer,
tmpBuffer,
layer.weights);
std::swap(inBuffer, tmpBuffer);
} else {
// convolution
convolve(layer.filter_size,
layer.channels,
layer.outputs,
inBuffer,
tmpBuffer,
mergeBuffer,
layer.weights);
std::swap(inBuffer, tmpBuffer);
}
}
queue.enqueueCopyBuffer(inBuffer, outBuffer, 0, 0, finalSize);
queue.enqueueReadBuffer(outBuffer, CL_FALSE, 0, finalSize, output.data());
opencl.m_cb_outstanding.fetch_add(1, std::memory_order_release);
queue.finish();
if (cb != nullptr) {
cb(CL_COMPLETE, 0, data);
} else {
assert(data == nullptr);
opencl_thread_data.m_results_outstanding.fetch_sub(1, std::memory_order_release);
opencl.callback_finished();
}
}
void OpenCL::callback_finished() {
m_cb_outstanding.fetch_sub(1, std::memory_order_release);
}
void OpenCL::join_outstanding_cb() {
while (m_cb_outstanding.load(std::memory_order_acquire) > 0);
}
void OpenCL_Network::convolve(int filter_size, int channels, int outputs,
cl::Buffer& bufferInput,
cl::Buffer& bufferOutput,
cl::Buffer& bufferMerge,
std::vector<cl::Buffer>& weights) {
// fixed for 19x19
constexpr int width = 19;
constexpr int height = 19;
constexpr int boardsize = width * height;
unsigned int filter_len = filter_size * filter_size;
size_t inSize = width * height * channels * sizeof(float);
// Every input channel is this big
size_t chanSize = width * height * sizeof(float);
size_t outputGroup;
cl::Kernel * m_convolve_kernel = nullptr;
if (filter_size == 3) {
m_convolve_kernel = &opencl_thread_data.m_convolve3_kernel;
} else {
assert(filter_size == 5);
m_convolve_kernel = &opencl_thread_data.m_convolve5_kernel;
}
constexpr int channelGroup = 8;
constexpr int channelShift = 3;
constexpr int rowGroup = 1;
// Workgroup things
if (opencl.m_max_workgroup_size < 512
|| opencl.m_max_workgroup_dims[1] < 64) {
outputGroup = std::min(outputs, 32);
} else {
// Can optionally be 64
outputGroup = std::min(outputs, 32);
}
if (outputs == 48) {
outputGroup = 16;
}
// Total output size after reducing
size_t outSize = width * height * outputs * sizeof(float);
// Produce channel * output planes and merge them at the end
size_t mergeSize = (channels >> channelShift) * outSize;
// Store the filters locally
// size_t filtSize = outputGroup * channelGroup * filter_len * sizeof(float);
// Copy the rows locally
size_t stripSize;
int rowTileSize;
int rowTiles;
if (filter_size == 3) {
stripSize = filter_size * (width + (filter_size - 1)) * sizeof(float);
rowTiles = cfg_rowtiles;
rowTileSize = (19 + rowTiles - 1) / rowTiles;
} else if (filter_size == 5) {
stripSize = filter_size * width * sizeof(float);
rowTiles = 19;
rowTileSize = 1;
}
int rowBuffer = std::min<int>(channelGroup, 7);
assert(rowBuffer == 7); // hardcoded in kernel
size_t rowSize = channelGroup * outputGroup * rowBuffer * sizeof(float);
assert(mergeSize <= bufferMerge.getInfo<CL_MEM_SIZE>());
cl::CommandQueue & queue = opencl_thread_data.m_commandqueue;
try {
m_convolve_kernel->setArg(0, bufferInput);
m_convolve_kernel->setArg(1, bufferMerge);
m_convolve_kernel->setArg(2, weights[0]);
m_convolve_kernel->setArg(3, cl::Local(stripSize * channelGroup * rowGroup));
m_convolve_kernel->setArg(4, cl::Local(rowSize));
if (filter_size == 3) {
m_convolve_kernel->setArg(5, rowTileSize);
}
queue.enqueueNDRangeKernel(*m_convolve_kernel, cl::NullRange,
cl::NDRange(channels, outputs, rowTiles),
cl::NDRange(channelGroup, outputGroup, rowGroup));
} catch (const cl::Error &e) {
std::cerr << "Error in convolve: " << e.what() << ": "
<< e.err() << std::endl;
throw;
}
cl::Kernel & merge_kernel = opencl_thread_data.m_merge_kernel;
try {
merge_kernel.setArg(0, bufferMerge);
merge_kernel.setArg(1, bufferOutput);
merge_kernel.setArg(2, weights[1]);
merge_kernel.setArg(3, channels >> channelShift);
queue.enqueueNDRangeKernel(merge_kernel, cl::NullRange,
cl::NDRange(outputs, boardsize),
cl::NDRange(std::min(8, outputs), 19));
} catch (const cl::Error &e) {
std::cerr << "Error in merge: " << e.what() << ": "
<< e.err() << std::endl;
throw;
}
}
void OpenCL_Network::batchnorm(int outputs,
int channel_size,
cl::Buffer & bufferInput,
cl::Buffer & bufferOutput,
std::vector<cl::Buffer>& weights) {
cl::CommandQueue & queue = opencl_thread_data.m_commandqueue;
cl::Kernel & batchnorm_kernel = opencl_thread_data.m_batchnorm_kernel;
size_t channelGroup = 1;
if (channel_size == 361) {
channelGroup = 19;
}
try {
batchnorm_kernel.setArg(0, bufferInput);
batchnorm_kernel.setArg(1, bufferOutput);
batchnorm_kernel.setArg(2, weights[0]);
batchnorm_kernel.setArg(3, weights[1]);
batchnorm_kernel.setArg(4, weights[2]);
queue.enqueueNDRangeKernel(batchnorm_kernel, cl::NullRange,
cl::NDRange(outputs, channel_size),
cl::NDRange(std::min(8, outputs), channelGroup));
} catch (const cl::Error &e) {
std::cerr << "Error in batchnorm: " << e.what() << ": "
<< e.err() << std::endl;
throw;
}
}
void OpenCL_Network::innerproduct(int inputs,
int outputs,
cl::Buffer & bufferInput,
cl::Buffer & bufferOutput,
std::vector<cl::Buffer>& weights) {
cl::CommandQueue & queue = opencl_thread_data.m_commandqueue;
cl::Kernel & innerproduct_kernel = opencl_thread_data.m_innerproduct_kernel;
try {
innerproduct_kernel.setArg(0, inputs);
innerproduct_kernel.setArg(1, bufferInput);
innerproduct_kernel.setArg(2, bufferOutput);
innerproduct_kernel.setArg(3, weights[0]);
innerproduct_kernel.setArg(4, weights[1]);
queue.enqueueNDRangeKernel(innerproduct_kernel, cl::NullRange,
cl::NDRange(outputs),
cl::NDRange(std::min(16, outputs)));
} catch (const cl::Error &e) {
std::cerr << "Error in innerproduct: " << e.what() << ": "
<< e.err() << std::endl;
throw;
}
}
template<class T>
static std::string opencl_dev_type_to_string(T type) {
if (type == CL_DEVICE_TYPE_CPU) {
return "CPU";
} else if (type == CL_DEVICE_TYPE_GPU) {
return "GPU";
} else if (type == CL_DEVICE_TYPE_ACCELERATOR) {
return "Accelerator";
} else {
return "Unknown";
}
}
static std::string trim(std::string trim_me) {
boost::algorithm::trim(trim_me);
return trim_me;
}
void OpenCL::initialize(void) {
std::vector<cl::Platform> platforms;
try {
cl::Platform::get(&platforms);
} catch (const cl::Error &e) {
myprintf("OpenCL: %s\n", e.what());
throw;
}
float best_version = 0.0f;
cl::Platform best_platform;
cl::Device best_device;
std::string best_vendor;
int best_score = 0;
bool found_device = false;
int id = 0;
myprintf("Detected %d OpenCL platforms\n", platforms.size());
for (auto &p : platforms) {
std::string platvers = p.getInfo<CL_PLATFORM_VERSION>();
std::string platprof = p.getInfo<CL_PLATFORM_PROFILE>();
std::string platname = p.getInfo<CL_PLATFORM_NAME>();
std::string platvend = p.getInfo<CL_PLATFORM_VENDOR>();
myprintf("Platform version: %s\n", platvers.c_str());;
myprintf("Platform profile: %s\n", platprof.c_str());
myprintf("Platform name: %s\n", platname.c_str());
myprintf("Platform vendor: %s\n", platvend.c_str());
std::istringstream versstream(platvers);
std::string tmp;
float opencl_version;
versstream >> tmp >> opencl_version;
std::vector<cl::Device> devices;
try {
p.getDevices(CL_DEVICE_TYPE_ALL, &devices);
} catch (const cl::Error &e) {
myprintf("Error getting device(s): %s: %d\n", e.what(), e.err());
devices.clear();
}
for (auto& d : devices) {
myprintf("Device ID: %d\n", id);
myprintf("Device name: %s\n",
trim(d.getInfo<CL_DEVICE_NAME>()).c_str());
myprintf("Device type: %s\n",
opencl_dev_type_to_string(d.getInfo<CL_DEVICE_TYPE>()).c_str());
myprintf("Device vendor: %s\n",
d.getInfo<CL_DEVICE_VENDOR>().c_str());
myprintf("Device driver: %s\n",
d.getInfo<CL_DRIVER_VERSION>().c_str());
myprintf("Device speed: %u MHz\n",
d.getInfo<CL_DEVICE_MAX_CLOCK_FREQUENCY>());
myprintf("Device cores: %u CU\n",
d.getInfo<CL_DEVICE_MAX_COMPUTE_UNITS>());
// assign score, try to find best device
int this_score = 0;
std::string this_vendor = d.getInfo<CL_DEVICE_VENDOR>();
this_score += 1000 * boost::icontains(this_vendor, "advanced micro devices");
this_score += 1000 * boost::icontains(this_vendor, "amd");
this_score += 1000 * boost::icontains(this_vendor, "nvidia");
this_score += 500 * boost::icontains(this_vendor, "intel");
this_score += 100 * (d.getInfo<CL_DEVICE_TYPE>() == CL_DEVICE_TYPE_GPU);
this_score += opencl_version * 10;
myprintf("Device score: %d\n", this_score);
bool preferred = std::find(cfg_gpus.cbegin(), cfg_gpus.cend(), id) != cfg_gpus.cend();
if ((this_score > best_score) || preferred) {
best_version = opencl_version;
best_platform = p;
best_device = d;
if (preferred) {
best_score = std::numeric_limits<decltype(best_score)>::max();
} else {
best_score = this_score;
}
found_device = true;
}
id++;
}
}
if (!found_device) {
throw std::runtime_error("No suitable OpenCL device found.");
}
cl::Platform::setDefault(best_platform);
myprintf("Selected platform: %s\n", best_platform.getInfo<CL_PLATFORM_NAME>().c_str());
myprintf("Selected device: %s\n", trim(best_device.getInfo<CL_DEVICE_NAME>()).c_str());
myprintf("with OpenCL %2.1f capability\n", best_version);
cl::Context context;
try {
context = cl::Context(best_device);
} catch (const cl::Error &e) {
myprintf("Error creating OpenCL context: %s: %d", e.what(), e.err());
throw;
}
cl::Context::setDefault(context);
cl::Device::setDefault(best_device);
// Read source file
//std::ifstream sourceFile("convolve_kernel.cl", std::ifstream::in);
//std::string sourceCode(std::istreambuf_iterator<char>(sourceFile),
// (std::istreambuf_iterator<char>()));
// Make program of the source code in the context
try {
m_program = cl::Program(sourceCode_convolve15
+ sourceCode_convolve3
+ sourceCode_utility);
} catch (const cl::Error &e) {
myprintf("Error getting kernels: %s: %d", e.what(), e.err());
throw;
}
// Build program for these specific devices
try {
m_program.build("-cl-mad-enable -cl-fast-relaxed-math -cl-no-signed-zeros -cl-denorms-are-zero");
} catch (const cl::Error&) {
myprintf("Error building kernels: %s\n",
m_program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(cl::Device::getDefault()).c_str());
throw;
}
ensure_thread_initialized();
m_wavefront_size =
opencl_thread_data.m_convolve3_kernel.getWorkGroupInfo<CL_KERNEL_PREFERRED_WORK_GROUP_SIZE_MULTIPLE>(
best_device);
myprintf("Wavefront/Warp size: %d\n", m_wavefront_size);
m_max_workgroup_size = best_device.getInfo<CL_DEVICE_MAX_WORK_GROUP_SIZE>();
m_max_workgroup_dims = best_device.getInfo<CL_DEVICE_MAX_WORK_ITEM_SIZES>();
myprintf("Max workgroup size: %d\n", m_max_workgroup_size);
myprintf("Max workgroup dimensions: ");
for (auto d : m_max_workgroup_dims) {
myprintf("%d ", d);
}
myprintf("\n");
m_init_ok = true;
}
std::string OpenCL::get_device_name() {
std::stringstream ss;
cl::Device device = cl::Device::getDefault();
ss << "OpenCL: ";
ss << device.getInfo<CL_DEVICE_VENDOR>() << " ";
ss << device.getInfo<CL_DEVICE_NAME>() << " @ ";
ss << device.getInfo<CL_DEVICE_MAX_CLOCK_FREQUENCY>() << "MHz";
return ss.str();
}
#endif