-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility_impl_backup.hpp
1588 lines (1226 loc) · 47.4 KB
/
utility_impl_backup.hpp
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) 2012 The Regents of the University of California,
through Lawrence Berkeley National Laboratory.
Author: Lin Lin and Mathias Jacquelin
This file is part of PEXSI. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
(1) Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
(2) Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
(3) Neither the name of the University of California, Lawrence Berkeley
National Laboratory, U.S. Dept. of Energy nor the names of its contributors may
be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
You are under no obligation whatsoever to provide any bug fixes, patches, or
upgrades to the features, functionality or performance of the source code
("Enhancements") to anyone; however, if you choose to make your Enhancements
available either publicly, or directly to Lawrence Berkeley National
Laboratory, without imposing a separate written license agreement for such
Enhancements, then you hereby grant the following license: a non-exclusive,
royalty-free perpetual license to install, use, modify, prepare derivative
works, incorporate into other computer software, distribute, and sublicense
such enhancements or derivative works thereof, in binary and source code form.
*/
/// @file utility_impl.hpp
/// @brief Implementation of utility subroutines.
/// @date 2012-09-27
#ifndef _PEXSI_UTILITY_IMPL_HPP_
#define _PEXSI_UTILITY_IMPL_HPP_
#include <numeric>
//using namespace std;
using std::ifstream;
using std::ofstream;
using std::vector;
using std::cerr;
namespace PEXSI{
// *********************************************************************
// Sparse Matrix
// *********************************************************************
//---------------------------------------------------------
inline void ReadSparseMatrix ( const char* filename, SparseMatrix<Real>& spmat )
{
// FIXME
// Binary format
if( 1 ){
std::istringstream iss;
SharedRead( filename, iss );
deserialize( spmat.size, iss, NO_MASK );
deserialize( spmat.nnz, iss, NO_MASK );
deserialize( spmat.colptr, iss, NO_MASK );
deserialize( spmat.rowind, iss, NO_MASK );
deserialize( spmat.nzval, iss, NO_MASK );
}
// Ascii format
if( 0 ) {
ifstream fin(filename);
fin >> spmat.size >> spmat.nnz;
spmat.colptr.Resize( spmat.size+1 );
spmat.rowind.Resize( spmat.nnz );
spmat.nzval.Resize ( spmat.nnz );
for( Int i = 0; i < spmat.size + 1; i++ ){
fin >> spmat.colptr(i);
}
for( Int i = 0; i < spmat.nnz; i++ ){
fin >> spmat.rowind(i);
}
for( Int i = 0; i < spmat.nnz; i++ ){
fin >> spmat.nzval(i);
}
fin.close();
}
return ;
} // ----- end of function ReadSparseMatrix -----
//---------------------------------------------------------
inline void ReadDistSparseMatrix ( const char* filename, DistSparseMatrix<Real>& pspmat, MPI_Comm comm )
{
// Get the processor information within the current communicator
MPI_Barrier( comm );
int mpirank; MPI_Comm_rank(comm, &mpirank);
int mpisize; MPI_Comm_size(comm, &mpisize);
MPI_Status mpistat;
std::ifstream fin;
// FIXME Maybe change to MPI_Comm_Dup.
pspmat.comm = comm;
// Read basic information
if( mpirank == 0 ){
fin.open(filename);
if( !fin.good() ){
ErrorHandling( "File cannot be opened!" );
}
fin.read((char*)&pspmat.size, sizeof(Int));
fin.read((char*)&pspmat.nnz, sizeof(Int));
}
// FIXME Maybe need LongInt format to read the number of nonzeros later
MPI_Bcast(&pspmat.size, 1, MPI_INT, 0, comm);
MPI_Bcast(&pspmat.nnz, 1, MPI_INT, 0, comm);
// Read colptr
IntNumVec colptr(pspmat.size+1);
if( mpirank == 0 ){
Int tmp;
fin.read((char*)&tmp, sizeof(Int));
if( tmp != pspmat.size+1 ){
ErrorHandling( "colptr is not of the right size." );
}
fin.read((char*)colptr.Data(), sizeof(Int)*tmp);
}
MPI_Bcast(colptr.Data(), pspmat.size+1, MPI_INT, 0, comm);
// Compute the number of columns on each processor
IntNumVec numColLocalVec(mpisize);
Int numColLocal, numColFirst;
numColFirst = pspmat.size / mpisize;
SetValue( numColLocalVec, numColFirst );
numColLocalVec[mpisize-1] = pspmat.size - numColFirst * (mpisize-1); // Modify the last entry
numColLocal = numColLocalVec[mpirank];
pspmat.colptrLocal.Resize( numColLocal + 1 );
for( Int i = 0; i < numColLocal + 1; i++ ){
pspmat.colptrLocal[i] = colptr[mpirank * numColFirst+i] - colptr[mpirank * numColFirst] + 1;
}
// Calculate nnz_loc on each processor
pspmat.nnzLocal = pspmat.colptrLocal[numColLocal] - pspmat.colptrLocal[0];
pspmat.rowindLocal.Resize( pspmat.nnzLocal );
pspmat.nzvalLocal.Resize ( pspmat.nnzLocal );
// Read and distribute the row indices
if( mpirank == 0 ){
Int tmp;
fin.read((char*)&tmp, sizeof(Int));
if( tmp != pspmat.nnz ){
std::ostringstream msg;
msg
<< "The number of nonzeros in row indices do not match." << std::endl
<< "nnz = " << pspmat.nnz << std::endl
<< "size of row indices = " << tmp << std::endl;
ErrorHandling( msg.str().c_str() );
}
IntNumVec buf;
Int numRead;
for( Int ip = 0; ip < mpisize; ip++ ){
numRead = colptr[ip*numColFirst + numColLocalVec[ip]] -
colptr[ip*numColFirst];
buf.Resize(numRead);
fin.read( (char*)buf.Data(), numRead*sizeof(Int) );
if( ip > 0 ){
MPI_Send(&numRead, 1, MPI_INT, ip, 0, comm);
MPI_Send(buf.Data(), numRead, MPI_INT, ip, 1, comm);
}
else{
pspmat.rowindLocal = buf;
}
}
}
else{
Int numRead;
MPI_Recv(&numRead, 1, MPI_INT, 0, 0, comm, &mpistat);
if( numRead != pspmat.nnzLocal ){
std::ostringstream msg;
msg << "The number of columns in row indices do not match." << std::endl
<< "numRead = " << numRead << std::endl
<< "nnzLocal = " << pspmat.nnzLocal << std::endl;
ErrorHandling( msg.str().c_str() );
}
pspmat.rowindLocal.Resize( numRead );
MPI_Recv( pspmat.rowindLocal.Data(), numRead, MPI_INT, 0, 1, comm, &mpistat );
}
// Read and distribute the nonzero values
if( mpirank == 0 ){
Int tmp;
fin.read((char*)&tmp, sizeof(Int));
if( tmp != pspmat.nnz ){
std::ostringstream msg;
msg
<< "The number of nonzeros in values do not match." << std::endl
<< "nnz = " << pspmat.nnz << std::endl
<< "size of values = " << tmp << std::endl;
ErrorHandling( msg.str().c_str() );
}
NumVec<Real> buf;
Int numRead;
for( Int ip = 0; ip < mpisize; ip++ ){
numRead = colptr[ip*numColFirst + numColLocalVec[ip]] -
colptr[ip*numColFirst];
buf.Resize(numRead);
fin.read( (char*)buf.Data(), numRead*sizeof(Real) );
if( ip > 0 ){
MPI_Send(&numRead, 1, MPI_INT, ip, 0, comm);
MPI_Send(buf.Data(), numRead, MPI_DOUBLE, ip, 1, comm);
}
else{
pspmat.nzvalLocal = buf;
}
}
}
else{
Int numRead;
MPI_Recv(&numRead, 1, MPI_INT, 0, 0, comm, &mpistat);
if( numRead != pspmat.nnzLocal ){
std::ostringstream msg;
msg << "The number of columns in values do not match." << std::endl
<< "numRead = " << numRead << std::endl
<< "nnzLocal = " << pspmat.nnzLocal << std::endl;
ErrorHandling( msg.str().c_str() );
}
pspmat.nzvalLocal.Resize( numRead );
MPI_Recv( pspmat.nzvalLocal.Data(), numRead, MPI_DOUBLE, 0, 1, comm, &mpistat );
}
// Close the file
if( mpirank == 0 ){
fin.close();
}
MPI_Barrier( comm );
return ;
} // ----- end of function ReadDistSparseMatrix -----
inline void ParaWriteDistSparseMatrix ( const char* filename, DistSparseMatrix<Real>& pspmat, MPI_Comm comm )
{
// Get the processor information within the current communicator
MPI_Barrier( comm );
int mpirank; MPI_Comm_rank(comm, &mpirank);
int mpisize; MPI_Comm_size(comm, &mpisize);
MPI_Status mpistat;
Int err = 0;
int filemode = MPI_MODE_WRONLY | MPI_MODE_CREATE | MPI_MODE_UNIQUE_OPEN;
MPI_File fout;
MPI_Status status;
err = MPI_File_open(comm,(char*) filename, filemode, MPI_INFO_NULL, &fout);
if (err != MPI_SUCCESS) {
ErrorHandling( "File cannot be opened!" );
}
// FIXME Note that nnz uses the Int data type for consistency of writing / reading
// Write header
if( mpirank == 0 ){
err = MPI_File_write_at(fout, 0,(char*)&pspmat.size, 1, MPI_INT, &status);
err = MPI_File_write_at(fout, sizeof(Int),(char*)&pspmat.nnz, 1, MPI_INT, &status);
}
// Compute the number of columns on each processor
Int numColLocal = pspmat.colptrLocal.m()-1;
Int numColFirst = pspmat.size / mpisize;
IntNumVec colptrChunk(numColLocal+1);
Int prev_nz = 0;
MPI_Exscan(&pspmat.nnzLocal, &prev_nz, 1, MPI_INT, MPI_SUM, comm);
for( Int i = 0; i < numColLocal + 1; i++ ){
colptrChunk[i] = pspmat.colptrLocal[i] + prev_nz;
}
MPI_Datatype memtype, filetype;
MPI_Aint disps[6];
int blklens[6];
MPI_Datatype types[6] = {MPI_INT,MPI_INT, MPI_INT,MPI_INT, MPI_INT,MPI_DOUBLE};
/* set block lengths (same for both types) */
blklens[0] = (mpirank==0)?1:0;
blklens[1] = numColLocal+1;
blklens[2] = (mpirank==0)?1:0;
blklens[3] = pspmat.nnzLocal;
blklens[4] = (mpirank==0)?1:0;
blklens[5] = pspmat.nnzLocal;
//Calculate offsets
MPI_Offset myColPtrOffset, myRowIdxOffset, myNzValOffset;
myColPtrOffset = 3*sizeof(int) + (mpirank*numColFirst)*sizeof(Int);
myRowIdxOffset = 3*sizeof(int) + (pspmat.size +1 + prev_nz)*sizeof(Int);
myNzValOffset = 4*sizeof(int) + (pspmat.size +1 + pspmat.nnz)*sizeof(Int)+ prev_nz*sizeof(Real);
disps[0] = 2*sizeof(int);
disps[1] = myColPtrOffset;
disps[2] = myRowIdxOffset;
disps[3] = sizeof(int)+myRowIdxOffset;
disps[4] = myNzValOffset;
disps[5] = sizeof(int)+myNzValOffset;
#if ( _DEBUGlevel_ >= 1 )
char msg[200];
char * tmp = msg;
tmp += sprintf(tmp,"P%d ",mpirank);
for(int i = 0; i<6; ++i){
if(i==5)
tmp += sprintf(tmp, "%d [%d - %d] | ",i,disps[i],disps[i]+blklens[i]*sizeof(double));
else
tmp += sprintf(tmp, "%d [%d - %d] | ",i,disps[i],disps[i]+blklens[i]*sizeof(int));
}
tmp += sprintf(tmp,"\n");
printf("%s",msg);
#endif
MPI_Type_create_struct(6, blklens, disps, types, &filetype);
MPI_Type_commit(&filetype);
/* create memory type */
Int np1 = pspmat.size+1;
MPI_Get_address( (void *)&np1, &disps[0]);
MPI_Get_address(colptrChunk.Data(), &disps[1]);
MPI_Get_address( (void *)&pspmat.nnz, &disps[2]);
MPI_Get_address((void *)pspmat.rowindLocal.Data(), &disps[3]);
MPI_Get_address( (void *)&pspmat.nnz, &disps[4]);
MPI_Get_address((void *)pspmat.nzvalLocal.Data(), &disps[5]);
MPI_Type_create_struct(6, blklens, disps, types, &memtype);
MPI_Type_commit(&memtype);
/* set file view */
err = MPI_File_set_view(fout, 0, MPI_BYTE, filetype, "native",MPI_INFO_NULL);
/* everyone writes their own row offsets, columns, and
* data with one big noncontiguous write (in memory and
* file)
*/
err = MPI_File_write_all(fout, MPI_BOTTOM, 1, memtype, &status);
MPI_Type_free(&filetype);
MPI_Type_free(&memtype);
MPI_Barrier( comm );
MPI_File_close(&fout);
return ;
} // ----- end of function ParaWriteDistSparseMatrix -----
inline void ParaReadDistSparseMatrix ( const char* filename, DistSparseMatrix<Real>& pspmat, MPI_Comm comm )
{
// Get the processor information within the current communicator
MPI_Barrier( comm );
int mpirank; MPI_Comm_rank(comm, &mpirank);
int mpisize; MPI_Comm_size(comm, &mpisize);
MPI_Status mpistat;
MPI_Datatype type;
int lens[3];
MPI_Aint disps[3]; //MPI_Aint是用来保存地址的类型哟
MPI_Datatype types[3];
Int err = 0;
int filemode = MPI_MODE_RDONLY | MPI_MODE_UNIQUE_OPEN;
MPI_File fin; //这是文件句柄
MPI_Status status;
// FIXME Maybe change to MPI_Comm_Dup.
pspmat.comm = comm;//所有进程的comm复制下来了
err = MPI_File_open(comm,(char*) filename, filemode, MPI_INFO_NULL, &fin);
if (err != MPI_SUCCESS) {
ErrorHandling( "File cannot be opened!" );
}
// FIXME Note that nnz uses the Int data type for consistency of writing / reading
// Read header
if( mpirank == 0 ){
//在第0个进程读取了dimension和nnz,也就是说应该是第零个进程用来分配数据的
err = MPI_File_read_at(fin, 0,(char*)&pspmat.size, 1, MPI_INT, &status);
err = MPI_File_read_at(fin, sizeof(Int),(char*)&pspmat.nnz, 1, MPI_INT, &status);
}
/* define a struct that describes all our data */
//首先将地址和长度以及类型保存下来
lens[0] = 1;
lens[1] = 1;
MPI_Get_address(&pspmat.size, &disps[0]);
MPI_Get_address(&pspmat.nnz, &disps[1]);
types[0] = MPI_INT;
types[1] = MPI_INT;
MPI_Type_create_struct(2, lens, disps, types, &type);
MPI_Type_commit(&type);
/* broadcast the header data to everyone */
MPI_Bcast(MPI_BOTTOM, 1, type, 0, comm);//MPI_BOTTOM指向内存的底部,相当于绝对地址0,即现在每一个processor都了size和nnz
MPI_Type_free(&type);//用完了就free
// Compute the number of columns on each processor
IntNumVec numColLocalVec(mpisize);//每个processor的column的数量
Int numColLocal, numColFirst;
numColFirst = pspmat.size / mpisize;
SetValue( numColLocalVec, numColFirst );
numColLocalVec[mpisize-1] = pspmat.size - numColFirst * (mpisize-1); // Modify the last entry
numColLocal = numColLocalVec[mpirank];
pspmat.colptrLocal.Resize( numColLocal + 1 );//对本processor保存的colptr的大小进行修改
//这个应该是第一个colptr的文件偏移
MPI_Offset myColPtrOffset = (2 + ((mpirank==0)?0:1) )*sizeof(int) + (mpirank*numColFirst)*sizeof(Int);
Int np1 = 0;
lens[0] = (mpirank==0)?1:0;
lens[1] = numColLocal + 1;
MPI_Get_address(&np1, &disps[0]);
MPI_Get_address(pspmat.colptrLocal.Data(), &disps[1]);
MPI_Type_create_hindexed(2, lens, disps, MPI_INT, &type);
MPI_Type_commit(&type);
//这里是把这个processor的colptr都从文件中读取进来了
err= MPI_File_read_at_all(fin, myColPtrOffset, MPI_BOTTOM, 1, type, &status);
if (err != MPI_SUCCESS) {
ErrorHandling( "error reading colptr" );
}
MPI_Type_free(&type);
// Calculate nnz_loc on each processor
//计算有多少非零元素
pspmat.nnzLocal = pspmat.colptrLocal[numColLocal] - pspmat.colptrLocal[0];
//根据非零元素来修改rowindices和nonzerovalues
pspmat.rowindLocal.Resize( pspmat.nnzLocal );
pspmat.nzvalLocal.Resize ( pspmat.nnzLocal );
//read rowIdx
//这里计算了每个processor的rowindices的偏移量
MPI_Offset myRowIdxOffset = (3 + ((mpirank==0)?0:1) )*sizeof(int) + (pspmat.size+1 + (pspmat.colptrLocal[0]-1))*sizeof(int);
lens[0] = (mpirank==0)?1:0;
lens[1] = pspmat.nnzLocal;
MPI_Get_address(&np1, &disps[0]);
MPI_Get_address(pspmat.rowindLocal.Data(), &disps[1]);
MPI_Type_create_hindexed(2, lens, disps, MPI_INT, &type);
MPI_Type_commit(&type);
//这里就全部读取了rowindices
err= MPI_File_read_at_all(fin, myRowIdxOffset, MPI_BOTTOM, 1, type,&status);
if (err != MPI_SUCCESS) {
ErrorHandling( "error reading rowind" );
}
MPI_Type_free(&type);
//read nzval
// MPI_Offset myNzValOffset = (4 + ((mpirank==0)?0:1) )*sizeof(int) + (pspmat.size+1 + pspmat.nnz)*sizeof(Int) + (pspmat.colptrLocal[0]-1)*sizeof(double);
//
// lens[0] = (mpirank==0)?1:0;
// lens[1] = pspmat.nnzLocal;
//
// MPI_Get_address(&np1, &disps[0]);
// MPI_Get_address(pspmat.nzvalLocal.Data(), &disps[1]);
//
// types[0] = MPI_INT;
// types[1] = MPI_DOUBLE;
//
// MPI_Type_create_struct(2, lens, disps, types, &type);
// MPI_Type_commit(&type);
//
// err = MPI_File_read_at_all(fin, myNzValOffset, MPI_BOTTOM, 1, type,&status);
// MPI_Type_free(&type);
//这里一个np不知道什么东西
if( mpirank == 0 ){
MPI_Offset myNzValOffset = (4 )*sizeof(int) + (pspmat.size+1 + pspmat.nnz)*sizeof(int) + (pspmat.colptrLocal[0]-1)*sizeof(double);
err = MPI_File_read_at(fin, myNzValOffset,(char*)&np1, 1, MPI_INT, &status);
}
MPI_Offset myNzValOffset = (5 )*sizeof(int) + (pspmat.size+1 + pspmat.nnz)*sizeof(int) + (pspmat.colptrLocal[0]-1)*sizeof(double);
err = MPI_File_read_at_all(fin, myNzValOffset, pspmat.nzvalLocal.Data(), pspmat.nnzLocal, MPI_DOUBLE,&status);
if (err != MPI_SUCCESS) {
ErrorHandling( "error reading nzval" );
}
//convert to local references
for( Int i = 1; i < numColLocal + 1; i++ ){
pspmat.colptrLocal[i] = pspmat.colptrLocal[i] - pspmat.colptrLocal[0] + 1;
}
pspmat.colptrLocal[0]=1;
MPI_Barrier( comm );
MPI_File_close(&fin);
return ;
} // ----- end of function ParaReadDistSparseMatrix -----
inline void ReadDistSparseMatrixFormatted ( const char* filename, DistSparseMatrix<Real>& pspmat, MPI_Comm comm )
{
// Get the processor information within the current communicator
MPI_Barrier( comm );
int mpirank; MPI_Comm_rank(comm, &mpirank);
int mpisize; MPI_Comm_size(comm, &mpisize);
MPI_Status mpistat;
std::ifstream fin;
// FIXME Maybe change to MPI_Comm_Dup.
pspmat.comm = comm;
// FIXME Maybe need LongInt format to read the number of nonzeros later
// Read basic information
if( mpirank == 0 ){
fin.open(filename);
if( !fin.good() ){
ErrorHandling( "File cannot be opened!" );
}
Int dummy;
fin >> pspmat.size >> dummy ;
fin >> pspmat.nnz >> dummy;
// FIXME this is temporary and only applies to 4*4 matrix.
// fin >> dummy;
}
MPI_Bcast(&pspmat.size, 1, MPI_INT, 0, comm);
MPI_Bcast(&pspmat.nnz, 1, MPI_INT, 0, comm);
// Read colptr
IntNumVec colptr(pspmat.size+1);
if( mpirank == 0 ){
Int* ptr = colptr.Data();
for( Int i = 0; i < pspmat.size+1; i++ )
fin >> *(ptr++);
}
MPI_Bcast(colptr.Data(), pspmat.size+1, MPI_INT, 0, comm);
// Compute the number of columns on each processor
IntNumVec numColLocalVec(mpisize);
Int numColLocal, numColFirst;
numColFirst = pspmat.size / mpisize;
SetValue( numColLocalVec, numColFirst );
numColLocalVec[mpisize-1] = pspmat.size - numColFirst * (mpisize-1); // Modify the last entry
numColLocal = numColLocalVec[mpirank];
pspmat.colptrLocal.Resize( numColLocal + 1 );
for( Int i = 0; i < numColLocal + 1; i++ ){
pspmat.colptrLocal[i] = colptr[mpirank * numColFirst+i] - colptr[mpirank * numColFirst] + 1;
}
// Calculate nnz_loc on each processor
pspmat.nnzLocal = pspmat.colptrLocal[numColLocal] - pspmat.colptrLocal[0];
pspmat.rowindLocal.Resize( pspmat.nnzLocal );
pspmat.nzvalLocal.Resize ( pspmat.nnzLocal );
// Read and distribute the row indices
if( mpirank == 0 ){
Int tmp;
IntNumVec buf;
Int numRead;
for( Int ip = 0; ip < mpisize; ip++ ){
numRead = colptr[ip*numColFirst + numColLocalVec[ip]] -
colptr[ip*numColFirst];
buf.Resize(numRead);
Int *ptr = buf.Data();
for( Int i = 0; i < numRead; i++ ){
fin >> *(ptr++);
}
if( ip > 0 ){
MPI_Send(&numRead, 1, MPI_INT, ip, 0, comm);
MPI_Send(buf.Data(), numRead, MPI_INT, ip, 1, comm);
}
else{
pspmat.rowindLocal = buf;
}
}
}
else{
Int numRead;
MPI_Recv(&numRead, 1, MPI_INT, 0, 0, comm, &mpistat);
if( numRead != pspmat.nnzLocal ){
std::ostringstream msg;
msg << "The number of columns in row indices do not match." << std::endl
<< "numRead = " << numRead << std::endl
<< "nnzLocal = " << pspmat.nnzLocal << std::endl;
ErrorHandling( msg.str().c_str() );
}
pspmat.rowindLocal.Resize( numRead );
MPI_Recv( pspmat.rowindLocal.Data(), numRead, MPI_INT, 0, 1, comm, &mpistat );
}
// std::cout << "Proc " << mpirank << " outputs rowindLocal.size() = "
// << pspmat.rowindLocal.m() << endl;
// Read and distribute the nonzero values
if( mpirank == 0 ){
Int tmp;
NumVec<Real> buf;
Int numRead;
for( Int ip = 0; ip < mpisize; ip++ ){
numRead = colptr[ip*numColFirst + numColLocalVec[ip]] -
colptr[ip*numColFirst];
buf.Resize(numRead);
Real *ptr = buf.Data();
for( Int i = 0; i < numRead; i++ ){
fin >> *(ptr++);
}
if( ip > 0 ){
MPI_Send(&numRead, 1, MPI_INT, ip, 0, comm);
MPI_Send(buf.Data(), numRead, MPI_DOUBLE, ip, 1, comm);
}
else{
pspmat.nzvalLocal = buf;
}
}
}
else{
Int numRead;
MPI_Recv(&numRead, 1, MPI_INT, 0, 0, comm, &mpistat);
if( numRead != pspmat.nnzLocal ){
std::ostringstream msg;
msg << "The number of columns in values do not match." << std::endl
<< "numRead = " << numRead << std::endl
<< "nnzLocal = " << pspmat.nnzLocal << std::endl;
ErrorHandling( msg.str().c_str() );
}
pspmat.nzvalLocal.Resize( numRead );
MPI_Recv( pspmat.nzvalLocal.Data(), numRead, MPI_DOUBLE, 0, 1, comm, &mpistat );
}
// Close the file
if( mpirank == 0 ){
fin.close();
}
MPI_Barrier( comm );
return ;
} // ----- end of function ReadDistSparseMatrixFormatted -----
inline void ParaReadDistSparseMatrix ( const char* filename, DistSparseMatrix<Complex>& pspmat, MPI_Comm comm )
{
// Get the processor information within the current communicator
MPI_Barrier( comm );
int mpirank; MPI_Comm_rank(comm, &mpirank);
int mpisize; MPI_Comm_size(comm, &mpisize);
MPI_Status mpistat;
MPI_Datatype type;
int lens[3];
MPI_Aint disps[3];
MPI_Datatype types[3];
Int err = 0;
int filemode = MPI_MODE_RDONLY | MPI_MODE_UNIQUE_OPEN;
MPI_File fin;
MPI_Status status;
// FIXME Maybe change to MPI_Comm_Dup.
pspmat.comm = comm;
err = MPI_File_open(comm,(char*) filename, filemode, MPI_INFO_NULL, &fin);
if (err != MPI_SUCCESS) {
ErrorHandling( "File cannot be opened!" );
}
// FIXME Note that nnz uses the Int data type for consistency of writing / reading
// Read header
if( mpirank == 0 ){
err = MPI_File_read_at(fin, 0,(char*)&pspmat.size, 1, MPI_INT, &status);
err = MPI_File_read_at(fin, sizeof(Int),(char*)&pspmat.nnz, 1, MPI_INT, &status);
}
/* define a struct that describes all our data */
lens[0] = 1;
lens[1] = 1;
MPI_Get_address(&pspmat.size, &disps[0]);
MPI_Get_address(&pspmat.nnz, &disps[1]);
types[0] = MPI_INT;
types[1] = MPI_INT;
MPI_Type_create_struct(2, lens, disps, types, &type);
MPI_Type_commit(&type);
/* broadcast the header data to everyone */
MPI_Bcast(MPI_BOTTOM, 1, type, 0, comm);
MPI_Type_free(&type);
// Compute the number of columns on each processor
IntNumVec numColLocalVec(mpisize);
Int numColLocal, numColFirst;
numColFirst = pspmat.size / mpisize;
SetValue( numColLocalVec, numColFirst );
numColLocalVec[mpisize-1] = pspmat.size - numColFirst * (mpisize-1); // Modify the last entry
numColLocal = numColLocalVec[mpirank];
pspmat.colptrLocal.Resize( numColLocal + 1 );
MPI_Offset myColPtrOffset = (2 + ((mpirank==0)?0:1) )*sizeof(int) + (mpirank*numColFirst)*sizeof(Int);
Int np1 = 0;
lens[0] = (mpirank==0)?1:0;
lens[1] = numColLocal + 1;
MPI_Get_address(&np1, &disps[0]);
MPI_Get_address(pspmat.colptrLocal.Data(), &disps[1]);
MPI_Type_create_hindexed(2, lens, disps, MPI_INT, &type);
MPI_Type_commit(&type);
err= MPI_File_read_at_all(fin, myColPtrOffset, MPI_BOTTOM, 1, type, &status);
if (err != MPI_SUCCESS) {
ErrorHandling( "error reading colptr" );
}
MPI_Type_free(&type);
// Calculate nnz_loc on each processor
pspmat.nnzLocal = pspmat.colptrLocal[numColLocal] - pspmat.colptrLocal[0];
pspmat.rowindLocal.Resize( pspmat.nnzLocal );
pspmat.nzvalLocal.Resize ( pspmat.nnzLocal );
//read rowIdx
MPI_Offset myRowIdxOffset = (3 + ((mpirank==0)?0:1) )*sizeof(int) + (pspmat.size+1 + pspmat.colptrLocal[0]-1)*sizeof(Int);
lens[0] = (mpirank==0)?1:0;
lens[1] = pspmat.nnzLocal;
MPI_Get_address(&np1, &disps[0]);
MPI_Get_address(pspmat.rowindLocal.Data(), &disps[1]);
// MPI_Type_create_hindexed(2, lens, disps, MPI_INT, &type);
MPI_Type_create_hindexed(2, lens, disps, MPI_INT, &type);
MPI_Type_commit(&type);
err= MPI_File_read_at_all(fin, myRowIdxOffset, MPI_BOTTOM, 1, type,&status);
if (err != MPI_SUCCESS) {
ErrorHandling( "error reading rowind" );
}
MPI_Type_free(&type);
//read nzval
// MPI_Offset myNzValOffset = (4 + ((mpirank==0)?0:1) )*sizeof(int) + (pspmat.size+1 + pspmat.nnz)*sizeof(int) + (pspmat.colptrLocal[0]-1)*sizeof(Complex);
// lens[0] = ((mpirank==0)?1:0)*sizeof(Int);
// lens[1] = pspmat.nnzLocal*sizeof(Complex);
//
// MPI_Get_address(&np1, &disps[0]);
// MPI_Get_address(pspmat.nzvalLocal.Data(), &disps[1]);
//
// types[0] = MPI_INT;
// types[1] = MPI_DOUBLE_COMPLEX;
// MPI_Type_create_struct(2, lens, disps, types, &type);
// MPI_Type_commit(&type);
//
// err = MPI_File_read_at_all(fin, myNzValOffset, MPI_BOTTOM, 1, type,&status);
//
// MPI_Type_free(&type);
if( mpirank == 0 ){
MPI_Offset myNzValOffset = (4 )*sizeof(int) + (pspmat.size+1 + pspmat.nnz)*sizeof(int) + (pspmat.colptrLocal[0]-1)*sizeof(Complex);
err = MPI_File_read_at(fin, myNzValOffset,(char*)&np1, 1, MPI_INT, &status);
}
MPI_Offset myNzValOffset = (5 )*sizeof(int) + (pspmat.size+1 + pspmat.nnz)*sizeof(int) + (pspmat.colptrLocal[0]-1)*sizeof(Complex);
err = MPI_File_read_at_all(fin, myNzValOffset, pspmat.nzvalLocal.Data(), pspmat.nnzLocal, MPI_DOUBLE_COMPLEX,&status);
if (err != MPI_SUCCESS) {
ErrorHandling( "error reading nzval" );
}
//convert to local references
for( Int i = 1; i < numColLocal + 1; i++ ){
pspmat.colptrLocal[i] = pspmat.colptrLocal[i] - pspmat.colptrLocal[0] + 1;
}
pspmat.colptrLocal[0]=1;
MPI_Barrier( comm );
MPI_File_close(&fin);
return ;
} // ----- end of function ParaReadDistSparseMatrix -----
inline void ParaWriteDistSparseMatrix ( const char* filename, DistSparseMatrix<Complex>& pspmat, MPI_Comm comm )
{
// Get the processor information within the current communicator
MPI_Barrier( comm );
int mpirank; MPI_Comm_rank(comm, &mpirank);
int mpisize; MPI_Comm_size(comm, &mpisize);
MPI_Status mpistat;
Int err = 0;
int filemode = MPI_MODE_WRONLY | MPI_MODE_CREATE | MPI_MODE_UNIQUE_OPEN;
MPI_File fout;
MPI_Status status;
err = MPI_File_open(comm,(char*) filename, filemode, MPI_INFO_NULL, &fout);
if (err != MPI_SUCCESS) {
ErrorHandling( "File cannot be opened!" );
}
// FIXME Note that nnz uses the Int data type for consistency of writing / reading
// Write header
if( mpirank == 0 ){
err = MPI_File_write_at(fout, 0,(char*)&pspmat.size, 1, MPI_INT, &status);
err = MPI_File_write_at(fout, sizeof(Int),(char*)&pspmat.nnz, 1, MPI_INT, &status);
}
// Compute the number of columns on each processor
Int numColLocal = pspmat.colptrLocal.m()-1;
Int numColFirst = pspmat.size / mpisize;
IntNumVec colptrChunk(numColLocal+1);
Int prev_nz = 0;
MPI_Exscan(&pspmat.nnzLocal, &prev_nz, 1, MPI_INT, MPI_SUM, comm);
for( Int i = 0; i < numColLocal + 1; i++ ){
colptrChunk[i] = pspmat.colptrLocal[i] + prev_nz;
}
MPI_Datatype memtype, filetype;
MPI_Aint disps[6];
int blklens[6];
MPI_Datatype types[6] = {MPI_INT,MPI_INT, MPI_INT,MPI_INT, MPI_INT,MPI_DOUBLE_COMPLEX};
/* set block lengths (same for both types) */
blklens[0] = (mpirank==0)?1:0;
blklens[1] = numColLocal+1;
blklens[2] = (mpirank==0)?1:0;
blklens[3] = pspmat.nnzLocal;
blklens[4] = (mpirank==0)?1:0;
blklens[5] = pspmat.nnzLocal;
//Calculate offsets
MPI_Offset myColPtrOffset, myRowIdxOffset, myNzValOffset;
myColPtrOffset = 3*sizeof(int) + (mpirank*numColFirst)*sizeof(Int);
myRowIdxOffset = 3*sizeof(int) + (pspmat.size +1 + prev_nz)*sizeof(Int);
myNzValOffset = 4*sizeof(int) + (pspmat.size +1 + pspmat.nnz)*sizeof(Int)+ prev_nz*sizeof(Complex);
disps[0] = 2*sizeof(int);
disps[1] = myColPtrOffset;
disps[2] = myRowIdxOffset;
disps[3] = sizeof(int)+myRowIdxOffset;
disps[4] = myNzValOffset;
disps[5] = sizeof(int)+myNzValOffset;
#if ( _DEBUGlevel_ >= 1 )
char msg[200];
char * tmp = msg;
tmp += sprintf(tmp,"P%d ",mpirank);
for(int i = 0; i<6; ++i){
if(i==5)
tmp += sprintf(tmp, "%d [%d - %d] | ",i,disps[i],disps[i]+blklens[i]*sizeof(Complex));
else
tmp += sprintf(tmp, "%d [%d - %d] | ",i,disps[i],disps[i]+blklens[i]*sizeof(int));
}
tmp += sprintf(tmp,"\n");
printf("%s",msg);
#endif
MPI_Type_create_struct(6, blklens, disps, types, &filetype);
MPI_Type_commit(&filetype);
/* create memory type */
Int np1 = pspmat.size+1;
MPI_Get_address( (void *)&np1, &disps[0]);
MPI_Get_address(colptrChunk.Data(), &disps[1]);
MPI_Get_address( (void *)&pspmat.nnz, &disps[2]);
MPI_Get_address((void *)pspmat.rowindLocal.Data(), &disps[3]);
MPI_Get_address( (void *)&pspmat.nnz, &disps[4]);
MPI_Get_address((void *)pspmat.nzvalLocal.Data(), &disps[5]);
MPI_Type_create_struct(6, blklens, disps, types, &memtype);
MPI_Type_commit(&memtype);
/* set file view */
err = MPI_File_set_view(fout, 0, MPI_BYTE, filetype, "native",MPI_INFO_NULL);
/* everyone writes their own row offsets, columns, and
* data with one big noncontiguous write (in memory and
* file)
*/
err = MPI_File_write_all(fout, MPI_BOTTOM, 1, memtype, &status);
MPI_Type_free(&filetype);
MPI_Type_free(&memtype);
MPI_Barrier( comm );