1 /*$Id: matio.c,v 1.72 2000/09/28 21:12:12 bsmith Exp bsmith $*/ 2 3 /* 4 This file contains simple binary read/write routines for matrices. 5 */ 6 7 #include "petsc.h" 8 #include "src/mat/matimpl.h" /*I "petscmat.h" I*/ 9 #include "petscsys.h" 10 PetscTruth MatLoadRegisterAllCalled = PETSC_FALSE; 11 FList MatLoadList = 0; 12 13 #undef __FUNC__ 14 #define __FUNC__ /*<a name=""></a>*/"MatLoadRegister" 15 /*@C 16 MatLoadRegister - Allows one to register a routine that reads matrices 17 from a binary file for a particular matrix type. 18 19 Not Collective 20 21 Input Parameters: 22 + type - the type of matrix (defined in include/petscmat.h), for example, MATSEQAIJ. 23 - loader - the function that reads the matrix from the binary file. 24 25 Level: developer 26 27 .seealso: MatLoadRegisterAll(), MatLoad() 28 29 @*/ 30 int MatLoadRegister(char *sname,char *path,char *name,int (*function)(Viewer,MatType,Mat*)) 31 { 32 int ierr; 33 char fullname[256]; 34 35 PetscFunctionBegin; 36 ierr = FListConcat(path,name,fullname);CHKERRQ(ierr); 37 ierr = FListAdd(&MatLoadList,sname,fullname,(int (*)(void*))function);CHKERRQ(ierr); 38 PetscFunctionReturn(0); 39 } 40 41 #undef __FUNC__ 42 #define __FUNC__ /*<a name=""></a>*/"MatLoadPrintHelp_Private" 43 static int MatLoadPrintHelp_Private(Mat A) 44 { 45 static PetscTruth called = PETSC_FALSE; 46 MPI_Comm comm = A->comm; 47 int ierr; 48 49 PetscFunctionBegin; 50 if (called) {PetscFunctionReturn(0);} else called = PETSC_TRUE; 51 ierr = (*PetscHelpPrintf)(comm," Options for MatLoad:\n");CHKERRQ(ierr); 52 ierr = (*PetscHelpPrintf)(comm," -matload_block_size <block_size> :Used for MATBAIJ, MATBDIAG\n");CHKERRQ(ierr); 53 ierr = (*PetscHelpPrintf)(comm," -matload_bdiag_diags <s1,s2,s3,...> : Used for MATBDIAG\n");CHKERRQ(ierr); 54 PetscFunctionReturn(0); 55 } 56 57 #undef __FUNC__ 58 #define __FUNC__ /*<a name=""></a>*/"MatLoad" 59 /*@C 60 MatLoad - Loads a matrix that has been stored in binary format 61 with MatView(). The matrix format is determined from the options database. 62 Generates a parallel MPI matrix if the communicator has more than one 63 processor. The default matrix type is AIJ. 64 65 Collective on Viewer 66 67 Input Parameters: 68 + viewer - binary file viewer, created with ViewerBinaryOpen() 69 - outtype - type of matrix desired, for example MATSEQAIJ, 70 MATMPIROWBS, etc. See types in petsc/include/petscmat.h. 71 72 Output Parameters: 73 . newmat - new matrix 74 75 Basic Options Database Keys: 76 + -matload_type seqaij - AIJ type 77 . -matload_type mpiaij - parallel AIJ type 78 . -matload_type seqbaij - block AIJ type 79 . -matload_type mpibaij - parallel block AIJ type 80 . -matload_type seqbdiag - block diagonal type 81 . -matload_type mpibdiag - parallel block diagonal type 82 . -matload_type mpirowbs - parallel rowbs type 83 . -matload_type seqdense - dense type 84 - -matload_type mpidense - parallel dense type 85 86 More Options Database Keys: 87 Used with block matrix formats (MATSEQBAIJ, MATMPIBDIAG, ...) to specify 88 block size 89 . -matload_block_size <bs> 90 91 Used to specify block diagonal numbers for MATSEQBDIAG and MATMPIBDIAG formats 92 . -matload_bdiag_diags <s1,s2,s3,...> 93 94 Level: beginner 95 96 Notes: 97 MatLoad() automatically loads into the options database any options 98 given in the file filename.info where filename is the name of the file 99 that was passed to the ViewerBinaryOpen(). The options in the info 100 file will be ignored if you use the -matload_ignore_info option. 101 102 In parallel, each processor can load a subset of rows (or the 103 entire matrix). This routine is especially useful when a large 104 matrix is stored on disk and only part of it existsis desired on each 105 processor. For example, a parallel solver may access only some of 106 the rows from each processor. The algorithm used here reads 107 relatively small blocks of data rather than reading the entire 108 matrix and then subsetting it. 109 110 Notes for advanced users: 111 Most users should not need to know the details of the binary storage 112 format, since MatLoad() and MatView() completely hide these details. 113 But for anyone who's interested, the standard binary matrix storage 114 format is 115 116 $ int MAT_COOKIE 117 $ int number of rows 118 $ int number of columns 119 $ int total number of nonzeros 120 $ int *number nonzeros in each row 121 $ int *column indices of all nonzeros (starting index is zero) 122 $ Scalar *values of all nonzeros 123 124 Note for Cray users, the int's stored in the binary file are 32 bit 125 integers; not 64 as they are represented in the memory, so if you 126 write your own routines to read/write these binary files from the Cray 127 you need to adjust the integer sizes that you read in, see 128 PetscReadBinary() and PetscWriteBinary() to see how this may be 129 done. 130 131 In addition, PETSc automatically does the byte swapping for 132 machines that store the bytes reversed, e.g. DEC alpha, freebsd, 133 linux, nt and the paragon; thus if you write your own binary 134 read/write routines you have to swap the bytes; see PetscReadBinary() 135 and PetscWriteBinary() to see how this may be done. 136 137 .keywords: matrix, load, binary, input 138 139 .seealso: ViewerBinaryOpen(), MatView(), VecLoad(), MatLoadRegister(), 140 MatLoadRegisterAll() 141 142 @*/ 143 int MatLoad(Viewer viewer,MatType outtype,Mat *newmat) 144 { 145 int ierr; 146 PetscTruth isbinary,flg; 147 MPI_Comm comm; 148 int (*r)(Viewer,MatType,Mat*); 149 char mtype[256]; 150 151 PetscFunctionBegin; 152 PetscValidHeaderSpecific(viewer,VIEWER_COOKIE); 153 *newmat = 0; 154 155 if (!MatLoadRegisterAllCalled) { 156 ierr = MatLoadRegisterAll(PETSC_NULL);CHKERRQ(ierr); 157 } 158 159 ierr = PetscTypeCompare((PetscObject)viewer,BINARY_VIEWER,&isbinary);CHKERRQ(ierr); 160 if (!isbinary) { 161 SETERRQ(PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with ViewerBinaryOpen()"); 162 } 163 164 ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr); 165 ierr = OptionsGetString(PETSC_NULL,"-matload_type",mtype,256,&flg);CHKERRQ(ierr); 166 if (flg) { 167 outtype = mtype; 168 } 169 if (!outtype) outtype = MATMPIAIJ; 170 ierr = FListFind(comm,MatLoadList,outtype,(int(**)(void*))&r);CHKERRQ(ierr); 171 if (!r) SETERRQ1(1,"Unknown Mat type given: %s",outtype); 172 173 ierr = PLogEventBegin(MAT_Load,viewer,0,0,0);CHKERRQ(ierr); 174 ierr = (*r)(viewer,outtype,newmat);CHKERRQ(ierr); 175 ierr = PLogEventEnd(MAT_Load,viewer,0,0,0);CHKERRQ(ierr); 176 177 ierr = OptionsHasName(PETSC_NULL,"-help",&flg);CHKERRQ(ierr); 178 if (flg) {ierr = MatLoadPrintHelp_Private(*newmat);CHKERRQ(ierr); } 179 PetscFunctionReturn(0); 180 } 181 182