forked from Yanghai717/InSAR-on-Sentinel-1-TOPS-data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EstCoh.cu
957 lines (562 loc) · 21.8 KB
/
EstCoh.cu
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
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#define __CUDA_INTERNAL_COMPILATION__
#include <math_functions.h>
#include <math_constants.h>
#include <device_functions.h>
#include <cuComplex.h>
//#include <helper_cuda.h>
#include "cuConstants.cuh"
#undef __CUDA_INTERNAL_COMPILATION__
#include <math.h>
#include <iostream>
#include<omp.h>
#include <complex>
using namespace std;
#define THREADBLOCK_SIZE 256
int MultipleOf8(int inputLines)
{
int remains = inputLines % 8;
int multiple = inputLines / 8;
if (remains == 0)
return inputLines;
else{
return (multiple + 1) * 8;
}
}
int MultipleOf32(int inputLines)
{
int remains = inputLines % 32;
int multiple = inputLines / 32;
if (remains == 0)
return inputLines;
else{
return (multiple + 1) * 32;
}
}
static int iDivUp(int dividend, int divisor)
{
return ((dividend % divisor) == 0) ? (dividend / divisor) : (dividend / divisor + 1);
}
__global__ void CohMatSet(cuComplex *DstMaster, cuComplex *DstSlave, cuComplex *SrcMaster,
cuComplex *SrcSlave, int SrcLines, int SrcPixels,
size_t DstPitch, size_t SrcPitch, int dx, int dy)
{
const int row = blockIdx.y*blockDim.y + threadIdx.y;
const int col = blockIdx.x*blockDim.x + threadIdx.x;
if (row < SrcLines&&col < SrcPixels)
{
cuComplex* rowDstMaster = (cuComplex *)((char*)DstMaster + (row + dy)*DstPitch);
cuComplex* rowDstSlave = (cuComplex *)((char*)DstSlave + (row + dy)*DstPitch);
cuComplex* rowSrcMaster = (cuComplex *)((char*)SrcMaster + row*SrcPitch);
cuComplex* rowSrcSlave = (cuComplex *)((char*)SrcSlave + row*SrcPitch);
cuComplex MasterTemp = rowSrcMaster[col];
cuComplex SlaveTemp = rowSrcSlave[col];
rowDstMaster[col + dx] = cuCmulf(MasterTemp, cuConjf(SlaveTemp));
rowDstSlave[col + dx] =
make_cuComplex(SlaveTemp.x*SlaveTemp.x + SlaveTemp.y*SlaveTemp.y,
MasterTemp.x*MasterTemp.x + MasterTemp.y*MasterTemp.y);
}
}
__global__ void CohMatSet
(cuComplex *DstMaster, cuComplex *DstSlave, short2 *SrcMaster,
cuComplex *SrcSlave, int SrcLines, int SrcPixels,
size_t DstPitch, size_t SrcSlavePitch,size_t SrcMasterPitch, int dx, int dy)
{
const int row = blockIdx.y*blockDim.y + threadIdx.y;
const int col = blockIdx.x*blockDim.x + threadIdx.x;
if (row < SrcLines&&col < SrcPixels)
{
cuComplex* rowDstMaster = (cuComplex *)((char*)DstMaster + (row + dy)*DstPitch);
cuComplex* rowDstSlave = (cuComplex *)((char*)DstSlave + (row + dy)*DstPitch);
short2* rowSrcMaster = (short2 *)((char*)SrcMaster + row*SrcMasterPitch);
cuComplex* rowSrcSlave = (cuComplex *)((char*)SrcSlave + row*SrcSlavePitch);
cuComplex MasterTemp = make_cuComplex(rowSrcMaster[col].x, rowSrcMaster[col].y);
cuComplex SlaveTemp = rowSrcSlave[col];
rowDstMaster[col + dx] = cuCmulf(MasterTemp, cuConjf(SlaveTemp));;
rowDstSlave[col + dx] = make_cuComplex(SlaveTemp.x*SlaveTemp.x + SlaveTemp.y*SlaveTemp.y,
MasterTemp.x*MasterTemp.x + MasterTemp.y*MasterTemp.y);
}
}
__global__ void CuCplCoherence_vertical(cuComplex *Sum1Array, cuComplex* Power1Array,
int inputLines, int inputPixels, size_t DstPitch, int LinesOffset)
{
const int row = blockIdx.y*blockDim.y + threadIdx.y;
const int col = blockIdx.x*blockDim.x + threadIdx.x;
if (row < inputLines&&col < inputPixels)
{
cuComplex sum1 = make_cuComplex(0.0f, 0.0f);
cuComplex power1 = make_cuComplex(0.0f, 0.0f);
int Roffset = LinesOffset;
for (int i = 0; i < c_winX; i++)
{
sum1 = cuCaddf(sum1, cuCsubf(tex2D(tex_input, col + i, row + Roffset + c_winY),
tex2D(tex_input, col + i, row + Roffset)));
}
Sum1Array[(row + 1)*(DstPitch / 8) + col] = sum1;
for (int i = 0; i < c_winX; i++)
{
power1 = cuCaddf(power1, cuCsubf(tex2D(tex_norms, col + i, row + Roffset + c_winY),
tex2D(tex_norms, col + i, row + Roffset)));
}
Power1Array[(row + 1)*(DstPitch / 8) + col] = power1;
}
}
__global__ void CuCplCoherence_basic(float *Result, cuComplex* JudgeArray, int inputLines, int inputPixels, size_t DstPitch, size_t judgePitch)
{
const int row = blockIdx.y*blockDim.y + threadIdx.y;
const int col = blockIdx.x*blockDim.x + threadIdx.x;
if (row < inputLines&&col < inputPixels)
{
int winY = c_winY;
int winX = c_winX;
cuComplex sum1 = make_cuComplex(0.0f, 0.0f);
cuComplex power1 = make_cuComplex(0.0f, 0.0f);
for (int j = 0; j < winY; j++)
{
for (int i = col; i < col + winX; i++)
{
sum1 = cuCaddf(sum1, tex2D(tex_input, i, row + j));
power1 = cuCaddf(power1, tex2D(tex_norms, i, row + j));
}
}
float p = power1.x*power1.y;
float* rowResult = (float *)((char*)Result + row*DstPitch);
rowResult[col] = (p > 0.0f) ? cuCabsf((cuCdivf(sum1, make_cuComplex(sqrt(p), 0.0f)))) : 0.0f;
}
}
__global__ void CohFirstLineSet(cuComplex *d_Sum1, cuComplex *d_Power1, int inputPixels, size_t InPitch, int Offset)
{
const int col = blockIdx.x*blockDim.x + threadIdx.x;
if (col < inputPixels)
{
int winY = c_winY;
int winX = c_winX;
cuComplex sum1 = make_cuComplex(0.0f, 0.0f);
cuComplex power1 = make_cuComplex(0.0f, 0.0f);
for (int j = Offset; j <Offset+ winY; j++)
{
for (int i = col; i < col + winX; i++)
{
sum1 = cuCaddf(sum1, tex2D(tex_input, i, j));
power1 = cuCaddf(power1, tex2D(tex_norms, i, j));
}
}
d_Sum1[col] = sum1;
d_Power1[col] = power1;
}
}
__global__ void shfl_vertical(cuComplex *Src, int width, int height, int Nstep)
{
__shared__ float sums_x[32][9];
__shared__ float sums_y[32][9];
int tidx = blockIdx.x * blockDim.x + threadIdx.x;
//int warp_id = threadIdx.x / warpSize ;
unsigned int lane_id = tidx % 8;
cuComplex stepSum = make_cuComplex(0.0f, 0.0f);
sums_x[threadIdx.x][threadIdx.y] = 0;
sums_y[threadIdx.x][threadIdx.y] = 0;
__syncthreads();
for (int step = 0; step < Nstep; step++)
{
cuComplex sum = make_cuComplex(0.0f, 0.0f);
cuComplex *p = Src + (threadIdx.y + step * 8)*width + tidx;
sum = *p;
sums_x[threadIdx.x][threadIdx.y] = sum.x;
sums_y[threadIdx.x][threadIdx.y] = sum.y;
__syncthreads();
// place into SMEM
// shfl scan reduce the SMEM, reformating so the column
// sums are computed in a warp
// then read out properly
float partial_sum_x = 0;
float partial_sum_y = 0;
int j = threadIdx.x % 8;
int k = threadIdx.x / 8 + threadIdx.y * 4;
partial_sum_x = sums_x[k][j];
partial_sum_y = sums_y[k][j];
for (int i = 1; i <= 8; i *= 2)
{
float n_x = __shfl_up_sync(0xffffffff, partial_sum_x, i, 32);
float n_y = __shfl_up_sync(0xffffffff, partial_sum_y, i, 32);// removed for debug
//float n_x = 0;
//float n_y = 0;
if (lane_id >= i)
{
partial_sum_x += n_x;
partial_sum_y += n_y;
}
}
sums_x[k][j] = partial_sum_x;
sums_y[k][j] = partial_sum_y;
__syncthreads();
if (threadIdx.y > 0)
{
sum.x += sums_x[threadIdx.x][threadIdx.y - 1];
sum.y += sums_y[threadIdx.x][threadIdx.y - 1];
}
sum.x += stepSum.x;
sum.y += stepSum.y;
stepSum.x += sums_x[threadIdx.x][blockDim.y - 1];
stepSum.y += sums_y[threadIdx.x][blockDim.y - 1];
__syncthreads();
*p = sum;
}
};
__global__ void CpCoh2(float *Res, cuComplex *Sum1Array, cuComplex *Power1Array, cuComplex*JudgeArray, size_t Pitch,
size_t Pitch2, size_t Pitch3, int inputLines)
{
int row = blockIdx.y*blockDim.y + threadIdx.y;
int col = blockIdx.x*blockDim.x + threadIdx.x;
if (row < inputLines&&col < c_Dw)
{
size_t ComplexPitch = Pitch;
size_t FloatPitch = Pitch2;
cuComplex* rowSum = (cuComplex *)((char*)Sum1Array + row*Pitch3);
cuComplex* rowPower = (cuComplex *)((char*)Power1Array + row*Pitch3);
float* rowRes = (float *)((char*)Res + row*FloatPitch);
cuComplex* rowJudgeArray = (cuComplex *)((char*)JudgeArray + row*ComplexPitch);
cuComplex tmpSum = rowSum[col];
cuComplex tmpPower = rowPower[col];
//if (row == 1000 && col == 5000)
//{
////printf("Judge Array:(%lf,%lf)\n", rowJudgeArray[col].x, rowJudgeArray[col].y);
// printf("TmpSum:(%lf,%lf)\n", tmpSum.x, tmpSum.y);
// printf("TmpPower:(%lf,%lf)\n", tmpPower.x, tmpPower.y);
//}
float pp = tmpPower.x*tmpPower.y;
if (cuCabsf(rowJudgeArray[col])<0.001)
{
rowRes[col] = 0.0f;
}
else
{
cuComplex TmpRes = (pp > 0.0) ? (cuCdivf(tmpSum, make_cuComplex(sqrt(pp), 0.0f))) : make_cuComplex(0.0f, 0.0f);
rowRes[col] = cuCabsf(TmpRes);
}
}
}
__global__ void Cohbasic(float *Result, int inputLines, int inputPixels, size_t DstPitch, cuComplex*JudgeArray,size_t complexpitch)
{
const int row = blockIdx.y*blockDim.y + threadIdx.y;
const int col = blockIdx.x*blockDim.x + threadIdx.x;
if (row < inputLines&&col < inputPixels)
{
int winY = c_winY;
int winX = c_winX;
cuComplex sum1 = make_cuComplex(0.0f, 0.0f);
cuComplex power1 = make_cuComplex(0.0f, 0.0f);
for (int j = 0; j < winY; j++)
{
for (int i = col; i < col + winX; i++)
{
sum1 = cuCaddf(sum1, tex2D(tex_input, i, row + j));
power1 = cuCaddf(power1, tex2D(tex_norms, i, row + j));
}
}
float p = power1.x*power1.y;
float* rowResult = (float *)((char*)Result + row*DstPitch);
cuComplex* rowJudgeArray = (cuComplex *)((char*)JudgeArray + row*complexpitch);
if (cuCabsf(rowJudgeArray[col])<0.001)
{
rowResult[col] = 0.0f;
}
else
{
rowResult[col] = (p > 0.0f) ? cuCabsf((cuCdivf(sum1, make_cuComplex(sqrt(p), 0.0f)))) : 0.0f;
}
}
}
extern "C" void CoherenceEst_ESD
(int Dx0,
int Dy0,
int Dw,
int Dh,
int dx,
int dy,
int cohWinRg,
int cohWinAz,
complex<short>* masterArray,
cuComplex* d_slaveArray,
float* cohdata
)
{
int dataBytes = Dw*Dh*sizeof(complex<float>);
cudaHostRegister(cohdata, dataBytes / 2.0, cudaHostRegisterDefault);
cudaHostRegister(masterArray, dataBytes / 2.0, cudaHostRegisterDefault);
size_t Pitch3;
cuComplex*d_masterArray = NULL;
cuComplex* d_masterArray2 = NULL;
cuComplex *d_slaveArray2 = NULL;
cuComplex *d_Sum1, *d_Power1;
short2* d_TempMasterArray;
float *d_CohMat_Amp = NULL;
size_t FloatPitch;
size_t Pitch;
size_t FloatPitch3;
cudaMallocPitch((void**)&d_TempMasterArray, &FloatPitch, Dw*sizeof(short2), Dh);// For copy in complex<short> data
//for vertical prefix sum arrays
int Lines = MultipleOf32(Dh);
int cohw = Dw + cohWinRg - 1;
int cohh = Lines + cohWinAz - 1;
cudaMallocPitch((void**)&d_CohMat_Amp, &FloatPitch, Dw*sizeof(float), Lines);
cudaMallocPitch((void**)&d_Sum1, &Pitch, Dw*sizeof(cuComplex), Lines);
cudaMallocPitch((void**)&d_Power1, &Pitch, Dw*sizeof(cuComplex), Lines);
int Nstep = Lines / 8;
dim3 blockSz(32, 8);
int PitchPixels = Pitch / sizeof(cuComplex);
//For horizontal
//int Nstep = PitchPixels / 8;
if (PitchPixels% blockSz.x != 0)
{
cout << "Warning:PitchSize is not multiple of 32!" << endl;
cout << "Do not worry! we can handle it!" << endl;
cudaFree(d_Sum1);
cudaFree(d_Power1);
Pitch = MultipleOf32(PitchPixels)*sizeof(cuComplex);
cudaMalloc((void**)&d_Sum1, Pitch*Lines);
cudaMalloc((void**)&d_Power1, Pitch*Lines);
PitchPixels = Pitch / sizeof(cuComplex);
}
int numX = PitchPixels;
dim3 Grid(numX / blockSz.x, 1);
size_t Pitch2;
cudaMallocPitch((void**)&d_masterArray2, &Pitch2, cohw*sizeof(cuComplex), cohh);
cudaMallocPitch((void**)&d_slaveArray2, &Pitch2, cohw*sizeof(cuComplex), cohh);
;
cudaMemcpyToSymbol(c_Dy0, &Dy0, sizeof(int), 0, cudaMemcpyHostToDevice);
cudaMemcpyToSymbol(c_Dw, &Dw, sizeof(int), 0, cudaMemcpyHostToDevice);
cudaMemcpyToSymbol(c_Dh, &Dh, sizeof(int), 0, cudaMemcpyHostToDevice);
cudaMemcpyToSymbol(c_winY, &cohWinAz, sizeof(int), 0, cudaMemcpyHostToDevice);
cudaMemcpyToSymbol(c_winX, &cohWinRg, sizeof(int), 0, cudaMemcpyHostToDevice);
cudaChannelFormatDesc channelDesc_Complex = cudaCreateChannelDesc(32, 32, 0, 0, cudaChannelFormatKindFloat);
dim3 threads(16, 16);
dim3 Outblocks = dim3((Dw + threads.x-1) /threads.x , (Dh + threads.y-1) / threads.y);
dim3 Cohblocks = dim3((cohw + threads.x-1) / threads.x, (cohh + threads.y-1) / threads.y);
cudaStream_t stream[4];
for (int i = 0; i<4; i++)
cudaStreamCreate(&stream[i]);
cudaEvent_t g_start, g_stop;
float time_cost1, time_cost2;
cudaEventCreate(&g_start);
cudaEventCreate(&g_stop);
cudaEventRecord(g_start, 0);
cudaMemcpy2DAsync(d_TempMasterArray, FloatPitch, masterArray, Dw*sizeof(short2),
Dw*sizeof(short2), Dh, cudaMemcpyHostToDevice, stream[0]);
cudaDeviceSynchronize();
//Windows does not need to memset the array with zero values
// (cudaMemsetAsync(d_masterArray2, 0, Pitch2*cohh, stream1));
// (cudaMemsetAsync(d_slaveArray2, 0, Pitch2*cohh, stream1));
CohMatSet << <Outblocks, threads, 0, stream[0] >> >
(d_masterArray2, d_slaveArray2, d_TempMasterArray, d_slaveArray, Dh,
Dw, Pitch2, Pitch,FloatPitch, dx, dy);
//BindTexture
cudaBindTexture2D(0, tex_input, d_masterArray2, channelDesc_Complex, cohw, cohh, Pitch2);
cudaBindTexture2D(0, tex_norms, d_slaveArray2, channelDesc_Complex, cohw, cohh, Pitch2);
dim3 threadsV(16, 16);
dim3 blocks = dim3((Dw + threadsV.x - 1) / threadsV.x, (Dh - 1 + threadsV.y - 1) / threadsV.y);
/*Hide the time for coping back the coherence map*/
int PartLines = Lines / 4;
int PartOffset = PartLines*numX;
int HostOffset = PartLines*Dw;
//
bool BasicOrNot = false;
bool OverlapOrNot = false;
if (OverlapOrNot)
{
/*Overlap transfer*/
int PartNstep = PartLines / 8;
dim3 PartBlocksDiff((Dw + threads.x - 1) / threads.x, (PartLines - 1 + threads.y - 1) / threads.y);
dim3 PartBlocks((Dw + threads.x - 1) / threads.x, (PartLines + threads.y - 1) / threads.y);
int outputLines[4];
outputLines[0] = PartLines;
outputLines[1] = PartLines;
outputLines[2] = PartLines;
outputLines[3] = Dh - 3 * PartLines;
for (int N = 0; N < 4; N++)
{
CohFirstLineSet << <(Dw + 255) / 256, 256, 0, stream[N] >> >(d_Sum1 + N* PartOffset,
d_Power1 + N*PartOffset, Dw, Pitch2, N*PartLines);
CuCplCoherence_vertical << <PartBlocksDiff, threads, 0, stream[N] >> >
(d_Sum1 + N* PartOffset, d_Power1 + N*PartOffset, PartLines - 1, Dw, Pitch, N*PartLines);
shfl_vertical << <Grid, blockSz, 0, stream[N] >> >(d_Sum1 + N*PartOffset, PitchPixels, Lines, PartNstep);
shfl_vertical << <Grid, blockSz, 0, stream[N] >> >(d_Power1 + N*PartOffset, PitchPixels, Lines, PartNstep);
CpCoh2 << <PartBlocks, threads, 0, stream[N] >> >
(d_CohMat_Amp + N*PartOffset, d_Sum1 + N*PartOffset, d_Power1 + N*PartOffset,
d_slaveArray + N*PartOffset,
Pitch, FloatPitch, Pitch, PartLines);
cudaStreamSynchronize(stream[N]);
cudaMemcpy2DAsync(cohdata + N*HostOffset, Dw*sizeof(float),
d_CohMat_Amp + N*PartOffset, FloatPitch, Dw*sizeof(float), outputLines[N], cudaMemcpyDeviceToHost, stream[N]);
}
}
if (!OverlapOrNot)
{
int threads1D = 256;
dim3 Blocks_vertical = dim3((Dw + threads.x - 1) / threads.x, (Dh - 1 + threads.y - 1) / threads.y);
CohFirstLineSet << <(Dw + threads1D - 1) / threads1D, threads1D,0,stream[2] >> >(d_Sum1, d_Power1, Dw, Pitch2, 0);
CuCplCoherence_vertical << <Blocks_vertical, threads, 0, stream[2] >> >(d_Sum1, d_Power1, Dh - 1, Dw, Pitch, 0);
shfl_vertical << <Grid, blockSz, 0, stream[2] >> >(d_Sum1, PitchPixels, Lines, Nstep);
shfl_vertical << <Grid, blockSz, 0, stream[2] >> >(d_Power1, PitchPixels, Lines, Nstep);
CpCoh2 << <Outblocks, threads,0, stream[2] >> >
(d_CohMat_Amp , d_Sum1 , d_Power1 ,d_slaveArray ,
Pitch, FloatPitch, Pitch, Dh);
cudaMemcpy2D(cohdata , Dw*sizeof(float),
d_CohMat_Amp, FloatPitch, Dw*sizeof(float), Dh, cudaMemcpyDeviceToHost);
}
if (BasicOrNot)
{
Cohbasic << <Outblocks, threads, 0, stream[3] >> >(d_CohMat_Amp, Dh, Dw, FloatPitch,d_slaveArray,Pitch);
cudaMemcpy2D(cohdata, Dw*sizeof(float),
d_CohMat_Amp, FloatPitch, Dw*sizeof(float), Dh, cudaMemcpyDeviceToHost);
}
for (int i = 0; i<4; i++)
cudaStreamDestroy(stream[i]);
cudaEventRecord(g_stop, 0);
cudaEventSynchronize(g_stop);
cudaEventElapsedTime(&time_cost2, g_start, g_stop);
cout << "coherence kernel duration:" << time_cost2<<"ms"<< endl;
cudaEventDestroy(g_start);
cudaEventDestroy(g_stop);
cudaHostUnregister(cohdata);
cudaHostUnregister(masterArray);
cudaFree(d_slaveArray);
cudaUnbindTexture(tex_input);
cudaUnbindTexture(tex_norms);
cudaFree(d_CohMat_Amp);
cudaFree(d_masterArray2);
cudaFree(d_slaveArray2);
cudaFree(d_Power1);
cudaFree(d_Sum1);
cudaFree(d_TempMasterArray);
cudaDeviceSynchronize();
int cuda_last_error_flag = cudaGetLastError();
if (cuda_last_error_flag != 0)
{
printf("Returns an error code (%d) from GPU execution!\n Please debug it or report it to developer!\n", cuda_last_error_flag);
exit(0);
}
cudaDeviceReset();
}
extern "C" void CoherenceEst_Basic
(int Dx0,
int Dy0,
int Dw,
int Dh,
int dx,
int dy,
int cohWinRg,
int cohWinAz,
complex<short>* masterArray,
cuComplex* d_slaveArray,
float* cohdata
)
{
int dataBytes = Dw*Dh*sizeof(complex<float>);
cudaHostRegister(cohdata, dataBytes / 2.0, cudaHostRegisterDefault);
cudaHostRegister(masterArray, dataBytes / 2.0, cudaHostRegisterDefault);
size_t Pitch3;
cuComplex*d_masterArray = NULL;
cuComplex* d_masterArray2 = NULL;
cuComplex *d_slaveArray2 = NULL;
short2* d_TempMasterArray;
float *d_CohMat_Amp = NULL;
size_t FloatPitch;
size_t Pitch;
size_t FloatPitch3;
size_t TotalBytes = 0;
cudaMallocPitch((void**)&d_TempMasterArray, &FloatPitch, Dw*sizeof(short2), Dh);// For copy in complex<short> data
//for vertical prefix sum arrays
int Lines = MultipleOf32(Dh);
int cohw = Dw + cohWinRg - 1;
int cohh = Lines + cohWinAz - 1;
cuComplex* PitchArray;
cudaMallocPitch((void**)&PitchArray, &Pitch, Dw*sizeof(cuComplex), 1);
cudaFree(PitchArray);
size_t Pitch2;
cudaMallocPitch((void**)&d_masterArray2, &Pitch2, cohw*sizeof(cuComplex), cohh);
cudaMallocPitch((void**)&d_slaveArray2, &Pitch2, cohw*sizeof(cuComplex), cohh);
cudaMemcpyToSymbol(c_Dy0, &Dy0, sizeof(int), 0, cudaMemcpyHostToDevice);
cudaMemcpyToSymbol(c_Dw, &Dw, sizeof(int), 0, cudaMemcpyHostToDevice);
cudaMemcpyToSymbol(c_Dh, &Dh, sizeof(int), 0, cudaMemcpyHostToDevice);
cudaMemcpyToSymbol(c_winY, &cohWinAz, sizeof(int), 0, cudaMemcpyHostToDevice);
cudaMemcpyToSymbol(c_winX, &cohWinRg, sizeof(int), 0, cudaMemcpyHostToDevice);
cudaChannelFormatDesc channelDesc_Complex = cudaCreateChannelDesc(32, 32, 0, 0, cudaChannelFormatKindFloat);
dim3 threads(16, 16);
dim3 Outblocks = dim3((Dw + threads.x - 1) / threads.x, (Dh + threads.y - 1) / threads.y);
dim3 Cohblocks = dim3((cohw + threads.x - 1) / threads.x, (cohh + threads.y - 1) / threads.y);
(cudaMemcpy2D(d_TempMasterArray, FloatPitch, masterArray, Dw*sizeof(short2),
Dw*sizeof(short2), Dh, cudaMemcpyHostToDevice));
//Windows does not need to memset the array with zero values
// (cudaMemsetAsync(d_masterArray2, 0, Pitch2*cohh, stream1));
// (cudaMemsetAsync(d_slaveArray2, 0, Pitch2*cohh, stream1));
CohMatSet << <Outblocks, threads >> >
(d_masterArray2, d_slaveArray2, d_TempMasterArray, d_slaveArray, Dh,
Dw, Pitch2, Pitch, FloatPitch, dx, dy);
cudaMallocPitch((void**)&d_CohMat_Amp, &FloatPitch, Dw*sizeof(float), Lines);
//BindTexture
cudaBindTexture2D(0, tex_input, d_masterArray2, channelDesc_Complex, cohw, cohh, Pitch2);
cudaBindTexture2D(0, tex_norms, d_slaveArray2, channelDesc_Complex, cohw, cohh, Pitch2);
dim3 threadsV(16, 16);
dim3 blocks = dim3((Dw + threadsV.x - 1) / threadsV.x, (Dh - 1 + threadsV.y - 1) / threadsV.y);
cudaEvent_t g_start, g_stop;
float time_cost1;
cudaEventCreate(&g_start);
cudaEventCreate(&g_stop);
cudaEventRecord(g_start, 0);
//Basic Coherence estimation
Cohbasic << <Outblocks, threads >> >(d_CohMat_Amp, Dh, Dw, FloatPitch, d_slaveArray, Pitch);
(cudaMemcpy2D(cohdata, Dw*sizeof(float),
d_CohMat_Amp, FloatPitch, Dw*sizeof(float), Dh, cudaMemcpyDeviceToHost));
cudaEventRecord(g_stop, 0);
cudaEventSynchronize(g_stop);
(cudaEventElapsedTime(&time_cost1, g_start, g_stop));
cout << "Basic coherence kernel duration:" << time_cost1 << "ms" << endl;
cudaEventDestroy(g_start);
cudaEventDestroy(g_stop);
(cudaDeviceSynchronize());
cudaHostUnregister(cohdata);
cudaHostUnregister(masterArray);
cudaUnbindTexture(tex_input);
cudaUnbindTexture(tex_norms);
(cudaFree(d_CohMat_Amp));
(cudaFree(d_masterArray2));
(cudaFree(d_slaveArray2));
(cudaFree(d_slaveArray));
(cudaFree(d_TempMasterArray));
cudaDeviceSynchronize();
int cuda_last_error_flag = cudaGetLastError();
if (cuda_last_error_flag != 0)
{
printf("Returns an error code (%d) from GPU execution!\n Please debug it or report it to developer!\n", cuda_last_error_flag);
exit(0);
}
cudaDeviceReset();
}
extern "C" void CoherenceEst
(int Dx0,
int Dy0,
int Dw,
int Dh,
int dx,
int dy,
int cohWinRg,
int cohWinAz,
complex<short>* masterArray,
cuComplex* d_slaveArray,
float* cohdata
)
{
bool GPUMemSmall = true;
//if the total memory is less than 2Gb, go to coherence basic estimation version.
if (GPUMemSmall)
{
CoherenceEst_Basic(Dx0, Dy0, Dw, Dh, dx, dy, cohWinRg, cohWinAz, masterArray,
d_slaveArray, cohdata);
}
else
{
CoherenceEst_ESD(Dx0, Dy0, Dw, Dh, dx, dy, cohWinRg, cohWinAz, masterArray,
d_slaveArray, cohdata);
}
}