xref: /petsc/src/mat/impls/baij/mpi/baijmkl/mpibaijmkl.c (revision 503c0ea9b45bcfbcebbb1ea5341243bbc69f0bea)
1 #include <../src/mat/impls/baij/mpi/mpibaij.h>
2 
3 PETSC_INTERN PetscErrorCode MatConvert_SeqBAIJ_SeqBAIJMKL(Mat,MatType,MatReuse,Mat*);
4 
5 static PetscErrorCode  MatMPIBAIJSetPreallocation_MPIBAIJMKL(Mat B,PetscInt bs,PetscInt d_nz,const PetscInt *d_nnz,PetscInt o_nz,const PetscInt *o_nnz)
6 {
7   Mat_MPIBAIJ     *b = (Mat_MPIBAIJ*)B->data;
8 
9   PetscFunctionBegin;
10   PetscCall(MatMPIBAIJSetPreallocation_MPIBAIJ(B,bs,d_nz,d_nnz,o_nz,o_nnz));
11   PetscCall(MatConvert_SeqBAIJ_SeqBAIJMKL(b->A,MATSEQBAIJMKL,MAT_INPLACE_MATRIX,&b->A));
12   PetscCall(MatConvert_SeqBAIJ_SeqBAIJMKL(b->B,MATSEQBAIJMKL,MAT_INPLACE_MATRIX,&b->B));
13   PetscFunctionReturn(0);
14 }
15 
16 static PetscErrorCode MatConvert_MPIBAIJ_MPIBAIJMKL(Mat A,MatType type,MatReuse reuse,Mat *newmat)
17 {
18   Mat            B = *newmat;
19 
20   PetscFunctionBegin;
21   if (reuse == MAT_INITIAL_MATRIX) {
22     PetscCall(MatDuplicate(A,MAT_COPY_VALUES,&B));
23   }
24 
25   PetscCall(PetscObjectChangeTypeName((PetscObject) B, MATMPIBAIJMKL));
26   PetscCall(PetscObjectComposeFunction((PetscObject)B,"MatMPIBAIJSetPreallocation_C",MatMPIBAIJSetPreallocation_MPIBAIJMKL));
27   *newmat = B;
28   PetscFunctionReturn(0);
29 }
30 
31 /*@C
32    MatCreateBAIJMKL - Creates a sparse parallel matrix in block AIJ format
33    (block compressed row).
34    This type inherits from BAIJ and is largely identical, but uses sparse BLAS
35    routines from Intel MKL whenever possible.
36    MatMult, MatMultAdd, MatMultTranspose, and MatMultTransposeAdd
37    operations are currently supported.
38    If the installed version of MKL supports the "SpMV2" sparse
39    inspector-executor routines, then those are used by default.
40    Default PETSc kernels are used otherwise.
41    For good matrix assembly performance the user should preallocate the matrix
42    storage by setting the parameters d_nz (or d_nnz) and o_nz (or o_nnz).
43    By setting these parameters accurately, performance can be increased by more
44    than a factor of 50.
45 
46    Collective
47 
48    Input Parameters:
49 +  comm - MPI communicator
50 .  bs   - size of block, the blocks are ALWAYS square. One can use MatSetBlockSizes() to set a different row and column blocksize but the row
51           blocksize always defines the size of the blocks. The column blocksize sets the blocksize of the vectors obtained with MatCreateVecs()
52 .  m - number of local rows (or PETSC_DECIDE to have calculated if M is given)
53            This value should be the same as the local size used in creating the
54            y vector for the matrix-vector product y = Ax.
55 .  n - number of local columns (or PETSC_DECIDE to have calculated if N is given)
56            This value should be the same as the local size used in creating the
57            x vector for the matrix-vector product y = Ax.
58 .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
59 .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
60 .  d_nz  - number of nonzero blocks per block row in diagonal portion of local
61            submatrix  (same for all local rows)
62 .  d_nnz - array containing the number of nonzero blocks in the various block rows
63            of the in diagonal portion of the local (possibly different for each block
64            row) or NULL.  If you plan to factor the matrix you must leave room for the diagonal entry
65            and set it even if it is zero.
66 .  o_nz  - number of nonzero blocks per block row in the off-diagonal portion of local
67            submatrix (same for all local rows).
68 -  o_nnz - array containing the number of nonzero blocks in the various block rows of the
69            off-diagonal portion of the local submatrix (possibly different for
70            each block row) or NULL.
71 
72    Output Parameter:
73 .  A - the matrix
74 
75    Options Database Keys:
76 +   -mat_block_size - size of the blocks to use
77 -   -mat_use_hash_table <fact> - set hash table factor
78 
79    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
80    MatXXXXSetPreallocation() paradigm instead of this routine directly.
81    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
82 
83    Notes:
84    If the *_nnz parameter is given then the *_nz parameter is ignored
85 
86    A nonzero block is any block that as 1 or more nonzeros in it
87 
88    The user MUST specify either the local or global matrix dimensions
89    (possibly both).
90 
91    If PETSC_DECIDE or  PETSC_DETERMINE is used for a particular argument on one processor
92    than it must be used on all processors that share the object for that argument.
93 
94    Storage Information:
95    For a square global matrix we define each processor's diagonal portion
96    to be its local rows and the corresponding columns (a square submatrix);
97    each processor's off-diagonal portion encompasses the remainder of the
98    local matrix (a rectangular submatrix).
99 
100    The user can specify preallocated storage for the diagonal part of
101    the local submatrix with either d_nz or d_nnz (not both).  Set
102    d_nz=PETSC_DEFAULT and d_nnz=NULL for PETSc to control dynamic
103    memory allocation.  Likewise, specify preallocated storage for the
104    off-diagonal part of the local submatrix with o_nz or o_nnz (not both).
105 
106    Consider a processor that owns rows 3, 4 and 5 of a parallel matrix. In
107    the figure below we depict these three local rows and all columns (0-11).
108 
109 .vb
110            0 1 2 3 4 5 6 7 8 9 10 11
111           --------------------------
112    row 3  |o o o d d d o o o o  o  o
113    row 4  |o o o d d d o o o o  o  o
114    row 5  |o o o d d d o o o o  o  o
115           --------------------------
116 .ve
117 
118    Thus, any entries in the d locations are stored in the d (diagonal)
119    submatrix, and any entries in the o locations are stored in the
120    o (off-diagonal) submatrix.  Note that the d and the o submatrices are
121    stored simply in the MATSEQBAIJMKL format for compressed row storage.
122 
123    Now d_nz should indicate the number of block nonzeros per row in the d matrix,
124    and o_nz should indicate the number of block nonzeros per row in the o matrix.
125    In general, for PDE problems in which most nonzeros are near the diagonal,
126    one expects d_nz >> o_nz.   For large problems you MUST preallocate memory
127    or you will get TERRIBLE performance; see the users' manual chapter on
128    matrices.
129 
130    Level: intermediate
131 
132 .seealso: MatCreate(), MatCreateSeqBAIJMKL(), MatSetValues(), MatCreateBAIJMKL(), MatMPIBAIJSetPreallocation(), MatMPIBAIJSetPreallocationCSR()
133 @*/
134 
135 PetscErrorCode  MatCreateBAIJMKL(MPI_Comm comm,PetscInt bs,PetscInt m,PetscInt n,PetscInt M,PetscInt N,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[],Mat *A)
136 {
137   PetscMPIInt    size;
138 
139   PetscFunctionBegin;
140   PetscCall(MatCreate(comm,A));
141   PetscCall(MatSetSizes(*A,m,n,M,N));
142   PetscCallMPI(MPI_Comm_size(comm,&size));
143   if (size > 1) {
144     PetscCall(MatSetType(*A,MATMPIBAIJMKL));
145     PetscCall(MatMPIBAIJSetPreallocation(*A,bs,d_nz,d_nnz,o_nz,o_nnz));
146   } else {
147     PetscCall(MatSetType(*A,MATSEQBAIJMKL));
148     PetscCall(MatSeqBAIJSetPreallocation(*A,bs,d_nz,d_nnz));
149   }
150   PetscFunctionReturn(0);
151 }
152 
153 PETSC_EXTERN PetscErrorCode MatCreate_MPIBAIJMKL(Mat A)
154 {
155   PetscFunctionBegin;
156   PetscCall(MatSetType(A,MATMPIBAIJ));
157   PetscCall(MatConvert_MPIBAIJ_MPIBAIJMKL(A,MATMPIBAIJMKL,MAT_INPLACE_MATRIX,&A));
158   PetscFunctionReturn(0);
159 }
160 
161 /*MC
162    MATBAIJMKL - MATBAIJMKL = "BAIJMKL" - A matrix type to be used for sparse matrices.
163 
164    This matrix type is identical to MATSEQBAIJMKL when constructed with a single process communicator,
165    and MATMPIBAIJMKL otherwise.  As a result, for single process communicators,
166   MatSeqBAIJSetPreallocation() is supported, and similarly MatMPIBAIJSetPreallocation() is supported
167   for communicators controlling multiple processes.  It is recommended that you call both of
168   the above preallocation routines for simplicity.
169 
170    Options Database Keys:
171 . -mat_type baijmkl - sets the matrix type to "BAIJMKL" during a call to MatSetFromOptions()
172 
173   Level: beginner
174 
175 .seealso: MatCreateBAIJMKL(), MATSEQBAIJMKL, MATMPIBAIJMKL
176 M*/
177