-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgramCU.cu
More file actions
executable file
·2097 lines (1832 loc) · 71.9 KB
/
ProgramCU.cu
File metadata and controls
executable file
·2097 lines (1832 loc) · 71.9 KB
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
#include "stdio.h"
#include <vector>
#include <cassert>
#include <cuda_runtime.h>
#include <device_launch_parameters.h>
#include "CuTexImage.h"
#include "ProgramCU.h"
#include "GlobalUtil.h"
#include "CUDATimer.h"
#include "GlobalDefines.h"
#include "SiftCameraParams.h"
//----------------------------------------------------------------
//Begin SiftGPU setting section.
// sift params
#define ORIENTATION_WINDOW_FACTOR 2.0f
#define ORINETATION_GAUSSIAN_FACTOR 1.5f
#define DESCRIPTOR_WINDOW_FACTOR 3.0f //descriptor sampling window factor
#define FILTER_WIDTH_FACTOR 4.0f //the filter size will be _FilterWidthFactor*sigma*2+1 (for pyramid building)
//////////////////////////////////////////////////////////
#define IMUL(X,Y) __mul24(X,Y)
//#define FDIV(X,Y) ((X)/(Y))
#define FDIV(X,Y) __fdividef(X,Y)
/////////////////////////////////////////////////////////
//filter kernel width range (don't change this)
#define KERNEL_MAX_WIDTH 33
#define KERNEL_MIN_WIDTH 5
//////////////////////////////////////////////////////////
//horizontal filter block size (32, 64, 128, 256, 512)
#define FILTERH_TILE_WIDTH 128
//thread block for vertical filter. FILTERV_BLOCK_WIDTH can be (4, 8 or 16)
#define FILTERV_BLOCK_WIDTH 16
#define FILTERV_BLOCK_HEIGHT 32
//The corresponding image patch for a thread block
#define FILTERV_PIXEL_PER_THREAD 4
#define FILTERV_TILE_WIDTH FILTERV_BLOCK_WIDTH
#define FILTERV_TILE_HEIGHT (FILTERV_PIXEL_PER_THREAD * FILTERV_BLOCK_HEIGHT)
//////////////////////////////////////////////////////////
//thread block size for computing Difference of Gaussian
#define DOG_BLOCK_LOG_DIMX 7
#define DOG_BLOCK_LOG_DIMY 0
#define DOG_BLOCK_DIMX (1 << DOG_BLOCK_LOG_DIMX)
#define DOG_BLOCK_DIMY (1 << DOG_BLOCK_LOG_DIMY)
//////////////////////////////////////////////////////////
//thread block size for keypoint detection
#define KEY_BLOCK_LOG_DIMX 3
#define KEY_BLOCK_LOG_DIMY 3
#define KEY_BLOCK_DIMX (1<<KEY_BLOCK_LOG_DIMX)
#define KEY_BLOCK_DIMY (1<<KEY_BLOCK_LOG_DIMY)
//#define KEY_OFFSET_ONE
//make KEY_BLOCK_LOG_DIMX 4 will make the write coalesced..
//but it seems uncoalesced writes don't affect the speed
//////////////////////////////////////////////////////////
//thread block size for initializing list generation (64, 128, 256, 512 ...)
#define HIST_INIT_WIDTH 128
//thread block size for generating feature list (32, 64, 128, 256, 512, ...)
#define LISTGEN_BLOCK_DIM 128
/////////////////////////////////////////////////////////
//how many keypoint orientations to compute in a block
#define ORIENTATION_COMPUTE_PER_BLOCK 64
//how many keypoint descriptor to compute in a block (2, 4, 8, 16, 32)
#define DESCRIPTOR_COMPUTE_PER_BLOCK 4
#define DESCRIPTOR_COMPUTE_BLOCK_SIZE (16 * DESCRIPTOR_COMPUTE_PER_BLOCK)
//how many keypoint descriptor to normalized in a block (32, ...)
#define DESCRIPTOR_NORMALIZ_PER_BLOCK 32
// MUST NOT BE CHANGED
#define COMPUTE_ORIENTATION_BLOCK 64
///////////////////////////////////////////
//Thread block size for visualization
//(This doesn't affect the speed of computation)
#define BLOCK_LOG_DIM 4
#define BLOCK_DIM (1 << BLOCK_LOG_DIM)
#define SIFT_NUM_KERNELS 30
//End SiftGPU setting section.
//----------------------------------------------------------------
extern __constant__ SiftCameraParams c_siftCameraParams;
__device__ __constant__ float d_kernel[SIFT_NUM_KERNELS][KERNEL_MAX_WIDTH];
texture<float, 1, cudaReadModeElementType> texData;
texture<unsigned char, 1, cudaReadModeNormalizedFloat> texDataB;
texture<float2, 2, cudaReadModeElementType> texDataF2;
texture<float4, 1, cudaReadModeElementType> texDataF4;
texture<int4, 1, cudaReadModeElementType> texDataI4;
texture<int4, 1, cudaReadModeElementType> texDataList;
//template<int i> __device__ float Conv(float *data) { return Conv<i-1>(data) + data[i]*d_kernel[i];}
//template<> __device__ float Conv<0>(float *data) { return data[0] * d_kernel[0]; }
//inline __device__ float bilinearInterpolationFloat(float x, float y, float* d_input, unsigned int imageWidth, unsigned int imageHeight)
//{
// const int2 p00 = make_int2(floor(x), floor(y));
// const int2 p01 = make_int2(p00.x + 0, p00.y + 1);
// const int2 p10 = make_int2(p00.x + 1, p00.y + 0);
// const int2 p11 = make_int2(p00.x + 1, p00.y + 1);
//
// const float alpha = x - p00.x;
// const float beta = y - p00.y;
//
// float s0 = 0.0f; float w0 = 0.0f;
// if(p00.x < imageWidth && p00.y < imageHeight) { float v00 = d_input[p00.y*imageWidth + p00.x]; if(v00 != MINF) { s0 += (1.0f-alpha)*v00; w0 += (1.0f-alpha); } }
// if(p10.x < imageWidth && p10.y < imageHeight) { float v10 = d_input[p10.y*imageWidth + p10.x]; if(v10 != MINF) { s0 += alpha *v10; w0 += alpha ; } }
//
// float s1 = 0.0f; float w1 = 0.0f;
// if(p01.x < imageWidth && p01.y < imageHeight) { float v01 = d_input[p01.y*imageWidth + p01.x]; if(v01 != MINF) { s1 += (1.0f-alpha)*v01; w1 += (1.0f-alpha);} }
// if(p11.x < imageWidth && p11.y < imageHeight) { float v11 = d_input[p11.y*imageWidth + p11.x]; if(v11 != MINF) { s1 += alpha *v11; w1 += alpha ;} }
//
// const float p0 = s0/w0;
// const float p1 = s1/w1;
//
// float ss = 0.0f; float ww = 0.0f;
// if(w0 > 0.0f) { ss += (1.0f-beta)*p0; ww += (1.0f-beta); }
// if(w1 > 0.0f) { ss += beta *p1; ww += beta ; }
//
// if(ww > 0.0f) return ss/ww;
// else return MINF;
//}
//////////////////////////////////////////////////////////////
template<int FW> __global__ void FilterH(float* d_result, int width, unsigned int filterIndex)
{
const int HALF_WIDTH = FW >> 1;
const int CACHE_WIDTH = FILTERH_TILE_WIDTH + FW - 1;
const int CACHE_COUNT = 2 + (CACHE_WIDTH - 2) / FILTERH_TILE_WIDTH;
__shared__ float data[CACHE_WIDTH];
const int bcol = IMUL(blockIdx.x, FILTERH_TILE_WIDTH);
const int col = bcol + threadIdx.x;
const int index_min = IMUL(blockIdx.y, width);
const int index_max = index_min + width - 1;
int src_index = index_min + bcol - HALF_WIDTH + threadIdx.x;
int cache_index = threadIdx.x;
float value = 0;
#pragma unroll
for (int j = 0; j < CACHE_COUNT; ++j)
{
if (cache_index < CACHE_WIDTH)
{
int fetch_index = src_index < index_min ? index_min : (src_index > index_max ? index_max : src_index);
data[cache_index] = tex1Dfetch(texData, fetch_index);
src_index += FILTERH_TILE_WIDTH;
cache_index += FILTERH_TILE_WIDTH;
}
}
__syncthreads();
if (col >= width) return;
#pragma unroll
for (int i = 0; i < FW; ++i)
{
value += (data[threadIdx.x + i] * d_kernel[filterIndex][i]);
}
// value = Conv<FW-1>(data + threadIdx.x);
d_result[index_min + col] = value;
}
////////////////////////////////////////////////////////////////////
template<int FW> __global__ void FilterV(float* d_result, int width, int height, unsigned int filterIndex)
{
const int HALF_WIDTH = FW >> 1;
const int CACHE_WIDTH = FW + FILTERV_TILE_HEIGHT - 1;
const int TEMP = CACHE_WIDTH & 0xf;
//add some extra space to avoid bank conflict
#if FILTERV_TILE_WIDTH == 16
//make the stride 16 * n +/- 1
const int EXTRA = (TEMP == 1 || TEMP == 0) ? 1 - TEMP : 15 - TEMP;
#elif FILTERV_TILE_WIDTH == 8
//make the stride 16 * n +/- 2
const int EXTRA = (TEMP == 2 || TEMP == 1 || TEMP == 0) ? 2 - TEMP : (TEMP == 15? 3 : 14 - TEMP);
#elif FILTERV_TILE_WIDTH == 4
//make the stride 16 * n +/- 4
const int EXTRA = (TEMP >=0 && TEMP <=4) ? 4 - TEMP : (TEMP > 12? 20 - TEMP : 12 - TEMP);
#else
#error
#endif
const int CACHE_TRUE_WIDTH = CACHE_WIDTH + EXTRA;
const int CACHE_COUNT = (CACHE_WIDTH + FILTERV_BLOCK_HEIGHT - 1) / FILTERV_BLOCK_HEIGHT;
const int WRITE_COUNT = (FILTERV_TILE_HEIGHT + FILTERV_BLOCK_HEIGHT - 1) / FILTERV_BLOCK_HEIGHT;
__shared__ float data[CACHE_TRUE_WIDTH * FILTERV_TILE_WIDTH];
const int row_block_first = IMUL(blockIdx.y, FILTERV_TILE_HEIGHT);
const int col = IMUL(blockIdx.x, FILTERV_TILE_WIDTH) + threadIdx.x;
const int row_first = row_block_first - HALF_WIDTH;
const int data_index_max = IMUL(height - 1, width) + col;
const int cache_col_start = threadIdx.y;
const int cache_row_start = IMUL(threadIdx.x, CACHE_TRUE_WIDTH);
int cache_index = cache_col_start + cache_row_start;
int data_index = IMUL(row_first + cache_col_start, width) + col;
if (col < width)
{
#pragma unroll
for (int i = 0; i < CACHE_COUNT; ++i)
{
if (cache_col_start < CACHE_WIDTH - i * FILTERV_BLOCK_HEIGHT)
{
int fetch_index = data_index < col ? col : (data_index > data_index_max ? data_index_max : data_index);
data[cache_index + i * FILTERV_BLOCK_HEIGHT] = tex1Dfetch(texData, fetch_index);
data_index += IMUL(FILTERV_BLOCK_HEIGHT, width);
}
}
}
__syncthreads();
if (col >= width) return;
int row = row_block_first + threadIdx.y;
int index_start = cache_row_start + threadIdx.y;
#pragma unroll
for (int i = 0; i < WRITE_COUNT; ++i,
row += FILTERV_BLOCK_HEIGHT, index_start += FILTERV_BLOCK_HEIGHT)
{
if (row < height)
{
int index_dest = IMUL(row, width) + col;
float value = 0;
#pragma unroll
for (int i = 0; i < FW; ++i)
{
value += (data[index_start + i] * d_kernel[filterIndex][i]);
}
d_result[index_dest] = value;
}
}
}
template<int LOG_SCALE> __global__ void UpsampleKernel(float* d_result, int width)
{
const int SCALE = (1 << LOG_SCALE), SCALE_MASK = (SCALE - 1);
const float INV_SCALE = 1.0f / (float(SCALE));
int col = IMUL(blockIdx.x, FILTERH_TILE_WIDTH) + threadIdx.x;
if (col >= width) return;
int row = blockIdx.y >> LOG_SCALE;
int index = row * width + col;
int dst_row = blockIdx.y;
int dst_idx = (width * dst_row + col) * SCALE;
int helper = blockIdx.y & SCALE_MASK;
if (helper)
{
float v11 = tex1Dfetch(texData, index);
float v12 = tex1Dfetch(texData, index + 1);
index += width;
float v21 = tex1Dfetch(texData, index);
float v22 = tex1Dfetch(texData, index + 1);
float w1 = INV_SCALE * helper, w2 = 1.0 - w1;
float v1 = (v21 * w1 + w2 * v11);
float v2 = (v22 * w1 + w2 * v12);
d_result[dst_idx] = v1;
#pragma unroll
for (int i = 1; i < SCALE; ++i)
{
const float r2 = i * INV_SCALE;
const float r1 = 1.0f - r2;
d_result[dst_idx + i] = v1 * r1 + v2 * r2;
}
}
else
{
float v1 = tex1Dfetch(texData, index);
float v2 = tex1Dfetch(texData, index + 1);
d_result[dst_idx] = v1;
#pragma unroll
for (int i = 1; i < SCALE; ++i)
{
const float r2 = i * INV_SCALE;
const float r1 = 1.0f - r2;
d_result[dst_idx + i] = v1 * r1 + v2 * r2;
}
}
}
////////////////////////////////////////////////////////////////////////////////////////
void ProgramCU::SampleImageU(CuTexImage *dst, CuTexImage *src, int log_scale)
{
int width = src->GetImgWidth(), height = src->GetImgHeight();
src->BindTexture(texData);
dim3 grid((width + FILTERH_TILE_WIDTH - 1) / FILTERH_TILE_WIDTH, height << log_scale);
dim3 block(FILTERH_TILE_WIDTH);
switch (log_scale)
{
case 1: UpsampleKernel<1> << < grid, block >> > ((float*)dst->_cuData, width); break;
case 2: UpsampleKernel<2> << < grid, block >> > ((float*)dst->_cuData, width); break;
case 3: UpsampleKernel<3> << < grid, block >> > ((float*)dst->_cuData, width); break;
default: break;
}
}
template<int LOG_SCALE> __global__ void DownsampleKernel(float* d_result, int src_width, int dst_width)
{
const int dst_col = IMUL(blockIdx.x, FILTERH_TILE_WIDTH) + threadIdx.x;
if (dst_col >= dst_width) return;
const int src_col = min((dst_col << LOG_SCALE), (src_width - 1));
const int dst_row = blockIdx.y;
const int src_row = blockIdx.y << LOG_SCALE;
const int src_idx = IMUL(src_row, src_width) + src_col;
const int dst_idx = IMUL(dst_width, dst_row) + dst_col;
d_result[dst_idx] = tex1Dfetch(texData, src_idx);
}
__global__ void DownsampleKernel(float* d_result, int src_width, int dst_width, const int log_scale)
{
const int dst_col = IMUL(blockIdx.x, FILTERH_TILE_WIDTH) + threadIdx.x;
if (dst_col >= dst_width) return;
const int src_col = min((dst_col << log_scale), (src_width - 1));
const int dst_row = blockIdx.y;
const int src_row = blockIdx.y << log_scale;
const int src_idx = IMUL(src_row, src_width) + src_col;
const int dst_idx = IMUL(dst_width, dst_row) + dst_col;
d_result[dst_idx] = tex1Dfetch(texData, src_idx);
}
void ProgramCU::SampleImageD(CuTexImage *dst, CuTexImage *src, int log_scale)
{
int src_width = src->GetImgWidth(), dst_width = dst->GetImgWidth();
src->BindTexture(texData);
dim3 grid((dst_width + FILTERH_TILE_WIDTH - 1) / FILTERH_TILE_WIDTH, dst->GetImgHeight());
dim3 block(FILTERH_TILE_WIDTH);
switch (log_scale)
{
case 1: DownsampleKernel<1> << < grid, block >> > ((float*)dst->_cuData, src_width, dst_width); break;
case 2: DownsampleKernel<2> << < grid, block >> > ((float*)dst->_cuData, src_width, dst_width); break;
case 3: DownsampleKernel<3> << < grid, block >> > ((float*)dst->_cuData, src_width, dst_width); break;
default: DownsampleKernel << < grid, block >> > ((float*)dst->_cuData, src_width, dst_width, log_scale);
}
}
__global__ void ChannelReduce_Kernel(float* d_result)
{
int index = IMUL(blockIdx.x, FILTERH_TILE_WIDTH) + threadIdx.x;
d_result[index] = tex1Dfetch(texData, index * 4);
}
__global__ void ChannelReduce_Convert_Kernel(float* d_result)
{
int index = IMUL(blockIdx.x, FILTERH_TILE_WIDTH) + threadIdx.x;
float4 rgba = tex1Dfetch(texDataF4, index);
d_result[index] = 0.299f * rgba.x + 0.587f* rgba.y + 0.114f * rgba.z;
}
void ProgramCU::ReduceToSingleChannel(CuTexImage* dst, CuTexImage* src, int convert_rgb)
{
int width = src->GetImgWidth(), height = dst->GetImgHeight();
dim3 grid((width * height + FILTERH_TILE_WIDTH - 1) / FILTERH_TILE_WIDTH);
dim3 block(FILTERH_TILE_WIDTH);
if (convert_rgb)
{
src->BindTexture(texDataF4);
ChannelReduce_Convert_Kernel << <grid, block >> >((float*)dst->_cuData);
}
else
{
src->BindTexture(texData);
ChannelReduce_Kernel << <grid, block >> >((float*)dst->_cuData);
}
}
__global__ void ConvertByteToFloat_Kernel(float* d_result)
{
int index = IMUL(blockIdx.x, FILTERH_TILE_WIDTH) + threadIdx.x;
d_result[index] = tex1Dfetch(texDataB, index);
}
void ProgramCU::ConvertByteToFloat(CuTexImage*src, CuTexImage* dst)
{
int width = src->GetImgWidth(), height = dst->GetImgHeight();
dim3 grid((width * height + FILTERH_TILE_WIDTH - 1) / FILTERH_TILE_WIDTH);
dim3 block(FILTERH_TILE_WIDTH);
src->BindTexture(texDataB);
ConvertByteToFloat_Kernel << <grid, block >> >((float*)dst->_cuData);
}
void ProgramCU::InitFilterKernels(const std::vector<float>& sigmas, std::vector<unsigned int>& filterWidths)
{
filterWidths.resize(sigmas.size());
float kernel[KERNEL_MAX_WIDTH];
for (unsigned int i = 0; i < sigmas.size(); i++) {
int width;
memset(kernel, 0, sizeof(float) * KERNEL_MAX_WIDTH);
CreateFilterKernel(sigmas[i], kernel, width);
cudaMemcpyToSymbol(d_kernel, kernel, KERNEL_MAX_WIDTH * sizeof(float), i * KERNEL_MAX_WIDTH * sizeof(float));
filterWidths[i] = width;
}
}
void ProgramCU::CreateFilterKernel(float sigma, float* kernel, int& width)
{
int i, sz = int(ceil(FILTER_WIDTH_FACTOR * sigma - 0.5));//
width = 2 * sz + 1;
if (width > KERNEL_MAX_WIDTH)
{
//filter size truncation
sz = KERNEL_MAX_WIDTH >> 1;
width = KERNEL_MAX_WIDTH;
}
else if (width < KERNEL_MIN_WIDTH)
{
sz = KERNEL_MIN_WIDTH >> 1;
width = KERNEL_MIN_WIDTH;
}
float rv = 1.0f / (sigma*sigma), v, ksum = 0;
// pre-compute filter
for (i = -sz; i <= sz; ++i)
{
kernel[i + sz] = v = exp(-0.5f * i * i *rv);
ksum += v;
}
//normalize the kernel
rv = 1.0f / ksum;
for (i = 0; i < width; i++) kernel[i] *= rv;
}
template<int FW> void ProgramCU::FilterImage(CuTexImage *dst, CuTexImage *src, CuTexImage* buf, unsigned int filterIndex)
{
int width = src->GetImgWidth(), height = src->GetImgHeight();
//horizontal filtering
src->BindTexture(texData);
dim3 gridh((width + FILTERH_TILE_WIDTH - 1) / FILTERH_TILE_WIDTH, height);
dim3 blockh(FILTERH_TILE_WIDTH);
FilterH<FW> << <gridh, blockh >> >((float*)buf->_cuData, width, filterIndex);
CheckErrorCUDA("FilterH");
///vertical filtering
buf->BindTexture(texData);
dim3 gridv((width + FILTERV_TILE_WIDTH - 1) / FILTERV_TILE_WIDTH, (height + FILTERV_TILE_HEIGHT - 1) / FILTERV_TILE_HEIGHT);
dim3 blockv(FILTERV_TILE_WIDTH, FILTERV_BLOCK_HEIGHT);
FilterV<FW> << <gridv, blockv >> >((float*)dst->_cuData, width, height, filterIndex);
CheckErrorCUDA("FilterV");
}
//////////////////////////////////////////////////////////////////////
// tested on 2048x1500 image, the time on pyramid construction is
// OpenGL version : 18ms
// CUDA version: 28 ms
void ProgramCU::FilterImage(CuTexImage *dst, CuTexImage *src, CuTexImage* buf, unsigned int width, unsigned int filterIndex)
{
//CUDATimer timer;
//timer.startEvent("FilterImage");
switch (width)
{
case 5: FilterImage< 5>(dst, src, buf, filterIndex); break;
case 7: FilterImage< 7>(dst, src, buf, filterIndex); break;
case 9: FilterImage< 9>(dst, src, buf, filterIndex); break;
case 11: FilterImage<11>(dst, src, buf, filterIndex); break;
case 13: FilterImage<13>(dst, src, buf, filterIndex); break;
case 15: FilterImage<15>(dst, src, buf, filterIndex); break;
case 17: FilterImage<17>(dst, src, buf, filterIndex); break;
case 19: FilterImage<19>(dst, src, buf, filterIndex); break;
case 21: FilterImage<21>(dst, src, buf, filterIndex); break;
case 23: FilterImage<23>(dst, src, buf, filterIndex); break;
case 25: FilterImage<25>(dst, src, buf, filterIndex); break;
case 27: FilterImage<27>(dst, src, buf, filterIndex); break;
case 29: FilterImage<29>(dst, src, buf, filterIndex); break;
case 31: FilterImage<31>(dst, src, buf, filterIndex); break;
case 33: FilterImage<33>(dst, src, buf, filterIndex); break;
default: break;
}
//timer.endEvent();
//if (src->GetImgWidth() == 1296 && width == 25) timer.evaluate();
}
//void ProgramCU::FilterImage(CuTexImage *dst, CuTexImage *src, CuTexImage* buf, float sigma)
//{
// CUDATimer timer;
// timer.startEvent("FilterImage");
//
// float filter_kernel[KERNEL_MAX_WIDTH]; int width;
// CreateFilterKernel(sigma, filter_kernel, width);
// cudaMemcpyToSymbol(d_kernel, filter_kernel, width * sizeof(float), 0, cudaMemcpyHostToDevice);
//
// switch (width)
// {
// case 5: FilterImage< 5>(dst, src, buf); break;
// case 7: FilterImage< 7>(dst, src, buf); break;
// case 9: FilterImage< 9>(dst, src, buf); break;
// case 11: FilterImage<11>(dst, src, buf); break;
// case 13: FilterImage<13>(dst, src, buf); break;
// case 15: FilterImage<15>(dst, src, buf); break;
// case 17: FilterImage<17>(dst, src, buf); break;
// case 19: FilterImage<19>(dst, src, buf); break;
// case 21: FilterImage<21>(dst, src, buf); break;
// case 23: FilterImage<23>(dst, src, buf); break;
// case 25: FilterImage<25>(dst, src, buf); break;
// case 27: FilterImage<27>(dst, src, buf); break;
// case 29: FilterImage<29>(dst, src, buf); break;
// case 31: FilterImage<31>(dst, src, buf); break;
// case 33: FilterImage<33>(dst, src, buf); break;
// default: break;
// }
// timer.endEvent();
// if (src->GetImgWidth() == 1296 && width == 25) timer.evaluate();
//}
texture<float, 1, cudaReadModeElementType> texC;
texture<float, 1, cudaReadModeElementType> texP;
texture<float, 1, cudaReadModeElementType> texN;
void __global__ ComputeDOG_Kernel(float* d_dog, float2* d_got, int width, int height)
{
int row = (blockIdx.y << DOG_BLOCK_LOG_DIMY) + threadIdx.y;
int col = (blockIdx.x << DOG_BLOCK_LOG_DIMX) + threadIdx.x;
if (col < width && row < height)
{
int index = IMUL(row, width) + col;
float vp = tex1Dfetch(texP, index);
float v = tex1Dfetch(texC, index);
d_dog[index] = v - vp;
float vxn = tex1Dfetch(texC, index + 1);
float vxp = tex1Dfetch(texC, index - 1);
float vyp = tex1Dfetch(texC, index - width);
float vyn = tex1Dfetch(texC, index + width);
float dx = vxn - vxp, dy = vyn - vyp;
float grd = 0.5f * sqrt(dx * dx + dy * dy);
float rot = (grd == 0.0f ? 0.0f : atan2(dy, dx));
d_got[index] = make_float2(grd, rot);
}
}
void __global__ ComputeDOG_Kernel(float* d_dog, int width, int height)
{
int row = (blockIdx.y << DOG_BLOCK_LOG_DIMY) + threadIdx.y;
int col = (blockIdx.x << DOG_BLOCK_LOG_DIMX) + threadIdx.x;
if (col < width && row < height)
{
int index = IMUL(row, width) + col;
float vp = tex1Dfetch(texP, index);
float v = tex1Dfetch(texC, index);
d_dog[index] = v - vp;
}
}
void ProgramCU::ComputeDOG(CuTexImage* gus, CuTexImage* dog, CuTexImage* got)
{
int width = gus->GetImgWidth(), height = gus->GetImgHeight();
dim3 grid((width + DOG_BLOCK_DIMX - 1) / DOG_BLOCK_DIMX, (height + DOG_BLOCK_DIMY - 1) / DOG_BLOCK_DIMY);
dim3 block(DOG_BLOCK_DIMX, DOG_BLOCK_DIMY);
gus->BindTexture(texC);
(gus - 1)->BindTexture(texP);
if (got->_cuData)
ComputeDOG_Kernel << <grid, block >> >((float*)dog->_cuData, (float2*)got->_cuData, width, height);
else
ComputeDOG_Kernel << <grid, block >> >((float*)dog->_cuData, width, height);
}
#define READ_CMP_DOG_DATA(datai, tex, idx) \
datai[0] = tex1Dfetch(tex, idx - 1);\
datai[1] = tex1Dfetch(tex, idx);\
datai[2] = tex1Dfetch(tex, idx + 1);\
if(v > nmax)\
{\
nmax = max(nmax, datai[0]);\
nmax = max(nmax, datai[1]);\
nmax = max(nmax, datai[2]);\
if(v < nmax) return;\
}else\
{\
nmin = min(nmin, datai[0]);\
nmin = min(nmin, datai[1]);\
nmin = min(nmin, datai[2]);\
if(v > nmin) return;\
}
void __global__ ComputeKEY_Kernel(float4* d_key, int width, int colmax, int rowmax,
float dog_threshold0, float dog_threshold, float edge_threshold, int subpixel_localization,
int4* d_featureList, int* d_featureCount, unsigned int featureOctLevelidx, float keyLocScale, float keyLocOffset, const float* d_depthData, float siftDepthMin, float siftDepthMax
, unsigned int maxNumFeatures
)
{
const unsigned int depthWidth = c_siftCameraParams.m_depthWidth;
const unsigned int depthHeight = c_siftCameraParams.m_depthHeight;
float data[3][3], v;
float datap[3][3], datan[3][3];
#ifdef KEY_OFFSET_ONE
int row = (blockIdx.y << KEY_BLOCK_LOG_DIMY) + threadIdx.y + 1;
int col = (blockIdx.x << KEY_BLOCK_LOG_DIMX) + threadIdx.x + 1;
#else
int row = (blockIdx.y << KEY_BLOCK_LOG_DIMY) + threadIdx.y;
int col = (blockIdx.x << KEY_BLOCK_LOG_DIMX) + threadIdx.x;
#endif
int index = IMUL(row, width) + col;
int idx[3] = { index - width, index, index + width };
float nmax, nmin, result = 0.0f;
float dx = 0, dy = 0, ds = 0;
bool offset_test_passed = true;
#ifdef KEY_OFFSET_ONE
if(row < rowmax && col < colmax)
#else
if (row > 0 && col > 0 && row < rowmax-1 && col < colmax-1)
#endif
{
d_key[index] = make_float4(result, dx, dy, ds);
// check if has valid depth
int depthx = round((keyLocScale * (float)col + keyLocOffset) * (float)(depthWidth-1) / (float)(c_siftCameraParams.m_intensityWidth-1));
int depthy = round((keyLocScale * (float)row + keyLocOffset) * (float)(depthHeight-1) / (float)(c_siftCameraParams.m_intensityHeight-1));
if (depthx < 0 || depthx >= depthWidth || depthy < 0 || depthy >= depthHeight) return;
//float depth = bilinearInterpolationFloat(depthx, depthy, d_depthData, 640, 480);
float depth = d_depthData[depthy * depthWidth + depthx];
if (depth == MINF || depth < siftDepthMin || depth > siftDepthMax) return;
data[1][1] = v = tex1Dfetch(texC, idx[1]);
if (fabs(v) <= dog_threshold0) return; // if pixel value less than dog thresh
data[1][0] = tex1Dfetch(texC, idx[1] - 1); // current(row, col-1)
data[1][2] = tex1Dfetch(texC, idx[1] + 1); // current(row, col+1)
nmax = max(data[1][0], data[1][2]);
nmin = min(data[1][0], data[1][2]);
if (v <= nmax && v >= nmin) return; // not a min or a max already
READ_CMP_DOG_DATA(data[0], texC, idx[0]); // current (row-1, col-1) (row-1, col) (row-1, col+1)
READ_CMP_DOG_DATA(data[2], texC, idx[2]); // current (row+1, col-1) (row+1, col) (row+1, col+1)
//edge supression
float vx2 = v * 2.0f;
float fxx = data[1][0] + data[1][2] - vx2;
float fyy = data[0][1] + data[2][1] - vx2;
float fxy = 0.25f * (data[2][2] + data[0][0] - data[2][0] - data[0][2]);
float temp1 = fxx * fyy - fxy * fxy;
float temp2 = (fxx + fyy) * (fxx + fyy);
if (temp1 <= 0 || temp2 > edge_threshold * temp1) return;
//read the previous level
READ_CMP_DOG_DATA(datap[0], texP, idx[0]);
READ_CMP_DOG_DATA(datap[1], texP, idx[1]);
READ_CMP_DOG_DATA(datap[2], texP, idx[2]);
//read the next level
READ_CMP_DOG_DATA(datan[0], texN, idx[0]);
READ_CMP_DOG_DATA(datan[1], texN, idx[1]);
READ_CMP_DOG_DATA(datan[2], texN, idx[2]);
if (subpixel_localization)
{
printf("ERROR should not get to subpixel localization\n");
//subpixel localization
float fx = 0.5f * (data[1][2] - data[1][0]);
float fy = 0.5f * (data[2][1] - data[0][1]);
float fs = 0.5f * (datan[1][1] - datap[1][1]);
float fss = (datan[1][1] + datap[1][1] - vx2);
float fxs = 0.25f* (datan[1][2] + datap[1][0] - datan[1][0] - datap[1][2]);
float fys = 0.25f* (datan[2][1] + datap[0][1] - datan[0][1] - datap[2][1]);
//need to solve dx, dy, ds;
// |-fx| | fxx fxy fxs | |dx|
// |-fy| = | fxy fyy fys | * |dy|
// |-fs| | fxs fys fss | |ds|
float4 A0 = fxx > 0 ? make_float4(fxx, fxy, fxs, -fx) : make_float4(-fxx, -fxy, -fxs, fx);
float4 A1 = fxy > 0 ? make_float4(fxy, fyy, fys, -fy) : make_float4(-fxy, -fyy, -fys, fy);
float4 A2 = fxs > 0 ? make_float4(fxs, fys, fss, -fs) : make_float4(-fxs, -fys, -fss, fs);
float maxa = max(max(A0.x, A1.x), A2.x);
if (maxa >= 1e-10)
{
if (maxa == A1.x)
{
float4 TEMP = A1; A1 = A0; A0 = TEMP;
}
else if (maxa == A2.x)
{
float4 TEMP = A2; A2 = A0; A0 = TEMP;
}
A0.y /= A0.x; A0.z /= A0.x; A0.w /= A0.x;
A1.y -= A1.x * A0.y; A1.z -= A1.x * A0.z; A1.w -= A1.x * A0.w;
A2.y -= A2.x * A0.y; A2.z -= A2.x * A0.z; A2.w -= A2.x * A0.w;
if (abs(A2.y) > abs(A1.y))
{
float4 TEMP = A2; A2 = A1; A1 = TEMP;
}
if (abs(A1.y) >= 1e-10)
{
A1.z /= A1.y; A1.w /= A1.y;
A2.z -= A2.y * A1.z; A2.w -= A2.y * A1.w;
if (abs(A2.z) >= 1e-10)
{
ds = A2.w / A2.z;
dy = A1.w - ds * A1.z;
dx = A0.w - ds * A0.z - dy * A0.y;
offset_test_passed =
fabs(data[1][1] + 0.5f * (dx * fx + dy * fy + ds * fs)) > dog_threshold
&&fabs(ds) < 1.0f && fabs(dx) < 1.0f && fabs(dy) < 1.0f;
}
}
}
}
if (offset_test_passed) {
result = v > nmax ? 1.0f : -1.0f;
int addr = atomicAdd(d_featureCount + featureOctLevelidx, 1);
if (addr < maxNumFeatures) {
d_featureList[addr] = make_int4(col, row, 0, 0);
d_key[index] = make_float4(result, dx, dy, ds);
}
}
}
}
//给定DoG金字塔,计算关键点位置, 这里用到了深度,但是只是简单的过滤而已
//void ProgramCU::ComputeKEY(CuTexImage* dog, CuTexImage* key, float Tdog, float Tedge)
void ProgramCU::ComputeKEY(CuTexImage* dog, CuTexImage* key, float Tdog, float Tedge, CuTexImage* featureList, int* d_featureCount, unsigned int featureOctLevelidx,
float keyLocScale, float keyLocOffset, const float* d_depthData, float siftDepthMin, float siftDepthMax)
{
int width = dog->GetImgWidth(), height = dog->GetImgHeight(); //2048-1536
float Tdog1 = (GlobalUtil::_SubpixelLocalization ? 0.8f : 1.0f) * Tdog; //Tdog1=0.006667, supixellocal=0
CuTexImage* dogp = dog - 1;
CuTexImage* dogn = dog + 1;
#ifdef KEY_OFFSET_ONE
dim3 grid((width - 1 + KEY_BLOCK_DIMX - 1) / KEY_BLOCK_DIMX, (height - 1 + KEY_BLOCK_DIMY - 1) / KEY_BLOCK_DIMY);
#else
dim3 grid((width + KEY_BLOCK_DIMX - 1) / KEY_BLOCK_DIMX, (height + KEY_BLOCK_DIMY - 1) / KEY_BLOCK_DIMY); //this way, 256*192
#endif
dim3 block(KEY_BLOCK_DIMX, KEY_BLOCK_DIMY); //8*8
dogp->BindTexture(texP);
dog->BindTexture(texC);
dogn->BindTexture(texN);
Tedge = (Tedge + 1)*(Tedge + 1) / Tedge; //12.1
ComputeKEY_Kernel << <grid, block >> >((float4*)key->_cuData, width,
width - 1, height - 1, Tdog1, Tdog, Tedge, GlobalUtil::_SubpixelLocalization,
(int4*)featureList->_cuData, d_featureCount, featureOctLevelidx,
keyLocScale, keyLocOffset, d_depthData, siftDepthMin, siftDepthMax
, featureList->GetImgWidth()* featureList->GetImgHeight() //4096*1
);
ProgramCU::CheckErrorCUDA("ComputeKEY");
}
void __global__ InitHist_Kernel(int4* hist, int ws, int wd, int height)
{
int row = IMUL(blockIdx.y, blockDim.y) + threadIdx.y;
int col = IMUL(blockIdx.x, blockDim.x) + threadIdx.x;
if (row < height && col < wd)
{
int hidx = IMUL(row, wd) + col; // (row, col)
int v[4] = { 0, 0, 0, 0 };
if (row > 0 && row < height - 1)
{
int scol = col << 2;
int sidx = IMUL(row, ws) + scol; // (row, col/4)
#pragma unroll
for (int i = 0; i < 4; ++i, ++scol)
{
float4 temp = tex1Dfetch(texDataF4, sidx + i);
v[i] = (scol < ws - 1 && scol > 0 && temp.x != 0) ? 1 : 0;
}
}
hist[hidx] = make_int4(v[0], v[1], v[2], v[3]); // 1 or 0 if (row, col/4 + [0,3]) has key response
}
}
void ProgramCU::InitHistogram(CuTexImage* key, CuTexImage* hist)
{
int ws = key->GetImgWidth(), hs = key->GetImgHeight();
int wd = hist->GetImgWidth(), hd = hist->GetImgHeight();
dim3 grid((wd + HIST_INIT_WIDTH - 1) / HIST_INIT_WIDTH, hd);
dim3 block(HIST_INIT_WIDTH, 1);
key->BindTexture(texDataF4);
InitHist_Kernel << <grid, block >> >((int4*)hist->_cuData, ws, wd, hd);
}
void __global__ ReduceHist_Kernel(int4* d_hist, int ws, int wd, int height)
{
int row = IMUL(blockIdx.y, blockDim.y) + threadIdx.y;
int col = IMUL(blockIdx.x, blockDim.x) + threadIdx.x;
if (row < height && col < wd)
{
int hidx = IMUL(row, wd) + col;
int scol = col << 2;
int sidx = IMUL(row, ws) + scol;
int v[4] = { 0, 0, 0, 0 };
#pragma unroll
for (int i = 0; i < 4 && scol < ws; ++i, ++scol)
{
int4 temp = tex1Dfetch(texDataI4, sidx + i);
v[i] = temp.x + temp.y + temp.z + temp.w;
}
d_hist[hidx] = make_int4(v[0], v[1], v[2], v[3]);
}
}
void ProgramCU::ReduceHistogram(CuTexImage*hist1, CuTexImage* hist2)
{
int ws = hist1->GetImgWidth(), hs = hist1->GetImgHeight();
int wd = hist2->GetImgWidth(), hd = hist2->GetImgHeight();
int temp = (int)floor(logf(float(wd * 2 / 3)) / logf(2.0f));
const int wi = min(7, max(temp, 0));
hist1->BindTexture(texDataI4);
const int BW = 1 << wi, BH = 1 << (7 - wi);
dim3 grid((wd + BW - 1) / BW, (hd + BH - 1) / BH);
dim3 block(BW, BH);
ReduceHist_Kernel << <grid, block >> >((int4*)hist2->_cuData, ws, wd, hd);
}
void __global__ ListGen_Kernel(int4* d_list, int width)
{
int idx1 = IMUL(blockIdx.x, blockDim.x) + threadIdx.x; // list index
int4 pos = tex1Dfetch(texDataList, idx1);
int idx2 = IMUL(pos.y, width) + pos.x; // hist index
int4 temp = tex1Dfetch(texDataI4, idx2);
int sum1 = temp.x + temp.y;
int sum2 = sum1 + temp.z;
pos.x <<= 2;
if (pos.z >= sum2)
{
pos.x += 3;
pos.z -= sum2;
}
else if (pos.z >= sum1)
{
pos.x += 2;
pos.z -= sum1;
}
else if (pos.z >= temp.x)
{
pos.x += 1;
pos.z -= temp.x;
}
d_list[idx1] = pos;
}
//input list (x, y) (x, y) ....
void ProgramCU::GenerateList(CuTexImage* list, CuTexImage* hist)
{
int len = list->GetImgWidth();
list->BindTexture(texDataList);
hist->BindTexture(texDataI4);
dim3 grid((len + LISTGEN_BLOCK_DIM - 1) / LISTGEN_BLOCK_DIM);
dim3 block(LISTGEN_BLOCK_DIM);
ListGen_Kernel << <grid, block >> >((int4*)list->_cuData, hist->GetImgWidth());
}
//const unsigned int warpSize = 32;
__inline__ __device__
float warpReduceMax(float val) {
for (int offset = 32 / 2; offset > 0; offset /= 2) {
val = max(val, __shfl_down(val, offset, 32));
}
return val;
}
void __global__ ComputeOrientation_Kernel(float4* d_list,
int list_len,
int width, int height,
float sigma, float sigma_step,
int num_orientation,
int subpixel)
{
unsigned int tidx = threadIdx.x;
const float ten_degree_per_radius = 5.7295779513082320876798154814105;
//const float radius_per_ten_degrees = 1.0 / 5.7295779513082320876798154814105;
//int idx = IMUL(blockDim.x, blockIdx.x);
int idx = blockIdx.x;
if (idx >= list_len) return;
float4 key;
int4 ikey = tex1Dfetch(texDataList, idx);
key.x = ikey.x + 0.5f;
key.y = ikey.y + 0.5f;
key.z = sigma;
if (subpixel)
{
float4 offset = tex1Dfetch(texDataF4, IMUL(width, ikey.y) + ikey.x);
if (subpixel)
{
key.x += offset.y;
key.y += offset.z;
key.z *= pow(sigma_step, offset.w);
}
}
if (num_orientation == 0)
{
key.w = 0;
d_list[idx] = key;
return;
}
float gsigma = key.z * ORINETATION_GAUSSIAN_FACTOR;
float win = fabs(key.z) * ORINETATION_GAUSSIAN_FACTOR * ORIENTATION_WINDOW_FACTOR;
float dist_threshold = win * win + 0.5;
float factor = -0.5f / (gsigma * gsigma);
float xmin = max(1.5f, floor(key.x - win) + 0.5f);
float ymin = max(1.5f, floor(key.y - win) + 0.5f);
float xmax = min(width - 1.5f, floor(key.x + win) + 0.5f);
float ymax = min(height - 1.5f, floor(key.y + win) + 0.5f);
__shared__ float vote[36];
if (tidx < 36) vote[tidx] = 0.0f;
__syncthreads();
unsigned int xlen = (unsigned int)round(xmax - xmin + 1);
unsigned int ylen = (unsigned int)round(ymax - ymin + 1);
unsigned int num = ylen * xlen;
for (unsigned int i = 0; i < num; i += COMPUTE_ORIENTATION_BLOCK) {
if (i + tidx < num) {
float x = ((i + tidx) % xlen) + xmin;
float y = ((i + tidx) / xlen) + ymin;
float dx = x - key.x;
float dy = y - key.y;
float sq_dist = dx * dx + dy * dy;
if (sq_dist < dist_threshold) {
float2 got = tex2D(texDataF2, x, y);
float weight = got.x * exp(sq_dist * factor);
float fidx = floor(got.y * ten_degree_per_radius);
int oidx = fidx;
if (oidx < 0) oidx += 36;
atomicAdd(&vote[oidx], weight);
}
}
}
__syncthreads();
//filter the vote
const float one_third = 1.0 / 3.0;
__shared__ float vote_tmp[36];
if (tidx < 36) {
volatile float* source = vote;
volatile float* target = vote_tmp;
#pragma unroll
for (int i = 0; i < 6; i++) {
const unsigned int m = (tidx + 36 - 1) % 36;
const unsigned int c = (tidx);
const unsigned int p = (tidx + 1) % 36;
target[tidx] = (source[m] + source[c] + source[p])*one_third;
__syncthreads();
volatile float *tmp = source;
source = target;
target = tmp;
}
}
// if (num_orientation == 1)
// {
// int index_max = 0;
// float max_vote = vote[0];
//#pragma unroll
// for (int i = 1; i < 36; ++i)
// {
// index_max = vote[i] > max_vote ? i : index_max;
// max_vote = max(max_vote, vote[i]);
// }
// float pre = vote[index_max == 0 ? 35 : index_max - 1];
// float next = vote[index_max + 1];
// float weight = max_vote;
// float off = 0.5f * FDIV(next - pre, weight + weight - next - pre);
// key.w = radius_per_ten_degrees * (index_max + 0.5f + off);
// d_list[idx] = key;
// }
// else
float max_vote = 0.0f;
if (tidx < 36) max_vote = vote[tidx];
//__syncthreads(); <- not quite sure if we need that ( probably not )