forked from LLNL/libROM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix.C
1265 lines (1121 loc) · 37.9 KB
/
Matrix.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
/******************************************************************************
*
* Copyright (c) 2013-2019, Lawrence Livermore National Security, LLC
* and other libROM project developers. See the top-level COPYRIGHT
* file for details.
*
* SPDX-License-Identifier: (Apache-2.0 OR MIT)
*
*****************************************************************************/
// Description: A simple, parallel Matrix class with the utility needed to
// support the basis generation methods of this library. A
// distributed Matrix has its rows distributed across processors.
#include "Matrix.h"
#include "HDFDatabase.h"
#include "mpi.h"
#include <string.h>
#ifdef CAROM_HAS_ELEMENTAL
#include <El.hpp>
#endif
/* Use Autotools-detected Fortran name-mangling scheme */
#define dgetrf FC_FUNC(dgetrf, DGETRF)
#define dgetri FC_FUNC(dgetri, DGETRI)
#define dgeqp3 FC_FUNC(dgeqp3, DGEQP3)
extern "C" {
// LU decomposition of a general matrix.
void
dgetrf(int*, int*, double*, int*, int*, int*);
// Generate inverse of a matrix given its LU decomposition.
void
dgetri(int*, double*, int*, int*, double*, int*, int*);
// BLAS-3 version of QR decomposition with column pivoting
void
dgeqp3(int*, int*, double*, int*, int*, double*, double*, int*, int*);
}
namespace CAROM {
Matrix::Matrix() :
d_mat(NULL),
d_alloc_size(0),
d_distributed(false),
d_owns_data(true)
{}
Matrix::Matrix(
int num_rows,
int num_cols,
bool distributed) :
d_mat(0),
d_alloc_size(0),
d_distributed(distributed),
d_owns_data(true)
{
CAROM_ASSERT(num_rows > 0);
CAROM_ASSERT(num_cols > 0);
setSize(num_rows, num_cols);
int mpi_init;
MPI_Initialized(&mpi_init);
if (mpi_init) {
MPI_Comm_size(MPI_COMM_WORLD, &d_num_procs);
}
else {
d_num_procs = 1;
}
}
Matrix::Matrix(
double* mat,
int num_rows,
int num_cols,
bool distributed,
bool copy_data) :
d_mat(0),
d_alloc_size(0),
d_distributed(distributed),
d_owns_data(copy_data)
{
CAROM_ASSERT(mat != 0);
CAROM_ASSERT(num_rows > 0);
CAROM_ASSERT(num_cols > 0);
if (copy_data) {
setSize(num_rows, num_cols);
memcpy(d_mat, mat, d_alloc_size*sizeof(double));
}
else {
d_mat = mat;
d_alloc_size = num_rows*num_cols;
d_num_cols = num_cols;
d_num_rows = num_rows;
}
int mpi_init;
MPI_Initialized(&mpi_init);
if (mpi_init) {
MPI_Comm_size(MPI_COMM_WORLD, &d_num_procs);
}
else {
d_num_procs = 1;
}
}
Matrix::Matrix(
const Matrix& other) :
d_mat(0),
d_alloc_size(0),
d_distributed(other.d_distributed),
d_owns_data(true)
{
setSize(other.d_num_rows, other.d_num_cols);
int mpi_init;
MPI_Initialized(&mpi_init);
if (mpi_init) {
MPI_Comm_size(MPI_COMM_WORLD, &d_num_procs);
}
else {
d_num_procs = 1;
}
memcpy(d_mat, other.d_mat, d_alloc_size*sizeof(double));
}
Matrix::~Matrix()
{
if (d_owns_data && d_mat) {
delete [] d_mat;
}
}
Matrix&
Matrix::operator = (
const Matrix& rhs)
{
d_distributed = rhs.d_distributed;
d_num_procs = rhs.d_num_procs;
setSize(rhs.d_num_rows, rhs.d_num_cols);
memcpy(d_mat, rhs.d_mat, d_num_rows*d_num_cols*sizeof(double));
return *this;
}
Matrix&
Matrix::operator += (
const Matrix& rhs)
{
CAROM_ASSERT(rhs.d_num_rows == d_num_rows);
CAROM_ASSERT(rhs.d_num_cols == d_num_cols);
for(int i=0; i<d_num_rows*d_num_cols; ++i) d_mat[i] += rhs.d_mat[i];
return *this;
}
Matrix&
Matrix::operator -= (
const Matrix& rhs)
{
CAROM_ASSERT(rhs.d_num_rows == d_num_rows);
CAROM_ASSERT(rhs.d_num_cols == d_num_cols);
for(int i=0; i<d_num_rows*d_num_cols; ++i) d_mat[i] -= rhs.d_mat[i];
return *this;
}
bool
Matrix::balanced() const
{
// TODO([email protected]): Relax the assumption in libROM that
// objects use MPI_COMM_WORLD, and that rank 0 is the master rank of
// an object.
const int master_rank = 0;
const MPI_Comm comm = MPI_COMM_WORLD;
// A Matrix is "balanced" (load-balanced for distributed dense matrix
// computations) if:
//
// (1) the number of rows owned by each process in any pair of
// processes on the communicator differs by at most one
//
// (2) process j has no fewer rows than k if j is less than k (j and
// k are both integers corresponding to process ranks)
// Serial matrices are balanced by definition; one process owns all
// rows
if (!distributed()) return true;
// Otherwise, get the total number of rows of the matrix.
int num_total_rows = d_num_rows;
const int reduce_count = 1;
CAROM_ASSERT(MPI_Allreduce(MPI_IN_PLACE,
&num_total_rows,
reduce_count,
MPI_INT,
MPI_SUM,
comm) == MPI_SUCCESS);
const int first_rank_with_fewer = num_total_rows % d_num_procs;
int my_rank;
CAROM_ASSERT(MPI_Comm_rank(comm, &my_rank) == MPI_SUCCESS);
const int min_rows_per_rank = num_total_rows / d_num_procs;
const bool has_extra_row = my_rank < first_rank_with_fewer;
const int max_rows_on_rank = min_rows_per_rank + has_extra_row;
const bool has_enough_rows = (d_num_rows >= min_rows_per_rank);
const bool has_too_many_rows = (d_num_rows > max_rows_on_rank);
int result = (has_enough_rows && !has_too_many_rows);
CAROM_ASSERT(MPI_Allreduce(MPI_IN_PLACE,
&result,
reduce_count,
MPI_INT,
MPI_LAND,
comm) == MPI_SUCCESS);
return result;
}
Matrix&
Matrix::operator = (
const double a)
{
for(int i=0; i<d_num_rows*d_num_cols; ++i) {
d_mat[i] = a;
}
return *this;
}
void
Matrix::mult(
const Matrix& other,
Matrix*& result) const
{
CAROM_ASSERT(result == 0 || result->distributed() == distributed());
CAROM_ASSERT(!other.distributed());
CAROM_ASSERT(numColumns() == other.numRows());
// If the result has not been allocated then do so. Otherwise size it
// correctly.
if (result == 0) {
result = new Matrix(d_num_rows, other.d_num_cols, d_distributed);
}
else {
result->setSize(d_num_rows, other.d_num_cols);
}
// Do the multiplication.
for (int this_row = 0; this_row < d_num_rows; ++this_row) {
for (int other_col = 0; other_col < other.d_num_cols; ++other_col) {
double result_val = 0.0;
for (int entry = 0; entry < d_num_cols; ++entry) {
result_val += item(this_row, entry)*other.item(entry, other_col);
}
result->item(this_row, other_col) = result_val;
}
}
}
void
Matrix::mult(
const Matrix& other,
Matrix& result) const
{
CAROM_ASSERT(result.distributed() == distributed());
CAROM_ASSERT(!other.distributed());
CAROM_ASSERT(numColumns() == other.numRows());
// Size result correctly.
result.setSize(d_num_rows, other.d_num_cols);
// Do the multiplication.
for (int this_row = 0; this_row < d_num_rows; ++this_row) {
for (int other_col = 0; other_col < other.d_num_cols; ++other_col) {
double result_val = 0.0;
for (int entry = 0; entry < d_num_cols; ++entry) {
result_val += item(this_row, entry)*other.item(entry, other_col);
}
result.item(this_row, other_col) = result_val;
}
}
}
void
Matrix::mult(
const Vector& other,
Vector*& result) const
{
CAROM_ASSERT(result == 0 || result->distributed() == distributed());
CAROM_ASSERT(!other.distributed());
CAROM_ASSERT(numColumns() == other.dim());
// If the result has not been allocated then do so. Otherwise size it
// correctly.
if (result == 0) {
result = new Vector(d_num_rows, d_distributed);
}
else {
result->setSize(d_num_rows);
}
// Do the multiplication.
for (int this_row = 0; this_row < d_num_rows; ++this_row) {
double result_val = 0.0;
for (int entry = 0; entry < d_num_cols; ++entry) {
result_val += item(this_row, entry)*other.item(entry);
}
result->item(this_row) = result_val;
}
}
void
Matrix::mult(
const Vector& other,
Vector& result) const
{
CAROM_ASSERT(result.distributed() == distributed());
CAROM_ASSERT(!other.distributed());
CAROM_ASSERT(numColumns() == other.dim());
// Size result correctly.
result.setSize(d_num_rows);
// Do the multiplication.
for (int this_row = 0; this_row < d_num_rows; ++this_row) {
double result_val = 0.0;
for (int entry = 0; entry < d_num_cols; ++entry) {
result_val += item(this_row, entry)*other.item(entry);
}
result.item(this_row) = result_val;
}
}
void
Matrix::pointwise_mult(
int this_row,
const Vector& other,
Vector& result) const
{
CAROM_ASSERT(!result.distributed());
CAROM_ASSERT(!distributed());
CAROM_ASSERT(!other.distributed());
CAROM_ASSERT(numColumns() == other.dim());
// Do the multiplication.
for (int entry = 0; entry < d_num_cols; ++entry) {
result.item(entry) = item(this_row, entry)*other.item(entry);
}
}
void
Matrix::pointwise_mult(
int this_row,
Vector& other) const
{
CAROM_ASSERT(!distributed());
CAROM_ASSERT(!other.distributed());
CAROM_ASSERT(numColumns() == other.dim());
// Do the multiplication.
for (int entry = 0; entry < d_num_cols; ++entry) {
other.item(entry) *= item(this_row, entry);
}
}
void
Matrix::multPlus(
Vector& a,
const Vector& b,
double c) const
{
CAROM_ASSERT(a.distributed() == distributed());
CAROM_ASSERT(!b.distributed());
CAROM_ASSERT(numColumns() == b.dim());
CAROM_ASSERT(numRows() == a.dim());
for (int this_row = 0; this_row < d_num_rows; ++this_row) {
double tmp = 0.0;
for (int this_col = 0; this_col < d_num_cols; ++this_col) {
tmp += item(this_row, this_col)*b.item(this_col);
}
a.item(this_row) += tmp*c;
}
}
void
Matrix::transposeMult(
const Matrix& other,
Matrix*& result) const
{
CAROM_ASSERT(result == 0 || !result->distributed());
CAROM_ASSERT(distributed() == other.distributed());
CAROM_ASSERT(numRows() == other.numRows());
// If the result has not been allocated then do so. Otherwise size it
// correctly.
if (result == 0) {
result = new Matrix(d_num_cols, other.d_num_cols, false);
}
else {
result->setSize(d_num_cols, other.d_num_cols);
}
// Do the multiplication.
for (int this_col = 0; this_col < d_num_cols; ++this_col) {
for (int other_col = 0; other_col < other.d_num_cols; ++other_col) {
double result_val = 0.0;
for (int entry = 0; entry < d_num_rows; ++entry) {
result_val += item(entry, this_col)*other.item(entry, other_col);
}
result->item(this_col, other_col) = result_val;
}
}
if (d_distributed && d_num_procs > 1) {
int new_mat_size = d_num_cols*other.d_num_cols;
MPI_Allreduce(MPI_IN_PLACE,
&result->item(0, 0),
new_mat_size,
MPI_DOUBLE,
MPI_SUM,
MPI_COMM_WORLD);
}
}
void
Matrix::transposeMult(
const Matrix& other,
Matrix& result) const
{
CAROM_ASSERT(!result.distributed());
CAROM_ASSERT(distributed() == other.distributed());
CAROM_ASSERT(numRows() == other.numRows());
// Size result correctly.
result.setSize(d_num_cols, other.d_num_cols);
// Do the multiplication.
for (int this_col = 0; this_col < d_num_cols; ++this_col) {
for (int other_col = 0; other_col < other.d_num_cols; ++other_col) {
double result_val = 0.0;
for (int entry = 0; entry < d_num_rows; ++entry) {
result_val += item(entry, this_col)*other.item(entry, other_col);
}
result.item(this_col, other_col) = result_val;
}
}
if (d_distributed && d_num_procs > 1) {
int new_mat_size = d_num_cols*other.d_num_cols;
MPI_Allreduce(MPI_IN_PLACE,
&result.item(0, 0),
new_mat_size,
MPI_DOUBLE,
MPI_SUM,
MPI_COMM_WORLD);
}
}
void
Matrix::transposeMult(
const Vector& other,
Vector*& result) const
{
CAROM_ASSERT(result == 0 || !result->distributed());
CAROM_ASSERT(distributed() == other.distributed());
CAROM_ASSERT(numRows() == other.dim());
// If the result has not been allocated then do so. Otherwise size it
// correctly.
if (result == 0) {
result = new Vector(d_num_cols, false);
}
else {
result->setSize(d_num_cols);
}
// Do the multiplication.
for (int this_col = 0; this_col < d_num_cols; ++this_col) {
double result_val = 0.0;
for (int entry = 0; entry < d_num_rows; ++entry) {
result_val += item(entry, this_col)*other.item(entry);
}
result->item(this_col) = result_val;
}
if (d_distributed && d_num_procs > 1) {
MPI_Allreduce(MPI_IN_PLACE,
&result->item(0),
d_num_cols,
MPI_DOUBLE,
MPI_SUM,
MPI_COMM_WORLD);
}
}
void
Matrix::transposeMult(
const Vector& other,
Vector& result) const
{
CAROM_ASSERT(!result.distributed());
CAROM_ASSERT(distributed() == other.distributed());
CAROM_ASSERT(numRows() == other.dim());
// If the result has not been allocated then do so. Otherwise size it
// correctly.
result.setSize(d_num_cols);
// Do the multiplication.
for (int this_col = 0; this_col < d_num_cols; ++this_col) {
double result_val = 0.0;
for (int entry = 0; entry < d_num_rows; ++entry) {
result_val += item(entry, this_col)*other.item(entry);
}
result.item(this_col) = result_val;
}
if (d_distributed && d_num_procs > 1) {
MPI_Allreduce(MPI_IN_PLACE,
&result.item(0),
d_num_cols,
MPI_DOUBLE,
MPI_SUM,
MPI_COMM_WORLD);
}
}
void
Matrix::inverse(
Matrix*& result) const
{
CAROM_ASSERT(result == 0 ||
(!result->distributed() &&
result->numRows() == numRows() &&
result->numColumns() == numColumns()));
CAROM_ASSERT(!distributed());
CAROM_ASSERT(numRows() == numColumns());
// If the result has not been allocated then do so. Otherwise size it
// correctly.
if (result == 0) {
result = new Matrix(d_num_rows, d_num_cols, false);
}
else {
result->setSize(d_num_rows, d_num_cols);
}
// Call lapack routines to do the inversion.
// Set up some stuff the lapack routines need.
int info;
int mtx_size = d_num_rows;
int lwork = mtx_size*mtx_size;
int* ipiv = new int [mtx_size];
double* work = new double [lwork];
// To use lapack we need a column major representation of this which is
// essentially the transform of this. Use result for this representation.
for (int row = 0; row < mtx_size; ++row) {
for (int col = 0; col < mtx_size; ++col) {
result->item(col, row) = item(row, col);
}
}
// Now call lapack to do the inversion.
dgetrf(&mtx_size, &mtx_size, result->d_mat, &mtx_size, ipiv, &info);
dgetri(&mtx_size, result->d_mat, &mtx_size, ipiv, work, &lwork, &info);
// Result now has the inverse in a column major representation. Put it
// into row major order.
for (int row = 0; row < mtx_size; ++row) {
for (int col = row+1; col < mtx_size; ++col) {
double tmp = result->item(row, col);
result->item(row, col) = result->item(col, row);
result->item(col, row) = tmp;
}
}
}
void
Matrix::inverse(
Matrix& result) const
{
CAROM_ASSERT(!result.distributed() && result.numRows() == numRows() &&
result.numColumns() == numColumns());
CAROM_ASSERT(!distributed());
CAROM_ASSERT(numRows() == numColumns());
// Size result correctly.
result.setSize(d_num_rows, d_num_cols);
// Call lapack routines to do the inversion.
// Set up some stuff the lapack routines need.
int info;
int mtx_size = d_num_rows;
int lwork = mtx_size*mtx_size;
int* ipiv = new int [mtx_size];
double* work = new double [lwork];
// To use lapack we need a column major representation of this which is
// essentially the transform of this. Use result for this representation.
for (int row = 0; row < mtx_size; ++row) {
for (int col = 0; col < mtx_size; ++col) {
result.item(col, row) = item(row, col);
}
}
// Now call lapack to do the inversion.
dgetrf(&mtx_size, &mtx_size, result.d_mat, &mtx_size, ipiv, &info);
dgetri(&mtx_size, result.d_mat, &mtx_size, ipiv, work, &lwork, &info);
// Result now has the inverse in a column major representation. Put it
// into row major order.
for (int row = 0; row < mtx_size; ++row) {
for (int col = row+1; col < mtx_size; ++col) {
double tmp = result.item(row, col);
result.item(row, col) = result.item(col, row);
result.item(col, row) = tmp;
}
}
}
void
Matrix::inverse()
{
CAROM_ASSERT(!distributed());
CAROM_ASSERT(numRows() == numColumns());
// Call lapack routines to do the inversion.
// Set up some stuff the lapack routines need.
int info;
int mtx_size = d_num_rows;
int lwork = mtx_size*mtx_size;
int* ipiv = new int [mtx_size];
double* work = new double [lwork];
// To use lapack we need a column major representation of this which is
// essentially the transform of this.
for (int row = 0; row < mtx_size; ++row) {
for (int col = row+1; col < mtx_size; ++col) {
double tmp = item(row, col);
item(row, col) = item(col, row);
item(col, row) = tmp;
}
}
// Now call lapack to do the inversion.
dgetrf(&mtx_size, &mtx_size, d_mat, &mtx_size, ipiv, &info);
dgetri(&mtx_size, d_mat, &mtx_size, ipiv, work, &lwork, &info);
// This now has its inverse in a column major representation. Put it into
// row major representation.
for (int row = 0; row < mtx_size; ++row) {
for (int col = row+1; col < mtx_size; ++col) {
double tmp = item(row, col);
item(row, col) = item(col, row);
item(col, row) = tmp;
}
}
}
void Matrix::transposePseudoinverse()
{
CAROM_ASSERT(!distributed());
CAROM_ASSERT(numRows() >= numColumns());
if (numRows() == numColumns())
{
inverse();
}
else
{
Matrix *AtA = this->transposeMult(this);
// Directly invert AtA, which is a bad idea if AtA is not small.
AtA->inverse();
// Pseudoinverse is (AtA)^{-1}*this^T, but we store the transpose of the result in this, namely this*(AtA)^{-T}.
Vector row(numColumns(), false);
Vector res(numColumns(), false);
for (int i=0; i<numRows(); ++i)
{ // Compute i-th row of this multiplied by (AtA)^{-T}, whose transpose is (AtA)^{-1} times i-th row transposed.
for (int j=0; j<numColumns(); ++j)
row.item(j) = this->item(i,j);
AtA->mult(row, res);
// Overwrite i-th row with transpose of result.
for (int j=0; j<numColumns(); ++j)
this->item(i,j) = res.item(j);
}
delete AtA;
}
}
void
Matrix::print(const char * prefix)
{
int my_rank;
const bool success = MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
CAROM_ASSERT(success);
std::string filename_str = prefix + std::to_string(my_rank);
const char * filename = filename_str.c_str();
FILE * pFile = fopen(filename,"w");
for (int row = 0; row < d_num_rows; ++row) {
for (int col = 0; col < d_num_cols; ++col) {
fprintf(pFile, " %25.20e\t", item(row,col));
}
fprintf(pFile, "\n");
}
fclose(pFile);
}
void
Matrix::write(const std::string& base_file_name)
{
CAROM_ASSERT(!base_file_name.empty());
int mpi_init;
MPI_Initialized(&mpi_init);
int rank;
if (mpi_init) {
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
}
else {
rank = 0;
}
char tmp[100];
sprintf(tmp, ".%06d", rank);
std::string full_file_name = base_file_name + tmp;
HDFDatabase database;
database.create(full_file_name);
sprintf(tmp, "distributed");
database.putInteger(tmp, d_distributed);
sprintf(tmp, "num_rows");
database.putInteger(tmp, d_num_rows);
sprintf(tmp, "num_cols");
database.putInteger(tmp, d_num_cols);
sprintf(tmp, "data");
database.putDoubleArray(tmp, d_mat, d_num_rows*d_num_cols);
database.close();
}
void
Matrix::read(const std::string& base_file_name)
{
CAROM_ASSERT(!base_file_name.empty());
int mpi_init;
MPI_Initialized(&mpi_init);
int rank;
if (mpi_init) {
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
}
else {
rank = 0;
}
char tmp[100];
sprintf(tmp, ".%06d", rank);
std::string full_file_name = base_file_name + tmp;
HDFDatabase database;
database.open(full_file_name);
sprintf(tmp, "distributed");
int distributed;
database.getInteger(tmp, distributed);
d_distributed = bool(distributed);
int num_rows;
sprintf(tmp, "num_rows");
database.getInteger(tmp, num_rows);
int num_cols;
sprintf(tmp, "num_cols");
database.getInteger(tmp, num_cols);
setSize(num_rows,num_cols);
sprintf(tmp, "data");
database.getDoubleArray(tmp, d_mat, d_alloc_size);
d_owns_data = true;
if (mpi_init) {
MPI_Comm_size(MPI_COMM_WORLD, &d_num_procs);
}
else {
d_num_procs = 1;
}
database.close();
}
void
Matrix::qrcp_pivots_transpose(int* row_pivot,
int* row_pivot_owner,
int pivots_requested) const
{
if(!distributed()) {
return qrcp_pivots_transpose_serial(row_pivot,
row_pivot_owner,
pivots_requested);
}
else{
#ifdef CAROM_HAS_ELEMENTAL
return qrcp_pivots_transpose_distributed(row_pivot,
row_pivot_owner,
pivots_requested);
#else
CAROM_ASSERT(false);
#endif
}
}
void
Matrix::qrcp_pivots_transpose_serial(int* row_pivot,
int* row_pivot_owner,
int pivots_requested) const
{
// This method assumes this matrix is serial
CAROM_ASSERT(!distributed());
// Number of pivots requested can't exceed the number of rows of the
// matrix
CAROM_ASSERT(pivots_requested <= numRows());
CAROM_ASSERT(pivots_requested > 0);
// Make sure arrays are allocated before entry; this method does not
// own the input pointers
CAROM_ASSERT(row_pivot != NULL);
CAROM_ASSERT(row_pivot_owner != NULL);
// Get dimensions of transpose of matrix
int num_rows_of_transpose = numColumns();
int num_cols_of_transpose = numRows();
// LAPACK routines tend to overwrite their inputs, but we'd like to
// keep the basis matrix and use it in later computations, so copy
// the basis matrix here.
Matrix scratch(*this);
// Allocate work arrays; work array for QR must be at least 1 plus 3
// times the number of columns of its matrix input. This algorithm
// applies QR to transposed basis matrix, so the applicable
// dimension is the number of rows of the basis matrix. It's
// possible to get better performance by computing the optimal block
// size and then using that value to size the work array; see the
// LAPACK source code and documentation for details.
int lwork = 20 * num_cols_of_transpose + 1;
double* work = new double[lwork];
double* tau = new double[std::min(num_rows_of_transpose,
num_cols_of_transpose)];
int* pivot = new int[num_cols_of_transpose];
int info;
// Compute the QR decomposition with column pivots of the transpose
// of this matrix by abusing the fact that the C++ memory model is
// row-major format, which is the transpose of the Fortran memory
// model (which is column-major). Passing the row-major data
// looks like an in-place transposition to Fortran.
dgeqp3(&num_rows_of_transpose,
&num_cols_of_transpose,
scratch.d_mat,
&num_rows_of_transpose,
pivot,
tau,
work,
&lwork,
&info);
// Fail if error in LAPACK routine.
CAROM_ASSERT(info == 0);
// Assume communicator is MPI_COMM_WORLD and get rank of this
// process
int is_mpi_initialized, is_mpi_finalized;
CAROM_ASSERT(MPI_Initialized(&is_mpi_initialized) == MPI_SUCCESS);
CAROM_ASSERT(MPI_Finalized(&is_mpi_finalized) == MPI_SUCCESS);
int my_rank = 0;
if(is_mpi_initialized && !is_mpi_finalized) {
const MPI_Comm my_comm = MPI_COMM_WORLD;
CAROM_ASSERT(MPI_Comm_rank(my_comm, &my_rank) == MPI_SUCCESS);
}
// Copy over pivots and subtract one to convert them from a
// Fortran-based indexing convention (first element of 1-D array by
// default corresponds to index of 1, though this convention can be
// overridden) to a C-based indexing convention (first element of
// 1-D array corresponds to index of 0).
for (int i = 0; i < pivots_requested; i++) {
row_pivot[i] = pivot[i] - 1;
row_pivot_owner[i] = my_rank;
}
// Free arrays
delete [] work;
delete [] tau;
delete [] pivot;
}
void
Matrix::qrcp_pivots_transpose_distributed(int* row_pivot,
int* row_pivot_owner,
int pivots_requested)
const
{
#ifdef CAROM_HAS_ELEMENTAL
// Shim to design interface; not implemented yet
// Check if distributed; otherwise, use serial implementation
CAROM_ASSERT(distributed());
// Elemental implementation
return qrcp_pivots_transpose_distributed_elemental
(row_pivot, row_pivot_owner, pivots_requested);
// TODO(oxberry1): ScaLAPACK implementation?
#else
CAROM_ASSERT(false);
#endif
}
void
Matrix::qrcp_pivots_transpose_distributed_elemental
(int* row_pivot, int* row_pivot_owner, int pivots_requested)
const
{
#ifdef CAROM_HAS_ELEMENTAL
// Check if distributed; otherwise, use serial implementation
CAROM_ASSERT(distributed());
// Check if balanced
if (balanced()) {
qrcp_pivots_transpose_distributed_elemental_balanced
(row_pivot, row_pivot_owner, pivots_requested);
}
else {
qrcp_pivots_transpose_distributed_elemental_unbalanced
(row_pivot, row_pivot_owner, pivots_requested);
}
#else
CAROM_ASSERT(false);
#endif
}
void
Matrix::qrcp_pivots_transpose_distributed_elemental_balanced
(int* row_pivot, int* row_pivot_owner, int pivots_requested)
const
{
#ifdef CAROM_HAS_ELEMENTAL
// Compute pivots redundantly across all processes using the QRCP
// from the distributed dense linear algebra library Elemental.
// The following assumptions are made in this implementation just to
// get a version up and running:
//
// (1) this->balanced() == true; // The matrix is balanced
//
// (2) Process 0 is the master rank of the object
//
// (3) Matrix rows are distributed block-cyclically over all processes
// starting on process zero with row 0, in increasing order of row
// index (all elements of a given row are still stored on the same
// process)
//
// (4) This Matrix is distributed over the MPI_COMM_WORLD
// communicator
//
// Some of these assumptions can be relaxed if the Matrix object
// stores more information.
// Check if distributed and balanced
CAROM_ASSERT(distributed() && balanced());
// Make sure arrays are allocated before entry; this method does not
// own the input pointers
CAROM_ASSERT(row_pivot != NULL);
CAROM_ASSERT(row_pivot_owner != NULL);
// Compute total number of rows to set global sizes of matrix
const MPI_Comm comm = MPI_COMM_WORLD;
const int master_rank = 0;
int num_total_rows = d_num_rows;
const int reduce_count = 1;
CAROM_ASSERT(MPI_Allreduce(MPI_IN_PLACE,
&num_total_rows,
reduce_count,
MPI_INT,
MPI_SUM,
comm) == MPI_SUCCESS);
// Number of pivots requested can't exceed the total number of rows
// of the matrix
CAROM_ASSERT(pivots_requested <= num_total_rows);
CAROM_ASSERT(pivots_requested > 0);
// To compute a column-pivoted QRCP using Elemental, we need to
// first get the data into datatypes Elemental can operate on.
// Construct a process grid on the communicator that is 1
// by (# processes), in column-major order; each process in the grid
// will own its own rows of the matrix
const El::Grid grid(comm, 1);
// The row communicator in the grid should have the same number
// of processes as the comm owned by the Matrix
CAROM_ASSERT(El::mpi::Size(grid.RowComm()) == d_num_procs);
// Instantiate the transposed matrix; Elemental calls the number of
// rows the "height" of the matrix, and the number of columns the
// "width" of the matrix
El::Int height = static_cast<El::Int>(numColumns());
El::Int width = static_cast<El::Int>(numRows());