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