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