-
Notifications
You must be signed in to change notification settings - Fork 1
/
cuda_cuda.c
4392 lines (4032 loc) · 224 KB
/
cuda_cuda.c
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:
mex cuda_cuda.c cudaArith.obj -Ic:\\CUDA\include\ -Lc:\\CUDA\lib64\ -lcublas -lcufft -lcudart
Windows 64 bit:
No Cula:
% mex cuda_cuda.c cudaArith.obj -DNOCULA "-IC:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v4.2\include" "-IC:\Program Files\CULA\R14\include" "-LC:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v4.2\lib\x64" -lcublas -lcufft -lcudart
mex cuda_cuda.c cudaArith.obj -DNOCULA "-IC:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.0\include" "-LC:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.0\lib\x64" -lcublas -lcufft -lcudart
* Cula:
mex cuda_cuda.c cudaArith.obj "-IC:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v4.2\include" "-IC:\Program Files\CULA\R14\include" "-LC:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v4.2\lib\x64" "-LC:\Program Files\CULA\R14\lib64" -lcublas -lcufft -lcudart -lcula_core -lcula_lapack
Linux:
mex -DNOCULA cuda_cuda.c cudaArith.o -I/usr/local/cuda/include -L/usr/local/cuda/lib64 -lcublas -lcufft -lcudart -v
or Linux including CULA support:
mex cuda_cuda.c cudaArith.o -I/usr/local/cuda/include -I/usr/local/cula/include -L/usr/local/cuda/lib64 -L/usr/local/cula/lib64 -lcublas -lcufft -lcudart -lcula
*/
/*
This is a mex file to perform a number of operations using cuda on graphic cards.
* The sytax is always cuda_cuda('operation',arg1, arg2) whereas arg2 can be empty.
'alloc' convert a matlab single into a cuda object which is stored on the graphics card. Returns integer reference to cuda object.
*/
#include "mex.h"
#include "cufft.h"
#include "cuda_runtime.h"
#include "cublas.h"
#define externC
#include "cudaArith.h"
#include "matrix.h"
#include "stdio.h"
#include "string.h"
#include "cufft.h"
// #define DEBUG
#ifdef DEBUG
#define Dbg_printf(arg) printf(arg)
#define Dbg_printf2(arg1,arg2) printf(arg1,arg2)
#define Dbg_printf3(arg1,arg2,arg3) printf(arg1,arg2,arg3)
#define Dbg_printf4(arg1,arg2,arg3,arg4) printf(arg1,arg2,arg3,arg4)
#define Dbg_printf5(arg1,arg2,arg3,arg4,arg5) printf(arg1,arg2,arg3,arg4,arg5)
#define Dbg_printf6(arg1,arg2,arg3,arg4,arg5,arg6) printf(arg1,arg2,arg3,arg4,arg5,arg6)
#define Dbg_printf7(arg1,arg2,arg3,arg4,arg5,arg6,arg7) printf(arg1,arg2,arg3,arg4,arg5,arg6,arg7)
#else
#define Dbg_printf(arg)
#define Dbg_printf2(arg1,arg2)
#define Dbg_printf3(arg1,arg2,arg3)
#define Dbg_printf4(arg1,arg2,arg3,arg4)
#define Dbg_printf5(arg1,arg2,arg3,arg4,arg5)
#define Dbg_printf6(arg1,arg2,arg3,arg4,arg5,arg6)
#define Dbg_printf7(arg1,arg2,arg3,arg4,arg5,arg6,arg7)
#endif
// https://undocumentedmatlab.com/blog/matlabs-internal-memory-representation
/* Definition of structure mxArray_tag for debugging purposes. Might not be fully correct
* for Matlab 2006b or 2007a, but the important things are. Thanks to Peter Boettcher.
*/
typedef struct {
const char *name;
mxClassID class_id;
int vartype;
mxArray *crosslink;
int number_of_dims;
int refcount;
struct {
unsigned int scalar_flag : 1;
unsigned int flag1 : 1;
unsigned int flag2 : 1;
unsigned int flag3 : 1;
unsigned int flag4 : 1;
unsigned int flag5 : 1;
unsigned int flag6 : 1;
unsigned int flag7 : 1;
unsigned int private_data_flag : 1;
unsigned int flag8 : 1;
unsigned int flag9 : 1;
unsigned int flag10 : 1;
unsigned int flag11 : 4;
unsigned int flag12 : 8;
unsigned int flag13 : 8;
} flags;
int rowdim;
int coldim;
union {
struct {
double *pdata; // original: void*
double *pimag_data; // original: void*
void *irptr;
void *jcptr;
int nelements;
int nfields;
} number_array;
struct {
mxArray **pdata;
char *field_names;
void *dummy1;
void *dummy2;
int dummy3;
int nfields;
} struct_array;
struct {
void *pdata; /*mxGetInfo*/
char *field_names;
char *name;
int checksum;
int nelements;
int reserved;
} object_array;
} data;
} mxArray_tag;
void printMem(size_t * start,int num)
{
int i;
for (i=0;i<num;i++)
printf("%lx\n",start[i]);
printf("\n");
}
#ifndef NOCULA
// #include "culadevice.h" // Only for old Cula releases
#include "cula.h" // Later releases such as R14
#include "cula_device.h" // Later releases such as R14
#endif
#define UseHeap // if defined the heap allocation and recycling is used. Otherwise normal allocation and free is used
#define AllocBlas // use the cublasAlloc and free functions instead of cudaMalloc and cudaFree
#define MAX_ARRAYS 65738 // maximum number of arrays simultaneously on card
#ifdef UseHeap
#define MAX_HEAP 25 // size of the heap of arrays to recycle
#endif
// is defined in stdlib.h in Windows which is WRONG!!
#undef min
#define min(a,b) ((a)<(b) ? (a):(b)) // define our own version so that it is compatible with Linux and Windows
// is defined in stdlib.h in Windows which is WRONG!!
#undef max
#define max(a,b) ((a)>(b) ? (a):(b)) // define our own version so that it is compatible with Linux and Windows
#define long_long_abs(a) ((a)>0 ? (a):(-a)) // since the abs function from the library works only with int and not long long
#define CHECK_CUDAREF(p) {if ((((double) (int) p) != p) || (p < 0) || (p >= MAX_ARRAYS)) \
mexErrMsgTxt("cuda: Reference must be an integer between 0 and max_array\n");}
#define CHECK_CUDASIZES(ref1,ref2) {if (cuda_array_dim[ref1] != cuda_array_dim[ref2]) mexErrMsgTxt("cuda: Arrays have different dimensionalities\n"); \
int d; for (d=0;d< cuda_array_dim[ref1]) if (cuda_array_size[ref1][d] != cuda_array_size[ref2][d])\
mexErrMsgTxt("cuda: Array sizes must be equal in all dimensions\n");}
#define CHECK_CUDATotalSIZES(ref1,ref2) {if (getTotalSizeFromRefNum(ref1) != getTotalSizeFromRefNum(ref2)) mexErrMsgTxt("cuda: Total array sizes are unequal. Bailing out\n");}
#define GET_MAXSIZE(ref,sizeC,numdims,totalResSize,NArgs) { \
int d,a; \
SizeND sizeA; \
numdims=1; \
totalResSize=1; \
for (a=0;a<NArgs;a++) { \
numdims=max(numdims,cuda_array_dim[ref[a]]); \
} \
for (d=0;d<CUDA_MAXDIM;d++) { \
Dbg_printf2("GET_MAXSIZE d %d \n",d); \
sizeC.s[d]=1; \
for (a=0;a<NArgs;a++) { \
sizeA=getSizeFromRefNum(ref[a]); \
sizeC.s[d]=max((sizeA.s[d]),(sizeC.s[d]));} \
totalResSize=totalResSize*sizeC.s[d]; \
Dbg_printf5("d %d SizeA: %d, SizeC %d, totalRes %d\n",d,sizeA.s[d],sizeC.s[d],totalResSize); \
} \
}
#define GET_MAXSIZE2(refA,refB,sizeC,numdims,totalResSize) { \
SizeND sizeA;SizeND sizeB; \
int d,dimsA=cuda_array_dim[refA], dimsB=cuda_array_dim[refB]; \
totalResSize=1; \
numdims=max(dimsA,dimsB); \
Dbg_printf2("GET_MAXSIZE2 BEFORE SizeA ref %d \n",refA); \
sizeA=getSizeFromRefNum(refA); \
Dbg_printf2("GET_MAXSIZE2 BEFORE SizeB ref %d \n",refB); \
sizeB=getSizeFromRefNum(refB); \
for (d=0;d<CUDA_MAXDIM;d++) { \
Dbg_printf2("GET_MAXSIZE2 d %d \n",d); \
sizeC.s[d]=max(sizeA.s[d],sizeB.s[d]); \
totalResSize*=sizeC.s[d]; \
Dbg_printf7("d %d SizeA: %d, SizeB: %d, SizeC %d, numdims: %d, totalResSize %d\n",d,sizeA.s[d],sizeB.s[d],sizeC.s[d],numdims,totalResSize);\
} \
}
// If sizes not equal: check if one dimension size is 1
// #define CHECK_CUDATotalSIZESSingleton(ref1,ref2,totalResSize, sizeC, singletonA, singletonB, numdims, hasSingleton) { \
// int d; \
// int dimsA=cuda_array_dim[ref1], dimsB=cuda_array_dim[ref2], SA,SB; \
// SizeND sizeA; SizeND sizeB; \
// sizeA=getSizeFromRefNum(ref1);sizeB=getSizeFromRefNum(ref2);totalResSize=1; \
// numdims=max(dimsA,dimsB); \
// Dbg_printf4("CHECK_CUDATotalSIZESSingleton numdims %d, ref1 %d ref2 %d\n",numdims,ref1,ref2); \
// for (d=0;d<CUDA_MAXDIM;d++) { \
// Dbg_printf2("CHECK_CUDATotalSIZESSingleton d %d \n",d); \
// if (d>dimsA) sizeA.s[d]=1; \
// if (d>dimsB) sizeB.s[d]=1; \
// singletonA.s[d]=0;singletonB.s[d]=0; \
// SA=sizeA.s[d]; SB=sizeB.s[d]; \
// sizeC.s[d]=max(SA,SB); \
// Dbg_printf5("d %d SizeA: %d, SizeB: %d, SizeC %d\n",d,SA,SB,sizeC.s[d]); \
// totalResSize *= sizeC.s[d]; \
// if (SA != SB) \
// if (SA != 1 && SB != 1) \
// { \
// printf("Dimension %d, SizeA: %d, SizeB: %d\n",d,SA,SB); \
// mexErrMsgTxt("cuda: Sizes are incompatible, even considering singleton dimensions. Bailing out\n");} \
// else if (SA==1) {singletonA.s[d]=1;hasSingleton=1;} \
// else {singletonB.s[d]=1;hasSingleton=1;} \
// } \
// Dbg_printf7("CHECK_CUDATotalSIZESSingleton ref1 %d, ref2 %d, totalResSize %d sizeC[0] %d sizeC[1] %d hasSingleton %d\n",ref1,ref2,totalResSize,sizeC.s[0],sizeC.s[1],hasSingleton); \
// }
// If sizes not equal: check if one dimension size is 1
#define CHECK_CUDATotalSIZESSingleton(ref1,sizeC, singletonA, hasSingleton) { \
int d; \
int dimsA=cuda_array_dim[ref1]; size_t SA; \
SizeND sizeA=getSizeFromRefNum(ref1); \
for (d=0;d<CUDA_MAXDIM;d++) { \
Dbg_printf2("CHECK_CUDATotalSIZESSingleton d %d \n",d); \
if (d>dimsA) sizeA.s[d]=1; \
singletonA.s[d]=0; \
SA=sizeA.s[d]; \
Dbg_printf4("d %d SizeA: %d, SizeC %d\n",d,SA,sizeC.s[d]); \
if (SA != sizeC.s[d]) \
if (SA != 1) \
{ \
printf("Dimension %d, SizeA: %d, SizeC: %d\n",d,SA,sizeC.s[d]); \
mexErrMsgTxt("cuda: Sizes are incompatible, even considering singleton dimensions. Bailing out\n");} \
else {singletonA.s[d]=1;hasSingleton=1;} \
} \
Dbg_printf3("CHECK_CUDATotalSIZESSingleton ref1 %d, hasSingleton %d\n",ref1,hasSingleton); \
}
// Generates a new array (float * newarr) from a size vector
#define CUDA_NewArrayFromSize(IsComplex) \
int dims_sizes; size_t nsizes[CUDA_MAXDIM],d,tsize=1; \
double * dsizes;float * newarr=0; \
if (nrhs < 2) mexErrMsgTxt("cuda: newarr needs >= 2 arguments\n"); \
dims_sizes=(int)(mxGetM(prhs[1]) * mxGetN(prhs[1])); \
dsizes=mxGetPr(prhs[1]); \
if (dims_sizes >= CUDA_MAXDIM) \
mexErrMsgTxt("cuda: newarr too many dimensions (>CUDA_MAXDIM)\n"); \
for (d=0;d<dims_sizes;d++) {nsizes[d]=(size_t) dsizes[d];tsize *= nsizes[d];} \
Dbg_printf5("newarray with dimension %d, sizes %d %d %d\n",dims_sizes,nsizes[0],nsizes[1],nsizes[2]); \
\
if (IsComplex) { \
newarr=cudaAllocDetailed(dims_sizes,nsizes,scomplex); \
} else { \
newarr=cudaAllocDetailed(dims_sizes,nsizes,single); \
}
// ----------------- Macros of code snippets, defining common ways of calling Cuda ---------
// ----------- macro with two arguments. An array and an index. AllocNum is 1 or 2 for first or second argument
#define CallCUDA_IdxFkt(FktName,AllocFkt,AllocNum,FirstSizeNum) \
const char *ret=0; size_t _ref1,_ref2; \
if (nrhs != 3 && nrhs != 4) mexErrMsgTxt("cuda: " #FktName " needs three or four arguments\n"); \
_ref1=getCudaRefNum(prhs[1]);_ref2=getCudaRefNum(prhs[2]); \
if (isComplexType(_ref2)) {mexErrMsgTxt("cuda: " #FktName " indexing with complex index arrays is not allowed\n");} \
if (isComplexType(_ref1)) { \
Dbg_printf("cuda: complex array " #FktName " index array\n"); \
ret=CUDAcarr_##FktName##_ind(getCudaRef(prhs[1]),getCudaRef(prhs[2]),AllocFkt(prhs[AllocNum]),getTotalSizeFromRef(prhs[FirstSizeNum]),getTotalSizeFromRef(prhs[2])); } \
else { \
Dbg_printf("cuda: array " #FktName " index array\n"); \
ret=CUDAarr_##FktName##_ind(getCudaRef(prhs[1]),getCudaRef(prhs[2]),AllocFkt(prhs[AllocNum]),getTotalSizeFromRef(prhs[FirstSizeNum]),getTotalSizeFromRef(prhs[2])); }\
if (ret!=(const char *) cudaSuccess) { printf("cuda " #FktName ": %s\n",cudaGetErrorString(cudaGetLastError())); mexErrMsgTxt("cuda error " #FktName ": Bailing out");} \
#define CallCUDA_IdxFktConst(FktName,AllocFkt) \
const char *ret=0; \
size_t ref; \
if (nrhs != 4) mexErrMsgTxt("cuda: " #FktName "_1Didx_const needs four arguments\n"); \
ref=getCudaRefNum(prhs[1]); \
if (mxIsComplex(prhs[3])) { \
double myreal = mxGetScalar(prhs[3]); \
double myimag = * ((double *) (mxGetPi(prhs[3]))); \
if (isComplexType(ref)) { \
Dbg_printf3("cuda: complex array " #FktName " complex-const Real: %g Imag: %g\n",myreal,myimag); \
ret=CUDAcarr_##FktName##_const(getCudaRef(prhs[2]),(float) myreal,(float) myimag,AllocFkt(prhs[1]),getTotalSizeFromRef(prhs[2])); \
if (ret!=(const char *) cudaSuccess) { printf("cuda " #FktName ": %s\n",cudaGetErrorString(cudaGetLastError())); mexErrMsgTxt("cuda error " #FktName ": Bailing out");} \
} else { \
Dbg_printf3("cuda: float array " #FktName " complex-const Real: %g Imag: %g\n",myreal,myimag); \
ret=CUDAarr_##FktName##_Cconst(getCudaRef(prhs[2]),(float) myreal,(float) myimag,AllocFkt(prhs[1]),getTotalSizeFromRef(prhs[2])); \
if (ret!=(const char *) cudaSuccess) { printf("cuda " #FktName ": %s\n",cudaGetErrorString(cudaGetLastError())); mexErrMsgTxt("cuda error " #FktName ": Bailing out");} \
} \
} else { \
double alpha = mxGetScalar(prhs[3]); \
if (isComplexType(ref)) { \
Dbg_printf("cuda: complex array " #FktName " real-const\n"); \
ret=CUDAcarr_##FktName##_const(getCudaRef(prhs[2]),(float) alpha,0.0,AllocFkt(prhs[1]),getTotalSizeFromRef(prhs[2])); \
if (ret!=(const char *) cudaSuccess) { printf("cuda " #FktName ": %s\n",cudaGetErrorString(cudaGetLastError())); mexErrMsgTxt("cuda error " #FktName ": Bailing out");} \
} else { \
Dbg_printf("cuda: float array " #FktName " real-const\n"); \
ret=CUDAarr_##FktName##_const(getCudaRef(prhs[2]),(float) alpha,AllocFkt(prhs[1]),getTotalSizeFromRef(prhs[2])); \
if (ret!=(const char *) cudaSuccess) { printf("cuda " #FktName ": %s\n",cudaGetErrorString(cudaGetLastError())); mexErrMsgTxt("cuda error " #FktName ": Bailing out");} \
} \
}
// Macro for index function with one list per dimension. The indices need to be given as a matrix with the longest index list dominating. The
#define CallCUDA_IdxFktND(FktName,AllocFkt) \
const char *ret=0; size_t _ref1,_ref2; SizeND sizeA,sizeC;float * pC; \
if (nrhs < 3 || nrhs > 5) mexErrMsgTxt("cuda: " #FktName " needs three to five arguments (excluding function name)\n"); \
_ref1=getCudaRefNum(prhs[1]);_ref2=getCudaRefNum(prhs[2]); \
if (isComplexType(_ref2)) {mexErrMsgTxt("cuda: " #FktName " indexing with complex index arrays is not allowed\n");} \
sizeA=getSizeFromRefNum(_ref1); \
if (nrhs >=4) \
sizeC=SizeNDFromRef(prhs[3]); \
else \
sizeC=getSizeFromRefNum(_ref2); \
if (nrhs >=5) \
pC=getCudaRef(prhs[4]); \
else \
pC=AllocFkt(prhs[1], sizeC, cuda_array_dim[_ref1]); \
Dbg_printf7("cuda: sizes of " #FktName " are %dx%dx%d , %dx%dx%d \n",sizeA.s[0],sizeA.s[1],sizeA.s[2],sizeC.s[0],sizeC.s[1],sizeC.s[2]); \
if (isComplexType(_ref1)) { \
Dbg_printf("cuda: complex array " #FktName " index array\n"); \
ret=CUDAcarr_##FktName(getCudaRef(prhs[1]),getCudaRef(prhs[2]),pC,sizeA,sizeC,cuda_array_size[_ref2][0],cuda_array_dim[_ref1]); } \
else { \
Dbg_printf("cuda: array " #FktName " index array\n"); \
Dbg_printf4("cuda: ref1 %d, ref2 %d, dim %d \n",_ref1,_ref2,cuda_array_dim[_ref1]); \
ret=CUDAarr_##FktName(getCudaRef(prhs[1]),getCudaRef(prhs[2]),pC,sizeA,sizeC,cuda_array_size[_ref2][0],cuda_array_dim[_ref1]); }\
Dbg_printf("cuda: came back \n"); \
if (ret!=(const char *) cudaSuccess) { printf("cuda " #FktName ": %s\n",cudaGetErrorString(cudaGetLastError())); mexErrMsgTxt("cuda error " #FktName ": Bailing out");} \
// Macro for index function with one list per dimension. The indices need to be given as a matrix with the longest index list dominating.
// A size vector of the compressed assinged area is needed for the parallelization, even though there is only a single value to be assigned.
#define CallCUDA_IdxFktNDConst(FktName) \
const char *ret=0; size_t _ref1,_ref2; SizeND sizeA,sizeC; \
if (nrhs != 5) mexErrMsgTxt("cuda: " #FktName " needs five arguments\n"); \
_ref1=getCudaRefNum(prhs[1]);_ref2=getCudaRefNum(prhs[2]); \
if (isComplexType(_ref2)) {mexErrMsgTxt("cuda: " #FktName " indexing with complex index arrays is not allowed\n");} \
sizeA=getSizeFromRefNum(_ref1); \
sizeC=SizeNDFromRef(prhs[4]); \
Dbg_printf7("cuda: sizes of " #FktName " are %dx%dx%d , %dx%dx%d \n",sizeA.s[0],sizeA.s[1],sizeA.s[2],sizeC.s[0],sizeC.s[1],sizeC.s[2]); \
if (mxIsComplex(prhs[3])) { \
double myreal = mxGetScalar(prhs[3]); \
double myimag = * ((double *) (mxGetPi(prhs[3]))); \
if (isComplexType(_ref1)) { \
Dbg_printf3("cuda: complex array " #FktName " complex-const Real: %g Imag: %g\n",myreal,myimag); \
ret=CUDAcarr_##FktName##_const_ind(getCudaRef(prhs[1]),getCudaRef(prhs[2]),(float) myreal,(float) myimag,sizeA,sizeC,cuda_array_size[_ref2][0],cuda_array_dim[_ref1]); \
if (ret!=(const char *) cudaSuccess) { printf("cuda " #FktName ": %s\n",cudaGetErrorString(cudaGetLastError())); mexErrMsgTxt("cuda error " #FktName ": Bailing out");} \
} else { \
Dbg_printf3("cuda: float array " #FktName " complex-const Real: %g Imag: %g\n",myreal,myimag); \
ret=CUDAarr_##FktName##_Cconst_ind(getCudaRef(prhs[1]),getCudaRef(prhs[2]),(float) myreal,(float) myimag,sizeA,sizeC,cuda_array_size[_ref2][0],cuda_array_dim[_ref1]); \
if (ret!=(const char *) cudaSuccess) { printf("cuda " #FktName ": %s\n",cudaGetErrorString(cudaGetLastError())); mexErrMsgTxt("cuda error " #FktName ": Bailing out");} \
} \
} else { \
double alpha = mxGetScalar(prhs[3]); \
if (isComplexType(_ref1)) { \
Dbg_printf("cuda: complex array " #FktName " real-const\n"); \
ret=CUDAcarr_##FktName##_const_ind(getCudaRef(prhs[1]),getCudaRef(prhs[2]),(float) alpha,0.0,sizeA,sizeC,cuda_array_size[_ref2][0],cuda_array_dim[_ref1]); \
if (ret!=(const char *) cudaSuccess) { printf("cuda " #FktName ": %s\n",cudaGetErrorString(cudaGetLastError())); mexErrMsgTxt("cuda error " #FktName ": Bailing out");} \
} else { \
Dbg_printf("cuda: float array " #FktName " real-const\n"); \
ret=CUDAarr_##FktName##_const_ind(getCudaRef(prhs[1]),getCudaRef(prhs[2]),(float) alpha,sizeA,sizeC,cuda_array_size[_ref2][0],cuda_array_dim[_ref1]); \
if (ret!=(const char *) cudaSuccess) { printf("cuda " #FktName ": %s\n",cudaGetErrorString(cudaGetLastError())); mexErrMsgTxt("cuda error " #FktName ": Bailing out");} \
} } \
Dbg_printf("cuda: came back \n"); \
if (ret!=(const char *) cudaSuccess) { printf("cuda " #FktName ": %s\n",cudaGetErrorString(cudaGetLastError())); mexErrMsgTxt("cuda error " #FktName ": Bailing out");} \
/* This was for debugging purposes. Commented out for now
// ----------------- general macro for binary array functions such as plus and minus ---------
#define CallCUDA_BinaryFktOld(FktName,AllocFkt) \
const char *ret=0; int _ref1,_ref2; \
int TotalResSize=0, numdims=0, usedims=0, hasSingleton=0; SizeND sizeC; BoolND singletonA,singletonB; \
if (nrhs != 3) mexErrMsgTxt("cuda: " #FktName " needs three arguments\n"); \
_ref1=getCudaRefNum(prhs[1]);_ref2=getCudaRefNum(prhs[2]); \
CHECK_CUDATotalSIZESSingleton(_ref1,_ref2, TotalResSize, sizeC, singletonA, singletonB, numdims, hasSingleton) \
Dbg_printf3("TotalSize %d, %d\n",TotalResSize,getTotalSizeFromRef(prhs[1])); \
if (isComplexType(_ref1) && isComplexType(_ref2)) { \
Dbg_printf("cuda: complex array " #FktName " complex array\n"); \
ret=CUDAcarr_##FktName##_carr(getCudaRef(prhs[1]),getCudaRef(prhs[2]),AllocFkt(prhs[1],sizeC,numdims),getTotalSizeFromRef(prhs[1]),usedims,sizeC,singletonA,singletonB); } \
else if (isComplexType(_ref1)) { \
Dbg_printf("cuda: complex array " #FktName " float array\n"); \
ret=CUDAcarr_##FktName##_arr(getCudaRef(prhs[1]),getCudaRef(prhs[2]),AllocFkt(prhs[1],sizeC,numdims),getTotalSizeFromRef(prhs[1]),usedims,sizeC,singletonA,singletonB); } \
else if (isComplexType(_ref2)) { \
Dbg_printf("cuda: float array " #FktName " complex array\n"); \
ret=CUDAarr_##FktName##_carr(getCudaRef(prhs[1]),getCudaRef(prhs[2]),AllocFkt(prhs[1],sizeC,numdims),getTotalSizeFromRef(prhs[1]),usedims,sizeC,singletonA,singletonB); }\
else { \
Dbg_printf("cuda: array " #FktName " array\n"); \
Dbg_printf3("TotalSize %d, %d\n",TotalResSize,getTotalSizeFromRef(prhs[1])); \
ret=CUDAarr_##FktName##_arr(getCudaRef(prhs[1]),getCudaRef(prhs[2]),AllocFkt(prhs[1],sizeC,numdims),getTotalSizeFromRef(prhs[1]),usedims,sizeC,singletonA,singletonB); }\
if (ret!=(const char *) cudaSuccess) { printf("cuda " #FktName ": %s\n",cudaGetErrorString(cudaGetLastError())); mexErrMsgTxt("cuda error " #FktName ": Bailing out");} \
*/
// ----------------- general macro for binary array functions such as plus and minus ---------
#define CallCUDA_BinaryFkt(FktName,AllocFkt) \
const char *ret=0; size_t _ref1,_ref2; \
size_t TotalResSize=0; int numdims=0, usedims=0, hasSingleton=0; SizeND sizeC; BoolND singletonA,singletonB; \
Dbg_printf("cuda: CallCUDA_BinaryFkt: " #FktName " Checkpoint 1\n"); \
if (nrhs != 3) mexErrMsgTxt("cuda: " #FktName " needs three arguments\n"); \
_ref1=getCudaRefNum(prhs[1]);_ref2=getCudaRefNum(prhs[2]); \
Dbg_printf("cuda: CallCUDA_BinaryFkt: " #FktName " before CHECK_CUDATotalSIZESSingleton\n"); \
GET_MAXSIZE2(_ref1,_ref2,sizeC,numdims,TotalResSize) \
CHECK_CUDATotalSIZESSingleton(_ref1, sizeC, singletonA, hasSingleton) \
CHECK_CUDATotalSIZESSingleton(_ref2, sizeC, singletonB, hasSingleton) \
usedims=numdims; \
if (! hasSingleton) usedims=0; \
if (isComplexType(_ref1) && isComplexType(_ref2)) { \
Dbg_printf("cuda: complex array " #FktName " complex array\n"); \
ret=CUDAcarr_##FktName##_carr(getCudaRef(prhs[1]),getCudaRef(prhs[2]),AllocFkt(prhs[1],sizeC,numdims),TotalResSize,usedims,sizeC,singletonA,singletonB); } \
else if (isComplexType(_ref1)) { \
Dbg_printf("cuda: complex array " #FktName " float array\n"); \
ret=CUDAcarr_##FktName##_arr(getCudaRef(prhs[1]),getCudaRef(prhs[2]),AllocFkt(prhs[1],sizeC,numdims),TotalResSize,usedims,sizeC,singletonA,singletonB); } \
else if (isComplexType(_ref2)) { \
Dbg_printf("cuda: float array " #FktName " complex array\n"); \
ret=CUDAarr_##FktName##_carr(getCudaRef(prhs[1]),getCudaRef(prhs[2]),AllocFkt(prhs[2],sizeC,numdims),TotalResSize,usedims,sizeC,singletonA,singletonB); }\
else { \
Dbg_printf("cuda: array " #FktName " array\n"); \
ret=CUDAarr_##FktName##_arr(getCudaRef(prhs[1]),getCudaRef(prhs[2]),AllocFkt(prhs[1],sizeC,numdims),TotalResSize,usedims,sizeC,singletonA,singletonB); }\
if (ret!=(const char *) cudaSuccess) { printf("cuda " #FktName ": %s\n",cudaGetErrorString(cudaGetLastError())); mexErrMsgTxt("cuda error " #FktName ": Bailing out");}
// ----------------- general macro for binary array functions such as plus and minus ---------
#define CallCUDA_NArgsFkt(FktName,AllocFkt,Nargs) \
const char *ret=0; size_t _refNum[Nargs]; float * _ref[Nargs]; \
size_t n,TotalResSize=0, numdims=0, usedims=0, hasSingleton=0; SizeND sizeC; BoolND singleton[Nargs];\
Dbg_printf("cuda: CallCUDA_NargsFkt: " #FktName " Checkpoint 1\n"); \
if (nrhs != Nargs+1) mexErrMsgTxt("cuda: " #FktName " needs " #Nargs " arguments\n"); \
for (n=0;n<Nargs;n++) {_refNum[n]=getCudaRefNum(prhs[n+1]);_ref[n]=getCudaRef(prhs[n+1]);} \
Dbg_printf("cuda: CallCUDA_NargsFkt: " #FktName " before CHECK_CUDATotalSIZESSingleton\n"); \
GET_MAXSIZE(_refNum,sizeC,numdims,TotalResSize,Nargs) \
for (n=1;n<Nargs;n++) {CHECK_CUDATotalSIZESSingleton(_refNum[n], sizeC, singleton[n], hasSingleton) } \
usedims=numdims; \
if (! hasSingleton) usedims=0; \
Dbg_printf("cuda: array " #FktName " array\n"); \
ret=CUDAarr_##FktName##_NArgs(_ref,AllocFkt(prhs[1],sizeC,numdims),TotalResSize,usedims,sizeC,singleton); \
if (ret!=(const char *) cudaSuccess) { printf("cuda " #FktName ": %s\n",cudaGetErrorString(cudaGetLastError())); mexErrMsgTxt("cuda error " #FktName ": Bailing out");}
// ----------------- for calling with array and constant ---------
#define CallCUDA_UnaryFktConst(FktName,AllocFkt) \
const char *ret=0; \
size_t ref; \
if (nrhs != 3) mexErrMsgTxt("cuda: " #FktName "_alpha needs three arguments\n"); \
ref=getCudaRefNum(prhs[1]); \
if (mxIsComplex(prhs[2])) { \
double myreal = mxGetScalar(prhs[2]); \
double myimag = * ((double *) (mxGetPi(prhs[2]))); \
if (isComplexType(ref)) { \
Dbg_printf3("cuda: complex array " #FktName " complex-const Real: %g Imag: %g\n",myreal,myimag); \
ret=CUDAcarr_##FktName##_const(getCudaRef(prhs[1]),(float) myreal,(float) myimag,AllocFkt(prhs[1]),getTotalSizeFromRef(prhs[1])); \
if (ret!=(const char *) cudaSuccess) { printf("cuda " #FktName ": %s\n",cudaGetErrorString(cudaGetLastError())); mexErrMsgTxt("cuda error " #FktName ": Bailing out");} \
} else { \
float * narr=cudaAllocComplex(prhs[1]); \
Dbg_printf3("cuda: float array " #FktName " complex-const Real: %g Imag: %g\n",myreal,myimag); \
ret=CUDAarr_##FktName##_Cconst(getCudaRef(prhs[1]),(float) myreal,(float) myimag,narr,getTotalSizeFromRef(prhs[1])); \
if (ret!=(const char *) cudaSuccess) { printf("cuda " #FktName ": %s\n",cudaGetErrorString(cudaGetLastError())); mexErrMsgTxt("cuda error " #FktName ": Bailing out");} \
} \
} else { \
double alpha = mxGetScalar(prhs[2]); \
if (isComplexType(ref)) { \
Dbg_printf("cuda: complex array " #FktName " real-const\n"); \
ret=CUDAcarr_##FktName##_const(getCudaRef(prhs[1]),(float) alpha,0.0,AllocFkt(prhs[1]),getTotalSizeFromRef(prhs[1])); \
if (ret!=(const char *) cudaSuccess) { printf("cuda " #FktName ": %s\n",cudaGetErrorString(cudaGetLastError())); mexErrMsgTxt("cuda error " #FktName ": Bailing out");} \
} else { \
Dbg_printf("cuda: float array " #FktName " real-const\n"); \
ret=CUDAarr_##FktName##_const(getCudaRef(prhs[1]),(float) alpha,AllocFkt(prhs[1]),getTotalSizeFromRef(prhs[1])); \
if (ret!=(const char *) cudaSuccess) { printf("cuda " #FktName ": %s\n",cudaGetErrorString(cudaGetLastError())); mexErrMsgTxt("cuda error " #FktName ": Bailing out");} \
} \
}
// ----------------- for calling with array and constant but in Reverse order---------
#define CallCUDA_UnaryFktConstR(FktName,AllocFkt) \
const char *ret=0; \
size_t ref; \
if (nrhs != 3) mexErrMsgTxt("cuda: " #FktName "_alpha needs three arguments\n"); \
ref=getCudaRefNum(prhs[1]); \
if (mxIsComplex(prhs[2])) { \
double myreal = mxGetScalar(prhs[2]); \
double myimag = * ((double *) (mxGetPi(prhs[2]))); \
if (isComplexType(ref)) { \
Dbg_printf("cuda: complex array " #FktName " complex-const\n"); \
ret=CUDAconst_##FktName##_carr(getCudaRef(prhs[1]),(float) myreal,(float) myimag,AllocFkt(prhs[1]),getTotalSizeFromRef(prhs[1])); \
if (ret!=(const char *) cudaSuccess) { printf("cuda " #FktName ": %s\n",cudaGetErrorString(cudaGetLastError())); mexErrMsgTxt("cuda error " #FktName ": Bailing out");} \
} else { \
float * narr=cudaAllocComplex(prhs[1]); \
Dbg_printf("cuda: float array " #FktName " complex-const\n"); \
ret=CUDACconst_##FktName##_arr(getCudaRef(prhs[1]),(float) myreal,(float) myimag,narr,getTotalSizeFromRef(prhs[1])); \
if (ret!=(const char *) cudaSuccess) { printf("cuda " #FktName ": %s\n",cudaGetErrorString(cudaGetLastError())); mexErrMsgTxt("cuda error " #FktName ": Bailing out");} \
} \
} else { \
double alpha = mxGetScalar(prhs[2]); \
if (isComplexType(ref)) { \
Dbg_printf("cuda: complex array " #FktName " real-const\n"); \
ret=CUDAconst_##FktName##_carr(getCudaRef(prhs[1]),(float) alpha,0.0,AllocFkt(prhs[1]),getTotalSizeFromRef(prhs[1])); \
if (ret!=(const char *) cudaSuccess) { printf("cuda " #FktName ": %s\n",cudaGetErrorString(cudaGetLastError())); mexErrMsgTxt("cuda error " #FktName ": Bailing out");} \
} else { \
Dbg_printf("cuda: float array " #FktName " real-const\n"); \
ret=CUDAconst_##FktName##_arr(getCudaRef(prhs[1]),(float) alpha,AllocFkt(prhs[1]),getTotalSizeFromRef(prhs[1])); \
if (ret!=(const char *) cudaSuccess) { printf("cuda " #FktName ": %s\n",cudaGetErrorString(cudaGetLastError())); mexErrMsgTxt("cuda error " #FktName ": Bailing out");} \
} \
}
// --------- The ones below are FOR REAL-VALUED Functions only --- (e.g. comparison operations) ----------
/*
#define CallCUDA_BinaryHRealFkt(FktName,AllocFkt) \
const char *ret=0; int _ref1,_ref2; \
if (nrhs != 3) mexErrMsgTxt("cuda: " #FktName " needs three arguments\n"); \
_ref1=getCudaRefNum(prhs[1]);_ref2=getCudaRefNum(prhs[2]); \
CHECK_CUDATotalSIZES(_ref1,_ref2); \
if (isComplexType(_ref1) && isComplexType(_ref2)) { \
mexErrMsgTxt("cuda: Function \"" #FktName "\" is defined only for real argument data.\n");} \
else if (isComplexType(_ref1)) { \
Dbg_printf("cuda: complex array " #FktName " float array\n"); \
ret=CUDAcarr_##FktName##_arr(getCudaRef(prhs[1]),getCudaRef(prhs[2]),AllocFkt(prhs[1]),getTotalSizeFromRef(prhs[1])); } \
else if (isComplexType(_ref2)) { \
mexErrMsgTxt("cuda: Function \"" #FktName "\" is defined only for real argument data.\n");} \
else { \
Dbg_printf("cuda: array " #FktName " array\n"); \
ret=CUDAarr_##FktName##_arr(getCudaRef(prhs[1]),getCudaRef(prhs[2]),AllocFkt(prhs[1]),getTotalSizeFromRef(prhs[1])); }\
if (ret!=(const char *) cudaSuccess) { printf("cuda " #FktName ": %s\n",cudaGetErrorString(cudaGetLastError())); mexErrMsgTxt("cuda error " #FktName ": Bailing out");}
*/
// --------- The ones below are FOR REAL-VALUED Functions only --- (e.g. comparison operations) ----------
#define CallCUDA_BinaryRealFkt(FktName,AllocFkt) \
const char *ret=0; size_t _ref1,_ref2; \
size_t TotalResSize=0; int numdims=0, hasSingleton=0; SizeND sizeC; BoolND singletonA,singletonB; \
if (nrhs != 3) mexErrMsgTxt("cuda: " #FktName " needs three arguments\n"); \
_ref1=getCudaRefNum(prhs[1]);_ref2=getCudaRefNum(prhs[2]); \
Dbg_printf("cuda: CallCUDA_BinaryRealFkt: " #FktName " before CHECK_CUDATotalSIZESSingleton\n"); \
GET_MAXSIZE2(_ref1,_ref2,sizeC,numdims,TotalResSize) \
CHECK_CUDATotalSIZESSingleton(_ref1, sizeC, singletonA, hasSingleton) \
CHECK_CUDATotalSIZESSingleton(_ref2, sizeC, singletonB, hasSingleton) \
if (isComplexType(_ref1) || isComplexType(_ref2)) \
mexErrMsgTxt("cuda: Function \"" #FktName "\" is defined only for real valued data.\n"); \
else { \
Dbg_printf("cuda: array " #FktName " array\n"); \
if (hasSingleton) \
ret=CUDAarr_##FktName##_arr(getCudaRef(prhs[1]),getCudaRef(prhs[2]),AllocFkt(prhs[1],sizeC,numdims),TotalResSize,numdims,sizeC,singletonA,singletonB); \
else \
ret=CUDAarr_##FktName##_arr(getCudaRef(prhs[1]),getCudaRef(prhs[2]),AllocFkt(prhs[1],sizeC,numdims),TotalResSize,0,sizeC,singletonA,singletonB); \
}\
if (ret!=(const char *) cudaSuccess) { printf("cuda " #FktName ": %s\n",cudaGetErrorString(cudaGetLastError())); mexErrMsgTxt("cuda error " #FktName ": Bailing out");}
// ----------------- for calling with array and constant ---------
#define CallCUDA_UnaryRealFktConst(FktName,AllocFkt) \
const char *ret=0; \
if (nrhs != 3) mexErrMsgTxt("cuda: " #FktName "_alpha needs three arguments\n"); \
if (mxIsComplex(prhs[2])) { \
mexErrMsgTxt("cuda: Function \"" #FktName "\" is defined only for real valued data.\n"); \
} else { \
double alpha = mxGetScalar(prhs[2]); \
if (isComplexType(getCudaRefNum(prhs[1]))) { \
mexErrMsgTxt("cuda: Function \"" #FktName "\" is defined only for real valued data.\n"); \
} else { \
Dbg_printf("cuda: float array " #FktName " real-const\n"); \
ret=CUDAarr_##FktName##_const(getCudaRef(prhs[1]),(float) alpha,AllocFkt(prhs[1]),getTotalSizeFromRef(prhs[1])); \
} \
} \
if (ret!=(const char *) cudaSuccess) { printf("cuda " #FktName ": %s\n",cudaGetErrorString(cudaGetLastError())); mexErrMsgTxt("cuda error " #FktName ": Bailing out");}
// ----------------- for calling with array and constant but in Reverse order (e.g. for alpha / array) ---------
#define CallCUDA_UnaryRealFktConstR(FktName,AllocFkt) \
const char *ret=0; \
if (nrhs != 3) mexErrMsgTxt("cuda: " #FktName "_alpha needs three arguments\n"); \
if (mxIsComplex(prhs[2])) { \
mexErrMsgTxt("cuda: Function \"" #FktName "\" is defined only for real valued data.\n"); \
} else { \
double alpha = mxGetScalar(prhs[2]); \
if (isComplexType(getCudaRefNum(prhs[1]))) { \
mexErrMsgTxt("cuda: Function \"" #FktName "\" is defined only for real valued data.\n"); \
} else { \
Dbg_printf("cuda: float array " #FktName " real-const\n"); \
ret=CUDAconst_##FktName##_arr(getCudaRef(prhs[1]),(float) alpha,AllocFkt(prhs[1]),getTotalSizeFromRef(prhs[1])); \
if (ret!=(const char *) cudaSuccess) { printf("cuda " #FktName ": %s\n",cudaGetErrorString(cudaGetLastError())); mexErrMsgTxt("cuda error " #FktName ": Bailing out");} \
} \
}
// ----------------- Unary function for real valued data only ------AllocCommand determines whether the result type is that same, complex or real ----------
#define CallCUDA_UnaryRealFkt(FktName,AllocCommand) \
const char *ret=0; \
if (nrhs != 2) mexErrMsgTxt("cuda: " #FktName " needs one argument\n"); \
if (isComplexType(getCudaRefNum(prhs[1]))) { \
mexErrMsgTxt("cuda error " #FktName ": Tried to apply to complex valued data."); \
ret="Error " #FktName ": Tried to apply to complex valued data."; \
} else { \
Dbg_printf("cuda: float array " #FktName "\n"); \
ret=CUDA##FktName##_arr(getCudaRef(prhs[1]),AllocCommand(prhs[1]),getTotalSizeFromRef(prhs[1])); \
if (ret!=(const char *) cudaSuccess) { printf("cuda " #FktName ": %s\n",cudaGetErrorString(cudaGetLastError())); mexErrMsgTxt("cuda error " #FktName ": Bailing out");} \
}
// ----------------- Unary function ------AllocCommand determines whether the result type is that same, complex or real ----------
#define CallCUDA_UnaryFkt(FktName,AllocCommand) \
const char *ret=0; \
if (nrhs != 2) mexErrMsgTxt("cuda: " #FktName " needs one argument\n"); \
if (isComplexType(getCudaRefNum(prhs[1]))) { \
Dbg_printf("cuda: complex array " #FktName "\n"); \
ret=CUDA##FktName##_carr(getCudaRef(prhs[1]),AllocCommand(prhs[1]),getTotalSizeFromRef(prhs[1])); \
} else { \
Dbg_printf("cuda: float array " #FktName "\n"); \
ret=CUDA##FktName##_arr(getCudaRef(prhs[1]),AllocCommand(prhs[1]),getTotalSizeFromRef(prhs[1])); \
if (ret!=(const char *) cudaSuccess) { printf("cuda " #FktName ": %s\n",cudaGetErrorString(cudaGetLastError())); mexErrMsgTxt("cuda error " #FktName ": Bailing out");} \
}
// ----------------- Unary function ------AllocCommand determines whether the result type is that same, complex or real ----------
// Important Note: The total size given to the algorithm corrsponds to the destination array C.
#define CallCUDA_UnaryFktSizeConst(FktName,AllocFkt) \
const char *ret=0;SizeND SC,SA;float * pC;size_t refA;double alpha; \
if (nrhs != 4) mexErrMsgTxt("cuda: " #FktName " needs three arguments\n"); \
alpha = mxGetScalar(prhs[2]); \
refA=getCudaRefNum(prhs[1]); \
SC=SizeNDFromRef(prhs[3]); \
SA=getSizeFromRefNum(refA); \
pC=AllocFkt(prhs[1], SC, cuda_array_dim[refA]); \
if (isComplexType(refA)) { \
Dbg_printf("cuda: complex array " #FktName "\n"); \
ret=CUDA##FktName##_carrSizeConst(getCudaRef(prhs[1]),pC,(float) alpha, SA, SC, getTotalSizeFromRefNum(free_array)); \
} else { \
Dbg_printf("cuda: float array " #FktName "\n"); \
ret=CUDA##FktName##_arrSizeConst(getCudaRef(prhs[1]),pC,(float) alpha, SA, SC, getTotalSizeFromRefNum(free_array)); \
if (ret!=(const char *) cudaSuccess) { printf("cuda " #FktName ": %s\n",cudaGetErrorString(cudaGetLastError())); mexErrMsgTxt("cuda error " #FktName ": Bailing out");} \
}
// Snippet below expects a vector(CUDA_MAXDIM) and an array as input and generates an array as output. E.g. circshift
#define CallCUDA_ArrVecFkt(FktName,AllocCommand,SetToVal) \
size_t dims_sizes,nshifts[CUDA_MAXDIM], dsize[CUDA_MAXDIM],d,tsize=1,ref; \
double * dshifts;float * newarr=0; \
const char * ret; \
\
if (nrhs != 3) mexErrMsgTxt("cuda: " #FktName " needs three arguments\n"); \
dims_sizes=(size_t)(mxGetM(prhs[2]) * mxGetN(prhs[2])); dshifts=mxGetPr(prhs[2]); \
if (dims_sizes > CUDA_MAXDIM) \
mexErrMsgTxt("cuda: " #FktName " too many dimensions (>CUDA_MAXDIM)\n"); \
\
ref=getCudaRefNum(prhs[1]); \
\
for (d=0;d<CUDA_MAXDIM;d++) { \
if (d<dims_sizes) \
nshifts[d]=(size_t) dshifts[d]; \
else \
nshifts[d]=SetToVal; \
if (d<cuda_array_dim[ref]) \
dsize[d]=cuda_array_size[ref][d]; \
else \
dsize[d]=1; \
} \
Dbg_printf5("" #FktName " with size %d, shifts %d %d %d\n",dims_sizes,nshifts[0],nshifts[1],nshifts[2]); \
if (nrhs != 3) mexErrMsgTxt("cuda: " #FktName " needs three arguments\n"); \
\
if (isComplexType(ref)) { \
ret=CUDAcarr_##FktName##_vec(getCudaRef(prhs[1]),nshifts,AllocCommand(prhs[1]),dsize,getTotalSizeFromRef(prhs[1])); \
if (ret!=(const char *) cudaSuccess) { printf("cuda complex " #FktName ": %s\n",cudaGetErrorString(cudaGetLastError())); mexErrMsgTxt("cuda error " #FktName ": Bailing out");} \
} else { \
ret=CUDAarr_##FktName##_vec(getCudaRef(prhs[1]),nshifts,AllocCommand(prhs[1]),dsize,getTotalSizeFromRef(prhs[1])); \
if (ret!=(const char *) cudaSuccess) { printf("cuda " #FktName ": %s\n",cudaGetErrorString(cudaGetLastError())); mexErrMsgTxt("cuda error " #FktName ": Bailing out");} \
} \
Dbg_printf("" #FktName "\n"); \
// Snippet below expects a vector(CUDA_MAXDIM) and two vectors as input and generates an array as output. E.g. xx,yy,zz,rr,phiphi
#define CallCUDA_GenArrFkt(FktName) \
{VecND vec1,vec2; \
SizeND sSize; \
if (nrhs != 4) mexErrMsgTxt("cuda: " #FktName " needs three arguments\n"); \
else {CUDA_NewArrayFromSize(0) /* uses Matlab Ref 1 as a size vector, 0 for no complex number*/ \
vec1=VecNDFromRef(prhs[2]); \
vec2=VecNDFromRef(prhs[3]); \
sSize=SizeNDFromRef(prhs[1]); \
CUDAarr_##FktName##_2vec(newarr, vec1, vec2, sSize, getTotalSizeFromRefNum(free_array));\
if (! (cudaGetLastError() == cudaSuccess)) \
{ printf("cuda " #FktName ": %s\n",cudaGetErrorString(cudaGetLastError())); mexErrMsgTxt(" " #FktName " Bailing out");} \
Dbg_printf("" #FktName "\n"); \
} }
static const char * ERROR_NAMES[]={
"SUCCESS",
"INVALID_PLAN",
"ALLOC_FAILED",
"INVALID_TYPE",
"INVALID_VALUE",
"INTERNAL_ERROR",
"EXEC_FAILED",
"SETUP_FAILED",
// "SHOWDOWN_FAILED",
"INVALID_SIZE",
"" // needed to stop the search
};
enum CUDA_TYPE {
single,
int16,
// fftSingle,
fftHalfSComplex,
scomplex
};
static const char * CUDA_TYPE_NAMES[]={
"single",
"int16",
// "fftSingle",
"fftHalfSComplex",
"scomplex",
"" // needed to stop the search
};
static const char * CUDA_PLAN_NAMES[]={ // indexed by plantype
"CUFFT_R2C",
"CUFFT_C2R",
"CUFFT_C2C",
""
};
static int showwarning=1; // defines whether a warning/error message will be shown when a wrong object is deleted and another one was primed for (protected) deletion
static int CUDA_TYPE_SIZE[]={
sizeof(float),
2,
// sizeof(float),
sizeof(cufftComplex),
sizeof(cufftComplex)
};
static int CUDA_MATLAB_CLASS[]={
mxSINGLE_CLASS,
mxINT16_CLASS,
// mxSINGLE_CLASS,
mxSINGLE_CLASS,
mxSINGLE_CLASS
};
#define MAX_FFTPLANS 50
#define MaxCudaDevices 5
static cufftHandle cuda_FFTplans[MAX_FFTPLANS]; // stores a number of plans for each type of transform and dimension
static size_t cuda_FFTplan_sizes[MAX_FFTPLANS][CUDA_MAXDIM]; // Stores the associated sizes, to check if plan needs to be re-done
static size_t cuda_FFTplan_dirYes[MAX_FFTPLANS][CUDA_MAXDIM]; // Stores the information which axes to transform, to check if plan needs to be re-done
static size_t cuda_FFTplan_ndims[MAX_FFTPLANS]; // Stores the information how many dimensions this transform has
static size_t cuda_FFTplan_plantype[MAX_FFTPLANS]; // Stores the information which type this transform is rft/fft/ift
static size_t cuda_FFTplan_extraBatch[MAX_FFTPLANS];
static size_t cuda_FFTplan_extraBatchStride[MAX_FFTPLANS];
static float * cuda_arrays[MAX_ARRAYS]; // static array of cuda arrays
static int cuda_array_dim[MAX_ARRAYS]; // type tags see CUDA_TYPE definitions above
static size_t * cuda_array_size[MAX_ARRAYS]; // dynamically allocated
//static int cuda_array_origFTsize[MAX_ARRAYS]; // for storing the original data size, when doing FFTs
//static float cuda_array_FTscale[MAX_ARRAYS]; // to do the maginitude correction
static int cuda_array_type[MAX_ARRAYS]; // type tags see CUDA_TYPE definitions above
static size_t free_array=0; // next number of array to fill
static size_t cuda_curr_arrays=0;
static int cuda_initialized=0;
// static int sizes[100];
// static int dims=0,totalsize=0;
static float * pOne[MaxCudaDevices], * pZero[MaxCudaDevices], * pReturnVal[MaxCudaDevices];
static size_t NumZero[MaxCudaDevices],NumOne[MaxCudaDevices],NumReturnVal[MaxCudaDevices]; // = {-1,-1,-1,-1,-1}
static int currentCudaDevice=0;
static int ignoreDelete=0; // Needed to avoid delete (or copyiing the whole array) after subassign
static size_t ignoreRef=-2; // Needed to avoid delete (or copyiing the whole array) after subassign
static void * fastMem=0, * fastMemI=0;
static const int fastMemSize=1024; // number of byte for fast transfer
static size_t SumAllocated=0; // next number of array to fill
static int forceDeviceSynchronization=0; // If this is active, synchonizeKernels() will be called after every comand. This is useful for profiling in Matlab.
#ifdef UseHeap
static void * mem_heap[MAX_HEAP]; // memory which can be reused if size matches
static size_t memsize_heap[MAX_HEAP]; // sizes in bytes
static size_t mem_heap_pos=0; // position of the next entry to free from the heap
static size_t mem_heap_first_free=0; // position of the next entry to free from the heap
static int mem_heap_allocated=0; // filling of the heap. Has to allow negative size!
#endif
/**************************************************************************/
/* MATLAB stores complex numbers in separate arrays for the real and
imaginary parts. The following functions take the data in
this format and pack it into a complex work array, or
unpack it, respectively.
We are using cufftComplex defined in cufft.h to handle complex on Windows and Linux
*/
#ifndef NOCULA
void checkCULAStatus(char * text, culaStatus status)
{
if(!status)
return;
if(status == culaArgumentError)
printf("%s: Invalid value for parameter %d\n", text,culaGetErrorInfo());
else if(status == culaDataError)
printf("%s: Data error (%d)\n", text,culaGetErrorInfo());
else if(status == culaBlasError)
printf("%s: Blas error (%d)\n", text,culaGetErrorInfo());
else if(status == culaRuntimeError)
printf("%s: Runtime error (%d)\n", text,culaGetErrorInfo());
else
printf("%s: %s\n", text,culaGetStatusString(status));
mexErrMsgTxt("CULA Error: Bailing out\n");
// culaShutdown();
}
#endif
void checkCudaError(char * text, cudaError_t err)
{
if(!err)
return;
printf("%s: %s\n", text, cudaGetErrorString(err));
mexErrMsgTxt("CULA Error: Bailing out\n");
}
// returns array size in 3D coordinates (expanding with ones)
void get3DSize(size_t ref, size_t * mysize) {
int d;
for (d=0;d<3;d++)
if (d< cuda_array_dim[ref])
mysize[d]=cuda_array_size[ref][d];
else
mysize[d]=1;
}
IntND getIntNDFromIntVec(int * mysizevec, size_t MaxDim) {
int d; IntND mysize;
for (d=0;d<CUDA_MAXDIM;d++) {
if (d< MaxDim)
mysize.s[d]=mysizevec[d];
else
mysize.s[d]=0; // because IntND is signed and used for shifts and alike
Dbg_printf3("getIntNDFromIntVec myIntVec[%d]=%d\n",d,mysize.s[d]);
}
return mysize;
}
SizeND getSizeFromIntVec(int * mysizevec, size_t MaxDim) {
int d; SizeND mysize;
for (d=0;d<CUDA_MAXDIM;d++) {
if (d< MaxDim)
mysize.s[d]=mysizevec[d];
else
mysize.s[d]=1;
Dbg_printf3("getSizeFromIntVec dim mysize[%d]=%d\n",d,mysize.s[d]);
}
return mysize;
}
SizeND getSizeFromVec(size_t * mysizevec, size_t MaxDim) {
int d;
SizeND mysize;
for (d=0;d<CUDA_MAXDIM;d++) {
if (d< MaxDim)
mysize.s[d]=mysizevec[d];
else
mysize.s[d]=1;
Dbg_printf3("getSizeFromVec mysize[%d]=%d\n",d,mysize.s[d]);
}
return mysize;
}
// returns array size in 3D coordinates (expanding with ones)
SizeND getSizeFromRefNum(size_t ref) {
Dbg_printf2("getSizeFromRefNum ref %d \n",ref);
return getSizeFromVec(cuda_array_size[ref],cuda_array_dim[ref]);
}
// returns array size in 5D coordinates (expanding with ones)
void get5DSize(size_t ref, size_t * mysize) {
int d;
for (d=0;d<5;d++)
if (d< cuda_array_dim[ref])
mysize[d]=cuda_array_size[ref][d];
else
mysize[d]=1;
}
// returns array size in 5D coordinates (expanding with ones)
Size5D getSize5D(size_t ref) {
Size5D mysize;
int d;
for (d=0;d<5;d++)
if (d< cuda_array_dim[ref])
mysize.s[d]=cuda_array_size[ref][d];
else
mysize.s[d]=1;
return mysize;
}
// Constructs a floating point ND vector from a Matlab reference
VecND VecNDFromRef(const mxArray * MatlabRef) {
VecND ret;
int d,dim;
double * pVal=mxGetPr(MatlabRef);
dim=(int)(mxGetM(MatlabRef) * mxGetN(MatlabRef));
for (d=0;d<CUDA_MAXDIM;d++)
if (d< dim)
ret.s[d]=(float) pVal[d];
else
ret.s[d]=0.0f;
return ret;
}
// Constructs an integer size vector from a Matlab reference
SizeND SizeNDFromRef(const mxArray * MatlabRef) {
SizeND ret;
int d,dim;
double * pVal=mxGetPr(MatlabRef);
dim=(int)(mxGetM(MatlabRef) * mxGetN(MatlabRef));
for (d=0;d<CUDA_MAXDIM;d++)
if (d< dim)
ret.s[d]=(int) pVal[d];
else
ret.s[d]=1;
return ret;
}
size_t getCudaRefNum(const mxArray * arg) {
double cudaref;
if (! mxIsDouble(arg))
mexErrMsgTxt("cuda: Obtaining reference number. Number must be a double");
cudaref = mxGetScalar(arg);
/* printf("cuda: get ref %g, next free array %d\n",cudaref,free_array); */
CHECK_CUDAREF(cudaref);
if (cuda_array_size[(size_t) cudaref] == 0) {
printf("While deleting Cuda Reference no. %ld\n",(size_t) cudaref);
mexErrMsgTxt("cuda: Trying to access non-existing cuda reference.");
}
return (size_t) cudaref;
}
bool isComplexType(size_t ref) {
return (cuda_array_type[ref] >= fftHalfSComplex);
}
size_t getTotalSize(const int dim,const size_t * sizevec) { // returns the total size in numbers (floats or complex), not accounting for complex size
size_t totalsize=1,d;
for (d=0;d<dim;d++) {
totalsize *= sizevec[d];
Dbg_printf4("Dimension %d, size %lld, total %lld\n",d,sizevec[d],totalsize);
if (sizevec[d]==0)
{
// mexErrMsgTxt("cuda: detected a zero in the size of an array. (e.g. in allocation)\n");
return 0;}
}
Dbg_printf2("Totalsize = %d\n",totalsize);
return totalsize;
}
size_t getTotalSizeFromRefNum(size_t pos) {
return getTotalSize(cuda_array_dim[pos],cuda_array_size[pos]);
}
size_t getTotalSizeFromRef(const mxArray * arg) {
return getTotalSizeFromRefNum(getCudaRefNum(arg));
}
size_t getTotalFloatSizeFromRef(const mxArray * arg) { // sizes in floating point numbers
size_t ref=getCudaRefNum(arg);
if (isComplexType(ref)) // this is a complex datatyp
return getTotalSizeFromRefNum(ref)*2;
else
return getTotalSizeFromRefNum(ref);
}
void CheckMemoryConsistency(); // Just declare, but no definition yet. See below
void PrintMemoryOverview() {
size_t p,n;