This repository has been archived by the owner on Nov 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
/
c_lapack_api.h
1093 lines (995 loc) · 59.2 KB
/
c_lapack_api.h
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
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*!
* \file c_lapack_api.h
* \brief Unified interface for CPU-based LAPACK calls.
* Purpose is to hide the platform specific differences.
*/
#ifndef MXNET_OPERATOR_C_LAPACK_API_H_
#define MXNET_OPERATOR_C_LAPACK_API_H_
// Manually maintained list of LAPACK interfaces that can be used
// within MXNET. Conventions:
// - We should only import LAPACK-functions that are useful and
// ensure that we support them most efficiently on CPU/GPU. As an
// example take "potrs": It can be emulated by two calls to
// "trsm" (from BLAS3) so not really needed from functionality point
// of view. In addition, trsm on GPU supports batch-mode processing
// which is much more efficient for a bunch of smaller matrices while
// there is no such batch support for potrs. As a result, we may
// not support "potrs" internally and if we want to expose it to the user as
// a convenience operator at some time, then we may implement it internally
// as a sequence of trsm.
// - Interfaces must be compliant with lapacke.h in terms of signature and
// naming conventions so wrapping a function "foo" which has the
// signature
// lapack_int LAPACKE_foo(int, char, lapack_int, float* , lapack_int)
// within lapacke.h should result in a wrapper with the following signature
// int MXNET_LAPACK_foo(int, char, int, float* , int)
// Note that function signatures in lapacke.h will always have as first
// argument the storage order (row/col-major). All wrappers have to support
// that argument. The underlying fortran functions will always assume a
// column-major layout.
// - In the (usual) case that a wrapper is called specifying row-major storage
// order of input/output data, there are two ways to handle this:
// 1) The wrapper may support this without allocating any additional memory
// for example by exploiting the fact that a matrix is symmetric and switching
// certain flags (upper/lower triangular) when calling the fortran code.
// 2) The wrapper may cause a runtime error. In that case it should be clearly
// documented that these functions do only support col-major layout.
// Rationale: This is a low level interface that is not expected to be called
// directly from many upstream functions. Usually all calls should go through
// the tensor-based interfaces in linalg.h which simplify calls to lapack further
// and are better suited to handle additional transpositions that may be necessary.
// Also we want to push allocation of temporary storage higher up in order to
// allow more efficient re-use of temporal storage. And don't want to plaster
// these interfaces here with additional requirements of providing buffers.
// - It is desired to add some basic checking in the C++-wrappers in order
// to catch simple mistakes when calling these wrappers.
// - Must support compilation without lapack-package but issue runtime error in this case.
#include <dmlc/logging.h>
#include "mshadow/tensor.h"
using namespace mshadow;
// Will cause clash with MKL/OpenBLAS fortran layer headers
#if MSHADOW_USE_MKL == 0 && MXNET_USE_LAPACKE_INTERFACE == 0
extern "C" {
// Fortran signatures
#ifdef __ANDROID__
#define MXNET_LAPACK_FSIGNATURE1(func, dtype) \
int func##_(char* uplo, int* n, dtype* a, int* lda, int* info);
#else
#define MXNET_LAPACK_FSIGNATURE1(func, dtype) \
void func##_(char* uplo, int* n, dtype* a, int* lda, int* info);
#endif
MXNET_LAPACK_FSIGNATURE1(spotrf, float)
MXNET_LAPACK_FSIGNATURE1(dpotrf, double)
MXNET_LAPACK_FSIGNATURE1(spotri, float)
MXNET_LAPACK_FSIGNATURE1(dpotri, double)
void dposv_(char* uplo, int* n, int* nrhs, double* a, int* lda, double* b, int* ldb, int* info);
void sposv_(char* uplo, int* n, int* nrhs, float* a, int* lda, float* b, int* ldb, int* info);
// Note: GELQF in row-major (MXNet) becomes GEQRF in column-major (LAPACK).
// Also, m and n are flipped, compared to the row-major version
#define MXNET_LAPACK_FSIG_GEQRF(func, dtype) \
void func##_(int* m, int* n, dtype* a, int* lda, dtype* tau, dtype* work, int* lwork, int* info);
MXNET_LAPACK_FSIG_GEQRF(sgeqrf, float)
MXNET_LAPACK_FSIG_GEQRF(dgeqrf, double)
// Note: ORGLQ in row-major (MXNet) becomes ORGQR in column-major (LAPACK)
// Also, m and n are flipped, compared to the row-major version
#define MXNET_LAPACK_FSIG_ORGQR(func, dtype) \
void func##_( \
int* m, int* n, int* k, dtype* a, int* lda, dtype* tau, dtype* work, int* lwork, int* info);
MXNET_LAPACK_FSIG_ORGQR(sorgqr, float)
MXNET_LAPACK_FSIG_ORGQR(dorgqr, double)
#define MXNET_LAPACK_FSIG_SYEVD(func, dtype) \
void func##_(char* jobz, \
char* uplo, \
int* n, \
dtype* a, \
int* lda, \
dtype* w, \
dtype* work, \
int* lwork, \
int* iwork, \
int* liwork, \
int* info);
MXNET_LAPACK_FSIG_SYEVD(ssyevd, float)
MXNET_LAPACK_FSIG_SYEVD(dsyevd, double)
#define MXNET_LAPACK_FSIG_GESVD(func, dtype) \
void func##_(char* jobu, \
char* jobvt, \
int* m, \
int* n, \
dtype* a, \
int* lda, \
dtype* s, \
dtype* u, \
int* ldu, \
dtype* vt, \
int* ldvt, \
dtype* work, \
int* lwork, \
int* info);
MXNET_LAPACK_FSIG_GESVD(sgesvd, float)
MXNET_LAPACK_FSIG_GESVD(dgesvd, double)
#ifdef __ANDROID__
#define MXNET_LAPACK_FSIG_GETRF(func, dtype) \
int func##_(int* m, int* n, dtype* a, int* lda, int* ipiv, int* info);
#else
#define MXNET_LAPACK_FSIG_GETRF(func, dtype) \
void func##_(int* m, int* n, dtype* a, int* lda, int* ipiv, int* info);
#endif
MXNET_LAPACK_FSIG_GETRF(sgetrf, float)
MXNET_LAPACK_FSIG_GETRF(dgetrf, double)
#ifdef __ANDROID__
#define MXNET_LAPACK_FSIG_GETRI(func, dtype) \
int func##_(int* n, dtype* a, int* lda, int* ipiv, dtype* work, int* lwork, int* info);
#else
#define MXNET_LAPACK_FSIG_GETRI(func, dtype) \
void func##_(int* n, dtype* a, int* lda, int* ipiv, dtype* work, int* lwork, int* info);
#endif
MXNET_LAPACK_FSIG_GETRI(sgetri, float)
MXNET_LAPACK_FSIG_GETRI(dgetri, double)
#ifdef __ANDROID__
#define MXNET_LAPACK_FSIG_GESV(func, dtype) \
int func##_(int* n, int* nrhs, dtype* a, int* lda, int* ipiv, dtype* b, int* ldb, int* info);
#else
#define MXNET_LAPACK_FSIG_GESV(func, dtype) \
void func##_(int* n, int* nrhs, dtype* a, int* lda, int* ipiv, dtype* b, int* ldb, int* info);
#endif
MXNET_LAPACK_FSIG_GESV(sgesv, float)
MXNET_LAPACK_FSIG_GESV(dgesv, double)
#ifdef __ANDROID__
#define MXNET_LAPACK_FSIG_GESDD(func, dtype) \
int func##_(char* jobz, \
int* m, \
int* n, \
dtype* a, \
int* lda, \
dtype* s, \
dtype* u, \
int* ldu, \
dtype* vt, \
int* ldvt, \
dtype* work, \
int* lwork, \
int* iwork, \
int* info);
#else
#define MXNET_LAPACK_FSIG_GESDD(func, dtype) \
void func##_(char* jobz, \
int* m, \
int* n, \
dtype* a, \
int* lda, \
dtype* s, \
dtype* u, \
int* ldu, \
dtype* vt, \
int* ldvt, \
dtype* work, \
int* lwork, \
int* iwork, \
int* info);
#endif
MXNET_LAPACK_FSIG_GESDD(sgesdd, float)
MXNET_LAPACK_FSIG_GESDD(dgesdd, double)
#ifdef __ANDROID__
#define MXNET_LAPACK_FSIG_GEEV(func, dtype) \
int func##_(char* jobvl, \
char* jobvr, \
int* n, \
dtype* a, \
int* lda, \
dtype* wr, \
dtype* wi, \
dtype* vl, \
int* ldvl, \
dtype* vr, \
int* ldvr, \
dtype* work, \
int* lwork, \
int* info);
#else
#define MXNET_LAPACK_FSIG_GEEV(func, dtype) \
void func##_(char* jobvl, \
char* jobvr, \
int* n, \
dtype* a, \
int* lda, \
dtype* wr, \
dtype* wi, \
dtype* vl, \
int* ldvl, \
dtype* vr, \
int* ldvr, \
dtype* work, \
int* lwork, \
int* info);
#endif
MXNET_LAPACK_FSIG_GEEV(sgeev, float)
MXNET_LAPACK_FSIG_GEEV(dgeev, double)
#ifdef __ANDROID__
#define MXNET_LAPACK_FSIG_GELSD(func, dtype) \
int func##_(int* m, \
int* n, \
int* nrhs, \
dtype* a, \
int* lda, \
dtype* b, \
int* ldb, \
dtype* s, \
dtype* rcond, \
int* rank, \
dtype* work, \
int* lwork, \
int* iwork, \
int* info);
#else
#define MXNET_LAPACK_FSIG_GELSD(func, dtype) \
void func##_(int* m, \
int* n, \
int* nrhs, \
dtype* a, \
int* lda, \
dtype* b, \
int* ldb, \
dtype* s, \
dtype* rcond, \
int* rank, \
dtype* work, \
int* lwork, \
int* iwork, \
int* info);
#endif
MXNET_LAPACK_FSIG_GELSD(sgelsd, float)
MXNET_LAPACK_FSIG_GELSD(dgelsd, double)
}
#endif // MSHADOW_USE_MKL == 0
#define CHECK_LAPACK_UPLO(a) \
CHECK(a == 'U' || a == 'L') << "neither L nor U specified as triangle in lapack call";
inline char loup(char uplo, bool invert) {
return invert ? (uplo == 'U' ? 'L' : 'U') : uplo;
}
/*!
* \brief Transpose matrix data in memory
*
* Equivalently we can see it as flipping the layout of the matrix
* between row-major and column-major.
*
* \param m number of rows of input matrix a
* \param n number of columns of input matrix a
* \param b output matrix
* \param ldb leading dimension of b
* \param a input matrix
* \param lda leading dimension of a
*/
template <typename xpu, typename DType>
inline void flip(int m, int n, DType* b, int ldb, DType* a, int lda) {
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
b[j * ldb + i] = a[i * lda + j];
}
#if (MXNET_USE_LAPACK && (MSHADOW_USE_MKL || MXNET_USE_LAPACKE_INTERFACE))
#if MSHADOW_USE_MKL
#include <mkl_lapacke.h>
#else
#if MXNET_USE_ILP64_LAPACKE
#define lapack_int int64_t
#endif
// prevent multiple inclusion of complex.h in lapacke.h
#define lapack_complex_float float _Complex
#define lapack_complex_double double _Complex
#include <lapacke.h>
#endif
#define MXNET_LAPACK_ROW_MAJOR LAPACK_ROW_MAJOR
#define MXNET_LAPACK_COL_MAJOR LAPACK_COL_MAJOR
// These function have already matching signature.
#define MXNET_LAPACK_spotrf LAPACKE_spotrf
#define MXNET_LAPACK_dpotrf LAPACKE_dpotrf
#define MXNET_LAPACK_spotri LAPACKE_spotri
#define MXNET_LAPACK_dpotri LAPACKE_dpotri
#define mxnet_lapack_sposv LAPACKE_sposv
#define mxnet_lapack_dposv LAPACKE_dposv
#define MXNET_LAPACK_dgesv LAPACKE_dgesv
#define MXNET_LAPACK_sgesv LAPACKE_sgesv
// The following functions differ in signature from the
// MXNET_LAPACK-signature and have to be wrapped.
#define MXNET_LAPACK_CWRAP_GELQF(prefix, dtype) \
inline int MXNET_LAPACK_##prefix##gelqf(int matrix_layout, \
lapack_index_t m, \
lapack_index_t n, \
dtype* a, \
lapack_index_t lda, \
dtype* tau, \
dtype* work, \
lapack_index_t lwork) { \
if (lwork != -1) { \
return LAPACKE_##prefix##gelqf(matrix_layout, m, n, a, lda, tau); \
} \
*work = 0; \
return 0; \
}
MXNET_LAPACK_CWRAP_GELQF(s, float)
MXNET_LAPACK_CWRAP_GELQF(d, double)
#define MXNET_LAPACK_CWRAP_ORGLQ(prefix, dtype) \
inline int MXNET_LAPACK_##prefix##orglq(int matrix_layout, \
lapack_index_t m, \
lapack_index_t n, \
dtype* a, \
lapack_index_t lda, \
dtype* tau, \
dtype* work, \
lapack_index_t lwork) { \
if (lwork != -1) { \
return LAPACKE_##prefix##orglq(matrix_layout, m, n, m, a, lda, tau); \
} \
*work = 0; \
return 0; \
}
MXNET_LAPACK_CWRAP_ORGLQ(s, float)
MXNET_LAPACK_CWRAP_ORGLQ(d, double)
#define MXNET_LAPACK_CWRAP_GEQRF(prefix, dtype) \
inline int MXNET_LAPACK_##prefix##geqrf(int matrix_layout, \
lapack_index_t m, \
lapack_index_t n, \
dtype* a, \
lapack_index_t lda, \
dtype* tau, \
dtype* work, \
lapack_index_t lwork) { \
if (lwork != -1) { \
return LAPACKE_##prefix##geqrf(matrix_layout, m, n, a, lda, tau); \
} \
*work = 0; \
return 0; \
}
MXNET_LAPACK_CWRAP_GEQRF(s, float)
MXNET_LAPACK_CWRAP_GEQRF(d, double)
#define MXNET_LAPACK_CWRAP_ORGQR(prefix, dtype) \
inline int MXNET_LAPACK_##prefix##orgqr(int matrix_layout, \
lapack_index_t m, \
lapack_index_t n, \
lapack_index_t k, \
dtype* a, \
lapack_index_t lda, \
dtype* tau, \
dtype* work, \
lapack_index_t lwork) { \
if (lwork != -1) { \
return LAPACKE_##prefix##orgqr(matrix_layout, m, n, k, a, lda, tau); \
} \
*work = 0; \
return 0; \
}
MXNET_LAPACK_CWRAP_ORGQR(s, float)
MXNET_LAPACK_CWRAP_ORGQR(d, double)
// This has to be called internally in COL_MAJOR format even when matrix_layout
// is row-major as otherwise the eigenvectors would be returned as cols in a
// row-major matrix layout (see MKL documentation).
// We also have to allocate at least one DType element as workspace as the
// calling code assumes that the workspace has at least that size.
#define MXNET_LAPACK_CWRAP_SYEVD(prefix, dtype) \
inline int MXNET_LAPACK_##prefix##syevd(int matrix_layout, \
char uplo, \
lapack_index_t n, \
dtype* a, \
lapack_index_t lda, \
dtype* w, \
dtype* work, \
lapack_index_t lwork, \
lapack_index_t* iwork, \
lapack_index_t liwork) { \
if (lwork != -1) { \
char o(loup(uplo, (matrix_layout == MXNET_LAPACK_ROW_MAJOR))); \
return LAPACKE_##prefix##syevd(LAPACK_COL_MAJOR, 'V', o, n, a, lda, w); \
} \
*work = 1; \
*iwork = 0; \
return 0; \
}
MXNET_LAPACK_CWRAP_SYEVD(s, float)
MXNET_LAPACK_CWRAP_SYEVD(d, double)
#define MXNET_LAPACK_sgetrf LAPACKE_sgetrf
#define MXNET_LAPACK_dgetrf LAPACKE_dgetrf
// Internally A is factorized as U * L * VT, and (according to the tech report)
// we want to factorize it as UT * L * V, so we pass ut as u and v as vt.
// We also have to allocate at least m - 1 DType elements as workspace as the internal
// LAPACKE function needs it to store `superb`. (see MKL documentation)
#define MXNET_LAPACK_CWRAP_GESVD(prefix, dtype) \
inline int MXNET_LAPACK_##prefix##gesvd(int matrix_layout, \
lapack_index_t m, \
lapack_index_t n, \
dtype* ut, \
lapack_index_t ldut, \
dtype* s, \
dtype* v, \
lapack_index_t ldv, \
dtype* work, \
lapack_index_t lwork) { \
if (lwork != -1) { \
return LAPACKE_##prefix##gesvd( \
matrix_layout, 'S', 'O', m, n, v, ldv, s, ut, ldut, v, ldv, work); \
} \
*work = m - 1; \
return 0; \
}
MXNET_LAPACK_CWRAP_GESVD(s, float)
MXNET_LAPACK_CWRAP_GESVD(d, double)
// Computes the singular value decomposition of a general rectangular matrix
// using a divide and conquer method.
#define MXNET_LAPACK_CWRAP_GESDD(prefix, dtype) \
inline int MXNET_LAPACK_##prefix##gesdd(int matrix_layout, \
lapack_index_t m, \
lapack_index_t n, \
dtype* a, \
lapack_index_t lda, \
dtype* s, \
dtype* u, \
lapack_index_t ldu, \
dtype* vt, \
lapack_index_t ldvt, \
dtype* work, \
lapack_index_t lwork, \
lapack_index_t* iwork) { \
if (lwork != -1) { \
return LAPACKE_##prefix##gesdd(matrix_layout, 'O', m, n, a, lda, s, u, ldu, vt, ldvt); \
} \
*work = 0; \
return 0; \
}
MXNET_LAPACK_CWRAP_GESDD(s, float)
MXNET_LAPACK_CWRAP_GESDD(d, double)
#define MXNET_LAPACK_CWRAP_GETRI(prefix, dtype) \
inline int MXNET_LAPACK_##prefix##getri(int matrix_layout, \
lapack_index_t n, \
dtype* a, \
lapack_index_t lda, \
lapack_index_t* ipiv, \
dtype* work, \
lapack_index_t lwork) { \
if (lwork != -1) { \
return LAPACKE_##prefix##getri(matrix_layout, n, a, lda, ipiv); \
} \
*work = 0; \
return 0; \
}
MXNET_LAPACK_CWRAP_GETRI(s, float)
MXNET_LAPACK_CWRAP_GETRI(d, double)
#define MXNET_LAPACK_CWRAP_GEEV(prefix, dtype) \
inline int MXNET_LAPACK_##prefix##geev(int matrix_layout, \
char jobvl, \
char jobvr, \
lapack_index_t n, \
dtype* a, \
lapack_index_t lda, \
dtype* wr, \
dtype* wi, \
dtype* vl, \
lapack_index_t ldvl, \
dtype* vr, \
lapack_index_t ldvr, \
dtype* work, \
lapack_index_t lwork) { \
if (lwork != -1) { \
return LAPACKE_##prefix##geev( \
matrix_layout, jobvl, jobvr, n, a, lda, wr, wi, vl, ldvl, vr, ldvr); \
} \
*work = 0; \
return 0; \
}
MXNET_LAPACK_CWRAP_GEEV(s, float)
MXNET_LAPACK_CWRAP_GEEV(d, double)
#define MXNET_LAPACK_CWRAP_GELSD(prefix, dtype) \
inline int MXNET_LAPACK_##prefix##gelsd(int matrix_layout, \
lapack_index_t m, \
lapack_index_t n, \
lapack_index_t nrhs, \
dtype* a, \
lapack_index_t lda, \
dtype* b, \
lapack_index_t ldb, \
dtype* s, \
dtype rcond, \
lapack_index_t* rank, \
dtype* work, \
lapack_index_t lwork, \
lapack_index_t* iwork) { \
if (lwork != -1) { \
return LAPACKE_##prefix##gelsd(matrix_layout, m, n, nrhs, a, lda, b, ldb, s, rcond, rank); \
} \
*work = 0; \
*iwork = 0; \
return 0; \
}
MXNET_LAPACK_CWRAP_GELSD(s, float)
MXNET_LAPACK_CWRAP_GELSD(d, double)
#elif MXNET_USE_LAPACK
#define MXNET_LAPACK_ROW_MAJOR 101
#define MXNET_LAPACK_COL_MAJOR 102
// These functions can be called with either row- or col-major format.
#define MXNET_LAPACK_CWRAPPER1(func, dtype) \
inline int MXNET_LAPACK_##func(int matrix_layout, char uplo, int n, dtype* a, int lda) { \
CHECK_LAPACK_UPLO(uplo); \
char o(loup(uplo, (matrix_layout == MXNET_LAPACK_ROW_MAJOR))); \
int ret(0); \
func##_(&o, &n, a, &lda, &ret); \
return ret; \
}
MXNET_LAPACK_CWRAPPER1(spotrf, float)
MXNET_LAPACK_CWRAPPER1(dpotrf, double)
MXNET_LAPACK_CWRAPPER1(spotri, float)
MXNET_LAPACK_CWRAPPER1(dpotri, double)
inline int mxnet_lapack_sposv(int matrix_layout,
char uplo,
int n,
int nrhs,
float* a,
int lda,
float* b,
int ldb) {
int info;
if (matrix_layout == MXNET_LAPACK_ROW_MAJOR) {
// Transpose b to b_t of shape (nrhs, n)
float* b_t = new float[nrhs * n];
flip<cpu, float>(n, nrhs, b_t, n, b, ldb);
sposv_(&uplo, &n, &nrhs, a, &lda, b_t, &n, &info);
flip<cpu, float>(nrhs, n, b, ldb, b_t, n);
delete[] b_t;
return info;
}
sposv_(&uplo, &n, &nrhs, a, &lda, b, &ldb, &info);
return info;
}
inline int mxnet_lapack_dposv(int matrix_layout,
char uplo,
int n,
int nrhs,
double* a,
int lda,
double* b,
int ldb) {
int info;
if (matrix_layout == MXNET_LAPACK_ROW_MAJOR) {
// Transpose b to b_t of shape (nrhs, n)
double* b_t = new double[nrhs * n];
flip<cpu, double>(n, nrhs, b_t, n, b, ldb);
dposv_(&uplo, &n, &nrhs, a, &lda, b_t, &n, &info);
flip<cpu, double>(nrhs, n, b, ldb, b_t, n);
delete[] b_t;
return info;
}
dposv_(&uplo, &n, &nrhs, a, &lda, b, &ldb, &info);
return info;
}
// Note: Both MXNET_LAPACK_*gelqf, MXNET_LAPACK_*orglq can only be called with
// row-major format (MXNet). Internally, the QR variants are done in column-major.
// In particular, the matrix dimensions m and n are flipped.
#define MXNET_LAPACK_CWRAP_GELQF(prefix, dtype) \
inline int MXNET_LAPACK_##prefix##gelqf( \
int matrix_layout, int m, int n, dtype* a, int lda, dtype* tau, dtype* work, int lwork) { \
if (matrix_layout == MXNET_LAPACK_ROW_MAJOR) { \
int info(0); \
prefix##geqrf_(&n, &m, a, &lda, tau, work, &lwork, &info); \
return info; \
} else { \
CHECK(false) << "MXNET_LAPACK_" << #prefix << "gelqf implemented for row-major layout only"; \
return 1; \
} \
}
MXNET_LAPACK_CWRAP_GELQF(s, float)
MXNET_LAPACK_CWRAP_GELQF(d, double)
// Note: The k argument (rank) is equal to m as well
#define MXNET_LAPACK_CWRAP_ORGLQ(prefix, dtype) \
inline int MXNET_LAPACK_##prefix##orglq( \
int matrix_layout, int m, int n, dtype* a, int lda, dtype* tau, dtype* work, int lwork) { \
if (matrix_layout == MXNET_LAPACK_ROW_MAJOR) { \
int info(0); \
prefix##orgqr_(&n, &m, &m, a, &lda, tau, work, &lwork, &info); \
return info; \
} else { \
CHECK(false) << "MXNET_LAPACK_" << #prefix << "orglq implemented for row-major layout only"; \
return 1; \
} \
}
MXNET_LAPACK_CWRAP_ORGLQ(s, float)
MXNET_LAPACK_CWRAP_ORGLQ(d, double)
#define MXNET_LAPACK_CWRAP_GEQRF(prefix, dtype) \
inline int MXNET_LAPACK_##prefix##geqrf( \
int matrix_layout, int m, int n, dtype* a, int lda, dtype* tau, dtype* work, int lwork) { \
if (matrix_layout == MXNET_LAPACK_ROW_MAJOR) { \
CHECK(false) << "MXNET_LAPACK_" << #prefix << "geqrf implemented for col-major layout only"; \
return 1; \
} else { \
int info(0); \
prefix##geqrf_(&m, &n, a, &lda, tau, work, &lwork, &info); \
return info; \
} \
}
MXNET_LAPACK_CWRAP_GEQRF(s, float)
MXNET_LAPACK_CWRAP_GEQRF(d, double)
#define MXNET_LAPACK_CWRAP_ORGQR(prefix, dtype) \
inline int MXNET_LAPACK_##prefix##orgqr(int matrix_layout, \
int m, \
int n, \
int k, \
dtype* a, \
int lda, \
dtype* tau, \
dtype* work, \
int lwork) { \
if (matrix_layout == MXNET_LAPACK_ROW_MAJOR) { \
CHECK(false) << "MXNET_LAPACK_" << #prefix << "orgqr implemented for col-major layout only"; \
return 1; \
} else { \
int info(0); \
prefix##orgqr_(&m, &n, &k, a, &lda, tau, work, &lwork, &info); \
return info; \
} \
}
MXNET_LAPACK_CWRAP_ORGQR(s, float)
MXNET_LAPACK_CWRAP_ORGQR(d, double)
// Note: Supports row-major format only. Internally, column-major is used, so all
// inputs/outputs are flipped (in particular, uplo is flipped).
#define MXNET_LAPACK_CWRAP_SYEVD(func, dtype) \
inline int MXNET_LAPACK_##func(int matrix_layout, \
char uplo, \
int n, \
dtype* a, \
int lda, \
dtype* w, \
dtype* work, \
int lwork, \
int* iwork, \
int liwork) { \
if (matrix_layout == MXNET_LAPACK_ROW_MAJOR) { \
int info(0); \
char jobz('V'); \
char uplo_(loup(uplo, true)); \
func##_(&jobz, &uplo_, &n, a, &lda, w, work, &lwork, iwork, &liwork, &info); \
return info; \
} else { \
CHECK(false) << "MXNET_LAPACK_" << #func << " implemented for row-major layout only"; \
return 1; \
} \
}
MXNET_LAPACK_CWRAP_SYEVD(ssyevd, float)
MXNET_LAPACK_CWRAP_SYEVD(dsyevd, double)
// Note: Supports row-major format only. Internally, column-major is used, so all
// inputs/outputs are flipped and transposed. m and n are flipped as well.
#define MXNET_LAPACK_CWRAP_GESVD(func, dtype) \
inline int MXNET_LAPACK_##func(int matrix_layout, \
int m, \
int n, \
dtype* ut, \
int ldut, \
dtype* s, \
dtype* v, \
int ldv, \
dtype* work, \
int lwork) { \
if (matrix_layout == MXNET_LAPACK_ROW_MAJOR) { \
int info(0); \
char jobu('O'); \
char jobvt('S'); \
func##_(&jobu, &jobvt, &n, &m, v, &ldv, s, v, &ldv, ut, &ldut, work, &lwork, &info); \
return info; \
} else { \
CHECK(false) << "MXNET_LAPACK_" << #func << " implemented for row-major layout only"; \
return 1; \
} \
}
MXNET_LAPACK_CWRAP_GESVD(sgesvd, float)
MXNET_LAPACK_CWRAP_GESVD(dgesvd, double)
#define MXNET_LAPACK_CWRAP_GESDD(func, dtype) \
inline int MXNET_LAPACK_##func(int matrix_layout, \
int m, \
int n, \
dtype* a, \
int lda, \
dtype* s, \
dtype* u, \
int ldu, \
dtype* vt, \
int ldvt, \
dtype* work, \
int lwork, \
int* iwork) { \
if (matrix_layout == MXNET_LAPACK_ROW_MAJOR) { \
CHECK(false) << "MXNET_LAPACK_" << #func << " implemented for row-major layout only"; \
return 1; \
} else { \
int info(0); \
char jobz('O'); \
func##_(&jobz, &m, &n, a, &lda, s, u, &ldu, vt, &ldvt, work, &lwork, iwork, &info); \
return info; \
} \
}
MXNET_LAPACK_CWRAP_GESDD(sgesdd, float)
MXNET_LAPACK_CWRAP_GESDD(dgesdd, double)
#define MXNET_LAPACK_CWRAP_GEEV(prefix, dtype) \
inline int MXNET_LAPACK_##prefix##geev(int matrix_layout, \
char jobvl, \
char jobvr, \
int n, \
dtype* a, \
int lda, \
dtype* wr, \
dtype* wi, \
dtype* vl, \
int ldvl, \
dtype* vr, \
int ldvr, \
dtype* work, \
int lwork) { \
if (matrix_layout == MXNET_LAPACK_ROW_MAJOR) { \
CHECK(false) << "MXNET_LAPACK_" << #prefix << "geev implemented for col-major layout only"; \
return 1; \
} else { \
int info(0); \
prefix##geev_( \
&jobvl, &jobvr, &n, a, &lda, wr, wi, vl, &ldvl, vr, &ldvr, work, &lwork, &info); \
return info; \
} \
}
MXNET_LAPACK_CWRAP_GEEV(s, float)
MXNET_LAPACK_CWRAP_GEEV(d, double)
#define MXNET_LAPACK
// Note: Both MXNET_LAPACK_*getrf, MXNET_LAPACK_*getri can only be called with col-major format
// (MXNet) for performance.
#define MXNET_LAPACK_CWRAP_GETRF(prefix, dtype) \
inline int MXNET_LAPACK_##prefix##getrf( \
int matrix_layout, int m, int n, dtype* a, int lda, int* ipiv) { \
if (matrix_layout == MXNET_LAPACK_ROW_MAJOR) { \
CHECK(false) << "MXNET_LAPACK_" << #prefix << "getri implemented for col-major layout only"; \
return 1; \
} else { \
int info(0); \
prefix##getrf_(&m, &n, a, &lda, ipiv, &info); \
return info; \
} \
}
MXNET_LAPACK_CWRAP_GETRF(s, float)
MXNET_LAPACK_CWRAP_GETRF(d, double)
#define MXNET_LAPACK_CWRAP_GETRI(prefix, dtype) \
inline int MXNET_LAPACK_##prefix##getri( \
int matrix_layout, int n, dtype* a, int lda, int* ipiv, dtype* work, int lwork) { \
if (matrix_layout == MXNET_LAPACK_ROW_MAJOR) { \
CHECK(false) << "MXNET_LAPACK_" << #prefix << "getri implemented for col-major layout only"; \
return 1; \
} else { \
int info(0); \
prefix##getri_(&n, a, &lda, ipiv, work, &lwork, &info); \
return info; \
} \
}
MXNET_LAPACK_CWRAP_GETRI(s, float)
MXNET_LAPACK_CWRAP_GETRI(d, double)
#define MXNET_LAPACK_CWRAP_GESV(prefix, dtype) \
inline int MXNET_LAPACK_##prefix##gesv( \
int matrix_layout, int n, int nrhs, dtype* a, int lda, int* ipiv, dtype* b, int ldb) { \
if (matrix_layout == MXNET_LAPACK_ROW_MAJOR) { \
CHECK(false) << "MXNET_LAPACK_" << #prefix << "gesv implemented for col-major layout only"; \
return 1; \
} else { \
int info(0); \
prefix##gesv_(&n, &nrhs, a, &lda, ipiv, b, &ldb, &info); \
return info; \
} \
}
MXNET_LAPACK_CWRAP_GESV(s, float)
MXNET_LAPACK_CWRAP_GESV(d, double)
#define MXNET_LAPACK_CWRAP_GELSD(prefix, dtype) \
inline int MXNET_LAPACK_##prefix##gelsd(int matrix_layout, \
int m, \
int n, \
int nrhs, \
dtype* a, \
int lda, \
dtype* b, \
int ldb, \
dtype* s, \
dtype rcond, \
int* rank, \
dtype* work, \
int lwork, \
int* iwork) { \
if (matrix_layout == MXNET_LAPACK_ROW_MAJOR) { \
CHECK(false) << "MXNET_LAPACK_" << #prefix << "gesv implemented for col-major layout only"; \
return 1; \
} else { \
int info(0); \
prefix##gelsd_( \
&m, &n, &nrhs, a, &lda, b, &ldb, s, &rcond, rank, work, &lwork, iwork, &info); \
return info; \
} \
}
MXNET_LAPACK_CWRAP_GELSD(s, float)
MXNET_LAPACK_CWRAP_GELSD(d, double)
#else
#define MXNET_LAPACK_ROW_MAJOR 101
#define MXNET_LAPACK_COL_MAJOR 102
// Define compilable stubs.
#define MXNET_LAPACK_CWRAPPER1(func, dtype) \
int MXNET_LAPACK_##func(int matrix_layout, char uplo, int n, dtype* a, int lda);
#define MXNET_LAPACK_CWRAPPER2(func, dtype) \
int MXNET_LAPACK_##func( \
int matrix_layout, int m, int n, dtype* a, int lda, dtype* tau, dtype* work, int lwork);
#define MXNET_LAPACK_CWRAPPER3(func, dtype) \
int MXNET_LAPACK_##func(int matrix_layout, \
char uplo, \
int n, \
dtype* a, \
int lda, \
dtype* w, \
dtype* work, \
int lwork, \
int* iwork, \
int liwork);
#define MXNET_LAPACK_CWRAPPER4(func, dtype) \
int MXNET_LAPACK_##func(int matrix_layout, int m, int n, dtype* a, int lda, int* ipiv);
#define MXNET_LAPACK_CWRAPPER5(func, dtype) \
int MXNET_LAPACK_##func( \
int matrix_layout, int n, dtype* a, int lda, int* ipiv, dtype* work, int lwork);
#define MXNET_LAPACK_CWRAPPER6(func, dtype) \
int MXNET_LAPACK_##func(int matrix_layout, \
int m, \
int n, \
dtype* ut, \
int ldut, \
dtype* s, \
dtype* v, \
int ldv, \
dtype* work, \
int lwork);
#define MXNET_LAPACK_CWRAPPER7(func, dtype) \
int MXNET_LAPACK_##func( \
int matrix_order, int n, int nrhs, dtype* a, int lda, int* ipiv, dtype* b, int ldb);
#define MXNET_LAPACK_CWRAPPER8(func, dtype) \
int MXNET_LAPACK_##func(int matrix_layout, \
char jobvl, \
char jobvr, \
int n, \
dtype* a, \
int lda, \
dtype* wr, \
dtype* wi, \
dtype* vl, \
int ldvl, \
dtype* vr, \
int ldvr, \
dtype* work, \
int lwork);
#define MXNET_LAPACK_CWRAPPER9(func, dtype) \
int MXNET_LAPACK_##func(int matrix_layout, \
int m, \
int n, \
dtype* a, \
int lda, \
dtype* s, \
dtype* u, \
int ldu, \
dtype* vt, \
int ldvt, \
dtype* work, \
int lwork, \
int* iwork);
#define MXNET_LAPACK_CWRAPPER10(func, dtype) \
int MXNET_LAPACK_##func( \
int matrix_layout, int m, int n, dtype* a, int lda, dtype* tau, dtype* work, int lwork);
#define MXNET_LAPACK_CWRAPPER11(func, dtype) \
int MXNET_LAPACK_##func(int matrix_layout, \
int m, \
int n, \
int nrhs, \
dtype* a, \
int lda, \
dtype* b, \
int ldb, \
dtype* s, \
dtype rcond, \
int* rank, \
dtype* work, \
int lwork, \
int* iwork);
#define MXNET_LAPACK_CWRAPPER12(func, dtype) \
int MXNET_LAPACK_##func(int matrix_layout, \
int m, \
int n, \
int k, \
dtype* a, \
int lda, \
dtype* tau, \
dtype* work, \
int lwork);
#define MXNET_LAPACK_UNAVAILABLE(func) int mxnet_lapack_##func(...);