xref: /petsc/src/mat/impls/baij/mpi/mpb_baij.c (revision 503c0ea9b45bcfbcebbb1ea5341243bbc69f0bea)
1 #include <../src/mat/impls/baij/mpi/mpibaij.h>
2 
3 PetscErrorCode  MatGetMultiProcBlock_MPIBAIJ(Mat mat, MPI_Comm subComm, MatReuse scall,Mat *subMat)
4 {
5   Mat_MPIBAIJ    *aij  = (Mat_MPIBAIJ*)mat->data;
6   Mat_SeqBAIJ    *aijB = (Mat_SeqBAIJ*)aij->B->data;
7   PetscMPIInt    commRank,subCommSize,subCommRank;
8   PetscMPIInt    *commRankMap,subRank,rank,commsize;
9   PetscInt       *garrayCMap,col,i,j,*nnz,newRow,newCol,*newbRow,*newbCol,k,k1;
10   PetscInt       bs=mat->rmap->bs;
11   PetscScalar    *vals,*aijBvals;
12 
13   PetscFunctionBegin;
14   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)mat),&commsize));
15   PetscCallMPI(MPI_Comm_size(subComm,&subCommSize));
16 
17   /* create subMat object with the relevant layout */
18   if (scall == MAT_INITIAL_MATRIX) {
19     PetscCall(MatCreate(subComm,subMat));
20     PetscCall(MatSetType(*subMat,MATMPIBAIJ));
21     PetscCall(MatSetSizes(*subMat,mat->rmap->n,mat->cmap->n,PETSC_DECIDE,PETSC_DECIDE));
22     PetscCall(MatSetBlockSizes(*subMat,mat->rmap->bs,mat->cmap->bs));
23 
24     /* need to setup rmap and cmap before Preallocation */
25     PetscCall(PetscLayoutSetBlockSize((*subMat)->rmap,mat->rmap->bs));
26     PetscCall(PetscLayoutSetBlockSize((*subMat)->cmap,mat->cmap->bs));
27     PetscCall(PetscLayoutSetUp((*subMat)->rmap));
28     PetscCall(PetscLayoutSetUp((*subMat)->cmap));
29   }
30 
31   /* create a map of comm_rank from subComm to comm - should commRankMap and garrayCMap be kept for reused? */
32   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)mat),&commRank));
33   PetscCallMPI(MPI_Comm_rank(subComm,&subCommRank));
34   PetscCall(PetscMalloc1(subCommSize,&commRankMap));
35   PetscCallMPI(MPI_Allgather(&commRank,1,MPI_INT,commRankMap,1,MPI_INT,subComm));
36 
37   /* Traverse garray and identify blocked column indices [of offdiag mat] that
38    should be discarded. For the ones not discarded, store the newCol+1
39    value in garrayCMap */
40   PetscCall(PetscCalloc1(aij->B->cmap->n/bs,&garrayCMap));
41   for (i=0; i<aij->B->cmap->n/bs; i++) {
42     col = aij->garray[i]; /* blocked column index */
43     for (subRank=0; subRank<subCommSize; subRank++) {
44       rank = commRankMap[subRank];
45       if ((col >= mat->cmap->range[rank]/bs) && (col < mat->cmap->range[rank+1]/bs)) {
46         garrayCMap[i] = (((*subMat)->cmap->range[subRank]- mat->cmap->range[rank])/bs + col + 1);
47         break;
48       }
49     }
50   }
51 
52   if (scall == MAT_INITIAL_MATRIX) {
53     /* Now compute preallocation for the offdiag mat */
54     PetscCall(PetscCalloc1(aij->B->rmap->n/bs,&nnz));
55     for (i=0; i<aij->B->rmap->n/bs; i++) {
56       for (j=aijB->i[i]; j<aijB->i[i+1]; j++) {
57         if (garrayCMap[aijB->j[j]]) nnz[i]++;
58       }
59     }
60     PetscCall(MatMPIBAIJSetPreallocation(*(subMat),bs,0,NULL,0,nnz));
61 
62     /* reuse diag block with the new submat */
63     PetscCall(MatDestroy(&((Mat_MPIBAIJ*)((*subMat)->data))->A));
64 
65     ((Mat_MPIBAIJ*)((*subMat)->data))->A = aij->A;
66 
67     PetscCall(PetscObjectReference((PetscObject)aij->A));
68   } else if (((Mat_MPIBAIJ*)(*subMat)->data)->A != aij->A) {
69     PetscObject obj = (PetscObject)((Mat_MPIBAIJ*)((*subMat)->data))->A;
70 
71     PetscCall(PetscObjectReference((PetscObject)obj));
72 
73     ((Mat_MPIBAIJ*)((*subMat)->data))->A = aij->A;
74 
75     PetscCall(PetscObjectReference((PetscObject)aij->A));
76   }
77 
78   /* Now traverse aij->B and insert values into subMat */
79   PetscCall(PetscMalloc3(bs,&newbRow,bs,&newbCol,bs*bs,&vals));
80   for (i=0; i<aij->B->rmap->n/bs; i++) {
81     newRow = (*subMat)->rmap->range[subCommRank] + i*bs;
82     for (j=aijB->i[i]; j<aijB->i[i+1]; j++) {
83       newCol = garrayCMap[aijB->j[j]];
84       if (newCol) {
85         newCol--; /* remove the increment */
86         newCol *= bs;
87         for (k=0; k<bs; k++) {
88           newbRow[k] = newRow + k;
89           newbCol[k] = newCol + k;
90         }
91         /* copy column-oriented aijB->a into row-oriented vals */
92         aijBvals = aijB->a + j*bs*bs;
93         for (k1=0; k1<bs; k1++) {
94           for (k=0; k<bs; k++) {
95             vals[k1+k*bs] = *aijBvals++;
96           }
97         }
98         PetscCall(MatSetValues(*subMat,bs,newbRow,bs,newbCol,vals,INSERT_VALUES));
99       }
100     }
101   }
102   PetscCall(MatAssemblyBegin(*subMat,MAT_FINAL_ASSEMBLY));
103   PetscCall(MatAssemblyEnd(*subMat,MAT_FINAL_ASSEMBLY));
104 
105   /* deallocate temporary data */
106   PetscCall(PetscFree3(newbRow,newbCol,vals));
107   PetscCall(PetscFree(commRankMap));
108   PetscCall(PetscFree(garrayCMap));
109   if (scall == MAT_INITIAL_MATRIX) {
110     PetscCall(PetscFree(nnz));
111   }
112   PetscFunctionReturn(0);
113 }
114