xref: /petsc/src/mat/impls/baij/mpi/mpiaijbaij.c (revision 247fa90bf5e10ed3512c8e8f64aafd98857b2846)
1ae8d29abSPierre Jolivet 
2ae8d29abSPierre Jolivet #include <../src/mat/impls/baij/mpi/mpibaij.h> /*I "petscmat.h" I*/
3ae8d29abSPierre Jolivet #include <../src/mat/impls/aij/mpi/mpiaij.h>
4ae8d29abSPierre Jolivet #include <petsc/private/matimpl.h>
5ae8d29abSPierre Jolivet #include <petscmat.h>
6ae8d29abSPierre Jolivet 
7*247fa90bSBarry Smith PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqBAIJ_Preallocate(Mat, PetscInt **);
8*247fa90bSBarry Smith 
9d71ae5a4SJacob Faibussowitsch PETSC_INTERN PetscErrorCode MatConvert_MPIAIJ_MPIBAIJ(Mat A, MatType newtype, MatReuse reuse, Mat *newmat)
10d71ae5a4SJacob Faibussowitsch {
11ae8d29abSPierre Jolivet   Mat         M;
12ae8d29abSPierre Jolivet   Mat_MPIAIJ *mpimat = (Mat_MPIAIJ *)A->data;
13ae8d29abSPierre Jolivet   PetscInt   *d_nnz, *o_nnz;
14*247fa90bSBarry Smith   PetscInt    m, n, lm, ln, bs = PetscAbs(A->rmap->bs);
15ae8d29abSPierre Jolivet 
16ae8d29abSPierre Jolivet   PetscFunctionBegin;
17ae8d29abSPierre Jolivet   if (reuse != MAT_REUSE_MATRIX) {
18*247fa90bSBarry Smith     PetscBool3 sym = A->symmetric, hermitian = A->hermitian, structurally_symmetric = A->structurally_symmetric, spd = A->spd;
19*247fa90bSBarry Smith     PetscCall(MatDisAssemble_MPIAIJ(A));
209566063dSJacob Faibussowitsch     PetscCall(MatGetSize(A, &m, &n));
219566063dSJacob Faibussowitsch     PetscCall(MatGetLocalSize(A, &lm, &ln));
22*247fa90bSBarry Smith     PetscCall(MatConvert_SeqAIJ_SeqBAIJ_Preallocate(mpimat->A, &d_nnz));
23*247fa90bSBarry Smith     PetscCall(MatConvert_SeqAIJ_SeqBAIJ_Preallocate(mpimat->B, &o_nnz));
249566063dSJacob Faibussowitsch     PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &M));
259566063dSJacob Faibussowitsch     PetscCall(MatSetSizes(M, lm, ln, m, n));
269566063dSJacob Faibussowitsch     PetscCall(MatSetType(M, MATMPIBAIJ));
279566063dSJacob Faibussowitsch     PetscCall(MatSeqBAIJSetPreallocation(M, bs, 0, d_nnz));
289566063dSJacob Faibussowitsch     PetscCall(MatMPIBAIJSetPreallocation(M, bs, 0, d_nnz, 0, o_nnz));
29*247fa90bSBarry Smith     PetscCall(PetscFree(d_nnz));
30*247fa90bSBarry Smith     PetscCall(PetscFree(o_nnz));
31*247fa90bSBarry Smith     PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
32*247fa90bSBarry Smith     PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
33*247fa90bSBarry Smith     A->symmetric              = sym;
34*247fa90bSBarry Smith     A->hermitian              = hermitian;
35*247fa90bSBarry Smith     A->structurally_symmetric = structurally_symmetric;
36*247fa90bSBarry Smith     A->spd                    = spd;
37ae8d29abSPierre Jolivet   } else M = *newmat;
38ae8d29abSPierre Jolivet 
39ae8d29abSPierre Jolivet   /* reuse may not be equal to MAT_REUSE_MATRIX, but the basic converter will reallocate or replace newmat if this value is not used */
40ae8d29abSPierre Jolivet   /* if reuse is equal to MAT_INITIAL_MATRIX, it has been appropriately preallocated before                                          */
41ae8d29abSPierre Jolivet   /*                      MAT_INPLACE_MATRIX, it will be replaced with MatHeaderReplace below                                        */
429566063dSJacob Faibussowitsch   PetscCall(MatConvert_Basic(A, newtype, MAT_REUSE_MATRIX, &M));
43ae8d29abSPierre Jolivet 
44ae8d29abSPierre Jolivet   if (reuse == MAT_INPLACE_MATRIX) {
459566063dSJacob Faibussowitsch     PetscCall(MatHeaderReplace(A, &M));
46ae8d29abSPierre Jolivet   } else *newmat = M;
47ae8d29abSPierre Jolivet   PetscFunctionReturn(0);
48ae8d29abSPierre Jolivet }
49