Actual source code: stride.c

petsc-master 2017-12-15
Report Typos and Errors

  2: /*
  3:        Index sets of evenly space integers, defined by a
  4:     start, stride and length.
  5: */
  6:  #include <petsc/private/isimpl.h>
  7:  #include <petscvec.h>
  8:  #include <petscviewer.h>

 10: typedef struct {
 11:   PetscInt N,n,first,step;
 12: } IS_Stride;

 14: PetscErrorCode ISIdentity_Stride(IS is,PetscBool  *ident)
 15: {
 16:   IS_Stride *is_stride = (IS_Stride*)is->data;

 19:   is->isidentity = PETSC_FALSE;
 20:   *ident         = PETSC_FALSE;
 21:   if (is_stride->first != 0) return(0);
 22:   if (is_stride->step  != 1) return(0);
 23:   *ident         = PETSC_TRUE;
 24:   is->isidentity = PETSC_TRUE;
 25:   return(0);
 26: }

 28: static PetscErrorCode ISCopy_Stride(IS is,IS isy)
 29: {
 30:   IS_Stride      *is_stride = (IS_Stride*)is->data,*isy_stride = (IS_Stride*)isy->data;

 34:   PetscMemcpy(isy_stride,is_stride,sizeof(IS_Stride));
 35:   return(0);
 36: }

 38: PetscErrorCode ISDuplicate_Stride(IS is,IS *newIS)
 39: {
 41:   IS_Stride      *sub = (IS_Stride*)is->data;

 44:   ISCreateStride(PetscObjectComm((PetscObject)is),sub->n,sub->first,sub->step,newIS);
 45:   return(0);
 46: }

 48: PetscErrorCode ISInvertPermutation_Stride(IS is,PetscInt nlocal,IS *perm)
 49: {
 50:   IS_Stride      *isstride = (IS_Stride*)is->data;

 54:   if (is->isidentity) {
 55:     ISCreateStride(PETSC_COMM_SELF,isstride->n,0,1,perm);
 56:   } else {
 57:     IS             tmp;
 58:     const PetscInt *indices,n = isstride->n;
 59:     ISGetIndices(is,&indices);
 60:     ISCreateGeneral(PetscObjectComm((PetscObject)is),n,indices,PETSC_COPY_VALUES,&tmp);
 61:     ISSetPermutation(tmp);
 62:     ISRestoreIndices(is,&indices);
 63:     ISInvertPermutation(tmp,nlocal,perm);
 64:     ISDestroy(&tmp);
 65:   }
 66:   return(0);
 67: }

 69: /*@
 70:    ISStrideGetInfo - Returns the first index in a stride index set and
 71:    the stride width.

 73:    Not Collective

 75:    Input Parameter:
 76: .  is - the index set

 78:    Output Parameters:
 79: .  first - the first index
 80: .  step - the stride width

 82:    Level: intermediate

 84:    Notes:
 85:    Returns info on stride index set. This is a pseudo-public function that
 86:    should not be needed by most users.

 88:    Concepts: index sets^getting information
 89:    Concepts: IS^getting information

 91: .seealso: ISCreateStride(), ISGetSize()
 92: @*/
 93: PetscErrorCode  ISStrideGetInfo(IS is,PetscInt *first,PetscInt *step)
 94: {
 95:   IS_Stride      *sub;
 96:   PetscBool      flg;

103:   PetscObjectTypeCompare((PetscObject)is,ISSTRIDE,&flg);
104:   if (!flg) SETERRQ(PetscObjectComm((PetscObject)is),PETSC_ERR_ARG_WRONG,"IS must be of type ISSTRIDE");

106:   sub = (IS_Stride*)is->data;
107:   if (first) *first = sub->first;
108:   if (step)  *step  = sub->step;
109:   return(0);
110: }

112: PetscErrorCode ISDestroy_Stride(IS is)
113: {

117:   PetscObjectComposeFunction((PetscObject)is,"ISStrideSetStride_C",NULL);
118:   PetscFree(is->data);
119:   return(0);
120: }

122: PetscErrorCode  ISToGeneral_Stride(IS inis)
123: {
125:   const PetscInt *idx;
126:   PetscInt       n;

129:   ISGetLocalSize(inis,&n);
130:   ISGetIndices(inis,&idx);
131:   ISSetType(inis,ISGENERAL);
132:   ISGeneralSetIndices(inis,n,idx,PETSC_OWN_POINTER);
133:   return(0);
134: }

136: PetscErrorCode ISLocate_Stride(IS is,PetscInt key,PetscInt *location)
137: {
138:   IS_Stride      *sub = (IS_Stride*)is->data;
139:   PetscInt       rem, step;

142:   *location = -1;
143:   step      = sub->step;
144:   key      -= sub->first;
145:   rem       = key / step;
146:   if ((rem < sub->n) && !(key % step)) {
147:     *location = rem;
148:   }
149:   return(0);
150: }

152: /*
153:      Returns a legitimate index memory even if
154:    the stride index set is empty.
155: */
156: PetscErrorCode ISGetIndices_Stride(IS in,const PetscInt *idx[])
157: {
158:   IS_Stride      *sub = (IS_Stride*)in->data;
160:   PetscInt       i,**dx = (PetscInt**)idx;

163:   PetscMalloc1(sub->n,(PetscInt**)idx);
164:   if (sub->n) {
165:     (*dx)[0] = sub->first;
166:     for (i=1; i<sub->n; i++) (*dx)[i] = (*dx)[i-1] + sub->step;
167:   }
168:   return(0);
169: }

171: PetscErrorCode ISRestoreIndices_Stride(IS in,const PetscInt *idx[])
172: {

176:   PetscFree(*(void**)idx);
177:   return(0);
178: }

180: PetscErrorCode ISGetSize_Stride(IS is,PetscInt *size)
181: {
182:   IS_Stride *sub = (IS_Stride*)is->data;

185:   *size = sub->N;
186:   return(0);
187: }

189: PetscErrorCode ISGetLocalSize_Stride(IS is,PetscInt *size)
190: {
191:   IS_Stride *sub = (IS_Stride*)is->data;

194:   *size = sub->n;
195:   return(0);
196: }

198: PetscErrorCode ISView_Stride(IS is,PetscViewer viewer)
199: {
200:   IS_Stride      *sub = (IS_Stride*)is->data;
201:   PetscInt       i,n = sub->n;
202:   PetscMPIInt    rank,size;
203:   PetscBool      iascii;

207:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
208:   if (iascii) {
209:     MPI_Comm_rank(PetscObjectComm((PetscObject)is),&rank);
210:     MPI_Comm_size(PetscObjectComm((PetscObject)is),&size);
211:     if (size == 1) {
212:       if (is->isperm) {
213:         PetscViewerASCIIPrintf(viewer,"Index set is permutation\n");
214:       }
215:       PetscViewerASCIIPrintf(viewer,"Number of indices in (stride) set %D\n",n);
216:       for (i=0; i<n; i++) {
217:         PetscViewerASCIIPrintf(viewer,"%D %D\n",i,sub->first + i*sub->step);
218:       }
219:       PetscViewerFlush(viewer);
220:     } else {
221:       PetscViewerASCIIPushSynchronized(viewer);
222:       if (is->isperm) {
223:         PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Index set is permutation\n",rank);
224:       }
225:       PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Number of indices in (stride) set %D\n",rank,n);
226:       for (i=0; i<n; i++) {
227:         PetscViewerASCIISynchronizedPrintf(viewer,"[%d] %D %D\n",rank,i,sub->first + i*sub->step);
228:       }
229:       PetscViewerFlush(viewer);
230:       PetscViewerASCIIPopSynchronized(viewer);
231:     }
232:   }
233:   return(0);
234: }

236: PetscErrorCode ISSort_Stride(IS is)
237: {
238:   IS_Stride *sub = (IS_Stride*)is->data;

241:   if (sub->step >= 0) return(0);
242:   sub->first += (sub->n - 1)*sub->step;
243:   sub->step  *= -1;
244:   return(0);
245: }

247: PetscErrorCode ISSorted_Stride(IS is,PetscBool * flg)
248: {
249:   IS_Stride *sub = (IS_Stride*)is->data;

252:   if (sub->step >= 0) *flg = PETSC_TRUE;
253:   else *flg = PETSC_FALSE;
254:   return(0);
255: }

257: static PetscErrorCode ISOnComm_Stride(IS is,MPI_Comm comm,PetscCopyMode mode,IS *newis)
258: {
260:   IS_Stride      *sub = (IS_Stride*)is->data;

263:   ISCreateStride(comm,sub->n,sub->first,sub->step,newis);
264:   return(0);
265: }

267: static PetscErrorCode ISSetBlockSize_Stride(IS is,PetscInt bs)
268: {
269:   IS_Stride     *sub = (IS_Stride*)is->data;

273:   if (sub->step != 1 && bs != 1) SETERRQ2(PetscObjectComm((PetscObject)is),PETSC_ERR_ARG_SIZ,"ISSTRIDE has stride %D, cannot be blocked of size %D",sub->step,bs);
274:   PetscLayoutSetBlockSize(is->map, bs);
275:   return(0);
276: }

278: static PetscErrorCode ISContiguousLocal_Stride(IS is,PetscInt gstart,PetscInt gend,PetscInt *start,PetscBool *contig)
279: {
280:   IS_Stride *sub = (IS_Stride*)is->data;

283:   if (sub->step == 1 && sub->first >= gstart && sub->first+sub->n <= gend) {
284:     *start  = sub->first - gstart;
285:     *contig = PETSC_TRUE;
286:   } else {
287:     *start  = -1;
288:     *contig = PETSC_FALSE;
289:   }
290:   return(0);
291: }


294: static struct _ISOps myops = { ISGetSize_Stride,
295:                                ISGetLocalSize_Stride,
296:                                ISGetIndices_Stride,
297:                                ISRestoreIndices_Stride,
298:                                ISInvertPermutation_Stride,
299:                                ISSort_Stride,
300:                                ISSort_Stride,
301:                                ISSorted_Stride,
302:                                ISDuplicate_Stride,
303:                                ISDestroy_Stride,
304:                                ISView_Stride,
305:                                ISLoad_Default,
306:                                ISIdentity_Stride,
307:                                ISCopy_Stride,
308:                                ISToGeneral_Stride,
309:                                ISOnComm_Stride,
310:                                ISSetBlockSize_Stride,
311:                                ISContiguousLocal_Stride,
312:                                ISLocate_Stride};


315: /*@
316:    ISStrideSetStride - Sets the stride information for a stride index set.

318:    Collective on IS

320:    Input Parameters:
321: +  is - the index set
322: .  n - the length of the locally owned portion of the index set
323: .  first - the first element of the locally owned portion of the index set
324: -  step - the change to the next index

326:    Level: beginner

328:   Concepts: IS^stride
329:   Concepts: index sets^stride
330:   Concepts: stride^index set

332: .seealso: ISCreateGeneral(), ISCreateBlock(), ISAllGather()
333: @*/
334: PetscErrorCode  ISStrideSetStride(IS is,PetscInt n,PetscInt first,PetscInt step)
335: {

339:   if (n < 0) SETERRQ1(PetscObjectComm((PetscObject)is), PETSC_ERR_ARG_OUTOFRANGE, "Negative length %d not valid", n);
340:   PetscUseMethod(is,"ISStrideSetStride_C",(IS,PetscInt,PetscInt,PetscInt),(is,n,first,step));
341:   return(0);
342: }

344: PetscErrorCode  ISStrideSetStride_Stride(IS is,PetscInt n,PetscInt first,PetscInt step)
345: {
347:   PetscInt       min,max;
348:   IS_Stride      *sub = (IS_Stride*)is->data;

351:   sub->n     = n;
352:   MPIU_Allreduce(&n,&sub->N,1,MPIU_INT,MPI_SUM,PetscObjectComm((PetscObject)is));
353:   sub->first = first;
354:   sub->step  = step;
355:   if (step > 0) {min = first; max = first + step*(n-1);}
356:   else          {max = first; min = first + step*(n-1);}

358:   is->min  = n > 0 ? min : PETSC_MAX_INT;
359:   is->max  = n > 0 ? max : PETSC_MIN_INT;
360:   is->data = (void*)sub;

362:   if ((!first && step == 1) || (first == max && step == -1 && !min)) is->isperm = PETSC_TRUE;
363:   else is->isperm = PETSC_FALSE;
364:   is->isidentity = PETSC_FALSE;
365:   return(0);
366: }

368: /*@
369:    ISCreateStride - Creates a data structure for an index set
370:    containing a list of evenly spaced integers.

372:    Collective on MPI_Comm

374:    Input Parameters:
375: +  comm - the MPI communicator
376: .  n - the length of the locally owned portion of the index set
377: .  first - the first element of the locally owned portion of the index set
378: -  step - the change to the next index

380:    Output Parameter:
381: .  is - the new index set

383:    Notes:
384:    When the communicator is not MPI_COMM_SELF, the operations on IS are NOT
385:    conceptually the same as MPI_Group operations. The IS are the
386:    distributed sets of indices and thus certain operations on them are collective.

388:    Level: beginner

390:   Concepts: IS^stride
391:   Concepts: index sets^stride
392:   Concepts: stride^index set

394: .seealso: ISCreateGeneral(), ISCreateBlock(), ISAllGather()
395: @*/
396: PetscErrorCode  ISCreateStride(MPI_Comm comm,PetscInt n,PetscInt first,PetscInt step,IS *is)
397: {

401:   ISCreate(comm,is);
402:   ISSetType(*is,ISSTRIDE);
403:   ISStrideSetStride(*is,n,first,step);
404:   return(0);
405: }

407: PETSC_EXTERN PetscErrorCode ISCreate_Stride(IS is)
408: {
410:   IS_Stride      *sub;

413:   PetscNewLog(is,&sub);
414:   is->data = (void *) sub;
415:   PetscMemcpy(is->ops,&myops,sizeof(myops));
416:   PetscObjectComposeFunction((PetscObject)is,"ISStrideSetStride_C",ISStrideSetStride_Stride);
417:   return(0);
418: }