Actual source code: sbaijov.c

  1: /*
  2:    Routines to compute overlapping regions of a parallel MPI matrix.
  3:    Used for finding submatrices that were shared across processors.
  4: */
  5: #include <../src/mat/impls/sbaij/mpi/mpisbaij.h>
  6: #include <petscbt.h>

  8: static PetscErrorCode MatIncreaseOverlap_MPISBAIJ_Once(Mat, PetscInt, IS *);
  9: static PetscErrorCode MatIncreaseOverlap_MPISBAIJ_Local(Mat, PetscInt *, PetscInt, PetscInt *, PetscBT *);

 11: PetscErrorCode MatIncreaseOverlap_MPISBAIJ(Mat C, PetscInt is_max, IS is[], PetscInt ov)
 12: {
 13:   PetscInt        i, N = C->cmap->N, bs = C->rmap->bs, nis;
 14:   IS             *is_new;
 15:   Mat             B;
 16:   const PetscInt *idx;
 17:   PetscBool       flg;

 19:   PetscFunctionBegin;
 20:   PetscCall(PetscMalloc1(is_max, &is_new));
 21:   /* Convert the indices into block format */
 22:   PetscCall(ISCompressIndicesGeneral(N, C->rmap->n, bs, is_max, is, is_new));
 23:   PetscCheck(ov >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Negative overlap specified");

 25:   /*  previous non-scalable implementation  */
 26:   flg = PETSC_FALSE;
 27:   PetscCall(PetscOptionsHasName(NULL, NULL, "-IncreaseOverlap_old", &flg));
 28:   if (flg) { /* previous non-scalable implementation */
 29:     printf("use previous non-scalable implementation...\n");
 30:     for (i = 0; i < ov; ++i) PetscCall(MatIncreaseOverlap_MPISBAIJ_Once(C, is_max, is_new));
 31:   } else if (ov && is_max) {
 32:     /* MPISBAIJ stores only the upper triangular part of the matrix. Build its full sparsity pattern once,
 33:        then use the MPIBAIJ overlap algorithm to find both row and column neighbors */
 34:     PetscCall(MatSBAIJCreateSymmetricStructure_Private(C, MATMPIBAIJ, PETSC_TRUE, &B));
 35:     for (i = 0; i < ov; ++i) PetscCall(MatIncreaseOverlap_MPIBAIJ_Once(B, is_max, is_new));
 36:     PetscCall(MatDestroy(&B));
 37:   }
 38:   for (i = 0; i < is_max; i++) {
 39:     PetscCall(ISDestroy(&is[i]));
 40:     PetscCall(ISGetLocalSize(is_new[i], &nis));
 41:     PetscCall(ISGetIndices(is_new[i], &idx));
 42:     PetscCall(ISCreateBlock(PetscObjectComm((PetscObject)is_new[i]), bs, nis, idx, PETSC_COPY_VALUES, &is[i]));
 43:     PetscCall(ISDestroy(&is_new[i]));
 44:   }
 45:   PetscCall(PetscFree(is_new));
 46:   PetscFunctionReturn(PETSC_SUCCESS);
 47: }

 49: typedef enum {
 50:   MINE,
 51:   OTHER
 52: } WhoseOwner;
 53: /*  data1, odata1 and odata2 are packed in the format (for communication):
 54:        data[0]          = is_max, no of is
 55:        data[1]          = size of is[0]
 56:         ...
 57:        data[is_max]     = size of is[is_max-1]
 58:        data[is_max + 1] = data(is[0])
 59:         ...
 60:        data[is_max+1+sum(size of is[k]), k=0,...,i-1] = data(is[i])
 61:         ...
 62:    data2 is packed in the format (for creating output is[]):
 63:        data[0]          = is_max, no of is
 64:        data[1]          = size of is[0]
 65:         ...
 66:        data[is_max]     = size of is[is_max-1]
 67:        data[is_max + 1] = data(is[0])
 68:         ...
 69:        data[is_max + 1 + Mbs*i) = data(is[i])
 70:         ...
 71: */
 72: static PetscErrorCode MatIncreaseOverlap_MPISBAIJ_Once(Mat C, PetscInt is_max, IS is[])
 73: {
 74:   Mat_MPISBAIJ   *c        = (Mat_MPISBAIJ *)C->data;
 75:   PetscMPIInt     proc_end = 0, size, rank, tag1, tag2, *len_s, nrqr, nrqs, *id_r1, *len_r1, flag, *iwork;
 76:   const PetscInt *idx_i;
 77:   PetscInt        idx, isz, col, *n, *data1, **data1_start, *data2, *data2_i, *data, *data_i, len;
 78:   PetscInt        Mbs, i, j, k, *odata1, *odata2;
 79:   PetscInt      **odata2_ptr, *ctable = NULL, *btable, len_max, len_est;
 80:   PetscInt        len_unused, nodata2;
 81:   PetscInt        ois_max; /* max no of is[] in each of processor */
 82:   PetscByte      *t_p;
 83:   MPI_Comm        comm;
 84:   MPI_Request    *s_waits1, *s_waits2, r_req;
 85:   MPI_Status     *s_status, r_status;
 86:   PetscBT        *table; /* mark indices of this processor's is[] */
 87:   PetscBT         table_i;
 88:   PetscBT         otable; /* mark indices of other processors' is[] */
 89:   PetscInt        bs = C->rmap->bs, Bn = c->B->cmap->n, Bnbs = Bn / bs, *Bowners;
 90:   IS              garray_local, garray_gl;

 92:   PetscFunctionBegin;
 93:   PetscCall(PetscObjectGetComm((PetscObject)C, &comm));
 94:   size = c->size;
 95:   rank = c->rank;
 96:   Mbs  = c->Mbs;

 98:   PetscCall(PetscObjectGetNewTag((PetscObject)C, &tag1));
 99:   PetscCall(PetscObjectGetNewTag((PetscObject)C, &tag2));

101:   /* create tables used in
102:      step 1: table[i] - mark c->garray of proc [i]
103:      step 3: table[i] - mark indices of is[i] when whose=MINE
104:              table[0] - mark incideces of is[] when whose=OTHER */
105:   len = PetscMax(is_max, size);
106:   PetscCall(PetscMalloc2(len, &table, (Mbs / PETSC_BITS_PER_BYTE + 1) * len, &t_p));
107:   for (i = 0; i < len; i++) table[i] = t_p + (Mbs / PETSC_BITS_PER_BYTE + 1) * i;

109:   PetscCallMPI(MPIU_Allreduce(&is_max, &ois_max, 1, MPIU_INT, MPI_MAX, comm));

111:   /* 1. Send this processor's is[] to other processors */
112:   /* allocate spaces */
113:   PetscCall(PetscMalloc1(is_max, &n));
114:   len = 0;
115:   for (i = 0; i < is_max; i++) {
116:     PetscCall(ISGetLocalSize(is[i], &n[i]));
117:     len += n[i];
118:   }
119:   if (!len) {
120:     is_max = 0;
121:   } else {
122:     len += 1 + is_max; /* max length of data1 for one processor */
123:   }

125:   PetscCall(PetscMalloc1(size * len + 1, &data1));
126:   PetscCall(PetscMalloc1(size, &data1_start));
127:   for (i = 0; i < size; i++) data1_start[i] = data1 + i * len;

129:   PetscCall(PetscMalloc4(size, &len_s, size, &btable, size, &iwork, size + 1, &Bowners));

131:   /* gather c->garray from all processors */
132:   PetscCall(ISCreateGeneral(comm, Bnbs, c->garray, PETSC_COPY_VALUES, &garray_local));
133:   PetscCall(ISAllGather(garray_local, &garray_gl));
134:   PetscCall(ISDestroy(&garray_local));
135:   PetscCallMPI(MPI_Allgather(&Bnbs, 1, MPIU_INT, Bowners + 1, 1, MPIU_INT, comm));

137:   Bowners[0] = 0;
138:   for (i = 0; i < size; i++) Bowners[i + 1] += Bowners[i];

140:   if (is_max) {
141:     /* hash table ctable which maps c->row to proc_id) */
142:     PetscCall(PetscMalloc1(Mbs, &ctable));
143:     j = 0;
144:     for (PetscMPIInt proc_id = 0; proc_id < size; proc_id++) {
145:       for (; j < C->rmap->range[proc_id + 1] / bs; j++) ctable[j] = proc_id;
146:     }

148:     /* hash tables marking c->garray */
149:     PetscCall(ISGetIndices(garray_gl, &idx_i));
150:     for (i = 0; i < size; i++) {
151:       table_i = table[i];
152:       PetscCall(PetscBTMemzero(Mbs, table_i));
153:       for (j = Bowners[i]; j < Bowners[i + 1]; j++) { /* go through B cols of proc[i]*/
154:         PetscCall(PetscBTSet(table_i, idx_i[j]));
155:       }
156:     }
157:     PetscCall(ISRestoreIndices(garray_gl, &idx_i));
158:   } /* if (is_max) */
159:   PetscCall(ISDestroy(&garray_gl));

161:   /* evaluate communication - mesg to who, length, and buffer space */
162:   for (i = 0; i < size; i++) len_s[i] = 0;

164:   /* header of data1 */
165:   for (PetscMPIInt proc_id = 0; proc_id < size; proc_id++) {
166:     iwork[proc_id]        = 0;
167:     *data1_start[proc_id] = is_max;
168:     data1_start[proc_id]++;
169:     for (j = 0; j < is_max; j++) {
170:       if (proc_id == rank) {
171:         *data1_start[proc_id] = n[j];
172:       } else {
173:         *data1_start[proc_id] = 0;
174:       }
175:       data1_start[proc_id]++;
176:     }
177:   }

179:   for (i = 0; i < is_max; i++) {
180:     PetscCall(ISGetIndices(is[i], &idx_i));
181:     for (j = 0; j < n[i]; j++) {
182:       idx                = idx_i[j];
183:       *data1_start[rank] = idx;
184:       data1_start[rank]++; /* for local processing */
185:       PetscCall(PetscMPIIntCast(ctable[idx], &proc_end));
186:       for (PetscMPIInt proc_id = 0; proc_id <= proc_end; proc_id++) {            /* for others to process */
187:         if (proc_id == rank) continue;                                           /* done before this loop */
188:         if (proc_id < proc_end && !PetscBTLookup(table[proc_id], idx)) continue; /* no need for sending idx to [proc_id] */
189:         *data1_start[proc_id] = idx;
190:         data1_start[proc_id]++;
191:         len_s[proc_id]++;
192:       }
193:     }
194:     /* update header data */
195:     for (PetscMPIInt proc_id = 0; proc_id < size; proc_id++) {
196:       if (proc_id == rank) continue;
197:       *(data1 + proc_id * len + 1 + i) = len_s[proc_id] - iwork[proc_id];
198:       iwork[proc_id]                   = len_s[proc_id];
199:     }
200:     PetscCall(ISRestoreIndices(is[i], &idx_i));
201:   }

203:   nrqs = 0;
204:   nrqr = 0;
205:   for (i = 0; i < size; i++) {
206:     data1_start[i] = data1 + i * len;
207:     if (len_s[i]) {
208:       nrqs++;
209:       len_s[i] += 1 + is_max; /* add no. of header msg */
210:     }
211:   }

213:   for (i = 0; i < is_max; i++) PetscCall(ISDestroy(&is[i]));
214:   PetscCall(PetscFree(n));
215:   PetscCall(PetscFree(ctable));

217:   /* Determine the number of messages to expect, their lengths, from from-ids */
218:   PetscCall(PetscGatherNumberOfMessages(comm, NULL, len_s, &nrqr));
219:   PetscCall(PetscGatherMessageLengths(comm, nrqs, nrqr, len_s, &id_r1, &len_r1));

221:   /*  Now  post the sends */
222:   PetscCall(PetscMalloc2(size, &s_waits1, size, &s_waits2));
223:   k = 0;
224:   for (PetscMPIInt proc_id = 0; proc_id < size; proc_id++) { /* send data1 to processor [proc_id] */
225:     if (len_s[proc_id]) {
226:       PetscCallMPI(MPIU_Isend(data1_start[proc_id], len_s[proc_id], MPIU_INT, proc_id, tag1, comm, s_waits1 + k));
227:       k++;
228:     }
229:   }

231:   /* 2. Receive other's is[] and process. Then send back */
232:   len = 0;
233:   for (i = 0; i < nrqr; i++) {
234:     if (len_r1[i] > len) len = len_r1[i];
235:   }
236:   PetscCall(PetscFree(len_r1));
237:   PetscCall(PetscFree(id_r1));

239:   for (PetscMPIInt proc_id = 0; proc_id < size; proc_id++) len_s[proc_id] = iwork[proc_id] = 0;

241:   PetscCall(PetscMalloc1(len + 1, &odata1));
242:   PetscCall(PetscMalloc1(size, &odata2_ptr));
243:   PetscCall(PetscBTCreate(Mbs, &otable));

245:   len_max = ois_max * (Mbs + 1); /* max space storing all is[] for each receive */
246:   len_est = 2 * len_max;         /* estimated space of storing is[] for all receiving messages */
247:   PetscCall(PetscMalloc1(len_est + 1, &odata2));
248:   nodata2 = 0; /* nodata2+1: num of PetscMalloc(,&odata2_ptr[]) called */

250:   odata2_ptr[nodata2] = odata2;

252:   len_unused = len_est; /* unused space in the array odata2_ptr[nodata2]-- needs to be >= len_max  */

254:   k = 0;
255:   while (k < nrqr) {
256:     PetscMPIInt ilen;

258:     /* Receive messages */
259:     PetscCallMPI(MPI_Iprobe(MPI_ANY_SOURCE, tag1, comm, &flag, &r_status));
260:     if (flag) {
261:       PetscMPIInt proc_id;

263:       PetscCallMPI(MPI_Get_count(&r_status, MPIU_INT, &ilen));
264:       proc_id = r_status.MPI_SOURCE;
265:       PetscCallMPI(MPIU_Irecv(odata1, ilen, MPIU_INT, proc_id, r_status.MPI_TAG, comm, &r_req));
266:       PetscCallMPI(MPI_Wait(&r_req, &r_status));

268:       /*  Process messages */
269:       /*  make sure there is enough unused space in odata2 array */
270:       if (len_unused < len_max) { /* allocate more space for odata2 */
271:         PetscCall(PetscMalloc1(len_est + 1, &odata2));
272:         odata2_ptr[++nodata2] = odata2;
273:         len_unused            = len_est;
274:       }

276:       PetscCall(MatIncreaseOverlap_MPISBAIJ_Local(C, odata1, OTHER, odata2, &otable));
277:       len = 1 + odata2[0];
278:       for (i = 0; i < odata2[0]; i++) len += odata2[1 + i];

280:       /* Send messages back */
281:       PetscCallMPI(MPIU_Isend(odata2, len, MPIU_INT, proc_id, tag2, comm, s_waits2 + k));
282:       k++;
283:       odata2 += len;
284:       len_unused -= len;
285:       PetscCall(PetscMPIIntCast(len, &iwork[proc_id])); /* length of message sending back to proc_id */
286:     }
287:   }
288:   PetscCall(PetscFree(odata1));
289:   PetscCall(PetscBTDestroy(&otable));

291:   /* 3. Do local work on this processor's is[] */
292:   /* make sure there is enough unused space in odata2(=data) array */
293:   len_max = is_max * (Mbs + 1); /* max space storing all is[] for this processor */
294:   if (len_unused < len_max) {   /* allocate more space for odata2 */
295:     PetscCall(PetscMalloc1(len_est + 1, &odata2));

297:     odata2_ptr[++nodata2] = odata2;
298:   }

300:   data = odata2;
301:   PetscCall(MatIncreaseOverlap_MPISBAIJ_Local(C, data1_start[rank], MINE, data, table));
302:   PetscCall(PetscFree(data1_start));

304:   /* 4. Receive work done on other processors, then merge */
305:   /* get max number of messages that this processor expects to recv */
306:   PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, iwork, size, MPI_INT, MPI_MAX, comm));
307:   PetscCall(PetscMalloc1(iwork[rank] + 1, &data2));
308:   PetscCall(PetscFree4(len_s, btable, iwork, Bowners));

310:   k = 0;
311:   while (k < nrqs) {
312:     /* Receive messages */
313:     PetscCallMPI(MPI_Iprobe(MPI_ANY_SOURCE, tag2, comm, &flag, &r_status));
314:     if (flag) {
315:       PetscMPIInt proc_id, ilen;
316:       PetscCallMPI(MPI_Get_count(&r_status, MPIU_INT, &ilen));
317:       proc_id = r_status.MPI_SOURCE;
318:       PetscCallMPI(MPIU_Irecv(data2, ilen, MPIU_INT, proc_id, r_status.MPI_TAG, comm, &r_req));
319:       PetscCallMPI(MPI_Wait(&r_req, &r_status));
320:       if (ilen > 1 + is_max) { /* Add data2 into data */
321:         data2_i = data2 + 1 + is_max;
322:         for (i = 0; i < is_max; i++) {
323:           table_i = table[i];
324:           data_i  = data + 1 + is_max + Mbs * i;
325:           isz     = data[1 + i];
326:           for (j = 0; j < data2[1 + i]; j++) {
327:             col = data2_i[j];
328:             if (!PetscBTLookupSet(table_i, col)) data_i[isz++] = col;
329:           }
330:           data[1 + i] = isz;
331:           if (i < is_max - 1) data2_i += data2[1 + i];
332:         }
333:       }
334:       k++;
335:     }
336:   }
337:   PetscCall(PetscFree(data2));
338:   PetscCall(PetscFree2(table, t_p));

340:   /* phase 1 sends are complete */
341:   PetscCall(PetscMalloc1(size, &s_status));
342:   if (nrqs) PetscCallMPI(MPI_Waitall(nrqs, s_waits1, s_status));
343:   PetscCall(PetscFree(data1));

345:   /* phase 2 sends are complete */
346:   if (nrqr) PetscCallMPI(MPI_Waitall(nrqr, s_waits2, s_status));
347:   PetscCall(PetscFree2(s_waits1, s_waits2));
348:   PetscCall(PetscFree(s_status));

350:   /* 5. Create new is[] */
351:   for (i = 0; i < is_max; i++) {
352:     data_i = data + 1 + is_max + Mbs * i;
353:     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, data[1 + i], data_i, PETSC_COPY_VALUES, is + i));
354:   }
355:   for (k = 0; k <= nodata2; k++) PetscCall(PetscFree(odata2_ptr[k]));
356:   PetscCall(PetscFree(odata2_ptr));
357:   PetscFunctionReturn(PETSC_SUCCESS);
358: }

360: /*
361:    MatIncreaseOverlap_MPISBAIJ_Local - Called by MatIncreaseOverlap, to do
362:        the work on the local processor.

364:      Inputs:
365:       C      - MAT_MPISBAIJ;
366:       data   - holds is[]. See MatIncreaseOverlap_MPISBAIJ_Once() for the format.
367:       whose  - whose is[] to be processed,
368:                MINE:  this processor's is[]
369:                OTHER: other processor's is[]
370:      Output:
371:        nidx  - whose = MINE:
372:                      holds input and newly found indices in the same format as data
373:                whose = OTHER:
374:                      only holds the newly found indices
375:        table - table[i]: mark the indices of is[i], i=0,...,is_max. Used only in the case 'whose=MINE'.
376: */
377: /* Would computation be reduced by swapping the loop 'for each is' and 'for each row'? */
378: static PetscErrorCode MatIncreaseOverlap_MPISBAIJ_Local(Mat C, PetscInt *data, PetscInt whose, PetscInt *nidx, PetscBT *table)
379: {
380:   Mat_MPISBAIJ *c = (Mat_MPISBAIJ *)C->data;
381:   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ *)c->A->data;
382:   Mat_SeqBAIJ  *b = (Mat_SeqBAIJ *)c->B->data;
383:   PetscInt      row, mbs, Mbs, *nidx_i, col, col_max, isz, isz0, *ai, *aj, *bi, *bj, *garray, rstart, l;
384:   PetscInt      a_start, a_end, b_start, b_end, i, j, k, is_max, *idx_i, n;
385:   PetscBT       table0;  /* mark the indices of input is[] for look up */
386:   PetscBT       table_i; /* points to i-th table. When whose=OTHER, a single table is used for all is[] */

388:   PetscFunctionBegin;
389:   Mbs    = c->Mbs;
390:   mbs    = a->mbs;
391:   ai     = a->i;
392:   aj     = a->j;
393:   bi     = b->i;
394:   bj     = b->j;
395:   garray = c->garray;
396:   rstart = c->rstartbs;
397:   is_max = data[0];

399:   PetscCall(PetscBTCreate(Mbs, &table0));

401:   nidx[0] = is_max;
402:   idx_i   = data + is_max + 1;   /* ptr to input is[0] array */
403:   nidx_i  = nidx + is_max + 1;   /* ptr to output is[0] array */
404:   for (i = 0; i < is_max; i++) { /* for each is */
405:     isz = 0;
406:     n   = data[1 + i]; /* size of input is[i] */

408:     /* initialize and set table_i(mark idx and nidx) and table0(only mark idx) */
409:     if (whose == MINE) { /* process this processor's is[] */
410:       table_i = table[i];
411:       nidx_i  = nidx + 1 + is_max + Mbs * i;
412:     } else { /* process other processor's is[] - only use one temp table */
413:       table_i = table[0];
414:     }
415:     PetscCall(PetscBTMemzero(Mbs, table_i));
416:     PetscCall(PetscBTMemzero(Mbs, table0));
417:     if (n == 0) {
418:       nidx[1 + i] = 0; /* size of new is[i] */
419:       continue;
420:     }

422:     isz0    = 0;
423:     col_max = 0;
424:     for (j = 0; j < n; j++) {
425:       col = idx_i[j];
426:       PetscCheck(col < Mbs, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "index col %" PetscInt_FMT " >= Mbs %" PetscInt_FMT, col, Mbs);
427:       if (!PetscBTLookupSet(table_i, col)) {
428:         PetscCall(PetscBTSet(table0, col));
429:         if (whose == MINE) nidx_i[isz0] = col;
430:         if (col_max < col) col_max = col;
431:         isz0++;
432:       }
433:     }

435:     if (whose == MINE) isz = isz0;
436:     k = 0; /* no. of indices from input is[i] that have been examined */
437:     for (row = 0; row < mbs; row++) {
438:       a_start = ai[row];
439:       a_end   = ai[row + 1];
440:       b_start = bi[row];
441:       b_end   = bi[row + 1];
442:       if (PetscBTLookup(table0, row + rstart)) { /* row is on input is[i]:
443:                                                 do row search: collect all col in this row */
444:         for (l = a_start; l < a_end; l++) {      /* Amat */
445:           col = aj[l] + rstart;
446:           if (!PetscBTLookupSet(table_i, col)) nidx_i[isz++] = col;
447:         }
448:         for (l = b_start; l < b_end; l++) { /* Bmat */
449:           col = garray[bj[l]];
450:           if (!PetscBTLookupSet(table_i, col)) nidx_i[isz++] = col;
451:         }
452:         k++;
453:         if (k >= isz0) break;               /* for (row=0; row<mbs; row++) */
454:       } else {                              /* row is not on input is[i]:
455:                   do col search: add row onto nidx_i if there is a col in nidx_i */
456:         for (l = a_start; l < a_end; l++) { /* Amat */
457:           col = aj[l] + rstart;
458:           if (col > col_max) break;
459:           if (PetscBTLookup(table0, col)) {
460:             if (!PetscBTLookupSet(table_i, row + rstart)) nidx_i[isz++] = row + rstart;
461:             break; /* for l = start; l<end ; l++) */
462:           }
463:         }
464:         for (l = b_start; l < b_end; l++) { /* Bmat */
465:           col = garray[bj[l]];
466:           if (col > col_max) break;
467:           if (PetscBTLookup(table0, col)) {
468:             if (!PetscBTLookupSet(table_i, row + rstart)) nidx_i[isz++] = row + rstart;
469:             break; /* for l = start; l<end ; l++) */
470:           }
471:         }
472:       }
473:     }

475:     if (i < is_max - 1) {
476:       idx_i += n;    /* ptr to input is[i+1] array */
477:       nidx_i += isz; /* ptr to output is[i+1] array */
478:     }
479:     nidx[1 + i] = isz; /* size of new is[i] */
480:   } /* for each is */
481:   PetscCall(PetscBTDestroy(&table0));
482:   PetscFunctionReturn(PETSC_SUCCESS);
483: }