-
Notifications
You must be signed in to change notification settings - Fork 31
/
pdflow_cudalib.cu
1477 lines (1259 loc) · 50.9 KB
/
pdflow_cudalib.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
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
/*****************************************************************************
** Primal-Dual Scene Flow for RGB-D cameras **
** ---------------------------------------- **
** **
** Copyright(c) 2015, Mariano Jaimez Tarifa, University of Malaga **
** Copyright(c) 2015, Mohamed Souiai, Technical University of Munich **
** Copyright(c) 2015, MAPIR group, University of Malaga **
** Copyright(c) 2015, Computer Vision group, Tech. University of Munich **
** **
** This program is free software: you can redistribute it and/or modify **
** it under the terms of the GNU General Public License (version 3) as **
** published by the Free Software Foundation. **
** **
** This program is distributed in the hope that it will be useful, but **
** WITHOUT ANY WARRANTY; without even the implied warranty of **
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the **
** GNU General Public License for more details. **
** **
** You should have received a copy of the GNU General Public License **
** along with this program. If not, see <http://www.gnu.org/licenses/>. **
** **
*****************************************************************************/
#include "pdflow_cudalib.h"
// Memory allocation - device
//=============================================================================
__host__ void CSF_cuda::allocateDevMemory()
{
const unsigned int width = 640/cam_mode;
const unsigned int height = 480/cam_mode;
unsigned int s;
//Allocate the unfiltered depth and colour images on GPU
cudaError_t err = cudaMalloc((void**)&depth_wf_dev, width*height*sizeof(float) );
//printf("%s", cudaGetErrorString(err));
cudaMalloc((void**)&colour_wf_dev, width*height*sizeof(float) );
//Resize pyramid. Allocate memory for the different levels
const unsigned int pyr_levels = roundf(log2f(width/cols)) + ctf_levels;
for (unsigned int i = 0; i<pyr_levels; i++)
{
s = static_cast<unsigned int>(powf(2,i));
cudaMalloc((void**)&colour_dev[i], width*height*sizeof(float)/(s*s) );
cudaMalloc((void**)&colour_old_dev[i], width*height*sizeof(float)/(s*s) );
cudaMalloc((void**)&depth_dev[i], width*height*sizeof(float)/(s*s) );
cudaMalloc((void**)&depth_old_dev[i], width*height*sizeof(float)/(s*s) );
cudaMalloc((void**)&xx_dev[i], width*height*sizeof(float)/(s*s) );
cudaMalloc((void**)&xx_old_dev[i], width*height*sizeof(float)/(s*s) );
cudaMalloc((void**)&yy_dev[i], width*height*sizeof(float)/(s*s) );
cudaMalloc((void**)&yy_old_dev[i], width*height*sizeof(float)/(s*s) );
}
//Allocate dx, dy, dz on GPU
cudaMalloc((void**)&dx_dev, sizeof(float) );
cudaMalloc((void**)&dy_dev, sizeof(float) );
cudaMalloc((void**)&dz_dev, sizeof(float) );
//Allocate final solutions at the biggest resolution only once
cudaMalloc((void**)&du_l_dev, width*height*sizeof(float) );
cudaMalloc((void**)&dv_l_dev, width*height*sizeof(float) );
cudaMalloc((void**)&dw_l_dev, width*height*sizeof(float) );
cudaMalloc((void**)&pd_l_dev, width*height*sizeof(float) );
cudaMalloc((void**)&puu_l_dev, width*height*sizeof(float) );
cudaMalloc((void**)&puv_l_dev, width*height*sizeof(float) );
cudaMalloc((void**)&pvu_l_dev, width*height*sizeof(float) );
cudaMalloc((void**)&pvv_l_dev, width*height*sizeof(float) );
cudaMalloc((void**)&pwu_l_dev, width*height*sizeof(float) );
cudaMalloc((void**)&pwv_l_dev, width*height*sizeof(float) );
}
__host__ void CSF_cuda::allocateMemoryNewLevel(unsigned int rows_loc, unsigned int cols_loc, unsigned int level_i, unsigned int level_image_i)
{
local_level = level_i;
level_image = level_image_i;
rows_i = rows_loc;
cols_i = cols_loc;
//Allocate derivatives on GPU
cudaMalloc((void**)&dct_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&dcu_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&dcv_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&ddt_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&ddu_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&ddv_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&dcu_aux_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&dcv_aux_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&ddu_aux_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&ddv_aux_dev, rows_i*cols_i*sizeof(float) );
//Allocate gradients on GPU
cudaMalloc((void**)&gradu1_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&gradu2_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&gradv1_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&gradv2_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&gradw1_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&gradw2_dev, rows_i*cols_i*sizeof(float) );
//Allocate divergence on GPU
cudaMalloc((void**)&divpu_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&divpv_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&divpw_dev, rows_i*cols_i*sizeof(float) );
//Allocate step sizes on GPU
cudaMalloc((void**)&sigma_pd_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&sigma_puvx_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&sigma_puvy_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&sigma_pwx_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&sigma_pwy_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&tau_u_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&tau_v_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&tau_w_dev, rows_i*cols_i*sizeof(float) );
//Allocate mu_uv on GPU
cudaMalloc((void**)&mu_uv_dev, rows_i*cols_i*sizeof(float) );
//Allocate du_acc, dv_acc, dw_acc on GPU
cudaMalloc((void**)&du_acc_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&dv_acc_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&dw_acc_dev, rows_i*cols_i*sizeof(float) );
//Allocate ri, rj, ri_2, rj_2, du_prev, dv_prev on GPU
cudaMalloc((void**)&ri_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&rj_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&ri_2_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&rj_2_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&du_prev_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&dv_prev_dev, rows_i*cols_i*sizeof(float) );
//Allocate values of previous level on GPU
cudaMalloc((void**)&du_new_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&dv_new_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&dw_new_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&pd_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&puu_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&puv_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&pvu_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&pvv_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&pwu_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&pwv_dev, rows_i*cols_i*sizeof(float) );
//Allocate memory for the upsampling variables
cudaMalloc((void**)&du_upsamp_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&dv_upsamp_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&dw_upsamp_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&pd_upsamp_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&puu_upsamp_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&puv_upsamp_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&pvu_upsamp_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&pvv_upsamp_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&pwu_upsamp_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&pwv_upsamp_dev, rows_i*cols_i*sizeof(float) );
//Allocate dx, dy, dz on GPU
cudaFree(dx_dev); cudaFree(dy_dev); cudaFree(dz_dev);
cudaMalloc((void**)&dx_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&dy_dev, rows_i*cols_i*sizeof(float) );
cudaMalloc((void**)&dz_dev, rows_i*cols_i*sizeof(float) );
}
// Copy object to device
//=============================================================================
CSF_cuda *ObjectToDevice(CSF_cuda *csf_host)
{
CSF_cuda *csf_device;
cudaMalloc((void**)&csf_device, sizeof(CSF_cuda) );
cudaMemcpy(csf_device, csf_host, sizeof(CSF_cuda), cudaMemcpyHostToDevice);
return csf_device;
}
// Copy data from host to device and viceversa
//=============================================================================
__host__ void CSF_cuda::readParameters(unsigned int rows_host, unsigned int cols_host, float lambda_i_host, float lambda_d_host, float mu_host,
float *g_mask, unsigned int levels_host, unsigned int cam_mode_host, float fovh_host, float fovv_host)
{
rows = rows_host;
cols = cols_host;
lambda_i = lambda_i_host;
lambda_d = lambda_d_host;
mu = mu_host;
ctf_levels = levels_host;
cam_mode = cam_mode_host;
fovh = fovh_host;
fovv = fovv_host;
//Allocate and copy gaussian mask
cudaError_t err = cudaMalloc((void**)&g_mask_dev, 5*5*sizeof(float));
//printf("%s", cudaGetErrorString(err));
cudaMemcpy(g_mask_dev, g_mask, 5*5*sizeof(float), cudaMemcpyHostToDevice);
}
__host__ void CSF_cuda::copyNewFrames(float *colour_wf, float *depth_wf)
{
const unsigned int width = 640/cam_mode;
const unsigned int height = 480/cam_mode;
cudaMemcpy(depth_wf_dev, depth_wf, width*height*sizeof(float), cudaMemcpyHostToDevice);
cudaMemcpy(colour_wf_dev, colour_wf, width*height*sizeof(float), cudaMemcpyHostToDevice);
//Swap pointers of old and new images of the pyramid (equivalent to pushing the new frames to the old ones)
for (unsigned int i=0; i<8; i++)
{
float *temp = colour_old_dev[i];
colour_old_dev[i] = colour_dev[i];
colour_dev[i] = temp;
temp = depth_old_dev[i];
depth_old_dev[i] = depth_dev[i];
depth_dev[i] = temp;
temp = xx_old_dev[i];
xx_old_dev[i] = xx_dev[i];
xx_dev[i] = temp;
temp = yy_old_dev[i];
yy_old_dev[i] = yy_dev[i];
yy_dev[i] = temp;
}
}
__host__ void CSF_cuda::copyAllSolutions(float *dx, float *dy, float *dz, float *depth, float *depth_old, float *colour, float *colour_old, float *xx, float *xx_old, float *yy, float *yy_old)
{
cudaMemcpy(dx, dx_dev, rows_i*cols_i*sizeof(float), cudaMemcpyDeviceToHost);
cudaMemcpy(dy, dy_dev, rows_i*cols_i*sizeof(float), cudaMemcpyDeviceToHost);
cudaMemcpy(dz, dz_dev, rows_i*cols_i*sizeof(float), cudaMemcpyDeviceToHost);
cudaMemcpy(depth, depth_dev[level_image], rows_i*cols_i*sizeof(float), cudaMemcpyDeviceToHost);
cudaMemcpy(depth_old, depth_old_dev[level_image], rows_i*cols_i*sizeof(float), cudaMemcpyDeviceToHost);
cudaMemcpy(colour, colour_dev[level_image], rows_i*cols_i*sizeof(float), cudaMemcpyDeviceToHost);
cudaMemcpy(colour_old, colour_old_dev[level_image], rows_i*cols_i*sizeof(float), cudaMemcpyDeviceToHost);
cudaMemcpy(xx, xx_dev[level_image], rows_i*cols_i*sizeof(float), cudaMemcpyDeviceToHost);
cudaMemcpy(xx_old, xx_old_dev[level_image], rows_i*cols_i*sizeof(float), cudaMemcpyDeviceToHost);
cudaMemcpy(yy, yy_dev[level_image], rows_i*cols_i*sizeof(float), cudaMemcpyDeviceToHost);
cudaMemcpy(yy_old, yy_old_dev[level_image], rows_i*cols_i*sizeof(float), cudaMemcpyDeviceToHost);
}
__host__ void CSF_cuda::copyMotionField(float *dx, float *dy, float *dz)
{
cudaMemcpy(dx, dx_dev, rows_i*cols_i*sizeof(float), cudaMemcpyDeviceToHost);
cudaMemcpy(dy, dy_dev, rows_i*cols_i*sizeof(float), cudaMemcpyDeviceToHost);
cudaMemcpy(dz, dz_dev, rows_i*cols_i*sizeof(float), cudaMemcpyDeviceToHost);
}
// Free memory - device
//=============================================================================
__host__ void CSF_cuda::freeDeviceMemory()
{
cudaFree(g_mask_dev);
const unsigned int width = 640/cam_mode;
const unsigned int pyr_levels = roundf(log2f(width/cols)) + ctf_levels;
for (unsigned int i = 0; i<pyr_levels; i++)
{
cudaFree(depth_old_dev[i]);
cudaFree(depth_dev[i]);
cudaFree(colour_old_dev[i]);
cudaFree(colour_dev[i]);
cudaFree(xx_old_dev[i]);
cudaFree(xx_dev[i]);
cudaFree(yy_old_dev[i]);
cudaFree(yy_dev[i]);
}
//Free pointers to pointers
cudaFree(depth_old_dev);
cudaFree(depth_dev);
cudaFree(colour_old_dev);
cudaFree(colour_dev);
cudaFree(xx_old_dev);
cudaFree(xx_dev);
cudaFree(yy_old_dev);
cudaFree(yy_dev);
cudaFree(du_l_dev); cudaFree(dv_l_dev); cudaFree(dw_l_dev);
cudaFree(pd_l_dev);
cudaFree(puu_l_dev); cudaFree(puv_l_dev);
cudaFree(pvu_l_dev); cudaFree(pvv_l_dev);
cudaFree(pwu_l_dev); cudaFree(pwv_l_dev);
cudaFree(colour_wf_dev);
cudaFree(depth_wf_dev);
cudaFree(dx_dev); cudaFree(dy_dev); cudaFree(dz_dev);
}
__host__ void CSF_cuda::freeLevelVariables()
{
cudaFree(du_upsamp_dev); cudaFree(dv_upsamp_dev); cudaFree(dw_upsamp_dev);
cudaFree(pd_upsamp_dev);
cudaFree(puu_upsamp_dev); cudaFree(puv_upsamp_dev);
cudaFree(pvu_upsamp_dev); cudaFree(pvv_upsamp_dev);
cudaFree(pwu_upsamp_dev); cudaFree(pwv_upsamp_dev);
cudaFree(du_new_dev);
cudaFree(dv_new_dev);
cudaFree(dw_new_dev);
cudaFree(pd_dev);
cudaFree(puu_dev); cudaFree(puv_dev);
cudaFree(pvu_dev); cudaFree(pvv_dev);
cudaFree(pwu_dev); cudaFree(pwv_dev);
cudaFree(mu_uv_dev);
cudaFree(ri_dev); cudaFree(rj_dev);
cudaFree(ri_2_dev); cudaFree(rj_2_dev);
cudaFree(du_acc_dev); cudaFree(dv_acc_dev); cudaFree(dw_acc_dev);
cudaFree(du_prev_dev); cudaFree(dv_prev_dev);
cudaFree(dct_dev); cudaFree(dcu_dev); cudaFree(dcv_dev);
cudaFree(ddt_dev); cudaFree(ddu_dev); cudaFree(ddv_dev);
cudaFree(dcu_aux_dev); cudaFree(dcv_aux_dev);
cudaFree(ddu_aux_dev); cudaFree(ddv_aux_dev);
cudaFree(gradu1_dev); cudaFree(gradu2_dev);
cudaFree(gradv1_dev); cudaFree(gradv2_dev);
cudaFree(gradw1_dev); cudaFree(gradw2_dev);
cudaFree(divpu_dev); cudaFree(divpv_dev); cudaFree(divpw_dev);
cudaFree(sigma_pd_dev); cudaFree(sigma_puvx_dev); cudaFree(sigma_puvy_dev);
cudaFree(sigma_pwx_dev); cudaFree(sigma_pwy_dev);
cudaFree(tau_u_dev); cudaFree(tau_v_dev); cudaFree(tau_w_dev);
}
// Create gaussian pyramid
//=============================================================================
__device__ void CSF_cuda::computePyramidLevel(unsigned int index, unsigned int level)
{
//Shared memory for the gaussian mask
__shared__ float mask_shared[25];
if (threadIdx.x < 25) //Warning!!!!!! Number of threads should be higher than 25
mask_shared[threadIdx.x] = g_mask_dev[threadIdx.x];
__syncthreads();
const float max_depth_dif = 0.1f;
//Calculate indices
const unsigned int v = index%(rows_i);
const unsigned int u = index/(rows_i);
if (level == 0)
{
//Copy intensity image
colour_dev[level][index] = colour_wf_dev[index];
//Copy depth image
depth_dev[level][index] = depth_wf_dev[index];
}
// Downsampling
//-----------------------------------------------------------------------------
else
{
float sumd = 0.f, sumc = 0.f, acu_weights_d = 0.f, acu_weights_c = 0.f;
const unsigned int ind_cent_prev = 2*v + 4*u*rows_i;
const float dcenter = depth_dev[level-1][ind_cent_prev];
//Inner pixels
if ((v>0)&&(v<rows_i-1)&&(u>0)&&(u<cols_i-1))
{
for (int k=-2; k<3; k++)
for (int l=-2; l<3; l++)
{
const unsigned int ind_loop_prev = 2*v+k + 2*(2*u+l)*rows_i;
const unsigned int ind_mask = 12+k+5*l; //2+k+(2+l)*5
//Colour
sumc += mask_shared[ind_mask]*colour_dev[level-1][ind_loop_prev];
//Depth
if ((depth_dev[level-1][ind_loop_prev] > 0.f)&&(fabsf(depth_dev[level-1][ind_loop_prev]-dcenter) < max_depth_dif))
{
const float aux_w = mask_shared[ind_mask]*(max_depth_dif - fabsf(depth_dev[level-1][ind_loop_prev] - dcenter));
acu_weights_d += aux_w;
sumd += aux_w*depth_dev[level-1][ind_loop_prev];
}
}
if (sumd > 0.f)
depth_dev[level][index] = sumd/acu_weights_d;
else
depth_dev[level][index] = 0.f;
colour_dev[level][index] = sumc;
}
//Boundary
else
{
for (int k=-2; k<3; k++)
for (int l=-2; l<3; l++)
{
const int indv = 2*v+k, indu = 2*u+l;
if ((indv>=0)&&(indv<2*rows_i)&&(indu>=0)&&(indu<2*cols_i))
{
const unsigned int ind_loop_prev = 2*v+k + 2*(2*u+l)*rows_i;
const unsigned int ind_mask = 12+k+5*l; //2+k+(2+l)*5
//Colour
sumc += mask_shared[ind_mask]*colour_dev[level-1][ind_loop_prev];
acu_weights_c += mask_shared[ind_mask];
//Depth
if ((depth_dev[level-1][ind_loop_prev] > 0.f)&&(fabsf(depth_dev[level-1][ind_loop_prev]-depth_dev[level-1][ind_cent_prev]) < max_depth_dif))
{
const float aux_w = mask_shared[ind_mask]*(max_depth_dif - fabsf(depth_dev[level-1][ind_loop_prev] - depth_dev[level-1][ind_cent_prev]));
acu_weights_d += aux_w;
sumd += aux_w*depth_dev[level-1][ind_loop_prev];
}
}
}
colour_dev[level][index] = sumc/acu_weights_c;
if (sumd > 0.f)
depth_dev[level][index] = sumd/acu_weights_d;
else
depth_dev[level][index] = 0.f;
}
}
//Calculate coordinates "xy" of the points
const float inv_f_i = 2.f*tan(0.5f*fovh)/float(cols_i);
const float disp_u_i = 0.5f*(cols_i-1);
const float disp_v_i = 0.5f*(rows_i-1);
xx_dev[level][index] = (u - disp_u_i)*depth_dev[level][index]*inv_f_i;
yy_dev[level][index] = (v - disp_v_i)*depth_dev[level][index]*inv_f_i;
}
// Initiallize some variables
//=============================================================================
__device__ void CSF_cuda::assignZeros(unsigned int index)
{
du_upsamp_dev[index] = 0.f; dv_upsamp_dev[index] = 0.f; dw_upsamp_dev[index] = 0.f;
pd_upsamp_dev[index] = 0.f;
puu_upsamp_dev[index] = 0.f; puv_upsamp_dev[index] = 0.f;
pvu_upsamp_dev[index] = 0.f; pvv_upsamp_dev[index] = 0.f;
pwu_upsamp_dev[index] = 0.f; pwv_upsamp_dev[index] = 0.f;
du_prev_dev[index] = 0.f; dv_prev_dev[index] = 0.f;
du_new_dev[index] = 0.f; dv_new_dev[index] = 0.f; dw_new_dev[index] = 0.f;
pd_dev[index] = 0.f;
puu_dev[index] = 0.f; puv_dev[index] = 0.f;
pvu_dev[index] = 0.f; pvv_dev[index] = 0.f;
pwu_dev[index] = 0.f; pwv_dev[index] = 0.f;
du_acc_dev[index] = 0.f; dv_acc_dev[index] = 0.f; dw_acc_dev[index] = 0.f;
}
// Upsample previous solution
//=============================================================================
__device__ void CSF_cuda::upsampleCopyPrevSolution(unsigned int index)
{
//Calculate (v,u)
const unsigned int v = index%(rows_i/2);
const unsigned int u = 2*index/(rows_i);
const unsigned int index_big = 2*(v + u*rows_i);
du_upsamp_dev[index_big] = 2.f*du_l_dev[index];
dv_upsamp_dev[index_big] = 2.f*dv_l_dev[index];
dw_upsamp_dev[index_big] = dw_l_dev[index];
pd_upsamp_dev[index_big] = pd_l_dev[index];
puu_upsamp_dev[index_big] = puu_l_dev[index];
puv_upsamp_dev[index_big] = puv_l_dev[index];
pvu_upsamp_dev[index_big] = pvu_l_dev[index];
pvv_upsamp_dev[index_big] = pvv_l_dev[index];
pwu_upsamp_dev[index_big] = pwu_l_dev[index];
pwv_upsamp_dev[index_big] = pwv_l_dev[index];
}
__device__ void CSF_cuda::upsampleFilterPrevSolution(unsigned int index)
{
const unsigned int v = index%rows_i;
const unsigned int u = index/rows_i;
//Shared memory for the gaussian mask - Warning!! The number of threads should be higher than 25
__shared__ float mask_shared[25];
if (threadIdx.x < 25)
mask_shared[threadIdx.x] = 4.f*g_mask_dev[threadIdx.x];
__syncthreads();
float du = 0.f, dv = 0.f, dw = 0.f, pd = 0.f, puu = 0.f, puv = 0.f, pvu = 0.f, pvv = 0.f, pwu = 0.f, pwv = 0.f;
//Inner pixels
if ((v>1)&&(v<rows_i-2)&&(u>1)&&(u<cols_i-2))
{
for (int k=-2; k<3; k++)
for (int l=-2; l<3; l++)
{
const unsigned incr_index = v+k+(u+l)*rows_i;
const float gmask_factor = mask_shared[12 + k + 5*l]; //[2+k + (2+l)*5]
du += gmask_factor*du_upsamp_dev[incr_index];
dv += gmask_factor*dv_upsamp_dev[incr_index];
dw += gmask_factor*dw_upsamp_dev[incr_index];
pd += gmask_factor*pd_upsamp_dev[incr_index];
puu += gmask_factor*puu_upsamp_dev[incr_index];
puv += gmask_factor*puv_upsamp_dev[incr_index];
pvu += gmask_factor*pvu_upsamp_dev[incr_index];
pvv += gmask_factor*pvv_upsamp_dev[incr_index];
pwu += gmask_factor*pwu_upsamp_dev[incr_index];
pwv += gmask_factor*pwv_upsamp_dev[incr_index];
}
}
//Boundary
else
{
float acu_weight = 1.f;
for (int k=-2; k<3; k++)
for (int l=-2; l<3; l++)
{
const int indv = v+k, indu = u+l;
if ((indv<0)||(indv>=rows_i)||(indu<0)||(indu>=cols_i))
{
acu_weight -= 0.25f*mask_shared[12 + k + 5*l]; //[2+k + (2+l)*5]
continue;
}
else
{
const unsigned incr_index = v+k+(u+l)*rows_i;
const float gmask_factor = mask_shared[12 + k + 5*l]; //[2+k + (2+l)*5]
du += gmask_factor*du_upsamp_dev[incr_index];
dv += gmask_factor*dv_upsamp_dev[incr_index];
dw += gmask_factor*dw_upsamp_dev[incr_index];
pd += gmask_factor*pd_upsamp_dev[incr_index];
puu += gmask_factor*puu_upsamp_dev[incr_index];
puv += gmask_factor*puv_upsamp_dev[incr_index];
pvu += gmask_factor*pvu_upsamp_dev[incr_index];
pvv += gmask_factor*pvv_upsamp_dev[incr_index];
pwu += gmask_factor*pwu_upsamp_dev[incr_index];
pwv += gmask_factor*pwv_upsamp_dev[incr_index];
}
}
const float inv_acu_weight = fdividef(1.f, acu_weight);
du *= inv_acu_weight;
dv *= inv_acu_weight;
dw *= inv_acu_weight;
pd *= inv_acu_weight;
puu *= inv_acu_weight;
puv *= inv_acu_weight;
pvu *= inv_acu_weight;
pvv *= inv_acu_weight;
pwu *= inv_acu_weight;
pwv *= inv_acu_weight;
}
//Write results to global memory
du_prev_dev[index] = du;
dv_prev_dev[index] = dv;
dw_new_dev[index] = dw;
pd_dev[index] = pd;
puu_dev[index] = puu;
puv_dev[index] = puv;
pvu_dev[index] = pvu;
pvv_dev[index] = pvv;
pwu_dev[index] = pwu;
pwv_dev[index] = pwv;
//Last update, for dw_acc
dw_acc_dev[index] = dw;
}
// Compute intensity and depth derivatives
//=============================================================================
__device__ void CSF_cuda::computeImGradients(unsigned int index)
{
//Calculate (v,u)
const unsigned int v = index%rows_i;
const unsigned int u = index/rows_i;
//Row gradients
if (u == 0)
{
dcu_aux_dev[index] = colour_dev[level_image][index+rows_i] - colour_dev[level_image][index];
ddu_aux_dev[index] = depth_dev[level_image][index+rows_i] - depth_dev[level_image][index];
}
else if (u == cols_i-1)
{
dcu_aux_dev[index] = colour_dev[level_image][index] - colour_dev[level_image][index-rows_i];
ddu_aux_dev[index] = depth_dev[level_image][index] - depth_dev[level_image][index-rows_i];
}
else
{
dcu_aux_dev[index] = (ri_2_dev[index]*(colour_dev[level_image][index+rows_i]-colour_dev[level_image][index])
+ ri_2_dev[index-rows_i]*(colour_dev[level_image][index]-colour_dev[level_image][index-rows_i]))
/(ri_2_dev[index]+ri_2_dev[index-rows_i]);
if (depth_dev[level_image][index] > 0.f)
ddu_aux_dev[index] = (ri_2_dev[index]*(depth_dev[level_image][index+rows_i]-depth_dev[level_image][index])
+ ri_2_dev[index-rows_i]*(depth_dev[level_image][index]-depth_dev[level_image][index-rows_i]))
/(ri_2_dev[index]+ri_2_dev[index-rows_i]);
else
ddu_aux_dev[index] = 0.f;
}
//Col gradients
if (v == 0)
{
dcv_aux_dev[index] = colour_dev[level_image][index+1] - colour_dev[level_image][index];
ddv_aux_dev[index] = depth_dev[level_image][index+1] - depth_dev[level_image][index];
}
else if (v == rows_i-1)
{
dcv_aux_dev[index] = colour_dev[level_image][index] - colour_dev[level_image][index-1];
ddv_aux_dev[index] = depth_dev[level_image][index] - depth_dev[level_image][index-1];
}
else
{
dcv_aux_dev[index] = (rj_2_dev[index]*(colour_dev[level_image][index+1]-colour_dev[level_image][index])
+ rj_2_dev[index-1]*(colour_dev[level_image][index]-colour_dev[level_image][index-1]))
/(rj_2_dev[index]+rj_2_dev[index-1]);
if (depth_dev[level_image][index] > 0.f)
ddv_aux_dev[index] = (rj_2_dev[index]*(depth_dev[level_image][index+1]-depth_dev[level_image][index])
+ rj_2_dev[index-1]*(depth_dev[level_image][index]-depth_dev[level_image][index-1]))
/(rj_2_dev[index]+rj_2_dev[index-1]);
else
ddv_aux_dev[index] = 0.f;
}
}
__device__ void CSF_cuda::performWarping(unsigned int index)
{
//Calculate (v,u)
const unsigned int v = index%rows_i;
const unsigned int u = index/rows_i;
float warped_pixel;
//Intensity images
const float ind_uf = float(u) + du_prev_dev[index];
const float ind_vf = float(v) + dv_prev_dev[index];
warped_pixel = interpolatePixel(colour_dev[level_image], ind_uf, ind_vf);
dct_dev[index] = warped_pixel - colour_old_dev[level_image][index];
dcu_dev[index] = interpolatePixel(dcu_aux_dev, ind_uf, ind_vf);
dcv_dev[index] = interpolatePixel(dcv_aux_dev, ind_uf, ind_vf);
//Depth images
warped_pixel = interpolatePixelDepth(depth_dev[level_image], ind_uf, ind_vf);
if (warped_pixel > 0.f)
ddt_dev[index] = warped_pixel - depth_old_dev[level_image][index];
else
ddt_dev[index] = 0.f;
ddu_dev[index] = interpolatePixel(ddu_aux_dev, ind_uf, ind_vf);
ddv_dev[index] = interpolatePixel(ddv_aux_dev, ind_uf, ind_vf);
}
__device__ float CSF_cuda::interpolatePixel(float *mat, float ind_u, float ind_v)
{
if (ind_u < 0.f) { ind_u = 0.f;}
else if (ind_u > cols_i - 1.f) { ind_u = cols_i - 1.f;}
if (ind_v < 0.f) { ind_v = 0.f;}
else if (ind_v > rows_i - 1.f) { ind_v = rows_i - 1.f;}
const unsigned int sup_u = __float2int_ru(ind_u);
const unsigned int inf_u = __float2int_rd(ind_u);
const unsigned int sup_v = __float2int_ru(ind_v);
const unsigned int inf_v = __float2int_rd(ind_v);
if ((sup_u == inf_u)&&(sup_v == inf_v))
return mat[lrintf(ind_v + rows_i*ind_u)];
else if (sup_u == inf_u)
return (sup_v - ind_v)*mat[inf_v + rows_i*lrintf(ind_u)] + (ind_v - inf_v)*mat[sup_v + rows_i*lrintf(ind_u)];
else if (sup_v == inf_v)
return (sup_u - ind_u)*mat[lrintf(ind_v) + rows_i*inf_u] + (ind_u - inf_u)*mat[lrintf(ind_v) + rows_i*sup_u];
else
{
//First in u
const float val_sup_v = (sup_u - ind_u)*mat[sup_v + rows_i*inf_u] + (ind_u - inf_u)*mat[sup_v + rows_i*sup_u];
const float val_inf_v = (sup_u - ind_u)*mat[inf_v + rows_i*inf_u] + (ind_u - inf_u)*mat[inf_v + rows_i*sup_u];
return (sup_v - ind_v)*val_inf_v + (ind_v - inf_v)*val_sup_v;
}
}
__device__ float CSF_cuda::interpolatePixelDepth(float *mat, float ind_u, float ind_v)
{
if (ind_u < 0.f) { ind_u = 0.f;}
else if (ind_u > cols_i - 1.f) { ind_u = cols_i - 1.f;}
if (ind_v < 0.f) { ind_v = 0.f;}
else if (ind_v > rows_i - 1.f) { ind_v = rows_i - 1.f;}
const unsigned int sup_u = __float2int_ru(ind_u);
const unsigned int inf_u = __float2int_rd(ind_u);
const unsigned int sup_v = __float2int_ru(ind_v);
const unsigned int inf_v = __float2int_rd(ind_v);
if ((mat[sup_v + rows_i*sup_u] == 0.f)||(mat[sup_v + rows_i*inf_u] == 0.f)||(mat[inf_v + rows_i*sup_u] == 0.f)||(mat[inf_v + rows_i*inf_u]==0.f))
{
const unsigned int rind_u = __float2int_rn(ind_u);
const unsigned int rind_v = __float2int_rn(ind_v);
return mat[rind_v + rows_i*rind_u];
}
else
{
if ((sup_u == inf_u)&&(sup_v == inf_v))
return mat[lrintf(ind_v + rows_i*ind_u)];
else if (sup_u == inf_u)
return (sup_v - ind_v)*mat[inf_v + rows_i*lroundf(ind_u)] + (ind_v - inf_v)*mat[sup_v + rows_i*lroundf(ind_u)];
else if (sup_v == inf_v)
return (sup_u - ind_u)*mat[lroundf(ind_v) + rows_i*inf_u] + (ind_u - inf_u)*mat[lroundf(ind_v) + rows_i*sup_u];
else
{
//First in u
const float val_sup_v = (sup_u - ind_u)*mat[sup_v + rows_i*inf_u] + (ind_u - inf_u)*mat[sup_v + rows_i*sup_u];
const float val_inf_v = (sup_u - ind_u)*mat[inf_v + rows_i*inf_u] + (ind_u - inf_u)*mat[inf_v + rows_i*sup_u];
return (sup_v - ind_v)*val_inf_v + (ind_v - inf_v)*val_sup_v;
}
}
}
// Preliminary computations
//=============================================================================
__device__ void CSF_cuda::computeRij(unsigned int index)
{
//Calculate (v,u)
const unsigned int v = index%rows_i;
const unsigned int u = index/rows_i;
float dxu, dzu, dxu_2, dzu_2;
float dyv, dzv, dyv_2, dzv_2;
if (u == cols_i-1)
{
dxu = 0.f; dzu = 0.f;
dxu_2 = 0.f; dzu_2 = 0.f;
}
else
{
dxu = xx_old_dev[level_image][index + rows_i] - xx_old_dev[level_image][index];
dzu = depth_old_dev[level_image][index + rows_i] - depth_old_dev[level_image][index];
dxu_2 = xx_dev[level_image][index + rows_i] - xx_dev[level_image][index];
dzu_2 = depth_dev[level_image][index + rows_i] - depth_dev[level_image][index];
}
if (v == rows_i-1)
{
dyv = 0.f; dzv = 0.f;
dyv_2 = 0.f; dzv_2 = 0.f;
}
else
{
dyv = yy_old_dev[level_image][index+1] - yy_old_dev[level_image][index];
dzv = depth_old_dev[level_image][index+1] - depth_old_dev[level_image][index];
dyv_2 = yy_dev[level_image][index+1] - yy_dev[level_image][index];
dzv_2 = depth_dev[level_image][index+1] - depth_dev[level_image][index];
}
if (fabsf(dxu) + fabsf(dzu) > 0.f)
ri_dev[index] = 2.f*rhypotf(dxu,dzu); //2.f/sqrtf(dxu*dxu + dzu*dzu);
else
ri_dev[index] = 1.f;
if (fabsf(dyv) + fabsf(dzv) > 0.f)
rj_dev[index] = 2.f*rhypotf(dyv,dzv); //2.f/sqrtf(dyv*dyv + dzv*dzv);
else
rj_dev[index] = 1.f;
if (fabsf(dxu_2) + fabsf(dzu_2) > 0.f)
ri_2_dev[index] = 2.f*rhypotf(dxu_2,dzu_2); //2.f/sqrtf(dxu*dxu + dzu*dzu);
else
ri_2_dev[index] = 1.f;
if (fabsf(dyv_2) + fabsf(dzv_2) > 0.f)
rj_2_dev[index] = 2.f*rhypotf(dyv_2,dzv_2); //2.f/sqrtf(dyv*dyv + dzv*dzv);
else
rj_2_dev[index] = 1.f;
}
__device__ void CSF_cuda::computeMu(unsigned int index)
{
mu_uv_dev[index] = fdividef(mu, 1.f + 1000.f*(ddu_dev[index]*ddu_dev[index] + ddv_dev[index]*ddv_dev[index] + ddt_dev[index]*ddt_dev[index]));
}
__device__ void CSF_cuda::computeStepSizes(unsigned int index)
{
//Load lambda from global memory
const float lambdai = lambda_i, lambdad = lambda_d;
sigma_pd_dev[index] = fdividef(1.f, mu_uv_dev[index]*(1.f + abs(ddu_dev[index]) + abs(ddv_dev[index])) + 1e-10f);
sigma_puvx_dev[index] = fdividef(0.5f, lambdai*ri_dev[index] + 1e-10f);
sigma_puvy_dev[index] = fdividef(0.5f, lambdai*rj_dev[index] + 1e-10f);
sigma_pwx_dev[index] = fdividef(0.5f, ri_dev[index]*lambdad + 1e-10f);
sigma_pwy_dev[index] = fdividef(0.5f, rj_dev[index]*lambdad + 1e-10f);
//Calculate (v,u)
const unsigned int v = index%rows_i;
const unsigned int u = index/rows_i;
float acu_r = ri_dev[index] + rj_dev[index];
if (u > 0) acu_r += ri_dev[index-rows_i];
if (v > 0) acu_r += rj_dev[index-1];
tau_u_dev[index] = fdividef(1.f, mu_uv_dev[index]*abs(ddu_dev[index]) + lambdai*acu_r + 1e-10f);
tau_v_dev[index] = fdividef(1.f, mu_uv_dev[index]*abs(ddv_dev[index]) + lambdai*acu_r + 1e-10f);
tau_w_dev[index] = fdividef(1.f, mu_uv_dev[index] + lambdad*acu_r + 1e-10f);
}
// Main iteration
//=============================================================================
__device__ void CSF_cuda::updateDualVariables(unsigned int index)
{
//Create aux variables to avoid repetitive global memory access
float module_p;
float pd = pd_dev[index], puu = puu_dev[index], puv = puv_dev[index];
float pvu = pvu_dev[index], pvv = pvv_dev[index], pwu = pwu_dev[index], pwv = pwv_dev[index];
//Update dual variables
//Solve pd
pd += sigma_pd_dev[index]*mu_uv_dev[index]*(-dw_acc_dev[index] + ddt_dev[index] + ddu_dev[index]*du_acc_dev[index] + ddv_dev[index]*dv_acc_dev[index]);
//Solve pu
puu += sigma_puvx_dev[index]*lambda_i*gradu1_dev[index];
puv += sigma_puvy_dev[index]*lambda_i*gradu2_dev[index];
//Solve pv
pvu += sigma_puvx_dev[index]*lambda_i*gradv1_dev[index];
pvv += sigma_puvy_dev[index]*lambda_i*gradv2_dev[index];
//Solve pw
pwu += sigma_pwx_dev[index]*lambda_d*gradw1_dev[index];
pwv += sigma_pwy_dev[index]*lambda_d*gradw2_dev[index];
//Constrain pd
module_p = fabsf(pd);
if (module_p > 1.f)
{
if (pd > 1.f)
pd_dev[index] = 1.f;
else
pd_dev[index] = -1.f;
}
else
pd_dev[index] = pd;
//Constrain pu
module_p = rhypotf(puu, puv); //1.f/sqrtf(puu*puu + puv*puv);
if (module_p < 1.f)
{
puu_dev[index] = puu*module_p;
puv_dev[index] = puv*module_p;
}
else
{
puu_dev[index] = puu;
puv_dev[index] = puv;
}
//Constrain pv
module_p = rhypotf(pvu, pvv); //1.f/sqrtf(pvu*pvu + pvv*pvv);
if (module_p < 1.f)
{
pvu_dev[index] = pvu*module_p;
pvv_dev[index] = pvv*module_p;
}
else
{
pvu_dev[index] = pvu;
pvv_dev[index] = pvv;
}
//Constrain pw
module_p = rhypotf(pwu, pwv); //1.f/sqrt(pwu*pwu + pwv*pwv);
if (module_p < 1.f)
{
pwu_dev[index] = pwu*module_p;
pwv_dev[index] = pwv*module_p;
}
else
{
pwu_dev[index] = pwu;
pwv_dev[index] = pwv;
}
}
__device__ void CSF_cuda::updatePrimalVariables(unsigned int index)
{
float du = du_new_dev[index], dv = dv_new_dev[index], dw = dw_new_dev[index];
const float du_old = du, dv_old = dv, dw_old = dw;
//Compute du, dv and dw
//Solve du
du += - tau_u_dev[index]*(mu_uv_dev[index]*ddu_dev[index]*pd_dev[index] - lambda_i*divpu_dev[index]);
//Solve dv
dv += - tau_v_dev[index]*(mu_uv_dev[index]*ddv_dev[index]*pd_dev[index] - lambda_i*divpv_dev[index]);
//Solve dw
dw += - tau_w_dev[index]*(-mu_uv_dev[index]*pd_dev[index] - lambda_d*divpw_dev[index]);
//shrink du, dv and dw
//-----------------------------------------------------------
const float optflow = dct_dev[index] + dcu_dev[index]*du + dcv_dev[index]*dv;
const float of_threshold = tau_u_dev[index]*dcu_dev[index]*dcu_dev[index] + tau_v_dev[index]*dcv_dev[index]*dcv_dev[index];
if (optflow < -of_threshold)
{
du += tau_u_dev[index]*dcu_dev[index];
dv += tau_v_dev[index]*dcv_dev[index];
}
else if (optflow > of_threshold)
{
du -= tau_u_dev[index]*dcu_dev[index];
dv -= tau_v_dev[index]*dcv_dev[index];
}
else
{
const float den = tau_u_dev[index]*dcu_dev[index]*dcu_dev[index] + tau_v_dev[index]*dcv_dev[index]*dcv_dev[index] + 1e-10f;
du -= tau_u_dev[index]*dcu_dev[index]*optflow/den;
dv -= tau_v_dev[index]*dcv_dev[index]*optflow/den;
}
//Update du, dv
du_acc_dev[index] = 2.f*du - du_old;
dv_acc_dev[index] = 2.f*dv - dv_old;
dw_acc_dev[index] = 2.f*dw - dw_old;
du_new_dev[index] = du;
dv_new_dev[index] = dv;
dw_new_dev[index] = dw;
}
__device__ void CSF_cuda::computeDivergence(unsigned int index)
{
//Calculate (v,u)
const unsigned int v = index%rows_i;
const unsigned int u = index/rows_i;
//First terms
if (u == 0)
{
divpu_dev[index] = ri_dev[index]*puu_dev[index];
divpv_dev[index] = ri_dev[index]*pvu_dev[index];
divpw_dev[index] = ri_dev[index]*pwu_dev[index];
}
else if (u == cols_i-1)
{
divpu_dev[index] = -ri_dev[index-rows_i]*puu_dev[index-rows_i];
divpv_dev[index] = -ri_dev[index-rows_i]*pvu_dev[index-rows_i];
divpw_dev[index] = -ri_dev[index-rows_i]*pwu_dev[index-rows_i];
}
else
{
divpu_dev[index] = ri_dev[index]*puu_dev[index] - ri_dev[index-rows_i]*puu_dev[index-rows_i];
divpv_dev[index] = ri_dev[index]*pvu_dev[index] - ri_dev[index-rows_i]*pvu_dev[index-rows_i];
divpw_dev[index] = ri_dev[index]*pwu_dev[index] - ri_dev[index-rows_i]*pwu_dev[index-rows_i];
}
//Second term
if (v == 0)
{
divpu_dev[index] += rj_dev[index]*puv_dev[index];
divpv_dev[index] += rj_dev[index]*pvv_dev[index];
divpw_dev[index] += rj_dev[index]*pwv_dev[index];
}
else if (v == rows_i-1)
{
divpu_dev[index] += -rj_dev[index-1]*puv_dev[index-1];
divpv_dev[index] += -rj_dev[index-1]*pvv_dev[index-1];
divpw_dev[index] += -rj_dev[index-1]*pwv_dev[index-1];
}
else
{
divpu_dev[index] += rj_dev[index]*puv_dev[index] - rj_dev[index-1]*puv_dev[index-1];
divpv_dev[index] += rj_dev[index]*pvv_dev[index] - rj_dev[index-1]*pvv_dev[index-1];
divpw_dev[index] += rj_dev[index]*pwv_dev[index] - rj_dev[index-1]*pwv_dev[index-1];
}
}
__device__ void CSF_cuda::computeGradient(unsigned int index)
{
//Calculate (v,u)
const unsigned int v = index%rows_i;
const unsigned int u = index/rows_i;
if (u == cols_i-1)
{
gradu1_dev[index] = 0.f;
gradv1_dev[index] = 0.f;
gradw1_dev[index] = 0.f;
}
else
{
gradu1_dev[index] = ri_dev[index]*((du_acc_dev[index+rows_i] + du_prev_dev[index+rows_i]) - (du_acc_dev[index] + du_prev_dev[index]));
gradv1_dev[index] = ri_dev[index]*((dv_acc_dev[index+rows_i] + dv_prev_dev[index+rows_i]) - (dv_acc_dev[index] + dv_prev_dev[index]));
gradw1_dev[index] = ri_dev[index]*(dw_acc_dev[index+rows_i] - dw_acc_dev[index]);
}