-
Notifications
You must be signed in to change notification settings - Fork 0
/
magma_compute_SVDpseudoInverse.c
1802 lines (1424 loc) · 50.9 KB
/
magma_compute_SVDpseudoInverse.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
/** @file magma_compute_SVDpseudoInverse.c
*/
#ifdef HAVE_CUDA
#include <cuda_runtime_api.h>
#include <cuda_runtime.h>
#include <cublas_v2.h>
#include <device_types.h>
#include <pthread.h>
#include <cusolverDn.h>
#ifdef HAVE_MAGMA
#include "magma_v2.h"
#include "magma_lapack.h"
#include "CommandLineInterface/CLIcore.h"
#include "CommandLineInterface/timeutils.h"
#include "COREMOD_memory/COREMOD_memory.h"
#include "COREMOD_iofits/COREMOD_iofits.h"
#include "COREMOD_tools/COREMOD_tools.h"
#include "cudacomp_types.h"
extern int INIT_MAGMA;
// queue for default magma device
extern magma_queue_t magmaqueue;
static long MAGMAloop_iter = 0;
static double *magma_h_A;
static double *magma_d_A;
static double *magma_d_AtA;
static double *magma_h_AtA;
static double *magma_w1; // eigenvalues
static double *magma_h_R;
static double *magma_h_work;
static double *magma_d_VT1;
static double *magma_h_VT1;
static double *magma_d_M2;
static double *magma_d_Ainv;
static double *magma_h_Ainv;
static double *magma_h_M2;
//static double *magma_h_S; //singular values
//static double *magma_d_U; //left singular vectors
//static double *magma_d_VT; //right singular vectors
//static double *magma_d_B;
static float *magmaf_h_A;
static float *magmaf_d_A;
static float *magmaf_d_AtA;
static float *magmaf_h_AtA;
static float *magmaf_w1; // eigenvalues
static float *magmaf_h_R;
static float *magmaf_h_work;
static float *magmaf_d_VT1;
static float *magmaf_h_VT1;
static float *magmaf_d_M2;
static float *magmaf_d_Ainv;
static float *magmaf_h_Ainv;
static float *magmaf_h_M2;
//static float *magmaf_h_S; //singular values
//static float *magmaf_d_U; //left singular vectors
//static float *magmaf_d_VT; //right singular vectors
//static float *magmaf_d_B;
static magma_int_t magma_aux_iwork[1];
static magma_int_t magma_lwork, magma_liwork;
static magma_int_t *magma_iwork;
// ==========================================
// Forward declaration(s)
// ==========================================
errno_t CUDACOMP_magma_compute_SVDpseudoInverse(
const char *ID_Rmatrix_name,
const char *ID_Cmatrix_name,
double SVDeps,
long MaxNBmodes,
const char *ID_VTmatrix_name,
int LOOPmode,
int PSINV_MODE,
__attribute__((unused)) double qdwh_s,
__attribute__((unused)) float qdwh_tol,
int testmode,
int precision,
imageID *outID
);
// ==========================================
// Command line interface wrapper function(s)
// ==========================================
static errno_t CUDACOMP_magma_compute_SVDpseudoInverse_cli()
{
if(
CLI_checkarg(1, 4) +
CLI_checkarg(2, 3) +
CLI_checkarg(3, 1) +
CLI_checkarg(4, 2) +
CLI_checkarg(5, 3) +
CLI_checkarg(6, 2) +
CLI_checkarg(7, 1) +
CLI_checkarg(8, 1)
== 0)
{
CUDACOMP_magma_compute_SVDpseudoInverse(
data.cmdargtoken[1].val.string,
data.cmdargtoken[2].val.string,
data.cmdargtoken[3].val.numf,
data.cmdargtoken[4].val.numl,
data.cmdargtoken[5].val.string,
0,
data.cmdargtoken[6].val.numl,
data.cmdargtoken[7].val.numf,
data.cmdargtoken[8].val.numf,
0,
64,
NULL);
return CLICMD_SUCCESS;
}
else
{
return CLICMD_INVALID_ARG;
}
}
// ==========================================
// Register CLI command(s)
// ==========================================
errno_t magma_compute_SVDpseudoInverse_addCLIcmd()
{
RegisterCLIcommand(
"cudacomppsinv",
__FILE__,
CUDACOMP_magma_compute_SVDpseudoInverse_cli,
"compute pseudo inverse",
"<input matrix [string]> <output pseudoinv [string]> <eps [float]> <NBmodes [long]> <VTmat [string]>",
"cudacomppsinv matA matAinv 0.01 100 VTmat 0 1e-4 1e-7",
"int CUDACOMP_magma_compute_SVDpseudoInverse(const char *ID_Rmatrix_name, const char *ID_Cmatrix_name, double SVDeps, long MaxNBmodes, const char *ID_VTmatrix_name, int LOOPmode, int PSINV_MODE, double qdwh_s, float qdwh_tol)");
return RETURN_SUCCESS;
}
/**
* @brief Computes matrix pseudo-inverse (AT A)^-1 AT, using eigenvector/eigenvalue decomposition of AT A
*
*
* Computes pseuso inverse of a matrix.\n
* Column-major representation used to match magma and lapack.\n
* When viewed as an image, matrix leading dimension is size[0] = horizontal axis. When viewed in an image viewer, the first column is on the bottom side with the first element in bottom left corner, so the matrix appears rotated counter-clockwise by 90deg from its conventional representation where first column is on the left side.\n
* Returns transpose of pseudoinverse.\n
*
*
*
* ## Matrix representation details
*
* Using column-major indexing\n
* When viewed as a FITS file, the first matrix column (= vector) appears as the bottom line of the FITS image.\n
* First matrix element is bottom left corner, second element is immediately to the right of it.
*
* Noting elements as a[row,column] = a[i,j], elements are accessed as in memory as:
* a[ j * M + i ]
*
* FITS file representation (ds9 view) starts from bottom left corner.
*
* a[000,N-1] -> a[001,N-1] -> ... -> a[M-1,N-1]
* ............................................. ^
* a[000,001] -> a[001,001] -> ... -> a[M-1,001] ^
* a[000,000] -> a[001,000] -> ... -> a[M-1,000] ^ : this is the first matrix row
*
* Note that a tall input matrix (M>N) will appear short if viewed as an image.
* To view the FITS file in the conventional matrix view, rotate by 90 deg clockwise.
*
*
*
* ## Application Notes
*
* Use LOOPmode = 1 for computing the same size SVD, same input and output location
*
* ### Use case: Response matrix to compute control matrix
*
* When using function to invert AO response matrix with AOloopControl module, input is 2D or 3D image:
* M: number of sensors (AO control) = size[0] (2D) = size[0]*size[1] (3D)
* N: number of actuators (AO control) = size[1] (2D) = size[2] (3D)
*
* We assume M>N
*
*
* ### Use case: Predictive control
*
* When using function to compute pseudo-inverse of data matrix (predictive control), input matrix is a 2D image which is the Transpose of the data matrix.
* M: number of measurements samples = size[0] (2D)
* N: dimension of each measurement = size[1] (2D)
*
* We assume M>N
*
*
*
*
* ## Algorithm details and main computation steps
*
* Notations:
* AT is transpose of A
* A+ is pseudo inverse of A
*
* Computes pseudo-inverse : A+ = (AT A)^-1 AT
* Inverse of AT A is computed by SVD
*
* SVD: A = U S V^T
* U are eigenvectors of A A^T
* V are eigenvectors of A^T A, computed at step 4 below
*
* Linear algebra reminder: equivalence between (AT A)^-1 AT and V S^-1 UT
*
* Definition of pseudoinverse:
* A+ = (AT A)^-1 AT
* singular value decomposition of A = U S VT
* A+ = ( V S UT U S VT )^-1 V S UT
* Since U is unitary, UT U = Id ->
* A+ = ( V S^2 VT )^-1 V S UT
* A+ = VT^-1 S^-2 V^-1 V S UT
* A+ = V S^-1 UT
*
* Main steps (non-QDWH):
*
* STEP 1 : Fill input data into magmaf_h_A on host
*
* STEP 2 : Copy input data to GPU -> magmaf_d_A (MxN matrix on device)
*
* STEP 3 : Compute trans(A) x A : magmaf_d_A x magmaf_d_A -> magmaf_d_AtA (NxN matrix on device)
*
* STEP 4 : Compute eigenvalues and eigenvectors of A^T A -> magmaf_d_AtA (NxN matrix on device)
* Calls magma_ssyevd_gpu :
* Compute the eigenvalues and optionally eigenvectors of a symmetric real matrix in single precision, GPU interface, big matrix.
* This function computes in single precision all eigenvalues and, optionally, eigenvectors of a real symmetric matrix A defined on the device.
* The first parameter can take the values MagmaVec,'V' or MagmaNoVec,'N' and answers the question whether the eigenvectors are desired.
* If the eigenvectors are desired, it uses a divide and conquer algorithm. The symmetric matrix A can be stored in lower (MagmaLower,'L')
* or upper (MagmaUpper,'U') mode. If the eigenvectors are desired, then on exit A contains orthonormal eigenvectors.
* The eigenvalues are stored in an array w
*
* STEP 5 : Set eigenvalue limit
*
* STEP 6 : Write eigenvectors to V^T matrix
*
* STEP 7 : Write eigenvectors/eigenvalue to magma_h_VT1 if eigenvalue > limit
* Copy to magma_d_VT1
*
* STEP 8 : Compute M2 = VT1 VT. M2 is (AT A)^-1
*
* STEP 9 : Compute Ainv = M2 A. This is the pseudo inverse
*
* @note SVDeps^2 is applied as a limit to the eigenvectors of AT A, which are equal to the squares of the singular values of A, so this is equivalent to applying SVDeps as a limit on the singular values of A
* @note When used to compute AO control matrix, N=number of actuators/modes, M=number of WFS elements
* @note EIGENVALUES are good to about 1e-6 of peak eigenvalue in single precision, much better with double precision
* @note 2.5x faster in single precision
*
* @note If provided with an additional data matrix named "", an additional Matrix Matrix product between Ainv and the provided matrix will be performed. This feature is used for predictive control and sensor fusion to create a control matrix.
*
* TEST MODE OUTPOUT
*
* non-QDWH mode:
*
* test_mA.fits content of magmaf_h_A
* test_mAtA.fits content of transpose(A) x A = magmaf_d_AtA (output of STEP 3)
* test_eigenv.dat list of eigenvalues
* test_SVDmodes.log number of singular values kept
* test_mM2.fits matrix M2 (output of STEP 8)
* test_mVT.fits matrix transpose(V) = eigenvectors (output of step 6)
* test_mAinv.fits transpose of pseudoinverse
* test_AinvA.fits product of Ainv with A, should be close to identity matrix size NxN
*
*
* QDWH mode:
*
* test_mA.QDWH.fits content of magmaf_h_A
* test_Aorig.QDWH.txt content of magmaf_h_A prior to calling psinv function
* test_sv.QDWH.dat singular values after call to psinv function
* test_SVDmodes.QDWH.log number of singular values kept (note : independent form pseudo-inverse computation)
* test_mAinv.QDWH.fits transpose of pseudoinverse
* test_AinvA.QDWH.fits product of Ainv with A, should be close to identity matrix size NxN
*/
errno_t CUDACOMP_magma_compute_SVDpseudoInverse(
const char *ID_Rmatrix_name,
const char *ID_Cmatrix_name,
double SVDeps,
long MaxNBmodes,
const char *ID_VTmatrix_name,
int LOOPmode,
__attribute__((unused)) int PSINV_MODE,
__attribute__((unused)) double qdwh_s,
__attribute__((unused)) float qdwh_tol,
int testmode,
int precision,
imageID *outID
)
{
DEBUG_TRACE_FSTART();
// input identifier
long ID_Rmatrix;
uint8_t datatype;
// output result
imageID ID_Cmatrix;
// matrix size
magma_int_t N, M;
magma_int_t info;
imageID ID_PFfmdat = -1; // optional final M-M product
/// Timing tests
// int timing = 1; /**< 1 if timing test ON, 0 otherwise */
// timers
struct timespec t0, t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13;
// times in sec
double t01d, t12d, t23d, t34d, t45d, t56d, t67d, t78d, t89d, t910d, t1011d, t1112d, t1213d, t013d;
long MaxNBmodes1, mode;
// TESTING FLAGS
int VERBOSE_CUDACOMP_magma_compute_SVDpseudoInverse = 1;
// 1 if single precision, 0 if double precision
int MAGMAfloat = 0;
if(precision == 32)
{
MAGMAfloat = 1;
}
int magmaXmode = 0; // expert mode, uses magma_ssyevdx_gpu
// this does not speed up computation
magma_int_t mout;
int dAinvMODE = 0;
// if(timing==1)
clock_gettime(CLOCK_REALTIME, &t0);
/**
*
*
* MATRIX REPRESENTATION CONVENTION
*
*
*/
///
/// MAGMA uses column-major matrices. For matrix A with dimension (M,N), element A(i,j) is A[ j*M + i]
/// i = 0 ... M : row index, coefficient of a vector
/// j = 0 ... N : column index, vector index
/// M is the matrix leading dimension = lda
/// M = number of rows
/// N = number of columns
/// (assuming here that vector = column of the matrix)
///
ID_Rmatrix = image_ID(ID_Rmatrix_name);
datatype = data.image[ID_Rmatrix].md[0].datatype;
if(data.image[ID_Rmatrix].md[0].naxis == 3)
{
/// each column (N=cst) of A is a z=cst slice of image Rmatrix
M = data.image[ID_Rmatrix].md[0].size[0] * data.image[ID_Rmatrix].md[0].size[1];
N = data.image[ID_Rmatrix].md[0].size[2];
if(VERBOSE_CUDACOMP_magma_compute_SVDpseudoInverse == 1)
{
printf("3D image -> %ld %ld\n", (long) M, (long) N);
fflush(stdout);
}
}
else
{
/// each column (N=cst) of A is a line (y=cst) of Rmatrix (90 deg rotation)
M = data.image[ID_Rmatrix].md[0].size[0];
N = data.image[ID_Rmatrix].md[0].size[1];
if(VERBOSE_CUDACOMP_magma_compute_SVDpseudoInverse == 1)
{
printf("2D image -> %ld %ld\n", (long) M, (long) N);
fflush(stdout);
}
}
//TEST
//for(ii=0;ii<N;ii++)
//data.image[ID_Rmatrix].array.F[ii*M+ii] += 1.0;
if(VERBOSE_CUDACOMP_magma_compute_SVDpseudoInverse == 1)
{
printf("magma : M = %ld , N = %ld\n", (long) M, (long) N);
fflush(stdout);
}
/// Initialize MAGMA if needed
if(INIT_MAGMA == 0)
{
if(VERBOSE_CUDACOMP_magma_compute_SVDpseudoInverse == 1)
{
printf("INITIALIZE MAGMA\n");
fflush(stdout);
}
magma_init();
magma_print_environment();
INIT_MAGMA = 1;
}
// =================================================================
// MEMORY ALLOCATION
//
// (for single precision, magma_ -> magmaf_)
//
// ----- QSWHpartial --------
// magma_h_A
// magma_d_A
// magma_h_S
// magma_d_U
// magma_d_VT
// magma_d_B
//
// ----- std magma ----------
// magma_h_A
// magma_d_A
// magma_h_AtA
// magma_d_AtA
// magma_h_VT1
// magma_d_VT1
// magma_d_M2
//
// =================================================================
if(VERBOSE_CUDACOMP_magma_compute_SVDpseudoInverse == 1)
{
printf(">>> ALLOCATION FOR PSINV MAGMA M=%ld N=%ld\n", (long) M, (long) N);
fflush(stdout);
}
if(MAGMAloop_iter == 0) /// memory is only allocated on first pass
{
printf(">>> LINE %d\n", __LINE__);//TBE
if(MAGMAfloat == 0)
{
printf(">>> LINE %d\n", __LINE__);//TBE
TESTING_MALLOC_CPU(magma_h_A, double, M * N);
printf(">>> LINE %d\n", __LINE__);//TBE
//TESTING_MALLOC_DEV(magma_d_A, double, M * N);
if ( MAGMA_SUCCESS !=
magma_malloc( (void**) &magma_d_A, (size_t) sizeof(double)*M*N )) {
printf(">>> LINE %d\n", __LINE__);//TBE
fprintf( stderr, "!!!! magma_malloc failed\n");
magma_finalize();
exit(-1);
}
printf(">>> LINE %d\n", __LINE__);//TBE
TESTING_MALLOC_CPU(magma_h_AtA, double, N * N);
printf(">>> LINE %d\n", __LINE__);//TBE
TESTING_MALLOC_DEV(magma_d_AtA, double, N * N);
printf(">>> LINE %d\n", __LINE__);//TBE
TESTING_MALLOC_CPU(magma_h_VT1, double, N * N);
printf(">>> LINE %d\n", __LINE__);//TBE
TESTING_MALLOC_DEV(magma_d_VT1, double, N * N);
printf(">>> LINE %d\n", __LINE__);//TBE
TESTING_MALLOC_DEV(magma_d_M2, double, N * N);
printf(">>> LINE %d\n", __LINE__);//TBE
}
else
{
printf(">>> LINE %d\n", __LINE__);//TBE
TESTING_MALLOC_CPU(magmaf_h_A, float, M * N);
printf("Allocating magmaf_d_A on device ...\n");
fflush(stdout);
TESTING_MALLOC_DEV(magmaf_d_A, float, M * N);
printf(" ... done\n");
fflush(stdout);
TESTING_MALLOC_CPU(magmaf_h_AtA, float, N * N);
TESTING_MALLOC_DEV(magmaf_d_AtA, float, N * N);
TESTING_MALLOC_CPU(magmaf_h_VT1, float, N * N);
TESTING_MALLOC_DEV(magmaf_d_VT1, float, N * N);
TESTING_MALLOC_DEV(magmaf_d_M2, float, N * N);
}
}
printf(">>> LINE %d\n", __LINE__);//TBE
if(VERBOSE_CUDACOMP_magma_compute_SVDpseudoInverse == 1)
{
printf("MAGMA READY\n");
fflush(stdout);
}
printf(">>> LINE %d\n", __LINE__);//TBE
if(MAGMAloop_iter == 0)
{
magma_queue_create(0, &magmaqueue);
}
printf(">>> LINE %d\n", __LINE__);//TBE
if(VERBOSE_CUDACOMP_magma_compute_SVDpseudoInverse == 1)
{
printf("MAGMA: CREATE QUEUE\n");
fflush(stdout);
}
printf(">>> LINE %d\n", __LINE__);//TBE
// if(timing==1)
magma_queue_sync(magmaqueue);
clock_gettime(CLOCK_REALTIME, &t1);
printf(">>> LINE %d\n", __LINE__);//TBE
// ****************************************************
// STEP 1 : Fill input data into magmaf_h_A on host
// ****************************************************
// magma array is column-major.
//
printf(">>> LINE %d\n", __LINE__);//TBE
if(datatype == _DATATYPE_FLOAT)
{
printf(">>> LINE %d\n", __LINE__);//TBE
if(MAGMAfloat == 1)
{
if((testmode == 1)) // need magmaf_h_A, otherwise, straight to magmaf_d_A
{
memcpy(magmaf_h_A, data.image[ID_Rmatrix].array.F, sizeof(float)*M * N);
// copy from host to device
magma_ssetmatrix(M, N, magmaf_h_A, M, magmaf_d_A, M, magmaqueue);
}
else
{
magma_ssetmatrix(M, N, data.image[ID_Rmatrix].array.F, M, magmaf_d_A, M,
magmaqueue);
}
}
else
{
for(long ii = 0; ii < M * N; ii++)
{
magma_h_A[ii] = data.image[ID_Rmatrix].array.F[ii];
}
// copy from host to device
magma_dsetmatrix(M, N, magma_h_A, M, magma_d_A, M, magmaqueue);
}
}
else
{
printf(">>> LINE %d\n", __LINE__);//TBE
if(MAGMAfloat == 1)
{
for(long ii = 0; ii < M * N; ii++)
{
magmaf_h_A[ii] = data.image[ID_Rmatrix].array.D[ii];
}
// copy from host to device
magma_ssetmatrix(M, N, magmaf_h_A, M, magmaf_d_A, M, magmaqueue);
}
else
{
if(testmode == 1) // need magma_h_A for testing
{
//for(ii=0; ii<M*N; ii++)
// magma_h_A[ii] = data.image[ID_Rmatrix].array.D[ii];
memcpy(magma_h_A, data.image[ID_Rmatrix].array.D, sizeof(double)*M * N);
// copy from host to device
magma_dsetmatrix(M, N, magma_h_A, M, magma_d_A, M, magmaqueue);
}
else
{
magma_dsetmatrix(M, N, data.image[ID_Rmatrix].array.D, M, magma_d_A, M,
magmaqueue);
}
}
}
printf(">>> LINE %d\n", __LINE__);//TBE
if(testmode == 1)
{
imageID ID_A;
FUNC_CHECK_RETURN(
create_2Dimage_ID("mA", M, N, &ID_A));
if(MAGMAfloat == 1)
{
for(long ii = 0; ii < M * N; ii++)
{
data.image[ID_A].array.F[ii] = magmaf_h_A[ii];
}
}
else
{
for(long ii = 0; ii < M * N; ii++)
{
data.image[ID_A].array.F[ii] = magma_h_A[ii];
}
}
FUNC_CHECK_RETURN(
save_fits("mA", "test_mA.QDWH.fits")
);
FUNC_CHECK_RETURN(
delete_image_ID("mA", DELETE_IMAGE_ERRMODE_WARNING)
);
}
// ****************************************************
// STEP 2 : Copy input data from CPU to GPU
// ****************************************************
if(VERBOSE_CUDACOMP_magma_compute_SVDpseudoInverse == 1)
{
printf("MAGMA: OFFLOAD TO THE GPU\n");
fflush(stdout);
}
// copy from host to device
//
if(MAGMAloop_iter == 0)
{
if(MAGMAfloat == 1)
{
TESTING_MALLOC_CPU(magmaf_h_Ainv, float, N * M);
}
else
{
TESTING_MALLOC_CPU(magma_h_Ainv, double, N * M);
}
}
{
// START STD MAGMA ===============================================
magma_queue_sync(magmaqueue);
clock_gettime(CLOCK_REALTIME, &t2);
if(VERBOSE_CUDACOMP_magma_compute_SVDpseudoInverse == 1)
{
printf("MAGMA: COMPUTE trans(A) x A\n");
fflush(stdout);
}
// ****************************************************
// STEP 3 : Compute trans(A) x A : magmaf_d_A x magmaf_d_A -> magmaf_d_AtA (NxN matrix on device)
// ****************************************************
if(MAGMAfloat == 1)
{
magma_ssyrk(MagmaLower, MagmaTrans, N, M, 1.0, magmaf_d_A, M, 0.0, magmaf_d_AtA,
N, magmaqueue);
magmablas_ssymmetrize(MagmaLower, N, magmaf_d_AtA, N, magmaqueue);
// Slower alternative
//magma_sgemm( MagmaTrans, MagmaNoTrans, N, N, M, 1.0, magmaf_d_A, M, magmaf_d_A, M, 0.0, magmaf_d_AtA, N, magmaqueue);
}
else
{
magma_dgemm(MagmaTrans, MagmaNoTrans, N, N, M, 1.0, magma_d_A, M, magma_d_A, M,
0.0, magma_d_AtA, N, magmaqueue);
}
if(testmode == 1)
{
// copy from GPU to CPU
if(MAGMAfloat == 1)
{
magma_sgetmatrix(N, N, magmaf_d_AtA, N, magmaf_h_AtA, N, magmaqueue);
}
else
{
magma_dgetmatrix(N, N, magma_d_AtA, N, magma_h_AtA, N, magmaqueue);
}
imageID ID_AtA;
FUNC_CHECK_RETURN(create_2Dimage_ID("mAtA", N, N, &ID_AtA));
if(MAGMAfloat == 1)
{
for(long ii = 0; ii < N * N; ii++)
{
data.image[ID_AtA].array.F[ii] = magmaf_h_AtA[ii];
}
}
else
{
for(long ii = 0; ii < N * N; ii++)
{
data.image[ID_AtA].array.F[ii] = magma_h_AtA[ii];
}
}
FUNC_CHECK_RETURN(save_fits("mAtA", "test_mAtA.fits"));
FUNC_CHECK_RETURN(delete_image_ID("mAtA", DELETE_IMAGE_ERRMODE_IGNORE));
}
//if(timing==1)
magma_queue_sync(magmaqueue);
clock_gettime(CLOCK_REALTIME, &t3);
// ****************************************************
// STEP 4 : Compute eigenvalues and eigenvectors of AT A -> magmaf_d_AtA (NxN matrix on device)
//
// SVD of AT A = V S^2 VT
// calls function magma_ssyevd_gpu
//
//
// ****************************************************
if(VERBOSE_CUDACOMP_magma_compute_SVDpseudoInverse == 1)
{
printf("COMPUTE eigenvalues and eigenvectors of AT A\n");
fflush(stdout);
}
if(MAGMAloop_iter == 0)
{
// get workspace size
if(MAGMAfloat == 1)
{
float auxf_work[1];
if(magmaXmode == 1)
{
magma_ssyevdx_gpu(MagmaVec, MagmaRangeI, MagmaLower, N, NULL, N, 0.0, 1.0,
N - MaxNBmodes, N, NULL, NULL, NULL, N, auxf_work, -1, magma_aux_iwork, -1,
&info);
}
else
{
magma_ssyevd_gpu(MagmaVec, MagmaLower, N, NULL, N,
NULL, NULL, N, auxf_work, -1, magma_aux_iwork, -1, &info);
}
// -> change to 2-stage magma SVD
// evd -> evr
// PALSMA
// alt -> LQ reduction -> SVD magma_dgsvd (more stable numerically)
magma_lwork = (magma_int_t) MAGMA_S_REAL(auxf_work[0]);
}
else
{
double aux_work[1];
magma_dsyevd_gpu(MagmaVec, MagmaLower, N, NULL, N, NULL, NULL, N, aux_work, -1,
magma_aux_iwork, -1, &info);
magma_lwork = (magma_int_t) MAGMA_S_REAL(aux_work[0]);
}
magma_liwork = magma_aux_iwork[0];
}
if(VERBOSE_CUDACOMP_magma_compute_SVDpseudoInverse == 1)
{
printf("MAGMA: allocate & compute\n");
fflush(stdout);
}
if(MAGMAloop_iter == 0)
{
if(MAGMAfloat == 1)
{
TESTING_MALLOC_CPU(magma_iwork, magma_int_t, magma_liwork);
TESTING_MALLOC_PIN(magmaf_h_work, float, magma_lwork);
TESTING_MALLOC_CPU(magmaf_w1, float, N);
TESTING_MALLOC_PIN(magmaf_h_R, float, N * N);
}
else
{
TESTING_MALLOC_CPU(magma_iwork, magma_int_t, magma_liwork);
TESTING_MALLOC_PIN(magma_h_work, double, magma_lwork);
TESTING_MALLOC_CPU(magma_w1, double, N);
TESTING_MALLOC_PIN(magma_h_R, double, N * N);
}
}
//if(timing==1)
magma_queue_sync(magmaqueue);
clock_gettime(CLOCK_REALTIME, &t4);
if(MAGMAfloat == 1)
{
// printf("============== %s %d\n", __FILE__, __LINE__);
// fflush(stdout);
if(VERBOSE_CUDACOMP_magma_compute_SVDpseudoInverse == 1)
{
printf(" -> FLOAT [%d] magma_ssyevd_gpu -> ", magmaXmode);
fflush(stdout);
}
// SSYEVD computes all eigenvalues and, optionally, eigenvectors of a real symmetric matrix A
if(magmaXmode == 1)
{
magma_ssyevdx_gpu(MagmaVec, MagmaRangeI, MagmaLower, N, magmaf_d_AtA, N, 0.0,
1.0, N - MaxNBmodes, N, &mout, magmaf_w1, magmaf_h_R, N, magmaf_h_work,
magma_lwork, magma_iwork, magma_liwork, &info);
}
else
{
magma_ssyevd_gpu(MagmaVec, MagmaLower, N, magmaf_d_AtA, N,
magmaf_w1, magmaf_h_R, N, magmaf_h_work, magma_lwork, magma_iwork, magma_liwork,
&info);
}
// printf("============== %s %d\n", __FILE__, __LINE__);
// fflush(stdout);
if(VERBOSE_CUDACOMP_magma_compute_SVDpseudoInverse == 1)
{
printf(" DONE\n");
fflush(stdout);
}
}
else
{
if(VERBOSE_CUDACOMP_magma_compute_SVDpseudoInverse == 1)
{
printf(" -> DOUBLE [%d] magma_dsyevd_gpu -> ", magmaXmode);
fflush(stdout);
}
// CODE CAN HANG HERE - THIS HAPPENS ONCE OUT OF multiple 1000s EXECUTIONS WHEN RUNNING IN A LOOP.. SEEMS TO BE A MAGMA ISSUE
// SSYEVD computes all eigenvalues and, optionally, eigenvectors of a real symmetric matrix A
magma_dsyevd_gpu(MagmaVec, MagmaLower, N, magma_d_AtA, N, magma_w1, magma_h_R,
N, magma_h_work, magma_lwork, magma_iwork, magma_liwork, &info);
if(VERBOSE_CUDACOMP_magma_compute_SVDpseudoInverse == 1)
{
printf(" DONE\n");
fflush(stdout);
}
}
if(LOOPmode == 0)
{
TESTING_FREE_CPU(magma_iwork);
if(MAGMAfloat == 1)
{
TESTING_FREE_PIN(magmaf_h_R);
}
else
{
TESTING_FREE_PIN(magma_h_R);
}
if(MAGMAfloat == 1)
{
TESTING_FREE_PIN(magma_h_work);
}
else
{
TESTING_FREE_PIN(magmaf_h_work);
}
}