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