xref: /petsc/src/mat/impls/aij/mpi/aijsell/mpiaijsell.c (revision 3ba1676111f5c958fe6c2729b46ca4d523958bb3)
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
5ca9cdca7SRichard Tran Mills    from SEQAIJ but performs some operations in SELL format).  The same
611a5261eSBarry Smith    guidelines that apply to `MATMPIAIJ` matrices for preallocating the matrix
7ca9cdca7SRichard Tran Mills    storage apply here as well.
8ca9cdca7SRichard Tran Mills 
9d083f849SBarry Smith       Collective
10ca9cdca7SRichard Tran Mills 
11ca9cdca7SRichard Tran Mills    Input Parameters:
12ca9cdca7SRichard Tran Mills +  comm - MPI communicator
1311a5261eSBarry Smith .  m - number of local rows (or `PETSC_DECIDE` to have calculated if M is given)
14ca9cdca7SRichard Tran Mills            This value should be the same as the local size used in creating the
15ca9cdca7SRichard Tran Mills            y vector for the matrix-vector product y = Ax.
16ca9cdca7SRichard Tran Mills .  n - This value should be the same as the local size used in creating the
1711a5261eSBarry Smith        x vector for the matrix-vector product y = Ax. (or `PETSC_DECIDE` to have
18ca9cdca7SRichard Tran Mills        calculated if N is given) For square matrices n is almost always m.
1911a5261eSBarry Smith .  M - number of global rows (or `PETSC_DETERMINE` to have calculated if m is given)
2011a5261eSBarry Smith .  N - number of global columns (or `PETSC_DETERMINE` to have calculated if n is given)
21ca9cdca7SRichard Tran Mills .  d_nz  - number of nonzeros per row in DIAGONAL portion of local submatrix
22ca9cdca7SRichard Tran Mills            (same value is used for all local rows)
23ca9cdca7SRichard Tran Mills .  d_nnz - array containing the number of nonzeros in the various rows of the
24ca9cdca7SRichard Tran Mills            DIAGONAL portion of the local submatrix (possibly different for each row)
25ca9cdca7SRichard Tran Mills            or NULL, if d_nz is used to specify the nonzero structure.
26ca9cdca7SRichard Tran Mills            The size of this array is equal to the number of local rows, i.e 'm'.
27ca9cdca7SRichard Tran Mills            For matrices you plan to factor you must leave room for the diagonal entry and
28ca9cdca7SRichard Tran Mills            put in the entry even if it is zero.
29ca9cdca7SRichard Tran Mills .  o_nz  - number of nonzeros per row in the OFF-DIAGONAL portion of local
30ca9cdca7SRichard Tran Mills            submatrix (same value is used for all local rows).
31ca9cdca7SRichard Tran Mills -  o_nnz - array containing the number of nonzeros in the various rows of the
32ca9cdca7SRichard Tran Mills            OFF-DIAGONAL portion of the local submatrix (possibly different for
33ca9cdca7SRichard Tran Mills            each row) or NULL, if o_nz is used to specify the nonzero
34ca9cdca7SRichard Tran Mills            structure. The size of this array is equal to the number
35ca9cdca7SRichard Tran Mills            of local rows, i.e 'm'.
36ca9cdca7SRichard Tran Mills 
37ca9cdca7SRichard Tran Mills    Output Parameter:
38ca9cdca7SRichard Tran Mills .  A - the matrix
39ca9cdca7SRichard Tran Mills 
40ca9cdca7SRichard Tran Mills    Notes:
41ca9cdca7SRichard Tran Mills    If the *_nnz parameter is given then the *_nz parameter is ignored
42ca9cdca7SRichard Tran Mills 
43ca9cdca7SRichard Tran Mills    m,n,M,N parameters specify the size of the matrix, and its partitioning across
44ca9cdca7SRichard Tran Mills    processors, while d_nz,d_nnz,o_nz,o_nnz parameters specify the approximate
45ca9cdca7SRichard Tran Mills    storage requirements for this matrix.
46ca9cdca7SRichard Tran Mills 
4711a5261eSBarry Smith    If `PETSC_DECIDE` or `PETSC_DETERMINE` is used for a particular argument on one
48ca9cdca7SRichard Tran Mills    processor than it must be used on all processors that share the object for
49ca9cdca7SRichard Tran Mills    that argument.
50ca9cdca7SRichard Tran Mills 
51ca9cdca7SRichard Tran Mills    The user MUST specify either the local or global matrix dimensions
52ca9cdca7SRichard Tran Mills    (possibly both).
53ca9cdca7SRichard Tran Mills 
54ca9cdca7SRichard Tran Mills    The parallel matrix is partitioned such that the first m0 rows belong to
55ca9cdca7SRichard Tran Mills    process 0, the next m1 rows belong to process 1, the next m2 rows belong
56ca9cdca7SRichard Tran Mills    to process 2 etc.. where m0,m1,m2... are the input parameter 'm'.
57ca9cdca7SRichard Tran Mills 
58ca9cdca7SRichard Tran Mills    The DIAGONAL portion of the local submatrix of a processor can be defined
59ca9cdca7SRichard Tran Mills    as the submatrix which is obtained by extraction the part corresponding
60ca9cdca7SRichard Tran Mills    to the rows r1-r2 and columns r1-r2 of the global matrix, where r1 is the
61ca9cdca7SRichard Tran Mills    first row that belongs to the processor, and r2 is the last row belonging
62ca9cdca7SRichard Tran Mills    to the this processor. This is a square mxm matrix. The remaining portion
63ca9cdca7SRichard Tran Mills    of the local submatrix (mxN) constitute the OFF-DIAGONAL portion.
64ca9cdca7SRichard Tran Mills 
65ca9cdca7SRichard Tran Mills    If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored.
66ca9cdca7SRichard Tran Mills 
67ca9cdca7SRichard Tran Mills    When calling this routine with a single process communicator, a matrix of
6811a5261eSBarry Smith    type `MATSEQAIJSELL` is returned.  If a matrix of type `MATMPIAIJSELL` is desired
69ca9cdca7SRichard Tran Mills    for this type of communicator, use the construction mechanism:
7011a5261eSBarry Smith      `MatCreate`(...,&A); `MatSetType`(A,MPIAIJSELL); `MatMPIAIJSetPreallocation`(A,...);
71ca9cdca7SRichard Tran Mills 
72ca9cdca7SRichard Tran Mills    Options Database Keys:
73ca9cdca7SRichard Tran Mills .  -mat_aijsell_eager_shadow - Construct shadow matrix upon matrix assembly; default is to take a "lazy" approach, performing this step the first time the matrix is applied
74ca9cdca7SRichard Tran Mills 
75ca9cdca7SRichard Tran Mills    Level: intermediate
76ca9cdca7SRichard Tran Mills 
7760161072SBarry Smith .seealso: [Sparse Matrix Creation](sec_matsparse), `MATSEQAIJSELL`, `MATMPIAIJSELL`, `MATAIJSELL`, `MatCreate()`, `MatCreateSeqAIJSELL()`, `MatSetValues()`
78ca9cdca7SRichard Tran Mills @*/
79d71ae5a4SJacob 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)
80d71ae5a4SJacob Faibussowitsch {
81ca9cdca7SRichard Tran Mills   PetscMPIInt size;
82ca9cdca7SRichard Tran Mills 
83ca9cdca7SRichard Tran Mills   PetscFunctionBegin;
849566063dSJacob Faibussowitsch   PetscCall(MatCreate(comm, A));
859566063dSJacob Faibussowitsch   PetscCall(MatSetSizes(*A, m, n, M, N));
869566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
87ca9cdca7SRichard Tran Mills   if (size > 1) {
889566063dSJacob Faibussowitsch     PetscCall(MatSetType(*A, MATMPIAIJSELL));
899566063dSJacob Faibussowitsch     PetscCall(MatMPIAIJSetPreallocation(*A, d_nz, d_nnz, o_nz, o_nnz));
90ca9cdca7SRichard Tran Mills   } else {
919566063dSJacob Faibussowitsch     PetscCall(MatSetType(*A, MATSEQAIJSELL));
929566063dSJacob Faibussowitsch     PetscCall(MatSeqAIJSetPreallocation(*A, d_nz, d_nnz));
93ca9cdca7SRichard Tran Mills   }
94*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
95ca9cdca7SRichard Tran Mills }
96ca9cdca7SRichard Tran Mills 
97ca9cdca7SRichard Tran Mills PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJSELL(Mat, MatType, MatReuse, Mat *);
98ca9cdca7SRichard Tran Mills 
99d71ae5a4SJacob Faibussowitsch PetscErrorCode MatMPIAIJSetPreallocation_MPIAIJSELL(Mat B, PetscInt d_nz, const PetscInt d_nnz[], PetscInt o_nz, const PetscInt o_nnz[])
100d71ae5a4SJacob Faibussowitsch {
101ca9cdca7SRichard Tran Mills   Mat_MPIAIJ *b = (Mat_MPIAIJ *)B->data;
102ca9cdca7SRichard Tran Mills 
103ca9cdca7SRichard Tran Mills   PetscFunctionBegin;
1049566063dSJacob Faibussowitsch   PetscCall(MatMPIAIJSetPreallocation_MPIAIJ(B, d_nz, d_nnz, o_nz, o_nnz));
1059566063dSJacob Faibussowitsch   PetscCall(MatConvert_SeqAIJ_SeqAIJSELL(b->A, MATSEQAIJSELL, MAT_INPLACE_MATRIX, &b->A));
1069566063dSJacob Faibussowitsch   PetscCall(MatConvert_SeqAIJ_SeqAIJSELL(b->B, MATSEQAIJSELL, MAT_INPLACE_MATRIX, &b->B));
107*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
108ca9cdca7SRichard Tran Mills }
109ca9cdca7SRichard Tran Mills 
110d71ae5a4SJacob Faibussowitsch PETSC_INTERN PetscErrorCode MatConvert_MPIAIJ_MPIAIJSELL(Mat A, MatType type, MatReuse reuse, Mat *newmat)
111d71ae5a4SJacob Faibussowitsch {
112ca9cdca7SRichard Tran Mills   Mat B = *newmat;
113ca9cdca7SRichard Tran Mills 
114ca9cdca7SRichard Tran Mills   PetscFunctionBegin;
11548a46eb9SPierre Jolivet   if (reuse == MAT_INITIAL_MATRIX) PetscCall(MatDuplicate(A, MAT_COPY_VALUES, &B));
116ca9cdca7SRichard Tran Mills 
1179566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)B, MATMPIAIJSELL));
1189566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMPIAIJSetPreallocation_C", MatMPIAIJSetPreallocation_MPIAIJSELL));
119ca9cdca7SRichard Tran Mills   *newmat = B;
120*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
121ca9cdca7SRichard Tran Mills }
122ca9cdca7SRichard Tran Mills 
123d71ae5a4SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode MatCreate_MPIAIJSELL(Mat A)
124d71ae5a4SJacob Faibussowitsch {
125ca9cdca7SRichard Tran Mills   PetscFunctionBegin;
1269566063dSJacob Faibussowitsch   PetscCall(MatSetType(A, MATMPIAIJ));
1279566063dSJacob Faibussowitsch   PetscCall(MatConvert_MPIAIJ_MPIAIJSELL(A, MATMPIAIJSELL, MAT_INPLACE_MATRIX, &A));
128*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
129ca9cdca7SRichard Tran Mills }
130ca9cdca7SRichard Tran Mills 
131ca9cdca7SRichard Tran Mills /*MC
132ca9cdca7SRichard Tran Mills    MATAIJSELL - MATAIJSELL = "AIJSELL" - A matrix type to be used for sparse matrices.
133ca9cdca7SRichard Tran Mills 
13411a5261eSBarry Smith    This matrix type is identical to `MATSEQAIJSELL` when constructed with a single process communicator,
13511a5261eSBarry Smith    and `MATMPIAIJSELL` otherwise.  As a result, for single process communicators,
13611a5261eSBarry Smith    MatSeqAIJSetPreallocation() is supported, and similarly `MatMPIAIJSetPreallocation()` is supported
137ca9cdca7SRichard Tran Mills    for communicators controlling multiple processes.  It is recommended that you call both of
138ca9cdca7SRichard Tran Mills    the above preallocation routines for simplicity.
139ca9cdca7SRichard Tran Mills 
140ca9cdca7SRichard Tran Mills    Options Database Keys:
14111a5261eSBarry Smith . -mat_type aijsell - sets the matrix type to `MATAIJSELL` during a call to `MatSetFromOptions()`
142ca9cdca7SRichard Tran Mills 
143ca9cdca7SRichard Tran Mills   Level: beginner
144ca9cdca7SRichard Tran Mills 
14511a5261eSBarry Smith .seealso: `MatCreateMPIAIJSELL()`, `MATSEQAIJSELL`, `MATMPIAIJSELL`, `MATSEQAIJ`, `MATMPIAIJ`, `MATSEQAIJPERM`, `MATMPIAIJPERM`, `MATSEQAIJMKL`, `MATMPIAIJMKL`
146ca9cdca7SRichard Tran Mills M*/
147