Actual source code: general.c

  1: #define PETSCVEC_DLL
  2: /*
  3:      Provides the functions for index sets (IS) defined by a list of integers.
  4: */
 5:  #include src/vec/is/impls/general/general.h
 6:  #include petscvec.h

 10: PetscErrorCode ISDuplicate_General(IS is,IS *newIS)
 11: {
 13:   IS_General     *sub = (IS_General *)is->data;

 16:   ISCreateGeneral(is->comm,sub->n,sub->idx,newIS);
 17:   return(0);
 18: }

 22: PetscErrorCode ISDestroy_General(IS is)
 23: {
 24:   IS_General     *is_general = (IS_General*)is->data;

 28:   if (is_general->allocated) {
 29:     PetscFree(is_general->idx);
 30:   }
 31:   PetscFree(is_general);
 32:   PetscHeaderDestroy(is);
 33:   return(0);
 34: }

 38: PetscErrorCode ISIdentity_General(IS is,PetscTruth *ident)
 39: {
 40:   IS_General *is_general = (IS_General*)is->data;
 41:   PetscInt   i,n = is_general->n,*idx = is_general->idx;

 44:   is->isidentity = PETSC_TRUE;
 45:   *ident         = PETSC_TRUE;
 46:   for (i=0; i<n; i++) {
 47:     if (idx[i] != i) {
 48:       is->isidentity = PETSC_FALSE;
 49:       *ident         = PETSC_FALSE;
 50:       break;
 51:     }
 52:   }
 53:   return(0);
 54: }

 58: PetscErrorCode ISGetIndices_General(IS in,PetscInt **idx)
 59: {
 60:   IS_General *sub = (IS_General*)in->data;

 63:   *idx = sub->idx;
 64:   return(0);
 65: }

 69: PetscErrorCode ISRestoreIndices_General(IS in,PetscInt **idx)
 70: {
 71:   IS_General *sub = (IS_General*)in->data;

 74:   if (*idx != sub->idx) {
 75:     SETERRQ(PETSC_ERR_ARG_WRONG,"Must restore with value from ISGetIndices()");
 76:   }
 77:   return(0);
 78: }

 82: PetscErrorCode ISGetSize_General(IS is,PetscInt *size)
 83: {
 84:   IS_General *sub = (IS_General *)is->data;

 87:   *size = sub->N;
 88:   return(0);
 89: }

 93: PetscErrorCode ISGetLocalSize_General(IS is,PetscInt *size)
 94: {
 95:   IS_General *sub = (IS_General *)is->data;

 98:   *size = sub->n;
 99:   return(0);
100: }

104: PetscErrorCode ISInvertPermutation_General(IS is,PetscInt nlocal,IS *isout)
105: {
106:   IS_General     *sub = (IS_General *)is->data;
107:   PetscInt       i,*ii,n = sub->n,*idx = sub->idx,nstart;
108:   PetscMPIInt    size;
109:   IS             istmp,nistmp;

113:   MPI_Comm_size(is->comm,&size);
114:   if (size == 1) {
115:     PetscMalloc(n*sizeof(PetscInt),&ii);
116:     for (i=0; i<n; i++) {
117:       ii[idx[i]] = i;
118:     }
119:     ISCreateGeneral(PETSC_COMM_SELF,n,ii,isout);
120:     ISSetPermutation(*isout);
121:     PetscFree(ii);
122:   } else {
123:     /* crude, nonscalable get entire IS on each processor */
124:     if (nlocal == PETSC_DECIDE) SETERRQ(PETSC_ERR_SUP,"Do not yet support nlocal of PETSC_DECIDE");
125:     ISAllGather(is,&istmp);
126:     ISSetPermutation(istmp);
127:     ISInvertPermutation(istmp,PETSC_DECIDE,&nistmp);
128:     ISDestroy(istmp);
129:     /* get the part we need */
130:     MPI_Scan(&nlocal,&nstart,1,MPIU_INT,MPI_SUM,is->comm);
131: #if defined(PETSC_USE_DEBUG)
132:     {
133:       PetscMPIInt rank;
134:       MPI_Comm_rank(is->comm,&rank);
135:       if (rank == size-1) {
136:         if (nstart != sub->N) SETERRQ2(PETSC_ERR_ARG_INCOMP,"Sum of nlocal lengths %d != total IS length %d",nstart,sub->N);
137:       }
138:     }
139: #endif
140:     nstart -= nlocal;
141:     ISGetIndices(nistmp,&idx);
142:     ISCreateGeneral(is->comm,nlocal,idx+nstart,isout);
143:     ISRestoreIndices(nistmp,&idx);
144:     ISDestroy(nistmp);
145:   }
146:   return(0);
147: }

151: PetscErrorCode ISView_General(IS is,PetscViewer viewer)
152: {
153:   IS_General     *sub = (IS_General *)is->data;
155:   PetscInt       i,n = sub->n,*idx = sub->idx;
156:   PetscTruth     iascii;

159:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);
160:   if (iascii) {
161:     MPI_Comm    comm;
162:     PetscMPIInt rank,size;

164:     PetscObjectGetComm((PetscObject)viewer,&comm);
165:     MPI_Comm_rank(comm,&rank);
166:     MPI_Comm_size(comm,&size);

168:     if (size > 1) {
169:       if (is->isperm) {
170:         PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Index set is permutation\n",rank);
171:       }
172:       PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Number of indices in set %D\n",rank,n);
173:       for (i=0; i<n; i++) {
174:         PetscViewerASCIISynchronizedPrintf(viewer,"[%d] %D %D\n",rank,i,idx[i]);
175:       }
176:     } else {
177:       if (is->isperm) {
178:         PetscViewerASCIISynchronizedPrintf(viewer,"Index set is permutation\n");
179:       }
180:       PetscViewerASCIISynchronizedPrintf(viewer,"Number of indices in set %D\n",n);
181:       for (i=0; i<n; i++) {
182:         PetscViewerASCIISynchronizedPrintf(viewer,"%D %D\n",i,idx[i]);
183:       }
184:     }
185:     PetscViewerFlush(viewer);
186:   } else {
187:     SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported for this object",((PetscObject)viewer)->type_name);
188:   }
189:   return(0);
190: }

194: PetscErrorCode ISSort_General(IS is)
195: {
196:   IS_General     *sub = (IS_General *)is->data;

200:   if (sub->sorted) return(0);
201:   PetscSortInt(sub->n,sub->idx);
202:   sub->sorted = PETSC_TRUE;
203:   return(0);
204: }

208: PetscErrorCode ISSorted_General(IS is,PetscTruth *flg)
209: {
210:   IS_General *sub = (IS_General *)is->data;

213:   *flg = sub->sorted;
214:   return(0);
215: }

217: static struct _ISOps myops = { ISGetSize_General,
218:                                ISGetLocalSize_General,
219:                                ISGetIndices_General,
220:                                ISRestoreIndices_General,
221:                                ISInvertPermutation_General,
222:                                ISSort_General,
223:                                ISSorted_General,
224:                                ISDuplicate_General,
225:                                ISDestroy_General,
226:                                ISView_General,
227:                                ISIdentity_General };

231: PetscErrorCode ISCreateGeneral_Private(MPI_Comm comm,IS *is)
232: {
234:   IS             Nindex = *is;
235:   IS_General     *sub = (IS_General*)Nindex->data;
236:   PetscInt       n = sub->n,i,min,max;
237:   const PetscInt *idx = sub->idx;
238:   PetscTruth     sorted = PETSC_TRUE;
239:   PetscTruth     flg;

243:   if (n < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"length < 0");
245:   *is = PETSC_NULL;
246: #ifndef PETSC_USE_DYNAMIC_LIBRARIES
247:   VecInitializePackage(PETSC_NULL);
248: #endif

250:   MPI_Allreduce(&n,&sub->N,1,MPIU_INT,MPI_SUM,comm);
251:   for (i=1; i<n; i++) {
252:     if (idx[i] < idx[i-1]) {sorted = PETSC_FALSE; break;}
253:   }
254:   if (n) {min = max = idx[0];} else {min = max = 0;}
255:   for (i=1; i<n; i++) {
256:     if (idx[i] < min) min = idx[i];
257:     if (idx[i] > max) max = idx[i];
258:   }
259:   sub->sorted     = sorted;
260:   Nindex->min     = min;
261:   Nindex->max     = max;
262:   PetscMemcpy(Nindex->ops,&myops,sizeof(myops));
263:   Nindex->isperm     = PETSC_FALSE;
264:   Nindex->isidentity = PETSC_FALSE;
265:   PetscOptionsHasName(PETSC_NULL,"-is_view",&flg);
266:   if (flg) {
267:     PetscViewer viewer;
268:     PetscViewerASCIIGetStdout(Nindex->comm,&viewer);
269:     ISView(Nindex,viewer);
270:   }
271:   *is = Nindex;
272:   return(0);
273: }


278: /*@
279:    ISCreateGeneral - Creates a data structure for an index set 
280:    containing a list of integers.

282:    Collective on MPI_Comm

284:    Input Parameters:
285: +  comm - the MPI communicator
286: .  n - the length of the index set
287: -  idx - the list of integers

289:    Output Parameter:
290: .  is - the new index set

292:    Notes:
293:    The index array is copied to internally allocated storage. After the call,
294:    the user can free the index array. Use ISCreateGeneralNC() to use the pointers
295:    passed in and NOT make a copy of the index array.

297:    When the communicator is not MPI_COMM_SELF, the operations on IS are NOT
298:    conceptually the same as MPI_Group operations. The IS are then
299:    distributed sets of indices and thus certain operations on them are
300:    collective.

302:    Level: beginner

304:   Concepts: index sets^creating
305:   Concepts: IS^creating

307: .seealso: ISCreateGeneralWithArray(), ISCreateStride(), ISCreateBlock(), ISAllGather(), ISCreateGeneralNC()
308: @*/
309: PetscErrorCode  ISCreateGeneral(MPI_Comm comm,PetscInt n,const PetscInt idx[],IS *is)
310: {
312:   IS             Nindex;
313:   IS_General     *sub;

317:   if (n < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"length < 0");
319:   *is = PETSC_NULL;
320: #ifndef PETSC_USE_DYNAMIC_LIBRARIES
321:   VecInitializePackage(PETSC_NULL);
322: #endif

324:   PetscHeaderCreate(Nindex,_p_IS,struct _ISOps,IS_COOKIE,IS_GENERAL,"IS",comm,ISDestroy,ISView);
325:   PetscNew(IS_General,&sub);
326:   PetscLogObjectMemory(Nindex,sizeof(IS_General)+n*sizeof(PetscInt)+sizeof(struct _p_IS));
327:   Nindex->data   = (void*)sub;
328:   PetscMalloc(n*sizeof(PetscInt),&sub->idx);
329:   PetscMemcpy(sub->idx,idx,n*sizeof(PetscInt));
330:   sub->n         = n;
331:   sub->allocated = PETSC_TRUE;

333:   *is = Nindex;
334:   ISCreateGeneral_Private(comm,is);

336:   return(0);
337: }

341: /*@C
342:    ISCreateGeneralNC - Creates a data structure for an index set 
343:    containing a list of integers.

345:    Collective on MPI_Comm

347:    Input Parameters:
348: +  comm - the MPI communicator
349: .  n - the length of the index set
350: -  idx - the list of integers

352:    Output Parameter:
353: .  is - the new index set

355:    Notes: This routine does not copy the indices, just keeps the pointer to the
356:    indices. The ISDestroy() will free the space so it must be obtained
357:    with PetscMalloc() and it must not be freed nor modified elsewhere.
358:    Use ISCreateGeneral() if you wish to copy the indices passed into the routine.
359:    Use ISCreateGeneralWithArray() to NOT copy the indices and NOT free the space when
360:    ISDestroy() is called.

362:    When the communicator is not MPI_COMM_SELF, the operations on IS are NOT
363:    conceptually the same as MPI_Group operations. The IS are then
364:    distributed sets of indices and thus certain operations on them are
365:    collective.

367:    Level: beginner

369:   Concepts: index sets^creating
370:   Concepts: IS^creating

372: .seealso: ISCreateGeneral(), ISCreateGeneralWithArray(), ISCreateStride(), ISCreateBlock(), ISAllGather()
373: @*/
374: PetscErrorCode  ISCreateGeneralNC(MPI_Comm comm,PetscInt n,const PetscInt idx[],IS *is)
375: {
377:   IS             Nindex;
378:   IS_General     *sub;

382:   if (n < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"length < 0");
384:   *is = PETSC_NULL;
385: #ifndef PETSC_USE_DYNAMIC_LIBRARIES
386:   VecInitializePackage(PETSC_NULL);
387: #endif

389:   PetscHeaderCreate(Nindex,_p_IS,struct _ISOps,IS_COOKIE,IS_GENERAL,"IS",comm,ISDestroy,ISView);
390:   PetscNew(IS_General,&sub);
391:   PetscLogObjectMemory(Nindex,sizeof(IS_General)+n*sizeof(PetscInt)+sizeof(struct _p_IS));
392:   Nindex->data   = (void*)sub;
393:   sub->n         = n;
394:   sub->idx       = (PetscInt*)idx;
395:   sub->allocated = PETSC_TRUE;

397:   *is = Nindex;
398:   ISCreateGeneral_Private(comm,is);

400:   return(0);
401: }

405: /*@C
406:    ISCreateGeneralWithArray - Creates a data structure for an index set 
407:    containing a list of integers.

409:    Collective on MPI_Comm

411:    Input Parameters:
412: +  comm - the MPI communicator
413: .  n - the length of the index set
414: -  idx - the list of integers

416:    Output Parameter:
417: .  is - the new index set

419:    Notes:
420:    Unlike with ISCreateGeneral, the indices are not copied to internally
421:    allocated storage. The user array is not freed by ISDestroy.

423:    When the communicator is not MPI_COMM_SELF, the operations on IS are NOT
424:    conceptually the same as MPI_Group operations. The IS are then
425:    distributed sets of indices and thus certain operations on them are collective.

427:    Level: beginner

429:   Concepts: index sets^creating
430:   Concepts: IS^creating

432: .seealso: ISCreateGeneral(), ISCreateStride(), ISCreateBlock(), ISAllGather()
433: @*/
434: PetscErrorCode  ISCreateGeneralWithArray(MPI_Comm comm,PetscInt n,PetscInt idx[],IS *is)
435: {
437:   IS             Nindex;
438:   IS_General     *sub;

442:   if (n < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"length < 0");
444:   *is = PETSC_NULL;
445: #ifndef PETSC_USE_DYNAMIC_LIBRARIES
446:   VecInitializePackage(PETSC_NULL);
447: #endif

449:   PetscHeaderCreate(Nindex,_p_IS,struct _ISOps,IS_COOKIE,IS_GENERAL,"IS",comm,ISDestroy,ISView);
450:   PetscNew(IS_General,&sub);
451:   PetscLogObjectMemory(Nindex,sizeof(IS_General)+n*sizeof(PetscInt)+sizeof(struct _p_IS));
452:   Nindex->data   = (void*)sub;
453:   sub->n         = n;
454:   sub->idx       = idx;
455:   sub->allocated = PETSC_FALSE;

457:   *is = Nindex;
458:   ISCreateGeneral_Private(comm,is);

460:   return(0);
461: }