Actual source code: mpiaij.h

  1: #pragma once

  3: #include <../src/mat/impls/aij/seq/aij.h>

  5: typedef struct { /* used by MatCreateMPIAIJSumSeqAIJ for reusing the merged matrix */
  6:   PetscLayout  rowmap;
  7:   PetscInt   **buf_ri, **buf_rj;
  8:   PetscMPIInt *len_s, *len_r, *id_r; /* array of length of comm->size, store send/recv matrix values */
  9:   PetscMPIInt  nsend, nrecv;
 10:   PetscInt    *bi, *bj;               /* i and j array of the local portion of mpi C (matrix product) - rename to ci, cj! */
 11:   PetscInt    *owners_co, *coi, *coj; /* i and j array of (p->B)^T*A*P - used in the communication */
 12: } MatMergeSeqsToMPI;

 14: typedef struct {                                /* used by MatPtAPXXX_MPIAIJ_MPIAIJ() and MatMatMultXXX_MPIAIJ_MPIAIJ() */
 15:   PetscInt              *startsj_s, *startsj_r; /* used by MatGetBrowsOfAoCols_MPIAIJ */
 16:   PetscScalar           *bufa;                  /* used by MatGetBrowsOfAoCols_MPIAIJ */
 17:   Mat                    P_loc, P_oth;          /* partial B_seq -- intend to replace B_seq */
 18:   PetscInt              *api, *apj;             /* symbolic i and j arrays of the local product A_loc*B_seq */
 19:   PetscScalar           *apv;
 20:   MatReuse               reuse; /* flag to skip MatGetBrowsOfAoCols_MPIAIJ() and MatMPIAIJGetLocalMat() in 1st call of MatPtAPNumeric_MPIAIJ_MPIAIJ() */
 21:   PetscScalar           *apa;   /* tmp array for store a row of A*P used in MatMatMult() */
 22:   Mat                    A_loc; /* used by MatTransposeMatMult(), contains api and apj */
 23:   ISLocalToGlobalMapping ltog;  /* mapping from local column indices to global column indices for A_loc */
 24:   Mat                    Pt;    /* used by MatTransposeMatMult(), Pt = P^T */
 25:   Mat                    Rd, Ro, AP_loc, C_loc, C_oth;
 26:   PetscInt               algType; /* implementation algorithm */
 27:   PetscSF                sf;      /* use it to communicate remote part of C */
 28:   PetscInt              *c_othi, *c_rmti;

 30:   MatMergeSeqsToMPI *merge;
 31: } MatProductCtx_APMPI;

 33: #if PetscDefined(USE_CTABLE)
 34:   #define PETSCTABLE PetscHMapI
 35: #else
 36:   #define PETSCTABLE PetscInt *
 37: #endif

 39: // Shared by MPIAIJ, MPIBAIJ, MPISBAIJ so that we can access common fields in the same way.
 40: #define MPIAIJHEADER \
 41:   Mat         A, B; /* local submatrices: A (diag part), B (off-diag part) */ \
 42:   PetscMPIInt size; /* size of communicator */ \
 43:   PetscMPIInt rank; /* rank of proc in communicator */ \
 44: \
 45:   /* The following variables are used for matrix assembly */ \
 46:   PetscBool    donotstash;        /* if 1, off processor entries dropped */ \
 47:   MPI_Request *send_waits;        /* array of send requests */ \
 48:   MPI_Request *recv_waits;        /* array of receive requests */ \
 49:   PetscInt     nsends, nrecvs;    /* numbers of sends and receives */ \
 50:   MatScalar   *svalues, *rvalues; /* sending and receiving data */ \
 51:   PetscInt     rmax;              /* maximum message length */ \
 52:   PETSCTABLE   colmap;            /* local col number of off-diag col */ \
 53:   PetscInt    *garray;            /* work array */ \
 54: \
 55:   /* The following variables are used for matrix-vector products */ \
 56:   Vec        lvec;        /* local vector */ \
 57:   VecScatter Mvctx;       /* scatter context for vector */ \
 58:   PetscBool  roworiented; /* if true, row-oriented input, default true */ \
 59: \
 60:   /* The following variables are for MatGetRow() */ \
 61:   PetscInt    *rowindices; /* column indices for row */ \
 62:   PetscScalar *rowvalues;  /* nonzero values in row */ \
 63:   PetscBool    getrowactive

 65: typedef struct {
 66:   MPIAIJHEADER;
 67:   Vec       diag;
 68:   PetscInt *ld; /* number of entries per row left of diagonal block */

 70:   /* Used by device classes */
 71:   void *spptr;

 73:   struct _MatOps cops;
 74: } Mat_MPIAIJ;

 76: typedef struct {
 77:   PetscCount       n;                            /* Number of COOs passed to MatSetPreallocationCOO)() */
 78:   PetscSF          sf;                           /* SF to send/recv remote values in MatSetValuesCOO() */
 79:   PetscCount       Annz, Bnnz;                   /* Number of entries in diagonal A and off-diagonal B */
 80:   PetscCount       Annz2, Bnnz2;                 /* Number of unique remote entries belonging to A and B */
 81:   PetscCount       Atot1, Atot2, Btot1, Btot2;   /* Total local (tot1) and remote (tot2) entries (which might contain repeats) belonging to A and B */
 82:   PetscCount      *Ajmap1, *Aperm1;              /* Lengths: [Annz+1], [Atot1]. Local entries to diag */
 83:   PetscCount      *Bjmap1, *Bperm1;              /* Lengths: [Bnnz+1], [Btot1]. Local entries to offdiag */
 84:   PetscCount      *Aimap2, *Ajmap2, *Aperm2;     /* Lengths: [Annz2], [Annz2+1], [Atot2]. Remote entries to diag */
 85:   PetscCount      *Bimap2, *Bjmap2, *Bperm2;     /* Lengths: [Bnnz2], [Bnnz2+1], [Btot2]. Remote entries to offdiag */
 86:   PetscCount      *Cperm1;                       /* [sendlen] Permutation to fill MPI send buffer. 'C' for communication */
 87:   PetscScalar     *sendbuf, *recvbuf;            /* Buffers for remote values in MatSetValuesCOO() */
 88:   PetscInt         sendlen, recvlen;             /* Lengths (in unit of PetscScalar) of send/recvbuf */
 89:   PetscObjectState Anonzerostate, Bnonzerostate; /* nonzero states of A and B that the maps above index into */
 90: } MatCOOStruct_MPIAIJ;

 92: PETSC_EXTERN PetscErrorCode MatCreate_MPIAIJ(Mat);

 94: PETSC_INTERN PetscErrorCode MatAssemblyEnd_MPIAIJ(Mat, MatAssemblyType);

 96: PETSC_INTERN PetscErrorCode MatSetUpMultiply_MPIAIJ(Mat);
 97: PETSC_INTERN PetscErrorCode MatDisAssemble_MPIAIJ(Mat, PetscBool);
 98: PETSC_INTERN PetscErrorCode MatDuplicate_MPIAIJ(Mat, MatDuplicateOption, Mat *);
 99: PETSC_INTERN PetscErrorCode MatIncreaseOverlap_MPIAIJ(Mat, PetscInt, IS[], PetscInt);
100: PETSC_INTERN PetscErrorCode MatIncreaseOverlap_MPIAIJ_Scalable(Mat, PetscInt, IS[], PetscInt);
101: PETSC_INTERN PetscErrorCode MatFDColoringCreate_MPIXAIJ(Mat, ISColoring, MatFDColoring);
102: PETSC_INTERN PetscErrorCode MatFDColoringSetUp_MPIXAIJ(Mat, ISColoring, MatFDColoring);
103: PETSC_INTERN PetscErrorCode MatCreateSubMatrices_MPIAIJ(Mat, PetscInt, const IS[], const IS[], MatReuse, Mat *[]);
104: PETSC_INTERN PetscErrorCode MatCreateSubMatricesMPI_MPIAIJ(Mat, PetscInt, const IS[], const IS[], MatReuse, Mat *[]);
105: PETSC_INTERN PetscErrorCode MatCreateSubMatrix_MPIAIJ_All(Mat, MatCreateSubMatrixOption, MatReuse, Mat *[]);
106: PETSC_INTERN PetscErrorCode MatView_MPIAIJ(Mat, PetscViewer);

108: PETSC_INTERN PetscErrorCode MatCreateSubMatrix_MPIAIJ(Mat, IS, IS, MatReuse, Mat *);
109: PETSC_INTERN PetscErrorCode MatCreateSubMatrix_MPIAIJ_nonscalable(Mat, IS, IS, PetscInt, MatReuse, Mat *);
110: PETSC_INTERN PetscErrorCode MatCreateSubMatrix_MPIAIJ_SameRowDist(Mat, IS, IS, IS, MatReuse, Mat *);
111: PETSC_INTERN PetscErrorCode MatCreateSubMatrix_MPIAIJ_SameRowColDist(Mat, IS, IS, MatReuse, Mat *);
112: PETSC_INTERN PetscErrorCode MatGetMultiProcBlock_MPIAIJ(Mat, MPI_Comm, MatReuse, Mat *);

114: PETSC_INTERN PetscErrorCode MatLoad_MPIAIJ(Mat, PetscViewer);
115: PETSC_INTERN PetscErrorCode MatLoad_MPIAIJ_Binary(Mat, PetscViewer);
116: PETSC_INTERN PetscErrorCode MatCreateColmap_MPIAIJ_Private(Mat);

118: PETSC_INTERN PetscErrorCode MatProductSetFromOptions_MPIAIJ(Mat);
119: PETSC_INTERN PetscErrorCode MatProductSetFromOptions_MPIAIJBACKEND(Mat);
120: PETSC_INTERN PetscErrorCode MatProductSymbolic_MPIAIJBACKEND(Mat);
121: PETSC_INTERN PetscErrorCode MatProductSymbolic_AB_MPIAIJ_MPIAIJ(Mat);

123: PETSC_INTERN PetscErrorCode MatProductSymbolic_PtAP_MPIAIJ_MPIAIJ(Mat);

125: PETSC_INTERN PetscErrorCode MatProductSymbolic_RARt_MPIAIJ_MPIAIJ(Mat);
126: PETSC_INTERN PetscErrorCode MatProductNumeric_RARt_MPIAIJ_MPIAIJ(Mat);

128: PETSC_INTERN PetscErrorCode MatMatMultSymbolic_MPIAIJ_MPIAIJ_nonscalable(Mat, Mat, PetscReal, Mat);
129: PETSC_INTERN PetscErrorCode MatMatMultSymbolic_MPIAIJ_MPIAIJ_seqMPI(Mat, Mat, PetscReal, Mat);
130: PETSC_INTERN PetscErrorCode MatMatMultSymbolic_MPIAIJ_MPIAIJ(Mat, Mat, PetscReal, Mat);
131: PETSC_INTERN PetscErrorCode MatMatMultNumeric_MPIAIJ_MPIAIJ(Mat, Mat, Mat);
132: PETSC_INTERN PetscErrorCode MatMatMultNumeric_MPIAIJ_MPIAIJ_nonscalable(Mat, Mat, Mat);

134: PETSC_INTERN PetscErrorCode MatMatMatMultSymbolic_MPIAIJ_MPIAIJ_MPIAIJ(Mat, Mat, Mat, PetscReal, Mat);
135: PETSC_INTERN PetscErrorCode MatMatMatMultNumeric_MPIAIJ_MPIAIJ_MPIAIJ(Mat, Mat, Mat, Mat);

137: PETSC_INTERN PetscErrorCode MatPtAPSymbolic_MPIAIJ_MPIAIJ(Mat, Mat, PetscReal, Mat);
138: PETSC_INTERN PetscErrorCode MatPtAPNumeric_MPIAIJ_MPIAIJ(Mat, Mat, Mat);

140: PETSC_INTERN PetscErrorCode MatPtAPSymbolic_MPIAIJ_MPIAIJ_scalable(Mat, Mat, PetscReal, Mat);
141: PETSC_INTERN PetscErrorCode MatPtAPSymbolic_MPIAIJ_MPIAIJ_allatonce(Mat, Mat, PetscReal, Mat);
142: PETSC_INTERN PetscErrorCode MatPtAPSymbolic_MPIAIJ_MPIAIJ_allatonce_merged(Mat, Mat, PetscReal, Mat);
143: PETSC_INTERN PetscErrorCode MatPtAPNumeric_MPIAIJ_MPIAIJ_scalable(Mat, Mat, Mat);
144: PETSC_INTERN PetscErrorCode MatPtAPNumeric_MPIAIJ_MPIAIJ_allatonce(Mat, Mat, Mat);
145: PETSC_INTERN PetscErrorCode MatPtAPNumeric_MPIAIJ_MPIAIJ_allatonce_merged(Mat, Mat, Mat);

147: #if PetscDefined(HAVE_HYPRE)
148: PETSC_INTERN PetscErrorCode MatPtAPSymbolic_AIJ_AIJ_wHYPRE(Mat, Mat, PetscReal, Mat);
149: #endif
150: PETSC_INTERN PetscErrorCode MatConvert_MPIAIJ_MPIDense(Mat, MatType, MatReuse, Mat *);
151: #if PetscDefined(HAVE_SCALAPACK) && (PetscDefined(USE_REAL_SINGLE) || PetscDefined(USE_REAL_DOUBLE))
152: PETSC_INTERN PetscErrorCode MatConvert_AIJ_ScaLAPACK(Mat, MatType, MatReuse, Mat *);
153: #endif

155: PETSC_INTERN PetscErrorCode MatDestroy_MPIAIJ(Mat);
156: PETSC_INTERN PetscErrorCode MatProductCtxDestroy_MPIAIJ_PtAP(PetscCtxRt);
157: PETSC_INTERN PetscErrorCode MatProductCtxDestroy_MPIAIJ_MatMatMult(PetscCtxRt);

159: PETSC_INTERN PetscErrorCode MatGetBrowsOfAoCols_MPIAIJ(Mat, Mat, MatReuse, PetscInt **, PetscInt **, MatScalar **, Mat *);
160: PETSC_INTERN PetscErrorCode MatGetBrowsOfAoCols_MPIAIJ_Private(Mat, Mat, MatReuse, PetscBool, PetscInt **, PetscInt **, MatScalar **, Mat *);
161: PETSC_INTERN PetscErrorCode MatMPIAIJGetLocalMat_Private(Mat, MatReuse, PetscBool, Mat *);
162: PETSC_INTERN PetscErrorCode MatSetValues_MPIAIJ(Mat, PetscInt, const PetscInt[], PetscInt, const PetscInt[], const PetscScalar[], InsertMode);
163: PETSC_INTERN PetscErrorCode MatSetValues_MPIAIJ_CopyFromCSRFormat(Mat, const PetscInt[], const PetscInt[], const PetscScalar[]);
164: PETSC_INTERN PetscErrorCode MatSetValues_MPIAIJ_CopyFromCSRFormat_Symbolic(Mat, const PetscInt[], const PetscInt[]);
165: PETSC_INTERN PetscErrorCode MatSetOption_MPIAIJ(Mat, MatOption, PetscBool);

167: PETSC_INTERN PetscErrorCode MatTransposeMatMultSymbolic_MPIAIJ_MPIAIJ_nonscalable(Mat, Mat, PetscReal, Mat);
168: PETSC_INTERN PetscErrorCode MatTransposeMatMultSymbolic_MPIAIJ_MPIAIJ(Mat, Mat, PetscReal, Mat);
169: PETSC_INTERN PetscErrorCode MatTransposeMatMultNumeric_MPIAIJ_MPIAIJ(Mat, Mat, Mat);
170: PETSC_INTERN PetscErrorCode MatTransposeMatMultNumeric_MPIAIJ_MPIAIJ_nonscalable(Mat, Mat, Mat);
171: PETSC_INTERN PetscErrorCode MatTransposeMatMultNumeric_MPIAIJ_MPIAIJ_matmatmult(Mat, Mat, Mat);
172: PETSC_INTERN PetscErrorCode MatTransposeMatMultSymbolic_MPIAIJ_MPIDense(Mat, Mat, PetscReal, Mat);
173: PETSC_INTERN PetscErrorCode MatGetSeqNonzeroStructure_MPIAIJ(Mat, Mat *);

175: PETSC_INTERN PetscErrorCode MatSetFromOptions_MPIAIJ(Mat, PetscOptionItems);
176: PETSC_INTERN PetscErrorCode MatMPIAIJSetPreallocation_MPIAIJ(Mat, PetscInt, const PetscInt[], PetscInt, const PetscInt[]);

178: PETSC_INTERN PetscErrorCode MatAXPYGetPreallocation_MPIX_private(PetscInt, const PetscInt *, const PetscInt *, const PetscInt *, const PetscInt *, const PetscInt *, const PetscInt *, PetscInt *);

180: extern PetscErrorCode MatGetDiagonalBlock_MPIAIJ(Mat, Mat *);
181: extern PetscErrorCode MatDiagonalScaleLocal_MPIAIJ(Mat, Vec);

183: PETSC_INTERN PetscErrorCode MatGetSeqMats_MPIAIJ(Mat, Mat *, Mat *);
184: PETSC_INTERN PetscErrorCode MatSetSeqMats_MPIAIJ(Mat, IS, IS, IS, MatStructure, Mat, Mat);

186: PETSC_INTERN PetscErrorCode MatSetPreallocationCOO_MPIAIJ(Mat, PetscCount, PetscInt[], PetscInt[]);
187: PETSC_INTERN PetscErrorCode MatMPIAIJCheckCOONonzeroState(Mat);

189: PETSC_INTERN PetscErrorCode MatGetCurrentMemType_MPIAIJ(Mat, PetscMemType *);

191: typedef struct {
192:   Mat           workB;
193:   MPI_Request  *rwaits, *swaits;
194:   PetscInt      nsends, nrecvs;
195:   MPI_Datatype *stype, *rtype;
196:   PetscInt      blda;
197:   PetscObjectId bid;   /* identity of the last dense input checked to have the leading dimension blda */
198:   PetscSF       sf[2]; /* strided scatters for ncols[0] = workB->cmap->n columns and ncols[1] = remainder columns of the batched path, NULL if unused */
199:   PetscInt      ncols[2];
200:   PetscBool     ondevice; /* workB has a device type, use sf[] instead of the MPI derived datatypes */
201:   /* state of a split device scatter, held between MatMPIDenseScatterDeviceBegin_Private() and MatMPIDenseScatterDeviceEnd_Private() */
202:   PetscSF            sfinuse; /* the sf[] entry the outstanding scatter uses, NULL when no scatter is in flight */
203:   const PetscScalar *barray;  /* root array, the local block of B that scatter reads */
204:   PetscScalar       *warray;  /* leaf array, the workB that scatter writes */
205: } MPIAIJ_MPIDense;

207: PETSC_INTERN PetscErrorCode MatMPIDenseScatterSetUp_Private(VecScatter, PetscInt, PetscInt, PetscInt, Mat, Mat, MPIAIJ_MPIDense *, PetscInt *, PetscInt *);
208: PETSC_INTERN PetscErrorCode MatMPIDenseScatterDestroy_Private(MPIAIJ_MPIDense *);
209: PETSC_INTERN PetscErrorCode MatMPIDenseScatterUpdateLDA_Private(VecScatter, PetscInt, Mat, Mat, MPIAIJ_MPIDense *);
210: PETSC_INTERN PetscErrorCode MatMPIDenseScatter_Private(VecScatter, PetscInt, PetscInt, Mat, MPIAIJ_MPIDense *, Mat, Mat);

212: /* compute apa = A[i,:]*P = Ad[i,:]*P_loc + Ao*[i,:]*P_oth using sparse axpy */
213: #define AProw_scalable(i, ad, ao, p_loc, p_oth, api, apj, apa) \
214:   do { \
215:     PetscInt     _anz, _pnz, _j, _k, *_ai, *_aj, _row, *_pi, *_pj, _nextp, *_apJ; \
216:     PetscScalar *_aa, _valtmp, *_pa; \
217:     _apJ = (apj) + (api)[i]; \
218:     /* diagonal portion of A */ \
219:     _ai  = (ad)->i; \
220:     _anz = _ai[(i) + 1] - _ai[i]; \
221:     _aj  = PetscSafePointerPlusOffset((ad)->j, _ai[i]); \
222:     _aa  = PetscSafePointerPlusOffset((ad)->a, _ai[i]); \
223:     for (_j = 0; _j < _anz; _j++) { \
224:       _row = _aj[_j]; \
225:       _pi  = (p_loc)->i; \
226:       _pnz = _pi[_row + 1] - _pi[_row]; \
227:       _pj  = (p_loc)->j + _pi[_row]; \
228:       _pa  = (p_loc)->a + _pi[_row]; \
229:       /* perform sparse axpy */ \
230:       _valtmp = _aa[_j]; \
231:       _nextp  = 0; \
232:       for (_k = 0; _nextp < _pnz; _k++) { \
233:         if (_apJ[_k] == _pj[_nextp]) { /* column of AP == column of P */ \
234:           (apa)[_k] += _valtmp * _pa[_nextp++]; \
235:         } \
236:       } \
237:       (void)PetscLogFlops(2.0 * _pnz); \
238:     } \
239:     /* off-diagonal portion of A */ \
240:     if (p_oth) { \
241:       _ai  = (ao)->i; \
242:       _anz = _ai[(i) + 1] - _ai[i]; \
243:       _aj  = PetscSafePointerPlusOffset((ao)->j, _ai[i]); \
244:       _aa  = PetscSafePointerPlusOffset((ao)->a, _ai[i]); \
245:       for (_j = 0; _j < _anz; _j++) { \
246:         _row = _aj[_j]; \
247:         _pi  = (p_oth)->i; \
248:         _pnz = _pi[_row + 1] - _pi[_row]; \
249:         _pj  = (p_oth)->j + _pi[_row]; \
250:         _pa  = (p_oth)->a + _pi[_row]; \
251:         /* perform sparse axpy */ \
252:         _valtmp = _aa[_j]; \
253:         _nextp  = 0; \
254:         for (_k = 0; _nextp < _pnz; _k++) { \
255:           if (_apJ[_k] == _pj[_nextp]) { /* column of AP == column of P */ \
256:             (apa)[_k] += _valtmp * _pa[_nextp++]; \
257:           } \
258:         } \
259:         (void)PetscLogFlops(2.0 * _pnz); \
260:       } \
261:     } \
262:   } while (0)

264: #define AProw_nonscalable(i, ad, ao, p_loc, p_oth, apa) \
265:   do { \
266:     PetscInt     _anz, _pnz, _j, _k, *_ai, *_aj, _row, *_pi, *_pj; \
267:     PetscScalar *_aa, _valtmp, *_pa; \
268:     /* diagonal portion of A */ \
269:     _ai  = (ad)->i; \
270:     _anz = _ai[(i) + 1] - _ai[i]; \
271:     _aj  = PetscSafePointerPlusOffset((ad)->j, _ai[i]); \
272:     _aa  = PetscSafePointerPlusOffset((ad)->a, _ai[i]); \
273:     for (_j = 0; _j < _anz; _j++) { \
274:       _row = _aj[_j]; \
275:       _pi  = (p_loc)->i; \
276:       _pnz = _pi[_row + 1] - _pi[_row]; \
277:       _pj  = PetscSafePointerPlusOffset((p_loc)->j, _pi[_row]); \
278:       _pa  = PetscSafePointerPlusOffset((p_loc)->a, _pi[_row]); \
279:       /* perform dense axpy */ \
280:       _valtmp = _aa[_j]; \
281:       for (_k = 0; _k < _pnz; _k++) (apa)[_pj[_k]] += _valtmp * _pa[_k]; \
282:       (void)PetscLogFlops(2.0 * _pnz); \
283:     } \
284:     /* off-diagonal portion of A */ \
285:     if (p_oth) { \
286:       _ai  = (ao)->i; \
287:       _anz = _ai[(i) + 1] - _ai[i]; \
288:       _aj  = PetscSafePointerPlusOffset((ao)->j, _ai[i]); \
289:       _aa  = PetscSafePointerPlusOffset((ao)->a, _ai[i]); \
290:       for (_j = 0; _j < _anz; _j++) { \
291:         _row = _aj[_j]; \
292:         _pi  = (p_oth)->i; \
293:         _pnz = _pi[_row + 1] - _pi[_row]; \
294:         _pj  = PetscSafePointerPlusOffset((p_oth)->j, _pi[_row]); \
295:         _pa  = PetscSafePointerPlusOffset((p_oth)->a, _pi[_row]); \
296:         /* perform dense axpy */ \
297:         _valtmp = _aa[_j]; \
298:         for (_k = 0; _k < _pnz; _k++) (apa)[_pj[_k]] += _valtmp * _pa[_k]; \
299:         (void)PetscLogFlops(2.0 * _pnz); \
300:       } \
301:     } \
302:   } while (0)