-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
1081 lines (873 loc) · 42.4 KB
/
main.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
//////////////////////////////////////////
//
// OpenCL host program template for multiple
// FPGA boards.
//
// Created by [email protected]
//
/////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <sys/time.h>
#include <iostream>
#include <fstream>
#include <chrono>
// Define colors
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_GREEN "\x1b[32m"
#define ANSI_COLOR_YELLOW "\x1b[33m"
#define ANSI_COLOR_BLUE "\x1b[34m"
#define ANSI_COLOR_MAGENTA "\x1b[35m"
#define ANSI_COLOR_CYAN "\x1b[36m"
#define ANSI_COLOR_RESET "\x1b[0m"
#include <CL/opencl.h>
// user defined library
#include "ocl_util.h"
#include "timer.h"
// CNN network configuration file
#include "../../device/DLA-nosys/hw_param.cl"
#include "layer_config.h"
#ifdef USE_OPENCV
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
using namespace cv;
#endif
using namespace std;
using namespace ocl_util;
typedef signed char DTYPE;
#ifdef XILINX
//#define USE_SDX_1DDR // reserved for v7-690T device, DO NOT USE
//#define USE_SDX_4DDR // reverved for 4-bank DDR BSP, DO NOT USE
#endif
//----------- Design Parameters --------------//
// select what platform is used
const char *vendor_name = "Intel";
#define DEVICE_TYPE CL_DEVICE_TYPE_ACCELERATOR
// SW System parameters
#define DMA_ALIGNMENT 64
#define IN_BUF_SIZE 256*256*64 // Note: the buffer size should be large enough to hold all temperary results
#define OUT_BUF_SIZE 256*256*64
// #define VERBOSE_OUTPUT
#ifdef ALEXNET_TEST
// Original problem size
// File size is in num of DTYPE numbers
#define IMAGE_FILE_SIZE (227*227*3)
//#define WEIGHTS_FILE_SIZE 60965224 //fc8-1000
#define WEIGHTS_FILE_SIZE 61063552 //fc8-1024
#define LAYER_NUM 8
#define CONV_NUM 5
const char *weight_file_path = "./data/data_alex/weights.dat";
const char *input_file_path = "./data/data_alex/image.dat";
#endif
#ifdef VGG16_TEST
// VGG16
// Original problem size
// File size is in num of DTYPE numbers
#define IMAGE_FILE_SIZE (224*224*3)
#define WEIGHTS_FILE_SIZE 324442112 //fc8-1024
#define BIASES_FILE_SIZE 13440
#define LAYER_NUM 16
#define CONV_NUM 16
const char *weight_file_path = "./data/data_vgg16/weights.dat";
const char *input_file_path = "./data/data_vgg16/image.dat";
#endif
// Configuration file instructions
enum config_item{
layer_type, // "0" -> conv, "1" -> fc
data_w, data_h, data_n, weight_w, weight_h, weight_n, weight_m, bias_size, //memRd Parameters
memrd_src, //"0"-> data_buf "1"-> output_buf "2"->"fc_1_buffer" "3"->"fc_2_buffer"
conv_x, conv_y, conv_z, conv_stride, conv_padding, conv_split, conv_relu, //Conv Parameters
pool_on, pool_x, pool_y, pool_z, pool_size, pool_stride, // Pooling Parameters
lrn_on,// lrn on/off control
memwr_dst//"0"-> data_buf "1"-> output_buf "2"->"fc_1_buffer" "3"->"fc_2_buffer"
};
enum input_item{
image_w, image_h, image_n, // original image size
batch_size
};
enum output_item{
output_w, output_h, output_n
};
enum precision_item{
frac_w, frac_din, frac_dout
};
typedef struct {
int layer_type;
int data_w, data_h, weight_w, weight_h, weight_n, weight_m, bias_size;
int memrd_src;
int conv_x, conv_y, conv_z, conv_stride, conv_padding, conv_split, conv_relu;
int pool_on, pool_x, pool_y, pool_z, pool_size, pool_stride;
int lrn_on;
int memwr_dst;
int num_bricks;
} fpga_configuration;
typedef struct device_runner_arg {
int device;
} device_runner_arg;
// Define the kernel names used
const char *knl_name_memRdData = "memReadData";
const char *knl_name_memRdWeight = "memReadWeight";
const char *knl_name_controller = "controller";
const char *knl_name_memWrite = "memWrite";
const char *knl_name_ser = "ser";
const char *knl_name_deser = "deser";
//------------ Global Functions & Variables ------------//
cl_uint num_devices = 0;
cl_platform_id platform_id = NULL;
scoped_array<cl_context> context;
scoped_array<cl_program> program;
scoped_array<cl_device_id> device;
scoped_array<cl_kernel> knl_memRdData;
scoped_array<cl_kernel> knl_memRdWeight;
scoped_array<cl_kernel> knl_controller;
scoped_array<cl_kernel> knl_memWrite;
scoped_array<cl_kernel> knl_ser;
scoped_array<cl_kernel> knl_deser;
scoped_array<cl_command_queue> que_memRdData;
scoped_array<cl_command_queue> que_memRdWeight;
scoped_array<cl_command_queue> que_controller;
scoped_array<cl_command_queue> que_memWrite;
scoped_array<cl_mem> config_buf;
scoped_array<cl_mem> bottom0_buf;
scoped_array<cl_mem> bottom1_buf;
scoped_array<cl_mem> weights_buf;
scoped_array<cl_mem> bias_buf;
DTYPE *weights;
DTYPE *biases;
DTYPE *image;
DTYPE *data_init;
scoped_array<int> layers_per_device;
scoped_array<scoped_array<int>> assigned_layers;
// Threads that are handling the devices
scoped_array<pthread_t> device_threads;
unsigned layer_config_original[LAYER_NUM][NUM_CONFIG_ITEM];
void loadImageToBuffer(int num);
int prepare();
void printCurrentTime();
void cleanup();
void SplitBufferToArray(char *buffer, char * delim, char ** Output);
void* device_runner (void* args);
int main(int argc, char** argv)
{
cl_int status;
unsigned int weight_buf_size;
unsigned int bias_buf_size;
unsigned int pic_num = 1;
if (argc < 3){
printf("Error: wrong commad format, usage:\n");
printf("%s <binaryfile> [layers,[...]]\n", argv[0]);
printf("Example: main.exe conv.aocx 1,2,3,4 5,6,7,8\n");
return EXIT_FAILURE;
}
printf("***************************************************\n");
printf("PipeCNN: An OpenCL-Based FPGA Accelerator for CNNs \n");
printf("***************************************************\n");
// Connect to the desired platform
platform_id = findPlatform(vendor_name);
if(platform_id == NULL) {
printf("ERROR: Unable to find the desired OpenCL platform.\n");
return false;
}
// Query the available OpenCL device
device.reset(getDevices(platform_id, DEVICE_TYPE, &num_devices));
printf("\nPlatform: %s\n", getPlatformName(platform_id).c_str());
printf("Using %d device(s)\n", num_devices);
for(unsigned i = 0; i < num_devices; ++i) {
printf(" Device %d: %s\n", i, getDeviceName(device[i]).c_str());
displayDeviceInfo(device[i]);
}
context.reset(num_devices);
program.reset(num_devices);
if (num_devices != argc - 2) {
printf ("ERROR: Number of layer segmentations should be\nequal to the number of devices!\n");
return false;
}
// Create the context.
context[0] = clCreateContext(NULL, 1, &(device[0]), NULL, NULL, &status);
checkError(status, "Failed to create context");
context[1] = clCreateContext(NULL, 1, &(device[1]), NULL, NULL, &status);
checkError(status, "Failed to create context");
// Create Program Objects
char *kernel_file_name=argv[1];
// Create the program for all device. All devices execute the same kernel.
program[0] = createProgramFromFile(context[0], (const char *) kernel_file_name, &(device[0]), 1);
program[1] = createProgramFromFile(context[1], (const char *) kernel_file_name, &(device[1]), 1);
// Extracting the layer segmentations
assigned_layers.reset(num_devices);
layers_per_device.reset(num_devices);
for (int i = 0; i < num_devices; i++) {
int* layers_as_array = new int[20];
int num_layers_involved = 0;
char* layers = argv[i+2];
char* pch = strtok(layers, ",");
while (pch != NULL) {
layers_as_array[num_layers_involved] = atoi(pch);
num_layers_involved++;
pch = strtok(NULL, ",");
}
assigned_layers[i].reset(num_layers_involved);
layers_per_device[i] = num_layers_involved;
for (int j = 0; j < num_layers_involved; j++) {
assigned_layers[i][j] = layers_as_array[j];
}
}
device_threads.reset(num_devices);
// Prepare compute data
status = prepare();
if(status == 1) {
printf("Allocate memory for data and weights failed !!!\n");
return false;
}
// Printing the input information
printf("[INFO] Number of involved devices are %d\n", num_devices);
for (int i = 0; i < num_devices; i++) {
printf("[INFO] layers involved for device %d is: ", i);
for (int j = 0; j < layers_per_device[i]; j++) {
printf("%d ", assigned_layers[i][j]);
}
printf ("\n");
}
// create per device object
que_memRdData.reset(num_devices);
que_memRdWeight.reset(num_devices);
que_controller.reset(num_devices);
que_memWrite.reset(num_devices);
knl_memRdData.reset(num_devices);
knl_memRdWeight.reset(num_devices);
knl_controller.reset(num_devices);
knl_memWrite.reset(num_devices);
knl_ser.reset(num_devices);
knl_deser.reset(num_devices);
config_buf.reset(num_devices);
bottom0_buf.reset(num_devices);
bottom1_buf.reset(num_devices);
weights_buf.reset(num_devices);
bias_buf.reset(num_devices);
// Command queue
for (int i = 0; i < num_devices; i++) {
printf ("[INFO] Creating the command queues for the " ANSI_COLOR_RED "Device %d " ANSI_COLOR_RESET "\n", i);
que_memRdData[i] = clCreateCommandQueue(context[i], device[i], CL_QUEUE_PROFILING_ENABLE, &status);
checkError(status, "Failed to create command queue for memReadData");
que_memRdWeight[i] = clCreateCommandQueue(context[i], device[i], CL_QUEUE_PROFILING_ENABLE, &status);
checkError(status, "Failed to create command queue for memRdWeight");
que_controller[i] = clCreateCommandQueue(context[i], device[i], CL_QUEUE_PROFILING_ENABLE, &status);
checkError(status, "Failed to create command queue for controller");
que_memWrite[i] = clCreateCommandQueue(context[i], device[i], CL_QUEUE_PROFILING_ENABLE, &status);
checkError(status, "Failed to create command queue for memWrite");
// Kernel
printf ("[INFO] Creating kernels for the " ANSI_COLOR_RED "DEVICE %d " ANSI_COLOR_RESET "\n", i);
knl_memRdData[i] = clCreateKernel(program[i], knl_name_memRdData, &status);
checkError(status, "Failed to create memRdData kernel");
knl_memRdWeight[i] = clCreateKernel(program[i], knl_name_memRdWeight, &status);
checkError(status, "Failed to create memRdWeight kernel");
knl_controller[i] = clCreateKernel(program[i], knl_name_controller, &status);
checkError(status, "Failed to create controller kernel");
knl_memWrite[i] = clCreateKernel(program[i], knl_name_memWrite, &status);
checkError(status, "Failed to create memWrite kernel");
knl_ser[i] = clCreateKernel(program[i], knl_name_ser, &status);
checkError(status, "Failed to create ser kernel");
knl_deser[i] = clCreateKernel(program[i], knl_name_deser, &status);
checkError(status, "Failed to create deser kernel");
// Here we should calculate how many items we will have in the whole weight buffer.
// For each layer, we have the total number of output cannels.
// Each output channels, is a brick which is of size
// => num_input_channels * weight_h * W_VEC
// as you can see, the weight_w is replaced by the W_VEC, because we are using the
// winograd transformation.
weight_buf_size = 0;
for (int layer = 0; layer < LAYER_NUM; layer++) {
weight_buf_size += (layer_config[layer][weight_m]*layer_config[layer][weight_n]*layer_config[layer][weight_h]*W_VEC);
}
printf ("[INFO] Creating the weight buffer for the " ANSI_COLOR_RED "DEVICE %d " ANSI_COLOR_RESET "\n", i);
weights_buf[i] = clCreateBuffer(context[i], CL_MEM_READ_ONLY, weight_buf_size * sizeof(DTYPE), NULL, &status);
checkError(status, "Failed to create buffer for weights in layer");
// The total number of biases, is equal to the total number of layers,
// multiplied by the number of output channels of the each layer
bias_buf_size = 0;
for (int layer = 0; layer < LAYER_NUM; layer++) {
bias_buf_size += layer_config[layer][weight_m];
}
printf ("[INFO] Creating the bias buffer for the " ANSI_COLOR_RED "DEVICE %d " ANSI_COLOR_RESET "\n", i);
bias_buf[i] = clCreateBuffer(context[i], CL_MEM_READ_ONLY, bias_buf_size * sizeof(DTYPE), NULL, &status);
checkError(status, "Failed to create buffer for bias in layer");
// Initializing all weights buffers, blocking write is used
//
printf ("[INFO] weight_buf_size=%d, WEIGHTS_FILE_SIZE=%d\n", weight_buf_size, WEIGHTS_FILE_SIZE);
printf ("[INFO] Enqueueing the weight buffer to weight queue for the " ANSI_COLOR_RED "DEVICE %d " ANSI_COLOR_RESET "\n", i);
status = clEnqueueWriteBuffer(que_memRdWeight[i], weights_buf[i], CL_TRUE, 0, weight_buf_size * sizeof(DTYPE), weights, 0, NULL, NULL);
checkError(status, "Failed to transfer weight");
printf ("[INFO] biases_buf_size=%d, BIASES_FILE_SIZE=%d\n", bias_buf_size, BIASES_FILE_SIZE);
printf ("[INFO] Enqueueing the bias buffer to bias queue for the " ANSI_COLOR_RED "DEVICE %d " ANSI_COLOR_RESET "\n", i);
status = clEnqueueWriteBuffer(que_memRdWeight[i], bias_buf[i], CL_TRUE, 0, bias_buf_size * sizeof(DTYPE), biases, 0, NULL, NULL);
checkError(status, "Failed to transfer bias");
// First buffer
printf ("[INFO] Creating the bottom0 buffer for the " ANSI_COLOR_RED "DEVICE %d " ANSI_COLOR_RESET "\n", i);
bottom0_buf[i] = clCreateBuffer(context[i], CL_MEM_READ_WRITE, IN_BUF_SIZE * sizeof(DTYPE), NULL, &status);
checkError(status, "Failed to create buffer for data in layer");
// Second buffer
printf ("[INFO] Creating the bottom1 buffer for the " ANSI_COLOR_RED "DEVICE %d " ANSI_COLOR_RESET "\n", i);
bottom1_buf[i] = clCreateBuffer(context[i], CL_MEM_READ_WRITE, OUT_BUF_SIZE * sizeof(DTYPE), NULL, &status);
checkError(status, "Failed to create buffer for output");
//printf ("[INFO] Enqueueing the bottom0 buffer\n");
//status = clEnqueueWriteBuffer(que_memRdData, bottom0_buf, CL_TRUE, 0, IN_BUF_SIZE * sizeof(DTYPE), NULL, 0, NULL, NULL);
//checkError(status, "Failed to transfer bottom0");
//printf ("[INFO] Enqueueing the bottom1 buffer\n");
//status = clEnqueueWriteBuffer(que_memRdData, bottom1_buf, CL_TRUE, 0, OUT_BUF_SIZE * sizeof(DTYPE), NULL, 0, NULL, NULL);
//checkError(status, "Failed to transfer bottom1");
printf ("[INFO] Creating the config buffer with size %d\n", sizeof(fpga_configuration) * LAYER_NUM);
config_buf[i] = clCreateBuffer(context[i], CL_MEM_READ_WRITE, sizeof(fpga_configuration) * (layers_per_device[i]+1), NULL, &status);
checkError(status, "Failed to create buffer for config");
fpga_configuration config[layers_per_device[i]+1];
//printf ("[INFO] Layers_per_device+1=%d\n", layers_per_device[i]+1);
//for (int j = 0; j < layers_per_device[i]+1; j++) {
// printf("[INFO] %d,%d\n",j, config[j].layer_type);
//}
printf ("[INFO] Setting the configurations per layer (for %d layers) for the " ANSI_COLOR_RED "DEVICE %d " ANSI_COLOR_RESET "\n", layers_per_device[i]+1, i);
for (int layer = 0; layer < layers_per_device[i]+1; layer++) {
printf ("[INFO] Working on the layer #%d\n", layer);
config[layer].layer_type = layer_config[assigned_layers[i][0]+layer][layer_type];
printf ("[INFO] Layer type is %d\n", layer_config[assigned_layers[i][0]+layer][layer_type]);
config[layer].data_w = layer_config[assigned_layers[i][0]+layer][data_w];
config[layer].data_h = layer_config[assigned_layers[i][0]+layer][data_h];
config[layer].weight_w = layer_config[assigned_layers[i][0]+layer][weight_w];
config[layer].weight_h = layer_config[assigned_layers[i][0]+layer][weight_h];
config[layer].weight_n = layer_config[assigned_layers[i][0]+layer][weight_n];
config[layer].weight_m = layer_config[assigned_layers[i][0]+layer][weight_m];
config[layer].memrd_src = layer_config[assigned_layers[i][0]+layer][memrd_src];
config[layer].conv_x = layer_config[assigned_layers[i][0]+layer][conv_x];
config[layer].conv_y = layer_config[assigned_layers[i][0]+layer][conv_y];
config[layer].conv_z = layer_config[assigned_layers[i][0]+layer][conv_z];
config[layer].conv_stride = layer_config[assigned_layers[i][0]+layer][conv_stride];
config[layer].conv_padding = layer_config[assigned_layers[i][0]+layer][conv_padding];
config[layer].conv_split = layer_config[assigned_layers[i][0]+layer][conv_split];
config[layer].conv_relu = layer_config[assigned_layers[i][0]+layer][conv_relu];
config[layer].pool_on = layer_config[assigned_layers[i][0]+layer][pool_on];
config[layer].pool_x = layer_config[assigned_layers[i][0]+layer][pool_x];
config[layer].pool_y = layer_config[assigned_layers[i][0]+layer][pool_y];
config[layer].pool_z = layer_config[assigned_layers[i][0]+layer][pool_z];
config[layer].pool_size = layer_config[assigned_layers[i][0]+layer][pool_size];
config[layer].conv_stride = layer_config[assigned_layers[i][0]+layer][pool_stride];
config[layer].lrn_on = layer_config[assigned_layers[i][0]+layer][lrn_on];
config[layer].memwr_dst = layer_config[assigned_layers[i][0]+layer][memwr_dst];
if (assigned_layers[i][0]+layer == LAYER_NUM-1) {
config[layer].layer_type = 0;
config[layer].data_w = 0;
config[layer].data_h = 0;
config[layer].weight_w = 0;
config[layer].weight_h = 0;
config[layer].weight_n = 0;
config[layer].weight_m = 0;
config[layer].memrd_src = 0;
config[layer].conv_x = 0;
config[layer].conv_y = 0;
config[layer].conv_z = 0;
config[layer].conv_stride = 0;
config[layer].conv_padding = 0;
config[layer].conv_split = 0;
config[layer].conv_relu = 0;
config[layer].pool_on = 0;
config[layer].pool_x = 0;
config[layer].pool_y = 0;
config[layer].pool_z = 0;
config[layer].pool_size = 0;
config[layer].conv_stride = 0;
config[layer].lrn_on = 0;
config[layer].memwr_dst = 0;
}
printf ("[INFO] " ANSI_COLOR_RED "DEVICE %d " ANSI_COLOR_RESET "layer_type: %d, data_w: %d, data_h: %d, weight_w: %d, weight_h: %d, weight_n: %d, weight_m: %d, memrd_src: %d, conv_x: %d, conv_y: %d, conv_z: %d, conv_stride: %d, conv_padding: %d, conv_split: %d, conv_relu: %d, pool_on: %d, pool_x: %d, pool_y: %d, pool_z: %d, pool_size: %d, conv_stride: %d, lrn_on: %d, memwr_dst: %d\n", i, layer_config[assigned_layers[i][0]+layer][layer_type], layer_config[assigned_layers[i][0]+layer][data_w], layer_config[assigned_layers[i][0]+layer][data_h], layer_config[assigned_layers[i][0]+layer][weight_w], layer_config[assigned_layers[i][0]+layer][weight_h], layer_config[assigned_layers[i][0]+layer][weight_n], layer_config[assigned_layers[i][0]+layer][weight_m], layer_config[assigned_layers[i][0]+layer][memrd_src], layer_config[assigned_layers[i][0]+layer][conv_x], layer_config[assigned_layers[i][0]+layer][conv_y], layer_config[assigned_layers[i][0]+layer][conv_z], layer_config[assigned_layers[i][0]+layer][conv_stride], layer_config[assigned_layers[i][0]+layer][conv_padding], layer_config[assigned_layers[i][0]+layer][conv_split], layer_config[assigned_layers[i][0]+layer][conv_relu], layer_config[assigned_layers[i][0]+layer][pool_on], layer_config[assigned_layers[i][0]+layer][pool_x], layer_config[assigned_layers[i][0]+layer][pool_y], layer_config[assigned_layers[i][0]+layer][pool_z], layer_config[assigned_layers[i][0]+layer][pool_size], layer_config[assigned_layers[i][0]+layer][pool_stride], layer_config[assigned_layers[i][0]+layer][lrn_on], layer_config[assigned_layers[i][0]+layer][memwr_dst]);
int w_vec = W_VEC;
config[assigned_layers[i][0]+layer].num_bricks = (layer_config[assigned_layers[i][0]+layer][data_h]+2*layer_config[assigned_layers[i][0]+layer][conv_padding]-layer_config[assigned_layers[i][0]+layer][weight_h]+1)*((layer_config[assigned_layers[i][0]+layer][data_w]+2*layer_config[assigned_layers[i][0]+layer][conv_padding]-layer_config[assigned_layers[i][0]+layer][weight_w])/(W_VEC-layer_config[assigned_layers[i][0]+layer][weight_w]+1) + 1);
printf ("[INFO] " ANSI_COLOR_RED "DEVICE %d " ANSI_COLOR_RESET "w_vec: %d\n", i, w_vec);
printf ("[INFO] " ANSI_COLOR_RED "DEVICE %d " ANSI_COLOR_RESET "data_w: %d\n", i, layer_config[assigned_layers[i][0]+layer][data_w]);
printf ("[INFO] " ANSI_COLOR_RED "DEVICE %d " ANSI_COLOR_RESET "data_h: %d\n", i, layer_config[assigned_layers[i][0]+layer][data_h]);
printf ("[INFO] " ANSI_COLOR_RED "DEVICE %d " ANSI_COLOR_RESET "conv_padding: %d\n", i, layer_config[assigned_layers[i][0]+layer][conv_padding]);
printf ("[INFO] " ANSI_COLOR_RED "DEVICE %d " ANSI_COLOR_RESET "weight_w: %d\n", i, layer_config[assigned_layers[i][0]+layer][weight_w]);
printf ("[INFO] " ANSI_COLOR_RED "DEVICE %d " ANSI_COLOR_RESET "Some #1: %d\n", i, layer_config[layer][data_h]+2*layer_config[assigned_layers[i][0]+layer][conv_padding]-layer_config[assigned_layers[i][0]+layer][weight_h]+1);
printf ("[INFO] " ANSI_COLOR_RED "DEVICE %d " ANSI_COLOR_RESET "first part: %d\n", i, (layer_config[assigned_layers[i][0]+layer][data_w]+2*layer_config[assigned_layers[i][0]+layer][conv_padding]-layer_config[assigned_layers[i][0]+layer][weight_w]));
printf ("[INFO] " ANSI_COLOR_RED "DEVICE %d " ANSI_COLOR_RESET "second part: %d\n", i, (w_vec-layer_config[assigned_layers[i][0]+layer][weight_w]+1));
printf ("[INFO] " ANSI_COLOR_RED "DEVICE %d " ANSI_COLOR_RESET "Some #2: %d\n", i, ((layer_config[assigned_layers[i][0]+layer][data_w]+2*layer_config[assigned_layers[i][0]+layer][conv_padding]-layer_config[assigned_layers[i][0]+layer][weight_w])/(w_vec-layer_config[assigned_layers[i][0]+layer][weight_w]+1)) + 1);
// printf ("[INFO] Some #1: %d, Some #2: %d, data_w: %d, conv_padding: %d, w_vec: %d, weight_w: %d\n", layer_config[layer][data_h]+2*layer_config[layer][conv_padding]-layer_config[layer][weight_h]+1, ceil((layer_config[layer][data_w]+2*layer_config[layer][conv_padding]-W_VEC)/(W_VEC-layer_config[layer][weight_w]+1)), layer_config[layer][data_w], layer_config[layer][conv_padding], w_vec, layer_config[layer][weight_w]);
}
printf ("[INFO] Enqueueing the config buffer to controller queue for the " ANSI_COLOR_RED "DEVICE %d" ANSI_COLOR_RESET "\n", i);
status = clEnqueueWriteBuffer(que_controller[i], config_buf[i], CL_TRUE, 0, sizeof(fpga_configuration) * (layers_per_device[i]+1), &(config[i]), 0, NULL, NULL);
checkError(status, "Failed to transfer config");
}
device_threads.reset(num_devices);
for (int i = 0; i < num_devices; i++) {
device_runner_arg* device_arg = new device_runner_arg;
device_arg->device = i;
printf ("[INFO] Dispatching thread #%d\n", i);
pthread_create(&(device_threads[i]), NULL, device_runner, (void *) device_arg);
}
for (int i = 0; i < num_devices; i++) {
printf ("[INFO] Waiting for device %d\n");
pthread_join((device_threads[i]), NULL);
printf ("[INFO] device %d has joined!\n");
}
//Recorde the end time
printf("\nPipeCNN exited !!!\n\n");
// Release resource
cleanup();
return EXIT_SUCCESS;
}
void loadImageToBuffer(int num)
{
cl_int status;
ifstream bin_file_r;
unsigned file_size;
// load image from binary files
bin_file_r.open(input_file_path, ios::in | ios::binary);
if(bin_file_r.is_open())
{
//Get file size
bin_file_r.seekg(0, bin_file_r.end);
file_size = bin_file_r.tellg();
bin_file_r.seekg(0, bin_file_r.beg);
bin_file_r.read((char *)image, sizeof(DTYPE)*IMAGE_FILE_SIZE);
printf("\n%d bytes image data read from binary files\n", file_size);
if(IMAGE_FILE_SIZE!=(file_size/(sizeof(DTYPE))))
printf("Warning: image file size does not match user configuration !!!\n");
bin_file_r.close();
}
else
printf("Image file does not exits !!!\n");
// Vectorize the input image by a factor of VEC_SIZE
for(unsigned n = 0; n<layer_config[0][data_n]/VEC_SIZE; n++){
for(unsigned i = 0; i<layer_config[0][data_h]; i++){
for(unsigned j = 0; j<layer_config[0][data_w]; j++){
for(unsigned k = 0; k<VEC_SIZE; k++){
if((n*VEC_SIZE+k)<layer_config_original[0][data_n]){ // when layer_config[0][data_n] > layer_config_original[0][data_n], only copy valid pixels
data_init[n*VEC_SIZE*layer_config[0][data_h]*layer_config[0][data_w] + i*layer_config[0][data_w]*VEC_SIZE + j*VEC_SIZE + k]
= (DTYPE) image[(n*VEC_SIZE+k)*layer_config[0][data_h]*layer_config[0][data_w] + i*layer_config[0][data_w] + j];
}
}
}
}
}
// Load image data into buffers
status = clEnqueueWriteBuffer(que_memRdData[0], bottom0_buf[0], CL_TRUE, 0, (layer_config[0][data_w]*layer_config[0][data_h]*layer_config[0][data_n]) * sizeof(DTYPE), data_init, 0, NULL, NULL);
checkError(status, "Failed to transfer input image");
#ifdef VERBOSE_OUTPUT
DTYPE* temp_output = new DTYPE[layer_config[0][data_w]*layer_config[0][data_h]*layer_config[0][data_n]];
status = clEnqueueReadBuffer(que_memRdData, bottom0_buf, CL_TRUE, 0, (layer_config[0][data_w]*layer_config[0][data_h]*layer_config[0][data_n]) * sizeof(DTYPE), (void *) temp_output, 0, NULL, NULL);
checkError (status, "Failed to read back the data");
char fileName[20] = {'\0'};
sprintf (fileName, "Input.txt");
FILE* fp;
fp = fopen(fileName, "w");
for (int i = 0; i < layer_config[0][data_w]*layer_config[0][data_h]*layer_config[0][data_n]; i++) {
fprintf (fp, "%f\n", (float) temp_output[i]);
#endif
}
// Read all input data and golden ref data
int prepare()
{
// Load Image data, CNN net weights and golden_results
ifstream bin_file_r;
unsigned file_size;
unsigned char conv_win_size_dim1, conv_win_size_dim2;
unsigned padding_offset[LAYER_NUM];
// Parameter initialization and safty check
for(unsigned ll=0; ll<LAYER_NUM; ll++){
// First, backup the original layer configurations
for(unsigned ii=0; ii<NUM_CONFIG_ITEM; ii++){
layer_config_original[ll][ii]=layer_config[ll][ii];
}
// Second, perform padding on dim4, when it is not divisible by LANE_NUM
if(layer_config[ll][weight_m]%LANE_NUM != 0){
printf("\nWarnning: layer-%d requires padding zero-value feature maps for give param LANE_NUM=%d\n", ll+1, LANE_NUM);
layer_config[ll][weight_m] = ceil((float)layer_config[ll][weight_m]/LANE_NUM)*LANE_NUM;
layer_config[ll][bias_size] = layer_config[ll][weight_m];
printf(" original num of feature maps is %d, new value is %d\n", layer_config_original[ll][weight_m], layer_config[ll][weight_m]);
// padding of weight on dim4 is needed
padding_offset[ll] = layer_config[ll][weight_m] - layer_config_original[ll][weight_m];
// check if evenly padding on two sides is possible
if(((layer_config[ll][weight_m]/LANE_NUM)%2!=0) & (layer_config[ll][conv_split]==1)){
printf("Error: could not perform padding for split mode, weight_m/LANE_NUM must be divisible by 2 !!!\n\n");
return 1;
}
else{ // padding zeros evenly on two sides of dim4
padding_offset[ll] = padding_offset[ll]/2;
printf(" padding_offset=%d (layer=%d)\n\n", padding_offset[ll], ll+1);
}
}
else{
padding_offset[ll] = 0;
}
// Check parameters
if(ll==0){ // check parameters for layer-1
if(input_config[image_w] != layer_config_original[ll][data_w] || input_config[image_h] != layer_config_original[ll][data_h]
|| input_config[image_n] != layer_config_original[ll][data_n] || input_config[image_n] != layer_config_original[ll][weight_n]){
printf("Error: incorrect layer configuration for layer-%d !!!\n", ll+1);
//return 1;
}
if((layer_config_original[ll][weight_n]!=input_config[image_n])){
printf("\nError: incorrect layer configuration for layer-%d !!!\n", ll+1);
//return 1;
}
}
else{ // other layers
// Currently weight_n must be divisible by VEC_SIZE (for first layer, padding is performed when weight_n is not divisible by VEC_SIZE)
if((layer_config[ll][weight_n]%VEC_SIZE)!=0){
printf("\nError: incorrect setting of parameter VEC_SIZE !!!\n");
return 1;
}
if((layer_config_original[ll][data_n]!=layer_config_original[ll-1][conv_z])){
printf("\nError: incorrect setting of convolution input/output size for layer-%d!!!\n", ll+1);
return 1;
}
}
if((layer_config_original[ll][conv_x]!=(layer_config_original[ll][data_w]-layer_config_original[ll][weight_w]+2*layer_config_original[ll][conv_padding])/layer_config_original[ll][conv_stride]+1)
|| (layer_config_original[ll][conv_y]!=(layer_config_original[ll][data_h]-layer_config_original[ll][weight_h]+2*layer_config_original[ll][conv_padding])/layer_config_original[ll][conv_stride]+1)
|| (layer_config_original[ll][conv_z]!=layer_config_original[ll][weight_m])){
printf("\nError: incorrect setting of convolution output size or filter params for layer-%d!!!\n", ll+1);
return 1;
}
if(layer_config_original[ll][pool_on] && ((layer_config_original[ll][pool_x]!=(layer_config_original[ll][conv_x]-layer_config_original[ll][pool_size])/layer_config_original[ll][pool_stride]+1)
|| (layer_config_original[ll][pool_y]!=(layer_config_original[ll][conv_y]-layer_config_original[ll][pool_size])/layer_config_original[ll][pool_stride]+1)
|| (layer_config_original[ll][pool_z]!=layer_config_original[ll][conv_z]))){
printf("\nError: incorrect setting of pooling input/output size for layer-%d!!!\n", ll+1);
return 1;
}
if(layer_config[ll][conv_x]==1){ // when only one group for FC layer
conv_win_size_dim1 = layer_config[ll][weight_w];
}
else{
conv_win_size_dim1 = layer_config[ll][weight_w]+(CONV_GP_SIZE_X-1)*layer_config[ll][conv_stride];
}
conv_win_size_dim2 = layer_config[ll][weight_h];
// check win_buffer size
/*
if(conv_win_size_dim1*conv_win_size_dim2*layer_config[ll][weight_n]/VEC_SIZE > WIN_BUF_SIZE){
printf("Error: required win_buffer size is %d, configured size is %d, because win_size_dim1=%d and win_size_dim2=%d and weight_n=%d\n", conv_win_size_dim1*conv_win_size_dim2*layer_config[ll][weight_n]/VEC_SIZE, WIN_BUF_SIZE, conv_win_size_dim1, conv_win_size_dim2, layer_config[ll][weight_n]);
return 1;
}
// check weight_buffer size
if(layer_config[ll][weight_w]*layer_config[ll][weight_h]*layer_config[ll][weight_n]/VEC_SIZE > WEIGHT_BUF_SIZE){
printf("Error: required weight_buffer size is %d, configured size is %d \n", layer_config[ll][weight_w]*layer_config[ll][weight_h]*layer_config[ll][weight_n]/VEC_SIZE, WEIGHT_BUF_SIZE);
return 1;
}
*/
}
// image and weight files
weights = (DTYPE *)alignedMalloc(sizeof(DTYPE)*WEIGHTS_FILE_SIZE, DMA_ALIGNMENT);
image = (DTYPE *)alignedMalloc(sizeof(DTYPE)*IMAGE_FILE_SIZE, DMA_ALIGNMENT);
biases = (DTYPE *)alignedMalloc(sizeof(DTYPE)*BIASES_FILE_SIZE, DMA_ALIGNMENT);
// input data buffers
// padding the input RGB image with extra number of zeros channels, so that data_n/weight_n is divisible by VEC_SIZE
layer_config[0][weight_n] = ceil((float)layer_config[0][weight_n]/VEC_SIZE)*VEC_SIZE;
printf ("[INFO] weight_n is changed to %d\n", layer_config[0][weight_n]);
layer_config[0][data_n] = layer_config[0][weight_n];
data_init = (DTYPE *)alignedMalloc(sizeof(DTYPE)*layer_config[0][data_w]*layer_config[0][data_h]*layer_config[0][data_n], DMA_ALIGNMENT);
memset(data_init, 0, sizeof(DTYPE)*layer_config[0][data_w]*layer_config[0][data_h]*layer_config[0][data_n]);// fill non-RGB dims with 0
if(weights == NULL || image == NULL || data_init == NULL || biases == NULL)
{
printf("Not enough memory !!!");
alignedFree(weights);
alignedFree(biases);
alignedFree(image);
alignedFree(data_init);
return 1;
}
// Weights
bin_file_r.open(weight_file_path, ios::in | ios::binary);
if(bin_file_r.is_open())
{
//Get file size
bin_file_r.seekg(0, bin_file_r.end);
file_size = bin_file_r.tellg();
bin_file_r.seekg(0, bin_file_r.beg);
bin_file_r.read((char *)weights, sizeof(DTYPE)*WEIGHTS_FILE_SIZE);
printf("\n%d total weights read \n", file_size/((int)sizeof(DTYPE)));
if(WEIGHTS_FILE_SIZE!=(file_size/(sizeof(DTYPE))))
printf("Warning: weight file size does not match user configuration !!!\n");
bin_file_r.close();
}
else
printf("Weights file does not exits !!!\n");
return 0;
}
// Release all memory resources here
void cleanup()
{
// Release the opencl runtime resource allocated
for(unsigned i = 0; i < 1; ++i) {
// Killing the kernels
if(knl_memRdData[0]) {
clReleaseKernel(knl_memRdData[0]);
}
if(knl_memRdData[1]) {
clReleaseKernel(knl_memRdData[1]);
}
if(knl_memRdWeight[0]) {
clReleaseKernel(knl_memRdWeight[0]);
}
if(knl_memRdWeight[1]) {
clReleaseKernel(knl_memRdWeight[1]);
}
if(knl_controller[0]) {
clReleaseKernel(knl_controller[0]);
}
if(knl_controller[1]) {
clReleaseKernel(knl_controller[1]);
}
if(knl_memWrite[0]) {
clReleaseKernel(knl_memWrite[0]);
}
if(knl_memWrite[1]) {
clReleaseKernel(knl_memWrite[1]);
}
// Killing all the queues
if(que_memRdData[0]) {
clReleaseCommandQueue(que_memRdData[0]);
}
if(que_memRdData[1]) {
clReleaseCommandQueue(que_memRdData[1]);
}
if(que_memRdWeight[0]) {
clReleaseCommandQueue(que_memRdWeight[0]);
}
if(que_memRdWeight[1]) {
clReleaseCommandQueue(que_memRdWeight[1]);
}
if(que_controller[0]) {
clReleaseCommandQueue(que_controller[0]);
}
if(que_controller[1]) {
clReleaseCommandQueue(que_controller[1]);
}
if(que_memWrite[0]) {
clReleaseCommandQueue(que_memWrite[0]);
}
if(que_memWrite[1]) {
clReleaseCommandQueue(que_memWrite[1]);
}
// Killing all the buffers
if(config_buf[0]) {
clReleaseMemObject(config_buf[0]);
}
if(config_buf[1]) {
clReleaseMemObject(config_buf[1]);
}
if(bottom0_buf[0]) {
clReleaseMemObject(bottom0_buf[0]);
}
if(bottom0_buf[1]) {
clReleaseMemObject(bottom1_buf[1]);
}
if(bottom1_buf[0]) {
clReleaseMemObject(bottom1_buf[0]);
}
if(bottom1_buf[1]) {
clReleaseMemObject(bottom1_buf[1]);
}
if(weights_buf[0]) {
clReleaseMemObject(weights_buf[0]);
}
if(weights_buf[1]) {
clReleaseMemObject(weights_buf[1]);
}
if(bias_buf[0]) {
clReleaseMemObject(bias_buf[0]);
}
if(bias_buf[1]) {
clReleaseMemObject(bias_buf[1]);
}
}
if(program[0]) {
clReleaseProgram(program[0]);
}
if(program[1]) {
clReleaseProgram(program[1]);
}
if(context[0]) {
clReleaseContext(context[0]);
}
if(context[1]) {
clReleaseContext(context[1]);
}
alignedFree(weights);
alignedFree(image);
alignedFree(biases);
}
void printCurrentTime() {
char fmt[64];
char buf[64];
struct timeval tv;
struct tm* tm;
gettimeofday(&tv, NULL);
tm = localtime (&tv.tv_sec);
strftime (fmt, sizeof (fmt), "%H:%M:%S:%%6u", tm);
snprintf (buf, sizeof (buf), fmt, tv.tv_usec);
printf ("[INFO] Reading at %s\n", buf);
}
void SplitBufferToArray(char *buffer, char * delim, char ** Output) {
int partcount = 0;
Output[partcount++] = buffer;
char* ptr = buffer;
while (ptr != 0) { //check if the string is over
ptr = strstr(ptr, delim);
if (ptr != NULL) {
*ptr = 0;
Output[partcount++] = ptr + strlen(delim);
ptr = ptr + strlen(delim);
}
}
Output[partcount++] = NULL;
}
void* device_runner (void* args) {
cl_int status;
unsigned int pic_num = 1;
device_runner_arg *device_arg = (device_runner_arg *) args;
int i = device_arg->device;
// Execute the kernel
cl_event deser_event;
cl_event memRdData_event;
cl_event memRdWeight_event;
cl_event memWrite_event;
cl_event ser_event;
cl_event controller_event;
// Recorde the excution time of each operation
cl_ulong memRdData_time;
cl_ulong memRdWeight_time;
cl_ulong memWrite_time;
cl_ulong controller_time;
for (int iter = 0; iter < 1; iter++) {
printf ("[INFO] Iteration number #%d\n", iter);
if (i == 0)
loadImageToBuffer(pic_num);
unsigned argi = 0;
char layer_num = layers_per_device[i]+1;
// Setting the arguments for the controller
argi = 0;
printf ("[INFO] Setting kernel arguments for the controller\n ");
status = clSetKernelArg(knl_controller[i], argi++, sizeof(cl_char), &layer_num);
checkError(status, "Failed to set argument %d of kernel controller", argi-1);
// Only the first device avoids deserialization of the data
char deser_data;
if (i == 0) deser_data = 0;
else deser_data = 1;
status = clSetKernelArg(knl_controller[i], argi++, sizeof(cl_char), &deser_data);
checkError(status, "Failed to set argument %d of kernel controller", argi-1);
char ser_data;
if (i == num_devices-1) ser_data = 0;
else ser_data = 1;
status = clSetKernelArg(knl_controller[i], argi++, sizeof(cl_char), &ser_data);
checkError(status, "Failed to set argument %d of kernel controller", argi-1);
status = clSetKernelArg(knl_controller[i], argi++, sizeof(cl_char), &precision_config[0][frac_w]);
checkError(status, "Failed to set argument %d of kernel controller", argi-1);
status = clSetKernelArg(knl_controller[i], argi++, sizeof(cl_char), &precision_config[0][frac_din]);
checkError(status, "Failed to set argument %d of kernel controller", argi-1);
status = clSetKernelArg(knl_controller[i], argi++, sizeof(cl_char), &precision_config[0][frac_dout]);
checkError(status, "Failed to set argument %d of kernel controller", argi-1);
status = clSetKernelArg(knl_controller[i], argi++, sizeof(cl_mem), &(config_buf[i]));
checkError(status, "Failed to set argument %d of kernel controller", argi-1);
// Setting the arguments for the deser module
argi = 0;
printf("[INFO] Setting kernel arguments for the deser\n");
status = clSetKernelArg(knl_deser[i], argi++, sizeof(cl_char), &deser_data);
checkError(status, "Failed to set argument %d of kernel deser", argi-1);
cl_mem *bottom;
if (assigned_layers[i][0] % 2 == 0) bottom = &(bottom0_buf[i]);
else bottom = &(bottom1_buf[i]);
status = clSetKernelArg(knl_deser[i], argi++, sizeof(cl_mem), bottom);
checkError(status, "Failed to set argument %d of kernel deser", argi-1);
// Setting the arguments for the memory read data module
argi = 0;
char config_size = layer_num;
char start_buffer;
if (assigned_layers[i][0] % 2 == 0) start_buffer = 0x00;
else start_buffer = 0x01;
printf ("[INFO] Setting kernel arguments for the memRdData\n");
status = clSetKernelArg(knl_memRdData[i], argi++, sizeof(cl_char), &config_size);
checkError(status, "Failed to set argument %d of kernel memory read data", argi-1);
status = clSetKernelArg(knl_memRdData[i], argi++, sizeof(cl_char), &start_buffer);
checkError(status, "Failed to set argument %d of kernel memory read data", argi-1);
status = clSetKernelArg(knl_memRdData[i], argi++, sizeof(cl_mem), &(bottom0_buf[i]));
checkError(status, "Failed to set argument %d of kernel memory read data", argi-1);
status = clSetKernelArg(knl_memRdData[i], argi++, sizeof(cl_mem), &(bottom1_buf[i]));
checkError(status, "Failed to set argument %d of kernel memory read data", argi-1);
argi = 0;
printf ("[INFO] Setting kernel arguments for the memRdWeight\n");
status = clSetKernelArg(knl_memRdWeight[i], argi++, sizeof(cl_char), &config_size);
checkError(status, "Failed to set argument %d of kernel memory read weight", argi-1);
status = clSetKernelArg(knl_memRdWeight[i], argi++, sizeof(cl_mem), &(weights_buf[i]));
checkError(status, "Failed to set argument %d of kernel memory read weight", argi-1);
status = clSetKernelArg(knl_memRdWeight[i], argi++, sizeof(cl_mem), &(bias_buf[i]));
checkError(status, "Failed to set argument %d of kernel memory read weight", argi-1);
argi = 0;
printf ("[INFO] Setting kernel arguments for the memWrite\n");
status = clSetKernelArg(knl_memWrite[i], argi++, sizeof(cl_char), &config_size);
checkError(status, "Failed to set argument %d of kernel memory write", argi-1);
status = clSetKernelArg(knl_memWrite[i], argi++, sizeof(cl_mem), &(bottom0_buf[i]));
checkError(status, "Failed to set argument %d of kernel memory write", argi-1);
status = clSetKernelArg(knl_memWrite[i], argi++, sizeof(cl_mem), &(bottom1_buf[i]));
checkError(status, "Failed to set argument %d of kernel memory write", argi-1);
argi = 0;
cl_mem* top;
if (assigned_layers[i][layers_per_device[i]-1] % 2 == 0) top = &(bottom1_buf[i]);
else top = &(bottom0_buf[i]);
printf ("[INFO] Setting kernel argumnents for the memWrite\n");