Actual source code: isltog.c
1: #define PETSCVEC_DLL
3: #include petscvec.h
4: #include src/vec/is/isimpl.h
6: PetscCookie PETSCVEC_DLLEXPORT IS_LTOGM_COOKIE = -1;
10: /*@C
11: ISLocalToGlobalMappingGetSize - Gets the local size of a local to global mapping.
13: Not Collective
15: Input Parameter:
16: . ltog - local to global mapping
18: Output Parameter:
19: . n - the number of entries in the local mapping
21: Level: advanced
23: Concepts: mapping^local to global
25: .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate()
26: @*/
27: PetscErrorCode PETSCVEC_DLLEXPORT ISLocalToGlobalMappingGetSize(ISLocalToGlobalMapping mapping,PetscInt *n)
28: {
32: *n = mapping->n;
33: return(0);
34: }
38: /*@C
39: ISLocalToGlobalMappingView - View a local to global mapping
41: Not Collective
43: Input Parameters:
44: + ltog - local to global mapping
45: - viewer - viewer
47: Level: advanced
49: Concepts: mapping^local to global
51: .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate()
52: @*/
53: PetscErrorCode PETSCVEC_DLLEXPORT ISLocalToGlobalMappingView(ISLocalToGlobalMapping mapping,PetscViewer viewer)
54: {
55: PetscInt i;
56: PetscMPIInt rank;
57: PetscTruth iascii;
58: PetscErrorCode ierr;
62: if (!viewer) viewer = PETSC_VIEWER_STDOUT_(mapping->comm);
65: MPI_Comm_rank(mapping->comm,&rank);
66: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);
67: if (iascii) {
68: for (i=0; i<mapping->n; i++) {
69: PetscViewerASCIISynchronizedPrintf(viewer,"[%d] %d %d\n",rank,i,mapping->indices[i]);
70: }
71: PetscViewerFlush(viewer);
72: } else {
73: SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported for ISLocalToGlobalMapping",((PetscObject)viewer)->type_name);
74: }
76: return(0);
77: }
81: /*@
82: ISLocalToGlobalMappingCreateIS - Creates a mapping between a local (0 to n)
83: ordering and a global parallel ordering.
85: Not collective
87: Input Parameter:
88: . is - index set containing the global numbers for each local
90: Output Parameter:
91: . mapping - new mapping data structure
93: Level: advanced
95: Concepts: mapping^local to global
97: .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate()
98: @*/
99: PetscErrorCode PETSCVEC_DLLEXPORT ISLocalToGlobalMappingCreateIS(IS is,ISLocalToGlobalMapping *mapping)
100: {
102: PetscInt n,*indices;
103: MPI_Comm comm;
109: PetscObjectGetComm((PetscObject)is,&comm);
110: ISGetLocalSize(is,&n);
111: ISGetIndices(is,&indices);
112: ISLocalToGlobalMappingCreate(comm,n,indices,mapping);
113: ISRestoreIndices(is,&indices);
115: return(0);
116: }
121: /*@
122: ISLocalToGlobalMappingCreate - Creates a mapping between a local (0 to n)
123: ordering and a global parallel ordering.
125: Not Collective, but communicator may have more than one process
127: Input Parameters:
128: + comm - MPI communicator
129: . n - the number of local elements
130: - indices - the global index for each local element
132: Output Parameter:
133: . mapping - new mapping data structure
135: Level: advanced
137: Concepts: mapping^local to global
139: .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreateIS(), ISLocalToGlobalMappingCreateNC()
140: @*/
141: PetscErrorCode PETSCVEC_DLLEXPORT ISLocalToGlobalMappingCreate(MPI_Comm cm,PetscInt n,const PetscInt indices[],ISLocalToGlobalMapping *mapping)
142: {
144: PetscInt *in;
149: PetscMalloc(n*sizeof(PetscInt),&in);
150: PetscMemcpy(in,indices,n*sizeof(PetscInt));
151: ISLocalToGlobalMappingCreateNC(cm,n,in,mapping);
152: return(0);
153: }
157: /*@C
158: ISLocalToGlobalMappingCreateNC - Creates a mapping between a local (0 to n)
159: ordering and a global parallel ordering.
161: Not Collective, but communicator may have more than one process
163: Input Parameters:
164: + comm - MPI communicator
165: . n - the number of local elements
166: - indices - the global index for each local element
168: Output Parameter:
169: . mapping - new mapping data structure
171: Level: developer
173: Notes: Does not copy the indices, just keeps the pointer to the indices. The ISLocalToGlobalMappingDestroy()
174: will free the space so it must be obtained with PetscMalloc() and it must not be freed elsewhere.
176: Concepts: mapping^local to global
178: .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreateIS(), ISLocalToGlobalMappingCreate()
179: @*/
180: PetscErrorCode PETSCVEC_DLLEXPORT ISLocalToGlobalMappingCreateNC(MPI_Comm cm,PetscInt n,const PetscInt indices[],ISLocalToGlobalMapping *mapping)
181: {
185: if (n) {
187: }
189: *mapping = PETSC_NULL;
190: #ifndef PETSC_USE_DYNAMIC_LIBRARIES
191: VecInitializePackage(PETSC_NULL);
192: #endif
193: if (IS_LTOGM_COOKIE == -1) {
194: PetscLogClassRegister(&IS_LTOGM_COOKIE,"IS Local to global mapping");
195: }
197: PetscHeaderCreate(*mapping,_p_ISLocalToGlobalMapping,int,IS_LTOGM_COOKIE,0,"ISLocalToGlobalMapping",
198: cm,ISLocalToGlobalMappingDestroy,ISLocalToGlobalMappingView);
199: PetscLogObjectMemory(*mapping,sizeof(struct _p_ISLocalToGlobalMapping)+n*sizeof(PetscInt));
201: (*mapping)->n = n;
202: (*mapping)->indices = (PetscInt*)indices;
204: /*
205: Do not create the global to local mapping. This is only created if
206: ISGlobalToLocalMapping() is called
207: */
208: (*mapping)->globals = 0;
209: return(0);
210: }
214: /*@
215: ISLocalToGlobalMappingBlock - Creates a blocked index version of an
216: ISLocalToGlobalMapping that is appropriate for MatSetLocalToGlobalMappingBlock()
217: and VecSetLocalToGlobalMappingBlock().
219: Not Collective, but communicator may have more than one process
221: Input Parameters:
222: + inmap - original point-wise mapping
223: - bs - block size
225: Output Parameter:
226: . outmap - block based mapping; the indices are relative to BLOCKS, not individual vector or matrix entries.
228: Level: advanced
230: Concepts: mapping^local to global
232: .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate(), ISLocalToGlobalMappingCreateIS()
233: @*/
234: PetscErrorCode PETSCVEC_DLLEXPORT ISLocalToGlobalMappingBlock(ISLocalToGlobalMapping inmap,PetscInt bs,ISLocalToGlobalMapping *outmap)
235: {
237: PetscInt *ii,i,n;
240: if (bs > 1) {
241: n = inmap->n/bs;
242: if (n*bs != inmap->n) SETERRQ(PETSC_ERR_ARG_INCOMP,"Pointwise mapping length is not divisible by block size");
243: PetscMalloc(n*sizeof(PetscInt),&ii);
244: for (i=0; i<n; i++) {
245: ii[i] = inmap->indices[bs*i]/bs;
246: }
247: ISLocalToGlobalMappingCreate(inmap->comm,n,ii,outmap);
248: PetscFree(ii);
249: } else {
250: *outmap = inmap;
251: PetscObjectReference((PetscObject)inmap);
252: }
253: return(0);
254: }
255:
258: /*@
259: ISLocalToGlobalMappingDestroy - Destroys a mapping between a local (0 to n)
260: ordering and a global parallel ordering.
262: Note Collective
264: Input Parameters:
265: . mapping - mapping data structure
267: Level: advanced
269: .seealso: ISLocalToGlobalMappingCreate()
270: @*/
271: PetscErrorCode PETSCVEC_DLLEXPORT ISLocalToGlobalMappingDestroy(ISLocalToGlobalMapping mapping)
272: {
276: if (--mapping->refct > 0) return(0);
277: if (mapping->refct < 0) {
278: SETERRQ(PETSC_ERR_PLIB,"Mapping already destroyed");
279: }
281: PetscFree(mapping->indices);
282: PetscFree(mapping->globals);
283: PetscHeaderDestroy(mapping);
284: return(0);
285: }
286:
289: /*@
290: ISLocalToGlobalMappingApplyIS - Creates from an IS in the local numbering
291: a new index set using the global numbering defined in an ISLocalToGlobalMapping
292: context.
294: Not collective
296: Input Parameters:
297: + mapping - mapping between local and global numbering
298: - is - index set in local numbering
300: Output Parameters:
301: . newis - index set in global numbering
303: Level: advanced
305: Concepts: mapping^local to global
307: .seealso: ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingCreate(),
308: ISLocalToGlobalMappingDestroy(), ISGlobalToLocalMappingApply()
309: @*/
310: PetscErrorCode PETSCVEC_DLLEXPORT ISLocalToGlobalMappingApplyIS(ISLocalToGlobalMapping mapping,IS is,IS *newis)
311: {
313: PetscInt n,i,*idxin,*idxmap,*idxout,Nmax = mapping->n;
320: ISGetLocalSize(is,&n);
321: ISGetIndices(is,&idxin);
322: idxmap = mapping->indices;
323:
324: PetscMalloc(n*sizeof(PetscInt),&idxout);
325: for (i=0; i<n; i++) {
326: if (idxin[i] >= Nmax) SETERRQ3(PETSC_ERR_ARG_OUTOFRANGE,"Local index %d too large %d (max) at %d",idxin[i],Nmax-1,i);
327: idxout[i] = idxmap[idxin[i]];
328: }
329: ISRestoreIndices(is,&idxin);
330: ISCreateGeneral(PETSC_COMM_SELF,n,idxout,newis);
331: PetscFree(idxout);
332: return(0);
333: }
335: /*MC
336: ISLocalToGlobalMappingApply - Takes a list of integers in a local numbering
337: and converts them to the global numbering.
339: Not collective
341: Input Parameters:
342: + mapping - the local to global mapping context
343: . N - number of integers
344: - in - input indices in local numbering
346: Output Parameter:
347: . out - indices in global numbering
349: Synopsis:
350: PetscErrorCode ISLocalToGlobalMappingApply(ISLocalToGlobalMapping mapping,int N,int in[],int out[])
352: Notes:
353: The in and out array parameters may be identical.
355: Level: advanced
357: .seealso: ISLocalToGlobalMappingCreate(),ISLocalToGlobalMappingDestroy(),
358: ISLocalToGlobalMappingApplyIS(),AOCreateBasic(),AOApplicationToPetsc(),
359: AOPetscToApplication(), ISGlobalToLocalMappingApply()
361: Concepts: mapping^local to global
363: M*/
365: /* -----------------------------------------------------------------------------------------*/
369: /*
370: Creates the global fields in the ISLocalToGlobalMapping structure
371: */
372: static PetscErrorCode ISGlobalToLocalMappingSetUp_Private(ISLocalToGlobalMapping mapping)
373: {
375: PetscInt i,*idx = mapping->indices,n = mapping->n,end,start,*globals;
378: end = 0;
379: start = 100000000;
381: for (i=0; i<n; i++) {
382: if (idx[i] < 0) continue;
383: if (idx[i] < start) start = idx[i];
384: if (idx[i] > end) end = idx[i];
385: }
386: if (start > end) {start = 0; end = -1;}
387: mapping->globalstart = start;
388: mapping->globalend = end;
390: PetscMalloc((end-start+2)*sizeof(PetscInt),&globals);
391: mapping->globals = globals;
392: for (i=0; i<end-start+1; i++) {
393: globals[i] = -1;
394: }
395: for (i=0; i<n; i++) {
396: if (idx[i] < 0) continue;
397: globals[idx[i] - start] = i;
398: }
400: PetscLogObjectMemory(mapping,(end-start+1)*sizeof(PetscInt));
401: return(0);
402: }
406: /*@
407: ISGlobalToLocalMappingApply - Provides the local numbering for a list of integers
408: specified with a global numbering.
410: Not collective
412: Input Parameters:
413: + mapping - mapping between local and global numbering
414: . type - IS_GTOLM_MASK - replaces global indices with no local value with -1
415: IS_GTOLM_DROP - drops the indices with no local value from the output list
416: . n - number of global indices to map
417: - idx - global indices to map
419: Output Parameters:
420: + nout - number of indices in output array (if type == IS_GTOLM_MASK then nout = n)
421: - idxout - local index of each global index, one must pass in an array long enough
422: to hold all the indices. You can call ISGlobalToLocalMappingApply() with
423: idxout == PETSC_NULL to determine the required length (returned in nout)
424: and then allocate the required space and call ISGlobalToLocalMappingApply()
425: a second time to set the values.
427: Notes:
428: Either nout or idxout may be PETSC_NULL. idx and idxout may be identical.
430: This is not scalable in memory usage. Each processor requires O(Nglobal) size
431: array to compute these.
433: Level: advanced
435: Concepts: mapping^global to local
437: .seealso: ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingCreate(),
438: ISLocalToGlobalMappingDestroy()
439: @*/
440: PetscErrorCode PETSCVEC_DLLEXPORT ISGlobalToLocalMappingApply(ISLocalToGlobalMapping mapping,ISGlobalToLocalMappingType type,
441: PetscInt n,const PetscInt idx[],PetscInt *nout,PetscInt idxout[])
442: {
443: PetscInt i,*globals,nf = 0,tmp,start,end;
447: if (!mapping->globals) {
448: ISGlobalToLocalMappingSetUp_Private(mapping);
449: }
450: globals = mapping->globals;
451: start = mapping->globalstart;
452: end = mapping->globalend;
454: if (type == IS_GTOLM_MASK) {
455: if (idxout) {
456: for (i=0; i<n; i++) {
457: if (idx[i] < 0) idxout[i] = idx[i];
458: else if (idx[i] < start) idxout[i] = -1;
459: else if (idx[i] > end) idxout[i] = -1;
460: else idxout[i] = globals[idx[i] - start];
461: }
462: }
463: if (nout) *nout = n;
464: } else {
465: if (idxout) {
466: for (i=0; i<n; i++) {
467: if (idx[i] < 0) continue;
468: if (idx[i] < start) continue;
469: if (idx[i] > end) continue;
470: tmp = globals[idx[i] - start];
471: if (tmp < 0) continue;
472: idxout[nf++] = tmp;
473: }
474: } else {
475: for (i=0; i<n; i++) {
476: if (idx[i] < 0) continue;
477: if (idx[i] < start) continue;
478: if (idx[i] > end) continue;
479: tmp = globals[idx[i] - start];
480: if (tmp < 0) continue;
481: nf++;
482: }
483: }
484: if (nout) *nout = nf;
485: }
487: return(0);
488: }
492: /*@C
493: ISLocalToGlobalMappingGetInfo - Gets the neighbor information for each processor and
494: each index shared by more than one processor
496: Collective on ISLocalToGlobalMapping
498: Input Parameters:
499: . mapping - the mapping from local to global indexing
501: Output Parameter:
502: + nproc - number of processors that are connected to this one
503: . proc - neighboring processors
504: . numproc - number of indices for each subdomain (processor)
505: - indices - indices of local nodes shared with neighbor (sorted by global numbering)
507: Level: advanced
509: Concepts: mapping^local to global
511: .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreateIS(), ISLocalToGlobalMappingCreate(),
512: ISLocalToGlobalMappingRestoreInfo()
513: @*/
514: PetscErrorCode PETSCVEC_DLLEXPORT ISLocalToGlobalMappingGetInfo(ISLocalToGlobalMapping mapping,PetscInt *nproc,PetscInt *procs[],PetscInt *numprocs[],PetscInt **indices[])
515: {
517: PetscMPIInt size,rank,tag1,tag2,tag3,*len,*source,imdex;
518: PetscInt i,n = mapping->n,Ng,ng,max = 0,*lindices = mapping->indices;
519: PetscInt *nprocs,*owner,nsends,*sends,j,*starts,nmax,nrecvs,*recvs,proc;
520: PetscInt cnt,scale,*ownedsenders,*nownedsenders,rstart,nowned;
521: PetscInt node,nownedm,nt,*sends2,nsends2,*starts2,*lens2,*dest,nrecvs2,*starts3,*recvs2,k,*bprocs,*tmp;
522: PetscInt first_procs,first_numprocs,*first_indices;
523: MPI_Request *recv_waits,*send_waits;
524: MPI_Status recv_status,*send_status,*recv_statuses;
525: MPI_Comm comm = mapping->comm;
526: PetscTruth debug = PETSC_FALSE;
529: MPI_Comm_size(comm,&size);
530: MPI_Comm_rank(comm,&rank);
531: if (size == 1) {
532: *nproc = 0;
533: *procs = PETSC_NULL;
534: PetscMalloc(sizeof(PetscInt),numprocs);
535: (*numprocs)[0] = 0;
536: PetscMalloc(sizeof(PetscInt*),indices);
537: (*indices)[0] = PETSC_NULL;
538: return(0);
539: }
541: PetscOptionsHasName(PETSC_NULL,"-islocaltoglobalmappinggetinfo_debug",&debug);
543: /*
544: Notes on ISLocalToGlobalMappingGetInfo
546: globally owned node - the nodes that have been assigned to this processor in global
547: numbering, just for this routine.
549: nontrivial globally owned node - node assigned to this processor that is on a subdomain
550: boundary (i.e. is has more than one local owner)
552: locally owned node - node that exists on this processors subdomain
554: nontrivial locally owned node - node that is not in the interior (i.e. has more than one
555: local subdomain
556: */
557: PetscObjectGetNewTag((PetscObject)mapping,&tag1);
558: PetscObjectGetNewTag((PetscObject)mapping,&tag2);
559: PetscObjectGetNewTag((PetscObject)mapping,&tag3);
561: for (i=0; i<n; i++) {
562: if (lindices[i] > max) max = lindices[i];
563: }
564: MPI_Allreduce(&max,&Ng,1,MPIU_INT,MPI_MAX,comm);
565: Ng++;
566: MPI_Comm_size(comm,&size);
567: MPI_Comm_rank(comm,&rank);
568: scale = Ng/size + 1;
569: ng = scale; if (rank == size-1) ng = Ng - scale*(size-1); ng = PetscMax(1,ng);
570: rstart = scale*rank;
572: /* determine ownership ranges of global indices */
573: PetscMalloc(2*size*sizeof(PetscInt),&nprocs);
574: PetscMemzero(nprocs,2*size*sizeof(PetscInt));
576: /* determine owners of each local node */
577: PetscMalloc(n*sizeof(PetscInt),&owner);
578: for (i=0; i<n; i++) {
579: proc = lindices[i]/scale; /* processor that globally owns this index */
580: nprocs[2*proc+1] = 1; /* processor globally owns at least one of ours */
581: owner[i] = proc;
582: nprocs[2*proc]++; /* count of how many that processor globally owns of ours */
583: }
584: nsends = 0; for (i=0; i<size; i++) nsends += nprocs[2*i+1];
585: PetscInfo1(0,"Number of global owners for my local data %d\n",nsends);
587: /* inform other processors of number of messages and max length*/
588: PetscMaxSum(comm,nprocs,&nmax,&nrecvs);
589: PetscInfo1(0,"Number of local owners for my global data %d\n",nrecvs);
591: /* post receives for owned rows */
592: PetscMalloc((2*nrecvs+1)*(nmax+1)*sizeof(PetscInt),&recvs);
593: PetscMalloc((nrecvs+1)*sizeof(MPI_Request),&recv_waits);
594: for (i=0; i<nrecvs; i++) {
595: MPI_Irecv(recvs+2*nmax*i,2*nmax,MPIU_INT,MPI_ANY_SOURCE,tag1,comm,recv_waits+i);
596: }
598: /* pack messages containing lists of local nodes to owners */
599: PetscMalloc((2*n+1)*sizeof(PetscInt),&sends);
600: PetscMalloc((size+1)*sizeof(PetscInt),&starts);
601: starts[0] = 0;
602: for (i=1; i<size; i++) { starts[i] = starts[i-1] + 2*nprocs[2*i-2];}
603: for (i=0; i<n; i++) {
604: sends[starts[owner[i]]++] = lindices[i];
605: sends[starts[owner[i]]++] = i;
606: }
607: PetscFree(owner);
608: starts[0] = 0;
609: for (i=1; i<size; i++) { starts[i] = starts[i-1] + 2*nprocs[2*i-2];}
611: /* send the messages */
612: PetscMalloc((nsends+1)*sizeof(MPI_Request),&send_waits);
613: PetscMalloc((nsends+1)*sizeof(PetscInt),&dest);
614: cnt = 0;
615: for (i=0; i<size; i++) {
616: if (nprocs[2*i]) {
617: MPI_Isend(sends+starts[i],2*nprocs[2*i],MPIU_INT,i,tag1,comm,send_waits+cnt);
618: dest[cnt] = i;
619: cnt++;
620: }
621: }
622: PetscFree(starts);
624: /* wait on receives */
625: PetscMalloc((nrecvs+1)*sizeof(PetscMPIInt),&source);
626: PetscMalloc((nrecvs+1)*sizeof(PetscMPIInt),&len);
627: cnt = nrecvs;
628: PetscMalloc((ng+1)*sizeof(PetscInt),&nownedsenders);
629: PetscMemzero(nownedsenders,ng*sizeof(PetscInt));
630: while (cnt) {
631: MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);
632: /* unpack receives into our local space */
633: MPI_Get_count(&recv_status,MPIU_INT,&len[imdex]);
634: source[imdex] = recv_status.MPI_SOURCE;
635: len[imdex] = len[imdex]/2;
636: /* count how many local owners for each of my global owned indices */
637: for (i=0; i<len[imdex]; i++) nownedsenders[recvs[2*imdex*nmax+2*i]-rstart]++;
638: cnt--;
639: }
640: PetscFree(recv_waits);
642: /* count how many globally owned indices are on an edge multiplied by how many processors own them. */
643: nowned = 0;
644: nownedm = 0;
645: for (i=0; i<ng; i++) {
646: if (nownedsenders[i] > 1) {nownedm += nownedsenders[i]; nowned++;}
647: }
649: /* create single array to contain rank of all local owners of each globally owned index */
650: PetscMalloc((nownedm+1)*sizeof(PetscInt),&ownedsenders);
651: PetscMalloc((ng+1)*sizeof(PetscInt),&starts);
652: starts[0] = 0;
653: for (i=1; i<ng; i++) {
654: if (nownedsenders[i-1] > 1) starts[i] = starts[i-1] + nownedsenders[i-1];
655: else starts[i] = starts[i-1];
656: }
658: /* for each nontrival globally owned node list all arriving processors */
659: for (i=0; i<nrecvs; i++) {
660: for (j=0; j<len[i]; j++) {
661: node = recvs[2*i*nmax+2*j]-rstart;
662: if (nownedsenders[node] > 1) {
663: ownedsenders[starts[node]++] = source[i];
664: }
665: }
666: }
668: if (debug) { /* ----------------------------------- */
669: starts[0] = 0;
670: for (i=1; i<ng; i++) {
671: if (nownedsenders[i-1] > 1) starts[i] = starts[i-1] + nownedsenders[i-1];
672: else starts[i] = starts[i-1];
673: }
674: for (i=0; i<ng; i++) {
675: if (nownedsenders[i] > 1) {
676: PetscSynchronizedPrintf(comm,"[%d] global node %d local owner processors: ",rank,i+rstart);
677: for (j=0; j<nownedsenders[i]; j++) {
678: PetscSynchronizedPrintf(comm,"%d ",ownedsenders[starts[i]+j]);
679: }
680: PetscSynchronizedPrintf(comm,"\n");
681: }
682: }
683: PetscSynchronizedFlush(comm);
684: }/* ----------------------------------- */
686: /* wait on original sends */
687: if (nsends) {
688: PetscMalloc(nsends*sizeof(MPI_Status),&send_status);
689: MPI_Waitall(nsends,send_waits,send_status);
690: PetscFree(send_status);
691: }
692: PetscFree(send_waits);
693: PetscFree(sends);
694: PetscFree(nprocs);
696: /* pack messages to send back to local owners */
697: starts[0] = 0;
698: for (i=1; i<ng; i++) {
699: if (nownedsenders[i-1] > 1) starts[i] = starts[i-1] + nownedsenders[i-1];
700: else starts[i] = starts[i-1];
701: }
702: nsends2 = nrecvs;
703: PetscMalloc((nsends2+1)*sizeof(PetscInt),&nprocs); /* length of each message */
704: for (i=0; i<nrecvs; i++) {
705: nprocs[i] = 1;
706: for (j=0; j<len[i]; j++) {
707: node = recvs[2*i*nmax+2*j]-rstart;
708: if (nownedsenders[node] > 1) {
709: nprocs[i] += 2 + nownedsenders[node];
710: }
711: }
712: }
713: nt = 0; for (i=0; i<nsends2; i++) nt += nprocs[i];
714: PetscMalloc((nt+1)*sizeof(PetscInt),&sends2);
715: PetscMalloc((nsends2+1)*sizeof(PetscInt),&starts2);
716: starts2[0] = 0; for (i=1; i<nsends2; i++) starts2[i] = starts2[i-1] + nprocs[i-1];
717: /*
718: Each message is 1 + nprocs[i] long, and consists of
719: (0) the number of nodes being sent back
720: (1) the local node number,
721: (2) the number of processors sharing it,
722: (3) the processors sharing it
723: */
724: for (i=0; i<nsends2; i++) {
725: cnt = 1;
726: sends2[starts2[i]] = 0;
727: for (j=0; j<len[i]; j++) {
728: node = recvs[2*i*nmax+2*j]-rstart;
729: if (nownedsenders[node] > 1) {
730: sends2[starts2[i]]++;
731: sends2[starts2[i]+cnt++] = recvs[2*i*nmax+2*j+1];
732: sends2[starts2[i]+cnt++] = nownedsenders[node];
733: PetscMemcpy(&sends2[starts2[i]+cnt],&ownedsenders[starts[node]],nownedsenders[node]*sizeof(PetscInt));
734: cnt += nownedsenders[node];
735: }
736: }
737: }
739: /* receive the message lengths */
740: nrecvs2 = nsends;
741: PetscMalloc((nrecvs2+1)*sizeof(PetscInt),&lens2);
742: PetscMalloc((nrecvs2+1)*sizeof(PetscInt),&starts3);
743: PetscMalloc((nrecvs2+1)*sizeof(MPI_Request),&recv_waits);
744: for (i=0; i<nrecvs2; i++) {
745: MPI_Irecv(&lens2[i],1,MPIU_INT,dest[i],tag2,comm,recv_waits+i);
746: }
748: /* send the message lengths */
749: for (i=0; i<nsends2; i++) {
750: MPI_Send(&nprocs[i],1,MPIU_INT,source[i],tag2,comm);
751: }
753: /* wait on receives of lens */
754: if (nrecvs2) {
755: PetscMalloc(nrecvs2*sizeof(MPI_Status),&recv_statuses);
756: MPI_Waitall(nrecvs2,recv_waits,recv_statuses);
757: PetscFree(recv_statuses);
758: }
759: PetscFree(recv_waits);
761: starts3[0] = 0;
762: nt = 0;
763: for (i=0; i<nrecvs2-1; i++) {
764: starts3[i+1] = starts3[i] + lens2[i];
765: nt += lens2[i];
766: }
767: nt += lens2[nrecvs2-1];
769: PetscMalloc((nt+1)*sizeof(PetscInt),&recvs2);
770: PetscMalloc((nrecvs2+1)*sizeof(MPI_Request),&recv_waits);
771: for (i=0; i<nrecvs2; i++) {
772: MPI_Irecv(recvs2+starts3[i],lens2[i],MPIU_INT,dest[i],tag3,comm,recv_waits+i);
773: }
774:
775: /* send the messages */
776: PetscMalloc((nsends2+1)*sizeof(MPI_Request),&send_waits);
777: for (i=0; i<nsends2; i++) {
778: MPI_Isend(sends2+starts2[i],nprocs[i],MPIU_INT,source[i],tag3,comm,send_waits+i);
779: }
781: /* wait on receives */
782: if (nrecvs2) {
783: PetscMalloc(nrecvs2*sizeof(MPI_Status),&recv_statuses);
784: MPI_Waitall(nrecvs2,recv_waits,recv_statuses);
785: PetscFree(recv_statuses);
786: }
787: PetscFree(recv_waits);
788: PetscFree(nprocs);
790: if (debug) { /* ----------------------------------- */
791: cnt = 0;
792: for (i=0; i<nrecvs2; i++) {
793: nt = recvs2[cnt++];
794: for (j=0; j<nt; j++) {
795: PetscSynchronizedPrintf(comm,"[%d] local node %d number of subdomains %d: ",rank,recvs2[cnt],recvs2[cnt+1]);
796: for (k=0; k<recvs2[cnt+1]; k++) {
797: PetscSynchronizedPrintf(comm,"%d ",recvs2[cnt+2+k]);
798: }
799: cnt += 2 + recvs2[cnt+1];
800: PetscSynchronizedPrintf(comm,"\n");
801: }
802: }
803: PetscSynchronizedFlush(comm);
804: } /* ----------------------------------- */
806: /* count number subdomains for each local node */
807: PetscMalloc(size*sizeof(PetscInt),&nprocs);
808: PetscMemzero(nprocs,size*sizeof(PetscInt));
809: cnt = 0;
810: for (i=0; i<nrecvs2; i++) {
811: nt = recvs2[cnt++];
812: for (j=0; j<nt; j++) {
813: for (k=0; k<recvs2[cnt+1]; k++) {
814: nprocs[recvs2[cnt+2+k]]++;
815: }
816: cnt += 2 + recvs2[cnt+1];
817: }
818: }
819: nt = 0; for (i=0; i<size; i++) nt += (nprocs[i] > 0);
820: *nproc = nt;
821: PetscMalloc((nt+1)*sizeof(PetscInt),procs);
822: PetscMalloc((nt+1)*sizeof(PetscInt),numprocs);
823: PetscMalloc((nt+1)*sizeof(PetscInt*),indices);
824: PetscMalloc(size*sizeof(PetscInt),&bprocs);
825: cnt = 0;
826: for (i=0; i<size; i++) {
827: if (nprocs[i] > 0) {
828: bprocs[i] = cnt;
829: (*procs)[cnt] = i;
830: (*numprocs)[cnt] = nprocs[i];
831: PetscMalloc(nprocs[i]*sizeof(PetscInt),&(*indices)[cnt]);
832: cnt++;
833: }
834: }
836: /* make the list of subdomains for each nontrivial local node */
837: PetscMemzero(*numprocs,nt*sizeof(PetscInt));
838: cnt = 0;
839: for (i=0; i<nrecvs2; i++) {
840: nt = recvs2[cnt++];
841: for (j=0; j<nt; j++) {
842: for (k=0; k<recvs2[cnt+1]; k++) {
843: (*indices)[bprocs[recvs2[cnt+2+k]]][(*numprocs)[bprocs[recvs2[cnt+2+k]]]++] = recvs2[cnt];
844: }
845: cnt += 2 + recvs2[cnt+1];
846: }
847: }
848: PetscFree(bprocs);
849: PetscFree(recvs2);
851: /* sort the node indexing by their global numbers */
852: nt = *nproc;
853: for (i=0; i<nt; i++) {
854: PetscMalloc(((*numprocs)[i])*sizeof(PetscInt),&tmp);
855: for (j=0; j<(*numprocs)[i]; j++) {
856: tmp[j] = lindices[(*indices)[i][j]];
857: }
858: PetscSortIntWithArray((*numprocs)[i],tmp,(*indices)[i]);
859: PetscFree(tmp);
860: }
862: if (debug) { /* ----------------------------------- */
863: nt = *nproc;
864: for (i=0; i<nt; i++) {
865: PetscSynchronizedPrintf(comm,"[%d] subdomain %d number of indices %d: ",rank,(*procs)[i],(*numprocs)[i]);
866: for (j=0; j<(*numprocs)[i]; j++) {
867: PetscSynchronizedPrintf(comm,"%d ",(*indices)[i][j]);
868: }
869: PetscSynchronizedPrintf(comm,"\n");
870: }
871: PetscSynchronizedFlush(comm);
872: } /* ----------------------------------- */
874: /* wait on sends */
875: if (nsends2) {
876: PetscMalloc(nsends2*sizeof(MPI_Status),&send_status);
877: MPI_Waitall(nsends2,send_waits,send_status);
878: PetscFree(send_status);
879: }
881: PetscFree(starts3);
882: PetscFree(dest);
883: PetscFree(send_waits);
885: PetscFree(nownedsenders);
886: PetscFree(ownedsenders);
887: PetscFree(starts);
888: PetscFree(starts2);
889: PetscFree(lens2);
891: PetscFree(source);
892: PetscFree(len);
893: PetscFree(recvs);
894: PetscFree(nprocs);
895: PetscFree(sends2);
897: /* put the information about myself as the first entry in the list */
898: first_procs = (*procs)[0];
899: first_numprocs = (*numprocs)[0];
900: first_indices = (*indices)[0];
901: for (i=0; i<*nproc; i++) {
902: if ((*procs)[i] == rank) {
903: (*procs)[0] = (*procs)[i];
904: (*numprocs)[0] = (*numprocs)[i];
905: (*indices)[0] = (*indices)[i];
906: (*procs)[i] = first_procs;
907: (*numprocs)[i] = first_numprocs;
908: (*indices)[i] = first_indices;
909: break;
910: }
911: }
912: return(0);
913: }
917: /*@C
918: ISLocalToGlobalMappingRestoreInfo - Frees the memory allocated by ISLocalToGlobalMappingGetInfo()
920: Collective on ISLocalToGlobalMapping
922: Input Parameters:
923: . mapping - the mapping from local to global indexing
925: Output Parameter:
926: + nproc - number of processors that are connected to this one
927: . proc - neighboring processors
928: . numproc - number of indices for each processor
929: - indices - indices of local nodes shared with neighbor (sorted by global numbering)
931: Level: advanced
933: .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreateIS(), ISLocalToGlobalMappingCreate(),
934: ISLocalToGlobalMappingGetInfo()
935: @*/
936: PetscErrorCode PETSCVEC_DLLEXPORT ISLocalToGlobalMappingRestoreInfo(ISLocalToGlobalMapping mapping,PetscInt *nproc,PetscInt *procs[],PetscInt *numprocs[],PetscInt **indices[])
937: {
939: PetscInt i;
942: PetscFree(*procs);
943: PetscFree(*numprocs);
944: if (*indices) {
945: PetscFree((*indices)[0]);
946: for (i=1; i<*nproc; i++) {
947: PetscFree((*indices)[i]);
948: }
949: PetscFree(*indices);
950: }
951: return(0);
952: }