Actual source code: mpb_aij.c

petsc-3.4.0 2013-05-13
  1: #include <../src/mat/impls/aij/mpi/mpiaij.h>

  5: /*
  6:     Developers Note: This is used directly by some preconditioners, hence is PETSC_EXTERN
  7: */
  8: PETSC_EXTERN PetscErrorCode  MatGetMultiProcBlock_MPIAIJ(Mat mat, MPI_Comm subComm, MatReuse scall,Mat *subMat)
  9: {
 11:   Mat_MPIAIJ     *aij  = (Mat_MPIAIJ*)mat->data;
 12:   Mat_SeqAIJ     *aijB = (Mat_SeqAIJ*)aij->B->data;
 13:   PetscMPIInt    commRank,subCommSize,subCommRank;
 14:   PetscMPIInt    *commRankMap,subRank,rank,commsize;
 15:   PetscInt       *garrayCMap,col,i,j,*nnz,newRow,newCol;

 18:   MPI_Comm_size(PetscObjectComm((PetscObject)mat),&commsize);
 19:   MPI_Comm_size(subComm,&subCommSize);

 21:   /* create subMat object with the relavent layout */
 22:   if (scall == MAT_INITIAL_MATRIX) {
 23:     MatCreate(subComm,subMat);
 24:     MatSetType(*subMat,MATMPIAIJ);
 25:     MatSetSizes(*subMat,mat->rmap->n,mat->cmap->n,PETSC_DECIDE,PETSC_DECIDE);
 26:     MatSetBlockSizes(*subMat,mat->rmap->bs,mat->cmap->bs);

 28:     /* need to setup rmap and cmap before Preallocation */
 29:     PetscLayoutSetBlockSize((*subMat)->rmap,mat->rmap->bs);
 30:     PetscLayoutSetBlockSize((*subMat)->cmap,mat->cmap->bs);
 31:     PetscLayoutSetUp((*subMat)->rmap);
 32:     PetscLayoutSetUp((*subMat)->cmap);
 33:   }

 35:   /* create a map of comm_rank from subComm to comm - should commRankMap and garrayCMap be kept for reused? */
 36:   MPI_Comm_rank(PetscObjectComm((PetscObject)mat),&commRank);
 37:   MPI_Comm_rank(subComm,&subCommRank);
 38:   PetscMalloc(subCommSize*sizeof(PetscMPIInt),&commRankMap);
 39:   MPI_Allgather(&commRank,1,MPI_INT,commRankMap,1,MPI_INT,subComm);

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

 57:   if (scall == MAT_INITIAL_MATRIX) {
 58:     /* Now compute preallocation for the offdiag mat */
 59:     PetscMalloc(aij->B->rmap->n*sizeof(PetscInt),&nnz);
 60:     PetscMemzero(nnz,aij->B->rmap->n*sizeof(PetscInt));
 61:     for (i=0; i<aij->B->rmap->n; i++) {
 62:       for (j=aijB->i[i]; j<aijB->i[i+1]; j++) {
 63:         if (garrayCMap[aijB->j[j]]) nnz[i]++;
 64:       }
 65:     }
 66:     MatMPIAIJSetPreallocation(*(subMat),0,NULL,0,nnz);

 68:     /* reuse diag block with the new submat */
 69:     MatDestroy(&((Mat_MPIAIJ*)((*subMat)->data))->A);

 71:     ((Mat_MPIAIJ*)((*subMat)->data))->A = aij->A;

 73:     PetscObjectReference((PetscObject)aij->A);
 74:   } else if (((Mat_MPIAIJ*)(*subMat)->data)->A != aij->A) {
 75:     PetscObject obj = (PetscObject)((Mat_MPIAIJ*)((*subMat)->data))->A;

 77:     PetscObjectReference((PetscObject)obj);

 79:     ((Mat_MPIAIJ*)((*subMat)->data))->A = aij->A;

 81:     PetscObjectReference((PetscObject)aij->A);
 82:   }

 84:   /* Now traverse aij->B and insert values into subMat */
 85:   for (i=0; i<aij->B->rmap->n; i++) {
 86:     newRow = (*subMat)->rmap->range[subCommRank] + i;
 87:     for (j=aijB->i[i]; j<aijB->i[i+1]; j++) {
 88:       newCol = garrayCMap[aijB->j[j]];
 89:       if (newCol) {
 90:         newCol--; /* remove the increment */
 91:         MatSetValues(*subMat,1,&newRow,1,&newCol,(aijB->a+j),INSERT_VALUES);
 92:       }
 93:     }
 94:   }

 96:   /* assemble the submat */
 97:   MatAssemblyBegin(*subMat,MAT_FINAL_ASSEMBLY);
 98:   MatAssemblyEnd(*subMat,MAT_FINAL_ASSEMBLY);

100:   /* deallocate temporary data */
101:   PetscFree(commRankMap);
102:   PetscFree(garrayCMap);
103:   if (scall == MAT_INITIAL_MATRIX) {
104:     PetscFree(nnz);
105:   }
106:   return(0);
107: }