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