Actual source code: vinv.c

  1: #define PETSCVEC_DLL

  3: /*
  4:      Some useful vector utility functions.
  5: */
 6:  #include private/vecimpl.h


 13: /*@
 14:    VecStrideScale - Scales a subvector of a vector defined 
 15:    by a starting point and a stride.

 17:    Collective on Vec

 19:    Input Parameter:
 20: +  v - the vector 
 21: .  start - starting point of the subvector (defined by a stride)
 22: -  scale - value to multiply each subvector entry by

 24:    Notes:
 25:    One must call VecSetBlockSize() before this routine to set the stride 
 26:    information, or use a vector created from a multicomponent DA.

 28:    This will only work if the desire subvector is a stride subvector

 30:    Level: advanced

 32:    Concepts: scale^on stride of vector
 33:    Concepts: stride^scale

 35: .seealso: VecNorm(), VecStrideGather(), VecStrideScatter(), VecStrideMin(), VecStrideMax(), VecStrideScale()
 36: @*/
 37: PetscErrorCode  VecStrideScale(Vec v,PetscInt start,PetscScalar scale)
 38: {
 40:   PetscInt       i,n,bs;
 41:   PetscScalar    *x;

 45:   VecGetLocalSize(v,&n);
 46:   VecGetArray(v,&x);

 48:   bs   = v->map.bs;
 49:   if (start < 0) {
 50:     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative start %D",start);
 51:   } else if (start >= bs) {
 52:     SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Start of stride subvector (%D) is too large for stride\n\
 53:             Have you set the vector blocksize (%D) correctly with VecSetBlockSize()?",start,bs);
 54:   }
 55:   x += start;

 57:   for (i=0; i<n; i+=bs) {
 58:     x[i] *= scale;
 59:   }
 60:   x -= start;

 62:   VecRestoreArray(v,&x);
 63:   return(0);
 64: }

 68: /*@
 69:    VecStrideNorm - Computes the norm of subvector of a vector defined 
 70:    by a starting point and a stride.

 72:    Collective on Vec

 74:    Input Parameter:
 75: +  v - the vector 
 76: .  start - starting point of the subvector (defined by a stride)
 77: -  ntype - type of norm, one of NORM_1, NORM_2, NORM_INFINITY

 79:    Output Parameter:
 80: .  norm - the norm

 82:    Notes:
 83:    One must call VecSetBlockSize() before this routine to set the stride 
 84:    information, or use a vector created from a multicomponent DA.

 86:    If x is the array representing the vector x then this computes the norm 
 87:    of the array (x[start],x[start+stride],x[start+2*stride], ....)

 89:    This is useful for computing, say the norm of the pressure variable when
 90:    the pressure is stored (interlaced) with other variables, say density etc.

 92:    This will only work if the desire subvector is a stride subvector

 94:    Level: advanced

 96:    Concepts: norm^on stride of vector
 97:    Concepts: stride^norm

 99: .seealso: VecNorm(), VecStrideGather(), VecStrideScatter(), VecStrideMin(), VecStrideMax()
100: @*/
101: PetscErrorCode  VecStrideNorm(Vec v,PetscInt start,NormType ntype,PetscReal *nrm)
102: {
104:   PetscInt       i,n,bs;
105:   PetscScalar    *x;
106:   PetscReal      tnorm;
107:   MPI_Comm       comm;

112:   VecGetLocalSize(v,&n);
113:   VecGetArray(v,&x);
114:   PetscObjectGetComm((PetscObject)v,&comm);

116:   bs   = v->map.bs;
117:   if (start < 0) {
118:     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative start %D",start);
119:   } else if (start >= bs) {
120:     SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Start of stride subvector (%D) is too large for stride\n\
121:             Have you set the vector blocksize (%D) correctly with VecSetBlockSize()?",start,bs);
122:   }
123:   x += start;

125:   if (ntype == NORM_2) {
126:     PetscScalar sum = 0.0;
127:     for (i=0; i<n; i+=bs) {
128:       sum += x[i]*(PetscConj(x[i]));
129:     }
130:     tnorm  = PetscRealPart(sum);
131:     MPI_Allreduce(&tnorm,nrm,1,MPIU_REAL,MPI_SUM,comm);
132:     *nrm = sqrt(*nrm);
133:   } else if (ntype == NORM_1) {
134:     tnorm = 0.0;
135:     for (i=0; i<n; i+=bs) {
136:       tnorm += PetscAbsScalar(x[i]);
137:     }
138:     MPI_Allreduce(&tnorm,nrm,1,MPIU_REAL,MPI_SUM,comm);
139:   } else if (ntype == NORM_INFINITY) {
140:     PetscReal tmp;
141:     tnorm = 0.0;

143:     for (i=0; i<n; i+=bs) {
144:       if ((tmp = PetscAbsScalar(x[i])) > tnorm) tnorm = tmp;
145:       /* check special case of tmp == NaN */
146:       if (tmp != tmp) {tnorm = tmp; break;}
147:     }
148:     MPI_Allreduce(&tnorm,nrm,1,MPIU_REAL,MPI_MAX,comm);
149:   } else {
150:     SETERRQ(PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown norm type");
151:   }

153:   VecRestoreArray(v,&x);
154:   return(0);
155: }

159: /*@
160:    VecStrideMax - Computes the maximum of subvector of a vector defined 
161:    by a starting point and a stride and optionally its location.

163:    Collective on Vec

165:    Input Parameter:
166: +  v - the vector 
167: -  start - starting point of the subvector (defined by a stride)

169:    Output Parameter:
170: +  index - the location where the maximum occurred  (pass PETSC_NULL if not required)
171: -  nrm - the max

173:    Notes:
174:    One must call VecSetBlockSize() before this routine to set the stride 
175:    information, or use a vector created from a multicomponent DA.

177:    If xa is the array representing the vector x, then this computes the max
178:    of the array (xa[start],xa[start+stride],xa[start+2*stride], ....)

180:    This is useful for computing, say the maximum of the pressure variable when
181:    the pressure is stored (interlaced) with other variables, e.g., density, etc.
182:    This will only work if the desire subvector is a stride subvector.

184:    Level: advanced

186:    Concepts: maximum^on stride of vector
187:    Concepts: stride^maximum

189: .seealso: VecMax(), VecStrideNorm(), VecStrideGather(), VecStrideScatter(), VecStrideMin()
190: @*/
191: PetscErrorCode  VecStrideMax(Vec v,PetscInt start,PetscInt *idex,PetscReal *nrm)
192: {
194:   PetscInt       i,n,bs,id;
195:   PetscScalar    *x;
196:   PetscReal      max,tmp;
197:   MPI_Comm       comm;


203:   VecGetLocalSize(v,&n);
204:   VecGetArray(v,&x);
205:   PetscObjectGetComm((PetscObject)v,&comm);

207:   bs   = v->map.bs;
208:   if (start < 0) {
209:     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative start %D",start);
210:   } else if (start >= bs) {
211:     SETERRQ2(PETSC_ERR_ARG_WRONG,"Start of stride subvector (%D) is too large for stride\n\
212:             Have you set the vector blocksize (%D) correctly with VecSetBlockSize()?",start,bs);
213:   }
214:   x += start;

216:   id = -1;
217:   if (!n) {
218:     max = PETSC_MIN;
219:   } else {
220:     id  = 0;
221: #if defined(PETSC_USE_COMPLEX)
222:     max = PetscRealPart(x[0]);
223: #else
224:     max = x[0];
225: #endif
226:     for (i=bs; i<n; i+=bs) {
227: #if defined(PETSC_USE_COMPLEX)
228:       if ((tmp = PetscRealPart(x[i])) > max) { max = tmp; id = i;}
229: #else
230:       if ((tmp = x[i]) > max) { max = tmp; id = i;}
231: #endif
232:     }
233:   }
234:   VecRestoreArray(v,&x);

236:   if (!idex) {
237:     MPI_Allreduce(&max,nrm,1,MPIU_REAL,MPI_MAX,comm);
238:   } else {
239:     PetscReal in[2],out[2];
240:     PetscInt  rstart;

242:     VecGetOwnershipRange(v,&rstart,PETSC_NULL);
243:     in[0] = max;
244:     in[1] = rstart+id;
245:     MPI_Allreduce(in,out,2,MPIU_REAL,VecMax_Local_Op,v->comm);
246:     *nrm  = out[0];
247:     *idex = (PetscInt)out[1];
248:   }

250:   return(0);
251: }

255: /*@
256:    VecStrideMin - Computes the minimum of subvector of a vector defined 
257:    by a starting point and a stride and optionally its location.

259:    Collective on Vec

261:    Input Parameter:
262: +  v - the vector 
263: -  start - starting point of the subvector (defined by a stride)

265:    Output Parameter:
266: +  idex - the location where the minimum occurred. (pass PETSC_NULL if not required)
267: -  nrm - the min

269:    Level: advanced

271:    Notes:
272:    One must call VecSetBlockSize() before this routine to set the stride 
273:    information, or use a vector created from a multicomponent DA.

275:    If xa is the array representing the vector x, then this computes the min
276:    of the array (xa[start],xa[start+stride],xa[start+2*stride], ....)

278:    This is useful for computing, say the minimum of the pressure variable when
279:    the pressure is stored (interlaced) with other variables, e.g., density, etc.
280:    This will only work if the desire subvector is a stride subvector.

282:    Concepts: minimum^on stride of vector
283:    Concepts: stride^minimum

285: .seealso: VecMin(), VecStrideNorm(), VecStrideGather(), VecStrideScatter(), VecStrideMax()
286: @*/
287: PetscErrorCode  VecStrideMin(Vec v,PetscInt start,PetscInt *idex,PetscReal *nrm)
288: {
290:   PetscInt       i,n,bs,id;
291:   PetscScalar    *x;
292:   PetscReal      min,tmp;
293:   MPI_Comm       comm;


299:   VecGetLocalSize(v,&n);
300:   VecGetArray(v,&x);
301:   PetscObjectGetComm((PetscObject)v,&comm);

303:   bs   = v->map.bs;
304:   if (start < 0) {
305:     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative start %D",start);
306:   } else if (start >= bs) {
307:     SETERRQ2(PETSC_ERR_ARG_WRONG,"Start of stride subvector (%D) is too large for stride\n\
308:             Have you set the vector blocksize (%D) correctly with VecSetBlockSize()?",start,bs);
309:   }
310:   x += start;

312:   id = -1;
313:   if (!n) {
314:     min = PETSC_MAX;
315:   } else {
316:     id = 0;
317: #if defined(PETSC_USE_COMPLEX)
318:     min = PetscRealPart(x[0]);
319: #else
320:     min = x[0];
321: #endif
322:     for (i=bs; i<n; i+=bs) {
323: #if defined(PETSC_USE_COMPLEX)
324:       if ((tmp = PetscRealPart(x[i])) < min) { min = tmp; id = i;}
325: #else
326:       if ((tmp = x[i]) < min) { min = tmp; id = i;}
327: #endif
328:     }
329:   }
330:   VecRestoreArray(v,&x);

332:   if (!idex) {
333:     MPI_Allreduce(&min,nrm,1,MPIU_REAL,MPI_MIN,comm);
334:   } else {
335:     PetscReal in[2],out[2];
336:     PetscInt  rstart;

338:     VecGetOwnershipRange(v,&rstart,PETSC_NULL);
339:     in[0] = min;
340:     in[1] = rstart+id;
341:     MPI_Allreduce(in,out,2,MPIU_REAL,VecMin_Local_Op,v->comm);
342:     *nrm  = out[0];
343:     *idex = (PetscInt)out[1];
344:   }

346:   return(0);
347: }

351: /*@
352:    VecStrideScaleAll - Scales the subvectors of a vector defined 
353:    by a starting point and a stride.

355:    Collective on Vec

357:    Input Parameter:
358: +  v - the vector 
359: -  scales - values to multiply each subvector entry by

361:    Notes:
362:    One must call VecSetBlockSize() before this routine to set the stride 
363:    information, or use a vector created from a multicomponent DA.


366:    Level: advanced

368:    Concepts: scale^on stride of vector
369:    Concepts: stride^scale

371: .seealso: VecNorm(), VecStrideScale(), VecScale(), VecStrideGather(), VecStrideScatter(), VecStrideMin(), VecStrideMax()
372: @*/
373: PetscErrorCode  VecStrideScaleAll(Vec v,PetscScalar *scales)
374: {
376:   PetscInt       i,j,n,bs;
377:   PetscScalar    *x;

382:   VecGetLocalSize(v,&n);
383:   VecGetArray(v,&x);

385:   bs   = v->map.bs;

387:   /* need to provide optimized code for each bs */
388:   for (i=0; i<n; i+=bs) {
389:     for (j=0; j<bs; j++) {
390:       x[i+j] *= scales[j];
391:     }
392:   }
393:   VecRestoreArray(v,&x);
394:   return(0);
395: }

399: /*@
400:    VecStrideNormAll - Computes the norms  subvectors of a vector defined 
401:    by a starting point and a stride.

403:    Collective on Vec

405:    Input Parameter:
406: +  v - the vector 
407: -  ntype - type of norm, one of NORM_1, NORM_2, NORM_INFINITY

409:    Output Parameter:
410: .  nrm - the norms

412:    Notes:
413:    One must call VecSetBlockSize() before this routine to set the stride 
414:    information, or use a vector created from a multicomponent DA.

416:    If x is the array representing the vector x then this computes the norm 
417:    of the array (x[start],x[start+stride],x[start+2*stride], ....)

419:    This is useful for computing, say the norm of the pressure variable when
420:    the pressure is stored (interlaced) with other variables, say density etc.

422:    This will only work if the desire subvector is a stride subvector

424:    Level: advanced

426:    Concepts: norm^on stride of vector
427:    Concepts: stride^norm

429: .seealso: VecNorm(), VecStrideGather(), VecStrideScatter(), VecStrideMin(), VecStrideMax()
430: @*/
431: PetscErrorCode  VecStrideNormAll(Vec v,NormType ntype,PetscReal *nrm)
432: {
434:   PetscInt       i,j,n,bs;
435:   PetscScalar    *x;
436:   PetscReal      tnorm[128];
437:   MPI_Comm       comm;

442:   VecGetLocalSize(v,&n);
443:   VecGetArray(v,&x);
444:   PetscObjectGetComm((PetscObject)v,&comm);

446:   bs   = v->map.bs;
447:   if (bs > 128) SETERRQ(PETSC_ERR_SUP,"Currently supports only blocksize up to 128");

449:   if (ntype == NORM_2) {
450:     PetscScalar sum[128];
451:     for (j=0; j<bs; j++) sum[j] = 0.0;
452:     for (i=0; i<n; i+=bs) {
453:       for (j=0; j<bs; j++) {
454:         sum[j] += x[i+j]*(PetscConj(x[i+j]));
455:       }
456:     }
457:     for (j=0; j<bs; j++) {
458:       tnorm[j]  = PetscRealPart(sum[j]);
459:     }
460:     MPI_Allreduce(tnorm,nrm,bs,MPIU_REAL,MPI_SUM,comm);
461:     for (j=0; j<bs; j++) {
462:       nrm[j] = sqrt(nrm[j]);
463:     }
464:   } else if (ntype == NORM_1) {
465:     for (j=0; j<bs; j++) {
466:       tnorm[j] = 0.0;
467:     }
468:     for (i=0; i<n; i+=bs) {
469:       for (j=0; j<bs; j++) {
470:         tnorm[j] += PetscAbsScalar(x[i+j]);
471:       }
472:     }
473:     MPI_Allreduce(tnorm,nrm,bs,MPIU_REAL,MPI_SUM,comm);
474:   } else if (ntype == NORM_INFINITY) {
475:     PetscReal tmp;
476:     for (j=0; j<bs; j++) {
477:       tnorm[j] = 0.0;
478:     }

480:     for (i=0; i<n; i+=bs) {
481:       for (j=0; j<bs; j++) {
482:         if ((tmp = PetscAbsScalar(x[i+j])) > tnorm[j]) tnorm[j] = tmp;
483:         /* check special case of tmp == NaN */
484:         if (tmp != tmp) {tnorm[j] = tmp; break;}
485:       }
486:     }
487:     MPI_Allreduce(tnorm,nrm,bs,MPIU_REAL,MPI_MAX,comm);
488:   } else {
489:     SETERRQ(PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown norm type");
490:   }

492:   VecRestoreArray(v,&x);
493:   return(0);
494: }

498: /*@
499:    VecStrideMaxAll - Computes the maximums of subvectors of a vector defined 
500:    by a starting point and a stride and optionally its location.

502:    Collective on Vec

504:    Input Parameter:
505: .  v - the vector 

507:    Output Parameter:
508: +  index - the location where the maximum occurred (not supported, pass PETSC_NULL,
509:            if you need this, send mail to petsc-maint@mcs.anl.gov to request it)
510: -  nrm - the maximums

512:    Notes:
513:    One must call VecSetBlockSize() before this routine to set the stride 
514:    information, or use a vector created from a multicomponent DA.

516:    This is useful for computing, say the maximum of the pressure variable when
517:    the pressure is stored (interlaced) with other variables, e.g., density, etc.
518:    This will only work if the desire subvector is a stride subvector.

520:    Level: advanced

522:    Concepts: maximum^on stride of vector
523:    Concepts: stride^maximum

525: .seealso: VecMax(), VecStrideNorm(), VecStrideGather(), VecStrideScatter(), VecStrideMin()
526: @*/
527: PetscErrorCode  VecStrideMaxAll(Vec v,PetscInt *idex,PetscReal *nrm)
528: {
530:   PetscInt       i,j,n,bs;
531:   PetscScalar    *x;
532:   PetscReal      max[128],tmp;
533:   MPI_Comm       comm;

538:   if (idex) {
539:     SETERRQ(PETSC_ERR_SUP,"No support yet for returning index; send mail to petsc-maint@mcs.anl.gov asking for it");
540:   }
541:   VecGetLocalSize(v,&n);
542:   VecGetArray(v,&x);
543:   PetscObjectGetComm((PetscObject)v,&comm);

545:   bs   = v->map.bs;
546:   if (bs > 128) SETERRQ(PETSC_ERR_SUP,"Currently supports only blocksize up to 128");

548:   if (!n) {
549:     for (j=0; j<bs; j++) {
550:       max[j] = PETSC_MIN;
551:     }
552:   } else {
553:     for (j=0; j<bs; j++) {
554: #if defined(PETSC_USE_COMPLEX)
555:       max[j] = PetscRealPart(x[j]);
556: #else
557:       max[j] = x[j];
558: #endif
559:     }
560:     for (i=bs; i<n; i+=bs) {
561:       for (j=0; j<bs; j++) {
562: #if defined(PETSC_USE_COMPLEX)
563:         if ((tmp = PetscRealPart(x[i+j])) > max[j]) { max[j] = tmp;}
564: #else
565:         if ((tmp = x[i+j]) > max[j]) { max[j] = tmp; }
566: #endif
567:       }
568:     }
569:   }
570:   MPI_Allreduce(max,nrm,bs,MPIU_REAL,MPI_MAX,comm);

572:   VecRestoreArray(v,&x);
573:   return(0);
574: }

578: /*@
579:    VecStrideMinAll - Computes the minimum of subvector of a vector defined 
580:    by a starting point and a stride and optionally its location.

582:    Collective on Vec

584:    Input Parameter:
585: .  v - the vector 

587:    Output Parameter:
588: +  idex - the location where the minimum occurred (not supported, pass PETSC_NULL,
589:            if you need this, send mail to petsc-maint@mcs.anl.gov to request it)
590: -  nrm - the minimums

592:    Level: advanced

594:    Notes:
595:    One must call VecSetBlockSize() before this routine to set the stride 
596:    information, or use a vector created from a multicomponent DA.

598:    This is useful for computing, say the minimum of the pressure variable when
599:    the pressure is stored (interlaced) with other variables, e.g., density, etc.
600:    This will only work if the desire subvector is a stride subvector.

602:    Concepts: minimum^on stride of vector
603:    Concepts: stride^minimum

605: .seealso: VecMin(), VecStrideNorm(), VecStrideGather(), VecStrideScatter(), VecStrideMax()
606: @*/
607: PetscErrorCode  VecStrideMinAll(Vec v,PetscInt *idex,PetscReal *nrm)
608: {
610:   PetscInt       i,n,bs,j;
611:   PetscScalar    *x;
612:   PetscReal      min[128],tmp;
613:   MPI_Comm       comm;

618:   if (idex) {
619:     SETERRQ(PETSC_ERR_SUP,"No support yet for returning index; send mail to petsc-maint@mcs.anl.gov asking for it");
620:   }
621:   VecGetLocalSize(v,&n);
622:   VecGetArray(v,&x);
623:   PetscObjectGetComm((PetscObject)v,&comm);

625:   bs   = v->map.bs;
626:   if (bs > 128) SETERRQ(PETSC_ERR_SUP,"Currently supports only blocksize up to 128");

628:   if (!n) {
629:     for (j=0; j<bs; j++) {
630:       min[j] = PETSC_MAX;
631:     }
632:   } else {
633:     for (j=0; j<bs; j++) {
634: #if defined(PETSC_USE_COMPLEX)
635:       min[j] = PetscRealPart(x[j]);
636: #else
637:       min[j] = x[j];
638: #endif
639:     }
640:     for (i=bs; i<n; i+=bs) {
641:       for (j=0; j<bs; j++) {
642: #if defined(PETSC_USE_COMPLEX)
643:         if ((tmp = PetscRealPart(x[i+j])) < min[j]) { min[j] = tmp;}
644: #else
645:         if ((tmp = x[i+j]) < min[j]) { min[j] = tmp; }
646: #endif
647:       }
648:     }
649:   }
650:   MPI_Allreduce(min,nrm,bs,MPIU_REAL,MPI_MIN,comm);

652:   VecRestoreArray(v,&x);
653:   return(0);
654: }

656: /*----------------------------------------------------------------------------------------------*/
659: /*@
660:    VecStrideGatherAll - Gathers all the single components from a multi-component vector into
661:    separate vectors.

663:    Collective on Vec

665:    Input Parameter:
666: +  v - the vector 
667: -  addv - one of ADD_VALUES,INSERT_VALUES,MAX_VALUES

669:    Output Parameter:
670: .  s - the location where the subvectors are stored

672:    Notes:
673:    One must call VecSetBlockSize() before this routine to set the stride 
674:    information, or use a vector created from a multicomponent DA.

676:    If x is the array representing the vector x then this gathers
677:    the arrays (x[start],x[start+stride],x[start+2*stride], ....)
678:    for start=0,1,2,...bs-1

680:    The parallel layout of the vector and the subvector must be the same;
681:    i.e., nlocal of v = stride*(nlocal of s) 

683:    Not optimized; could be easily

685:    Level: advanced

687:    Concepts: gather^into strided vector

689: .seealso: VecStrideNorm(), VecStrideScatter(), VecStrideMin(), VecStrideMax(), VecStrideGather(),
690:           VecStrideScatterAll()
691: @*/
692: PetscErrorCode  VecStrideGatherAll(Vec v,Vec *s,InsertMode addv)
693: {
695:   PetscInt       i,n,bs,j,k,*bss = PETSC_NULL,nv,jj,nvc;
696:   PetscScalar    *x,**y;

702:   VecGetLocalSize(v,&n);
703:   VecGetArray(v,&x);
704:   bs   = v->map.bs;
705:   if (bs < 0) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Input vector does not have a valid blocksize set");
706:   PetscMalloc2(bs,PetscReal*,&y,bs,PetscInt,&bss);
707:   nv   = 0;
708:   nvc  = 0;
709:   for (i=0; i<bs; i++) {
710:     VecGetBlockSize(s[i],&bss[i]);
711:     if (bss[i] < 1) bss[i] = 1; /* if user never set it then assume 1  Re: [PETSC #8241] VecStrideGatherAll */
712:     VecGetArray(s[i],&y[i]);
713:     nvc  += bss[i];
714:     nv++;
715:     if (nvc > bs)  SETERRQ(PETSC_ERR_ARG_INCOMP,"Number of subvectors in subvectors > number of vectors in main vector");
716:     if (nvc == bs) break;
717:   }

719:   n =  n/bs;

721:   jj = 0;
722:   if (addv == INSERT_VALUES) {
723:     for (j=0; j<nv; j++) {
724:       for (k=0; k<bss[j]; k++) {
725:         for (i=0; i<n; i++) {
726:           y[j][i*bss[j] + k] = x[bs*i+jj+k];
727:         }
728:       }
729:       jj += bss[j];
730:     }
731:   } else if (addv == ADD_VALUES) {
732:     for (j=0; j<nv; j++) {
733:       for (k=0; k<bss[j]; k++) {
734:         for (i=0; i<n; i++) {
735:           y[j][i*bss[j] + k] += x[bs*i+jj+k];
736:         }
737:       }
738:       jj += bss[j];
739:     }
740: #if !defined(PETSC_USE_COMPLEX)
741:   } else if (addv == MAX_VALUES) {
742:     for (j=0; j<nv; j++) {
743:       for (k=0; k<bss[j]; k++) {
744:         for (i=0; i<n; i++) {
745:           y[j][i*bss[j] + k] = PetscMax(y[j][i*bss[j] + k],x[bs*i+jj+k]);
746:         }
747:       }
748:       jj += bss[j];
749:     }
750: #endif
751:   } else {
752:     SETERRQ(PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown insert type");
753:   }

755:   VecRestoreArray(v,&x);
756:   for (i=0; i<nv; i++) {
757:     VecRestoreArray(s[i],&y[i]);
758:   }

760:   PetscFree2(y,bss);
761:   return(0);
762: }

766: /*@
767:    VecStrideScatterAll - Scatters all the single components from separate vectors into 
768:      a multi-component vector.

770:    Collective on Vec

772:    Input Parameter:
773: +  s - the location where the subvectors are stored
774: -  addv - one of ADD_VALUES,INSERT_VALUES,MAX_VALUES

776:    Output Parameter:
777: .  v - the multicomponent vector 

779:    Notes:
780:    One must call VecSetBlockSize() before this routine to set the stride 
781:    information, or use a vector created from a multicomponent DA.

783:    The parallel layout of the vector and the subvector must be the same;
784:    i.e., nlocal of v = stride*(nlocal of s) 

786:    Not optimized; could be easily

788:    Level: advanced

790:    Concepts:  scatter^into strided vector

792: .seealso: VecStrideNorm(), VecStrideScatter(), VecStrideMin(), VecStrideMax(), VecStrideGather(),
793:           VecStrideScatterAll()
794: @*/
795: PetscErrorCode  VecStrideScatterAll(Vec *s,Vec v,InsertMode addv)
796: {
798:   PetscInt        i,n,bs,j,jj,k,*bss = PETSC_NULL,nv,nvc;
799:   PetscScalar     *x,**y;

805:   VecGetLocalSize(v,&n);
806:   VecGetArray(v,&x);
807:   bs   = v->map.bs;
808:   if (bs < 0) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Input vector does not have a valid blocksize set");

810:   PetscMalloc2(bs,PetscScalar**,&y,bs,PetscInt,&bss);
811:   nv  = 0;
812:   nvc = 0;
813:   for (i=0; i<bs; i++) {
814:     VecGetBlockSize(s[i],&bss[i]);
815:     if (bss[i] < 1) bss[i] = 1; /* if user never set it then assume 1  Re: [PETSC #8241] VecStrideGatherAll */
816:     VecGetArray(s[i],&y[i]);
817:     nvc  += bss[i];
818:     nv++;
819:     if (nvc > bs)  SETERRQ(PETSC_ERR_ARG_INCOMP,"Number of subvectors in subvectors > number of vectors in main vector");
820:     if (nvc == bs) break;
821:   }

823:   n =  n/bs;

825:   jj = 0;
826:   if (addv == INSERT_VALUES) {
827:     for (j=0; j<nv; j++) {
828:       for (k=0; k<bss[j]; k++) {
829:         for (i=0; i<n; i++) {
830:           x[bs*i+jj+k] = y[j][i*bss[j] + k];
831:         }
832:       }
833:       jj += bss[j];
834:     }
835:   } else if (addv == ADD_VALUES) {
836:     for (j=0; j<nv; j++) {
837:       for (k=0; k<bss[j]; k++) {
838:         for (i=0; i<n; i++) {
839:           x[bs*i+jj+k] += y[j][i*bss[j] + k];
840:         }
841:       }
842:       jj += bss[j];
843:     }
844: #if !defined(PETSC_USE_COMPLEX)
845:   } else if (addv == MAX_VALUES) {
846:     for (j=0; j<nv; j++) {
847:       for (k=0; k<bss[j]; k++) {
848:         for (i=0; i<n; i++) {
849:           x[bs*i+jj+k] = PetscMax(x[bs*i+jj+k],y[j][i*bss[j] + k]);
850:         }
851:       }
852:       jj += bss[j];
853:     }
854: #endif
855:   } else {
856:     SETERRQ(PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown insert type");
857:   }

859:   VecRestoreArray(v,&x);
860:   for (i=0; i<nv; i++) {
861:     VecRestoreArray(s[i],&y[i]);
862:   }
863:   PetscFree2(y,bss);
864:   return(0);
865: }

869: /*@
870:    VecStrideGather - Gathers a single component from a multi-component vector into
871:    another vector.

873:    Collective on Vec

875:    Input Parameter:
876: +  v - the vector 
877: .  start - starting point of the subvector (defined by a stride)
878: -  addv - one of ADD_VALUES,INSERT_VALUES,MAX_VALUES

880:    Output Parameter:
881: .  s - the location where the subvector is stored

883:    Notes:
884:    One must call VecSetBlockSize() before this routine to set the stride 
885:    information, or use a vector created from a multicomponent DA.

887:    If x is the array representing the vector x then this gathers
888:    the array (x[start],x[start+stride],x[start+2*stride], ....)

890:    The parallel layout of the vector and the subvector must be the same;
891:    i.e., nlocal of v = stride*(nlocal of s) 

893:    Not optimized; could be easily

895:    Level: advanced

897:    Concepts: gather^into strided vector

899: .seealso: VecStrideNorm(), VecStrideScatter(), VecStrideMin(), VecStrideMax(), VecStrideGatherAll(),
900:           VecStrideScatterAll()
901: @*/
902: PetscErrorCode  VecStrideGather(Vec v,PetscInt start,Vec s,InsertMode addv)
903: {
905:   PetscInt       i,n,bs,ns;
906:   PetscScalar    *x,*y;

911:   VecGetLocalSize(v,&n);
912:   VecGetLocalSize(s,&ns);
913:   VecGetArray(v,&x);
914:   VecGetArray(s,&y);

916:   bs   = v->map.bs;
917:   if (start < 0) {
918:     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative start %D",start);
919:   } else if (start >= bs) {
920:     SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Start of stride subvector (%D) is too large for stride\n\
921:             Have you set the vector blocksize (%D) correctly with VecSetBlockSize()?",start,bs);
922:   }
923:   if (n != ns*bs) {
924:     SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Subvector length * blocksize %D not correct for gather from original vector %D",ns*bs,n);
925:   }
926:   x += start;
927:   n =  n/bs;

929:   if (addv == INSERT_VALUES) {
930:     for (i=0; i<n; i++) {
931:       y[i] = x[bs*i];
932:     }
933:   } else if (addv == ADD_VALUES) {
934:     for (i=0; i<n; i++) {
935:       y[i] += x[bs*i];
936:     }
937: #if !defined(PETSC_USE_COMPLEX)
938:   } else if (addv == MAX_VALUES) {
939:     for (i=0; i<n; i++) {
940:       y[i] = PetscMax(y[i],x[bs*i]);
941:     }
942: #endif
943:   } else {
944:     SETERRQ(PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown insert type");
945:   }

947:   VecRestoreArray(v,&x);
948:   VecRestoreArray(s,&y);
949:   return(0);
950: }

954: /*@
955:    VecStrideScatter - Scatters a single component from a vector into a multi-component vector.

957:    Collective on Vec

959:    Input Parameter:
960: +  s - the single-component vector 
961: .  start - starting point of the subvector (defined by a stride)
962: -  addv - one of ADD_VALUES,INSERT_VALUES,MAX_VALUES

964:    Output Parameter:
965: .  v - the location where the subvector is scattered (the multi-component vector)

967:    Notes:
968:    One must call VecSetBlockSize() on the multi-component vector before this
969:    routine to set the stride  information, or use a vector created from a multicomponent DA.

971:    The parallel layout of the vector and the subvector must be the same;
972:    i.e., nlocal of v = stride*(nlocal of s) 

974:    Not optimized; could be easily

976:    Level: advanced

978:    Concepts: scatter^into strided vector

980: .seealso: VecStrideNorm(), VecStrideGather(), VecStrideMin(), VecStrideMax(), VecStrideGatherAll(),
981:           VecStrideScatterAll()
982: @*/
983: PetscErrorCode  VecStrideScatter(Vec s,PetscInt start,Vec v,InsertMode addv)
984: {
986:   PetscInt       i,n,bs,ns;
987:   PetscScalar    *x,*y;

992:   VecGetLocalSize(v,&n);
993:   VecGetLocalSize(s,&ns);
994:   VecGetArray(v,&x);
995:   VecGetArray(s,&y);

997:   bs   = v->map.bs;
998:   if (start < 0) {
999:     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative start %D",start);
1000:   } else if (start >= bs) {
1001:     SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Start of stride subvector (%D) is too large for stride\n\
1002:             Have you set the vector blocksize (%D) correctly with VecSetBlockSize()?",start,bs);
1003:   }
1004:   if (n != ns*bs) {
1005:     SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Subvector length * blocksize %D not correct for scatter to multicomponent vector %D",ns*bs,n);
1006:   }
1007:   x += start;
1008:   n =  n/bs;


1011:   if (addv == INSERT_VALUES) {
1012:     for (i=0; i<n; i++) {
1013:       x[bs*i] = y[i];
1014:     }
1015:   } else if (addv == ADD_VALUES) {
1016:     for (i=0; i<n; i++) {
1017:       x[bs*i] += y[i];
1018:     }
1019: #if !defined(PETSC_USE_COMPLEX)
1020:   } else if (addv == MAX_VALUES) {
1021:     for (i=0; i<n; i++) {
1022:       x[bs*i] = PetscMax(y[i],x[bs*i]);
1023:     }
1024: #endif
1025:   } else {
1026:     SETERRQ(PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown insert type");
1027:   }


1030:   VecRestoreArray(v,&x);
1031:   VecRestoreArray(s,&y);
1032:   return(0);
1033: }

1037: PetscErrorCode VecReciprocal_Default(Vec v)
1038: {
1040:   PetscInt         i,n;
1041:   PetscScalar *x;

1044:   VecGetLocalSize(v,&n);
1045:   VecGetArray(v,&x);
1046:   for (i=0; i<n; i++) {
1047:     if (x[i] != 0.0) x[i] = 1.0/x[i];
1048:   }
1049:   VecRestoreArray(v,&x);
1050:   return(0);
1051: }

1055: /*@
1056:   VecSqrt - Replaces each component of a vector by the square root of its magnitude.

1058:   Not collective

1060:   Input Parameter:
1061: . v - The vector

1063:   Output Parameter:
1064: . v - The vector square root

1066:   Level: beginner

1068:   Note: The actual function is sqrt(|x_i|)

1070: .keywords: vector, sqrt, square root
1071: @*/
1072: PetscErrorCode  VecSqrt(Vec v)
1073: {
1074:   PetscScalar *x;
1075:   PetscInt         i, n;

1080:   VecGetLocalSize(v, &n);
1081:   VecGetArray(v, &x);
1082:   for(i = 0; i < n; i++) {
1083:     x[i] = sqrt(PetscAbsScalar(x[i]));
1084:   }
1085:   VecRestoreArray(v, &x);
1086:   return(0);
1087: }

1091: /*@
1092:    VecSum - Computes the sum of all the components of a vector.

1094:    Collective on Vec

1096:    Input Parameter:
1097: .  v - the vector 

1099:    Output Parameter:
1100: .  sum - the result

1102:    Level: beginner

1104:    Concepts: sum^of vector entries

1106: .seealso: VecNorm()
1107: @*/
1108: PetscErrorCode  VecSum(Vec v,PetscScalar *sum)
1109: {
1111:   PetscInt       i,n;
1112:   PetscScalar    *x,lsum = 0.0;

1117:   VecGetLocalSize(v,&n);
1118:   VecGetArray(v,&x);
1119:   for (i=0; i<n; i++) {
1120:     lsum += x[i];
1121:   }
1122:   MPI_Allreduce(&lsum,sum,1,MPIU_SCALAR,PetscSum_Op,v->comm);
1123:   VecRestoreArray(v,&x);
1124:   return(0);
1125: }

1129: /*@
1130:    VecShift - Shifts all of the components of a vector by computing
1131:    x[i] = x[i] + shift.

1133:    Collective on Vec

1135:    Input Parameters:
1136: +  v - the vector 
1137: -  shift - the shift

1139:    Output Parameter:
1140: .  v - the shifted vector 

1142:    Level: intermediate

1144:    Concepts: vector^adding constant

1146: @*/
1147: PetscErrorCode  VecShift(Vec v,PetscScalar shift)
1148: {
1150:   PetscInt       i,n;
1151:   PetscScalar    *x;

1155:   VecGetLocalSize(v,&n);
1156:   VecGetArray(v,&x);
1157:   for (i=0; i<n; i++) {
1158:     x[i] += shift;
1159:   }
1160:   VecRestoreArray(v,&x);
1161:   return(0);
1162: }

1166: /*@
1167:    VecAbs - Replaces every element in a vector with its absolute value.

1169:    Collective on Vec

1171:    Input Parameters:
1172: .  v - the vector 

1174:    Level: intermediate

1176:    Concepts: vector^absolute value

1178: @*/
1179: PetscErrorCode  VecAbs(Vec v)
1180: {
1182:   PetscInt       i,n;
1183:   PetscScalar    *x;

1187:   VecGetLocalSize(v,&n);
1188:   VecGetArray(v,&x);
1189:   for (i=0; i<n; i++) {
1190:     x[i] = PetscAbsScalar(x[i]);
1191:   }
1192:   VecRestoreArray(v,&x);
1193:   return(0);
1194: }

1198: /*@
1199:   VecPermute - Permutes a vector in place using the given ordering.

1201:   Input Parameters:
1202: + vec   - The vector
1203: . order - The ordering
1204: - inv   - The flag for inverting the permutation

1206:   Level: beginner

1208:   Note: This function does not yet support parallel Index Sets

1210: .seealso: MatPermute()
1211: .keywords: vec, permute
1212: @*/
1213: PetscErrorCode  VecPermute(Vec x, IS row, PetscTruth inv)
1214: {
1215:   PetscScalar    *array, *newArray;
1216:   PetscInt       *idx;
1217:   PetscInt       i;

1221:   ISGetIndices(row, &idx);
1222:   VecGetArray(x, &array);
1223:   PetscMalloc(x->map.n*sizeof(PetscScalar), &newArray);
1224: #ifdef PETSC_USE_DEBUG
1225:   for(i = 0; i < x->map.n; i++) {
1226:     if ((idx[i] < 0) || (idx[i] >= x->map.n)) {
1227:       SETERRQ2(PETSC_ERR_ARG_CORRUPT, "Permutation index %D is out of bounds: %D", i, idx[i]);
1228:     }
1229:   }
1230: #endif
1231:   if (!inv) {
1232:     for(i = 0; i < x->map.n; i++) newArray[i]      = array[idx[i]];
1233:   } else {
1234:     for(i = 0; i < x->map.n; i++) newArray[idx[i]] = array[i];
1235:   }
1236:   VecRestoreArray(x, &array);
1237:   ISRestoreIndices(row, &idx);
1238:   VecReplaceArray(x, newArray);
1239:   return(0);
1240: }

1244: /*@
1245:    VecEqual - Compares two vectors.

1247:    Collective on Vec

1249:    Input Parameters:
1250: +  vec1 - the first vector
1251: -  vec2 - the second vector

1253:    Output Parameter:
1254: .  flg - PETSC_TRUE if the vectors are equal; PETSC_FALSE otherwise.

1256:    Level: intermediate

1258:    Concepts: equal^two vectors
1259:    Concepts: vector^equality

1261: @*/
1262: PetscErrorCode  VecEqual(Vec vec1,Vec vec2,PetscTruth *flg)
1263: {
1264:   PetscScalar    *v1,*v2;
1266:   PetscInt       n1,n2,N1,N2;
1267:   PetscInt       state1,state2;
1268:   PetscTruth     flg1;
1269: 
1274:   if (vec1 == vec2) {
1275:     *flg = PETSC_TRUE;
1276:   } else {
1277:     VecGetSize(vec1,&N1);
1278:     VecGetSize(vec2,&N2);
1279:     if (N1 != N2) {
1280:       flg1 = PETSC_FALSE;
1281:     } else {
1282:       VecGetLocalSize(vec1,&n1);
1283:       VecGetLocalSize(vec2,&n2);
1284:       if (n1 != n2) {
1285:         flg1 = PETSC_FALSE;
1286:       } else {
1287:         PetscObjectStateQuery((PetscObject) vec1,&state1);
1288:         PetscObjectStateQuery((PetscObject) vec2,&state2);
1289:         VecGetArray(vec1,&v1);
1290:         VecGetArray(vec2,&v2);
1291:         PetscMemcmp(v1,v2,n1*sizeof(PetscScalar),&flg1);
1292:         VecRestoreArray(vec1,&v1);
1293:         VecRestoreArray(vec2,&v2);
1294:         PetscObjectSetState((PetscObject) vec1,state1);
1295:         PetscObjectSetState((PetscObject) vec2,state2);
1296:       }
1297:     }
1298:     /* combine results from all processors */
1299:     MPI_Allreduce(&flg1,flg,1,MPI_INT,MPI_MIN,vec1->comm);
1300:   }
1301:   return(0);
1302: }