Actual source code: mpb_baij.c

  1: #include <../src/mat/impls/baij/mpi/mpibaij.h>

  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:   PetscBool    nooffprocentries;

 14:   PetscFunctionBegin;
 15:   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)mat), &commsize));
 16:   PetscCallMPI(MPI_Comm_size(subComm, &subCommSize));

 18:   /* create subMat object with the relevant layout */
 19:   if (scall == MAT_INITIAL_MATRIX) {
 20:     PetscCall(MatCreate(subComm, subMat));
 21:     PetscCall(MatSetType(*subMat, MATMPIBAIJ));
 22:     PetscCall(MatSetSizes(*subMat, mat->rmap->n, mat->cmap->n, PETSC_DECIDE, PETSC_DECIDE));
 23:     PetscCall(MatSetBlockSizes(*subMat, mat->rmap->bs, mat->cmap->bs));

 25:     /* need to setup rmap and cmap before Preallocation */
 26:     PetscCall(PetscLayoutSetBlockSize((*subMat)->rmap, mat->rmap->bs));
 27:     PetscCall(PetscLayoutSetBlockSize((*subMat)->cmap, mat->cmap->bs));
 28:     PetscCall(PetscLayoutSetUp((*subMat)->rmap));
 29:     PetscCall(PetscLayoutSetUp((*subMat)->cmap));
 30:   }

 32:   /* create a map of comm_rank from subComm to comm - should commRankMap and garrayCMap be kept for reused? */
 33:   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)mat), &commRank));
 34:   PetscCallMPI(MPI_Comm_rank(subComm, &subCommRank));
 35:   PetscCall(PetscMalloc1(subCommSize, &commRankMap));
 36:   PetscCallMPI(MPI_Allgather(&commRank, 1, MPI_INT, commRankMap, 1, MPI_INT, subComm));

 38:   /* Traverse garray and identify blocked column indices [of offdiag mat] that
 39:    should be discarded. For the ones not discarded, store the newCol+1
 40:    value in garrayCMap */
 41:   PetscCall(PetscCalloc1(aij->B->cmap->n / bs, &garrayCMap));
 42:   for (i = 0; i < aij->B->cmap->n / bs; i++) {
 43:     col = aij->garray[i]; /* blocked column index */
 44:     for (subRank = 0; subRank < subCommSize; subRank++) {
 45:       rank = commRankMap[subRank];
 46:       if (col >= mat->cmap->range[rank] / bs && col < mat->cmap->range[rank + 1] / bs) {
 47:         garrayCMap[i] = (((*subMat)->cmap->range[subRank] - mat->cmap->range[rank]) / bs + col + 1);
 48:         break;
 49:       }
 50:     }
 51:   }

 53:   if (scall == MAT_INITIAL_MATRIX) {
 54:     /* Now compute preallocation for the offdiag mat */
 55:     PetscCall(PetscCalloc1(aij->B->rmap->n / bs, &nnz));
 56:     for (i = 0; i < aij->B->rmap->n / bs; i++) {
 57:       for (j = aijB->i[i]; j < aijB->i[i + 1]; j++) {
 58:         if (garrayCMap[aijB->j[j]]) nnz[i]++;
 59:       }
 60:     }
 61:     PetscCall(MatMPIBAIJSetPreallocation(*(subMat), bs, 0, NULL, 0, nnz));

 63:     /* reuse diag block with the new submat */
 64:     PetscCall(MatDestroy(&((Mat_MPIBAIJ *)((*subMat)->data))->A));

 66:     ((Mat_MPIBAIJ *)(*subMat)->data)->A = aij->A;

 68:     PetscCall(PetscObjectReference((PetscObject)aij->A));
 69:   } else if (((Mat_MPIBAIJ *)(*subMat)->data)->A != aij->A) {
 70:     PetscObject obj = (PetscObject)((Mat_MPIBAIJ *)(*subMat)->data)->A;

 72:     PetscCall(PetscObjectReference(obj));

 74:     ((Mat_MPIBAIJ *)(*subMat)->data)->A = aij->A;

 76:     PetscCall(PetscObjectReference((PetscObject)aij->A));
 77:   }

 79:   /* Now traverse aij->B and insert values into subMat */
 80:   /* Does not need PetscShmgetAllocateArray() since temporary */
 81:   PetscCall(PetscMalloc3(bs, &newbRow, bs, &newbCol, bs * bs, &vals));
 82:   for (i = 0; i < aij->B->rmap->n / bs; i++) {
 83:     newRow = (*subMat)->rmap->range[subCommRank] + i * bs;
 84:     for (j = aijB->i[i]; j < aijB->i[i + 1]; j++) {
 85:       newCol = garrayCMap[aijB->j[j]];
 86:       if (newCol) {
 87:         newCol--; /* remove the increment */
 88:         newCol *= bs;
 89:         for (k = 0; k < bs; k++) {
 90:           newbRow[k] = newRow + k;
 91:           newbCol[k] = newCol + k;
 92:         }
 93:         /* copy column-oriented aijB->a into row-oriented vals */
 94:         aijBvals = aijB->a + j * bs * bs;
 95:         for (k1 = 0; k1 < bs; k1++) {
 96:           for (k = 0; k < bs; k++) vals[k1 + k * bs] = *aijBvals++;
 97:         }
 98:         PetscCall(MatSetValues(*subMat, bs, newbRow, bs, newbCol, vals, INSERT_VALUES));
 99:       }
100:     }
101:   }
102:   nooffprocentries            = (*subMat)->nooffprocentries;
103:   (*subMat)->nooffprocentries = PETSC_TRUE;
104:   PetscCall(MatAssemblyBegin(*subMat, MAT_FINAL_ASSEMBLY));
105:   PetscCall(MatAssemblyEnd(*subMat, MAT_FINAL_ASSEMBLY));
106:   (*subMat)->nooffprocentries = nooffprocentries;

108:   /* deallocate temporary data */
109:   PetscCall(PetscFree3(newbRow, newbCol, vals));
110:   PetscCall(PetscFree(commRankMap));
111:   PetscCall(PetscFree(garrayCMap));
112:   if (scall == MAT_INITIAL_MATRIX) PetscCall(PetscFree(nnz));
113:   PetscFunctionReturn(PETSC_SUCCESS);
114: }