1 #ifndef lint 2 static char vcid[] = "$Id: matio.c,v 1.28 1996/06/18 20:31:22 balay Exp balay $"; 3 #endif 4 5 /* 6 This file contains simple binary read/write routines for matrices. 7 */ 8 9 #include "petsc.h" 10 #include "../matimpl.h" 11 #include "sys.h" 12 #include "pinclude/pviewer.h" 13 14 extern int MatLoad_MPIRowbs(Viewer,MatType,Mat*); 15 extern int MatLoad_SeqAIJ(Viewer,MatType,Mat*); 16 extern int MatLoad_MPIAIJ(Viewer,MatType,Mat*); 17 extern int MatLoad_SeqBDiag(Viewer,MatType,Mat*); 18 extern int MatLoad_MPIBDiag(Viewer,MatType,Mat*); 19 extern int MatLoad_SeqDense(Viewer,MatType,Mat*); 20 extern int MatLoad_MPIDense(Viewer,MatType,Mat*); 21 extern int MatLoad_SeqBAIJ(Viewer,MatType,Mat*); 22 extern int MatLoad_MPIBAIJ(Viewer,MatType,Mat*); 23 24 extern int MatLoadGetInfo_Private(Viewer); 25 26 /*@C 27 MatLoad - Loads a matrix that has been stored in binary format 28 with MatView(). 29 30 Input Parameters: 31 . viewer - binary file viewer, created with ViewerFileOpenBinary() 32 . outtype - type of matrix desired, for example MATSEQAIJ, 33 MATMPIROWBS, etc. See types in petsc/include/mat.h. 34 35 Output Parameters: 36 . newmat - new matrix 37 38 Options Database Key: 39 Used with block matrix formats (MATSEQBAIJ, MATMPIBDIAG, etc.) 40 $ -matload_block_size <bs> 41 42 Notes: 43 In parallel, each processor can load a subset of rows (or the 44 entire matrix). This routine is especially useful when a large 45 matrix is stored on disk and only part of it is desired on each 46 processor. For example, a parallel solver may access only some of 47 the rows from each processor. The algorithm used here reads 48 relatively small blocks of data rather than reading the entire 49 matrix and then subsetting it. 50 51 Notes for advanced users: 52 Most users should not need to know the details of the binary storage 53 format, since MatLoad() and MatView() completely hide these details. 54 But for anyone who's interested, the standard binary matrix storage 55 format is 56 57 $ int MAT_COOKIE 58 $ int number of rows 59 $ int number of columns 60 $ int total number of nonzeros 61 $ int *number nonzeros in each row 62 $ int *column indices of all nonzeros (starting index is zero) 63 $ Scalar *values of all nonzeros 64 65 .keywords: matrix, load, binary, input 66 67 .seealso: ViewerFileOpenBinary(), MatView(), VecLoad() 68 @*/ 69 int MatLoad(Viewer viewer,MatType outtype,Mat *newmat) 70 { 71 int ierr,set; 72 MatType type; 73 ViewerType vtype; 74 MPI_Comm comm; 75 *newmat = 0; 76 77 PetscValidHeaderSpecific(viewer,VIEWER_COOKIE); 78 ierr = ViewerGetType(viewer,&vtype); CHKERRQ(ierr); 79 if (vtype != BINARY_FILE_VIEWER) 80 SETERRQ(1,"MatLoad: Invalid viewer; open viewer with ViewerFileOpenBinary()"); 81 82 PetscObjectGetComm((PetscObject)viewer,&comm); 83 ierr = MatGetTypeFromOptions(comm,0,&type,&set); CHKERRQ(ierr); 84 if (!set) type = outtype; 85 86 ierr = MatLoadGetInfo_Private(viewer); CHKERRQ(ierr); 87 88 PLogEventBegin(MAT_Load,viewer,0,0,0); 89 90 if (type == MATSEQAIJ) { 91 ierr = MatLoad_SeqAIJ(viewer,type,newmat); CHKERRQ(ierr); 92 } 93 else if (type == MATMPIAIJ) { 94 ierr = MatLoad_MPIAIJ(viewer,type,newmat); CHKERRQ(ierr); 95 } 96 else if (type == MATSEQBDIAG) { 97 ierr = MatLoad_SeqBDiag(viewer,type,newmat); CHKERRQ(ierr); 98 } 99 else if (type == MATMPIBDIAG) { 100 ierr = MatLoad_MPIBDiag(viewer,type,newmat); CHKERRQ(ierr); 101 } 102 else if (type == MATSEQDENSE) { 103 ierr = MatLoad_SeqDense(viewer,type,newmat); CHKERRQ(ierr); 104 } 105 else if (type == MATMPIDENSE) { 106 ierr = MatLoad_MPIDense(viewer,type,newmat); CHKERRQ(ierr); 107 } 108 else if (type == MATMPIROWBS) { 109 #if defined(HAVE_BLOCKSOLVE) && !defined(__cplusplus) 110 ierr = MatLoad_MPIRowbs(viewer,type,newmat); CHKERRQ(ierr); 111 #else 112 SETERRQ(1,"MatLoad: MATMPIROWBS format does not support complex numbers."); 113 #endif 114 } 115 else if (type == MATSEQBAIJ) { 116 ierr = MatLoad_SeqBAIJ(viewer,type,newmat); CHKERRQ(ierr); 117 } 118 else if (type == MATMPIBAIJ) { 119 ierr = MatLoad_MPIBAIJ(viewer,type,newmat); CHKERRQ(ierr); 120 } 121 else { 122 SETERRQ(1,"MatLoad: cannot load with that matrix type yet"); 123 } 124 125 PLogEventEnd(MAT_Load,viewer,0,0,0); 126 return 0; 127 } 128 129 /* 130 MatLoadGetInfo_Private - Loads the matrix options from the name.info file 131 if it exists. 132 133 */ 134 int MatLoadGetInfo_Private(Viewer viewer) 135 { 136 FILE *file; 137 char string[128],*first,*second,*final; 138 int len,ierr,flg; 139 140 ierr = OptionsHasName(PETSC_NULL,"-matload_ignore_info",&flg);CHKERRQ(ierr); 141 if (flg) return 0; 142 143 ierr = ViewerBinaryGetInfoPointer(viewer,&file); CHKERRQ(ierr); 144 if (!file) return 0; 145 146 /* read rows of the file adding them to options database */ 147 while (fgets(string,128,file)) { 148 /* Comments are indicated by #, ! or % in the first column */ 149 if (string[0] == '#') continue; 150 if (string[0] == '!') continue; 151 if (string[0] == '%') continue; 152 first = PetscStrtok(string," "); 153 second = PetscStrtok(0," "); 154 if (first && first[0] == '-') { 155 if (second) {final = second;} else {final = first;} 156 len = PetscStrlen(final); 157 while (len > 0 && (final[len-1] == ' ' || final[len-1] == '\n')) { 158 len--; final[len] = 0; 159 } 160 ierr = OptionsSetValue(first,second); CHKERRQ(ierr); 161 } 162 } 163 return 0; 164 165 } 166