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
80: - -matload_symmetric - matrix in file is symmetric
82: More Options Database Keys:
83: Used with block matrix formats (MATSEQBAIJ, MATMPIBDIAG, ...) to specify
84: block size
85: . -matload_block_size <bs>
87: Used to specify block diagonal numbers for MATSEQBDIAG and MATMPIBDIAG formats
88: . -matload_bdiag_diags <s1,s2,s3,...>
90: Level: beginner
92: Notes:
93: MatLoad() automatically loads into the options database any options
94: given in the file filename.info where filename is the name of the file
95: that was passed to the PetscViewerBinaryOpen(). The options in the info
96: file will be ignored if you use the -matload_ignore_info option.
98: In parallel, each processor can load a subset of rows (or the
99: entire matrix). This routine is especially useful when a large
100: matrix is stored on disk and only part of it existsis desired on each
101: processor. For example, a parallel solver may access only some of
102: the rows from each processor. The algorithm used here reads
103: relatively small blocks of data rather than reading the entire
104: matrix and then subsetting it.
106: Notes for advanced users:
107: Most users should not need to know the details of the binary storage
108: format, since MatLoad() and MatView() completely hide these details.
109: But for anyone who's interested, the standard binary matrix storage
110: format is
112: $ int MAT_FILE_COOKIE
113: $ int number of rows
114: $ int number of columns
115: $ int total number of nonzeros
116: $ int *number nonzeros in each row
117: $ int *column indices of all nonzeros (starting index is zero)
118: $ PetscScalar *values of all nonzeros
120: Note for Cray users, the int's stored in the binary file are 32 bit
121: integers; not 64 as they are represented in the memory, so if you
122: write your own routines to read/write these binary files from the Cray
123: you need to adjust the integer sizes that you read in, see
124: PetscReadBinary() and PetscWriteBinary() to see how this may be
125: done.
127: In addition, PETSc automatically does the byte swapping for
128: machines that store the bytes reversed, e.g. DEC alpha, freebsd,
129: linux, nt and the paragon; thus if you write your own binary
130: read/write routines you have to swap the bytes; see PetscReadBinary()
131: and PetscWriteBinary() to see how this may be done.
133: .keywords: matrix, load, binary, input
135: .seealso: PetscViewerBinaryOpen(), MatView(), VecLoad(), MatLoadRegister(),
136: MatLoadRegisterAll()
138: @*/
139: int MatLoad(PetscViewer viewer,MatType outtype,Mat *newmat)
140: {
141: int ierr;
142: PetscTruth isbinary,flg;
143: MPI_Comm comm;
144: int (*r)(PetscViewer,MatType,Mat*);
145: char mtype[256];
149: *newmat = 0;
151: if (!MatLoadRegisterAllCalled) {
152: MatLoadRegisterAll(PETSC_NULL);
153: }
155: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);
156: if (!isbinary) {
157: SETERRQ(PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
158: }
160: PetscOptionsGetString(PETSC_NULL,"-mat_type",mtype,256,&flg);
161: if (flg) {
162: outtype = mtype;
163: }
164: PetscOptionsGetString(PETSC_NULL,"-matload_type",mtype,256,&flg);
165: if (flg) {
166: outtype = mtype;
167: }
168: PetscObjectGetComm((PetscObject)viewer,&comm);
169: if (!outtype) outtype = MATMPIAIJ;
170: PetscFListFind(comm,MatLoadList,outtype,(void(**)(void))&r);
171: if (!r) SETERRQ1(1,"Unknown Mat type given: %s",outtype);
173: PetscLogEventBegin(MAT_Load,viewer,0,0,0);
174: (*r)(viewer,outtype,newmat);
175: PetscLogEventEnd(MAT_Load,viewer,0,0,0);
177: PetscOptionsHasName(PETSC_NULL,"-matload_symmetric",&flg);
178: if (flg) {
179: MatSetOption(*newmat,MAT_SYMMETRIC);
180: }
181: PetscOptionsHasName(PETSC_NULL,"-help",&flg);
182: if (flg) {MatLoadPrintHelp_Private(*newmat); }
183: return(0);
184: }