xref: /petsc/src/mat/impls/aij/mpi/aijsell/mpiaijsell.c (revision 9c5460f9064ca60dd71a234a1f6faf93e7a6b0c9)
1ca9cdca7SRichard Tran Mills #include <../src/mat/impls/aij/mpi/mpiaij.h>
2ca9cdca7SRichard Tran Mills /*@C
3ca9cdca7SRichard Tran Mills   MatCreateMPIAIJSELL - Creates a sparse parallel matrix whose local
411a5261eSBarry Smith   portions are stored as `MATSEQAIJSELL` matrices (a matrix class that inherits
520f4b53cSBarry Smith   from SEQAIJ but performs some operations in SELL format).
6ca9cdca7SRichard Tran Mills 
7d083f849SBarry Smith   Collective
8ca9cdca7SRichard Tran Mills 
9ca9cdca7SRichard Tran Mills   Input Parameters:
10ca9cdca7SRichard Tran Mills + comm  - MPI communicator
112ef1f0ffSBarry Smith . m     - number of local rows (or `PETSC_DECIDE` to have calculated if `M` is given)
12ca9cdca7SRichard Tran Mills            This value should be the same as the local size used in creating the
13ca9cdca7SRichard Tran Mills            y vector for the matrix-vector product y = Ax.
14ca9cdca7SRichard Tran Mills . n     - This value should be the same as the local size used in creating the
1511a5261eSBarry Smith        x vector for the matrix-vector product y = Ax. (or `PETSC_DECIDE` to have
162ef1f0ffSBarry Smith        calculated if `N` is given) For square matrices `n` is almost always `m`.
172ef1f0ffSBarry Smith . M     - number of global rows (or `PETSC_DETERMINE` to have calculated if `m` is given)
182ef1f0ffSBarry Smith . N     - number of global columns (or `PETSC_DETERMINE` to have calculated if `n` is given)
19ca9cdca7SRichard Tran Mills . d_nz  - number of nonzeros per row in DIAGONAL portion of local submatrix
20ca9cdca7SRichard Tran Mills            (same value is used for all local rows)
21ca9cdca7SRichard Tran Mills . d_nnz - array containing the number of nonzeros in the various rows of the
22ca9cdca7SRichard Tran Mills            DIAGONAL portion of the local submatrix (possibly different for each row)
232ef1f0ffSBarry Smith            or `NULL`, if `d_nz` is used to specify the nonzero structure.
242ef1f0ffSBarry Smith            The size of this array is equal to the number of local rows, i.e `m`.
25ca9cdca7SRichard Tran Mills            For matrices you plan to factor you must leave room for the diagonal entry and
26ca9cdca7SRichard Tran Mills            put in the entry even if it is zero.
27ca9cdca7SRichard Tran Mills . o_nz  - number of nonzeros per row in the OFF-DIAGONAL portion of local
28ca9cdca7SRichard Tran Mills            submatrix (same value is used for all local rows).
29ca9cdca7SRichard Tran Mills - o_nnz - array containing the number of nonzeros in the various rows of the
30ca9cdca7SRichard Tran Mills            OFF-DIAGONAL portion of the local submatrix (possibly different for
312ef1f0ffSBarry Smith            each row) or `NULL`, if `o_nz` is used to specify the nonzero
32ca9cdca7SRichard Tran Mills            structure. The size of this array is equal to the number
332ef1f0ffSBarry Smith            of local rows, i.e `m`.
34ca9cdca7SRichard Tran Mills 
35ca9cdca7SRichard Tran Mills   Output Parameter:
36ca9cdca7SRichard Tran Mills . A - the matrix
37ca9cdca7SRichard Tran Mills 
382ef1f0ffSBarry Smith   Options Database Key:
392ef1f0ffSBarry Smith . -mat_aijsell_eager_shadow - Construct shadow matrix upon matrix assembly; default is to take a "lazy" approach, performing this step the first
402ef1f0ffSBarry Smith                                time the matrix is applied
412ef1f0ffSBarry Smith 
422ef1f0ffSBarry Smith   Level: intermediate
432ef1f0ffSBarry Smith 
44ca9cdca7SRichard Tran Mills   Notes:
45ca9cdca7SRichard Tran Mills   If the *_nnz parameter is given then the *_nz parameter is ignored
46ca9cdca7SRichard Tran Mills 
472ef1f0ffSBarry Smith   `m`,`n`,`M`,`N` parameters specify the size of the matrix, and its partitioning across
482ef1f0ffSBarry Smith   processors, while `d_nz`,`d_nnz`,`o_nz`,`o_nnz` parameters specify the approximate
49ca9cdca7SRichard Tran Mills   storage requirements for this matrix.
50ca9cdca7SRichard Tran Mills 
5111a5261eSBarry Smith   If `PETSC_DECIDE` or `PETSC_DETERMINE` is used for a particular argument on one
52ca9cdca7SRichard Tran Mills   processor than it must be used on all processors that share the object for
53ca9cdca7SRichard Tran Mills   that argument.
54ca9cdca7SRichard Tran Mills 
55ca9cdca7SRichard Tran Mills   The user MUST specify either the local or global matrix dimensions
56ca9cdca7SRichard Tran Mills   (possibly both).
57ca9cdca7SRichard Tran Mills 
58ca9cdca7SRichard Tran Mills   The parallel matrix is partitioned such that the first m0 rows belong to
59ca9cdca7SRichard Tran Mills   process 0, the next m1 rows belong to process 1, the next m2 rows belong
602ef1f0ffSBarry Smith   to process 2 etc.. where m0,m1,m2... are the input parameter `m`.
61ca9cdca7SRichard Tran Mills 
62ca9cdca7SRichard Tran Mills   The DIAGONAL portion of the local submatrix of a processor can be defined
63ca9cdca7SRichard Tran Mills   as the submatrix which is obtained by extraction the part corresponding
64ca9cdca7SRichard Tran Mills   to the rows r1-r2 and columns r1-r2 of the global matrix, where r1 is the
65ca9cdca7SRichard Tran Mills   first row that belongs to the processor, and r2 is the last row belonging
66ca9cdca7SRichard Tran Mills   to the this processor. This is a square mxm matrix. The remaining portion
67ca9cdca7SRichard Tran Mills   of the local submatrix (mxN) constitute the OFF-DIAGONAL portion.
68ca9cdca7SRichard Tran Mills 
692ef1f0ffSBarry Smith   If `o_nnz`, `d_nnz` are specified, then `o_nz`, and `d_nz` are ignored.
70ca9cdca7SRichard Tran Mills 
71ca9cdca7SRichard Tran Mills   When calling this routine with a single process communicator, a matrix of
7211a5261eSBarry Smith   type `MATSEQAIJSELL` is returned.  If a matrix of type `MATMPIAIJSELL` is desired
7320f4b53cSBarry Smith   for this type of communicator, use the construction mechanism
742ef1f0ffSBarry Smith .vb
752ef1f0ffSBarry Smith    MatCreate(...,&A);
762ef1f0ffSBarry Smith    MatSetType(A,MPIAIJSELL);
772ef1f0ffSBarry Smith    MatMPIAIJSetPreallocation(A,...);
782ef1f0ffSBarry Smith .ve
79ca9cdca7SRichard Tran Mills 
801cc06b55SBarry Smith .seealso: [](ch_matrices), `Mat`, [Sparse Matrix Creation](sec_matsparse), `MATSEQAIJSELL`, `MATMPIAIJSELL`, `MATAIJSELL`, `MatCreate()`, `MatCreateSeqAIJSELL()`, `MatSetValues()`
81ca9cdca7SRichard Tran Mills @*/
82d71ae5a4SJacob Faibussowitsch PetscErrorCode MatCreateMPIAIJSELL(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)
83d71ae5a4SJacob Faibussowitsch {
84ca9cdca7SRichard Tran Mills   PetscMPIInt size;
85ca9cdca7SRichard Tran Mills 
86ca9cdca7SRichard Tran Mills   PetscFunctionBegin;
879566063dSJacob Faibussowitsch   PetscCall(MatCreate(comm, A));
889566063dSJacob Faibussowitsch   PetscCall(MatSetSizes(*A, m, n, M, N));
899566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
90ca9cdca7SRichard Tran Mills   if (size > 1) {
919566063dSJacob Faibussowitsch     PetscCall(MatSetType(*A, MATMPIAIJSELL));
929566063dSJacob Faibussowitsch     PetscCall(MatMPIAIJSetPreallocation(*A, d_nz, d_nnz, o_nz, o_nnz));
93ca9cdca7SRichard Tran Mills   } else {
949566063dSJacob Faibussowitsch     PetscCall(MatSetType(*A, MATSEQAIJSELL));
959566063dSJacob Faibussowitsch     PetscCall(MatSeqAIJSetPreallocation(*A, d_nz, d_nnz));
96ca9cdca7SRichard Tran Mills   }
973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
98ca9cdca7SRichard Tran Mills }
99ca9cdca7SRichard Tran Mills 
100ca9cdca7SRichard Tran Mills PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJSELL(Mat, MatType, MatReuse, Mat *);
101ca9cdca7SRichard Tran Mills 
102*66976f2fSJacob Faibussowitsch static PetscErrorCode MatMPIAIJSetPreallocation_MPIAIJSELL(Mat B, PetscInt d_nz, const PetscInt d_nnz[], PetscInt o_nz, const PetscInt o_nnz[])
103d71ae5a4SJacob Faibussowitsch {
104ca9cdca7SRichard Tran Mills   Mat_MPIAIJ *b = (Mat_MPIAIJ *)B->data;
105ca9cdca7SRichard Tran Mills 
106ca9cdca7SRichard Tran Mills   PetscFunctionBegin;
1079566063dSJacob Faibussowitsch   PetscCall(MatMPIAIJSetPreallocation_MPIAIJ(B, d_nz, d_nnz, o_nz, o_nnz));
1089566063dSJacob Faibussowitsch   PetscCall(MatConvert_SeqAIJ_SeqAIJSELL(b->A, MATSEQAIJSELL, MAT_INPLACE_MATRIX, &b->A));
1099566063dSJacob Faibussowitsch   PetscCall(MatConvert_SeqAIJ_SeqAIJSELL(b->B, MATSEQAIJSELL, MAT_INPLACE_MATRIX, &b->B));
1103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
111ca9cdca7SRichard Tran Mills }
112ca9cdca7SRichard Tran Mills 
113d71ae5a4SJacob Faibussowitsch PETSC_INTERN PetscErrorCode MatConvert_MPIAIJ_MPIAIJSELL(Mat A, MatType type, MatReuse reuse, Mat *newmat)
114d71ae5a4SJacob Faibussowitsch {
115ca9cdca7SRichard Tran Mills   Mat B = *newmat;
116ca9cdca7SRichard Tran Mills 
117ca9cdca7SRichard Tran Mills   PetscFunctionBegin;
11848a46eb9SPierre Jolivet   if (reuse == MAT_INITIAL_MATRIX) PetscCall(MatDuplicate(A, MAT_COPY_VALUES, &B));
119ca9cdca7SRichard Tran Mills 
1209566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)B, MATMPIAIJSELL));
1219566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMPIAIJSetPreallocation_C", MatMPIAIJSetPreallocation_MPIAIJSELL));
122ca9cdca7SRichard Tran Mills   *newmat = B;
1233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
124ca9cdca7SRichard Tran Mills }
125ca9cdca7SRichard Tran Mills 
126d71ae5a4SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode MatCreate_MPIAIJSELL(Mat A)
127d71ae5a4SJacob Faibussowitsch {
128ca9cdca7SRichard Tran Mills   PetscFunctionBegin;
1299566063dSJacob Faibussowitsch   PetscCall(MatSetType(A, MATMPIAIJ));
1309566063dSJacob Faibussowitsch   PetscCall(MatConvert_MPIAIJ_MPIAIJSELL(A, MATMPIAIJSELL, MAT_INPLACE_MATRIX, &A));
1313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
132ca9cdca7SRichard Tran Mills }
133ca9cdca7SRichard Tran Mills 
134ca9cdca7SRichard Tran Mills /*MC
1352ef1f0ffSBarry Smith    MATAIJSELL - "AIJSELL" - A matrix type to be used for sparse matrices.
136ca9cdca7SRichard Tran Mills 
13711a5261eSBarry Smith    This matrix type is identical to `MATSEQAIJSELL` when constructed with a single process communicator,
13811a5261eSBarry Smith    and `MATMPIAIJSELL` otherwise.  As a result, for single process communicators,
13911a5261eSBarry Smith    MatSeqAIJSetPreallocation() is supported, and similarly `MatMPIAIJSetPreallocation()` is supported
140ca9cdca7SRichard Tran Mills    for communicators controlling multiple processes.  It is recommended that you call both of
141ca9cdca7SRichard Tran Mills    the above preallocation routines for simplicity.
142ca9cdca7SRichard Tran Mills 
1432ef1f0ffSBarry Smith    Options Database Key:
1442ef1f0ffSBarry Smith . -mat_type aijsell - sets the matrix type to `MATAIJSELL`
145ca9cdca7SRichard Tran Mills 
146ca9cdca7SRichard Tran Mills   Level: beginner
147ca9cdca7SRichard Tran Mills 
1481cc06b55SBarry Smith .seealso: [](ch_matrices), `Mat`, `MatCreateMPIAIJSELL()`, `MATSEQAIJSELL`, `MATMPIAIJSELL`, `MATSEQAIJ`, `MATMPIAIJ`, `MATSEQAIJPERM`, `MATMPIAIJPERM`, `MATSEQAIJMKL`, `MATMPIAIJMKL`
149ca9cdca7SRichard Tran Mills M*/
150