Actual source code: mpiaijviennacl.cxx
petsc-3.8.4 2018-03-24
1: #include <petscconf.h>
2: #include <../src/mat/impls/aij/mpi/mpiaij.h>
3: #include <../src/mat/impls/aij/seq/seqviennacl/viennaclmatimpl.h>
5: PetscErrorCode MatMPIAIJSetPreallocation_MPIAIJViennaCL(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[])
6: {
7: Mat_MPIAIJ *b = (Mat_MPIAIJ*)B->data;
11: PetscLayoutSetUp(B->rmap);
12: PetscLayoutSetUp(B->cmap);
13: if (!B->preallocated) {
14: /* Explicitly create the two MATSEQAIJVIENNACL matrices. */
15: MatCreate(PETSC_COMM_SELF,&b->A);
16: MatSetSizes(b->A,B->rmap->n,B->cmap->n,B->rmap->n,B->cmap->n);
17: MatSetType(b->A,MATSEQAIJVIENNACL);
18: PetscLogObjectParent((PetscObject)B,(PetscObject)b->A);
19: MatCreate(PETSC_COMM_SELF,&b->B);
20: MatSetSizes(b->B,B->rmap->n,B->cmap->N,B->rmap->n,B->cmap->N);
21: MatSetType(b->B,MATSEQAIJVIENNACL);
22: PetscLogObjectParent((PetscObject)B,(PetscObject)b->B);
23: }
24: MatSeqAIJSetPreallocation(b->A,d_nz,d_nnz);
25: MatSeqAIJSetPreallocation(b->B,o_nz,o_nnz);
26: B->preallocated = PETSC_TRUE;
27: return(0);
28: }
30: PetscErrorCode MatCreateVecs_MPIAIJViennaCL(Mat mat,Vec *right,Vec *left)
31: {
33: PetscInt rbs,cbs;
36: MatGetBlockSizes(mat,&rbs,&cbs);
37: if (right) {
38: VecCreate(PetscObjectComm((PetscObject)mat),right);
39: VecSetSizes(*right,mat->cmap->n,PETSC_DETERMINE);
40: VecSetBlockSize(*right,cbs);
41: VecSetType(*right,VECVIENNACL);
42: VecSetLayout(*right,mat->cmap);
43: }
44: if (left) {
45: VecCreate(PetscObjectComm((PetscObject)mat),left);
46: VecSetSizes(*left,mat->rmap->n,PETSC_DETERMINE);
47: VecSetBlockSize(*left,rbs);
48: VecSetType(*left,VECVIENNACL);
49: VecSetLayout(*left,mat->rmap);
50: }
51: return(0);
52: }
55: PetscErrorCode MatDestroy_MPIAIJViennaCL(Mat A)
56: {
60: MatDestroy_MPIAIJ(A);
61: return(0);
62: }
64: PETSC_EXTERN PetscErrorCode MatCreate_MPIAIJViennaCL(Mat A)
65: {
69: MatCreate_MPIAIJ(A);
70: PetscObjectComposeFunction((PetscObject)A,"MatMPIAIJSetPreallocation_C",MatMPIAIJSetPreallocation_MPIAIJViennaCL);
71: A->ops->getvecs = MatCreateVecs_MPIAIJViennaCL;
73: PetscObjectChangeTypeName((PetscObject)A,MATMPIAIJVIENNACL);
74: return(0);
75: }
78: /*@
79: MatCreateAIJViennaCL - Creates a sparse matrix in AIJ (compressed row) format
80: (the default parallel PETSc format). This matrix will ultimately be pushed down
81: to GPUs and use the ViennaCL library for calculations. For good matrix
82: assembly performance the user should preallocate the matrix storage by setting
83: the parameter nz (or the array nnz). By setting these parameters accurately,
84: performance during matrix assembly can be increased substantially.
87: Collective on MPI_Comm
89: Input Parameters:
90: + comm - MPI communicator, set to PETSC_COMM_SELF
91: . m - number of rows
92: . n - number of columns
93: . nz - number of nonzeros per row (same for all rows)
94: - nnz - array containing the number of nonzeros in the various rows
95: (possibly different for each row) or NULL
97: Output Parameter:
98: . A - the matrix
100: It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
101: MatXXXXSetPreallocation() paradigm instead of this routine directly.
102: [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
104: Notes:
105: If nnz is given then nz is ignored
107: The AIJ format (also called the Yale sparse matrix format or
108: compressed row storage), is fully compatible with standard Fortran 77
109: storage. That is, the stored row and column indices can begin at
110: either one (as in Fortran) or zero. See the users' manual for details.
112: Specify the preallocated storage with either nz or nnz (not both).
113: Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory
114: allocation. For large problems you MUST preallocate memory or you
115: will get TERRIBLE performance, see the users' manual chapter on matrices.
117: Level: intermediate
119: .seealso: MatCreate(), MatCreateAIJ(), MatCreateAIJCUSP(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatCreateAIJ(), MATMPIAIJVIENNACL, MATAIJVIENNACL
120: @*/
121: 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)
122: {
124: PetscMPIInt size;
127: MatCreate(comm,A);
128: MatSetSizes(*A,m,n,M,N);
129: MPI_Comm_size(comm,&size);
130: if (size > 1) {
131: MatSetType(*A,MATMPIAIJVIENNACL);
132: MatMPIAIJSetPreallocation(*A,d_nz,d_nnz,o_nz,o_nnz);
133: } else {
134: MatSetType(*A,MATSEQAIJVIENNACL);
135: MatSeqAIJSetPreallocation(*A,d_nz,d_nnz);
136: }
137: return(0);
138: }
140: /*M
141: MATAIJVIENNACL - MATMPIAIJVIENNACL= "aijviennacl" = "mpiaijviennacl" - A matrix type to be used for sparse matrices.
143: A matrix type (CSR format) whose data resides on GPUs.
144: All matrix calculations are performed using the ViennaCL library.
146: This matrix type is identical to MATSEQAIJVIENNACL when constructed with a single process communicator,
147: and MATMPIAIJVIENNACL otherwise. As a result, for single process communicators,
148: MatSeqAIJSetPreallocation is supported, and similarly MatMPIAIJSetPreallocation is supported
149: for communicators controlling multiple processes. It is recommended that you call both of
150: the above preallocation routines for simplicity.
152: Options Database Keys:
153: + -mat_type mpiaijviennacl - sets the matrix type to "mpiaijviennacl" during a call to MatSetFromOptions()
155: Level: beginner
157: .seealso: MatCreateAIJViennaCL(), MATSEQAIJVIENNACL, MatCreateSeqAIJVIENNACL()
158: M*/