xref: /petsc/src/mat/impls/aij/mpi/mpiviennacl/mpiaijviennacl.cxx (revision fe59aa6d68c880d4014a5813129926ee5b21e858)
199acd6aaSStefano Zampini #define PETSC_SKIP_IMMINTRIN_H_CUDAWORKAROUND 1
299acd6aaSStefano Zampini 
3aaa7dc30SBarry Smith #include <petscconf.h>
48f86e40fSKarl Rupp #include <../src/mat/impls/aij/mpi/mpiaij.h> /*I "petscmat.h" I*/
565e3cb35SKarl Rupp #include <../src/mat/impls/aij/seq/seqviennacl/viennaclmatimpl.h>
68f86e40fSKarl Rupp 
7d71ae5a4SJacob Faibussowitsch PetscErrorCode MatMPIAIJSetPreallocation_MPIAIJViennaCL(Mat B, PetscInt d_nz, const PetscInt d_nnz[], PetscInt o_nz, const PetscInt o_nnz[])
8d71ae5a4SJacob Faibussowitsch {
98f86e40fSKarl Rupp   Mat_MPIAIJ *b = (Mat_MPIAIJ *)B->data;
108f86e40fSKarl Rupp 
118f86e40fSKarl Rupp   PetscFunctionBegin;
129566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetUp(B->rmap));
139566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetUp(B->cmap));
148f86e40fSKarl Rupp   if (!B->preallocated) {
158f86e40fSKarl Rupp     /* Explicitly create the two MATSEQAIJVIENNACL matrices. */
169566063dSJacob Faibussowitsch     PetscCall(MatCreate(PETSC_COMM_SELF, &b->A));
179566063dSJacob Faibussowitsch     PetscCall(MatSetSizes(b->A, B->rmap->n, B->cmap->n, B->rmap->n, B->cmap->n));
189566063dSJacob Faibussowitsch     PetscCall(MatSetType(b->A, MATSEQAIJVIENNACL));
199566063dSJacob Faibussowitsch     PetscCall(MatCreate(PETSC_COMM_SELF, &b->B));
209566063dSJacob Faibussowitsch     PetscCall(MatSetSizes(b->B, B->rmap->n, B->cmap->N, B->rmap->n, B->cmap->N));
219566063dSJacob Faibussowitsch     PetscCall(MatSetType(b->B, MATSEQAIJVIENNACL));
228f86e40fSKarl Rupp   }
239566063dSJacob Faibussowitsch   PetscCall(MatSeqAIJSetPreallocation(b->A, d_nz, d_nnz));
249566063dSJacob Faibussowitsch   PetscCall(MatSeqAIJSetPreallocation(b->B, o_nz, o_nnz));
258f86e40fSKarl Rupp   B->preallocated = PETSC_TRUE;
263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
278f86e40fSKarl Rupp }
288f86e40fSKarl Rupp 
29d71ae5a4SJacob Faibussowitsch PetscErrorCode MatAssemblyEnd_MPIAIJViennaCL(Mat A, MatAssemblyType mode)
30d71ae5a4SJacob Faibussowitsch {
3174fd8ad3SBarry Smith   Mat_MPIAIJ *b = (Mat_MPIAIJ *)A->data;
3274fd8ad3SBarry Smith   PetscBool   v;
3374fd8ad3SBarry Smith 
3474fd8ad3SBarry Smith   PetscFunctionBegin;
359566063dSJacob Faibussowitsch   PetscCall(MatAssemblyEnd_MPIAIJ(A, mode));
369566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)b->lvec, VECSEQVIENNACL, &v));
3774fd8ad3SBarry Smith   if (!v) {
3874fd8ad3SBarry Smith     PetscInt m;
399566063dSJacob Faibussowitsch     PetscCall(VecGetSize(b->lvec, &m));
409566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&b->lvec));
419566063dSJacob Faibussowitsch     PetscCall(VecCreateSeqViennaCL(PETSC_COMM_SELF, m, &b->lvec));
4274fd8ad3SBarry Smith   }
433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4474fd8ad3SBarry Smith }
458f86e40fSKarl Rupp 
46d71ae5a4SJacob Faibussowitsch PetscErrorCode MatDestroy_MPIAIJViennaCL(Mat A)
47d71ae5a4SJacob Faibussowitsch {
488f86e40fSKarl Rupp   PetscFunctionBegin;
499566063dSJacob Faibussowitsch   PetscCall(MatDestroy_MPIAIJ(A));
503ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
518f86e40fSKarl Rupp }
528f86e40fSKarl Rupp 
53d71ae5a4SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode MatCreate_MPIAIJViennaCL(Mat A)
54d71ae5a4SJacob Faibussowitsch {
558f86e40fSKarl Rupp   PetscFunctionBegin;
569566063dSJacob Faibussowitsch   PetscCall(MatCreate_MPIAIJ(A));
576f3d89d0SStefano Zampini   A->boundtocpu = PETSC_FALSE;
589566063dSJacob Faibussowitsch   PetscCall(PetscFree(A->defaultvectype));
599566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(VECVIENNACL, &A->defaultvectype));
609566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatMPIAIJSetPreallocation_C", MatMPIAIJSetPreallocation_MPIAIJViennaCL));
6174fd8ad3SBarry Smith   A->ops->assemblyend = MatAssemblyEnd_MPIAIJViennaCL;
629566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)A, MATMPIAIJVIENNACL));
633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
648f86e40fSKarl Rupp }
658f86e40fSKarl Rupp 
66cab5ea25SPierre Jolivet /*@C
6711a5261eSBarry Smith   MatCreateAIJViennaCL - Creates a sparse matrix in `MATAIJ` (compressed row) format
68023073b3SKarl Rupp   (the default parallel PETSc format).  This matrix will ultimately be pushed down
6920f4b53cSBarry Smith   to GPUs and use the ViennaCL library for calculations.
708f86e40fSKarl Rupp 
71d083f849SBarry Smith   Collective
728f86e40fSKarl Rupp 
738f86e40fSKarl Rupp   Input Parameters:
7411a5261eSBarry Smith + comm  - MPI communicator, set to `PETSC_COMM_SELF`
7520f4b53cSBarry Smith . m     - number of rows, or `PETSC_DECIDE` if `M` is provided
7620f4b53cSBarry Smith . n     - number of columns, or `PETSC_DECIDE` if `N` is provided
7720f4b53cSBarry Smith . M     - number of global rows in the matrix, or `PETSC_DETERMINE` if `m` is provided
7820f4b53cSBarry Smith . N     - number of global columns in the matrix, or `PETSC_DETERMINE` if `n` is provided
7920f4b53cSBarry Smith . d_nz  - number of nonzeros per row in DIAGONAL portion of local submatrix
8020f4b53cSBarry Smith            (same value is used for all local rows)
8120f4b53cSBarry Smith . d_nnz - array containing the number of nonzeros in the various rows of the
8220f4b53cSBarry Smith            DIAGONAL portion of the local submatrix (possibly different for each row)
8320f4b53cSBarry Smith            or `NULL`, if `d_nz` is used to specify the nonzero structure.
8420f4b53cSBarry Smith            The size of this array is equal to the number of local rows, i.e `m`.
8520f4b53cSBarry Smith            For matrices you plan to factor you must leave room for the diagonal entry and
8620f4b53cSBarry Smith            put in the entry even if it is zero.
8720f4b53cSBarry Smith . o_nz  - number of nonzeros per row in the OFF-DIAGONAL portion of local
8820f4b53cSBarry Smith            submatrix (same value is used for all local rows).
8920f4b53cSBarry Smith - o_nnz - array containing the number of nonzeros in the various rows of the
9020f4b53cSBarry Smith            OFF-DIAGONAL portion of the local submatrix (possibly different for
9120f4b53cSBarry Smith            each row) or `NULL`, if `o_nz` is used to specify the nonzero
9220f4b53cSBarry Smith            structure. The size of this array is equal to the number
9320f4b53cSBarry Smith            of local rows, i.e `m`.
948f86e40fSKarl Rupp 
958f86e40fSKarl Rupp   Output Parameter:
968f86e40fSKarl Rupp . A - the matrix
978f86e40fSKarl Rupp 
9820f4b53cSBarry Smith   Level: intermediate
9920f4b53cSBarry Smith 
10020f4b53cSBarry Smith   Notes:
10111a5261eSBarry Smith   It is recommended that one use the `MatCreate()`, `MatSetType()` and/or `MatSetFromOptions()`,
1028f86e40fSKarl Rupp   MatXXXXSetPreallocation() paradigm instead of this routine directly.
10311a5261eSBarry Smith   [MatXXXXSetPreallocation() is, for example, `MatSeqAIJSetPreallocation()`]
1048f86e40fSKarl Rupp 
10511a5261eSBarry Smith   The AIJ format, also called
1062ef1f0ffSBarry Smith   compressed row storage), is fully compatible with standard Fortran
1078f86e40fSKarl Rupp   storage.  That is, the stored row and column indices can begin at
10820f4b53cSBarry Smith   either one (as in Fortran) or zero.
1098f86e40fSKarl Rupp 
11020f4b53cSBarry Smith .seealso: `Mat`, `MatCreate()`, `MatCreateAIJ()`, `MatCreateAIJCUSPARSE()`, `MatSetValues()`, `MatSeqAIJSetColumnIndices()`, `MatCreateSeqAIJWithArrays()`,
111*fe59aa6dSJacob Faibussowitsch           `MATMPIAIJVIENNACL`, `MATAIJVIENNACL`
1128f86e40fSKarl Rupp @*/
113d71ae5a4SJacob Faibussowitsch PetscErrorCode MatCreateAIJViennaCL(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)
114d71ae5a4SJacob Faibussowitsch {
1158f86e40fSKarl Rupp   PetscMPIInt size;
1168f86e40fSKarl Rupp 
1178f86e40fSKarl Rupp   PetscFunctionBegin;
1189566063dSJacob Faibussowitsch   PetscCall(MatCreate(comm, A));
1199566063dSJacob Faibussowitsch   PetscCall(MatSetSizes(*A, m, n, M, N));
1209566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
1218f86e40fSKarl Rupp   if (size > 1) {
1229566063dSJacob Faibussowitsch     PetscCall(MatSetType(*A, MATMPIAIJVIENNACL));
1239566063dSJacob Faibussowitsch     PetscCall(MatMPIAIJSetPreallocation(*A, d_nz, d_nnz, o_nz, o_nnz));
1248f86e40fSKarl Rupp   } else {
1259566063dSJacob Faibussowitsch     PetscCall(MatSetType(*A, MATSEQAIJVIENNACL));
1269566063dSJacob Faibussowitsch     PetscCall(MatSeqAIJSetPreallocation(*A, d_nz, d_nnz));
1278f86e40fSKarl Rupp   }
1283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1298f86e40fSKarl Rupp }
1308f86e40fSKarl Rupp 
1313ca39a21SBarry Smith /*MC
1328f86e40fSKarl Rupp    MATAIJVIENNACL - MATMPIAIJVIENNACL= "aijviennacl" = "mpiaijviennacl" - A matrix type to be used for sparse matrices.
1338f86e40fSKarl Rupp 
1348f86e40fSKarl Rupp    A matrix type (CSR format) whose data resides on GPUs.
1358f86e40fSKarl Rupp    All matrix calculations are performed using the ViennaCL library.
1368f86e40fSKarl Rupp 
13711a5261eSBarry Smith    This matrix type is identical to `MATSEQAIJVIENNACL` when constructed with a single process communicator,
13811a5261eSBarry Smith    and `MATMPIAIJVIENNACL` otherwise.  As a result, for single process communicators,
13911a5261eSBarry Smith    `MatSeqAIJSetPreallocation()` is supported, and similarly `MatMPIAIJSetPreallocation()` is supported
1408f86e40fSKarl Rupp    for communicators controlling multiple processes.  It is recommended that you call both of
1418f86e40fSKarl Rupp    the above preallocation routines for simplicity.
1428f86e40fSKarl Rupp 
1438f86e40fSKarl Rupp    Options Database Keys:
14411a5261eSBarry Smith .  -mat_type mpiaijviennacl - sets the matrix type to `MATAIJVIENNACL` during a call to `MatSetFromOptions()`
1458f86e40fSKarl Rupp 
1468f86e40fSKarl Rupp   Level: beginner
1478f86e40fSKarl Rupp 
14820f4b53cSBarry Smith .seealso: `Mat`, `MatType`, `MatCreateAIJViennaCL()`, `MATSEQAIJVIENNACL`, `MatCreateSeqAIJVIENNACL()`
1498f86e40fSKarl Rupp M*/
150