-
Notifications
You must be signed in to change notification settings - Fork 4
/
nufft_plan.cu.cc
2798 lines (2497 loc) · 111 KB
/
nufft_plan.cu.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright 2021 University College London. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE - 2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
/* Copyright 2017 - 2021 The Simons Foundation. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE - 2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#if GOOGLE_CUDA
#include "tensorflow_nufft/cc/kernels/nufft_plan.h"
#include <thrust/device_ptr.h>
#include <thrust/scan.h>
#include "tensorflow/core/platform/stream_executor.h"
#include "tensorflow/core/util/gpu_device_functions.h"
#include "tensorflow/core/util/gpu_kernel_helper.h"
#include "tensorflow_nufft/cc/kernels/nufft_util.h"
#include "tensorflow_nufft/cc/kernels/omp_api.h"
// NU coord handling macro: if p is true, rescales from [-pi, pi] to [0, N],
// then folds *only* one period below and above, ie [-N, 2N], into the domain
// [0, N]...
#define RESCALE(x, N, p) (p ? \
((x * kOneOverTwoPi<FloatType> + (x < -kPi<FloatType> ? 1.5 : \
(x >= kPi<FloatType> ? -0.5 : 0.5))) * N) : \
(x < 0 ? x + N : (x >= N ? x - N : x)))
namespace tensorflow {
namespace nufft {
namespace {
template<typename FloatType>
using GpuComplex = typename ComplexType<GPUDevice, FloatType>::Type;
template<typename FloatType>
constexpr cufftType kCufftType = CUFFT_C2C;
template<>
constexpr cufftType kCufftType<float> = CUFFT_C2C;
template<>
constexpr cufftType kCufftType<double> = CUFFT_Z2Z;
template<typename FloatType>
cufftResult cufftExec(
cufftHandle plan, GpuComplex<FloatType> *idata,
GpuComplex<FloatType> *odata, int direction);
template<>
cufftResult cufftExec<float>(
cufftHandle plan, GpuComplex<float> *idata,
GpuComplex<float> *odata, int direction) {
return cufftExecC2C(plan, idata, odata, direction);
}
template<>
cufftResult cufftExec<double>(
cufftHandle plan, GpuComplex<double> *idata,
GpuComplex<double> *odata, int direction) {
return cufftExecZ2Z(plan, idata, odata, direction);
}
template<typename FloatType>
Status setup_spreader(int rank, FloatType eps, double upsampling_factor,
KernelEvaluationMethod kernel_evaluation_method,
SpreadParameters<FloatType>& spread_params);
template<typename FloatType>
Status setup_spreader_for_nufft(int rank, FloatType eps,
const Options& options,
SpreadParameters<FloatType> &spread_params);
void set_bin_sizes(TransformType type, int rank, Options& options);
template<typename FloatType>
Status set_grid_size(int ms,
int bin_size,
const Options& options,
const SpreadParameters<FloatType>& spread_params,
int* grid_size);
__device__ int CalcGlobalIdxV2(int xidx, int yidx, int zidx, int nbinx, int nbiny, int nbinz) {
return xidx + yidx * nbinx + zidx * nbinx * nbiny;
}
template<typename FloatType>
__global__ void CalcBinSizeNoGhost2DKernel(int M, int nf1, int nf2, int bin_size_x,
int bin_size_y, int nbinx, int nbiny, int* bin_sizes, FloatType *x, FloatType *y,
int* sortidx, int pirange) {
int binidx, binx, biny;
int oldidx;
FloatType x_rescaled, y_rescaled;
for (int i = threadIdx.x + blockIdx.x * blockDim.x; i<M; i += gridDim.x * blockDim.x) {
x_rescaled = RESCALE(x[i], nf1, pirange);
y_rescaled = RESCALE(y[i], nf2, pirange);
binx = floor(x_rescaled / bin_size_x);
binx = binx >= nbinx ? binx - 1 : binx;
binx = binx < 0 ? 0 : binx;
biny = floor(y_rescaled / bin_size_y);
biny = biny >= nbiny ? biny - 1 : biny;
biny = biny < 0 ? 0 : biny;
binidx = binx + biny * nbinx;
oldidx = GpuAtomicAdd(&bin_sizes[binidx], 1);
sortidx[i] = oldidx;
if (binx >= nbinx || biny >= nbiny) {
sortidx[i] = -biny;
}
}
}
template<typename FloatType>
__global__ void CalcBinSizeNoGhost3DKernel(int M, int nf1, int nf2, int nf3,
int bin_size_x, int bin_size_y, int bin_size_z,
int nbinx, int nbiny, int nbinz, int* bin_sizes, FloatType *x, FloatType *y, FloatType *z,
int* sortidx, int pirange) {
int binidx, binx, biny, binz;
int oldidx;
FloatType x_rescaled, y_rescaled, z_rescaled;
for (int i = threadIdx.x + blockIdx.x * blockDim.x; i<M; i += gridDim.x * blockDim.x) {
x_rescaled = RESCALE(x[i], nf1, pirange);
y_rescaled = RESCALE(y[i], nf2, pirange);
z_rescaled = RESCALE(z[i], nf3, pirange);
binx = floor(x_rescaled / bin_size_x);
binx = binx >= nbinx ? binx - 1 : binx;
binx = binx < 0 ? 0 : binx;
biny = floor(y_rescaled / bin_size_y);
biny = biny >= nbiny ? biny - 1 : biny;
biny = biny < 0 ? 0 : biny;
binz = floor(z_rescaled / bin_size_z);
binz = binz >= nbinz ? binz - 1 : binz;
binz = binz < 0 ? 0 : binz;
binidx = binx + biny * nbinx + binz * nbinx * nbiny;
oldidx = GpuAtomicAdd(&bin_sizes[binidx], 1);
sortidx[i] = oldidx;
}
}
template<typename FloatType>
__global__ void CalcInvertofGlobalSortIdx2DKernel(int M, int bin_size_x, int bin_size_y,
int nbinx, int nbiny, int* bin_startpts, int* sortidx, FloatType *x, FloatType *y,
int* index, int pirange, int nf1, int nf2) {
int binx, biny;
int binidx;
FloatType x_rescaled, y_rescaled;
for (int i = threadIdx.x + blockIdx.x * blockDim.x; i<M; i += gridDim.x * blockDim.x) {
x_rescaled = RESCALE(x[i], nf1, pirange);
y_rescaled = RESCALE(y[i], nf2, pirange);
binx = floor(x_rescaled / bin_size_x);
binx = binx >= nbinx ? binx - 1 : binx;
binx = binx < 0 ? 0 : binx;
biny = floor(y_rescaled / bin_size_y);
biny = biny >= nbiny ? biny - 1 : biny;
biny = biny < 0 ? 0 : biny;
binidx = binx + biny * nbinx;
index[bin_startpts[binidx]+sortidx[i]] = i;
}
}
template<typename FloatType>
__global__ void CalcInvertofGlobalSortIdx3DKernel(int M, int bin_size_x, int bin_size_y,
int bin_size_z, int nbinx, int nbiny, int nbinz, int* bin_startpts,
int* sortidx, FloatType *x, FloatType *y, FloatType *z, int* index, int pirange, int nf1,
int nf2, int nf3) {
int binx, biny, binz;
int binidx;
FloatType x_rescaled, y_rescaled, z_rescaled;
for (int i = threadIdx.x + blockIdx.x * blockDim.x; i<M; i += gridDim.x * blockDim.x) {
x_rescaled = RESCALE(x[i], nf1, pirange);
y_rescaled = RESCALE(y[i], nf2, pirange);
z_rescaled = RESCALE(z[i], nf3, pirange);
binx = floor(x_rescaled / bin_size_x);
binx = binx >= nbinx ? binx - 1 : binx;
binx = binx < 0 ? 0 : binx;
biny = floor(y_rescaled / bin_size_y);
biny = biny >= nbiny ? biny - 1 : biny;
biny = biny < 0 ? 0 : biny;
binz = floor(z_rescaled / bin_size_z);
binz = binz >= nbinz ? binz - 1 : binz;
binz = binz < 0 ? 0 : binz;
binidx = CalcGlobalIdxV2(binx, biny, binz, nbinx, nbiny, nbinz);
index[bin_startpts[binidx]+sortidx[i]] = i;
}
}
__global__ void TrivialGlobalSortIdxKernel(int M, int* index) {
for (int i = threadIdx.x + blockIdx.x * blockDim.x; i<M; i += gridDim.x * blockDim.x) {
index[i] = i;
}
}
__global__ void CalcSubproblemKernel(int* bin_sizes, int* num_subprob, int max_subprob_size,
int numbins) {
for (int i = threadIdx.x + blockIdx.x * blockDim.x; i<numbins;
i += gridDim.x * blockDim.x) {
num_subprob[i]=ceil(bin_sizes[i]/(float) max_subprob_size);
}
}
__global__ void MapBinToSubproblemKernel(
int* subprob_bins, int* subprob_start_pts, int* num_subprob, int numbins) {
for (int i = threadIdx.x + blockIdx.x * blockDim.x; i < numbins;
i += gridDim.x * blockDim.x) {
for (int j = 0; j < num_subprob[i]; j++) {
subprob_bins[subprob_start_pts[i] + j] = i;
}
}
}
/* Kernel for copying fw to fk with amplication by prefac / ker */
// Note: assume modeord = 0: CMCL - compatible mode ordering in fk (from -N / 2 up
// to N / 2 - 1)
template<typename FloatType>
__global__ void Deconvolve2DKernel(
int ms, int mt, int nf1, int nf2, GpuComplex<FloatType>* fw, GpuComplex<FloatType> *fk,
FloatType *fwkerhalf1, FloatType *fwkerhalf2)
{
for (int i = blockDim.x * blockIdx.x + threadIdx.x; i<ms * mt; i += blockDim.x * gridDim.x) {
int k1 = i % ms;
int k2 = i / ms;
int outidx = k1 + k2 * ms;
int w1 = k1 - ms / 2 >= 0 ? k1 - ms / 2 : nf1 + k1 - ms / 2;
int w2 = k2 - mt / 2 >= 0 ? k2 - mt / 2 : nf2 + k2 - mt / 2;
int inidx = w1 + w2 * nf1;
FloatType kervalue = fwkerhalf1[abs(k1 - ms / 2)]*fwkerhalf2[abs(k2 - mt / 2)];
fk[outidx].x = fw[inidx].x / kervalue;
fk[outidx].y = fw[inidx].y / kervalue;
}
}
template<typename FloatType>
__global__ void Deconvolve3DKernel(
int ms, int mt, int mu, int nf1, int nf2, int nf3, GpuComplex<FloatType>* fw,
GpuComplex<FloatType> *fk, FloatType *fwkerhalf1, FloatType *fwkerhalf2, FloatType *fwkerhalf3)
{
for (int i = blockDim.x * blockIdx.x + threadIdx.x; i<ms * mt * mu; i += blockDim.x*
gridDim.x) {
int k1 = i % ms;
int k2 = (i / ms) % mt;
int k3 = (i / ms / mt);
int outidx = k1 + k2 * ms + k3 * ms * mt;
int w1 = k1 - ms / 2 >= 0 ? k1 - ms / 2 : nf1 + k1 - ms / 2;
int w2 = k2 - mt / 2 >= 0 ? k2 - mt / 2 : nf2 + k2 - mt / 2;
int w3 = k3 - mu / 2 >= 0 ? k3 - mu / 2 : nf3 + k3 - mu / 2;
int inidx = w1 + w2 * nf1 + w3 * nf1 * nf2;
FloatType kervalue = fwkerhalf1[abs(k1 - ms / 2)]*fwkerhalf2[abs(k2 - mt / 2)]*
fwkerhalf3[abs(k3 - mu / 2)];
fk[outidx].x = fw[inidx].x / kervalue;
fk[outidx].y = fw[inidx].y / kervalue;
//fk[outidx].x = kervalue;
//fk[outidx].y = kervalue;
}
}
/* Kernel for copying fk to fw with same amplication */
template<typename FloatType>
__global__ void Amplify2DKernel(
int ms, int mt, int nf1, int nf2, GpuComplex<FloatType>* fw, GpuComplex<FloatType> *fk,
FloatType *fwkerhalf1, FloatType *fwkerhalf2)
{
for (int i = blockDim.x * blockIdx.x + threadIdx.x; i < ms * mt; i += blockDim.x * gridDim.x) {
int k1 = i % ms;
int k2 = i / ms;
int inidx = k1 + k2 * ms;
int w1 = k1 - ms / 2 >= 0 ? k1 - ms / 2 : nf1 + k1 - ms / 2;
int w2 = k2 - mt / 2 >= 0 ? k2 - mt / 2 : nf2 + k2 - mt / 2;
int outidx = w1 + w2 * nf1;
FloatType kervalue = fwkerhalf1[abs(k1 - ms / 2)]*fwkerhalf2[abs(k2 - mt / 2)];
fw[outidx].x = fk[inidx].x / kervalue;
fw[outidx].y = fk[inidx].y / kervalue;
}
}
template<typename FloatType>
__global__ void Amplify3DKernel(
int ms, int mt, int mu, int nf1, int nf2, int nf3, GpuComplex<FloatType>* fw,
GpuComplex<FloatType> *fk, FloatType *fwkerhalf1, FloatType *fwkerhalf2, FloatType *fwkerhalf3)
{
for (int i = blockDim.x * blockIdx.x + threadIdx.x; i < ms * mt * mu;
i += blockDim.x * gridDim.x) {
int k1 = i % ms;
int k2 = (i / ms) % mt;
int k3 = (i / ms / mt);
int inidx = k1 + k2 * ms + k3 * ms * mt;
int w1 = k1 - ms / 2 >= 0 ? k1 - ms / 2 : nf1 + k1 - ms / 2;
int w2 = k2 - mt / 2 >= 0 ? k2 - mt / 2 : nf2 + k2 - mt / 2;
int w3 = k3 - mu / 2 >= 0 ? k3 - mu / 2 : nf3 + k3 - mu / 2;
int outidx = w1 + w2 * nf1 + w3 * nf1 * nf2;
FloatType kervalue = fwkerhalf1[abs(k1 - ms / 2)]*fwkerhalf2[abs(k2 - mt / 2)]*
fwkerhalf3[abs(k3 - mu / 2)];
fw[outidx].x = fk[inidx].x / kervalue;
fw[outidx].y = fk[inidx].y / kervalue;
}
}
/* ES ("exp sqrt") kernel evaluation at single real argument:
phi(x) = exp(beta.sqrt(1 - (2x / n_s)^2)), for |x| < kernel_width / 2
related to an asymptotic approximation to the Kaiser--Bessel, itself an
approximation to prolate spheroidal wavefunction (PSWF) of order 0.
This is the "reference implementation", used by eg common / onedim_*
2 / 17 / 17 */
template<typename FloatType>
static __forceinline__ __device__ FloatType EvaluateKernel(
FloatType x, FloatType es_c, FloatType es_beta, int ns) {
return abs(x) < ns / 2.0 ? exp(es_beta * (sqrt(1.0 - es_c * x * x))) : 0.0;
}
// Fill ker[] with Horner piecewise poly approx to [-w / 2, w / 2] ES kernel eval at
// x_j = x + j, for j = 0,..,w - 1. Thus x in [-w / 2,-w / 2 + 1]. w is aka ns.
// This is the current evaluation method, since it's faster (except i7 w = 16).
// Two upsampfacs implemented. Params must match ref formula. Barnett 4 / 24 / 18
template<typename FloatType>
static __inline__ __device__ void EvaluateKernelVectorHorner(
FloatType *ker, const FloatType x, const int w,
const double upsampling_factor) {
FloatType z = 2 * x + w - 1.0; // scale so local grid offset z in [-1, 1]
// insert the auto - generated code which expects z, w args, writes to ker...
if (upsampling_factor == 2.0) { // floating point equality is fine here
#include "tensorflow_nufft/cc/kernels/kernel_horner_sigma2.inc"
}
}
template<typename FloatType>
static __inline__ __device__ void EvaluateKernelVector(
FloatType *ker, const FloatType x, const double w, const double es_c,
const double es_beta) {
for (int i = 0; i < w; i++) {
ker[i] = EvaluateKernel<FloatType>(abs(x + i), es_c, es_beta, w);
}
}
template<typename FloatType>
__global__ void SpreadNuptsDriven2DKernel(
FloatType *x, FloatType *y, GpuComplex<FloatType> *c,
GpuComplex<FloatType> *fw, int M, const int ns, int nf1, int nf2,
FloatType es_c, FloatType es_beta, int *idxnupts, int pirange) {
int xstart, ystart, xend, yend;
int xx, yy, ix, iy;
int outidx;
FloatType ker1[kMaxKernelWidth];
FloatType ker2[kMaxKernelWidth];
FloatType x_rescaled, y_rescaled;
FloatType kervalue1, kervalue2;
GpuComplex<FloatType> cnow;
for (int i = blockDim.x * blockIdx.x + threadIdx.x; i < M; i += blockDim.x * gridDim.x) {
x_rescaled = RESCALE(x[idxnupts[i]], nf1, pirange);
y_rescaled = RESCALE(y[idxnupts[i]], nf2, pirange);
cnow = c[idxnupts[i]];
xstart = ceil(x_rescaled - ns / 2.0);
ystart = ceil(y_rescaled - ns / 2.0);
xend = floor(x_rescaled + ns / 2.0);
yend = floor(y_rescaled + ns / 2.0);
FloatType x1 = (FloatType)xstart - x_rescaled;
FloatType y1 = (FloatType)ystart - y_rescaled;
EvaluateKernelVector(ker1, x1, ns, es_c, es_beta);
EvaluateKernelVector(ker2, y1, ns, es_c, es_beta);
for (yy = ystart; yy<=yend; yy++) {
for (xx = xstart; xx<=xend; xx++) {
ix = xx < 0 ? xx + nf1 : (xx > nf1 - 1 ? xx - nf1 : xx);
iy = yy < 0 ? yy + nf2 : (yy > nf2 - 1 ? yy - nf2 : yy);
outidx = ix + iy * nf1;
kervalue1 = ker1[xx - xstart];
kervalue2 = ker2[yy - ystart];
GpuAtomicAdd(&fw[outidx].x, cnow.x * kervalue1 * kervalue2);
GpuAtomicAdd(&fw[outidx].y, cnow.y * kervalue1 * kervalue2);
}
}
}
}
template<typename FloatType>
__global__ void SpreadNuptsDrivenHorner2DKernel(
FloatType *x, FloatType *y, GpuComplex<FloatType> *c,
GpuComplex<FloatType> *fw, int M, const int ns, int nf1, int nf2,
FloatType sigma, int* idxnupts, int pirange) {
int xx, yy, ix, iy;
int outidx;
FloatType ker1[kMaxKernelWidth];
FloatType ker2[kMaxKernelWidth];
FloatType ker1val, ker2val;
FloatType x_rescaled, y_rescaled;
GpuComplex<FloatType> cnow;
for (int i = blockDim.x * blockIdx.x + threadIdx.x; i<M; i += blockDim.x * gridDim.x) {
x_rescaled = RESCALE(x[idxnupts[i]], nf1, pirange);
y_rescaled = RESCALE(y[idxnupts[i]], nf2, pirange);
cnow = c[idxnupts[i]];
int xstart = ceil(x_rescaled - ns / 2.0);
int ystart = ceil(y_rescaled - ns / 2.0);
int xend = floor(x_rescaled + ns / 2.0);
int yend = floor(y_rescaled + ns / 2.0);
FloatType x1 = (FloatType)xstart - x_rescaled;
FloatType y1 = (FloatType)ystart - y_rescaled;
EvaluateKernelVectorHorner(ker1, x1, ns, sigma);
EvaluateKernelVectorHorner(ker2, y1, ns, sigma);
for (yy = ystart; yy<=yend; yy++) {
for (xx = xstart; xx<=xend; xx++) {
ix = xx < 0 ? xx + nf1 : (xx > nf1 - 1 ? xx - nf1 : xx);
iy = yy < 0 ? yy + nf2 : (yy > nf2 - 1 ? yy - nf2 : yy);
outidx = ix + iy * nf1;
ker1val = ker1[xx - xstart];
ker2val = ker2[yy - ystart];
FloatType kervalue = ker1val * ker2val;
GpuAtomicAdd(&fw[outidx].x, cnow.x * kervalue);
GpuAtomicAdd(&fw[outidx].y, cnow.y * kervalue);
}
}
}
}
template<typename FloatType>
__global__ void SpreadSubproblem2DKernel(
FloatType *x, FloatType *y, GpuComplex<FloatType> *c,
GpuComplex<FloatType> *fw, int M, const int ns, int nf1, int nf2,
FloatType es_c, FloatType es_beta, FloatType sigma, int* binstartpts,
int* bin_sizes, int bin_size_x, int bin_size_y, int* subprob_bins,
int* subprob_start_pts, int* num_subprob, int max_subprob_size, int nbinx,
int nbiny, int* idxnupts, int pirange) {
// Shared memory pointers cannot be declared with a type template because
// it results in a "declaration is incompatible with previous declaration"
// error. To get around this issue, we declare the shared memory pointer as
// `unsigned char` and then cast it to the appropriate type. See also
// https://stackoverflow.com / a/27570775 / 9406746
// Note: `nvcc` emits a warning warning for this code: "#1886 - D: specified
// alignment (16) is different from alignment (8) specified on a previous
// declaration". This can be safely ignored and is disabled in the Makefile.
extern __shared__ __align__(sizeof(GpuComplex<FloatType>)) unsigned char fwshared_[];
GpuComplex<FloatType> *fwshared = reinterpret_cast<GpuComplex<FloatType>*>(fwshared_);
int xstart, ystart, xend, yend;
int subpidx = blockIdx.x;
int bidx = subprob_bins[subpidx];
int binsubp_idx = subpidx - subprob_start_pts[bidx];
int ix, iy;
int outidx;
int ptstart = binstartpts[bidx]+binsubp_idx * max_subprob_size;
int nupts = min(max_subprob_size, bin_sizes[bidx]-binsubp_idx * max_subprob_size);
int xoffset = (bidx % nbinx) * bin_size_x;
int yoffset = (bidx / nbinx) * bin_size_y;
int N = (bin_size_x + 2 * ceil(ns / 2.0)) * (bin_size_y + 2 * ceil(ns / 2.0));
FloatType ker1[kMaxKernelWidth];
FloatType ker2[kMaxKernelWidth];
for (int i = threadIdx.x; i<N; i += blockDim.x) {
fwshared[i].x = 0.0;
fwshared[i].y = 0.0;
}
__syncthreads();
FloatType x_rescaled, y_rescaled;
GpuComplex<FloatType> cnow;
for (int i = threadIdx.x; i<nupts; i += blockDim.x) {
int idx = ptstart + i;
x_rescaled = RESCALE(x[idxnupts[idx]], nf1, pirange);
y_rescaled = RESCALE(y[idxnupts[idx]], nf2, pirange);
cnow = c[idxnupts[idx]];
xstart = ceil(x_rescaled - ns / 2.0) - xoffset;
ystart = ceil(y_rescaled - ns / 2.0) - yoffset;
xend = floor(x_rescaled + ns / 2.0) - xoffset;
yend = floor(y_rescaled + ns / 2.0) - yoffset;
FloatType x1 = (FloatType)xstart + xoffset - x_rescaled;
FloatType y1 = (FloatType)ystart + yoffset - y_rescaled;
EvaluateKernelVector(ker1, x1, ns, es_c, es_beta);
EvaluateKernelVector(ker2, y1, ns, es_c, es_beta);
for (int yy = ystart; yy<=yend; yy++) {
iy = yy + ceil(ns / 2.0);
if (iy >= (bin_size_y + (int) ceil(ns / 2.0) * 2) || iy<0) break;
for (int xx = xstart; xx<=xend; xx++) {
ix = xx + ceil(ns / 2.0);
if (ix >= (bin_size_x + (int) ceil(ns / 2.0) * 2) || ix<0) break;
outidx = ix + iy * (bin_size_x + ceil(ns / 2.0) * 2);
FloatType kervalue1 = ker1[xx - xstart];
FloatType kervalue2 = ker2[yy - ystart];
GpuAtomicAdd(&fwshared[outidx].x, cnow.x * kervalue1 * kervalue2);
GpuAtomicAdd(&fwshared[outidx].y, cnow.y * kervalue1 * kervalue2);
}
}
}
__syncthreads();
/* write to global memory */
for (int k = threadIdx.x; k<N; k += blockDim.x) {
int i = k % (int) (bin_size_x + 2 * ceil(ns / 2.0) );
int j = k /( bin_size_x + 2 * ceil(ns / 2.0) );
ix = xoffset - ceil(ns / 2.0) + i;
iy = yoffset - ceil(ns / 2.0) + j;
if (ix < (nf1 + ceil(ns / 2.0)) && iy < (nf2 + ceil(ns / 2.0))) {
ix = ix < 0 ? ix + nf1 : (ix > nf1 - 1 ? ix - nf1 : ix);
iy = iy < 0 ? iy + nf2 : (iy > nf2 - 1 ? iy - nf2 : iy);
outidx = ix + iy * nf1;
int sharedidx = i + j * (bin_size_x + ceil(ns / 2.0) * 2);
GpuAtomicAdd(&fw[outidx].x, fwshared[sharedidx].x);
GpuAtomicAdd(&fw[outidx].y, fwshared[sharedidx].y);
}
}
}
template<typename FloatType>
__global__ void SpreadSubproblemHorner2DKernel(
FloatType *x, FloatType *y, GpuComplex<FloatType> *c,
GpuComplex<FloatType> *fw, int M, const int ns, int nf1, int nf2,
FloatType sigma, int* binstartpts, int* bin_sizes, int bin_size_x,
int bin_size_y, int* subprob_bins, int* subprob_start_pts, int* num_subprob,
int max_subprob_size, int nbinx, int nbiny, int* idxnupts, int pirange) {
extern __shared__ __align__(sizeof(GpuComplex<FloatType>)) unsigned char fwshared_[];
GpuComplex<FloatType> *fwshared = reinterpret_cast<GpuComplex<FloatType>*>(fwshared_);
int xstart, ystart, xend, yend;
int subpidx = blockIdx.x;
int bidx = subprob_bins[subpidx];
int binsubp_idx = subpidx - subprob_start_pts[bidx];
int ix, iy, outidx;
int ptstart = binstartpts[bidx]+binsubp_idx * max_subprob_size;
int nupts = min(max_subprob_size, bin_sizes[bidx]-binsubp_idx * max_subprob_size);
int xoffset = (bidx % nbinx) * bin_size_x;
int yoffset = (bidx / nbinx) * bin_size_y;
int N = (bin_size_x + 2 * ceil(ns / 2.0)) * (bin_size_y + 2 * ceil(ns / 2.0));
FloatType ker1[kMaxKernelWidth];
FloatType ker2[kMaxKernelWidth];
for (int i = threadIdx.x; i<N; i += blockDim.x) {
fwshared[i].x = 0.0;
fwshared[i].y = 0.0;
}
__syncthreads();
FloatType x_rescaled, y_rescaled;
GpuComplex<FloatType> cnow;
for (int i = threadIdx.x; i<nupts; i += blockDim.x) {
int idx = ptstart + i;
x_rescaled = RESCALE(x[idxnupts[idx]], nf1, pirange);
y_rescaled = RESCALE(y[idxnupts[idx]], nf2, pirange);
cnow = c[idxnupts[idx]];
xstart = ceil(x_rescaled - ns / 2.0) - xoffset;
ystart = ceil(y_rescaled - ns / 2.0) - yoffset;
xend = floor(x_rescaled + ns / 2.0) - xoffset;
yend = floor(y_rescaled + ns / 2.0) - yoffset;
EvaluateKernelVectorHorner(ker1, xstart + xoffset - x_rescaled, ns, sigma);
EvaluateKernelVectorHorner(ker2, ystart + yoffset - y_rescaled, ns, sigma);
for (int yy = ystart; yy<=yend; yy++) {
iy = yy + ceil(ns / 2.0);
if (iy >= (bin_size_y + (int) ceil(ns / 2.0) * 2) || iy<0) break;
FloatType kervalue2 = ker2[yy - ystart];
for (int xx = xstart; xx<=xend; xx++) {
ix = xx + ceil(ns / 2.0);
if (ix >= (bin_size_x + (int) ceil(ns / 2.0) * 2) || ix<0) break;
outidx = ix + iy * (bin_size_x+ (int) ceil(ns / 2.0) * 2);
FloatType kervalue1 = ker1[xx - xstart];
GpuAtomicAdd(&fwshared[outidx].x, cnow.x * kervalue1 * kervalue2);
GpuAtomicAdd(&fwshared[outidx].y, cnow.y * kervalue1 * kervalue2);
}
}
}
__syncthreads();
/* write to global memory */
for (int k = threadIdx.x; k<N; k += blockDim.x) {
int i = k % (int) (bin_size_x + 2 * ceil(ns / 2.0) );
int j = k /( bin_size_x + 2 * ceil(ns / 2.0) );
ix = xoffset - ceil(ns / 2.0) + i;
iy = yoffset - ceil(ns / 2.0) + j;
if (ix < (nf1 + ceil(ns / 2.0)) && iy < (nf2 + ceil(ns / 2.0))) {
ix = ix < 0 ? ix + nf1 : (ix > nf1 - 1 ? ix - nf1 : ix);
iy = iy < 0 ? iy + nf2 : (iy > nf2 - 1 ? iy - nf2 : iy);
outidx = ix + iy * nf1;
int sharedidx = i + j * (bin_size_x + ceil(ns / 2.0) * 2);
GpuAtomicAdd(&fw[outidx].x, fwshared[sharedidx].x);
GpuAtomicAdd(&fw[outidx].y, fwshared[sharedidx].y);
}
}
}
template<typename FloatType>
__global__ void InterpNuptsDriven2DKernel(
FloatType *x, FloatType *y, GpuComplex<FloatType> *c,
GpuComplex<FloatType> *fw, int M, const int ns, int nf1, int nf2,
FloatType es_c, FloatType es_beta, int* idxnupts, int pirange) {
for (int i = blockDim.x * blockIdx.x + threadIdx.x; i<M; i += blockDim.x * gridDim.x) {
FloatType x_rescaled = RESCALE(x[idxnupts[i]], nf1, pirange);
FloatType y_rescaled = RESCALE(y[idxnupts[i]], nf2, pirange);
int xstart = ceil(x_rescaled - ns / 2.0);
int ystart = ceil(y_rescaled - ns / 2.0);
int xend = floor(x_rescaled + ns / 2.0);
int yend = floor(y_rescaled + ns / 2.0);
GpuComplex<FloatType> cnow;
cnow.x = 0.0;
cnow.y = 0.0;
for (int yy = ystart; yy<=yend; yy++) {
FloatType disy = abs(y_rescaled - yy);
FloatType kervalue2 = EvaluateKernel(disy, es_c, es_beta, ns);
for (int xx = xstart; xx<=xend; xx++) {
int ix = xx < 0 ? xx + nf1 : (xx > nf1 - 1 ? xx - nf1 : xx);
int iy = yy < 0 ? yy + nf2 : (yy > nf2 - 1 ? yy - nf2 : yy);
int inidx = ix + iy * nf1;
FloatType disx = abs(x_rescaled - xx);
FloatType kervalue1 = EvaluateKernel(disx, es_c, es_beta, ns);
cnow.x += fw[inidx].x * kervalue1 * kervalue2;
cnow.y += fw[inidx].y * kervalue1 * kervalue2;
}
}
c[idxnupts[i]].x = cnow.x;
c[idxnupts[i]].y = cnow.y;
}
}
template<typename FloatType>
__global__ void InterpNuptsDrivenHorner2DKernel(
FloatType *x, FloatType *y, GpuComplex<FloatType> *c,
GpuComplex<FloatType> *fw, int M, const int ns, int nf1, int nf2,
FloatType sigma, int* idxnupts, int pirange) {
for (int i = blockDim.x * blockIdx.x + threadIdx.x; i < M; i += blockDim.x * gridDim.x) {
FloatType x_rescaled = RESCALE(x[idxnupts[i]], nf1, pirange);
FloatType y_rescaled = RESCALE(y[idxnupts[i]], nf2, pirange);
int xstart = ceil(x_rescaled - ns / 2.0);
int ystart = ceil(y_rescaled - ns / 2.0);
int xend = floor(x_rescaled + ns / 2.0);
int yend = floor(y_rescaled + ns / 2.0);
GpuComplex<FloatType> cnow;
cnow.x = 0.0;
cnow.y = 0.0;
FloatType ker1[kMaxKernelWidth];
FloatType ker2[kMaxKernelWidth];
EvaluateKernelVectorHorner(ker1, xstart - x_rescaled, ns, sigma);
EvaluateKernelVectorHorner(ker2, ystart - y_rescaled, ns, sigma);
for (int yy = ystart; yy <= yend; yy++) {
FloatType disy = abs(y_rescaled - yy);
FloatType kervalue2 = ker2[yy - ystart];
for (int xx = xstart; xx<=xend; xx++) {
int ix = xx < 0 ? xx + nf1 : (xx > nf1 - 1 ? xx - nf1 : xx);
int iy = yy < 0 ? yy + nf2 : (yy > nf2 - 1 ? yy - nf2 : yy);
int inidx = ix + iy * nf1;
FloatType disx = abs(x_rescaled - xx);
FloatType kervalue1 = ker1[xx - xstart];
cnow.x += fw[inidx].x * kervalue1 * kervalue2;
cnow.y += fw[inidx].y * kervalue1 * kervalue2;
}
}
c[idxnupts[i]].x = cnow.x;
c[idxnupts[i]].y = cnow.y;
}
}
template<typename FloatType>
__global__ void InterpSubproblem2DKernel(
FloatType *x, FloatType *y, GpuComplex<FloatType> *c,
GpuComplex<FloatType> *fw, int M, const int ns, int nf1, int nf2,
FloatType es_c, FloatType es_beta, FloatType sigma, int* binstartpts,
int* bin_sizes, int bin_size_x, int bin_size_y, int* subprob_bins,
int* subprob_start_pts, int* num_subprob, int max_subprob_size, int nbinx,
int nbiny, int* idxnupts, int pirange) {
extern __shared__ __align__(sizeof(GpuComplex<FloatType>)) unsigned char fwshared_[];
GpuComplex<FloatType> *fwshared = reinterpret_cast<GpuComplex<FloatType>*>(fwshared_);
int xstart, ystart, xend, yend;
int subpidx = blockIdx.x;
int bidx = subprob_bins[subpidx];
int binsubp_idx = subpidx - subprob_start_pts[bidx];
int ix, iy;
int outidx;
int ptstart = binstartpts[bidx]+binsubp_idx * max_subprob_size;
int nupts = min(max_subprob_size, bin_sizes[bidx]-binsubp_idx * max_subprob_size);
int xoffset = (bidx % nbinx) * bin_size_x;
int yoffset = (bidx / nbinx) * bin_size_y;
int N = (bin_size_x + 2 * ceil(ns / 2.0)) * (bin_size_y + 2 * ceil(ns / 2.0));
for (int k = threadIdx.x;k<N; k += blockDim.x) {
int i = k % (int) (bin_size_x + 2 * ceil(ns / 2.0) );
int j = k /( bin_size_x + 2 * ceil(ns / 2.0) );
ix = xoffset - ceil(ns / 2.0) + i;
iy = yoffset - ceil(ns / 2.0) + j;
if (ix < (nf1 + ceil(ns / 2.0)) && iy < (nf2 + ceil(ns / 2.0))) {
ix = ix < 0 ? ix + nf1 : (ix > nf1 - 1 ? ix - nf1 : ix);
iy = iy < 0 ? iy + nf2 : (iy > nf2 - 1 ? iy - nf2 : iy);
outidx = ix + iy * nf1;
int sharedidx = i + j * (bin_size_x + ceil(ns / 2.0) * 2);
fwshared[sharedidx].x = fw[outidx].x;
fwshared[sharedidx].y = fw[outidx].y;
}
}
__syncthreads();
FloatType x_rescaled, y_rescaled;
GpuComplex<FloatType> cnow;
for (int i = threadIdx.x; i<nupts; i += blockDim.x) {
int idx = ptstart + i;
x_rescaled = RESCALE(x[idxnupts[idx]], nf1, pirange);
y_rescaled = RESCALE(y[idxnupts[idx]], nf2, pirange);
cnow.x = 0.0;
cnow.y = 0.0;
xstart = ceil(x_rescaled - ns / 2.0) - xoffset;
ystart = ceil(y_rescaled - ns / 2.0) - yoffset;
xend = floor(x_rescaled + ns / 2.0) - xoffset;
yend = floor(y_rescaled + ns / 2.0) - yoffset;
for (int yy = ystart; yy<=yend; yy++) {
FloatType disy = abs(y_rescaled - (yy + yoffset));
FloatType kervalue2 = EvaluateKernel(disy, es_c, es_beta, ns);
for (int xx = xstart; xx<=xend; xx++) {
ix = xx + ceil(ns / 2.0);
iy = yy + ceil(ns / 2.0);
outidx = ix + iy * (bin_size_x + ceil(ns / 2.0) * 2);
FloatType disx = abs(x_rescaled - (xx + xoffset));
FloatType kervalue1 = EvaluateKernel(disx, es_c, es_beta, ns);
cnow.x += fwshared[outidx].x * kervalue1 * kervalue2;
cnow.y += fwshared[outidx].y * kervalue1 * kervalue2;
}
}
c[idxnupts[idx]] = cnow;
}
}
template<typename FloatType>
__global__ void InterpSubproblemHorner2DKernel(
FloatType *x, FloatType *y, GpuComplex<FloatType> *c, GpuComplex<FloatType> *fw, int M,
const int ns, int nf1, int nf2, FloatType sigma, int* binstartpts, int* bin_sizes,
int bin_size_x, int bin_size_y, int* subprob_bins, int* subprob_start_pts,
int* num_subprob, int max_subprob_size, int nbinx, int nbiny, int* idxnupts,
int pirange) {
extern __shared__ __align__(sizeof(GpuComplex<FloatType>)) unsigned char fwshared_[];
GpuComplex<FloatType> *fwshared = reinterpret_cast<GpuComplex<FloatType>*>(fwshared_);
int xstart, ystart, xend, yend;
int subpidx = blockIdx.x;
int bidx = subprob_bins[subpidx];
int binsubp_idx = subpidx - subprob_start_pts[bidx];
int ix, iy;
int outidx;
int ptstart = binstartpts[bidx]+binsubp_idx * max_subprob_size;
int nupts = min(max_subprob_size, bin_sizes[bidx]-binsubp_idx * max_subprob_size);
int xoffset = (bidx % nbinx) * bin_size_x;
int yoffset = (bidx / nbinx) * bin_size_y;
int N = (bin_size_x + 2 * ceil(ns / 2.0)) * (bin_size_y + 2 * ceil(ns / 2.0));
for (int k = threadIdx.x;k<N; k += blockDim.x) {
int i = k % (int) (bin_size_x + 2 * ceil(ns / 2.0) );
int j = k /( bin_size_x + 2 * ceil(ns / 2.0) );
ix = xoffset - ceil(ns / 2.0) + i;
iy = yoffset - ceil(ns / 2.0) + j;
if (ix < (nf1 + ceil(ns / 2.0)) && iy < (nf2 + ceil(ns / 2.0))) {
ix = ix < 0 ? ix + nf1 : (ix > nf1 - 1 ? ix - nf1 : ix);
iy = iy < 0 ? iy + nf2 : (iy > nf2 - 1 ? iy - nf2 : iy);
outidx = ix + iy * nf1;
int sharedidx = i + j * (bin_size_x + ceil(ns / 2.0) * 2);
fwshared[sharedidx].x = fw[outidx].x;
fwshared[sharedidx].y = fw[outidx].y;
}
}
__syncthreads();
FloatType ker1[kMaxKernelWidth];
FloatType ker2[kMaxKernelWidth];
FloatType x_rescaled, y_rescaled;
GpuComplex<FloatType> cnow;
for (int i = threadIdx.x; i<nupts; i += blockDim.x) {
int idx = ptstart + i;
x_rescaled = RESCALE(x[idxnupts[idx]], nf1, pirange);
y_rescaled = RESCALE(y[idxnupts[idx]], nf2, pirange);
cnow.x = 0.0;
cnow.y = 0.0;
xstart = ceil(x_rescaled - ns / 2.0) - xoffset;
ystart = ceil(y_rescaled - ns / 2.0) - yoffset;
xend = floor(x_rescaled + ns / 2.0) - xoffset;
yend = floor(y_rescaled + ns / 2.0) - yoffset;
EvaluateKernelVectorHorner(ker1, xstart + xoffset - x_rescaled, ns, sigma);
EvaluateKernelVectorHorner(ker2, ystart + yoffset - y_rescaled, ns, sigma);
for (int yy = ystart; yy<=yend; yy++) {
FloatType disy = abs(y_rescaled - (yy + yoffset));
FloatType kervalue2 = ker2[yy - ystart];
for (int xx = xstart; xx<=xend; xx++) {
ix = xx + ceil(ns / 2.0);
iy = yy + ceil(ns / 2.0);
outidx = ix + iy * (bin_size_x + ceil(ns / 2.0) * 2);
FloatType kervalue1 = ker1[xx - xstart];
cnow.x += fwshared[outidx].x * kervalue1 * kervalue2;
cnow.y += fwshared[outidx].y * kervalue1 * kervalue2;
}
}
c[idxnupts[idx]] = cnow;
}
}
template<typename FloatType>
__global__ void SpreadNuptsDrivenHorner3DKernel(
FloatType *x, FloatType *y, FloatType *z, GpuComplex<FloatType> *c,
GpuComplex<FloatType> *fw, int M, const int ns, int nf1, int nf2, int nf3,
FloatType sigma, int* idxnupts, int pirange) {
int xx, yy, zz, ix, iy, iz;
int outidx;
FloatType ker1[kMaxKernelWidth];
FloatType ker2[kMaxKernelWidth];
FloatType ker3[kMaxKernelWidth];
FloatType ker1val, ker2val, ker3val;
FloatType x_rescaled, y_rescaled, z_rescaled;
for (int i = blockDim.x * blockIdx.x + threadIdx.x; i<M; i += blockDim.x * gridDim.x) {
x_rescaled = RESCALE(x[idxnupts[i]], nf1, pirange);
y_rescaled = RESCALE(y[idxnupts[i]], nf2, pirange);
z_rescaled = RESCALE(z[idxnupts[i]], nf3, pirange);
int xstart = ceil(x_rescaled - ns / 2.0);
int ystart = ceil(y_rescaled - ns / 2.0);
int zstart = ceil(z_rescaled - ns / 2.0);
int xend = floor(x_rescaled + ns / 2.0);
int yend = floor(y_rescaled + ns / 2.0);
int zend = floor(z_rescaled + ns / 2.0);
FloatType x1 = (FloatType)xstart - x_rescaled;
FloatType y1 = (FloatType)ystart - y_rescaled;
FloatType z1 = (FloatType)zstart - z_rescaled;
EvaluateKernelVectorHorner(ker1, x1, ns, sigma);
EvaluateKernelVectorHorner(ker2, y1, ns, sigma);
EvaluateKernelVectorHorner(ker3, z1, ns, sigma);
for (zz = zstart; zz<=zend; zz++) {
ker3val = ker3[zz - zstart];
for (yy = ystart; yy<=yend; yy++) {
ker2val = ker2[yy - ystart];
for (xx = xstart; xx<=xend; xx++) {
ix = xx < 0 ? xx + nf1 : (xx > nf1 - 1 ? xx - nf1 : xx);
iy = yy < 0 ? yy + nf2 : (yy > nf2 - 1 ? yy - nf2 : yy);
iz = zz < 0 ? zz + nf3 : (zz > nf3 - 1 ? zz - nf3 : zz);
outidx = ix + iy * nf1 + iz * nf1 * nf2;
ker1val = ker1[xx - xstart];
FloatType kervalue = ker1val * ker2val * ker3val;
GpuAtomicAdd(&fw[outidx].x, c[idxnupts[i]].x * kervalue);
GpuAtomicAdd(&fw[outidx].y, c[idxnupts[i]].y * kervalue);
}
}
}
}
}
template<typename FloatType>
__global__ void SpreadNuptsDriven3DKernel(
FloatType *x, FloatType *y, FloatType *z, GpuComplex<FloatType> *c,
GpuComplex<FloatType> *fw, int M, const int ns, int nf1, int nf2, int nf3,
FloatType es_c, FloatType es_beta, int* idxnupts, int pirange) {
int xx, yy, zz, ix, iy, iz;
int outidx;
FloatType ker1[kMaxKernelWidth];
FloatType ker2[kMaxKernelWidth];
FloatType ker3[kMaxKernelWidth];
FloatType x_rescaled, y_rescaled, z_rescaled;
FloatType ker1val, ker2val, ker3val;
for (int i = blockDim.x * blockIdx.x + threadIdx.x; i<M; i += blockDim.x * gridDim.x) {
x_rescaled = RESCALE(x[idxnupts[i]], nf1, pirange);
y_rescaled = RESCALE(y[idxnupts[i]], nf2, pirange);
z_rescaled = RESCALE(z[idxnupts[i]], nf3, pirange);
int xstart = ceil(x_rescaled - ns / 2.0);
int ystart = ceil(y_rescaled - ns / 2.0);
int zstart = ceil(z_rescaled - ns / 2.0);
int xend = floor(x_rescaled + ns / 2.0);
int yend = floor(y_rescaled + ns / 2.0);
int zend = floor(z_rescaled + ns / 2.0);
FloatType x1 = (FloatType)xstart - x_rescaled;
FloatType y1 = (FloatType)ystart - y_rescaled;
FloatType z1 = (FloatType)zstart - z_rescaled;
EvaluateKernelVector(ker1, x1, ns, es_c, es_beta);
EvaluateKernelVector(ker2, y1, ns, es_c, es_beta);
EvaluateKernelVector(ker3, z1, ns, es_c, es_beta);
for (zz = zstart; zz<=zend; zz++) {
ker3val = ker3[zz - zstart];
for (yy = ystart; yy<=yend; yy++) {
ker2val = ker2[yy - ystart];
for (xx = xstart; xx<=xend; xx++) {
ix = xx < 0 ? xx + nf1 : (xx > nf1 - 1 ? xx - nf1 : xx);
iy = yy < 0 ? yy + nf2 : (yy > nf2 - 1 ? yy - nf2 : yy);
iz = zz < 0 ? zz + nf3 : (zz > nf3 - 1 ? zz - nf3 : zz);
outidx = ix + iy * nf1 + iz * nf1 * nf2;
ker1val = ker1[xx - xstart];
FloatType kervalue = ker1val * ker2val * ker3val;
GpuAtomicAdd(&fw[outidx].x, c[idxnupts[i]].x * kervalue);
GpuAtomicAdd(&fw[outidx].y, c[idxnupts[i]].y * kervalue);
}
}
}
}
}
template<typename FloatType>
__global__ void SpreadSubproblemHorner3DKernel(
FloatType *x, FloatType *y, FloatType *z, GpuComplex<FloatType> *c,
GpuComplex<FloatType> *fw, int M, const int ns, int nf1, int nf2, int nf3,
FloatType sigma, int* binstartpts, int* bin_sizes, int bin_size_x,
int bin_size_y, int bin_size_z,
int* subprob_bins, int* subprob_start_pts, int* num_subprob,
int max_subprob_size, int nbinx, int nbiny, int nbinz, int* idxnupts,
int pirange) {
extern __shared__ __align__(sizeof(GpuComplex<FloatType>)) unsigned char fwshared_[];
GpuComplex<FloatType> *fwshared = reinterpret_cast<GpuComplex<FloatType>*>(fwshared_);
int xstart, ystart, xend, yend, zstart, zend;
int bidx = subprob_bins[blockIdx.x];
int binsubp_idx = blockIdx.x - subprob_start_pts[bidx];
int ix, iy, iz, outidx;
int ptstart = binstartpts[bidx]+binsubp_idx * max_subprob_size;
int nupts = min(max_subprob_size, bin_sizes[bidx]-binsubp_idx * max_subprob_size);
int xoffset = (bidx % nbinx) * bin_size_x;
int yoffset = ((bidx / nbinx)%nbiny) * bin_size_y;
int zoffset = (bidx/ (nbinx * nbiny)) * bin_size_z;
int N = (bin_size_x + 2 * ceil(ns / 2.0)) * (bin_size_y + 2 * ceil(ns / 2.0))*
(bin_size_z + 2 * ceil(ns / 2.0));
for (int i = threadIdx.x; i<N; i += blockDim.x) {
fwshared[i].x = 0.0;
fwshared[i].y = 0.0;
}
__syncthreads();
FloatType x_rescaled, y_rescaled, z_rescaled;
GpuComplex<FloatType> cnow;
for (int i = threadIdx.x; i<nupts; i += blockDim.x) {
FloatType ker1[kMaxKernelWidth];
FloatType ker2[kMaxKernelWidth];
FloatType ker3[kMaxKernelWidth];
int nuptsidx = idxnupts[ptstart + i];
x_rescaled = RESCALE(x[nuptsidx],nf1, pirange);
y_rescaled = RESCALE(y[nuptsidx],nf2, pirange);
z_rescaled = RESCALE(z[nuptsidx],nf3, pirange);
cnow = c[nuptsidx];
xstart = ceil(x_rescaled - ns / 2.0) - xoffset;
ystart = ceil(y_rescaled - ns / 2.0) - yoffset;
zstart = ceil(z_rescaled - ns / 2.0) - zoffset;