-
Notifications
You must be signed in to change notification settings - Fork 2
/
sobel.cl
1364 lines (1182 loc) · 82.1 KB
/
sobel.cl
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
/**
*****************************************************************************
* Copyright (C) 2021 Kalray
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @file sobel.cl
*
* Device Sobel kernels step-by-step
*
* @author Minh Quan HO <mqho@kalrayinc.com>
*
******************************************************************************
*/
#include "sobel_config.h"
#define IMAGE_INDEX(y, x) \
((min(y, image_height-1) * image_width) + min(x, image_width-1))
// ============================================================================
// Step 0: Reference GPU-friendly kernel.
// This kernel is considered as baseline for further steps.
// NOTE: For the sake of simplicity of further optimization steps, we implement
// here a "shifted" Sobel filter, in which the output pixel (y+1, x+1) will
// be stored to the index (y, x).
// ============================================================================
__kernel void sobel_step_0(__global uchar *image_in, __global uchar *image_out,
int image_width, int image_height, float scale)
{
int x = get_global_id(0);
int y = get_global_id(1);
if (x >= image_width || y >= image_height) { return; }
// load neighbors
float c0 = image_in[IMAGE_INDEX(y+0, x+0)];
float c1 = image_in[IMAGE_INDEX(y+0, x+1)];
float c2 = image_in[IMAGE_INDEX(y+0, x+2)];
float n0 = image_in[IMAGE_INDEX(y+1, x+0)];
float n2 = image_in[IMAGE_INDEX(y+1, x+2)];
float t0 = image_in[IMAGE_INDEX(y+2, x+0)];
float t1 = image_in[IMAGE_INDEX(y+2, x+1)];
float t2 = image_in[IMAGE_INDEX(y+2, x+2)];
// compute
float magx = mad(2.0f, (n2 - n0), (c2 - c0 + t2 - t0));
float magy = mad(2.0f, (t1 - c1), (t0 - c0 + t2 - c2));
float mag = hypot(magx, magy) * scale;
// store pixel
image_out[IMAGE_INDEX(y, x)] = convert_uchar_sat(mag);
}
// ============================================================================
// Step 1: Explicit Tiling
//
// Note:
// - This is different from the implicit tiling via 2D local workgroup size in
// clEnqueueNDRangeKernel()
// - This is explicit tiling in the kernel code (via macros and loops), the
// workgroup deployment(*) and the way compute is dispatched on all workitems
// within the workgroup
// - Explicit (large) tiling provides better data-locality and cache-hit ratio
// on stencil kernels
//
// (*) One workgroup computes one tile with pre-defined size
// TILE_HEIGHT x TILE_WIDTH, regardless its number of local workitems
// ============================================================================
// NOTE: "Shifted" Sobel filter, whose the output tile is shifted back to
// top-left by one pixel, yielding the below stencil block with HALO_SIZE == 2:
//
// |<------ TILE_WIDTH ------->| HALO_SIZE (== 2)
// -----------+---------------------------+----+
// ʌ | | |
// | | | |
// | | | |
// | | | |
// TILE_HEIGHT | Tile | |
// | | | |
// | | | |
// v | | |
// -----------+---------------------------+ |
// HALO_SIZE | |
// +--------------------------------+
//
// HALO_SIZE will be pruned off on edge tiles to avoid out-of-bound memory access
//
#define BLOCK_INDEX(y, x) \
((min(y, block_height_halo-1) * block_row_stride) + min(x, block_width_halo-1))
/**
* @brief Compute a tile (aka block) (block_height x block_width)
*
* @param block_in Input block
* @param block_out Output block
* @param[in] block_row_stride Row stride in uchar-pixel of input block
* @param[in] block_width Block width
* @param[in] block_height Block height
* @param[in] block_width_halo Maximal block width with halo (used for clamp)
* @param[in] block_height_halo Maximal block height with halo (used for clamp)
* @param[in] scale Magnitude scale
*/
static void sobel_compute_block_step_1(__global uchar *block_in,
__global uchar *block_out,
int block_row_stride,
int block_width, int block_height,
int block_width_halo, int block_height_halo,
float scale)
{
const int lsizex = get_local_size(0);
const int lsizey = get_local_size(1);
const int lidx = get_local_id(0);
const int lidy = get_local_id(1);
// number of workitems in workgroup
const int num_wi = lsizex * lsizey;
// linearized workitem id in workgroup
const int wid = lidx + lidy * lsizex;
// dispatch rows of block block_height x block_width on workitems
const int num_rows_per_wi = block_height / num_wi;
const int num_rows_trailing = block_height % num_wi;
const int irow_begin = wid * num_rows_per_wi + min(wid, num_rows_trailing);
const int irow_end = irow_begin + num_rows_per_wi + ((wid < num_rows_trailing) ? 1 : 0);
for (int irow = irow_begin; irow < irow_end; irow++)
{
for (int icol = 0; icol < block_width; icol++)
{
// load neighbors
float c0 = block_in[BLOCK_INDEX(irow+0, icol+0)];
float c1 = block_in[BLOCK_INDEX(irow+0, icol+1)];
float c2 = block_in[BLOCK_INDEX(irow+0, icol+2)];
float n0 = block_in[BLOCK_INDEX(irow+1, icol+0)];
float n2 = block_in[BLOCK_INDEX(irow+1, icol+2)];
float t0 = block_in[BLOCK_INDEX(irow+2, icol+0)];
float t1 = block_in[BLOCK_INDEX(irow+2, icol+1)];
float t2 = block_in[BLOCK_INDEX(irow+2, icol+2)];
// compute
float magx = mad(2.0f, (n2 - n0), (c2 - c0 + t2 - t0));
float magy = mad(2.0f, (t1 - c1), (t0 - c0 + t2 - c2));
float mag = hypot(magx, magy) * scale;
// store pixel
block_out[BLOCK_INDEX(irow, icol)] = convert_uchar_sat(mag);
}
}
}
__kernel void sobel_step_1(__global uchar *image_in, __global uchar *image_out,
int image_width, int image_height, float scale)
{
const int group_idx = get_group_id(0);
const int group_idy = get_group_id(1);
const int block_idx = group_idx * TILE_WIDTH;
const int block_idy = group_idy * TILE_HEIGHT;
if (block_idx >= image_width || block_idy >= image_height) { return; }
const ulong block_offset = (block_idy * image_width) + block_idx;
__global uchar *block_in = image_in + block_offset;
__global uchar *block_out = image_out + block_offset;
const int block_row_stride = image_width;
const int block_width = min(TILE_WIDTH, (image_width-block_idx));
const int block_height = min(TILE_HEIGHT, (image_height-block_idy));
const int block_width_halo = min((TILE_WIDTH+HALO_SIZE), (image_width-block_idx));
const int block_height_halo = min((TILE_HEIGHT+HALO_SIZE), (image_height-block_idy));
sobel_compute_block_step_1(block_in, block_out,
block_row_stride,
block_width, block_height,
block_width_halo, block_height_halo,
scale);
}
// ============================================================================
// Step 2: Explicit Tiling + __local on input
//
// - Same as Step-1, but use __local to preload input block into local memory
// before processing.
// - Some parameters such as `block_row_stride` will change because we are
// working from local memory, instead of global memory in step-1.
// - Note that this step does not necessarily outperform the step-1, typically
// when tiling has been done and $L2 cache enabled ($L2 cache is
// in SMEM, same as __local). The goal of this step is to introduce using of
// __local and basic async-copy (no overlapping, yet), preparing for later
// optimization steps (3, 4, 5, ...).
// ============================================================================
// We differentiate now between block_in_row_stride and
// block_out_row_stride, since blocks now may reside in different address
// spaces, between __local and __global.
#define BLOCK_IN_INDEX(y, x) \
((min(y, block_height_halo-1) * block_in_row_stride) + min(x, block_width_halo-1))
#define BLOCK_OUT_INDEX(y, x) \
((min(y, block_height_halo-1) * block_out_row_stride) + min(x, block_width_halo-1))
/**
* @brief Compute a tile (aka block) (block_height x block_width)
*
* @param block_in_local Input block in __local
* @param block_out Output block
* @param[in] block_in_row_stride Row stride in uchar-pixel of __local input block
* @param[in] block_out_row_stride Row stride in uchar-pixel of __global output block
* @param[in] block_width Block width
* @param[in] block_height Block height
* @param[in] block_width_halo Maximal block width with halo (used for clamp)
* @param[in] block_height_halo Maximal block height with halo (used for clamp)
* @param[in] scale Magnitude scale
*/
static void sobel_compute_block_step_2(__local uchar *block_in_local,
__global uchar *block_out,
int block_in_row_stride, int block_out_row_stride,
int block_width, int block_height,
int block_width_halo, int block_height_halo,
float scale)
{
const int lsizex = get_local_size(0);
const int lsizey = get_local_size(1);
const int lidx = get_local_id(0);
const int lidy = get_local_id(1);
// number of workitems in workgroup
const int num_wi = lsizex * lsizey;
// linearized workitem id in workgroup
const int wid = lidx + lidy * lsizex;
// dispatch rows of block block_height x block_width on workitems
const int num_rows_per_wi = block_height / num_wi;
const int num_rows_trailing = block_height % num_wi;
const int irow_begin = wid * num_rows_per_wi + min(wid, num_rows_trailing);
const int irow_end = irow_begin + num_rows_per_wi + ((wid < num_rows_trailing) ? 1 : 0);
for (int irow = irow_begin; irow < irow_end; irow++)
{
for (int icol = 0; icol < block_width; icol++)
{
// load neighbors
float c0 = block_in_local[BLOCK_IN_INDEX(irow+0, icol+0)];
float c1 = block_in_local[BLOCK_IN_INDEX(irow+0, icol+1)];
float c2 = block_in_local[BLOCK_IN_INDEX(irow+0, icol+2)];
float n0 = block_in_local[BLOCK_IN_INDEX(irow+1, icol+0)];
float n2 = block_in_local[BLOCK_IN_INDEX(irow+1, icol+2)];
float t0 = block_in_local[BLOCK_IN_INDEX(irow+2, icol+0)];
float t1 = block_in_local[BLOCK_IN_INDEX(irow+2, icol+1)];
float t2 = block_in_local[BLOCK_IN_INDEX(irow+2, icol+2)];
// compute
float magx = mad(2.0f, (n2 - n0), (c2 - c0 + t2 - t0));
float magy = mad(2.0f, (t1 - c1), (t0 - c0 + t2 - c2));
float mag = hypot(magx, magy) * scale;
// store pixel
block_out[BLOCK_OUT_INDEX(irow, icol)] = convert_uchar_sat(mag);
}
}
}
__kernel void sobel_step_2(__global uchar *image_in, __global uchar *image_out,
int image_width, int image_height, float scale)
{
__local uchar block_in_local[(TILE_HEIGHT + HALO_SIZE) * (TILE_WIDTH + HALO_SIZE)];
const int group_idx = get_group_id(0);
const int group_idy = get_group_id(1);
const int block_idx = group_idx * TILE_WIDTH;
const int block_idy = group_idy * TILE_HEIGHT;
if (block_idx >= image_width || block_idy >= image_height) { return; }
const ulong block_offset = (block_idy * image_width) + block_idx;
__global uchar *block_in = image_in + block_offset;
__global uchar *block_out = image_out + block_offset;
const int block_width = min(TILE_WIDTH, (image_width-block_idx));
const int block_height = min(TILE_HEIGHT, (image_height-block_idy));
const int block_width_halo = min((TILE_WIDTH+HALO_SIZE), (image_width-block_idx));
const int block_height_halo = min((TILE_HEIGHT+HALO_SIZE), (image_height-block_idy));
const int block_in_row_stride = block_width_halo;
const int block_out_row_stride = image_width;
// ------------------------------------------------------------
// Before computing, copy data to __local
// (block_height_halo x block_width_halo)
// ------------------------------------------------------------
event_t event;
// To perform a 2D block-copy, we can either use:
// - for-loop with traditional load/store
// - for-loop with 1D async-copy primitive (OpenCL 1.2)
// - Kalray extension 2D async-copy primitive
//
// Let's use 2D copy for better performance than for-loop, either
// - OpenCL-3.0 draft spec of 2D2D, or
// - Kalray-own extension of 2D2D (pixel-based)
#define HAVE_ASYNC_COPY_2D2D_OCL_3_0
// #define HAVE_ASYNC_COPY_2D2D_KALRAY
// grayscale image, but can change on RGB or YUV or RGBA images
const size_t num_gentype_per_pixel = 1;
#ifdef HAVE_ASYNC_COPY_2D2D_OCL_3_0
size_t num_elements_per_line = block_width_halo * num_gentype_per_pixel;
size_t num_lines = block_height_halo;
// pre-subtract strides by num_elements_per_line, then the call will
// (according to the spec) increment them by num_elements_per_line again
size_t src_stride = (image_width * num_gentype_per_pixel) - num_elements_per_line;
size_t dst_stride = (block_width_halo * num_gentype_per_pixel) - num_elements_per_line;
// copy to {0, 0} in __local
size_t local_offset_gentypes = ((0 * block_width_halo) + 0) * num_gentype_per_pixel;
// copy from {block_idx, block_idy} in __global
size_t global_offset_gentypes = ((block_idy * image_width) + block_idx) * num_gentype_per_pixel;
event = async_work_group_copy_2D2D(
block_in_local + local_offset_gentypes, // __local buffer (dst)
image_in + global_offset_gentypes, // __global buffer (src)
num_elements_per_line, // num_elements_per_line
num_lines, // num_lines
src_stride, // src_stride
dst_stride, // dst_stride
0);
#elif defined(HAVE_ASYNC_COPY_2D2D_KALRAY)
int2 block_to_copy = (int2)(block_width_halo, block_height_halo);
// copy to {0, 0} in __local
int4 local_point = (int4)(0, 0, block_width_halo, block_height_halo);
// copy from {block_idx, block_idy} in __global
int4 global_point = (int4)(block_idx, block_idy, image_width, image_height );
event = async_work_group_copy_block_2D2D(
block_in_local, // __local buffer
image_in, // __global image
num_gentype_per_pixel, // num_gentype_per_pixel
block_to_copy, // block to copy
local_point, // local_point
global_point, // global_point
0);
#else
for (int irow = 0; irow < block_height_halo; irow++)
{
event = async_work_group_copy(block_in_local + (irow * block_in_row_stride),
block_in + (irow * block_out_row_stride),
block_width_halo, 0);
}
#endif
// Wait immediately. There is almost no overlapping. This will be tackled
// in the future steps
wait_group_events(1, &event);
// ------------------------------------------------------------
// Compute (same as sobel_compute_block_step_1() but with
// block_in_local[])
// ------------------------------------------------------------
sobel_compute_block_step_2(block_in_local, block_out,
block_in_row_stride, block_out_row_stride,
block_width, block_height,
block_width_halo, block_height_halo,
scale);
}
// ============================================================================
// Step 3: Explicit Tiling + __local on both input/output
//
// - Same as Step-2, but use __local to store output block before sending to
// __global
// ============================================================================
/**
* @brief Compute a tile (aka block) (block_height x block_width)
*
* @param block_in_local Input block in __local
* @param block_out_local Output block in __local
* @param[in] block_in_row_stride Row stride in uchar-pixel of __local input block
* @param[in] block_out_row_stride Row stride in uchar-pixel of __local output block
* @param[in] block_width Block width
* @param[in] block_height Block height
* @param[in] block_width_halo Maximal block width with halo (used for clamp)
* @param[in] block_height_halo Maximal block height with halo (used for clamp)
* @param[in] scale Magnitude scale
*/
static void sobel_compute_block_step_3(__local uchar *block_in_local,
__local uchar *block_out_local,
int block_in_row_stride, int block_out_row_stride,
int block_width, int block_height,
int block_width_halo, int block_height_halo,
float scale)
{
const int lsizex = get_local_size(0);
const int lsizey = get_local_size(1);
const int lidx = get_local_id(0);
const int lidy = get_local_id(1);
// number of workitems in workgroup
const int num_wi = lsizex * lsizey;
// linearized workitem id in workgroup
const int wid = lidx + lidy * lsizex;
// dispatch rows of block block_height x block_width on workitems
const int num_rows_per_wi = block_height / num_wi;
const int num_rows_trailing = block_height % num_wi;
const int irow_begin = wid * num_rows_per_wi + min(wid, num_rows_trailing);
const int irow_end = irow_begin + num_rows_per_wi + ((wid < num_rows_trailing) ? 1 : 0);
for (int irow = irow_begin; irow < irow_end; irow++)
{
for (int icol = 0; icol < block_width; icol++)
{
// load neighbors
float c0 = block_in_local[BLOCK_IN_INDEX(irow+0, icol+0)];
float c1 = block_in_local[BLOCK_IN_INDEX(irow+0, icol+1)];
float c2 = block_in_local[BLOCK_IN_INDEX(irow+0, icol+2)];
float n0 = block_in_local[BLOCK_IN_INDEX(irow+1, icol+0)];
float n2 = block_in_local[BLOCK_IN_INDEX(irow+1, icol+2)];
float t0 = block_in_local[BLOCK_IN_INDEX(irow+2, icol+0)];
float t1 = block_in_local[BLOCK_IN_INDEX(irow+2, icol+1)];
float t2 = block_in_local[BLOCK_IN_INDEX(irow+2, icol+2)];
// compute
float magx = mad(2.0f, (n2 - n0), (c2 - c0 + t2 - t0));
float magy = mad(2.0f, (t1 - c1), (t0 - c0 + t2 - c2));
float mag = hypot(magx, magy) * scale;
// store pixel
block_out_local[BLOCK_OUT_INDEX(irow, icol)] = convert_uchar_sat(mag);
}
}
// sync to gather result from all WI
barrier(CLK_LOCAL_MEM_FENCE);
}
__kernel void sobel_step_3(__global uchar *image_in, __global uchar *image_out,
int image_width, int image_height, float scale)
{
__local uchar block_in_local [(TILE_HEIGHT + HALO_SIZE) * (TILE_WIDTH + HALO_SIZE)];
__local uchar block_out_local[ TILE_HEIGHT * TILE_WIDTH ];
const int group_idx = get_group_id(0);
const int group_idy = get_group_id(1);
const int block_idx = group_idx * TILE_WIDTH;
const int block_idy = group_idy * TILE_HEIGHT;
if (block_idx >= image_width || block_idy >= image_height) { return; }
const ulong block_offset = (block_idy * image_width) + block_idx;
__global uchar *block_in = image_in + block_offset;
__global uchar *block_out = image_out + block_offset;
const int block_width = min(TILE_WIDTH, (image_width-block_idx));
const int block_height = min(TILE_HEIGHT, (image_height-block_idy));
const int block_width_halo = min((TILE_WIDTH+HALO_SIZE), (image_width-block_idx));
const int block_height_halo = min((TILE_HEIGHT+HALO_SIZE), (image_height-block_idy));
const int block_in_row_stride = block_width_halo;
const int block_out_row_stride = block_width;
// ------------------------------------------------------------
// Before computing, copy data to __local
// (block_height_halo x block_width_halo)
// ------------------------------------------------------------
event_t event;
int2 block_to_copy = (int2)(block_width_halo, block_height_halo);
int4 local_point = (int4)( 0 , 0 , block_width_halo, block_height_halo);
int4 global_point = (int4)(block_idx, block_idy, image_width , image_height );
event = async_work_group_copy_block_2D2D(
block_in_local, // __local buffer
image_in, // __global image
1, // num_gentype_per_pixel
block_to_copy, // block to copy
local_point, // local_point
global_point, // global_point
0);
// Wait immediately. There is almost no overlapping. This will be tackled
// in the future steps
wait_group_events(1, &event);
// ------------------------------------------------------------
// Compute (same as sobel_compute_block_step_1() but with
// block_in_local[])
// ------------------------------------------------------------
sobel_compute_block_step_3(block_in_local, block_out_local,
block_in_row_stride, block_out_row_stride,
block_width, block_height,
block_width_halo, block_height_halo,
scale);
// ------------------------------------------------------------
// After computing, send result to __global
// (block_height x block_width)
// ------------------------------------------------------------
block_to_copy = (int2)(block_width, block_height);
local_point = (int4)( 0 , 0 , block_width, block_height);
event = async_work_group_copy_block_2D2D(
image_out, // __global image
block_out_local, // __local buffer
1, // num_gentype_per_pixel
block_to_copy, // block to copy
local_point, // local_point
global_point, // global_point
0);
// Wait immediately. There is almost no overlapping. This will be tackled
// in the future steps
wait_group_events(1, &event);
}
// ============================================================================
// Step 4: Explicit Tiling + __local on both input/output + double-buffering
//
// - Same as Step-3, but with double-buffering and overlapping, this will be
// the ultimate optimization step on data transfer.
// - Unlike previous steps 1/2/3, in which each WG computes one tile, and
// there are as many WG as the number of tiles in image. In this step, there
// will be only 5 WG spawned (CL_DEVICE_MAX_COMPUTE_UNITS). Each WG will be
// in charge of multiple tiles and perform overlapping through
// double-buffering and async-copy.
// ============================================================================
// We reuse the same sobel_compute_block_step_3() function for step-4
static void sobel_compute_block_step_4(__local uchar *block_in_local,
__local uchar *block_out_local,
int block_in_row_stride, int block_out_row_stride,
int block_width, int block_height,
int block_width_halo, int block_height_halo,
float scale)
__attribute__((alias("sobel_compute_block_step_3")));
#define OCL_KERNEL_DMA_TILING_ENGINE_SOBEL(KERNEL_NAME, COMPUTE_BLOCK_FUNC) \
\
__kernel void KERNEL_NAME(__global uchar *image_in, __global uchar *image_out, \
int image_width, int image_height, float scale) \
{ \
__local uchar block_in_local [2][(TILE_HEIGHT + HALO_SIZE) * (TILE_WIDTH + HALO_SIZE)]; \
__local uchar block_out_local[2][ TILE_HEIGHT * TILE_WIDTH ]; \
\
event_t event_read[2] = {0, 0}; \
event_t event_write[2] = {0, 0}; \
\
const int group_idx = get_group_id(0); \
const int group_idy = get_group_id(1); \
\
const int num_groups = get_num_groups(0) * get_num_groups(1); \
const int group_id = (group_idy * get_num_groups(0)) + group_idx; \
\
const int num_blocks_x = (int)ceil(((float)image_width) / TILE_WIDTH); \
const int num_blocks_y = (int)ceil(((float)image_height) / TILE_HEIGHT); \
const int num_blocks_total = num_blocks_x * num_blocks_y; \
\
const int block_dispatch_step = num_groups; \
const int iblock_begin = group_id; \
const int iblock_end = num_blocks_total; \
\
/* ===================================================================== */ \
/* PROLOGUE: prefetch first block */ \
/* ===================================================================== */ \
int2 block_to_copy; \
int4 local_point; \
int4 global_point; \
\
int iblock_x_next = iblock_begin % num_blocks_x; \
int iblock_y_next = iblock_begin / num_blocks_x; \
\
int block_idx_next = iblock_x_next * TILE_WIDTH; \
int block_idy_next = iblock_y_next * TILE_HEIGHT; \
\
int block_width_next = min(TILE_WIDTH, (image_width-block_idx_next)); \
int block_height_next = min(TILE_HEIGHT, (image_height-block_idy_next)); \
\
int block_width_halo_next = min((TILE_WIDTH+HALO_SIZE), (image_width-block_idx_next)); \
int block_height_halo_next = min((TILE_HEIGHT+HALO_SIZE), (image_height-block_idy_next)); \
\
/* prefetch first block */ \
int block_counter = 0; \
block_to_copy = (int2)(block_width_halo_next, block_height_halo_next); \
local_point = (int4)(0, 0, block_width_halo_next, block_height_halo_next); \
global_point = (int4)(block_idx_next, block_idy_next, image_width, image_height); \
\
event_read[block_counter & 1] = async_work_group_copy_block_2D2D( \
block_in_local[block_counter & 1], /* __local buffer */ \
image_in, /* __global image */ \
1, /* num_gentype_per_pixel */ \
block_to_copy, /* block to copy */ \
local_point, /* local_point */ \
global_point, /* global_point */ \
0); \
\
/* ===================================================================== */ \
/* FOR-LOOP: Compute all blocks */ \
/* ===================================================================== */ \
for (int iblock = iblock_begin; iblock < iblock_end; iblock += block_dispatch_step, \
block_counter++) \
{ \
/* ------------------------------------------------------------ */ \
/* current block to be processed */ \
/* ------------------------------------------------------------ */ \
const int iblock_parity = block_counter & 1; \
\
const int block_idx = block_idx_next; \
const int block_idy = block_idy_next; \
\
const int block_width = block_width_next; \
const int block_height = block_height_next; \
\
const int block_width_halo = block_width_halo_next; \
const int block_height_halo = block_height_halo_next; \
\
const int block_in_row_stride = block_width_halo; \
const int block_out_row_stride = block_width; \
\
/* ------------------------------------------------------------ */ \
/* prefetch next block (if any) */ \
/* ------------------------------------------------------------ */ \
const int iblock_next = iblock + block_dispatch_step; \
\
if (iblock_next < iblock_end) \
{ \
const int iblock_next_parity = (block_counter+1) & 1; \
\
iblock_x_next = iblock_next % num_blocks_x; \
iblock_y_next = iblock_next / num_blocks_x; \
block_idx_next = iblock_x_next * TILE_WIDTH; \
block_idy_next = iblock_y_next * TILE_HEIGHT; \
\
block_width_next = min(TILE_WIDTH, (image_width-block_idx_next)); \
block_height_next = min(TILE_HEIGHT, (image_height-block_idy_next)); \
\
block_width_halo_next = min((TILE_WIDTH+HALO_SIZE), (image_width-block_idx_next)); \
block_height_halo_next = min((TILE_HEIGHT+HALO_SIZE), (image_height-block_idy_next)); \
\
block_to_copy = (int2)(block_width_halo_next, block_height_halo_next); \
local_point = (int4)(0, 0, block_width_halo_next, block_height_halo_next); \
global_point = (int4)(block_idx_next, block_idy_next, image_width, image_height); \
\
event_read[iblock_next_parity] = async_work_group_copy_block_2D2D( \
block_in_local[iblock_next_parity], /* __local buffer */ \
image_in, /* __global image */ \
1, /* num_gentype_per_pixel */ \
block_to_copy, /* block to copy */ \
local_point, /* local_point */ \
global_point, /* global_point */ \
0); \
} \
\
/* ------------------------------------------------------------ */ \
/* wait for prefetch of current block */ \
/* ------------------------------------------------------------ */ \
wait_group_events(1, &event_read[iblock_parity]); \
\
/* ------------------------------------------------------------ */ \
/* wait for previous put of the 2D block from local to global */ \
/* to avoid data race: writing result to a being-put buffer */ \
/* ------------------------------------------------------------ */ \
if (block_counter >= 2) { \
wait_group_events(1, &event_write[iblock_parity]); \
} \
\
/* ------------------------------------------------------------ */ \
/* now ready to compute the current block */ \
/* ------------------------------------------------------------ */ \
COMPUTE_BLOCK_FUNC(block_in_local[iblock_parity], \
block_out_local[iblock_parity], \
block_in_row_stride, block_out_row_stride, \
block_width, block_height, \
block_width_halo, block_height_halo, \
scale); \
\
/* ------------------------------------------------------------ */ \
/* put result to global memory */ \
/* ------------------------------------------------------------ */ \
block_to_copy = (int2)(block_width, block_height); \
local_point = (int4)( 0 , 0 , block_width, block_height); \
global_point = (int4)(block_idx, block_idy, image_width, image_height); \
event_write[iblock_parity] = async_work_group_copy_block_2D2D( \
image_out, /* __global image */ \
block_out_local[iblock_parity], /* __local buffer */ \
1, /* num_gentype_per_pixel */ \
block_to_copy, /* block to copy */ \
local_point, /* local_point */ \
global_point, /* global_point */ \
0); \
\
} \
\
/* ===================================================================== */ \
/* End of compute, fence all outstanding put */ \
/* ===================================================================== */ \
async_work_group_copy_fence(CLK_GLOBAL_MEM_FENCE); \
}
OCL_KERNEL_DMA_TILING_ENGINE_SOBEL(sobel_step_4, sobel_compute_block_step_4)
// ============================================================================
// Step 4-PAPI: PAPI Profiling
//
// - Now we have async-ed all the things, let's try identifying potential
// bottlenecks with some PAPI instrumentation.
//
// - Depending on image-processing algorithms, performance bottlenecks can be
// in data transfer, in pre/post-processing, or in compute_block() etc.
//
// ============================================================================
#define MAX_PAPI_EVENTS 2
#define PAPI_LOG_REAL_CYCLE(counter) \
if (group_id < 5 && lidx == 0 && lidy == 0) { counter = PAPI_get_real_cyc(); }
#define PAPI_ACCUMULATE_REAL_CYCLE(counter, acc) \
if (group_id < 5 && lidx == 0 && lidy == 0) { acc += PAPI_get_real_cyc() - counter; }
#define PAPI_LOG_REAL_USEC(counter) \
if (group_id < 5 && lidx == 0 && lidy == 0) { counter = PAPI_get_real_usec(); }
#define PAPI_ACCUMULATE_REAL_USEC(counter, acc) \
if (group_id < 5 && lidx == 0 && lidy == 0) { acc += PAPI_get_real_usec() - counter; }
#define OCL_KERNEL_DMA_TILING_ENGINE_SOBEL_PAPI(KERNEL_NAME, COMPUTE_BLOCK_FUNC) \
\
__kernel void KERNEL_NAME(__global uchar *image_in, __global uchar *image_out, \
int image_width, int image_height, float scale) \
{ \
__local uchar block_in_local [2][(TILE_HEIGHT + HALO_SIZE) * (TILE_WIDTH + HALO_SIZE)]; \
__local uchar block_out_local[2][ TILE_HEIGHT * TILE_WIDTH ]; \
\
event_t event_read[2] = {0, 0}; \
event_t event_write[2] = {0, 0}; \
\
const int lidx = get_local_id(0); \
const int lidy = get_local_id(1); \
\
const int group_idx = get_group_id(0); \
const int group_idy = get_group_id(1); \
\
const int num_groups = get_num_groups(0) * get_num_groups(1); \
const int group_id = (group_idy * get_num_groups(0)) + group_idx; \
\
const int num_blocks_x = (int)ceil(((float)image_width) / TILE_WIDTH); \
const int num_blocks_y = (int)ceil(((float)image_height) / TILE_HEIGHT); \
const int num_blocks_total = num_blocks_x * num_blocks_y; \
\
const int block_dispatch_step = num_groups; \
const int iblock_begin = group_id; \
const int iblock_end = num_blocks_total; \
\
/* ===================================================================== */ \
/* PROLOGUE: prefetch first block */ \
/* ===================================================================== */ \
int2 block_to_copy; \
int4 local_point; \
int4 global_point; \
\
int iblock_x_next = iblock_begin % num_blocks_x; \
int iblock_y_next = iblock_begin / num_blocks_x; \
\
int block_idx_next = iblock_x_next * TILE_WIDTH; \
int block_idy_next = iblock_y_next * TILE_HEIGHT; \
\
int block_width_next = min(TILE_WIDTH, (image_width-block_idx_next)); \
int block_height_next = min(TILE_HEIGHT, (image_height-block_idy_next)); \
\
int block_width_halo_next = min((TILE_WIDTH+HALO_SIZE), (image_width-block_idx_next)); \
int block_height_halo_next = min((TILE_HEIGHT+HALO_SIZE), (image_height-block_idy_next)); \
\
/* prefetch first block */ \
int block_counter = 0; \
block_to_copy = (int2)(block_width_halo_next, block_height_halo_next); \
local_point = (int4)(0, 0, block_width_halo_next, block_height_halo_next); \
global_point = (int4)(block_idx_next, block_idy_next, image_width, image_height); \
\
event_read[block_counter & 1] = async_work_group_copy_block_2D2D( \
block_in_local[block_counter & 1], /* __local buffer */ \
image_in, /* __global image */ \
1, /* num_gentype_per_pixel */ \
block_to_copy, /* block to copy */ \
local_point, /* local_point */ \
global_point, /* global_point */ \
0); \
\
/* ===================================================================== */ \
/* Setting up PAPI */ \
/* ===================================================================== */ \
/* Hardware Performance Monitoring Counters (PMC) */ \
/* With PAPI, each core can use upto two PMCs to track events. */ \
/* For simplicity, we use PE0 of each cluster to measure two differents */ \
/* events. User can refer to the Kalray PAPI manual and/or Kalray VLIW */ \
/* architecture document for details on these PMC events. */ \
__constant char *pmc_names[10] = { \
/* Work-group 0 */ "PCC", "EBE", \
/* Work-group 1 */ "DCLHE", "DCLME", \
/* Work-group 2 */ "DARSC", "LDSC", \
/* Work-group 3 */ "DMAE", "DDSC", \
/* Work-group 4 */ "PSC", "SC" \
}; \
long pmc_values[MAX_PAPI_EVENTS] = {0}; \
long tmp_cycle = 0; \
long tmp_usec = 0; \
long sobel_cycles = 0; \
long sobel_usec = 0; \
int papi_evenSet = PAPI_NULL; \
if (group_id < 5 && lidx == 0 && lidy == 0) { \
int ret; \
ret = PAPI_create_eventset(&papi_evenSet); \
if (ret != PAPI_OK) { \
printf("Failed to PAPI_create_eventset\n"); \
} \
for (int i = 0; i < MAX_PAPI_EVENTS; i++) { \
ret = PAPI_add_named_event(papi_evenSet, pmc_names[group_id*2 + i]); \
if (ret != PAPI_OK) { \
printf("Failed to PAPI_add_named_event(%s)\n", pmc_names[group_id*2 + i]); \
} \
} \
} \
\
/* ===================================================================== */ \
/* FOR-LOOP: Compute all blocks */ \
/* ===================================================================== */ \
for (int iblock = iblock_begin; iblock < iblock_end; iblock += block_dispatch_step, \
block_counter++) \
{ \
/* ------------------------------------------------------------ */ \
/* current block to be processed */ \
/* ------------------------------------------------------------ */ \
const int iblock_parity = block_counter & 1; \
\
const int block_idx = block_idx_next; \
const int block_idy = block_idy_next; \
\
const int block_width = block_width_next; \
const int block_height = block_height_next; \
\
const int block_width_halo = block_width_halo_next; \
const int block_height_halo = block_height_halo_next; \
\
const int block_in_row_stride = block_width_halo; \
const int block_out_row_stride = block_width; \
\
/* ------------------------------------------------------------ */ \
/* prefetch next block (if any) */ \
/* ------------------------------------------------------------ */ \
const int iblock_next = iblock + block_dispatch_step; \
\
if (iblock_next < iblock_end) \
{ \
const int iblock_next_parity = (block_counter+1) & 1; \
\
iblock_x_next = iblock_next % num_blocks_x; \
iblock_y_next = iblock_next / num_blocks_x; \
block_idx_next = iblock_x_next * TILE_WIDTH; \
block_idy_next = iblock_y_next * TILE_HEIGHT; \
\
block_width_next = min(TILE_WIDTH, (image_width-block_idx_next)); \
block_height_next = min(TILE_HEIGHT, (image_height-block_idy_next)); \
\
block_width_halo_next = min((TILE_WIDTH+HALO_SIZE), (image_width-block_idx_next)); \
block_height_halo_next = min((TILE_HEIGHT+HALO_SIZE), (image_height-block_idy_next)); \
\
block_to_copy = (int2)(block_width_halo_next, block_height_halo_next); \
local_point = (int4)(0, 0, block_width_halo_next, block_height_halo_next); \
global_point = (int4)(block_idx_next, block_idy_next, image_width, image_height); \
\
event_read[iblock_next_parity] = async_work_group_copy_block_2D2D( \
block_in_local[iblock_next_parity], /* __local buffer */ \
image_in, /* __global image */ \
1, /* num_gentype_per_pixel */ \
block_to_copy, /* block to copy */ \
local_point, /* local_point */ \
global_point, /* global_point */ \
0); \
} \
\
/* ------------------------------------------------------------ */ \
/* wait for prefetch of current block */ \
/* ------------------------------------------------------------ */ \
wait_group_events(1, &event_read[iblock_parity]); \
\
/* ------------------------------------------------------------ */ \
/* wait for previous put of the 2D block from local to global */ \
/* to avoid data race: writing result to a being-put buffer */ \
/* ------------------------------------------------------------ */ \
if (block_counter >= 2) { \
wait_group_events(1, &event_write[iblock_parity]); \
} \
\
/* ------------------------------------------------------------ */ \
/* now ready to compute the current block */ \
/* ------------------------------------------------------------ */ \
PAPI_LOG_REAL_CYCLE(tmp_cycle); \
PAPI_LOG_REAL_USEC(tmp_usec); \
\
long pmc_unit[MAX_PAPI_EVENTS] = {0}; \
if (group_id < 5 && lidx == 0 && lidy == 0) { \
int ret; \
ret = PAPI_reset(papi_evenSet); \
if (ret != PAPI_OK) { \
printf("Failed to PAPI_reset\n"); \
} \
ret = PAPI_start(papi_evenSet); \
if (ret != PAPI_OK) { \
printf("Failed to PAPI_start\n"); \
} \
} \
\
COMPUTE_BLOCK_FUNC(block_in_local[iblock_parity], \
block_out_local[iblock_parity], \
block_in_row_stride, block_out_row_stride, \
block_width, block_height, \
block_width_halo, block_height_halo, \
scale); \
\
if (group_id < 5 && lidx == 0 && lidy == 0) { \
int ret = PAPI_stop(papi_evenSet, pmc_unit); \
if (ret != PAPI_OK) { \
printf("Failed to PAPI_stop\n"); \
} \
\
/* accumulate */ \
for (int i = 0; i < MAX_PAPI_EVENTS; i++) { \
pmc_values[i] += pmc_unit[i]; \
} \
} \
\
PAPI_ACCUMULATE_REAL_CYCLE(tmp_cycle, sobel_cycles); \
PAPI_ACCUMULATE_REAL_USEC(tmp_usec, sobel_usec); \
\
/* ------------------------------------------------------------ */ \
/* put result to global memory */ \
/* ------------------------------------------------------------ */ \
block_to_copy = (int2)(block_width, block_height); \