-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcudaArith - Kopie.cu
1425 lines (1276 loc) · 78.7 KB
/
cudaArith - Kopie.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
/************************* CudaMat ******************************************
* Copyright (C) 2008-2009 by Rainer Heintzmann *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; Version 2 of the License. *
* *
* 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, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************
* Compile with:
* Windows:
system('"c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin\vcvars32.bat"')
system('nvcc -c cudaArith.cu -ccbin "c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin')
Window 64 bit:
system('nvcc -c cudaArith.cu -ccbin "c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin" "-Ic:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include"')
Linux:
* File sudo vi /usr/local/cuda/bin/nvcc.profile
* needs the flag -fPIC in the include line
system('nvcc -c cudaArith.cu -v -I/usr/local/cuda/include/')
*/
#include <cuda.h>
#include <stdio.h>
#include "cudaArith.h"
#define IMUL(a, b) __mul24(a, b)
#define BLOCKSIZE 512
//#define BLOCKSIZE 512
#define NBLOCKS(N,blockSize) (N/blockSize+(N%blockSize==0?0:1))
// below is blocksize
#define CUIMAGE_REDUCE_THREADS 128
// #define CUIMAGE_REDUCE_THREADS 128
//#define CUIMAGE_REDUCE_BLOCKS 64
#define mysum(a,b) ((a)+(b))
#define maxCond(a,b) (((b)>(a)))
#define minCond(a,b) (((b)<(a)))
// THESE STRUCT DEVINITION ARE NEEDED, AS CUDA CANNOT DEAL CORRECTLY WITH FIXED LENGTH ARRAYS IN THE ARGUMENT
// ACCESING THEM WILL CAUSE A CRASH!
// HOWEVER, STRUCTS WITH THE ARRAY INSIDE ARE OK
// Also inside a Cuda function one has to use the structure rather than an array of fixed size.
typedef struct {
int s[CUDA_MAXDIM];
} SizeND ;
// below are code snippets used in other macros
#define Coords3DFromIdx(idx,sSize) \
int x=(idx)%sSize.s[0]; \
int y=(idx/sSize.s[0])%sSize.s[1]; \
int z=(idx/(sSize.s[0]*sSize.s[1]))%sSize.s[2];
#define IdxFromCoords3D(x,y,z,dSize,dOffs) \
unsigned int idd=x+dOffs.s[0]+dSize.s[0]*(y+dOffs.s[1])+dSize.s[0]*dSize.s[1]*(z+dOffs.s[2]); \
#define CoordsNDFromIdx(idx,sSize,pos) \
SizeND pos; \
{ unsigned int resid=idx; \
for(int _d=0;_d<CUDA_MAXDIM;_d++) \
if (resid > 0) \
{ pos.s[_d]=resid%sSize.s[_d]; \
resid/=sSize.s[_d]; } \
else \
pos.s[_d]=0; \
}
#define IdxNDFromCoords(pos,dSize,idd) \
(idd)=0; \
{ \
unsigned int _Stride=1; \
for(int _d=0;_d<CUDA_MAXDIM;_d++) \
if (dSize.s[_d]>0) { \
if (pos.s[_d] < 0) \
{(idd) += (dSize.s[_d]-((-pos.s[_d]) % dSize.s[_d])) *_Stride;} \
else \
{(idd) += (pos.s[_d] % dSize.s[_d]) *_Stride;} \
_Stride *= dSize.s[_d]; } \
}
// The partial reduction funciton below projects the data along one dimension
// the processors are assigned to the result image pixels
// CAVE: These versions can be slow, if the resulting data has is smaller than the number of processors
#define CUDA_PartRedMask(FktName, OP) \
__global__ void FktName (float *in, float *out, float * mask, int N, int dSizeX, int sStrideX, int sStrideY, int ProjStride, int ProjSize){ \
int idd=(blockIdx.x*blockDim.x+threadIdx.x); \
if(idd>=N) return; \
int p; \
int ids=(idd%dSizeX)*sStrideX+(idd/dSizeX)*sStrideY; \
float accu=0.0; \
int laterPix=0; \
for (p=0;p<ProjSize;p++) \
{ \
if (mask == 0 || mask[ids] != 0.0) \
if (! laterPix) { \
accu=in[ids]; \
laterPix=1; \
} else { \
accu=OP(accu,in[ids]); \
} \
ids += ProjStride; \
} \
out[idd] = accu; \
} \
\
extern "C" const char * CUDA ## FktName(float *a, float * mask, float * c, int sSize[3], int ProjDir)\
{ \
int dSize[3],d; \
for (d=0;d<3;d++) dSize[d]=sSize[d]; \
dSize[ProjDir-1]=1; \
int N=dSize[0]*dSize[1]*dSize[2]; \
int blockSize=BLOCKSIZE; int nBlocks=NBLOCKS(N,blockSize); \
int ProjStride=0,ProjSize=0,sStrideX=0,sStrideY=0,dSizeX=0; \
if (ProjDir==1) \
{ProjStride=1;ProjSize=sSize[0];dSizeX=sSize[1];sStrideX=sSize[0];sStrideY=sSize[0]*sSize[1];}\
else if (ProjDir == 2) \
{ProjStride=sSize[0];ProjSize=sSize[1];dSizeX=sSize[0];sStrideX=1;sStrideY=sSize[0]*sSize[1];}\
else if (ProjDir == 3) \
{ProjStride=sSize[0]*sSize[1];ProjSize=sSize[2];dSizeX=sSize[0];sStrideX=1;sStrideY=sSize[0];}\
else \
return "Error: Unsupported projection direction"; \
FktName<<<nBlocks,blockSize>>>(a,c,mask,N,dSizeX,sStrideX,sStrideY,ProjStride,ProjSize);\
if (cudaGetLastError() == cudaSuccess) \
return 0; \
else \
return cudaGetErrorString(cudaGetLastError()); \
}
// This is the same as the above but suited for complex numbers
#define CUDA_PartRedMaskCpx(FktName, OP) \
__global__ void FktName (float *in, float *out, float * mask, int N, int dSizeX, int sStrideX, int sStrideY, int ProjStride, int ProjSize){ \
int idd=(blockIdx.x*blockDim.x+threadIdx.x); \
if(idd>=N) return; \
int p; \
int ids=(idd%dSizeX)*sStrideX+(idd/dSizeX)*sStrideY; \
float accu=0.0; \
float accuI=0.0; \
int laterPix=0; \
for (p=0;p<ProjSize;p++) \
{ \
if (mask == 0 || mask[ids] != 0.0) \
if (! laterPix) { \
accu=in[2*ids]; \
accuI=in[2*ids+1]; \
laterPix=1; \
} else { \
accu=OP(accu,in[2*ids]); \
accuI=OP(accuI,in[2*ids+1]); \
} \
ids += ProjStride; \
} \
out[2*idd] = accu; \
out[2*idd+1] = accuI; \
} \
\
extern "C" const char * CUDA ## FktName(float *a, float * mask, float * c, int sSize[3], int ProjDir)\
{ \
int dSize[3],d; \
for (d=0;d<3;d++) dSize[d]=sSize[d]; \
dSize[ProjDir-1]=1; \
int N=dSize[0]*dSize[1]*dSize[2]; \
int blockSize=BLOCKSIZE; int nBlocks=NBLOCKS(N,blockSize); \
int ProjStride=0,ProjSize=0,sStrideX=0,sStrideY=0,dSizeX=0; \
if (ProjDir==1) \
{ProjStride=1;ProjSize=sSize[0];dSizeX=sSize[1];sStrideX=sSize[0];sStrideY=sSize[0]*sSize[1];}\
else if (ProjDir == 2) \
{ProjStride=sSize[0];ProjSize=sSize[1];dSizeX=sSize[0];sStrideX=1;sStrideY=sSize[0]*sSize[1];}\
else if (ProjDir == 3) \
{ProjStride=sSize[0]*sSize[1];ProjSize=sSize[2];dSizeX=sSize[0];sStrideX=1;sStrideY=sSize[0];}\
else \
return "Error: Unsupported projection direction"; \
FktName<<<nBlocks,blockSize>>>(a,c,mask,N,dSizeX,sStrideX,sStrideY,ProjStride,ProjSize);\
if (cudaGetLastError() == cudaSuccess) \
return 0; \
else \
return cudaGetErrorString(cudaGetLastError()); \
}
// This partial reduction code keeps track of the index
#define CUDA_PartRedMaskIdx(FktName, OP) \
__global__ void FktName (float *in, float *out, float * outIdx, float * mask, int N, int dSizeX, int sStrideX, int sStrideY, int ProjStride, int ProjSize){ \
int idd=(blockIdx.x*blockDim.x+threadIdx.x); \
if(idd>=N) return; \
int p; \
int ids=(idd%dSizeX)*sStrideX+(idd/dSizeX)*sStrideY; \
float accu=0.0; \
float accuIdx=-1; \
int laterPix=0; \
for (p=0;p<ProjSize;p++) \
{ \
if (mask == 0 || mask[ids] != 0.0) \
if (! laterPix) { \
accu=in[ids]; \
accuIdx=p; \
laterPix=1; \
} else { \
if (OP(accu,in[ids])) {accu=in[ids];accuIdx=p;} \
} \
ids += ProjStride; \
} \
out[idd] = accu; \
if (outIdx != 0) \
outIdx[idd] = accuIdx; \
} \
\
extern "C" const char * CUDA ## FktName(float *a, float * mask, float * c, float * cIdx, int sSize[5], int ProjDir)\
{ \
int dSize[5],d; \
for (d=0;d<5;d++) dSize[d]=sSize[d]; \
dSize[ProjDir-1]=1; \
int N=dSize[0]*dSize[1]*dSize[2]*dSize[3]*dSize[4]; \
int blockSize=BLOCKSIZE; int nBlocks=NBLOCKS(N,blockSize); \
int ProjStride=0,ProjSize=0,sStrideX=0,sStrideY=0,dSizeX=0; \
if (ProjDir==1) \
{ProjStride=1;ProjSize=sSize[0];dSizeX=sSize[1];sStrideX=sSize[0];sStrideY=sSize[0]*sSize[1];}\
else if (ProjDir == 2) \
{ProjStride=sSize[0];ProjSize=sSize[1];dSizeX=sSize[0];sStrideX=1;sStrideY=sSize[0]*sSize[1];}\
else if (ProjDir == 3) \
{ProjStride=sSize[0]*sSize[1];ProjSize=sSize[2];dSizeX=sSize[0];sStrideX=1;sStrideY=sSize[0];}\
else if (ProjDir == 4) \
{ProjStride=sSize[0]*sSize[1]*sSize[2];ProjSize=sSize[3];dSizeX=sSize[0];sStrideX=1;sStrideY=sSize[0];}\
else if (ProjDir == 5) \
{ProjStride=sSize[0]*sSize[1]*sSize[2]*sSize[3];ProjSize=sSize[4];dSizeX=sSize[0];sStrideX=1;sStrideY=sSize[0];}\
else \
return "Error: Unsupported projection direction"; \
FktName<<<nBlocks,blockSize>>>(a,c,cIdx,mask,N,dSizeX,sStrideX,sStrideY,ProjStride,ProjSize);\
if (cudaGetLastError() == cudaSuccess) \
return 0; \
else \
return cudaGetErrorString(cudaGetLastError()); \
}
// Below is the reduction code of Wouter Caarls, modified
// This could potentially also be run sequentially over the remaining dimension
#define CUDA_FullRed(FktName, OP) \
__global__ void FktName (float *in, float *out, int size){ \
const int stride = blockDim.x * gridDim.x; \
const int start = IMUL(blockDim.x, blockIdx.x) + threadIdx.x;\
__shared__ float accum[CUIMAGE_REDUCE_THREADS]; \
if (start >= size) return; \
\
accum[threadIdx.x] = in[start]; \
for (int ii=start+stride; ii < size; ii += stride) { \
accum[threadIdx.x] = OP(accum[threadIdx.x], in[ii]); \
} \
__syncthreads(); \
if (!threadIdx.x) \
{ \
float res = accum[0]; \
int limit; \
if (start+blockDim.x > size) limit=1+(size-start-1)/gridDim.x; \
else limit=blockDim.x; \
for (int ii = 1; ii < limit; ii++) { \
res=OP(res,accum[ii]); \
} \
out[blockIdx.x] = res; \
} \
} \
\
extern "C" const char * CUDA ## FktName(float * a, int N, float * resp) \
{ \
float *interm, res; \
int CUIMAGE_REDUCE_BLOCKS=NBLOCKS(N,CUIMAGE_REDUCE_THREADS); \
float * accum = (float *) malloc(CUIMAGE_REDUCE_BLOCKS*sizeof(float));\
if (! accum) \
return "Malloc failed"; \
\
int status=cudaMalloc((void **) &interm, 2*CUIMAGE_REDUCE_BLOCKS*sizeof(float)); \
\
dim3 threadBlock(CUIMAGE_REDUCE_THREADS); \
dim3 blockGrid(CUIMAGE_REDUCE_BLOCKS); \
\
FktName<<<blockGrid, threadBlock>>>(a, interm, N); \
if (cudaGetLastError() != cudaSuccess) \
return cudaGetErrorString(cudaGetLastError()); \
\
status=cudaMemcpy(accum, interm, CUIMAGE_REDUCE_BLOCKS*sizeof(float), cudaMemcpyDeviceToHost);\
\
res = accum[0]; \
for (int ii=1; ii < CUIMAGE_REDUCE_BLOCKS; ii++) { \
res=OP(res,accum[ii]); \
} \
cudaFree(interm); \
free(accum); \
\
(* resp)=res; \
return 0; \
}
// The version below is for complex valued arrays
#define CUDA_FullRedCpx(FktName, OP) \
__global__ void FktName (float *in, float *out, int size){ \
const int stride = blockDim.x * gridDim.x; \
const int start = IMUL(blockDim.x, blockIdx.x) + threadIdx.x;\
__shared__ float accum[CUIMAGE_REDUCE_THREADS]; \
__shared__ float accumI[CUIMAGE_REDUCE_THREADS]; \
if (start >= size) return; \
\
accum[threadIdx.x] = in[2*start]; \
accumI[threadIdx.x] = in[2*start+1]; \
for (int ii=start+stride; ii < size; ii += stride) { \
accum[threadIdx.x] = OP(accum[threadIdx.x], in[2*ii]); \
accumI[threadIdx.x] = OP(accumI[threadIdx.x], in[2*ii +1]); \
} \
__syncthreads(); \
if (!threadIdx.x) \
{ \
float res = accum[0]; \
float resI = accumI[0]; \
int limit; \
if (start+blockDim.x > size) limit=1+(size-start-1)/gridDim.x; \
else limit=blockDim.x; \
for (int ii = 1; ii < limit; ii++) { \
res=OP(res,accum[ii]); \
resI=OP(resI,accumI[ii]); \
} \
out[2*blockIdx.x] = res; \
out[2*blockIdx.x + 1] = resI; \
} \
} \
\
extern "C" const char * CUDA ## FktName(float * a, int N, float * resp) \
{ \
float *interm, res, resI; \
int CUIMAGE_REDUCE_BLOCKS=NBLOCKS(N,CUIMAGE_REDUCE_THREADS); \
float * accum = (float *) malloc(2*CUIMAGE_REDUCE_BLOCKS*sizeof(float));\
if (! accum) \
return "Malloc failed"; \
\
int status=cudaMalloc((void **) &interm, 2*CUIMAGE_REDUCE_BLOCKS*sizeof(float)); \
\
dim3 threadBlock(CUIMAGE_REDUCE_THREADS); \
dim3 blockGrid(CUIMAGE_REDUCE_BLOCKS); \
\
FktName<<<blockGrid, threadBlock>>>(a, interm, N); \
if (cudaGetLastError() != cudaSuccess) \
return cudaGetErrorString(cudaGetLastError()); \
\
status=cudaMemcpy(accum, interm, 2*CUIMAGE_REDUCE_BLOCKS*sizeof(float), cudaMemcpyDeviceToHost);\
\
res = accum[0]; \
resI = accum[1]; \
for (int ii=1; ii < CUIMAGE_REDUCE_BLOCKS; ii++) { \
res=OP(res,accum[2*ii]); \
resI=OP(resI,accum[2*ii + 1]); \
} \
cudaFree(interm); \
free(accum); \
\
(* resp)=res; \
(* (resp+1))=resI; \
return 0; \
}
// The version below is for remembering the index (e.g. max and min)
#define CUDA_FullRedIdx(FktName, OP) \
__global__ void FktName (float *in, float *out, int size){ \
const int stride = blockDim.x * gridDim.x; \
const int start = IMUL(blockDim.x, blockIdx.x) + threadIdx.x;\
__shared__ float accum[CUIMAGE_REDUCE_THREADS]; \
__shared__ float accumI[CUIMAGE_REDUCE_THREADS]; \
if (start >= size) return; \
\
accum[threadIdx.x] = in[start]; \
accumI[threadIdx.x] = start; \
for (int ii=start+stride; ii < size; ii += stride) { \
if OP(accum[threadIdx.x], in[ii]) { accum[threadIdx.x]= in[ii]; accumI[threadIdx.x]= ii; } \
} \
__syncthreads(); \
if (!threadIdx.x) \
{ \
float res = accum[0]; \
float resI = accumI[0]; \
int limit; \
if (start+blockDim.x > size) limit=1+(size-start-1)/gridDim.x; \
else limit=blockDim.x; \
for (int ii = 1; ii < limit; ii++) { \
if OP(res, accum[ii]){ res= accum[ii]; resI= accumI[ii]; } \
} \
out[2*blockIdx.x] = res; \
out[2*blockIdx.x + 1] = resI; \
} \
} \
\
extern "C" const char * CUDA ## FktName(float * a, int N, float * resp) \
{ \
float *interm, res, resI; \
int CUIMAGE_REDUCE_BLOCKS=NBLOCKS(N,CUIMAGE_REDUCE_THREADS); \
float * accum = (float *) malloc(2*CUIMAGE_REDUCE_BLOCKS*sizeof(float));\
if (! accum) \
return "Malloc failed"; \
\
int status=cudaMalloc((void **) &interm, 2*CUIMAGE_REDUCE_BLOCKS*sizeof(float)); \
\
dim3 threadBlock(CUIMAGE_REDUCE_THREADS); \
dim3 blockGrid(CUIMAGE_REDUCE_BLOCKS); \
\
FktName<<<blockGrid, threadBlock>>>(a, interm, N); \
if (cudaGetLastError() != cudaSuccess) \
return cudaGetErrorString(cudaGetLastError()); \
\
status=cudaMemcpy(accum, interm, 2*CUIMAGE_REDUCE_BLOCKS*sizeof(float), cudaMemcpyDeviceToHost);\
\
res = accum[0]; \
resI = accum[1]; \
for (int ii=1; ii < CUIMAGE_REDUCE_BLOCKS; ii++) { \
if OP(res, accum[2*ii]) {res= accum[2*ii]; resI= accum[2*ii+1]; } \
} \
cudaFree(interm); \
free(accum); \
\
(* resp)=res; \
(* (resp+1))=resI; \
return 0; \
}
// Allows to work with the linear index image from a binary mask image.
// useful for: a(mask) = 2*a(mask)
/// Algorithm: pass1 : count ones in your area
// pass 2: integrate accum over thread number to get block ones offset
// pass 3: Apply index
#define CUDA_MaskIdx(FktName, EXPRESSIONS) \
__global__ void FktName (float *a, float * mask,float *c, int N, int * interm){ \
int Blocksize = N/CUIMAGE_REDUCE_THREADS + 1; \
int start = Blocksize * threadIdx.x; \
__shared__ int accum[CUIMAGE_REDUCE_THREADS+1]; \
if (start >= N) return; \
\
accum[threadIdx.x+1] = 0; \
for (int ii=start; ii < start+Blocksize; ii ++) { \
if (ii < N) \
accum[threadIdx.x+1] += (mask[ii] != 0); \
} \
__syncthreads(); \
if (!threadIdx.x) \
{ \
int res = accum[0]; \
int limit; \
if (CUIMAGE_REDUCE_THREADS > N) limit=N; \
else limit=CUIMAGE_REDUCE_THREADS; \
for (int ii = 1; ii < limit+1; ii++) { \
res += accum[ii]; \
accum[ii] = res; \
} \
interm[0] = res; \
} \
__syncthreads(); \
int mask_idx= accum[threadIdx.x]; \
for (int idx=start; idx < start+Blocksize; idx ++) { \
if ((idx < N) && (mask[idx] != 0)) \
{ \
EXPRESSIONS \
mask_idx ++; \
} \
} \
} \
\
extern "C" const char * CUDA ## FktName(float * in, float * mask, float * out, int N, int * pM) \
{ \
int *interm; \
int CUIMAGE_REDUCE_BLOCKS=1; \
dim3 threadBlock(CUIMAGE_REDUCE_THREADS); \
dim3 blockGrid(CUIMAGE_REDUCE_BLOCKS); \
int status=cudaMalloc((void **) &interm,1*sizeof(int)); \
\
FktName<<<blockGrid, threadBlock>>>(in, mask, out, N, interm); \
if (cudaGetLastError() != cudaSuccess) \
return cudaGetErrorString(cudaGetLastError()); \
\
status=cudaMemcpy(pM, interm, sizeof(int), cudaMemcpyDeviceToHost);\
cudaFree(interm); \
\
if (cudaGetLastError() == cudaSuccess) \
return 0; \
else \
return cudaGetErrorString(cudaGetLastError()); \
}
// In the expression one can use the variables idx (for real valued arrays) and idc (for complex valued arrays)
// -------------- caller function is also generated -------------
#define CUDA_BinaryFkt(FktName,expression) \
__global__ void \
FktName(float*a,float *b, float * c, int N) \
{ \
int idx=blockIdx.x*blockDim.x+threadIdx.x; if(idx>=N) return; \
expression \
} \
extern "C" const char * CUDA ## FktName(float * a, float * b, float * c, int N) \
{ \
int blockSize=BLOCKSIZE; int nBlocks=NBLOCKS(N,blockSize); \
FktName<<<nBlocks,blockSize>>>(a,b,c,N); \
if (cudaGetLastError() == cudaSuccess) \
return 0; \
else \
return cudaGetErrorString(cudaGetLastError()); \
} \
// --------------Macro generating operation of array with real constant -------------
#define CUDA_UnaryFktConst(FktName,expression) \
__global__ void \
FktName(float*a,float b, float * c, int N) \
{ \
int idx=blockIdx.x*blockDim.x+threadIdx.x; if(idx>=N) return; \
expression \
} \
extern "C" const char * CUDA ## FktName(float * a, float b, float * c, int N) \
{ \
int blockSize=BLOCKSIZE; int nBlocks=NBLOCKS(N,blockSize); \
FktName<<<nBlocks,blockSize>>>(a,b,c,N); \
if (cudaGetLastError() == cudaSuccess) \
return 0; \
else \
return cudaGetErrorString(cudaGetLastError()); \
} \
// --------------Macro generating operation with complex array and constant -------------
#define CUDA_UnaryFktConstC(FktName,expression) \
__global__ void \
FktName(float*a,float br, float bi, float * c, int N) \
{ \
int idx=blockIdx.x*blockDim.x+threadIdx.x; if(idx>=N) return; \
expression \
} \
extern "C" const char * CUDA ## FktName(float * a, float br, float bi, float * c, int N) \
{ \
int blockSize=BLOCKSIZE; int nBlocks=NBLOCKS(N,blockSize); \
FktName<<<nBlocks,blockSize>>>(a,br,bi,c,N); \
if (cudaGetLastError() == cudaSuccess) \
return 0; \
else \
return cudaGetErrorString(cudaGetLastError()); \
} \
// ----------- Makro for function with an integer Vector ---- e.g.- for cyclic shifts etc. -----
#define CUDA_UnaryFktIntVec(FktName,expression) \
__global__ void \
FktName(float*a, SizeND b, float * c, SizeND sSize, int N) \
{ \
int idx=blockIdx.x*blockDim.x+threadIdx.x; if(idx>=N) return; \
expression \
} \
extern "C" const char * CUDA ## FktName(float * a, int b[CUDA_MAXDIM], float * c, int mySize[CUDA_MAXDIM], int N) \
{ \
int blockSize=BLOCKSIZE; int nBlocks=NBLOCKS(N,blockSize); \
SizeND sb,sSize; \
for (int d=0;d<CUDA_MAXDIM;d++) \
{ sb.s[d]=b[d];sSize.s[d]=mySize[d]; } \
FktName<<<nBlocks,blockSize>>>(a,sb,c,sSize,N); \
if (cudaGetLastError() == cudaSuccess) \
return 0; \
else \
return cudaGetErrorString(cudaGetLastError()); \
} \
// --------------Macro generating unary operation with complex array -------------
#define CUDA_UnaryFkt(FktName,expression) \
__global__ void \
FktName(float*a, float * c, int N) \
{ \
int idx=blockIdx.x*blockDim.x+threadIdx.x; if(idx>=N) return; \
expression \
} \
extern "C" const char * CUDA ## FktName(float * a, float * c, int N) \
{ \
int blockSize=BLOCKSIZE; int nBlocks=NBLOCKS(N,blockSize); \
FktName<<<nBlocks,blockSize>>>(a,c,N); \
if (cudaGetLastError() == cudaSuccess) \
return 0; \
else \
return cudaGetErrorString(cudaGetLastError()); \
} \
// ---------------------- Some functions which know about x, and z position --------
// gets two sources and one destination, the two sources are assumed to have the same size
// sx,sy,sz : Source array sizes (total)
// sox,soy,soy : offsets
// ssx, ssy,ssz : source (or destination) subarray sizes
// dx,dy,dz: destination total array sizes
// dox,doy,doz : destination offsets
// THESE STRUCT DEVINITION ARE NEEDED, AS CUDA CANNOT DEAL CORRECTLY WITH FIXED LENGTH ARRAYS IN THE ARGUMENT
// ACCESING THEM WILL CAUSE A CRASH!
// HOWEVER, STRUCTS WITH THE ARRAY INSIDE ARE OK
typedef struct {
int s[3];
} Size3D ;
#define CUDA_3DFkt(FktName,expressions) \
__global__ void \
FktName(float *a, float *c, Size3D sSize,Size3D dSize,Size3D sOffs, Size3D sROI, Size3D dOffs) \
{ \
int idx=(blockIdx.x*blockDim.x+threadIdx.x); \
int N=sROI.s[0]*sROI.s[1]*sROI.s[2]; \
if(idx>=N) return; \
int x=(idx)%sROI.s[0]; \
int y=(idx/sROI.s[0])%sROI.s[1]; \
int z=(idx/(sROI.s[0]*sROI.s[1]))%sROI.s[2]; \
int ids=x+sOffs.s[0]+sSize.s[0]*(y+sOffs.s[1])+sSize.s[0]*sSize.s[1]*(z+sOffs.s[2]); \
int idd=x+dOffs.s[0]+dSize.s[0]*(y+dOffs.s[1])+dSize.s[0]*dSize.s[1]*(z+dOffs.s[2]); \
expressions \
} \
extern "C" const char * CUDA ## FktName(float * a, float *c, int sSize[3], int dSize[3], int sOffs[3], int sROI[3], int dOffs[3]) \
{ \
int N=sROI[0]*sROI[1]*sROI[2]; \
int blockSize=BLOCKSIZE; int nBlocks=NBLOCKS(N,blockSize); \
Size3D sS,dS,sO,sR,dO; \
int d; \
for (d=0;d<3;d++) \
{sS.s[d]=sSize[d];dS.s[d]=dSize[d];sO.s[d]=sOffs[d];sR.s[d]=sROI[d];dO.s[d]=dOffs[d];} \
FktName<<<nBlocks,blockSize>>>(a,c,sS,dS,sO,sR,dO); \
if (cudaGetLastError() == cudaSuccess || strcmp("no error",cudaGetErrorString(cudaGetLastError())) == 0) \
return 0; \
else \
return cudaGetErrorString(cudaGetLastError()); \
} \
// --- macros for sub-assigning a block with vectors in each dimension -----
#define CUDA_3DAsgFkt(FktName,expressions) \
__global__ void \
FktName(float *c, float br, float bi, Size3D dSize, Size3D dROI, Size3D dOffs) \
{ \
int idx=(blockIdx.x*blockDim.x+threadIdx.x); \
int N=dROI.s[0]*dROI.s[1]*dROI.s[2]; \
if(idx>=N) return; \
int x=(idx)%dROI.s[0]; \
int y=(idx/dROI.s[0])%dROI.s[1]; \
int z=(idx/(dROI.s[0]*dROI.s[1]))%dROI.s[2]; \
int idd=x+dOffs.s[0]+dSize.s[0]*(y+dOffs.s[1])+dSize.s[0]*dSize.s[1]*(z+dOffs.s[2]); \
expressions \
} \
extern "C" const char * CUDA ## FktName(float * c, float br, float bi, int dSize[3], int dROI[3], int dOffs[3]) \
{ \
int N=dROI[0]*dROI[1]*dROI[2]; \
int blockSize=BLOCKSIZE; int nBlocks=NBLOCKS(N,blockSize); \
Size3D dR,dS,dO; \
int d; \
for (d=0;d<3;d++) \
{dS.s[d]=dSize[d];dR.s[d]=dROI[d];dO.s[d]=dOffs[d];} \
FktName<<<nBlocks,blockSize>>>(c,br,bi,dS,dR,dO); \
if (cudaGetLastError() == cudaSuccess) \
return 0; \
else \
return cudaGetErrorString(cudaGetLastError()); \
} \
// --- macros for sub-assigning a block with vectors in each dimension - Extended version to be suitable for repmat
#define CUDA_3DWrapAsgFkt(FktName,expressions) \
__global__ void \
FktName(float *a, float *c, Size3D dSize, Size3D sSize) \
{ \
int idd=(blockIdx.x*blockDim.x+threadIdx.x); \
int N=dSize.s[0]*dSize.s[1]*dSize.s[2]; \
if(idd>=N) return; \
int x=(idd)%dSize.s[0]; \
int y=(idd/dSize.s[0])%dSize.s[1]; \
int z=(idd/(dSize.s[0]*dSize.s[1]))%dSize.s[2]; \
int ids=x%sSize.s[0]+sSize.s[0]*(y%sSize.s[1])+sSize.s[0]*sSize.s[1]*(z%sSize.s[2]); \
expressions \
} \
extern "C" const char * CUDA ## FktName(float *a, float * c, int sSize[3], int dSize[3]) \
{ \
int N=dSize[0]*dSize[1]*dSize[2]; \
int blockSize=BLOCKSIZE; int nBlocks=NBLOCKS(N,blockSize); \
Size3D sS,dS; \
int d; \
for (d=0;d<3;d++) \
{dS.s[d]=dSize[d];sS.s[d]=sSize[d];} \
FktName<<<nBlocks,blockSize>>>(a,c,dS,sS); \
if (cudaGetLastError() == cudaSuccess) \
return 0; \
else \
return cudaGetErrorString(cudaGetLastError()); \
} \
// Now the 5D Versions of the same code
// THESE STRUCT DEVINITION ARE NEEDED, AS CUDA CANNOT DEAL CORRECTLY WITH FIXED LENGTH ARRAYS IN THE ARGUMENT
// ACCESING THEM WILL CAUSE A CRASH!
// HOWEVER, STRUCTS WITH THE ARRAY INSIDE ARE OK
typedef struct {
int s[5];
} Size5D ;
#define CUDA_5DFkt(FktName,expressions) \
__global__ void \
FktName(float *a, float *c, Size5D sSize,Size5D dSize,Size5D sOffs, Size5D sROI, Size5D dOffs) \
{ \
int idx=(blockIdx.x*blockDim.x+threadIdx.x); \
int N=sROI.s[0]*sROI.s[1]*sROI.s[2]*sROI.s[3]*sROI.s[4]; \
if(idx>=N) return; \
int x=(idx)%sROI.s[0]; \
int y=(idx/sROI.s[0])%sROI.s[1]; \
int z=(idx/(sROI.s[0]*sROI.s[1]))%sROI.s[2]; \
int t=(idx/(sROI.s[0]*sROI.s[1]*sROI.s[2]))%sROI.s[3]; \
int e=(idx/(sROI.s[0]*sROI.s[1]*sROI.s[2]*sROI.s[3]))%sROI.s[4]; \
int ids=x+sOffs.s[0]+sSize.s[0]*(y+sOffs.s[1])+sSize.s[0]*sSize.s[1]*(z+sOffs.s[2])+sSize.s[0]*sSize.s[1]*sSize.s[2]*(t+sOffs.s[3])+sSize.s[0]*sSize.s[1]*sSize.s[2]*sSize.s[3]*(e+sOffs.s[4]); \
int idd=x+dOffs.s[0]+dSize.s[0]*(y+dOffs.s[1])+dSize.s[0]*dSize.s[1]*(z+dOffs.s[2])+dSize.s[0]*dSize.s[1]*dSize.s[2]*(t+dOffs.s[3])+dSize.s[0]*dSize.s[1]*dSize.s[2]*dSize.s[3]*(e+dOffs.s[4]); \
expressions \
} \
extern "C" const char * CUDA ## FktName(float * a, float *c, int sSize[5], int dSize[5], int sOffs[5], int sROI[5], int dOffs[5]) \
{ \
int N=sROI[0]*sROI[1]*sROI[2]*sROI[3]*sROI[4]; \
int blockSize=BLOCKSIZE; int nBlocks=NBLOCKS(N,blockSize); \
Size5D sS,dS,sO,sR,dO; \
int d; \
for (d=0;d<5;d++) \
{sS.s[d]=sSize[d];dS.s[d]=dSize[d];sO.s[d]=sOffs[d];sR.s[d]=sROI[d];dO.s[d]=dOffs[d];} \
FktName<<<nBlocks,blockSize>>>(a,c,sS,dS,sO,sR,dO); \
if (cudaGetLastError() == cudaSuccess || strcmp("no error",cudaGetErrorString(cudaGetLastError())) == 0) \
return 0; \
else \
return cudaGetErrorString(cudaGetLastError()); \
} \
// --- macros for sub-assigning a block with vectors in each dimension -----
#define CUDA_5DAsgFkt(FktName,expressions) \
__global__ void \
FktName(float *c, float br, float bi, Size5D dSize, Size5D dROI, Size5D dOffs) \
{ \
int idx=(blockIdx.x*blockDim.x+threadIdx.x); \
int N=dROI.s[0]*dROI.s[1]*dROI.s[2]*dROI.s[3]*dROI.s[4]; \
if(idx>=N) return; \
int x=(idx)%dROI.s[0]; \
int y=(idx/dROI.s[0])%dROI.s[1]; \
int z=(idx/(dROI.s[0]*dROI.s[1]))%dROI.s[2]; \
int t=(idx/(dROI.s[0]*dROI.s[1]*dROI.s[2]))%dROI.s[3]; \
int e=(idx/(dROI.s[0]*dROI.s[1]*dROI.s[2]*dROI.s[3]))%dROI.s[4]; \
int idd=x+dOffs.s[0]+dSize.s[0]*(y+dOffs.s[1])+dSize.s[0]*dSize.s[1]*(z+dOffs.s[2])+dSize.s[0]*dSize.s[1]*dSize.s[2]*(t+dOffs.s[3])+dSize.s[0]*dSize.s[1]*dSize.s[2]*dSize.s[3]*(e+dOffs.s[4]); \
expressions \
} \
extern "C" const char * CUDA ## FktName(float * c, float br, float bi, int dSize[5], int dROI[5], int dOffs[5]) \
{ \
int N=dROI[0]*dROI[1]*dROI[2]*dROI[3]*dROI[4]; \
int blockSize=BLOCKSIZE; int nBlocks=NBLOCKS(N,blockSize); \
Size5D dR,dS,dO; \
int d; \
for (d=0;d<5;d++) \
{dS.s[d]=dSize[d];dR.s[d]=dROI[d];dO.s[d]=dOffs[d];} \
FktName<<<nBlocks,blockSize>>>(c,br,bi,dS,dR,dO); \
if (cudaGetLastError() == cudaSuccess) \
return 0; \
else \
return cudaGetErrorString(cudaGetLastError()); \
} \
// --- macros for sub-assigning a block with vectors in each dimension - Extended version to be suitable for repmat
#define CUDA_5DWrapAsgFkt(FktName,expressions) \
__global__ void \
FktName(float *a, float *c, Size5D dSize, Size5D sSize) \
{ \
int idd=(blockIdx.x*blockDim.x+threadIdx.x); \
int N=dSize.s[0]*dSize.s[1]*dSize.s[2]*dSize.s[3]*dSize.s[4]; \
if(idd>=N) return; \
int x=(idd)%dSize.s[0]; \
int y=(idd/dSize.s[0])%dSize.s[1]; \
int z=(idd/(dSize.s[0]*dSize.s[1]))%dSize.s[2]; \
int t=(idd/(dSize.s[0]*dSize.s[1]*dSize.s[2]))%dSize.s[3]; \
int e=(idd/(dSize.s[0]*dSize.s[1]*dSize.s[2]*dSize.s[3]))%dSize.s[4]; \
int ids=x%sSize.s[0]+sSize.s[0]*(y%sSize.s[1])+sSize.s[0]*sSize.s[1]*(z%sSize.s[2]) + sSize.s[0]*sSize.s[1]*sSize.s[2]*(t%sSize.s[3])+sSize.s[0]*sSize.s[1]*sSize.s[2]*sSize.s[3]*(e%sSize.s[4]); \
expressions \
} \
extern "C" const char * CUDA ## FktName(float *a, float * c, int sSize[5], int dSize[5]) \
{ \
int N=dSize[0]*dSize[1]*dSize[2]*dSize[3]*dSize[4]; \
int blockSize=BLOCKSIZE; int nBlocks=NBLOCKS(N,blockSize); \
Size5D sS,dS; \
int d; \
for (d=0;d<5;d++) \
{dS.s[d]=dSize[d];sS.s[d]=sSize[d];} \
FktName<<<nBlocks,blockSize>>>(a,c,dS,sS); \
if (cudaGetLastError() == cudaSuccess) \
return 0; \
else \
return cudaGetErrorString(cudaGetLastError()); \
} \
/*__global__ void \
bla_ ## FktName(float*a, float * c, int N, Size3D sSize,Size3D dSize,Size3D sOffs, Size3D sROI, Size3D dOffs) { \
int idx=(blockIdx.x*blockDim.x+threadIdx.x); \
int idcd=0,idcs=0,ids=0; \
if(idx>=N) return; \
expression \
} \ */
// FktName<<<nBlocks,blockSize>>>(a,c,sSize,dSize,sOffs, sROI, dOffs); \
CUDA_FullRed(sum_arr,mysum)
CUDA_FullRedCpx(sum_carr,mysum)
// CUDA_FullRed(sum_carr,res+=accum[ii];)
CUDA_FullRedIdx(max_arr,maxCond)
CUDA_FullRedIdx(min_arr,minCond)
CUDA_PartRedMask(psum_arr,mysum)
CUDA_PartRedMaskCpx(psum_carr,mysum)
CUDA_PartRedMaskIdx(pmax_arr,maxCond)
CUDA_PartRedMaskIdx(pmin_arr,minCond)
// Sub copying, copies a source area into a destination area. Can be used for cat and subassign
// CUDA_3DFkt(arr_subcpy_arr,c[idd]=a[ids];)
CUDA_3DAsgFkt(const_3dsubcpy_arr,c[idd]=br;)
CUDA_3DAsgFkt(cconst_3dsubcpy_carr,c[2*idd]=br;c[2*idd+1]=bi;)
// repcopy for repmat command
CUDA_3DWrapAsgFkt(arr_3drepcpy_arr,c[idd]=a[ids];)
CUDA_3DWrapAsgFkt(carr_3drepcpy_carr,c[2*idd]=a[2*ids];c[2*idd+1]=a[2*ids+1];)
// Sub copying, copies a source area into a destination area. Can be used for cat and subassign
CUDA_5DAsgFkt(const_5dsubcpy_arr,c[idd]=br;)
CUDA_5DAsgFkt(cconst_5dsubcpy_carr,c[2*idd]=br;c[2*idd+1]=bi;)
// repcopy for repmat command
CUDA_5DWrapAsgFkt(arr_5drepcpy_arr,c[idd]=a[ids];)
CUDA_5DWrapAsgFkt(carr_5drepcpy_carr,c[2*idd]=a[2*ids];c[2*idd+1]=a[2*ids+1];)
// Assigning constant values to arrays accessed with a boolean array
CUDA_UnaryFktConst(arr_boolassign_const,if (a[idx]!=0) c[idx]=b;)
CUDA_UnaryFktConstC(carr_boolassign_const,if (a[idx]!=0) {c[2*idx]=br;c[2*idx+1]=bi;})
// Sub copying, copies a source area into a destination area. Can be used for cat and subassign
// CUDA_3DFkt(arr_subcpy_arr,c[idd]=a[ids];)
CUDA_3DFkt(arr_3dsubcpy_arr,c[idd]=a[ids];)
CUDA_3DFkt(carr_3dsubcpy_carr,c[2*idd]=a[2*ids];c[2*idd+1]=a[2*ids+1];)
CUDA_3DFkt(arr_3dsubcpy_carr,c[2*idd]=a[ids];c[2*idd+1]=0;)
// Sub copying, copies a source area into a destination area. Can be used for cat and subassign
// These versions intoduce a transpose operation
CUDA_3DFkt(arr_3dsubcpyT_arr, int iddt=y+dOffs.s[0]+dSize.s[0]*(x+dOffs.s[1])+dSize.s[0]*dSize.s[1]*(z+dOffs.s[2]); c[iddt]=a[ids];)
CUDA_3DFkt(carr_3dsubcpyT_carr,int idcdt=2*(y+dOffs.s[0]+dSize.s[0]*(x+dOffs.s[1])+dSize.s[0]*dSize.s[1]*(z+dOffs.s[2])); c[idcdt]=a[2*ids];c[idcdt+1]=a[2*ids+1];)
// with conjugation
CUDA_3DFkt(carr_3dsubcpyCT_carr,int idcdt=2*(y+dOffs.s[0]+dSize.s[0]*(x+dOffs.s[1])+dSize.s[0]*dSize.s[1]*(z+dOffs.s[2])); c[idcdt]=a[2*ids];c[idcdt+1]=-a[2*ids+1];)
//CUDA_3DFkt(arr_subref_arr3d,c[idd]=)
//getCudaRef(prhs[1]),newarr,sSize,dSize,cuda_array[newref[0]],cuda_array[newref[1]],cuda_array[newref[2]]);
// Sub copying, copies a source area into a destination area. Can be used for cat and subassign
CUDA_5DFkt(arr_5dsubcpy_arr,c[idd]=a[ids];)
CUDA_5DFkt(carr_5dsubcpy_carr,c[2*idd]=a[2*ids];c[2*idd+1]=a[2*ids+1];)
CUDA_5DFkt(arr_5dsubcpy_carr,c[2*idd]=a[ids];c[2*idd+1]=0;)
// Sub copying, copies a source area into a destination area. Can be used for cat and subassign
// These versions intoduce a transpose operation
CUDA_5DFkt(arr_5dsubcpyT_arr, int iddt=y+dOffs.s[0]+dSize.s[0]*(x+dOffs.s[1])+dSize.s[0]*dSize.s[1]*(z+dOffs.s[2]); c[iddt]=a[ids];)
CUDA_5DFkt(carr_5dsubcpyT_carr,int idcdt=2*(y+dOffs.s[0]+dSize.s[0]*(x+dOffs.s[1])+dSize.s[0]*dSize.s[1]*(z+dOffs.s[2])); c[idcdt]=a[2*ids];c[idcdt+1]=a[2*ids+1];)
// with conjugation
CUDA_5DFkt(carr_5dsubcpyCT_carr,int idcdt=2*(y+dOffs.s[0]+dSize.s[0]*(x+dOffs.s[1])+dSize.s[0]*dSize.s[1]*(z+dOffs.s[2])); c[idcdt]=a[2*ids];c[idcdt+1]=-a[2*ids+1];)
// Power
CUDA_BinaryFkt(arr_power_arr,c[idx]=pow(a[idx],b[idx]);)
CUDA_UnaryFktConst(arr_power_const,c[idx]=pow(a[idx],b);)
CUDA_UnaryFktConst(const_power_arr,c[idx]=pow(b,a[idx]);)
// Multiplications
CUDA_BinaryFkt(arr_times_arr,c[idx]=a[idx]*b[idx];)
CUDA_BinaryFkt(carr_times_carr,
int idc=2*idx;
float myr=a[idc]*b[idc]-a[idc+1]*b[idc+1];float myi=a[idc]*b[idc+1]+a[idc+1]*b[idc];
c[idc]=myr;c[idc+1]=myi;
)
CUDA_BinaryFkt(carr_times_arr,c[2*idx]=a[2*idx]*b[idx];c[2*idx+1]=a[2*idx+1]*b[idx];)
CUDA_BinaryFkt(arr_times_carr,c[2*idx]=a[idx]*b[2*idx];c[2*idx+1]=a[idx+1]*b[2*idx];)
CUDA_UnaryFktConst(arr_times_const,c[idx]=a[idx]*b;)
CUDA_UnaryFktConst(const_times_arr,c[idx]=a[idx]*b;)
CUDA_UnaryFktConstC(carr_times_const,
int idc=2*idx;
float myr=a[idc]*br-a[idc+1]*bi;float myi=a[idc]*bi+a[idc+1]*br;
c[idc]=myr;c[idc+1]=myi;
)
CUDA_UnaryFktConstC(const_times_carr,
int idc=2*idx;
float myr=a[idc]*br-a[idc+1]*bi;float myi=a[idc]*bi+a[idc+1]*br;
c[idc]=myr;c[idc+1]=myi;
)
CUDA_UnaryFktConstC(arr_times_Cconst,c[2*idx]=a[idx]*br;c[2*idx+1]=a[idx]*bi;)
CUDA_UnaryFktConstC(Cconst_times_arr,c[2*idx]=br*a[idx];c[2*idx+1]=bi*a[idx];)
// Divisions
CUDA_BinaryFkt(arr_divide_arr,c[idx]=a[idx]/b[idx];)
CUDA_BinaryFkt(carr_divide_carr,
int idc=2*idx;
float tmp=b[idc]*b[idc]+b[idc+1]*b[idc+1];
float myr=(a[idc]*b[idc]+a[idc+1]*b[idc+1])/tmp;float myi=(a[idc+1]*b[idc]-a[idc]*b[idc+1])/tmp;
c[idc]=myr;c[idc+1]=myi;
)
CUDA_BinaryFkt(carr_divide_arr,c[2*idx]=a[2*idx]/b[idx];c[2*idx+1]=a[2*idx+1]/b[idx];)
CUDA_BinaryFkt(arr_divide_carr,
int idc=2*idx;
float tmp=b[idc]*b[idc]+b[idc+1]*b[idc+1];
float myr=(a[idx]*b[idc]+a[idx+1]*b[idc+1])/tmp;float myi=(a[idx+1]*b[idc]-a[idx]*b[idc+1])/tmp;
c[idc]=myr;c[idc+1]=myi;
)
CUDA_UnaryFktConst(arr_divide_const,c[idx]=a[idx]/b;)
CUDA_UnaryFktConst(const_divide_arr,c[idx]=b/a[idx];)
CUDA_UnaryFktConstC(carr_divide_const,
int idc=2*idx;
float tmp=br*br+bi*bi;
float myr=(a[idc]*br+a[idc+1]*bi)/tmp;float myi=(a[idc+1]*br-a[idc]*bi)/tmp;
c[idc]=myr;c[idc+1]=myi;
)
CUDA_UnaryFktConstC(const_divide_carr,
int idc=2*idx;
float tmp=a[idc]*a[idc]+a[idc+1]*a[idc+1];
float myr=(br*a[idc]+bi*a[idc+1])/tmp;float myi=(bi*a[idc]-br*a[idc+1])/tmp;
c[idc]=myr;c[idc+1]=myi;
)
CUDA_UnaryFktConstC(arr_divide_Cconst,
float tmp=br*br+bi*bi;
float myr=a[idx]*br/tmp;float myi= -a[idx]*bi/tmp;
c[2*idx]=myr;c[2*idx+1]=myi;
)
CUDA_UnaryFktConstC(Cconst_divide_arr,c[2*idx]=br/a[idx];c[2*idx+1]=bi/a[idx];)
// Additions
CUDA_BinaryFkt(arr_plus_arr,c[idx]=a[idx]+b[idx];)
CUDA_BinaryFkt(carr_plus_carr, int idc=2*idx; c[idc]=a[idc]+b[idc];c[idc+1]=a[idc+1]+b[idc+1];)
CUDA_BinaryFkt(carr_plus_arr,int idc=2*idx;c[idc]=a[idc]+b[idx];c[idc+1]=a[idc+1];)
CUDA_BinaryFkt(arr_plus_carr,int idc=2*idx;c[idc]=a[idx]+b[idc];c[idc+1]=b[idc+1];)
CUDA_UnaryFktConst(arr_plus_const,c[idx]=a[idx]+b;)
CUDA_UnaryFktConst(const_plus_arr,c[idx]=a[idx]+b;)
CUDA_UnaryFktConstC(carr_plus_const,int idc=2*idx;c[idc]=a[idc]+br;c[idc+1]=a[idc+1]+bi;)
CUDA_UnaryFktConstC(const_plus_carr,int idc=2*idx;c[idc]=a[idc]+br;c[idc+1]=a[idc+1]+bi;)
CUDA_UnaryFktConstC(arr_plus_Cconst,int idc=2*idx;c[idc]=a[idx]+br;c[idc+1]=a[idx]+bi;)
CUDA_UnaryFktConstC(Cconst_plus_arr,int idc=2*idx;c[idc]=br+a[idx];c[idc+1]=bi;)
// Subtractions
CUDA_BinaryFkt(arr_minus_arr,c[idx]=a[idx]-b[idx];)
CUDA_BinaryFkt(carr_minus_carr,int idc=2*idx; c[idc]=a[idc]-b[idc];c[idc+1]=a[idc+1]-b[idc+1];)
CUDA_BinaryFkt(carr_minus_arr,int idc=2*idx;c[idc]=a[idc]-b[idx];c[idc+1]=a[idc+1];)
CUDA_BinaryFkt(arr_minus_carr,int idc=2*idx;c[idc]=a[idx]-b[idc];c[idc+1]=-b[idc+1];)
CUDA_UnaryFktConst(arr_minus_const,c[idx]=a[idx]-b;)
CUDA_UnaryFktConst(const_minus_arr,c[idx]=b-a[idx];)
CUDA_UnaryFktConstC(carr_minus_const,int idc=2*idx;c[idc]=a[idc]-br;c[idc+1]=a[idc+1]-bi;)
CUDA_UnaryFktConstC(const_minus_carr,int idc=2*idx;c[idc]=br-a[idc];c[idc+1]=bi-a[idc+1];)
CUDA_UnaryFktConstC(arr_minus_Cconst,int idc=2*idx;c[idc]=a[idx]-br;c[idc+1]=a[idx]-bi;)
CUDA_UnaryFktConstC(Cconst_minus_arr,int idc=2*idx;c[idc]=br-a[idx];c[idc+1]=bi;)
// Referencing and assignment // STILL NEEDS SOME WORK
// CUDA_BinaryFkt(arr_subsref_arr,c[idx]=(b[idx] == 0) ? 0 : a[idx];)
// CUDA_BinaryFkt(carr_subsref_arr,c[idc]=(b[idx] == 0) ? 0 : a[idc]; c[idc+1]=(b[idx] == 0) ? 0 : a[idc+1];)
// CUDA_BinaryFkt(arr_subsasgn_arr,if (b[idx] == 0) c[idx] = a[idx];)
// CUDA_BinaryFkt(carr_subsasgn_arr,if (b[idx] == 0) {c[idc] = a[idc];c[idc+1] = a[idc+1];})
CUDA_MaskIdx(arr_subsref_arr,c[mask_idx]=a[idx];)
CUDA_MaskIdx(carr_subsref_arr,c[2*mask_idx]=a[2*idx]; c[2*mask_idx+1]=a[2*idx+1];)
CUDA_MaskIdx(arr_subsasgn_arr,a[idx]=c[mask_idx];)
CUDA_MaskIdx(carr_subsasgn_arr,a[2*idx]=c[2*mask_idx]; a[2*idx+1]=c[2*mask_idx+1];)
// diagonal matrix generation
CUDA_3DFkt(arr_diag_set, int iddt=ids+dOffs.s[0]+dSize.s[0]*(ids+dOffs.s[1]); c[iddt]=a[ids];)
CUDA_3DFkt(carr_diag_set, int idcdt=2*(ids+dOffs.s[0]+dSize.s[0]*(ids+dOffs.s[1])); c[idcdt]=a[2*ids];c[idcdt+1]=a[2*ids*1];)
CUDA_3DFkt(arr_diag_get, int iddt=ids+dOffs.s[0]+dSize.s[0]*(ids+dOffs.s[1]); a[ids]=c[iddt];)
CUDA_3DFkt(carr_diag_get, int idcdt=2*(ids+dOffs.s[0]+dSize.s[0]*(ids+dOffs.s[1])); a[2*ids]=c[idcdt];a[2*ids*1]=c[idcdt+1];)
// referencing and assignment with index vectors.No Index checking performed
CUDA_BinaryFkt(arr_subsref_vec,{c[idx]=a[(int) b[idx]];})
CUDA_BinaryFkt(carr_subsref_vec,{c[2*idx]=a[2*((int) b[idx])];c[2*idx+1]=a[2*((int) b[idx])+1];})
CUDA_BinaryFkt(arr_subsasg_vec,{c[(int) b[idx]]=a[idx];})
CUDA_BinaryFkt(carr_subsasg_vec,{c[2*((int) b[idx])]=a[2*idx];c[2*((int) b[idx])+1]=a[2*idx+1];})
// binary logical operations
CUDA_BinaryFkt(arr_or_arr,{c[idx]=(float) (a[idx]!=0) || (b[idx]!=0);})
CUDA_UnaryFktConst(arr_or_const,{c[idx]=(float) (a[idx]!=0) || (b!=0);})
CUDA_UnaryFktConst(const_or_arr,{c[idx]=(float) (b!=0) || (a[idx]!=0);})
CUDA_BinaryFkt(arr_and_arr,{c[idx]=(float) (a[idx]!=0) && (b[idx]!=0);})
CUDA_UnaryFktConst(arr_and_const,{c[idx]=(float) (a[idx]!=0) && (b!=0);})
CUDA_UnaryFktConst(const_and_arr,{c[idx]=(float) (b!=0) && (a[idx]!=0);})