Actual source code: isltog.c

petsc-3.10.5 2019-03-28
Report Typos and Errors

  2:  #include <petsc/private/isimpl.h>
  3:  #include <petsc/private/hashmapi.h>
  4:  #include <petscsf.h>
  5:  #include <petscviewer.h>

  7: PetscClassId IS_LTOGM_CLASSID;
  8: static PetscErrorCode  ISLocalToGlobalMappingGetBlockInfo_Private(ISLocalToGlobalMapping,PetscInt*,PetscInt**,PetscInt**,PetscInt***);

 10: typedef struct {
 11:   PetscInt *globals;
 12: } ISLocalToGlobalMapping_Basic;

 14: typedef struct {
 15:   PetscHMapI globalht;
 16: } ISLocalToGlobalMapping_Hash;


 19: PetscErrorCode ISGetPointRange(IS pointIS, PetscInt *pStart, PetscInt *pEnd, const PetscInt **points)
 20: {
 21:   PetscInt       numCells, step = 1;
 22:   PetscBool      isStride;

 26:   *pStart = 0;
 27:   *points = NULL;
 28:   ISGetLocalSize(pointIS, &numCells);
 29:   PetscObjectTypeCompare((PetscObject) pointIS, ISSTRIDE, &isStride);
 30:   if (isStride) {ISStrideGetInfo(pointIS, pStart, &step);}
 31:   *pEnd   = *pStart + numCells;
 32:   if (!isStride || step != 1) {ISGetIndices(pointIS, points);}
 33:   return(0);
 34: }

 36: PetscErrorCode ISRestorePointRange(IS pointIS, PetscInt *pStart, PetscInt *pEnd, const PetscInt **points)
 37: {
 38:   PetscInt       step = 1;
 39:   PetscBool      isStride;

 43:   PetscObjectTypeCompare((PetscObject) pointIS, ISSTRIDE, &isStride);
 44:   if (isStride) {ISStrideGetInfo(pointIS, pStart, &step);}
 45:   if (!isStride || step != 1) {ISGetIndices(pointIS, points);}
 46:   return(0);
 47: }

 49: PetscErrorCode ISGetPointSubrange(IS subpointIS, PetscInt pStart, PetscInt pEnd, const PetscInt *points)
 50: {

 54:   if (points) {
 55:     ISSetType(subpointIS, ISGENERAL);
 56:     ISGeneralSetIndices(subpointIS, pEnd-pStart, &points[pStart], PETSC_USE_POINTER);
 57:   } else {
 58:     ISSetType(subpointIS, ISSTRIDE);
 59:     ISStrideSetStride(subpointIS, pEnd-pStart, pStart, 1);
 60:   }
 61:   return(0);
 62: }

 64: /* -----------------------------------------------------------------------------------------*/

 66: /*
 67:     Creates the global mapping information in the ISLocalToGlobalMapping structure

 69:     If the user has not selected how to handle the global to local mapping then use HASH for "large" problems
 70: */
 71: static PetscErrorCode ISGlobalToLocalMappingSetUp(ISLocalToGlobalMapping mapping)
 72: {
 73:   PetscInt       i,*idx = mapping->indices,n = mapping->n,end,start;

 77:   if (mapping->data) return(0);
 78:   end   = 0;
 79:   start = PETSC_MAX_INT;

 81:   for (i=0; i<n; i++) {
 82:     if (idx[i] < 0) continue;
 83:     if (idx[i] < start) start = idx[i];
 84:     if (idx[i] > end)   end   = idx[i];
 85:   }
 86:   if (start > end) {start = 0; end = -1;}
 87:   mapping->globalstart = start;
 88:   mapping->globalend   = end;
 89:   if (!((PetscObject)mapping)->type_name) {
 90:     if ((end - start) > PetscMax(4*n,1000000)) {
 91:       ISLocalToGlobalMappingSetType(mapping,ISLOCALTOGLOBALMAPPINGHASH);
 92:     } else {
 93:       ISLocalToGlobalMappingSetType(mapping,ISLOCALTOGLOBALMAPPINGBASIC);
 94:     }
 95:   }
 96:   (*mapping->ops->globaltolocalmappingsetup)(mapping);
 97:   return(0);
 98: }

100: static PetscErrorCode ISGlobalToLocalMappingSetUp_Basic(ISLocalToGlobalMapping mapping)
101: {
102:   PetscErrorCode              ierr;
103:   PetscInt                    i,*idx = mapping->indices,n = mapping->n,end,start,*globals;
104:   ISLocalToGlobalMapping_Basic *map;

107:   start            = mapping->globalstart;
108:   end              = mapping->globalend;
109:   PetscNew(&map);
110:   PetscMalloc1(end-start+2,&globals);
111:   map->globals     = globals;
112:   for (i=0; i<end-start+1; i++) globals[i] = -1;
113:   for (i=0; i<n; i++) {
114:     if (idx[i] < 0) continue;
115:     globals[idx[i] - start] = i;
116:   }
117:   mapping->data = (void*)map;
118:   PetscLogObjectMemory((PetscObject)mapping,(end-start+1)*sizeof(PetscInt));
119:   return(0);
120: }

122: static PetscErrorCode ISGlobalToLocalMappingSetUp_Hash(ISLocalToGlobalMapping mapping)
123: {
124:   PetscErrorCode              ierr;
125:   PetscInt                    i,*idx = mapping->indices,n = mapping->n;
126:   ISLocalToGlobalMapping_Hash *map;

129:   PetscNew(&map);
130:   PetscHMapICreate(&map->globalht);
131:   for (i=0; i<n; i++ ) {
132:     if (idx[i] < 0) continue;
133:     PetscHMapISet(map->globalht,idx[i],i);
134:   }
135:   mapping->data = (void*)map;
136:   PetscLogObjectMemory((PetscObject)mapping,2*n*sizeof(PetscInt));
137:   return(0);
138: }

140: static PetscErrorCode ISLocalToGlobalMappingDestroy_Basic(ISLocalToGlobalMapping mapping)
141: {
142:   PetscErrorCode              ierr;
143:   ISLocalToGlobalMapping_Basic *map  = (ISLocalToGlobalMapping_Basic *)mapping->data;

146:   if (!map) return(0);
147:   PetscFree(map->globals);
148:   PetscFree(mapping->data);
149:   return(0);
150: }

152: static PetscErrorCode ISLocalToGlobalMappingDestroy_Hash(ISLocalToGlobalMapping mapping)
153: {
154:   PetscErrorCode              ierr;
155:   ISLocalToGlobalMapping_Hash *map  = (ISLocalToGlobalMapping_Hash*)mapping->data;

158:   if (!map) return(0);
159:   PetscHMapIDestroy(&map->globalht);
160:   PetscFree(mapping->data);
161:   return(0);
162: }

164: #define GTOLTYPE _Basic
165: #define GTOLNAME _Basic
166: #define GTOLBS mapping->bs
167: #define GTOL(g, local) do {                  \
168:     local = map->globals[g/bs - start];      \
169:     local = bs*local + (g % bs);             \
170:   } while (0)

172:  #include <../src/vec/is/utils/isltog.h>

174: #define GTOLTYPE _Basic
175: #define GTOLNAME Block_Basic
176: #define GTOLBS 1
177: #define GTOL(g, local) do {                  \
178:     local = map->globals[g - start];         \
179:   } while (0)
180:  #include <../src/vec/is/utils/isltog.h>

182: #define GTOLTYPE _Hash
183: #define GTOLNAME _Hash
184: #define GTOLBS mapping->bs
185: #define GTOL(g, local) do {                         \
186:     (void)PetscHMapIGet(map->globalht,g/bs,&local); \
187:     local = bs*local + (g % bs);                    \
188:    } while (0)
189:  #include <../src/vec/is/utils/isltog.h>

191: #define GTOLTYPE _Hash
192: #define GTOLNAME Block_Hash
193: #define GTOLBS 1
194: #define GTOL(g, local) do {                         \
195:     (void)PetscHMapIGet(map->globalht,g,&local);    \
196:   } while (0)
197:  #include <../src/vec/is/utils/isltog.h>

199: /*@
200:     ISLocalToGlobalMappingDuplicate - Duplicates the local to global mapping object

202:     Not Collective

204:     Input Parameter:
205: .   ltog - local to global mapping

207:     Output Parameter:
208: .   nltog - the duplicated local to global mapping

210:     Level: advanced

212:     Concepts: mapping^local to global

214: .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate()
215: @*/
216: PetscErrorCode  ISLocalToGlobalMappingDuplicate(ISLocalToGlobalMapping ltog,ISLocalToGlobalMapping* nltog)
217: {

222:   ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)ltog),ltog->bs,ltog->n,ltog->indices,PETSC_COPY_VALUES,nltog);
223:   return(0);
224: }

226: /*@
227:     ISLocalToGlobalMappingGetSize - Gets the local size of a local to global mapping

229:     Not Collective

231:     Input Parameter:
232: .   ltog - local to global mapping

234:     Output Parameter:
235: .   n - the number of entries in the local mapping, ISLocalToGlobalMappingGetIndices() returns an array of this length

237:     Level: advanced

239:     Concepts: mapping^local to global

241: .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate()
242: @*/
243: PetscErrorCode  ISLocalToGlobalMappingGetSize(ISLocalToGlobalMapping mapping,PetscInt *n)
244: {
248:   *n = mapping->bs*mapping->n;
249:   return(0);
250: }

252: /*@C
253:     ISLocalToGlobalMappingView - View a local to global mapping

255:     Not Collective

257:     Input Parameters:
258: +   ltog - local to global mapping
259: -   viewer - viewer

261:     Level: advanced

263:     Concepts: mapping^local to global

265: .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate()
266: @*/
267: PetscErrorCode  ISLocalToGlobalMappingView(ISLocalToGlobalMapping mapping,PetscViewer viewer)
268: {
269:   PetscInt       i;
270:   PetscMPIInt    rank;
271:   PetscBool      iascii;

276:   if (!viewer) {
277:     PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)mapping),&viewer);
278:   }

281:   MPI_Comm_rank(PetscObjectComm((PetscObject)mapping),&rank);
282:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
283:   if (iascii) {
284:     PetscObjectPrintClassNamePrefixType((PetscObject)mapping,viewer);
285:     PetscViewerASCIIPushSynchronized(viewer);
286:     for (i=0; i<mapping->n; i++) {
287:       PetscViewerASCIISynchronizedPrintf(viewer,"[%d] %D %D\n",rank,i,mapping->indices[i]);
288:     }
289:     PetscViewerFlush(viewer);
290:     PetscViewerASCIIPopSynchronized(viewer);
291:   }
292:   return(0);
293: }

295: /*@
296:     ISLocalToGlobalMappingCreateIS - Creates a mapping between a local (0 to n)
297:     ordering and a global parallel ordering.

299:     Not collective

301:     Input Parameter:
302: .   is - index set containing the global numbers for each local number

304:     Output Parameter:
305: .   mapping - new mapping data structure

307:     Notes:
308:     the block size of the IS determines the block size of the mapping
309:     Level: advanced

311:     Concepts: mapping^local to global

313: .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate(), ISLocalToGlobalMappingSetFromOptions()
314: @*/
315: PetscErrorCode  ISLocalToGlobalMappingCreateIS(IS is,ISLocalToGlobalMapping *mapping)
316: {
318:   PetscInt       n,bs;
319:   const PetscInt *indices;
320:   MPI_Comm       comm;
321:   PetscBool      isblock;


327:   PetscObjectGetComm((PetscObject)is,&comm);
328:   ISGetLocalSize(is,&n);
329:   PetscObjectTypeCompare((PetscObject)is,ISBLOCK,&isblock);
330:   if (!isblock) {
331:     ISGetIndices(is,&indices);
332:     ISLocalToGlobalMappingCreate(comm,1,n,indices,PETSC_COPY_VALUES,mapping);
333:     ISRestoreIndices(is,&indices);
334:   } else {
335:     ISGetBlockSize(is,&bs);
336:     ISBlockGetIndices(is,&indices);
337:     ISLocalToGlobalMappingCreate(comm,bs,n/bs,indices,PETSC_COPY_VALUES,mapping);
338:     ISBlockRestoreIndices(is,&indices);
339:   }
340:   return(0);
341: }

343: /*@C
344:     ISLocalToGlobalMappingCreateSF - Creates a mapping between a local (0 to n)
345:     ordering and a global parallel ordering.

347:     Collective

349:     Input Parameter:
350: +   sf - star forest mapping contiguous local indices to (rank, offset)
351: -   start - first global index on this process

353:     Output Parameter:
354: .   mapping - new mapping data structure

356:     Level: advanced

358:     Concepts: mapping^local to global

360: .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate(), ISLocalToGlobalMappingCreateIS(), ISLocalToGlobalMappingSetFromOptions()
361: @*/
362: PetscErrorCode ISLocalToGlobalMappingCreateSF(PetscSF sf,PetscInt start,ISLocalToGlobalMapping *mapping)
363: {
365:   PetscInt       i,maxlocal,nroots,nleaves,*globals,*ltog;
366:   const PetscInt *ilocal;
367:   MPI_Comm       comm;


373:   PetscObjectGetComm((PetscObject)sf,&comm);
374:   PetscSFGetGraph(sf,&nroots,&nleaves,&ilocal,NULL);
375:   if (ilocal) {
376:     for (i=0,maxlocal=0; i<nleaves; i++) maxlocal = PetscMax(maxlocal,ilocal[i]+1);
377:   }
378:   else maxlocal = nleaves;
379:   PetscMalloc1(nroots,&globals);
380:   PetscMalloc1(maxlocal,&ltog);
381:   for (i=0; i<nroots; i++) globals[i] = start + i;
382:   for (i=0; i<maxlocal; i++) ltog[i] = -1;
383:   PetscSFBcastBegin(sf,MPIU_INT,globals,ltog);
384:   PetscSFBcastEnd(sf,MPIU_INT,globals,ltog);
385:   ISLocalToGlobalMappingCreate(comm,1,maxlocal,ltog,PETSC_OWN_POINTER,mapping);
386:   PetscFree(globals);
387:   return(0);
388: }

390: /*@
391:     ISLocalToGlobalMappingSetBlockSize - Sets the blocksize of the mapping

393:     Not collective

395:     Input Parameters:
396: .   mapping - mapping data structure
397: .   bs - the blocksize

399:     Level: advanced

401:     Concepts: mapping^local to global

403: .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreateIS()
404: @*/
405: PetscErrorCode  ISLocalToGlobalMappingSetBlockSize(ISLocalToGlobalMapping mapping,PetscInt bs)
406: {
407:   PetscInt       *nid;
408:   const PetscInt *oid;
409:   PetscInt       i,cn,on,obs,nn;

414:   if (bs < 1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid block size %D",bs);
415:   if (bs == mapping->bs) return(0);
416:   on  = mapping->n;
417:   obs = mapping->bs;
418:   oid = mapping->indices;
419:   nn  = (on*obs)/bs;
420:   if ((on*obs)%bs) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Block size %D is inconsistent with block size %D and number of block indices %D",bs,obs,on);

422:   PetscMalloc1(nn,&nid);
423:   ISLocalToGlobalMappingGetIndices(mapping,&oid);
424:   for (i=0;i<nn;i++) {
425:     PetscInt j;
426:     for (j=0,cn=0;j<bs-1;j++) {
427:       if (oid[i*bs+j] < 0) { cn++; continue; }
428:       if (oid[i*bs+j] != oid[i*bs+j+1]-1) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Block sizes %D and %D are incompatible with the block indices: non consecutive indices %D %D",bs,obs,oid[i*bs+j],oid[i*bs+j+1]);
429:     }
430:     if (oid[i*bs+j] < 0) cn++;
431:     if (cn) {
432:       if (cn != bs) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Block sizes %D and %D are incompatible with the block indices: invalid number of negative entries in block %D",bs,obs,cn);
433:       nid[i] = -1;
434:     } else {
435:       nid[i] = oid[i*bs]/bs;
436:     }
437:   }
438:   ISLocalToGlobalMappingRestoreIndices(mapping,&oid);

440:   mapping->n           = nn;
441:   mapping->bs          = bs;
442:   PetscFree(mapping->indices);
443:   mapping->indices     = nid;
444:   mapping->globalstart = 0;
445:   mapping->globalend   = 0;

447:   /* reset the cached information */
448:   PetscFree(mapping->info_procs);
449:   PetscFree(mapping->info_numprocs);
450:   if (mapping->info_indices) {
451:     PetscInt i;

453:     PetscFree((mapping->info_indices)[0]);
454:     for (i=1; i<mapping->info_nproc; i++) {
455:       PetscFree(mapping->info_indices[i]);
456:     }
457:     PetscFree(mapping->info_indices);
458:   }
459:   mapping->info_cached = PETSC_FALSE;

461:   if (mapping->ops->destroy) {
462:     (*mapping->ops->destroy)(mapping);
463:   }
464:   return(0);
465: }

467: /*@
468:     ISLocalToGlobalMappingGetBlockSize - Gets the blocksize of the mapping
469:     ordering and a global parallel ordering.

471:     Not Collective

473:     Input Parameters:
474: .   mapping - mapping data structure

476:     Output Parameter:
477: .   bs - the blocksize

479:     Level: advanced

481:     Concepts: mapping^local to global

483: .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreateIS()
484: @*/
485: PetscErrorCode  ISLocalToGlobalMappingGetBlockSize(ISLocalToGlobalMapping mapping,PetscInt *bs)
486: {
489:   *bs = mapping->bs;
490:   return(0);
491: }

493: /*@
494:     ISLocalToGlobalMappingCreate - Creates a mapping between a local (0 to n)
495:     ordering and a global parallel ordering.

497:     Not Collective, but communicator may have more than one process

499:     Input Parameters:
500: +   comm - MPI communicator
501: .   bs - the block size
502: .   n - the number of local elements divided by the block size, or equivalently the number of block indices
503: .   indices - the global index for each local element, these do not need to be in increasing order (sorted), these values should not be scaled (i.e. multiplied) by the blocksize bs
504: -   mode - see PetscCopyMode

506:     Output Parameter:
507: .   mapping - new mapping data structure

509:     Notes:
510:     There is one integer value in indices per block and it represents the actual indices bs*idx + j, where j=0,..,bs-1

512:     For "small" problems when using ISGlobalToLocalMappingApply() and ISGlobalToLocalMappingApplyBlock(), the ISLocalToGlobalMappingType of ISLOCALTOGLOBALMAPPINGBASIC will be used;
513:     this uses more memory but is faster; this approach is not scalable for extremely large mappings. For large problems ISLOCALTOGLOBALMAPPINGHASH is used, this is scalable.
514:     Use ISLocalToGlobalMappingSetType() or call ISLocalToGlobalMappingSetFromOptions() with the option -islocaltoglobalmapping_type <basic,hash> to control which is used.

516:     Level: advanced

518:     Concepts: mapping^local to global

520: .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreateIS(), ISLocalToGlobalMappingSetFromOptions(), ISLOCALTOGLOBALMAPPINGBASIC, ISLOCALTOGLOBALMAPPINGHASH
521:           ISLocalToGlobalMappingSetType(), ISLocalToGlobalMappingType
522: @*/
523: PetscErrorCode  ISLocalToGlobalMappingCreate(MPI_Comm comm,PetscInt bs,PetscInt n,const PetscInt indices[],PetscCopyMode mode,ISLocalToGlobalMapping *mapping)
524: {
526:   PetscInt       *in;


532:   *mapping = NULL;
533:   ISInitializePackage();

535:   PetscHeaderCreate(*mapping,IS_LTOGM_CLASSID,"ISLocalToGlobalMapping","Local to global mapping","IS",
536:                            comm,ISLocalToGlobalMappingDestroy,ISLocalToGlobalMappingView);
537:   (*mapping)->n             = n;
538:   (*mapping)->bs            = bs;
539:   (*mapping)->info_cached   = PETSC_FALSE;
540:   (*mapping)->info_free     = PETSC_FALSE;
541:   (*mapping)->info_procs    = NULL;
542:   (*mapping)->info_numprocs = NULL;
543:   (*mapping)->info_indices  = NULL;
544:   (*mapping)->info_nodec    = NULL;
545:   (*mapping)->info_nodei    = NULL;

547:   (*mapping)->ops->globaltolocalmappingapply      = NULL;
548:   (*mapping)->ops->globaltolocalmappingapplyblock = NULL;
549:   (*mapping)->ops->destroy                        = NULL;
550:   if (mode == PETSC_COPY_VALUES) {
551:     PetscMalloc1(n,&in);
552:     PetscMemcpy(in,indices,n*sizeof(PetscInt));
553:     (*mapping)->indices = in;
554:     PetscLogObjectMemory((PetscObject)*mapping,n*sizeof(PetscInt));
555:   } else if (mode == PETSC_OWN_POINTER) {
556:     (*mapping)->indices = (PetscInt*)indices;
557:     PetscLogObjectMemory((PetscObject)*mapping,n*sizeof(PetscInt));
558:   }
559:   else SETERRQ(comm,PETSC_ERR_SUP,"Cannot currently use PETSC_USE_POINTER");
560:   return(0);
561: }

563: PetscFunctionList ISLocalToGlobalMappingList = NULL;


566: /*@
567:    ISLocalToGlobalMappingSetFromOptions - Set mapping options from the options database.

569:    Not collective

571:    Input Parameters:
572: .  mapping - mapping data structure

574:    Level: advanced

576: @*/
577: PetscErrorCode ISLocalToGlobalMappingSetFromOptions(ISLocalToGlobalMapping mapping)
578: {
579:   PetscErrorCode             ierr;
580:   char                       type[256];
581:   ISLocalToGlobalMappingType defaulttype = "Not set";
582:   PetscBool                  flg;

586:   ISLocalToGlobalMappingRegisterAll();
587:   PetscObjectOptionsBegin((PetscObject)mapping);
588:   PetscOptionsFList("-islocaltoglobalmapping_type","ISLocalToGlobalMapping method","ISLocalToGlobalMappingSetType",ISLocalToGlobalMappingList,(char*)(((PetscObject)mapping)->type_name) ? ((PetscObject)mapping)->type_name : defaulttype,type,256,&flg);
589:   if (flg) {
590:     ISLocalToGlobalMappingSetType(mapping,type);
591:   }
592:   PetscOptionsEnd();
593:   return(0);
594: }

596: /*@
597:    ISLocalToGlobalMappingDestroy - Destroys a mapping between a local (0 to n)
598:    ordering and a global parallel ordering.

600:    Note Collective

602:    Input Parameters:
603: .  mapping - mapping data structure

605:    Level: advanced

607: .seealso: ISLocalToGlobalMappingCreate()
608: @*/
609: PetscErrorCode  ISLocalToGlobalMappingDestroy(ISLocalToGlobalMapping *mapping)
610: {

614:   if (!*mapping) return(0);
616:   if (--((PetscObject)(*mapping))->refct > 0) {*mapping = 0;return(0);}
617:   PetscFree((*mapping)->indices);
618:   PetscFree((*mapping)->info_procs);
619:   PetscFree((*mapping)->info_numprocs);
620:   if ((*mapping)->info_indices) {
621:     PetscInt i;

623:     PetscFree(((*mapping)->info_indices)[0]);
624:     for (i=1; i<(*mapping)->info_nproc; i++) {
625:       PetscFree(((*mapping)->info_indices)[i]);
626:     }
627:     PetscFree((*mapping)->info_indices);
628:   }
629:   if ((*mapping)->info_nodei) {
630:     PetscFree(((*mapping)->info_nodei)[0]);
631:   }
632:   PetscFree((*mapping)->info_nodei);
633:   PetscFree((*mapping)->info_nodec);
634:   if ((*mapping)->ops->destroy) {
635:     (*(*mapping)->ops->destroy)(*mapping);
636:   }
637:   PetscHeaderDestroy(mapping);
638:   *mapping = 0;
639:   return(0);
640: }

642: /*@
643:     ISLocalToGlobalMappingApplyIS - Creates from an IS in the local numbering
644:     a new index set using the global numbering defined in an ISLocalToGlobalMapping
645:     context.

647:     Not collective

649:     Input Parameters:
650: +   mapping - mapping between local and global numbering
651: -   is - index set in local numbering

653:     Output Parameters:
654: .   newis - index set in global numbering

656:     Level: advanced

658:     Concepts: mapping^local to global

660: .seealso: ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingCreate(),
661:           ISLocalToGlobalMappingDestroy(), ISGlobalToLocalMappingApply()
662: @*/
663: PetscErrorCode  ISLocalToGlobalMappingApplyIS(ISLocalToGlobalMapping mapping,IS is,IS *newis)
664: {
666:   PetscInt       n,*idxout;
667:   const PetscInt *idxin;


674:   ISGetLocalSize(is,&n);
675:   ISGetIndices(is,&idxin);
676:   PetscMalloc1(n,&idxout);
677:   ISLocalToGlobalMappingApply(mapping,n,idxin,idxout);
678:   ISRestoreIndices(is,&idxin);
679:   ISCreateGeneral(PetscObjectComm((PetscObject)is),n,idxout,PETSC_OWN_POINTER,newis);
680:   return(0);
681: }

683: /*@
684:    ISLocalToGlobalMappingApply - Takes a list of integers in a local numbering
685:    and converts them to the global numbering.

687:    Not collective

689:    Input Parameters:
690: +  mapping - the local to global mapping context
691: .  N - number of integers
692: -  in - input indices in local numbering

694:    Output Parameter:
695: .  out - indices in global numbering

697:    Notes:
698:    The in and out array parameters may be identical.

700:    Level: advanced

702: .seealso: ISLocalToGlobalMappingApplyBlock(), ISLocalToGlobalMappingCreate(),ISLocalToGlobalMappingDestroy(),
703:           ISLocalToGlobalMappingApplyIS(),AOCreateBasic(),AOApplicationToPetsc(),
704:           AOPetscToApplication(), ISGlobalToLocalMappingApply()

706:     Concepts: mapping^local to global
707: @*/
708: PetscErrorCode ISLocalToGlobalMappingApply(ISLocalToGlobalMapping mapping,PetscInt N,const PetscInt in[],PetscInt out[])
709: {
710:   PetscInt i,bs,Nmax;

714:   bs   = mapping->bs;
715:   Nmax = bs*mapping->n;
716:   if (bs == 1) {
717:     const PetscInt *idx = mapping->indices;
718:     for (i=0; i<N; i++) {
719:       if (in[i] < 0) {
720:         out[i] = in[i];
721:         continue;
722:       }
723:       if (in[i] >= Nmax) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Local index %D too large %D (max) at %D",in[i],Nmax-1,i);
724:       out[i] = idx[in[i]];
725:     }
726:   } else {
727:     const PetscInt *idx = mapping->indices;
728:     for (i=0; i<N; i++) {
729:       if (in[i] < 0) {
730:         out[i] = in[i];
731:         continue;
732:       }
733:       if (in[i] >= Nmax) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Local index %D too large %D (max) at %D",in[i],Nmax-1,i);
734:       out[i] = idx[in[i]/bs]*bs + (in[i] % bs);
735:     }
736:   }
737:   return(0);
738: }

740: /*@
741:    ISLocalToGlobalMappingApplyBlock - Takes a list of integers in a local block numbering and converts them to the global block numbering

743:    Not collective

745:    Input Parameters:
746: +  mapping - the local to global mapping context
747: .  N - number of integers
748: -  in - input indices in local block numbering

750:    Output Parameter:
751: .  out - indices in global block numbering

753:    Notes:
754:    The in and out array parameters may be identical.

756:    Example:
757:      If the index values are {0,1,6,7} set with a call to ISLocalToGlobalMappingCreate(PETSC_COMM_SELF,2,2,{0,3}) then the mapping applied to 0
758:      (the first block) would produce 0 and the mapping applied to 1 (the second block) would produce 3.

760:    Level: advanced

762: .seealso: ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingCreate(),ISLocalToGlobalMappingDestroy(),
763:           ISLocalToGlobalMappingApplyIS(),AOCreateBasic(),AOApplicationToPetsc(),
764:           AOPetscToApplication(), ISGlobalToLocalMappingApply()

766:     Concepts: mapping^local to global
767: @*/
768: PetscErrorCode ISLocalToGlobalMappingApplyBlock(ISLocalToGlobalMapping mapping,PetscInt N,const PetscInt in[],PetscInt out[])
769: {

773:   {
774:     PetscInt i,Nmax = mapping->n;
775:     const PetscInt *idx = mapping->indices;

777:     for (i=0; i<N; i++) {
778:       if (in[i] < 0) {
779:         out[i] = in[i];
780:         continue;
781:       }
782:       if (in[i] >= Nmax) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Local block index %D too large %D (max) at %D",in[i],Nmax-1,i);
783:       out[i] = idx[in[i]];
784:     }
785:   }
786:   return(0);
787: }

789: /*@
790:     ISGlobalToLocalMappingApply - Provides the local numbering for a list of integers
791:     specified with a global numbering.

793:     Not collective

795:     Input Parameters:
796: +   mapping - mapping between local and global numbering
797: .   type - IS_GTOLM_MASK - replaces global indices with no local value with -1
798:            IS_GTOLM_DROP - drops the indices with no local value from the output list
799: .   n - number of global indices to map
800: -   idx - global indices to map

802:     Output Parameters:
803: +   nout - number of indices in output array (if type == IS_GTOLM_MASK then nout = n)
804: -   idxout - local index of each global index, one must pass in an array long enough
805:              to hold all the indices. You can call ISGlobalToLocalMappingApply() with
806:              idxout == NULL to determine the required length (returned in nout)
807:              and then allocate the required space and call ISGlobalToLocalMappingApply()
808:              a second time to set the values.

810:     Notes:
811:     Either nout or idxout may be NULL. idx and idxout may be identical.

813:     For "small" problems when using ISGlobalToLocalMappingApply() and ISGlobalToLocalMappingApplyBlock(), the ISLocalToGlobalMappingType of ISLOCALTOGLOBALMAPPINGBASIC will be used;
814:     this uses more memory but is faster; this approach is not scalable for extremely large mappings. For large problems ISLOCALTOGLOBALMAPPINGHASH is used, this is scalable.
815:     Use ISLocalToGlobalMappingSetType() or call ISLocalToGlobalMappingSetFromOptions() with the option -islocaltoglobalmapping_type <basic,hash> to control which is used.

817:     Level: advanced

819:     Developer Note: The manual page states that idx and idxout may be identical but the calling
820:        sequence declares idx as const so it cannot be the same as idxout.

822:     Concepts: mapping^global to local

824: .seealso: ISLocalToGlobalMappingApply(), ISGlobalToLocalMappingApplyBlock(), ISLocalToGlobalMappingCreate(),
825:           ISLocalToGlobalMappingDestroy()
826: @*/
827: PetscErrorCode  ISGlobalToLocalMappingApply(ISLocalToGlobalMapping mapping,ISGlobalToLocalMappingMode type,PetscInt n,const PetscInt idx[],PetscInt *nout,PetscInt idxout[])
828: {

833:   if (!mapping->data) {
834:     ISGlobalToLocalMappingSetUp(mapping);
835:   }
836:   (*mapping->ops->globaltolocalmappingapply)(mapping,type,n,idx,nout,idxout);
837:   return(0);
838: }

840: /*@
841:     ISGlobalToLocalMappingApplyIS - Creates from an IS in the global numbering
842:     a new index set using the local numbering defined in an ISLocalToGlobalMapping
843:     context.

845:     Not collective

847:     Input Parameters:
848: +   mapping - mapping between local and global numbering
849: .   type - IS_GTOLM_MASK - replaces global indices with no local value with -1
850:            IS_GTOLM_DROP - drops the indices with no local value from the output list
851: -   is - index set in global numbering

853:     Output Parameters:
854: .   newis - index set in local numbering

856:     Level: advanced

858:     Concepts: mapping^local to global

860: .seealso: ISGlobalToLocalMappingApply(), ISLocalToGlobalMappingCreate(),
861:           ISLocalToGlobalMappingDestroy()
862: @*/
863: PetscErrorCode  ISGlobalToLocalMappingApplyIS(ISLocalToGlobalMapping mapping,ISGlobalToLocalMappingMode type,IS is,IS *newis)
864: {
866:   PetscInt       n,nout,*idxout;
867:   const PetscInt *idxin;


874:   ISGetLocalSize(is,&n);
875:   ISGetIndices(is,&idxin);
876:   if (type == IS_GTOLM_MASK) {
877:     PetscMalloc1(n,&idxout);
878:   } else {
879:     ISGlobalToLocalMappingApply(mapping,type,n,idxin,&nout,NULL);
880:     PetscMalloc1(nout,&idxout);
881:   }
882:   ISGlobalToLocalMappingApply(mapping,type,n,idxin,&nout,idxout);
883:   ISRestoreIndices(is,&idxin);
884:   ISCreateGeneral(PetscObjectComm((PetscObject)is),nout,idxout,PETSC_OWN_POINTER,newis);
885:   return(0);
886: }

888: /*@
889:     ISGlobalToLocalMappingApplyBlock - Provides the local block numbering for a list of integers
890:     specified with a block global numbering.

892:     Not collective

894:     Input Parameters:
895: +   mapping - mapping between local and global numbering
896: .   type - IS_GTOLM_MASK - replaces global indices with no local value with -1
897:            IS_GTOLM_DROP - drops the indices with no local value from the output list
898: .   n - number of global indices to map
899: -   idx - global indices to map

901:     Output Parameters:
902: +   nout - number of indices in output array (if type == IS_GTOLM_MASK then nout = n)
903: -   idxout - local index of each global index, one must pass in an array long enough
904:              to hold all the indices. You can call ISGlobalToLocalMappingApplyBlock() with
905:              idxout == NULL to determine the required length (returned in nout)
906:              and then allocate the required space and call ISGlobalToLocalMappingApplyBlock()
907:              a second time to set the values.

909:     Notes:
910:     Either nout or idxout may be NULL. idx and idxout may be identical.

912:     For "small" problems when using ISGlobalToLocalMappingApply() and ISGlobalToLocalMappingApplyBlock(), the ISLocalToGlobalMappingType of ISLOCALTOGLOBALMAPPINGBASIC will be used;
913:     this uses more memory but is faster; this approach is not scalable for extremely large mappings. For large problems ISLOCALTOGLOBALMAPPINGHASH is used, this is scalable.
914:     Use ISLocalToGlobalMappingSetType() or call ISLocalToGlobalMappingSetFromOptions() with the option -islocaltoglobalmapping_type <basic,hash> to control which is used.

916:     Level: advanced

918:     Developer Note: The manual page states that idx and idxout may be identical but the calling
919:        sequence declares idx as const so it cannot be the same as idxout.

921:     Concepts: mapping^global to local

923: .seealso: ISLocalToGlobalMappingApply(), ISGlobalToLocalMappingApply(), ISLocalToGlobalMappingCreate(),
924:           ISLocalToGlobalMappingDestroy()
925: @*/
926: PetscErrorCode  ISGlobalToLocalMappingApplyBlock(ISLocalToGlobalMapping mapping,ISGlobalToLocalMappingMode type,
927:                                                  PetscInt n,const PetscInt idx[],PetscInt *nout,PetscInt idxout[])
928: {

933:   if (!mapping->data) {
934:     ISGlobalToLocalMappingSetUp(mapping);
935:   }
936:   (*mapping->ops->globaltolocalmappingapplyblock)(mapping,type,n,idx,nout,idxout);
937:   return(0);
938: }


941: /*@C
942:     ISLocalToGlobalMappingGetBlockInfo - Gets the neighbor information for each processor and
943:      each index shared by more than one processor

945:     Collective on ISLocalToGlobalMapping

947:     Input Parameters:
948: .   mapping - the mapping from local to global indexing

950:     Output Parameter:
951: +   nproc - number of processors that are connected to this one
952: .   proc - neighboring processors
953: .   numproc - number of indices for each subdomain (processor)
954: -   indices - indices of nodes (in local numbering) shared with neighbors (sorted by global numbering)

956:     Level: advanced

958:     Concepts: mapping^local to global

960:     Fortran Usage:
961: $        ISLocalToGlobalMpngGetInfoSize(ISLocalToGlobalMapping,PetscInt nproc,PetscInt numprocmax,ierr) followed by
962: $        ISLocalToGlobalMappingGetInfo(ISLocalToGlobalMapping,PetscInt nproc, PetscInt procs[nproc],PetscInt numprocs[nproc],
963:           PetscInt indices[nproc][numprocmax],ierr)
964:         There is no ISLocalToGlobalMappingRestoreInfo() in Fortran. You must make sure that procs[], numprocs[] and
965:         indices[][] are large enough arrays, either by allocating them dynamically or defining static ones large enough.


968: .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreateIS(), ISLocalToGlobalMappingCreate(),
969:           ISLocalToGlobalMappingRestoreInfo()
970: @*/
971: PetscErrorCode  ISLocalToGlobalMappingGetBlockInfo(ISLocalToGlobalMapping mapping,PetscInt *nproc,PetscInt *procs[],PetscInt *numprocs[],PetscInt **indices[])
972: {

977:   if (mapping->info_cached) {
978:     *nproc    = mapping->info_nproc;
979:     *procs    = mapping->info_procs;
980:     *numprocs = mapping->info_numprocs;
981:     *indices  = mapping->info_indices;
982:   } else {
983:     ISLocalToGlobalMappingGetBlockInfo_Private(mapping,nproc,procs,numprocs,indices);
984:   }
985:   return(0);
986: }

988: static PetscErrorCode  ISLocalToGlobalMappingGetBlockInfo_Private(ISLocalToGlobalMapping mapping,PetscInt *nproc,PetscInt *procs[],PetscInt *numprocs[],PetscInt **indices[])
989: {
991:   PetscMPIInt    size,rank,tag1,tag2,tag3,*len,*source,imdex;
992:   PetscInt       i,n = mapping->n,Ng,ng,max = 0,*lindices = mapping->indices;
993:   PetscInt       *nprocs,*owner,nsends,*sends,j,*starts,nmax,nrecvs,*recvs,proc;
994:   PetscInt       cnt,scale,*ownedsenders,*nownedsenders,rstart,nowned;
995:   PetscInt       node,nownedm,nt,*sends2,nsends2,*starts2,*lens2,*dest,nrecvs2,*starts3,*recvs2,k,*bprocs,*tmp;
996:   PetscInt       first_procs,first_numprocs,*first_indices;
997:   MPI_Request    *recv_waits,*send_waits;
998:   MPI_Status     recv_status,*send_status,*recv_statuses;
999:   MPI_Comm       comm;
1000:   PetscBool      debug = PETSC_FALSE;

1003:   PetscObjectGetComm((PetscObject)mapping,&comm);
1004:   MPI_Comm_size(comm,&size);
1005:   MPI_Comm_rank(comm,&rank);
1006:   if (size == 1) {
1007:     *nproc         = 0;
1008:     *procs         = NULL;
1009:     PetscNew(numprocs);
1010:     (*numprocs)[0] = 0;
1011:     PetscNew(indices);
1012:     (*indices)[0]  = NULL;
1013:     /* save info for reuse */
1014:     mapping->info_nproc = *nproc;
1015:     mapping->info_procs = *procs;
1016:     mapping->info_numprocs = *numprocs;
1017:     mapping->info_indices = *indices;
1018:     mapping->info_cached = PETSC_TRUE;
1019:     return(0);
1020:   }

1022:   PetscOptionsGetBool(((PetscObject)mapping)->options,NULL,"-islocaltoglobalmappinggetinfo_debug",&debug,NULL);

1024:   /*
1025:     Notes on ISLocalToGlobalMappingGetBlockInfo

1027:     globally owned node - the nodes that have been assigned to this processor in global
1028:            numbering, just for this routine.

1030:     nontrivial globally owned node - node assigned to this processor that is on a subdomain
1031:            boundary (i.e. is has more than one local owner)

1033:     locally owned node - node that exists on this processors subdomain

1035:     nontrivial locally owned node - node that is not in the interior (i.e. has more than one
1036:            local subdomain
1037:   */
1038:   PetscObjectGetNewTag((PetscObject)mapping,&tag1);
1039:   PetscObjectGetNewTag((PetscObject)mapping,&tag2);
1040:   PetscObjectGetNewTag((PetscObject)mapping,&tag3);

1042:   for (i=0; i<n; i++) {
1043:     if (lindices[i] > max) max = lindices[i];
1044:   }
1045:   MPIU_Allreduce(&max,&Ng,1,MPIU_INT,MPI_MAX,comm);
1046:   Ng++;
1047:   MPI_Comm_size(comm,&size);
1048:   MPI_Comm_rank(comm,&rank);
1049:   scale  = Ng/size + 1;
1050:   ng     = scale; if (rank == size-1) ng = Ng - scale*(size-1); ng = PetscMax(1,ng);
1051:   rstart = scale*rank;

1053:   /* determine ownership ranges of global indices */
1054:   PetscMalloc1(2*size,&nprocs);
1055:   PetscMemzero(nprocs,2*size*sizeof(PetscInt));

1057:   /* determine owners of each local node  */
1058:   PetscMalloc1(n,&owner);
1059:   for (i=0; i<n; i++) {
1060:     proc             = lindices[i]/scale; /* processor that globally owns this index */
1061:     nprocs[2*proc+1] = 1;                 /* processor globally owns at least one of ours */
1062:     owner[i]         = proc;
1063:     nprocs[2*proc]++;                     /* count of how many that processor globally owns of ours */
1064:   }
1065:   nsends = 0; for (i=0; i<size; i++) nsends += nprocs[2*i+1];
1066:   PetscInfo1(mapping,"Number of global owners for my local data %D\n",nsends);

1068:   /* inform other processors of number of messages and max length*/
1069:   PetscMaxSum(comm,nprocs,&nmax,&nrecvs);
1070:   PetscInfo1(mapping,"Number of local owners for my global data %D\n",nrecvs);

1072:   /* post receives for owned rows */
1073:   PetscMalloc1((2*nrecvs+1)*(nmax+1),&recvs);
1074:   PetscMalloc1(nrecvs+1,&recv_waits);
1075:   for (i=0; i<nrecvs; i++) {
1076:     MPI_Irecv(recvs+2*nmax*i,2*nmax,MPIU_INT,MPI_ANY_SOURCE,tag1,comm,recv_waits+i);
1077:   }

1079:   /* pack messages containing lists of local nodes to owners */
1080:   PetscMalloc1(2*n+1,&sends);
1081:   PetscMalloc1(size+1,&starts);
1082:   starts[0] = 0;
1083:   for (i=1; i<size; i++) starts[i] = starts[i-1] + 2*nprocs[2*i-2];
1084:   for (i=0; i<n; i++) {
1085:     sends[starts[owner[i]]++] = lindices[i];
1086:     sends[starts[owner[i]]++] = i;
1087:   }
1088:   PetscFree(owner);
1089:   starts[0] = 0;
1090:   for (i=1; i<size; i++) starts[i] = starts[i-1] + 2*nprocs[2*i-2];

1092:   /* send the messages */
1093:   PetscMalloc1(nsends+1,&send_waits);
1094:   PetscMalloc1(nsends+1,&dest);
1095:   cnt = 0;
1096:   for (i=0; i<size; i++) {
1097:     if (nprocs[2*i]) {
1098:       MPI_Isend(sends+starts[i],2*nprocs[2*i],MPIU_INT,i,tag1,comm,send_waits+cnt);
1099:       dest[cnt] = i;
1100:       cnt++;
1101:     }
1102:   }
1103:   PetscFree(starts);

1105:   /* wait on receives */
1106:   PetscMalloc1(nrecvs+1,&source);
1107:   PetscMalloc1(nrecvs+1,&len);
1108:   cnt  = nrecvs;
1109:   PetscMalloc1(ng+1,&nownedsenders);
1110:   PetscMemzero(nownedsenders,ng*sizeof(PetscInt));
1111:   while (cnt) {
1112:     MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);
1113:     /* unpack receives into our local space */
1114:     MPI_Get_count(&recv_status,MPIU_INT,&len[imdex]);
1115:     source[imdex] = recv_status.MPI_SOURCE;
1116:     len[imdex]    = len[imdex]/2;
1117:     /* count how many local owners for each of my global owned indices */
1118:     for (i=0; i<len[imdex]; i++) nownedsenders[recvs[2*imdex*nmax+2*i]-rstart]++;
1119:     cnt--;
1120:   }
1121:   PetscFree(recv_waits);

1123:   /* count how many globally owned indices are on an edge multiplied by how many processors own them. */
1124:   nowned  = 0;
1125:   nownedm = 0;
1126:   for (i=0; i<ng; i++) {
1127:     if (nownedsenders[i] > 1) {nownedm += nownedsenders[i]; nowned++;}
1128:   }

1130:   /* create single array to contain rank of all local owners of each globally owned index */
1131:   PetscMalloc1(nownedm+1,&ownedsenders);
1132:   PetscMalloc1(ng+1,&starts);
1133:   starts[0] = 0;
1134:   for (i=1; i<ng; i++) {
1135:     if (nownedsenders[i-1] > 1) starts[i] = starts[i-1] + nownedsenders[i-1];
1136:     else starts[i] = starts[i-1];
1137:   }

1139:   /* for each nontrival globally owned node list all arriving processors */
1140:   for (i=0; i<nrecvs; i++) {
1141:     for (j=0; j<len[i]; j++) {
1142:       node = recvs[2*i*nmax+2*j]-rstart;
1143:       if (nownedsenders[node] > 1) ownedsenders[starts[node]++] = source[i];
1144:     }
1145:   }

1147:   if (debug) { /* -----------------------------------  */
1148:     starts[0] = 0;
1149:     for (i=1; i<ng; i++) {
1150:       if (nownedsenders[i-1] > 1) starts[i] = starts[i-1] + nownedsenders[i-1];
1151:       else starts[i] = starts[i-1];
1152:     }
1153:     for (i=0; i<ng; i++) {
1154:       if (nownedsenders[i] > 1) {
1155:         PetscSynchronizedPrintf(comm,"[%d] global node %D local owner processors: ",rank,i+rstart);
1156:         for (j=0; j<nownedsenders[i]; j++) {
1157:           PetscSynchronizedPrintf(comm,"%D ",ownedsenders[starts[i]+j]);
1158:         }
1159:         PetscSynchronizedPrintf(comm,"\n");
1160:       }
1161:     }
1162:     PetscSynchronizedFlush(comm,PETSC_STDOUT);
1163:   } /* -----------------------------------  */

1165:   /* wait on original sends */
1166:   if (nsends) {
1167:     PetscMalloc1(nsends,&send_status);
1168:     MPI_Waitall(nsends,send_waits,send_status);
1169:     PetscFree(send_status);
1170:   }
1171:   PetscFree(send_waits);
1172:   PetscFree(sends);
1173:   PetscFree(nprocs);

1175:   /* pack messages to send back to local owners */
1176:   starts[0] = 0;
1177:   for (i=1; i<ng; i++) {
1178:     if (nownedsenders[i-1] > 1) starts[i] = starts[i-1] + nownedsenders[i-1];
1179:     else starts[i] = starts[i-1];
1180:   }
1181:   nsends2 = nrecvs;
1182:   PetscMalloc1(nsends2+1,&nprocs); /* length of each message */
1183:   for (i=0; i<nrecvs; i++) {
1184:     nprocs[i] = 1;
1185:     for (j=0; j<len[i]; j++) {
1186:       node = recvs[2*i*nmax+2*j]-rstart;
1187:       if (nownedsenders[node] > 1) nprocs[i] += 2 + nownedsenders[node];
1188:     }
1189:   }
1190:   nt = 0;
1191:   for (i=0; i<nsends2; i++) nt += nprocs[i];

1193:   PetscMalloc1(nt+1,&sends2);
1194:   PetscMalloc1(nsends2+1,&starts2);

1196:   starts2[0] = 0;
1197:   for (i=1; i<nsends2; i++) starts2[i] = starts2[i-1] + nprocs[i-1];
1198:   /*
1199:      Each message is 1 + nprocs[i] long, and consists of
1200:        (0) the number of nodes being sent back
1201:        (1) the local node number,
1202:        (2) the number of processors sharing it,
1203:        (3) the processors sharing it
1204:   */
1205:   for (i=0; i<nsends2; i++) {
1206:     cnt = 1;
1207:     sends2[starts2[i]] = 0;
1208:     for (j=0; j<len[i]; j++) {
1209:       node = recvs[2*i*nmax+2*j]-rstart;
1210:       if (nownedsenders[node] > 1) {
1211:         sends2[starts2[i]]++;
1212:         sends2[starts2[i]+cnt++] = recvs[2*i*nmax+2*j+1];
1213:         sends2[starts2[i]+cnt++] = nownedsenders[node];
1214:         PetscMemcpy(&sends2[starts2[i]+cnt],&ownedsenders[starts[node]],nownedsenders[node]*sizeof(PetscInt));
1215:         cnt += nownedsenders[node];
1216:       }
1217:     }
1218:   }

1220:   /* receive the message lengths */
1221:   nrecvs2 = nsends;
1222:   PetscMalloc1(nrecvs2+1,&lens2);
1223:   PetscMalloc1(nrecvs2+1,&starts3);
1224:   PetscMalloc1(nrecvs2+1,&recv_waits);
1225:   for (i=0; i<nrecvs2; i++) {
1226:     MPI_Irecv(&lens2[i],1,MPIU_INT,dest[i],tag2,comm,recv_waits+i);
1227:   }

1229:   /* send the message lengths */
1230:   for (i=0; i<nsends2; i++) {
1231:     MPI_Send(&nprocs[i],1,MPIU_INT,source[i],tag2,comm);
1232:   }

1234:   /* wait on receives of lens */
1235:   if (nrecvs2) {
1236:     PetscMalloc1(nrecvs2,&recv_statuses);
1237:     MPI_Waitall(nrecvs2,recv_waits,recv_statuses);
1238:     PetscFree(recv_statuses);
1239:   }
1240:   PetscFree(recv_waits);

1242:   starts3[0] = 0;
1243:   nt         = 0;
1244:   for (i=0; i<nrecvs2-1; i++) {
1245:     starts3[i+1] = starts3[i] + lens2[i];
1246:     nt          += lens2[i];
1247:   }
1248:   if (nrecvs2) nt += lens2[nrecvs2-1];

1250:   PetscMalloc1(nt+1,&recvs2);
1251:   PetscMalloc1(nrecvs2+1,&recv_waits);
1252:   for (i=0; i<nrecvs2; i++) {
1253:     MPI_Irecv(recvs2+starts3[i],lens2[i],MPIU_INT,dest[i],tag3,comm,recv_waits+i);
1254:   }

1256:   /* send the messages */
1257:   PetscMalloc1(nsends2+1,&send_waits);
1258:   for (i=0; i<nsends2; i++) {
1259:     MPI_Isend(sends2+starts2[i],nprocs[i],MPIU_INT,source[i],tag3,comm,send_waits+i);
1260:   }

1262:   /* wait on receives */
1263:   if (nrecvs2) {
1264:     PetscMalloc1(nrecvs2,&recv_statuses);
1265:     MPI_Waitall(nrecvs2,recv_waits,recv_statuses);
1266:     PetscFree(recv_statuses);
1267:   }
1268:   PetscFree(recv_waits);
1269:   PetscFree(nprocs);

1271:   if (debug) { /* -----------------------------------  */
1272:     cnt = 0;
1273:     for (i=0; i<nrecvs2; i++) {
1274:       nt = recvs2[cnt++];
1275:       for (j=0; j<nt; j++) {
1276:         PetscSynchronizedPrintf(comm,"[%d] local node %D number of subdomains %D: ",rank,recvs2[cnt],recvs2[cnt+1]);
1277:         for (k=0; k<recvs2[cnt+1]; k++) {
1278:           PetscSynchronizedPrintf(comm,"%D ",recvs2[cnt+2+k]);
1279:         }
1280:         cnt += 2 + recvs2[cnt+1];
1281:         PetscSynchronizedPrintf(comm,"\n");
1282:       }
1283:     }
1284:     PetscSynchronizedFlush(comm,PETSC_STDOUT);
1285:   } /* -----------------------------------  */

1287:   /* count number subdomains for each local node */
1288:   PetscMalloc1(size,&nprocs);
1289:   PetscMemzero(nprocs,size*sizeof(PetscInt));
1290:   cnt  = 0;
1291:   for (i=0; i<nrecvs2; i++) {
1292:     nt = recvs2[cnt++];
1293:     for (j=0; j<nt; j++) {
1294:       for (k=0; k<recvs2[cnt+1]; k++) nprocs[recvs2[cnt+2+k]]++;
1295:       cnt += 2 + recvs2[cnt+1];
1296:     }
1297:   }
1298:   nt = 0; for (i=0; i<size; i++) nt += (nprocs[i] > 0);
1299:   *nproc    = nt;
1300:   PetscMalloc1(nt+1,procs);
1301:   PetscMalloc1(nt+1,numprocs);
1302:   PetscMalloc1(nt+1,indices);
1303:   for (i=0;i<nt+1;i++) (*indices)[i]=NULL;
1304:   PetscMalloc1(size,&bprocs);
1305:   cnt  = 0;
1306:   for (i=0; i<size; i++) {
1307:     if (nprocs[i] > 0) {
1308:       bprocs[i]        = cnt;
1309:       (*procs)[cnt]    = i;
1310:       (*numprocs)[cnt] = nprocs[i];
1311:       PetscMalloc1(nprocs[i],&(*indices)[cnt]);
1312:       cnt++;
1313:     }
1314:   }

1316:   /* make the list of subdomains for each nontrivial local node */
1317:   PetscMemzero(*numprocs,nt*sizeof(PetscInt));
1318:   cnt  = 0;
1319:   for (i=0; i<nrecvs2; i++) {
1320:     nt = recvs2[cnt++];
1321:     for (j=0; j<nt; j++) {
1322:       for (k=0; k<recvs2[cnt+1]; k++) (*indices)[bprocs[recvs2[cnt+2+k]]][(*numprocs)[bprocs[recvs2[cnt+2+k]]]++] = recvs2[cnt];
1323:       cnt += 2 + recvs2[cnt+1];
1324:     }
1325:   }
1326:   PetscFree(bprocs);
1327:   PetscFree(recvs2);

1329:   /* sort the node indexing by their global numbers */
1330:   nt = *nproc;
1331:   for (i=0; i<nt; i++) {
1332:     PetscMalloc1((*numprocs)[i],&tmp);
1333:     for (j=0; j<(*numprocs)[i]; j++) tmp[j] = lindices[(*indices)[i][j]];
1334:     PetscSortIntWithArray((*numprocs)[i],tmp,(*indices)[i]);
1335:     PetscFree(tmp);
1336:   }

1338:   if (debug) { /* -----------------------------------  */
1339:     nt = *nproc;
1340:     for (i=0; i<nt; i++) {
1341:       PetscSynchronizedPrintf(comm,"[%d] subdomain %D number of indices %D: ",rank,(*procs)[i],(*numprocs)[i]);
1342:       for (j=0; j<(*numprocs)[i]; j++) {
1343:         PetscSynchronizedPrintf(comm,"%D ",(*indices)[i][j]);
1344:       }
1345:       PetscSynchronizedPrintf(comm,"\n");
1346:     }
1347:     PetscSynchronizedFlush(comm,PETSC_STDOUT);
1348:   } /* -----------------------------------  */

1350:   /* wait on sends */
1351:   if (nsends2) {
1352:     PetscMalloc1(nsends2,&send_status);
1353:     MPI_Waitall(nsends2,send_waits,send_status);
1354:     PetscFree(send_status);
1355:   }

1357:   PetscFree(starts3);
1358:   PetscFree(dest);
1359:   PetscFree(send_waits);

1361:   PetscFree(nownedsenders);
1362:   PetscFree(ownedsenders);
1363:   PetscFree(starts);
1364:   PetscFree(starts2);
1365:   PetscFree(lens2);

1367:   PetscFree(source);
1368:   PetscFree(len);
1369:   PetscFree(recvs);
1370:   PetscFree(nprocs);
1371:   PetscFree(sends2);

1373:   /* put the information about myself as the first entry in the list */
1374:   first_procs    = (*procs)[0];
1375:   first_numprocs = (*numprocs)[0];
1376:   first_indices  = (*indices)[0];
1377:   for (i=0; i<*nproc; i++) {
1378:     if ((*procs)[i] == rank) {
1379:       (*procs)[0]    = (*procs)[i];
1380:       (*numprocs)[0] = (*numprocs)[i];
1381:       (*indices)[0]  = (*indices)[i];
1382:       (*procs)[i]    = first_procs;
1383:       (*numprocs)[i] = first_numprocs;
1384:       (*indices)[i]  = first_indices;
1385:       break;
1386:     }
1387:   }

1389:   /* save info for reuse */
1390:   mapping->info_nproc = *nproc;
1391:   mapping->info_procs = *procs;
1392:   mapping->info_numprocs = *numprocs;
1393:   mapping->info_indices = *indices;
1394:   mapping->info_cached = PETSC_TRUE;
1395:   return(0);
1396: }

1398: /*@C
1399:     ISLocalToGlobalMappingRestoreBlockInfo - Frees the memory allocated by ISLocalToGlobalMappingGetBlockInfo()

1401:     Collective on ISLocalToGlobalMapping

1403:     Input Parameters:
1404: .   mapping - the mapping from local to global indexing

1406:     Output Parameter:
1407: +   nproc - number of processors that are connected to this one
1408: .   proc - neighboring processors
1409: .   numproc - number of indices for each processor
1410: -   indices - indices of local nodes shared with neighbor (sorted by global numbering)

1412:     Level: advanced

1414: .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreateIS(), ISLocalToGlobalMappingCreate(),
1415:           ISLocalToGlobalMappingGetInfo()
1416: @*/
1417: PetscErrorCode  ISLocalToGlobalMappingRestoreBlockInfo(ISLocalToGlobalMapping mapping,PetscInt *nproc,PetscInt *procs[],PetscInt *numprocs[],PetscInt **indices[])
1418: {

1423:   if (mapping->info_free) {
1424:     PetscFree(*numprocs);
1425:     if (*indices) {
1426:       PetscInt i;

1428:       PetscFree((*indices)[0]);
1429:       for (i=1; i<*nproc; i++) {
1430:         PetscFree((*indices)[i]);
1431:       }
1432:       PetscFree(*indices);
1433:     }
1434:   }
1435:   *nproc    = 0;
1436:   *procs    = NULL;
1437:   *numprocs = NULL;
1438:   *indices  = NULL;
1439:   return(0);
1440: }

1442: /*@C
1443:     ISLocalToGlobalMappingGetInfo - Gets the neighbor information for each processor and
1444:      each index shared by more than one processor

1446:     Collective on ISLocalToGlobalMapping

1448:     Input Parameters:
1449: .   mapping - the mapping from local to global indexing

1451:     Output Parameter:
1452: +   nproc - number of processors that are connected to this one
1453: .   proc - neighboring processors
1454: .   numproc - number of indices for each subdomain (processor)
1455: -   indices - indices of nodes (in local numbering) shared with neighbors (sorted by global numbering)

1457:     Level: advanced

1459:     Concepts: mapping^local to global

1461:     Notes: The user needs to call ISLocalToGlobalMappingRestoreInfo when the data is no longer needed.

1463:     Fortran Usage:
1464: $        ISLocalToGlobalMpngGetInfoSize(ISLocalToGlobalMapping,PetscInt nproc,PetscInt numprocmax,ierr) followed by
1465: $        ISLocalToGlobalMappingGetInfo(ISLocalToGlobalMapping,PetscInt nproc, PetscInt procs[nproc],PetscInt numprocs[nproc],
1466:           PetscInt indices[nproc][numprocmax],ierr)
1467:         There is no ISLocalToGlobalMappingRestoreInfo() in Fortran. You must make sure that procs[], numprocs[] and
1468:         indices[][] are large enough arrays, either by allocating them dynamically or defining static ones large enough.


1471: .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreateIS(), ISLocalToGlobalMappingCreate(),
1472:           ISLocalToGlobalMappingRestoreInfo()
1473: @*/
1474: PetscErrorCode  ISLocalToGlobalMappingGetInfo(ISLocalToGlobalMapping mapping,PetscInt *nproc,PetscInt *procs[],PetscInt *numprocs[],PetscInt **indices[])
1475: {
1477:   PetscInt       **bindices = NULL,*bnumprocs = NULL,bs = mapping->bs,i,j,k;

1481:   ISLocalToGlobalMappingGetBlockInfo(mapping,nproc,procs,&bnumprocs,&bindices);
1482:   if (bs > 1) { /* we need to expand the cached info */
1483:     PetscCalloc1(*nproc,&*indices);
1484:     PetscCalloc1(*nproc,&*numprocs);
1485:     for (i=0; i<*nproc; i++) {
1486:       PetscMalloc1(bs*bnumprocs[i],&(*indices)[i]);
1487:       for (j=0; j<bnumprocs[i]; j++) {
1488:         for (k=0; k<bs; k++) {
1489:           (*indices)[i][j*bs+k] = bs*bindices[i][j] + k;
1490:         }
1491:       }
1492:       (*numprocs)[i] = bnumprocs[i]*bs;
1493:     }
1494:     mapping->info_free = PETSC_TRUE;
1495:   } else {
1496:     *numprocs = bnumprocs;
1497:     *indices  = bindices;
1498:   }
1499:   return(0);
1500: }

1502: /*@C
1503:     ISLocalToGlobalMappingRestoreInfo - Frees the memory allocated by ISLocalToGlobalMappingGetInfo()

1505:     Collective on ISLocalToGlobalMapping

1507:     Input Parameters:
1508: .   mapping - the mapping from local to global indexing

1510:     Output Parameter:
1511: +   nproc - number of processors that are connected to this one
1512: .   proc - neighboring processors
1513: .   numproc - number of indices for each processor
1514: -   indices - indices of local nodes shared with neighbor (sorted by global numbering)

1516:     Level: advanced

1518: .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreateIS(), ISLocalToGlobalMappingCreate(),
1519:           ISLocalToGlobalMappingGetInfo()
1520: @*/
1521: PetscErrorCode  ISLocalToGlobalMappingRestoreInfo(ISLocalToGlobalMapping mapping,PetscInt *nproc,PetscInt *procs[],PetscInt *numprocs[],PetscInt **indices[])
1522: {

1526:   ISLocalToGlobalMappingRestoreBlockInfo(mapping,nproc,procs,numprocs,indices);
1527:   return(0);
1528: }

1530: /*@C
1531:     ISLocalToGlobalMappingGetNodeInfo - Gets the neighbor information for each node

1533:     Collective on ISLocalToGlobalMapping

1535:     Input Parameters:
1536: .   mapping - the mapping from local to global indexing

1538:     Output Parameter:
1539: +   nnodes - number of local nodes (same ISLocalToGlobalMappingGetSize())
1540: .   count - number of neighboring processors per node
1541: -   indices - indices of processes sharing the node (sorted)

1543:     Level: advanced

1545:     Concepts: mapping^local to global

1547:     Notes: The user needs to call ISLocalToGlobalMappingRestoreInfo when the data is no longer needed.

1549: .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreateIS(), ISLocalToGlobalMappingCreate(),
1550:           ISLocalToGlobalMappingGetInfo(), ISLocalToGlobalMappingRestoreNodeInfo()
1551: @*/
1552: PetscErrorCode  ISLocalToGlobalMappingGetNodeInfo(ISLocalToGlobalMapping mapping,PetscInt *nnodes,PetscInt *count[],PetscInt **indices[])
1553: {
1554:   PetscInt       n;

1559:   ISLocalToGlobalMappingGetSize(mapping,&n);
1560:   if (!mapping->info_nodec) {
1561:     PetscInt i,m,n_neigh,*neigh,*n_shared,**shared;

1563:     PetscCalloc1(n+1,&mapping->info_nodec); /* always allocate to flag setup */
1564:     PetscMalloc1(n,&mapping->info_nodei);
1565:     ISLocalToGlobalMappingGetInfo(mapping,&n_neigh,&neigh,&n_shared,&shared);
1566:     for (i=0,m=0;i<n;i++) { mapping->info_nodec[i] = 1; m++; }
1567:     for (i=1;i<n_neigh;i++) {
1568:       PetscInt j;

1570:       m += n_shared[i];
1571:       for (j=0;j<n_shared[i];j++) mapping->info_nodec[shared[i][j]] += 1;
1572:     }
1573:     if (n) { PetscMalloc1(m,&mapping->info_nodei[0]); }
1574:     for (i=1;i<n;i++) mapping->info_nodei[i] = mapping->info_nodei[i-1] + mapping->info_nodec[i-1];
1575:     PetscMemzero(mapping->info_nodec,n*sizeof(PetscInt));
1576:     for (i=0;i<n;i++) { mapping->info_nodec[i] = 1; mapping->info_nodei[i][0] = neigh[0]; }
1577:     for (i=1;i<n_neigh;i++) {
1578:       PetscInt j;

1580:       for (j=0;j<n_shared[i];j++) {
1581:         PetscInt k = shared[i][j];

1583:         mapping->info_nodei[k][mapping->info_nodec[k]] = neigh[i];
1584:         mapping->info_nodec[k] += 1;
1585:       }
1586:     }
1587:     for (i=0;i<n;i++) { PetscSortRemoveDupsInt(&mapping->info_nodec[i],mapping->info_nodei[i]); }
1588:     ISLocalToGlobalMappingRestoreInfo(mapping,&n_neigh,&neigh,&n_shared,&shared);
1589:   }
1590:   if (nnodes)  *nnodes  = n;
1591:   if (count)   *count   = mapping->info_nodec;
1592:   if (indices) *indices = mapping->info_nodei;
1593:   return(0);
1594: }

1596: /*@C
1597:     ISLocalToGlobalMappingRestoreNodeInfo - Frees the memory allocated by ISLocalToGlobalMappingGetNodeInfo()

1599:     Collective on ISLocalToGlobalMapping

1601:     Input Parameters:
1602: .   mapping - the mapping from local to global indexing

1604:     Output Parameter:
1605: +   nnodes - number of local nodes
1606: .   count - number of neighboring processors per node
1607: -   indices - indices of processes sharing the node (sorted)

1609:     Level: advanced

1611: .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreateIS(), ISLocalToGlobalMappingCreate(),
1612:           ISLocalToGlobalMappingGetInfo()
1613: @*/
1614: PetscErrorCode  ISLocalToGlobalMappingRestoreNodeInfo(ISLocalToGlobalMapping mapping,PetscInt *nnodes,PetscInt *count[],PetscInt **indices[])
1615: {
1618:   if (nnodes)  *nnodes  = 0;
1619:   if (count)   *count   = NULL;
1620:   if (indices) *indices = NULL;
1621:   return(0);
1622: }

1624: /*@C
1625:    ISLocalToGlobalMappingGetIndices - Get global indices for every local point that is mapped

1627:    Not Collective

1629:    Input Arguments:
1630: . ltog - local to global mapping

1632:    Output Arguments:
1633: . array - array of indices, the length of this array may be obtained with ISLocalToGlobalMappingGetSize()

1635:    Level: advanced

1637:    Notes:
1638:     ISLocalToGlobalMappingGetSize() returns the length the this array

1640: .seealso: ISLocalToGlobalMappingCreate(), ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingRestoreIndices(), ISLocalToGlobalMappingGetBlockIndices(), ISLocalToGlobalMappingRestoreBlockIndices()
1641: @*/
1642: PetscErrorCode  ISLocalToGlobalMappingGetIndices(ISLocalToGlobalMapping ltog,const PetscInt **array)
1643: {
1647:   if (ltog->bs == 1) {
1648:     *array = ltog->indices;
1649:   } else {
1650:     PetscInt       *jj,k,i,j,n = ltog->n, bs = ltog->bs;
1651:     const PetscInt *ii;

1654:     PetscMalloc1(bs*n,&jj);
1655:     *array = jj;
1656:     k    = 0;
1657:     ii   = ltog->indices;
1658:     for (i=0; i<n; i++)
1659:       for (j=0; j<bs; j++)
1660:         jj[k++] = bs*ii[i] + j;
1661:   }
1662:   return(0);
1663: }

1665: /*@C
1666:    ISLocalToGlobalMappingRestoreIndices - Restore indices obtained with ISLocalToGlobalMappingGetIndices()

1668:    Not Collective

1670:    Input Arguments:
1671: + ltog - local to global mapping
1672: - array - array of indices

1674:    Level: advanced

1676: .seealso: ISLocalToGlobalMappingCreate(), ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingGetIndices()
1677: @*/
1678: PetscErrorCode  ISLocalToGlobalMappingRestoreIndices(ISLocalToGlobalMapping ltog,const PetscInt **array)
1679: {
1683:   if (ltog->bs == 1 && *array != ltog->indices) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_BADPTR,"Trying to return mismatched pointer");

1685:   if (ltog->bs > 1) {
1687:     PetscFree(*(void**)array);
1688:   }
1689:   return(0);
1690: }

1692: /*@C
1693:    ISLocalToGlobalMappingGetBlockIndices - Get global indices for every local block

1695:    Not Collective

1697:    Input Arguments:
1698: . ltog - local to global mapping

1700:    Output Arguments:
1701: . array - array of indices

1703:    Level: advanced

1705: .seealso: ISLocalToGlobalMappingCreate(), ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingRestoreBlockIndices()
1706: @*/
1707: PetscErrorCode  ISLocalToGlobalMappingGetBlockIndices(ISLocalToGlobalMapping ltog,const PetscInt **array)
1708: {
1712:   *array = ltog->indices;
1713:   return(0);
1714: }

1716: /*@C
1717:    ISLocalToGlobalMappingRestoreBlockIndices - Restore indices obtained with ISLocalToGlobalMappingGetBlockIndices()

1719:    Not Collective

1721:    Input Arguments:
1722: + ltog - local to global mapping
1723: - array - array of indices

1725:    Level: advanced

1727: .seealso: ISLocalToGlobalMappingCreate(), ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingGetIndices()
1728: @*/
1729: PetscErrorCode  ISLocalToGlobalMappingRestoreBlockIndices(ISLocalToGlobalMapping ltog,const PetscInt **array)
1730: {
1734:   if (*array != ltog->indices) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_BADPTR,"Trying to return mismatched pointer");
1735:   *array = NULL;
1736:   return(0);
1737: }

1739: /*@C
1740:    ISLocalToGlobalMappingConcatenate - Create a new mapping that concatenates a list of mappings

1742:    Not Collective

1744:    Input Arguments:
1745: + comm - communicator for the new mapping, must contain the communicator of every mapping to concatenate
1746: . n - number of mappings to concatenate
1747: - ltogs - local to global mappings

1749:    Output Arguments:
1750: . ltogcat - new mapping

1752:    Note: this currently always returns a mapping with block size of 1

1754:    Developer Note: If all the input mapping have the same block size we could easily handle that as a special case

1756:    Level: advanced

1758: .seealso: ISLocalToGlobalMappingCreate()
1759: @*/
1760: PetscErrorCode ISLocalToGlobalMappingConcatenate(MPI_Comm comm,PetscInt n,const ISLocalToGlobalMapping ltogs[],ISLocalToGlobalMapping *ltogcat)
1761: {
1762:   PetscInt       i,cnt,m,*idx;

1766:   if (n < 0) SETERRQ1(comm,PETSC_ERR_ARG_OUTOFRANGE,"Must have a non-negative number of mappings, given %D",n);
1770:   for (cnt=0,i=0; i<n; i++) {
1771:     ISLocalToGlobalMappingGetSize(ltogs[i],&m);
1772:     cnt += m;
1773:   }
1774:   PetscMalloc1(cnt,&idx);
1775:   for (cnt=0,i=0; i<n; i++) {
1776:     const PetscInt *subidx;
1777:     ISLocalToGlobalMappingGetSize(ltogs[i],&m);
1778:     ISLocalToGlobalMappingGetIndices(ltogs[i],&subidx);
1779:     PetscMemcpy(&idx[cnt],subidx,m*sizeof(PetscInt));
1780:     ISLocalToGlobalMappingRestoreIndices(ltogs[i],&subidx);
1781:     cnt += m;
1782:   }
1783:   ISLocalToGlobalMappingCreate(comm,1,cnt,idx,PETSC_OWN_POINTER,ltogcat);
1784:   return(0);
1785: }

1787: /*MC
1788:       ISLOCALTOGLOBALMAPPINGBASIC - basic implementation of the ISLocalToGlobalMapping object. When ISGlobalToLocalMappingApply() is
1789:                                     used this is good for only small and moderate size problems.

1791:    Options Database Keys:
1792: +   -islocaltoglobalmapping_type basic - select this method

1794:    Level: beginner

1796: .seealso:  ISLocalToGlobalMappingCreate(), ISLocalToGlobalMappingSetType(), ISLOCALTOGLOBALMAPPINGHASH
1797: M*/
1798: PETSC_EXTERN PetscErrorCode ISLocalToGlobalMappingCreate_Basic(ISLocalToGlobalMapping ltog)
1799: {
1801:   ltog->ops->globaltolocalmappingapply      = ISGlobalToLocalMappingApply_Basic;
1802:   ltog->ops->globaltolocalmappingsetup      = ISGlobalToLocalMappingSetUp_Basic;
1803:   ltog->ops->globaltolocalmappingapplyblock = ISGlobalToLocalMappingApplyBlock_Basic;
1804:   ltog->ops->destroy                        = ISLocalToGlobalMappingDestroy_Basic;
1805:   return(0);
1806: }

1808: /*MC
1809:       ISLOCALTOGLOBALMAPPINGHASH - hash implementation of the ISLocalToGlobalMapping object. When ISGlobalToLocalMappingApply() is
1810:                                     used this is good for large memory problems.

1812:    Options Database Keys:
1813: +   -islocaltoglobalmapping_type hash - select this method


1816:    Notes:
1817:     This is selected automatically for large problems if the user does not set the type.

1819:    Level: beginner

1821: .seealso:  ISLocalToGlobalMappingCreate(), ISLocalToGlobalMappingSetType(), ISLOCALTOGLOBALMAPPINGHASH
1822: M*/
1823: PETSC_EXTERN PetscErrorCode ISLocalToGlobalMappingCreate_Hash(ISLocalToGlobalMapping ltog)
1824: {
1826:   ltog->ops->globaltolocalmappingapply      = ISGlobalToLocalMappingApply_Hash;
1827:   ltog->ops->globaltolocalmappingsetup      = ISGlobalToLocalMappingSetUp_Hash;
1828:   ltog->ops->globaltolocalmappingapplyblock = ISGlobalToLocalMappingApplyBlock_Hash;
1829:   ltog->ops->destroy                        = ISLocalToGlobalMappingDestroy_Hash;
1830:   return(0);
1831: }


1834: /*@C
1835:     ISLocalToGlobalMappingRegister -  Adds a method for applying a global to local mapping with an ISLocalToGlobalMapping

1837:    Not Collective

1839:    Input Parameters:
1840: +  sname - name of a new method
1841: -  routine_create - routine to create method context

1843:    Notes:
1844:    ISLocalToGlobalMappingRegister() may be called multiple times to add several user-defined mappings.

1846:    Sample usage:
1847: .vb
1848:    ISLocalToGlobalMappingRegister("my_mapper",MyCreate);
1849: .ve

1851:    Then, your mapping can be chosen with the procedural interface via
1852: $     ISLocalToGlobalMappingSetType(ltog,"my_mapper")
1853:    or at runtime via the option
1854: $     -islocaltoglobalmapping_type my_mapper

1856:    Level: advanced

1858: .keywords: ISLocalToGlobalMappingType, register

1860: .seealso: ISLocalToGlobalMappingRegisterAll(), ISLocalToGlobalMappingRegisterDestroy(), ISLOCALTOGLOBALMAPPINGBASIC, ISLOCALTOGLOBALMAPPINGHASH

1862: @*/
1863: PetscErrorCode  ISLocalToGlobalMappingRegister(const char sname[],PetscErrorCode (*function)(ISLocalToGlobalMapping))
1864: {

1868:   ISInitializePackage();
1869:   PetscFunctionListAdd(&ISLocalToGlobalMappingList,sname,function);
1870:   return(0);
1871: }

1873: /*@C
1874:    ISLocalToGlobalMappingSetType - Builds ISLocalToGlobalMapping for a particular global to local mapping approach.

1876:    Logically Collective on ISLocalToGlobalMapping

1878:    Input Parameters:
1879: +  ltog - the ISLocalToGlobalMapping object
1880: -  type - a known method

1882:    Options Database Key:
1883: .  -islocaltoglobalmapping_type  <method> - Sets the method; use -help for a list
1884:     of available methods (for instance, basic or hash)

1886:    Notes:
1887:    See "petsc/include/petscis.h" for available methods

1889:   Normally, it is best to use the ISLocalToGlobalMappingSetFromOptions() command and
1890:   then set the ISLocalToGlobalMapping type from the options database rather than by using
1891:   this routine.

1893:   Level: intermediate

1895:   Developer Note: ISLocalToGlobalMappingRegister() is used to add new types to ISLocalToGlobalMappingList from which they
1896:   are accessed by ISLocalToGlobalMappingSetType().

1898: .keywords: ISLocalToGlobalMapping, set, method

1900: .seealso: PCSetType(), ISLocalToGlobalMappingType, ISLocalToGlobalMappingRegister(), ISLocalToGlobalMappingCreate()

1902: @*/
1903: PetscErrorCode  ISLocalToGlobalMappingSetType(ISLocalToGlobalMapping ltog, ISLocalToGlobalMappingType type)
1904: {
1905:   PetscErrorCode ierr,(*r)(ISLocalToGlobalMapping);
1906:   PetscBool      match;


1912:   PetscObjectTypeCompare((PetscObject)ltog,type,&match);
1913:   if (match) return(0);

1915:    PetscFunctionListFind(ISLocalToGlobalMappingList,type,&r);
1916:   if (!r) SETERRQ1(PetscObjectComm((PetscObject)ltog),PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested ISLocalToGlobalMapping type %s",type);
1917:   /* Destroy the previous private LTOG context */
1918:   if (ltog->ops->destroy) {
1919:     (*ltog->ops->destroy)(ltog);
1920:     ltog->ops->destroy = NULL;
1921:   }
1922:   PetscObjectChangeTypeName((PetscObject)ltog,type);
1923:   (*r)(ltog);
1924:   return(0);
1925: }

1927: PetscBool ISLocalToGlobalMappingRegisterAllCalled = PETSC_FALSE;

1929: /*@C
1930:   ISLocalToGlobalMappingRegisterAll - Registers all of the local to global mapping components in the IS package.

1932:   Not Collective

1934:   Level: advanced

1936: .keywords: IS, register, all
1937: .seealso:  ISRegister(),  ISLocalToGlobalRegister()
1938: @*/
1939: PetscErrorCode  ISLocalToGlobalMappingRegisterAll(void)
1940: {

1944:   if (ISLocalToGlobalMappingRegisterAllCalled) return(0);
1945:   ISLocalToGlobalMappingRegisterAllCalled = PETSC_TRUE;

1947:   ISLocalToGlobalMappingRegister(ISLOCALTOGLOBALMAPPINGBASIC, ISLocalToGlobalMappingCreate_Basic);
1948:   ISLocalToGlobalMappingRegister(ISLOCALTOGLOBALMAPPINGHASH, ISLocalToGlobalMappingCreate_Hash);
1949:   return(0);
1950: }