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