xref: /petsc/src/mat/impls/aij/mpi/aijperm/mpiaijperm.c (revision 1cc06b555e92f8ec64db10330b8bbd830e5bc876)
1938d9b04SRichard Tran Mills 
2938d9b04SRichard Tran Mills #include <../src/mat/impls/aij/mpi/mpiaij.h>
3938d9b04SRichard Tran Mills /*@C
4938d9b04SRichard Tran Mills    MatCreateMPIAIJPERM - Creates a sparse parallel matrix whose local
511a5261eSBarry Smith    portions are stored as `MATSEQAIJPERM` matrices (a matrix class that inherits
6938d9b04SRichard Tran Mills    from SEQAIJ but includes some optimizations to allow more effective
720f4b53cSBarry Smith    vectorization).
8938d9b04SRichard Tran Mills 
9d083f849SBarry Smith       Collective
10938d9b04SRichard Tran Mills 
11938d9b04SRichard Tran Mills    Input Parameters:
12938d9b04SRichard Tran Mills +  comm - MPI communicator
132ef1f0ffSBarry Smith .  m - number of local rows (or `PETSC_DECIDE` to have calculated if `M` is given)
14938d9b04SRichard Tran Mills            This value should be the same as the local size used in creating the
15938d9b04SRichard Tran Mills            y vector for the matrix-vector product y = Ax.
16938d9b04SRichard Tran Mills .  n - This value should be the same as the local size used in creating the
17938d9b04SRichard Tran Mills        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
182ef1f0ffSBarry Smith        calculated if `N` is given) For square matrices `n` is almost always `m`.
192ef1f0ffSBarry Smith .  M - number of global rows (or `PETSC_DETERMINE` to have calculated if `m` is given)
202ef1f0ffSBarry Smith .  N - number of global columns (or `PETSC_DETERMINE` to have calculated if `n` is given)
21938d9b04SRichard Tran Mills .  d_nz  - number of nonzeros per row in DIAGONAL portion of local submatrix
22938d9b04SRichard Tran Mills            (same value is used for all local rows)
23938d9b04SRichard Tran Mills .  d_nnz - array containing the number of nonzeros in the various rows of the
24938d9b04SRichard Tran Mills            DIAGONAL portion of the local submatrix (possibly different for each row)
252ef1f0ffSBarry Smith            or `NULL`, if `d_nz` is used to specify the nonzero structure.
262ef1f0ffSBarry Smith            The size of this array is equal to the number of local rows, i.e `m`.
27938d9b04SRichard Tran Mills            For matrices you plan to factor you must leave room for the diagonal entry and
28938d9b04SRichard Tran Mills            put in the entry even if it is zero.
29938d9b04SRichard Tran Mills .  o_nz  - number of nonzeros per row in the OFF-DIAGONAL portion of local
30938d9b04SRichard Tran Mills            submatrix (same value is used for all local rows).
31938d9b04SRichard Tran Mills -  o_nnz - array containing the number of nonzeros in the various rows of the
32938d9b04SRichard Tran Mills            OFF-DIAGONAL portion of the local submatrix (possibly different for
332ef1f0ffSBarry Smith            each row) or `NULL`, if `o_nz` is used to specify the nonzero
34938d9b04SRichard Tran Mills            structure. The size of this array is equal to the number
352ef1f0ffSBarry Smith            of local rows, i.e `m`.
36938d9b04SRichard Tran Mills 
37938d9b04SRichard Tran Mills    Output Parameter:
38938d9b04SRichard Tran Mills .  A - the matrix
39938d9b04SRichard Tran Mills 
4027430b45SBarry Smith    Options Database Keys:
4127430b45SBarry Smith +  -mat_no_inode  - Do not use inodes
4227430b45SBarry Smith -  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
4327430b45SBarry Smith 
4427430b45SBarry Smith    Level: intermediate
4527430b45SBarry Smith 
46938d9b04SRichard Tran Mills    Notes:
47938d9b04SRichard Tran Mills    If the *_nnz parameter is given then the *_nz parameter is ignored
48938d9b04SRichard Tran Mills 
492ef1f0ffSBarry Smith    `m`,`n`,`M`,`N` parameters specify the size of the matrix, and its partitioning across
502ef1f0ffSBarry Smith    processors, while `d_nz`,`d_nnz`,`o_nz`,`o_nnz` parameters specify the approximate
51938d9b04SRichard Tran Mills    storage requirements for this matrix.
52938d9b04SRichard Tran Mills 
5311a5261eSBarry Smith    If `PETSC_DECIDE` or  `PETSC_DETERMINE` is used for a particular argument on one
54938d9b04SRichard Tran Mills    processor than it must be used on all processors that share the object for
55938d9b04SRichard Tran Mills    that argument.
56938d9b04SRichard Tran Mills 
57938d9b04SRichard Tran Mills    The user MUST specify either the local or global matrix dimensions
58938d9b04SRichard Tran Mills    (possibly both).
59938d9b04SRichard Tran Mills 
60938d9b04SRichard Tran Mills    The parallel matrix is partitioned such that the first m0 rows belong to
61938d9b04SRichard Tran Mills    process 0, the next m1 rows belong to process 1, the next m2 rows belong
622ef1f0ffSBarry Smith    to process 2 etc.. where m0,m1,m2... are the input parameter `m`.
63938d9b04SRichard Tran Mills 
64938d9b04SRichard Tran Mills    The DIAGONAL portion of the local submatrix of a processor can be defined
65938d9b04SRichard Tran Mills    as the submatrix which is obtained by extraction the part corresponding
66938d9b04SRichard Tran Mills    to the rows r1-r2 and columns r1-r2 of the global matrix, where r1 is the
67938d9b04SRichard Tran Mills    first row that belongs to the processor, and r2 is the last row belonging
68938d9b04SRichard Tran Mills    to the this processor. This is a square mxm matrix. The remaining portion
69938d9b04SRichard Tran Mills    of the local submatrix (mxN) constitute the OFF-DIAGONAL portion.
70938d9b04SRichard Tran Mills 
712ef1f0ffSBarry Smith    If `o_nnz`, `d_nnz` are specified, then `o_nz`, and `d_nz` are ignored.
72938d9b04SRichard Tran Mills 
73938d9b04SRichard Tran Mills    When calling this routine with a single process communicator, a matrix of
7411a5261eSBarry Smith    type `MATSEQAIJPERM` is returned.  If a matrix of type `MATMPIAIJPERM` is desired
7527430b45SBarry Smith    for this type of communicator, use the construction mechanism
7627430b45SBarry Smith .vb
7727430b45SBarry Smith    MatCreate(...,&A);
7827430b45SBarry Smith    MatSetType(A,MPIAIJ);
7927430b45SBarry Smith    MatMPIAIJSetPreallocation(A,...);
8027430b45SBarry Smith .ve
81938d9b04SRichard Tran Mills 
82938d9b04SRichard Tran Mills    By default, this format uses inodes (identical nodes) when possible.
83938d9b04SRichard Tran Mills    We search for consecutive rows with the same nonzero structure, thereby
84938d9b04SRichard Tran Mills    reusing matrix information to achieve increased efficiency.
85938d9b04SRichard Tran Mills 
86*1cc06b55SBarry Smith .seealso: [](ch_matrices), `Mat`, [Sparse Matrix Creation](sec_matsparse), `MATMPIAIJPERM`, `MatCreate()`, `MatCreateSeqAIJPERM()`, `MatSetValues()`
87938d9b04SRichard Tran Mills @*/
88d71ae5a4SJacob Faibussowitsch PetscErrorCode MatCreateMPIAIJPERM(MPI_Comm comm, PetscInt m, PetscInt n, PetscInt M, PetscInt N, PetscInt d_nz, const PetscInt d_nnz[], PetscInt o_nz, const PetscInt o_nnz[], Mat *A)
89d71ae5a4SJacob Faibussowitsch {
90938d9b04SRichard Tran Mills   PetscMPIInt size;
91938d9b04SRichard Tran Mills 
92938d9b04SRichard Tran Mills   PetscFunctionBegin;
939566063dSJacob Faibussowitsch   PetscCall(MatCreate(comm, A));
949566063dSJacob Faibussowitsch   PetscCall(MatSetSizes(*A, m, n, M, N));
959566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
96938d9b04SRichard Tran Mills   if (size > 1) {
979566063dSJacob Faibussowitsch     PetscCall(MatSetType(*A, MATMPIAIJPERM));
989566063dSJacob Faibussowitsch     PetscCall(MatMPIAIJSetPreallocation(*A, d_nz, d_nnz, o_nz, o_nnz));
99938d9b04SRichard Tran Mills   } else {
1009566063dSJacob Faibussowitsch     PetscCall(MatSetType(*A, MATSEQAIJPERM));
1019566063dSJacob Faibussowitsch     PetscCall(MatSeqAIJSetPreallocation(*A, d_nz, d_nnz));
102938d9b04SRichard Tran Mills   }
1033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
104938d9b04SRichard Tran Mills }
105938d9b04SRichard Tran Mills 
106d71ae5a4SJacob Faibussowitsch PetscErrorCode MatMPIAIJSetPreallocation_MPIAIJPERM(Mat B, PetscInt d_nz, const PetscInt d_nnz[], PetscInt o_nz, const PetscInt o_nnz[])
107d71ae5a4SJacob Faibussowitsch {
108938d9b04SRichard Tran Mills   Mat_MPIAIJ *b = (Mat_MPIAIJ *)B->data;
109938d9b04SRichard Tran Mills 
110938d9b04SRichard Tran Mills   PetscFunctionBegin;
1119566063dSJacob Faibussowitsch   PetscCall(MatMPIAIJSetPreallocation_MPIAIJ(B, d_nz, d_nnz, o_nz, o_nnz));
1129566063dSJacob Faibussowitsch   PetscCall(MatConvert_SeqAIJ_SeqAIJPERM(b->A, MATSEQAIJPERM, MAT_INPLACE_MATRIX, &b->A));
1139566063dSJacob Faibussowitsch   PetscCall(MatConvert_SeqAIJ_SeqAIJPERM(b->B, MATSEQAIJPERM, MAT_INPLACE_MATRIX, &b->B));
1143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
115938d9b04SRichard Tran Mills }
116938d9b04SRichard Tran Mills 
117d71ae5a4SJacob Faibussowitsch PETSC_INTERN PetscErrorCode MatConvert_MPIAIJ_MPIAIJPERM(Mat A, MatType type, MatReuse reuse, Mat *newmat)
118d71ae5a4SJacob Faibussowitsch {
119938d9b04SRichard Tran Mills   Mat B = *newmat;
120938d9b04SRichard Tran Mills 
121938d9b04SRichard Tran Mills   PetscFunctionBegin;
12248a46eb9SPierre Jolivet   if (reuse == MAT_INITIAL_MATRIX) PetscCall(MatDuplicate(A, MAT_COPY_VALUES, &B));
123938d9b04SRichard Tran Mills 
1249566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)B, MATMPIAIJPERM));
1259566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMPIAIJSetPreallocation_C", MatMPIAIJSetPreallocation_MPIAIJPERM));
126938d9b04SRichard Tran Mills   *newmat = B;
1273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
128938d9b04SRichard Tran Mills }
129938d9b04SRichard Tran Mills 
130d71ae5a4SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode MatCreate_MPIAIJPERM(Mat A)
131d71ae5a4SJacob Faibussowitsch {
132938d9b04SRichard Tran Mills   PetscFunctionBegin;
1339566063dSJacob Faibussowitsch   PetscCall(MatSetType(A, MATMPIAIJ));
1349566063dSJacob Faibussowitsch   PetscCall(MatConvert_MPIAIJ_MPIAIJPERM(A, MATMPIAIJPERM, MAT_INPLACE_MATRIX, &A));
1353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
136938d9b04SRichard Tran Mills }
137938d9b04SRichard Tran Mills 
138938d9b04SRichard Tran Mills /*MC
1392ef1f0ffSBarry Smith    MATAIJPERM - "AIJPERM" - A matrix type to be used for sparse matrices.
140938d9b04SRichard Tran Mills 
14111a5261eSBarry Smith    This matrix type is identical to `MATSEQAIJPERM` when constructed with a single process communicator,
14211a5261eSBarry Smith    and `MATMPIAIJPERM` otherwise.  As a result, for single process communicators,
14311a5261eSBarry Smith   `MatSeqAIJSetPreallocation()` is supported, and similarly `MatMPIAIJSetPreallocation()` is supported
144938d9b04SRichard Tran Mills   for communicators controlling multiple processes.  It is recommended that you call both of
145938d9b04SRichard Tran Mills   the above preallocation routines for simplicity.
146938d9b04SRichard Tran Mills 
14727430b45SBarry Smith    Options Database Key:
1482ef1f0ffSBarry Smith . -mat_type aijperm - sets the matrix type to `MATAIJPERM`
149938d9b04SRichard Tran Mills 
150938d9b04SRichard Tran Mills   Level: beginner
151938d9b04SRichard Tran Mills 
152*1cc06b55SBarry Smith .seealso: [](ch_matrices), `Mat`, `MatCreateMPIAIJPERM()`, `MATSEQAIJPERM`, `MATMPIAIJPERM`, `MATSEQAIJ`, `MATMPIAIJ`, `MATSEQAIJMKL`, `MATMPIAIJMKL`, `MATSEQAIJSELL`, `MATMPIAIJSELL`
153938d9b04SRichard Tran Mills M*/
154