xref: /petsc/src/mat/impls/aij/mpi/aijmkl/mpiaijmkl.c (revision c74fe4ad55794f2229f685f3bf7ba9fdc9763619)
1a84739b8SRichard Tran Mills #include <../src/mat/impls/aij/mpi/mpiaij.h>
2a84739b8SRichard Tran Mills /*@C
3a84739b8SRichard Tran Mills   MatCreateMPIAIJMKL - Creates a sparse parallel matrix whose local
411a5261eSBarry Smith   portions are stored as `MATSEQAIJMKL` matrices (a matrix class that inherits
520f4b53cSBarry Smith   from `MATSEQAIJ` but uses some operations provided by Intel MKL).
6a84739b8SRichard Tran Mills 
7d083f849SBarry Smith   Collective
8a84739b8SRichard Tran Mills 
9a84739b8SRichard Tran Mills   Input Parameters:
10a84739b8SRichard Tran Mills + comm  - MPI communicator
112ef1f0ffSBarry Smith . m     - number of local rows (or `PETSC_DECIDE` to have calculated if `M` is given)
12a84739b8SRichard Tran Mills            This value should be the same as the local size used in creating the
13a84739b8SRichard Tran Mills            y vector for the matrix-vector product y = Ax.
14a84739b8SRichard 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)
19a84739b8SRichard Tran Mills . d_nz  - number of nonzeros per row in DIAGONAL portion of local submatrix
20a84739b8SRichard Tran Mills            (same value is used for all local rows)
21a84739b8SRichard Tran Mills . d_nnz - array containing the number of nonzeros in the various rows of the
22a84739b8SRichard 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`.
25a84739b8SRichard Tran Mills            For matrices you plan to factor you must leave room for the diagonal entry and
26a84739b8SRichard Tran Mills            put in the entry even if it is zero.
27a84739b8SRichard Tran Mills . o_nz  - number of nonzeros per row in the OFF-DIAGONAL portion of local
28a84739b8SRichard Tran Mills            submatrix (same value is used for all local rows).
29a84739b8SRichard Tran Mills - o_nnz - array containing the number of nonzeros in the various rows of the
30a84739b8SRichard 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
32a84739b8SRichard Tran Mills            structure. The size of this array is equal to the number
332ef1f0ffSBarry Smith            of local rows, i.e `m`.
34a84739b8SRichard Tran Mills 
35a84739b8SRichard Tran Mills   Output Parameter:
36a84739b8SRichard Tran Mills . A - the matrix
37a84739b8SRichard Tran Mills 
382ef1f0ffSBarry Smith   Options Database Key:
392ef1f0ffSBarry Smith . -mat_aijmkl_no_spmv2 - disables use of the SpMV2 inspector-executor routines
402ef1f0ffSBarry Smith 
412ef1f0ffSBarry Smith   Level: intermediate
422ef1f0ffSBarry Smith 
43a84739b8SRichard Tran Mills   Notes:
44a84739b8SRichard Tran Mills   If the *_nnz parameter is given then the *_nz parameter is ignored
45a84739b8SRichard Tran Mills 
462ef1f0ffSBarry Smith   `m`,`n`,`M`,`N` parameters specify the size of the matrix, and its partitioning across
472ef1f0ffSBarry Smith   processors, while `d_nz`,`d_nnz`,`o_nz`,`o_nnz` parameters specify the approximate
48a84739b8SRichard Tran Mills   storage requirements for this matrix.
49a84739b8SRichard Tran Mills 
5011a5261eSBarry Smith   If `PETSC_DECIDE` or `PETSC_DETERMINE` is used for a particular argument on one
51a84739b8SRichard Tran Mills   processor than it must be used on all processors that share the object for
52a84739b8SRichard Tran Mills   that argument.
53a84739b8SRichard Tran Mills 
54a84739b8SRichard Tran Mills   The user MUST specify either the local or global matrix dimensions
55a84739b8SRichard Tran Mills   (possibly both).
56a84739b8SRichard Tran Mills 
57*727bdf9bSBarry Smith   If `m` and `n` are not `PETSC_DECIDE`, then the values determine the `PetscLayout` of the matrix and the ranges returned by
58*727bdf9bSBarry Smith   `MatGetOwnershipRange()`, `MatGetOwnershipRanges()`, `MatGetOwnershipRangeColumn()`, and `MatGetOwnershipRangesColumn()`.
59*727bdf9bSBarry Smith 
60*727bdf9bSBarry Smith   The parallel matrix is partitioned such that the first `m0` rows belong to
61*727bdf9bSBarry Smith   process 0, the next `m1` rows belong to process 1, the next `m2` rows belong
62*727bdf9bSBarry Smith   to process 2, etc., where `m0`, `m1`, `m2`... are the input parameter `m` on each MPI process.
63a84739b8SRichard Tran Mills 
64a84739b8SRichard Tran Mills   The DIAGONAL portion of the local submatrix of a processor can be defined
65a84739b8SRichard Tran Mills   as the submatrix which is obtained by extraction the part corresponding
66*727bdf9bSBarry Smith   to the rows `r1` - `r2` and columns `r1` - `r2` of the global matrix, where `r1` is the
67*727bdf9bSBarry Smith   first row that belongs to the processor, and `r2` is the last row belonging
68a84739b8SRichard Tran Mills   to the this processor. This is a square mxm matrix. The remaining portion
69a84739b8SRichard Tran Mills   of the local submatrix (mxN) constitute the OFF-DIAGONAL portion.
70a84739b8SRichard Tran Mills 
712ef1f0ffSBarry Smith   If `o_nnz`, `d_nnz` are specified, then `o_nz`, and `d_nz` are ignored.
72a84739b8SRichard Tran Mills 
73a84739b8SRichard Tran Mills   When calling this routine with a single process communicator, a matrix of
7411a5261eSBarry Smith   type `MATSEQAIJMKL` is returned.  If a matrix of type `MATMPIAIJMKL` is desired
7520f4b53cSBarry Smith   for this type of communicator, use the construction mechanism
7620f4b53cSBarry Smith .vb
7720f4b53cSBarry Smith   MatCreate(...,&A);
7820f4b53cSBarry Smith   MatSetType(A,MPIAIJMKL);
7920f4b53cSBarry Smith   MatMPIAIJSetPreallocation(A,...);
8020f4b53cSBarry Smith .ve
81a84739b8SRichard Tran Mills 
82*727bdf9bSBarry Smith .seealso: [](ch_matrices), `Mat`, [Sparse Matrix Creation](sec_matsparse), `MATMPIAIJMKL`, `MatCreate()`, `MatCreateSeqAIJMKL()`,
83*727bdf9bSBarry Smith           `MatSetValues()`, `MatGetOwnershipRange()`, `MatGetOwnershipRanges()`, `MatGetOwnershipRangeColumn()`,
84*727bdf9bSBarry Smith           `MatGetOwnershipRangesColumn()`, `PetscLayout`
85a84739b8SRichard Tran Mills @*/
MatCreateMPIAIJMKL(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)86d71ae5a4SJacob Faibussowitsch PetscErrorCode MatCreateMPIAIJMKL(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)
87d71ae5a4SJacob Faibussowitsch {
88a84739b8SRichard Tran Mills   PetscMPIInt size;
89a84739b8SRichard Tran Mills 
90a84739b8SRichard Tran Mills   PetscFunctionBegin;
919566063dSJacob Faibussowitsch   PetscCall(MatCreate(comm, A));
929566063dSJacob Faibussowitsch   PetscCall(MatSetSizes(*A, m, n, M, N));
939566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
94a84739b8SRichard Tran Mills   if (size > 1) {
959566063dSJacob Faibussowitsch     PetscCall(MatSetType(*A, MATMPIAIJMKL));
969566063dSJacob Faibussowitsch     PetscCall(MatMPIAIJSetPreallocation(*A, d_nz, d_nnz, o_nz, o_nnz));
97a84739b8SRichard Tran Mills   } else {
989566063dSJacob Faibussowitsch     PetscCall(MatSetType(*A, MATSEQAIJMKL));
999566063dSJacob Faibussowitsch     PetscCall(MatSeqAIJSetPreallocation(*A, d_nz, d_nnz));
100a84739b8SRichard Tran Mills   }
1013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
102a84739b8SRichard Tran Mills }
103a84739b8SRichard Tran Mills 
104a84739b8SRichard Tran Mills PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJMKL(Mat, MatType, MatReuse, Mat *);
105a84739b8SRichard Tran Mills 
MatMPIAIJSetPreallocation_MPIAIJMKL(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[])10666976f2fSJacob Faibussowitsch static PetscErrorCode MatMPIAIJSetPreallocation_MPIAIJMKL(Mat B, PetscInt d_nz, const PetscInt d_nnz[], PetscInt o_nz, const PetscInt o_nnz[])
107d71ae5a4SJacob Faibussowitsch {
108a84739b8SRichard Tran Mills   Mat_MPIAIJ *b = (Mat_MPIAIJ *)B->data;
109a84739b8SRichard Tran Mills 
110a84739b8SRichard Tran Mills   PetscFunctionBegin;
1119566063dSJacob Faibussowitsch   PetscCall(MatMPIAIJSetPreallocation_MPIAIJ(B, d_nz, d_nnz, o_nz, o_nnz));
1129566063dSJacob Faibussowitsch   PetscCall(MatConvert_SeqAIJ_SeqAIJMKL(b->A, MATSEQAIJMKL, MAT_INPLACE_MATRIX, &b->A));
1139566063dSJacob Faibussowitsch   PetscCall(MatConvert_SeqAIJ_SeqAIJMKL(b->B, MATSEQAIJMKL, MAT_INPLACE_MATRIX, &b->B));
1143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
115a84739b8SRichard Tran Mills }
116a84739b8SRichard Tran Mills 
MatConvert_MPIAIJ_MPIAIJMKL(Mat A,MatType type,MatReuse reuse,Mat * newmat)117d71ae5a4SJacob Faibussowitsch PETSC_INTERN PetscErrorCode MatConvert_MPIAIJ_MPIAIJMKL(Mat A, MatType type, MatReuse reuse, Mat *newmat)
118d71ae5a4SJacob Faibussowitsch {
119a84739b8SRichard Tran Mills   Mat B = *newmat;
120a84739b8SRichard Tran Mills 
121a84739b8SRichard Tran Mills   PetscFunctionBegin;
1229566063dSJacob Faibussowitsch   if (reuse == MAT_INITIAL_MATRIX) PetscCall(MatDuplicate(A, MAT_COPY_VALUES, &B));
1239566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)B, MATMPIAIJMKL));
1249566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMPIAIJSetPreallocation_C", MatMPIAIJSetPreallocation_MPIAIJMKL));
125a84739b8SRichard Tran Mills   *newmat = B;
1263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
127a84739b8SRichard Tran Mills }
128a84739b8SRichard Tran Mills 
MatCreate_MPIAIJMKL(Mat A)129d71ae5a4SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode MatCreate_MPIAIJMKL(Mat A)
130d71ae5a4SJacob Faibussowitsch {
131a84739b8SRichard Tran Mills   PetscFunctionBegin;
1329566063dSJacob Faibussowitsch   PetscCall(MatSetType(A, MATMPIAIJ));
1339566063dSJacob Faibussowitsch   PetscCall(MatConvert_MPIAIJ_MPIAIJMKL(A, MATMPIAIJMKL, MAT_INPLACE_MATRIX, &A));
1343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
135a84739b8SRichard Tran Mills }
136a84739b8SRichard Tran Mills 
137a84739b8SRichard Tran Mills /*MC
138a84739b8SRichard Tran Mills    MATAIJMKL - MATAIJMKL = "AIJMKL" - A matrix type to be used for sparse matrices.
139a84739b8SRichard Tran Mills 
14011a5261eSBarry Smith    This matrix type is identical to `MATSEQAIJMKL` when constructed with a single process communicator,
14111a5261eSBarry Smith    and `MATMPIAIJMKL` otherwise.  As a result, for single process communicators,
14211a5261eSBarry Smith   MatSeqAIJSetPreallocation() is supported, and similarly `MatMPIAIJSetPreallocation()` is supported
143a84739b8SRichard Tran Mills   for communicators controlling multiple processes.  It is recommended that you call both of
144a84739b8SRichard Tran Mills   the above preallocation routines for simplicity.
145a84739b8SRichard Tran Mills 
1462ef1f0ffSBarry Smith    Options Database Key:
14711a5261eSBarry Smith . -mat_type aijmkl - sets the matrix type to `MATAIJMKL` during a call to `MatSetFromOptions()`
148a84739b8SRichard Tran Mills 
149a84739b8SRichard Tran Mills   Level: beginner
150a84739b8SRichard Tran Mills 
1511cc06b55SBarry Smith .seealso: [](ch_matrices), `Mat`, `MATMPIAIJMKL`, `MATSEQAIJMKL`, `MatCreateMPIAIJMKL()`, `MATSEQAIJMKL`, `MATMPIAIJMKL`, `MATSEQAIJSELL`, `MATMPIAIJSELL`, `MATSEQAIJPERM`, `MATMPIAIJPERM`
152a84739b8SRichard Tran Mills M*/
153