Actual source code: rvector.c

  1: /*
  2:      Provides the interface functions for vector operations that have PetscScalar/PetscReal in the signature
  3:    These are the vector functions the user calls.
  4: */
  5: #include "petsc/private/sfimpl.h"
  6: #include "petscsystypes.h"
  7: #include <petsc/private/vecimpl.h>
  8: #if defined(PETSC_HAVE_CUDA)
  9: #include <../src/vec/vec/impls/dvecimpl.h>
 10: #include <petsc/private/cudavecimpl.h>
 11: #endif
 12: #if defined(PETSC_HAVE_HIP)
 13: #include <../src/vec/vec/impls/dvecimpl.h>
 14: #include <petsc/private/hipvecimpl.h>
 15: #endif
 16: PetscInt VecGetSubVectorSavedStateId = -1;

 18: #if PetscDefined(USE_DEBUG)
 19: // this is a no-op '0' macro in optimized builds
 20: PetscErrorCode VecValidValues_Internal(Vec vec, PetscInt argnum, PetscBool begin)
 21: {
 22:   if (vec->petscnative || vec->ops->getarray) {
 23:     PetscInt           n;
 24:     const PetscScalar *x;
 25:     PetscOffloadMask   mask;

 27:     VecGetOffloadMask(vec, &mask);
 28:     if (!PetscOffloadHost(mask)) return 0;
 29:     VecGetLocalSize(vec, &n);
 30:     VecGetArrayRead(vec, &x);
 31:     for (PetscInt i = 0; i < n; i++) {
 32:       if (begin) {
 34:       } else {
 36:       }
 37:     }
 38:     VecRestoreArrayRead(vec, &x);
 39:   }
 40:   return 0;
 41: }
 42: #endif

 44: /*@
 45:    VecMaxPointwiseDivide - Computes the maximum of the componentwise division max = max_i abs(x_i/y_i).

 47:    Logically Collective on Vec

 49:    Input Parameters:
 50: .  x, y  - the vectors

 52:    Output Parameter:
 53: .  max - the result

 55:    Level: advanced

 57:    Notes:
 58:     x and y may be the same vector
 59:           if a particular y_i is zero, it is treated as 1 in the above formula

 61: .seealso: `VecPointwiseDivide()`, `VecPointwiseMult()`, `VecPointwiseMax()`, `VecPointwiseMin()`, `VecPointwiseMaxAbs()`
 62: @*/
 63: PetscErrorCode VecMaxPointwiseDivide(Vec x, Vec y, PetscReal *max)
 64: {
 71:   VecCheckSameSize(x, 1, y, 2);
 72:   VecLockReadPush(x);
 73:   VecLockReadPush(y);
 74:   PetscUseTypeMethod(x, maxpointwisedivide, y, max);
 75:   VecLockReadPop(x);
 76:   VecLockReadPop(y);
 77:   return 0;
 78: }

 80: /*@
 81:    VecDot - Computes the vector dot product.

 83:    Collective on Vec

 85:    Input Parameters:
 86: .  x, y - the vectors

 88:    Output Parameter:
 89: .  val - the dot product

 91:    Performance Issues:
 92: $    per-processor memory bandwidth
 93: $    interprocessor latency
 94: $    work load imbalance that causes certain processes to arrive much earlier than others

 96:    Notes for Users of Complex Numbers:
 97:    For complex vectors, VecDot() computes
 98: $     val = (x,y) = y^H x,
 99:    where y^H denotes the conjugate transpose of y. Note that this corresponds to the usual "mathematicians" complex
100:    inner product where the SECOND argument gets the complex conjugate. Since the BLASdot() complex conjugates the first
101:    first argument we call the BLASdot() with the arguments reversed.

103:    Use VecTDot() for the indefinite form
104: $     val = (x,y) = y^T x,
105:    where y^T denotes the transpose of y.

107:    Level: intermediate

109: .seealso: `VecMDot()`, `VecTDot()`, `VecNorm()`, `VecDotBegin()`, `VecDotEnd()`, `VecDotRealPart()`
110: @*/
111: PetscErrorCode VecDot(Vec x, Vec y, PetscScalar *val)
112: {
119:   VecCheckSameSize(x, 1, y, 2);

121:   VecLockReadPush(x);
122:   VecLockReadPush(y);
123:   PetscLogEventBegin(VEC_Dot, x, y, 0, 0);
124:   PetscUseTypeMethod(x, dot, y, val);
125:   PetscLogEventEnd(VEC_Dot, x, y, 0, 0);
126:   VecLockReadPop(x);
127:   VecLockReadPop(y);
128:   return 0;
129: }

131: /*@
132:    VecDotRealPart - Computes the real part of the vector dot product.

134:    Collective on Vec

136:    Input Parameters:
137: .  x, y - the vectors

139:    Output Parameter:
140: .  val - the real part of the dot product;

142:    Performance Issues:
143: $    per-processor memory bandwidth
144: $    interprocessor latency
145: $    work load imbalance that causes certain processes to arrive much earlier than others

147:    Notes for Users of Complex Numbers:
148:      See VecDot() for more details on the definition of the dot product for complex numbers

150:      For real numbers this returns the same value as VecDot()

152:      For complex numbers in C^n (that is a vector of n components with a complex number for each component) this is equal to the usual real dot product on the
153:      the space R^{2n} (that is a vector of 2n components with the real or imaginary part of the complex numbers for components)

155:    Developer Note: This is not currently optimized to compute only the real part of the dot product.

157:    Level: intermediate

159: .seealso: `VecMDot()`, `VecTDot()`, `VecNorm()`, `VecDotBegin()`, `VecDotEnd()`, `VecDot()`, `VecDotNorm2()`
160: @*/
161: PetscErrorCode VecDotRealPart(Vec x, Vec y, PetscReal *val)
162: {
163:   PetscScalar fdot;

165:   VecDot(x, y, &fdot);
166:   *val = PetscRealPart(fdot);
167:   return 0;
168: }

170: /*@
171:    VecNorm  - Computes the vector norm.

173:    Collective on Vec

175:    Input Parameters:
176: +  x - the vector
177: -  type - the type of the norm requested

179:    Output Parameter:
180: .  val - the norm

182:    Values of NormType:
183: +     NORM_1 - sum_i |x_i|
184: .     NORM_2 - sqrt(sum_i |x_i|^2)
185: .     NORM_INFINITY - max_i |x_i|
186: -     NORM_1_AND_2 - computes efficiently both  NORM_1 and NORM_2 and stores them each in an output array

188:    Notes:
189:       For complex numbers NORM_1 will return the traditional 1 norm of the 2 norm of the complex numbers; that is the 1
190:       norm of the absolute values of the complex entries. In PETSc 3.6 and earlier releases it returned the 1 norm of
191:       the 1 norm of the complex entries (what is returned by the BLAS routine asum()). Both are valid norms but most
192:       people expect the former.

194:       This routine stashes the computed norm value, repeated calls before the vector entries are changed are then rapid since the
195:       precomputed value is immediately available. Certain vector operations such as VecSet() store the norms so the value is
196:       immediately available and does not need to be explicitly computed. VecScale() updates any stashed norm values, thus calls after VecScale()
197:       do not need to explicitly recompute the norm.

199:    Level: intermediate

201:    Performance Issues:
202: +    per-processor memory bandwidth - limits the speed of the computation of local portion of the norm
203: .    interprocessor latency - limits the accumulation of the result across ranks, .i.e. MPI_Allreduce() time
204: .    number of ranks - the time for the result will grow with the log base 2 of the number of ranks sharing the vector
205: -    work load imbalance - the rank with the largest number of vector entries will limit the speed up

207: .seealso: `VecDot()`, `VecTDot()`, `VecNorm()`, `VecDotBegin()`, `VecDotEnd()`, `VecNormAvailable()`,
208:           `VecNormBegin()`, `VecNormEnd()`, `NormType()`

210: @*/
211: PetscErrorCode VecNorm(Vec x, NormType type, PetscReal *val)
212: {

217:   /* Cached data? */
218:   if (type != NORM_1_AND_2) {
219:     PetscBool flg;

221:     PetscObjectComposedDataGetReal((PetscObject)x, NormIds[type], *val, flg);
222:     if (flg) return 0;
223:   }

225:   VecLockReadPush(x);
226:   PetscLogEventBegin(VEC_Norm, x, 0, 0, 0);
227:   PetscUseTypeMethod(x, norm, type, val);
228:   PetscLogEventEnd(VEC_Norm, x, 0, 0, 0);
229:   VecLockReadPop(x);

231:   if (type != NORM_1_AND_2) PetscObjectComposedDataSetReal((PetscObject)x, NormIds[type], *val);
232:   return 0;
233: }

235: /*@
236:    VecNormAvailable  - Returns the vector norm if it is already known.

238:    Not Collective

240:    Input Parameters:
241: +  x - the vector
242: -  type - one of NORM_1, NORM_2, NORM_INFINITY.  Also available
243:           NORM_1_AND_2, which computes both norms and stores them
244:           in a two element array.

246:    Output Parameters:
247: +  available - PETSC_TRUE if the val returned is valid
248: -  val - the norm

250:    Notes:
251: $     NORM_1 denotes sum_i |x_i|
252: $     NORM_2 denotes sqrt(sum_i (x_i)^2)
253: $     NORM_INFINITY denotes max_i |x_i|

255:    Level: intermediate

257:    Performance Issues:
258: $    per-processor memory bandwidth
259: $    interprocessor latency
260: $    work load imbalance that causes certain processes to arrive much earlier than others

262:    Compile Option:
263:    PETSC_HAVE_SLOW_BLAS_NORM2 will cause a C (loop unrolled) version of the norm to be used, rather
264:  than the BLAS. This should probably only be used when one is using the FORTRAN BLAS routines
265:  (as opposed to vendor provided) because the FORTRAN BLAS NRM2() routine is very slow.

267: .seealso: `VecDot()`, `VecTDot()`, `VecNorm()`, `VecDotBegin()`, `VecDotEnd()`, `VecNorm()`
268:           `VecNormBegin()`, `VecNormEnd()`

270: @*/
271: PetscErrorCode VecNormAvailable(Vec x, NormType type, PetscBool *available, PetscReal *val)
272: {

278:   if (type == NORM_1_AND_2) {
279:     *available = PETSC_FALSE;
280:   } else {
281:     PetscObjectComposedDataGetReal((PetscObject)x, NormIds[type], *val, *available);
282:   }
283:   return 0;
284: }

286: /*@
287:    VecNormalize - Normalizes a vector by 2-norm.

289:    Collective on Vec

291:    Input Parameter:
292: .  x - the vector

294:    Output Parameter:
295: .  val - the vector norm before normalization. May be `NULL` if the value is not needed.

297:    Level: intermediate

299: @*/
300: PetscErrorCode VecNormalize(Vec x, PetscReal *val)
301: {
302:   PetscReal norm;

306:   VecSetErrorIfLocked(x, 1);
308:   PetscLogEventBegin(VEC_Normalize, x, 0, 0, 0);
309:   VecNorm(x, NORM_2, &norm);
310:   if (norm == 0.0) {
311:     PetscInfo(x, "Vector of zero norm can not be normalized; Returning only the zero norm\n");
312:   } else if (norm != 1.0) {
313:     VecScale(x, 1.0 / norm);
314:   }
315:   PetscLogEventEnd(VEC_Normalize, x, 0, 0, 0);
316:   if (val) *val = norm;
317:   return 0;
318: }

320: /*@C
321:    VecMax - Determines the vector component with maximum real part and its location.

323:    Collective on Vec

325:    Input Parameter:
326: .  x - the vector

328:    Output Parameters:
329: +  p - the location of val (pass NULL if you don't want this)
330: -  val - the maximum component

332:    Notes:
333:    Returns the value PETSC_MIN_REAL and negative p if the vector is of length 0.

335:    Returns the smallest index with the maximum value
336:    Level: intermediate

338: .seealso: `VecNorm()`, `VecMin()`
339: @*/
340: PetscErrorCode VecMax(Vec x, PetscInt *p, PetscReal *val)
341: {
346:   VecLockReadPush(x);
347:   PetscLogEventBegin(VEC_Max, x, 0, 0, 0);
348:   PetscUseTypeMethod(x, max, p, val);
349:   PetscLogEventEnd(VEC_Max, x, 0, 0, 0);
350:   VecLockReadPop(x);
351:   return 0;
352: }

354: /*@C
355:    VecMin - Determines the vector component with minimum real part and its location.

357:    Collective on Vec

359:    Input Parameter:
360: .  x - the vector

362:    Output Parameters:
363: +  p - the location of val (pass NULL if you don't want this location)
364: -  val - the minimum component

366:    Level: intermediate

368:    Notes:
369:    Returns the value PETSC_MAX_REAL and negative p if the vector is of length 0.

371:    This returns the smallest index with the minumum value

373: .seealso: `VecMax()`
374: @*/
375: PetscErrorCode VecMin(Vec x, PetscInt *p, PetscReal *val)
376: {
381:   VecLockReadPush(x);
382:   PetscLogEventBegin(VEC_Min, x, 0, 0, 0);
383:   PetscUseTypeMethod(x, min, p, val);
384:   PetscLogEventEnd(VEC_Min, x, 0, 0, 0);
385:   VecLockReadPop(x);
386:   return 0;
387: }

389: /*@
390:    VecTDot - Computes an indefinite vector dot product. That is, this
391:    routine does NOT use the complex conjugate.

393:    Collective on Vec

395:    Input Parameters:
396: .  x, y - the vectors

398:    Output Parameter:
399: .  val - the dot product

401:    Notes for Users of Complex Numbers:
402:    For complex vectors, VecTDot() computes the indefinite form
403: $     val = (x,y) = y^T x,
404:    where y^T denotes the transpose of y.

406:    Use VecDot() for the inner product
407: $     val = (x,y) = y^H x,
408:    where y^H denotes the conjugate transpose of y.

410:    Level: intermediate

412: .seealso: `VecDot()`, `VecMTDot()`
413: @*/
414: PetscErrorCode VecTDot(Vec x, Vec y, PetscScalar *val)
415: {
422:   VecCheckSameSize(x, 1, y, 2);

424:   VecLockReadPush(x);
425:   VecLockReadPush(y);
426:   PetscLogEventBegin(VEC_TDot, x, y, 0, 0);
427:   PetscUseTypeMethod(x, tdot, y, val);
428:   PetscLogEventEnd(VEC_TDot, x, y, 0, 0);
429:   VecLockReadPop(x);
430:   VecLockReadPop(y);
431:   return 0;
432: }

434: /*@
435:    VecScale - Scales a vector.

437:    Not collective on Vec

439:    Input Parameters:
440: +  x - the vector
441: -  alpha - the scalar

443:    Note:
444:    For a vector with n components, VecScale() computes
445: $      x[i] = alpha * x[i], for i=1,...,n.

447:    Level: intermediate

449: @*/
450: PetscErrorCode VecScale(Vec x, PetscScalar alpha)
451: {
452:   PetscReal norms[4];
453:   PetscBool flgs[4];

458:   VecSetErrorIfLocked(x, 1);
459:   if (alpha == (PetscScalar)1.0) return 0;

461:   /* get current stashed norms */
462:   for (PetscInt i = 0; i < 4; i++) PetscObjectComposedDataGetReal((PetscObject)x, NormIds[i], norms[i], flgs[i]);

464:   PetscLogEventBegin(VEC_Scale, x, 0, 0, 0);
465:   PetscUseTypeMethod(x, scale, alpha);
466:   PetscLogEventEnd(VEC_Scale, x, 0, 0, 0);

468:   PetscObjectStateIncrease((PetscObject)x);
469:   /* put the scaled stashed norms back into the Vec */
470:   for (PetscInt i = 0; i < 4; i++) {
471:     if (flgs[i]) PetscObjectComposedDataSetReal((PetscObject)x, NormIds[i], PetscAbsScalar(alpha) * norms[i]);
472:   }
473:   return 0;
474: }

476: /*@
477:    VecSet - Sets all components of a vector to a single scalar value.

479:    Logically Collective on Vec

481:    Input Parameters:
482: +  x  - the vector
483: -  alpha - the scalar

485:    Output Parameter:
486: .  x  - the vector

488:    Note:
489:    For a vector of dimension n, VecSet() computes
490: $     x[i] = alpha, for i=1,...,n,
491:    so that all vector entries then equal the identical
492:    scalar value, alpha.  Use the more general routine
493:    VecSetValues() to set different vector entries.

495:    You CANNOT call this after you have called VecSetValues() but before you call
496:    VecAssemblyBegin/End().

498:    Level: beginner

500: .seealso `VecSetValues()`, `VecSetValuesBlocked()`, `VecSetRandom()`

502: @*/
503: PetscErrorCode VecSet(Vec x, PetscScalar alpha)
504: {
509:   VecSetErrorIfLocked(x, 1);

511:   PetscLogEventBegin(VEC_Set, x, 0, 0, 0);
512:   PetscUseTypeMethod(x, set, alpha);
513:   PetscLogEventEnd(VEC_Set, x, 0, 0, 0);
514:   PetscObjectStateIncrease((PetscObject)x);

516:   /*  norms can be simply set (if |alpha|*N not too large) */

518:   {
519:     PetscReal      val = PetscAbsScalar(alpha);
520:     const PetscInt N   = x->map->N;

522:     if (N == 0) {
523:       PetscObjectComposedDataSetReal((PetscObject)x, NormIds[NORM_1], 0.0l);
524:       PetscObjectComposedDataSetReal((PetscObject)x, NormIds[NORM_INFINITY], 0.0);
525:       PetscObjectComposedDataSetReal((PetscObject)x, NormIds[NORM_2], 0.0);
526:       PetscObjectComposedDataSetReal((PetscObject)x, NormIds[NORM_FROBENIUS], 0.0);
527:     } else if (val > PETSC_MAX_REAL / N) {
528:       PetscObjectComposedDataSetReal((PetscObject)x, NormIds[NORM_INFINITY], val);
529:     } else {
530:       PetscObjectComposedDataSetReal((PetscObject)x, NormIds[NORM_1], N * val);
531:       PetscObjectComposedDataSetReal((PetscObject)x, NormIds[NORM_INFINITY], val);
532:       val = PetscSqrtReal((PetscReal)N) * val;
533:       PetscObjectComposedDataSetReal((PetscObject)x, NormIds[NORM_2], val);
534:       PetscObjectComposedDataSetReal((PetscObject)x, NormIds[NORM_FROBENIUS], val);
535:     }
536:   }
537:   return 0;
538: }

540: /*@
541:    VecAXPY - Computes y = alpha x + y.

543:    Logically Collective on Vec

545:    Input Parameters:
546: +  alpha - the scalar
547: -  x, y  - the vectors

549:    Output Parameter:
550: .  y - output vector

552:    Level: intermediate

554:    Notes:
555:     x and y MUST be different vectors
556:     This routine is optimized for alpha of 0.0, otherwise it calls the BLAS routine

558: $    VecAXPY(y,alpha,x)                   y = alpha x           +      y
559: $    VecAYPX(y,beta,x)                    y =       x           + beta y
560: $    VecAXPBY(y,alpha,beta,x)             y = alpha x           + beta y
561: $    VecWAXPY(w,alpha,x,y)                w = alpha x           +      y
562: $    VecAXPBYPCZ(w,alpha,beta,gamma,x,y)  z = alpha x           + beta y + gamma z
563: $    VecMAXPY(y,nv,alpha[],x[])           y = sum alpha[i] x[i] +      y

565: .seealso: `VecAYPX()`, `VecMAXPY()`, `VecWAXPY()`, `VecAXPBYPCZ()`, `VecAXPBY()`
566: @*/
567: PetscErrorCode VecAXPY(Vec y, PetscScalar alpha, Vec x)
568: {
574:   VecCheckSameSize(x, 3, y, 1);
577:   if (alpha == (PetscScalar)0.0) return 0;

579:   VecSetErrorIfLocked(y, 1);
580:   VecLockReadPush(x);
581:   PetscLogEventBegin(VEC_AXPY, x, y, 0, 0);
582:   PetscUseTypeMethod(y, axpy, alpha, x);
583:   PetscLogEventEnd(VEC_AXPY, x, y, 0, 0);
584:   VecLockReadPop(x);
585:   PetscObjectStateIncrease((PetscObject)y);
586:   return 0;
587: }

589: /*@
590:    VecAYPX - Computes y = x + beta y.

592:    Logically Collective on Vec

594:    Input Parameters:
595: +  beta - the scalar
596: -  x, y  - the vectors

598:    Output Parameter:
599: .  y - output vector

601:    Level: intermediate

603:    Notes:
604:     x and y MUST be different vectors
605:     The implementation is optimized for beta of -1.0, 0.0, and 1.0

607: .seealso: `VecMAXPY()`, `VecWAXPY()`, `VecAXPY()`, `VecAXPBYPCZ()`, `VecAXPBY()`
608: @*/
609: PetscErrorCode VecAYPX(Vec y, PetscScalar beta, Vec x)
610: {
616:   VecCheckSameSize(x, 1, y, 3);
619:   VecSetErrorIfLocked(y, 1);
620:   VecLockReadPush(x);
621:   if (beta == (PetscScalar)0.0) {
622:     VecCopy(x, y);
623:   } else {
624:     PetscLogEventBegin(VEC_AYPX, x, y, 0, 0);
625:     PetscUseTypeMethod(y, aypx, beta, x);
626:     PetscLogEventEnd(VEC_AYPX, x, y, 0, 0);
627:     PetscObjectStateIncrease((PetscObject)y);
628:   }
629:   VecLockReadPop(x);
630:   return 0;
631: }

633: /*@
634:    VecAXPBY - Computes y = alpha x + beta y.

636:    Logically Collective on Vec

638:    Input Parameters:
639: +  alpha,beta - the scalars
640: -  x, y  - the vectors

642:    Output Parameter:
643: .  y - output vector

645:    Level: intermediate

647:    Notes:
648:     x and y MUST be different vectors
649:     The implementation is optimized for alpha and/or beta values of 0.0 and 1.0

651: .seealso: `VecAYPX()`, `VecMAXPY()`, `VecWAXPY()`, `VecAXPY()`, `VecAXPBYPCZ()`
652: @*/
653: PetscErrorCode VecAXPBY(Vec y, PetscScalar alpha, PetscScalar beta, Vec x)
654: {
660:   VecCheckSameSize(y, 1, x, 4);
664:   if (alpha == (PetscScalar)0.0 && beta == (PetscScalar)1.0) return 0;

666:   VecSetErrorIfLocked(y, 1);
667:   VecLockReadPush(x);
668:   PetscLogEventBegin(VEC_AXPY, y, x, 0, 0);
669:   PetscUseTypeMethod(y, axpby, alpha, beta, x);
670:   PetscLogEventEnd(VEC_AXPY, y, x, 0, 0);
671:   PetscObjectStateIncrease((PetscObject)y);
672:   VecLockReadPop(x);
673:   return 0;
674: }

676: /*@
677:    VecAXPBYPCZ - Computes z = alpha x + beta y + gamma z

679:    Logically Collective on Vec

681:    Input Parameters:
682: +  alpha,beta, gamma - the scalars
683: -  x, y, z  - the vectors

685:    Output Parameter:
686: .  z - output vector

688:    Level: intermediate

690:    Notes:
691:     x, y and z must be different vectors
692:     The implementation is optimized for alpha of 1.0 and gamma of 1.0 or 0.0

694: .seealso: `VecAYPX()`, `VecMAXPY()`, `VecWAXPY()`, `VecAXPY()`, `VecAXPBY()`
695: @*/
696: PetscErrorCode VecAXPBYPCZ(Vec z, PetscScalar alpha, PetscScalar beta, PetscScalar gamma, Vec x, Vec y)
697: {
706:   VecCheckSameSize(x, 1, y, 5);
707:   VecCheckSameSize(x, 1, z, 6);
713:   if (alpha == (PetscScalar)0.0 && beta == (PetscScalar)0.0 && gamma == (PetscScalar)1.0) return 0;

715:   VecSetErrorIfLocked(z, 1);
716:   VecLockReadPush(x);
717:   VecLockReadPush(y);
718:   PetscLogEventBegin(VEC_AXPBYPCZ, x, y, z, 0);
719:   PetscUseTypeMethod(z, axpbypcz, alpha, beta, gamma, x, y);
720:   PetscLogEventEnd(VEC_AXPBYPCZ, x, y, z, 0);
721:   PetscObjectStateIncrease((PetscObject)z);
722:   VecLockReadPop(x);
723:   VecLockReadPop(y);
724:   return 0;
725: }

727: /*@
728:    VecWAXPY - Computes w = alpha x + y.

730:    Logically Collective on Vec

732:    Input Parameters:
733: +  alpha - the scalar
734: -  x, y  - the vectors

736:    Output Parameter:
737: .  w - the result

739:    Level: intermediate

741:    Notes:
742:     w cannot be either x or y, but x and y can be the same
743:     The implementation is optimzed for alpha of -1.0, 0.0, and 1.0

745: .seealso: `VecAXPY()`, `VecAYPX()`, `VecAXPBY()`, `VecMAXPY()`, `VecAXPBYPCZ()`
746: @*/
747: PetscErrorCode VecWAXPY(Vec w, PetscScalar alpha, Vec x, Vec y)
748: {
757:   VecCheckSameSize(x, 3, y, 4);
758:   VecCheckSameSize(x, 3, w, 1);
762:   VecSetErrorIfLocked(w, 1);

764:   VecLockReadPush(x);
765:   VecLockReadPush(y);
766:   if (alpha == (PetscScalar)0.0) {
767:     VecCopy(y, w);
768:   } else {
769:     PetscLogEventBegin(VEC_WAXPY, x, y, w, 0);
770:     PetscUseTypeMethod(w, waxpy, alpha, x, y);
771:     PetscLogEventEnd(VEC_WAXPY, x, y, w, 0);
772:     PetscObjectStateIncrease((PetscObject)w);
773:   }
774:   VecLockReadPop(x);
775:   VecLockReadPop(y);
776:   return 0;
777: }

779: /*@C
780:    VecSetValues - Inserts or adds values into certain locations of a vector.

782:    Not Collective

784:    Input Parameters:
785: +  x - vector to insert in
786: .  ni - number of elements to add
787: .  ix - indices where to add
788: .  y - array of values
789: -  iora - either INSERT_VALUES or ADD_VALUES, where
790:    ADD_VALUES adds values to any existing entries, and
791:    INSERT_VALUES replaces existing entries with new values

793:    Notes:
794:    VecSetValues() sets x[ix[i]] = y[i], for i=0,...,ni-1.

796:    Calls to VecSetValues() with the INSERT_VALUES and ADD_VALUES
797:    options cannot be mixed without intervening calls to the assembly
798:    routines.

800:    These values may be cached, so VecAssemblyBegin() and VecAssemblyEnd()
801:    MUST be called after all calls to VecSetValues() have been completed.

803:    VecSetValues() uses 0-based indices in Fortran as well as in C.

805:    If you call VecSetOption(x, VEC_IGNORE_NEGATIVE_INDICES,PETSC_TRUE),
806:    negative indices may be passed in ix. These rows are
807:    simply ignored. This allows easily inserting element load matrices
808:    with homogeneous Dirchlet boundary conditions that you don't want represented
809:    in the vector.

811:    Level: beginner

813: .seealso: `VecAssemblyBegin()`, `VecAssemblyEnd()`, `VecSetValuesLocal()`,
814:           `VecSetValue()`, `VecSetValuesBlocked()`, `InsertMode`, `INSERT_VALUES`, `ADD_VALUES`, `VecGetValues()`
815: @*/
816: PetscErrorCode VecSetValues(Vec x, PetscInt ni, const PetscInt ix[], const PetscScalar y[], InsertMode iora)
817: {
820:   if (!ni) return 0;

825:   PetscLogEventBegin(VEC_SetValues, x, 0, 0, 0);
826:   PetscUseTypeMethod(x, setvalues, ni, ix, y, iora);
827:   PetscLogEventEnd(VEC_SetValues, x, 0, 0, 0);
828:   PetscObjectStateIncrease((PetscObject)x);
829:   return 0;
830: }

832: /*@C
833:    VecGetValues - Gets values from certain locations of a vector. Currently
834:           can only get values on the same processor

836:     Not Collective

838:    Input Parameters:
839: +  x - vector to get values from
840: .  ni - number of elements to get
841: -  ix - indices where to get them from (in global 1d numbering)

843:    Output Parameter:
844: .   y - array of values

846:    Notes:
847:    The user provides the allocated array y; it is NOT allocated in this routine

849:    VecGetValues() gets y[i] = x[ix[i]], for i=0,...,ni-1.

851:    VecAssemblyBegin() and VecAssemblyEnd()  MUST be called before calling this

853:    VecGetValues() uses 0-based indices in Fortran as well as in C.

855:    If you call VecSetOption(x, VEC_IGNORE_NEGATIVE_INDICES,PETSC_TRUE),
856:    negative indices may be passed in ix. These rows are
857:    simply ignored.

859:    Level: beginner

861: .seealso: `VecAssemblyBegin()`, `VecAssemblyEnd()`, `VecSetValues()`
862: @*/
863: PetscErrorCode VecGetValues(Vec x, PetscInt ni, const PetscInt ix[], PetscScalar y[])
864: {
866:   if (!ni) return 0;
870:   PetscUseTypeMethod(x, getvalues, ni, ix, y);
871:   return 0;
872: }

874: /*@C
875:    VecSetValuesBlocked - Inserts or adds blocks of values into certain locations of a vector.

877:    Not Collective

879:    Input Parameters:
880: +  x - vector to insert in
881: .  ni - number of blocks to add
882: .  ix - indices where to add in block count, rather than element count
883: .  y - array of values
884: -  iora - either INSERT_VALUES or ADD_VALUES, where
885:    ADD_VALUES adds values to any existing entries, and
886:    INSERT_VALUES replaces existing entries with new values

888:    Notes:
889:    VecSetValuesBlocked() sets x[bs*ix[i]+j] = y[bs*i+j],
890:    for j=0,...,bs-1, for i=0,...,ni-1. where bs was set with VecSetBlockSize().

892:    Calls to VecSetValuesBlocked() with the INSERT_VALUES and ADD_VALUES
893:    options cannot be mixed without intervening calls to the assembly
894:    routines.

896:    These values may be cached, so VecAssemblyBegin() and VecAssemblyEnd()
897:    MUST be called after all calls to VecSetValuesBlocked() have been completed.

899:    VecSetValuesBlocked() uses 0-based indices in Fortran as well as in C.

901:    Negative indices may be passed in ix, these rows are
902:    simply ignored. This allows easily inserting element load matrices
903:    with homogeneous Dirchlet boundary conditions that you don't want represented
904:    in the vector.

906:    Level: intermediate

908: .seealso: `VecAssemblyBegin()`, `VecAssemblyEnd()`, `VecSetValuesBlockedLocal()`,
909:           `VecSetValues()`
910: @*/
911: PetscErrorCode VecSetValuesBlocked(Vec x, PetscInt ni, const PetscInt ix[], const PetscScalar y[], InsertMode iora)
912: {
915:   if (!ni) return 0;

920:   PetscLogEventBegin(VEC_SetValues, x, 0, 0, 0);
921:   PetscUseTypeMethod(x, setvaluesblocked, ni, ix, y, iora);
922:   PetscLogEventEnd(VEC_SetValues, x, 0, 0, 0);
923:   PetscObjectStateIncrease((PetscObject)x);
924:   return 0;
925: }

927: /*@C
928:    VecSetValuesLocal - Inserts or adds values into certain locations of a vector,
929:    using a local ordering of the nodes.

931:    Not Collective

933:    Input Parameters:
934: +  x - vector to insert in
935: .  ni - number of elements to add
936: .  ix - indices where to add
937: .  y - array of values
938: -  iora - either INSERT_VALUES or ADD_VALUES, where
939:    ADD_VALUES adds values to any existing entries, and
940:    INSERT_VALUES replaces existing entries with new values

942:    Level: intermediate

944:    Notes:
945:    VecSetValuesLocal() sets x[ix[i]] = y[i], for i=0,...,ni-1.

947:    Calls to VecSetValues() with the INSERT_VALUES and ADD_VALUES
948:    options cannot be mixed without intervening calls to the assembly
949:    routines.

951:    These values may be cached, so VecAssemblyBegin() and VecAssemblyEnd()
952:    MUST be called after all calls to VecSetValuesLocal() have been completed.

954:    VecSetValuesLocal() uses 0-based indices in Fortran as well as in C.

956: .seealso: `VecAssemblyBegin()`, `VecAssemblyEnd()`, `VecSetValues()`, `VecSetLocalToGlobalMapping()`,
957:           `VecSetValuesBlockedLocal()`
958: @*/
959: PetscErrorCode VecSetValuesLocal(Vec x, PetscInt ni, const PetscInt ix[], const PetscScalar y[], InsertMode iora)
960: {
961:   PetscInt lixp[128], *lix = lixp;

965:   if (!ni) return 0;

970:   PetscLogEventBegin(VEC_SetValues, x, 0, 0, 0);
971:   if (!x->ops->setvalueslocal) {
972:     if (x->map->mapping) {
973:       if (ni > 128) PetscMalloc1(ni, &lix);
974:       ISLocalToGlobalMappingApply(x->map->mapping, ni, (PetscInt *)ix, lix);
975:       PetscUseTypeMethod(x, setvalues, ni, lix, y, iora);
976:       if (ni > 128) PetscFree(lix);
977:     } else PetscUseTypeMethod(x, setvalues, ni, ix, y, iora);
978:   } else PetscUseTypeMethod(x, setvalueslocal, ni, ix, y, iora);
979:   PetscLogEventEnd(VEC_SetValues, x, 0, 0, 0);
980:   PetscObjectStateIncrease((PetscObject)x);
981:   return 0;
982: }

984: /*@
985:    VecSetValuesBlockedLocal - Inserts or adds values into certain locations of a vector,
986:    using a local ordering of the nodes.

988:    Not Collective

990:    Input Parameters:
991: +  x - vector to insert in
992: .  ni - number of blocks to add
993: .  ix - indices where to add in block count, not element count
994: .  y - array of values
995: -  iora - either INSERT_VALUES or ADD_VALUES, where
996:    ADD_VALUES adds values to any existing entries, and
997:    INSERT_VALUES replaces existing entries with new values

999:    Level: intermediate

1001:    Notes:
1002:    VecSetValuesBlockedLocal() sets x[bs*ix[i]+j] = y[bs*i+j],
1003:    for j=0,..bs-1, for i=0,...,ni-1, where bs has been set with VecSetBlockSize().

1005:    Calls to VecSetValuesBlockedLocal() with the INSERT_VALUES and ADD_VALUES
1006:    options cannot be mixed without intervening calls to the assembly
1007:    routines.

1009:    These values may be cached, so VecAssemblyBegin() and VecAssemblyEnd()
1010:    MUST be called after all calls to VecSetValuesBlockedLocal() have been completed.

1012:    VecSetValuesBlockedLocal() uses 0-based indices in Fortran as well as in C.

1014: .seealso: `VecAssemblyBegin()`, `VecAssemblyEnd()`, `VecSetValues()`, `VecSetValuesBlocked()`,
1015:           `VecSetLocalToGlobalMapping()`
1016: @*/
1017: PetscErrorCode VecSetValuesBlockedLocal(Vec x, PetscInt ni, const PetscInt ix[], const PetscScalar y[], InsertMode iora)
1018: {
1019:   PetscInt lixp[128], *lix = lixp;

1023:   if (!ni) return 0;
1027:   PetscLogEventBegin(VEC_SetValues, x, 0, 0, 0);
1028:   if (x->map->mapping) {
1029:     if (ni > 128) PetscMalloc1(ni, &lix);
1030:     ISLocalToGlobalMappingApplyBlock(x->map->mapping, ni, (PetscInt *)ix, lix);
1031:     PetscUseTypeMethod(x, setvaluesblocked, ni, lix, y, iora);
1032:     if (ni > 128) PetscFree(lix);
1033:   } else {
1034:     PetscUseTypeMethod(x, setvaluesblocked, ni, ix, y, iora);
1035:   }
1036:   PetscLogEventEnd(VEC_SetValues, x, 0, 0, 0);
1037:   PetscObjectStateIncrease((PetscObject)x);
1038:   return 0;
1039: }

1041: /*@
1042:    VecMTDot - Computes indefinite vector multiple dot products.
1043:    That is, it does NOT use the complex conjugate.

1045:    Collective on Vec

1047:    Input Parameters:
1048: +  x - one vector
1049: .  nv - number of vectors
1050: -  y - array of vectors.  Note that vectors are pointers

1052:    Output Parameter:
1053: .  val - array of the dot products

1055:    Notes for Users of Complex Numbers:
1056:    For complex vectors, VecMTDot() computes the indefinite form
1057: $      val = (x,y) = y^T x,
1058:    where y^T denotes the transpose of y.

1060:    Use VecMDot() for the inner product
1061: $      val = (x,y) = y^H x,
1062:    where y^H denotes the conjugate transpose of y.

1064:    Level: intermediate

1066: .seealso: `VecMDot()`, `VecTDot()`
1067: @*/
1068: PetscErrorCode VecMTDot(Vec x, PetscInt nv, const Vec y[], PetscScalar val[])
1069: {
1073:   if (!nv) return 0;
1075:   for (PetscInt i = 0; i < nv; ++i) {
1079:     VecCheckSameSize(x, 1, y[i], 3);
1080:     VecLockReadPush(y[i]);
1081:   }

1084:   VecLockReadPush(x);
1085:   PetscLogEventBegin(VEC_MTDot, x, *y, 0, 0);
1086:   PetscUseTypeMethod(x, mtdot, nv, y, val);
1087:   PetscLogEventEnd(VEC_MTDot, x, *y, 0, 0);
1088:   VecLockReadPop(x);
1089:   for (PetscInt i = 0; i < nv; ++i) VecLockReadPop(y[i]);
1090:   return 0;
1091: }

1093: /*@
1094:    VecMDot - Computes vector multiple dot products.

1096:    Collective on Vec

1098:    Input Parameters:
1099: +  x - one vector
1100: .  nv - number of vectors
1101: -  y - array of vectors.

1103:    Output Parameter:
1104: .  val - array of the dot products (does not allocate the array)

1106:    Notes for Users of Complex Numbers:
1107:    For complex vectors, VecMDot() computes
1108: $     val = (x,y) = y^H x,
1109:    where y^H denotes the conjugate transpose of y.

1111:    Use VecMTDot() for the indefinite form
1112: $     val = (x,y) = y^T x,
1113:    where y^T denotes the transpose of y.

1115:    Level: intermediate

1117: .seealso: `VecMTDot()`, `VecDot()`
1118: @*/
1119: PetscErrorCode VecMDot(Vec x, PetscInt nv, const Vec y[], PetscScalar val[])
1120: {
1124:   if (!nv) return 0;
1126:   for (PetscInt i = 0; i < nv; ++i) {
1130:     VecCheckSameSize(x, 1, y[i], 3);
1131:     VecLockReadPush(y[i]);
1132:   }

1135:   VecLockReadPush(x);
1136:   PetscLogEventBegin(VEC_MDot, x, *y, 0, 0);
1137:   PetscUseTypeMethod(x, mdot, nv, y, val);
1138:   PetscLogEventEnd(VEC_MDot, x, *y, 0, 0);
1139:   VecLockReadPop(x);
1140:   for (PetscInt i = 0; i < nv; ++i) VecLockReadPop(y[i]);
1141:   return 0;
1142: }

1144: /*@
1145:    VecMAXPY - Computes y = y + sum alpha[i] x[i]

1147:    Logically Collective on Vec

1149:    Input Parameters:
1150: +  nv - number of scalars and x-vectors
1151: .  alpha - array of scalars
1152: .  y - one vector
1153: -  x - array of vectors

1155:    Level: intermediate

1157:    Notes:
1158:     y cannot be any of the x vectors

1160: .seealso: `VecAYPX()`, `VecWAXPY()`, `VecAXPY()`, `VecAXPBYPCZ()`, `VecAXPBY()`
1161: @*/
1162: PetscErrorCode VecMAXPY(Vec y, PetscInt nv, const PetscScalar alpha[], Vec x[])
1163: {
1166:   VecSetErrorIfLocked(y, 1);
1168:   if (nv) {
1169:     PetscInt zeros = 0;

1173:     for (PetscInt i = 0; i < nv; ++i) {
1178:       VecCheckSameSize(y, 1, x[i], 4);
1180:       VecLockReadPush(x[i]);
1181:       zeros += alpha[i] == (PetscScalar)0.0;
1182:     }

1184:     if (zeros < nv) {
1185:       PetscLogEventBegin(VEC_MAXPY, y, *x, 0, 0);
1186:       PetscUseTypeMethod(y, maxpy, nv, alpha, x);
1187:       PetscLogEventEnd(VEC_MAXPY, y, *x, 0, 0);
1188:       PetscObjectStateIncrease((PetscObject)y);
1189:     }

1191:     for (PetscInt i = 0; i < nv; ++i) VecLockReadPop(x[i]);
1192:   }
1193:   return 0;
1194: }

1196: /*@
1197:    VecConcatenate - Creates a new vector that is a vertical concatenation of all the given array of vectors
1198:                     in the order they appear in the array. The concatenated vector resides on the same
1199:                     communicator and is the same type as the source vectors.

1201:    Collective on X

1203:    Input Parameters:
1204: +  nx   - number of vectors to be concatenated
1205: -  X    - array containing the vectors to be concatenated in the order of concatenation

1207:    Output Parameters:
1208: +  Y    - concatenated vector
1209: -  x_is - array of index sets corresponding to the concatenated components of Y (NULL if not needed)

1211:    Notes:
1212:    Concatenation is similar to the functionality of a VecNest object; they both represent combination of
1213:    different vector spaces. However, concatenated vectors do not store any information about their
1214:    sub-vectors and own their own data. Consequently, this function provides index sets to enable the
1215:    manipulation of data in the concatenated vector that corresponds to the original components at creation.

1217:    This is a useful tool for outer loop algorithms, particularly constrained optimizers, where the solver
1218:    has to operate on combined vector spaces and cannot utilize VecNest objects due to incompatibility with
1219:    bound projections.

1221:    Level: advanced

1223: .seealso: `VECNEST`, `VECSCATTER`, `VecScatterCreate()`
1224: @*/
1225: PetscErrorCode VecConcatenate(PetscInt nx, const Vec X[], Vec *Y, IS *x_is[])
1226: {
1227:   MPI_Comm comm;
1228:   VecType  vec_type;
1229:   Vec      Ytmp, Xtmp;
1230:   IS      *is_tmp;
1231:   PetscInt i, shift = 0, Xnl, Xng, Xbegin;


1238:   if ((*X)->ops->concatenate) {
1239:     /* use the dedicated concatenation function if available */
1240:     (*(*X)->ops->concatenate)(nx, X, Y, x_is);
1241:   } else {
1242:     /* loop over vectors and start creating IS */
1243:     comm = PetscObjectComm((PetscObject)(*X));
1244:     VecGetType(*X, &vec_type);
1245:     PetscMalloc1(nx, &is_tmp);
1246:     for (i = 0; i < nx; i++) {
1247:       VecGetSize(X[i], &Xng);
1248:       VecGetLocalSize(X[i], &Xnl);
1249:       VecGetOwnershipRange(X[i], &Xbegin, NULL);
1250:       ISCreateStride(comm, Xnl, shift + Xbegin, 1, &is_tmp[i]);
1251:       shift += Xng;
1252:     }
1253:     /* create the concatenated vector */
1254:     VecCreate(comm, &Ytmp);
1255:     VecSetType(Ytmp, vec_type);
1256:     VecSetSizes(Ytmp, PETSC_DECIDE, shift);
1257:     VecSetUp(Ytmp);
1258:     /* copy data from X array to Y and return */
1259:     for (i = 0; i < nx; i++) {
1260:       VecGetSubVector(Ytmp, is_tmp[i], &Xtmp);
1261:       VecCopy(X[i], Xtmp);
1262:       VecRestoreSubVector(Ytmp, is_tmp[i], &Xtmp);
1263:     }
1264:     *Y = Ytmp;
1265:     if (x_is) {
1266:       *x_is = is_tmp;
1267:     } else {
1268:       for (i = 0; i < nx; i++) ISDestroy(&is_tmp[i]);
1269:       PetscFree(is_tmp);
1270:     }
1271:   }
1272:   return 0;
1273: }

1275: /* A helper function for VecGetSubVector to check if we can implement it with no-copy (i.e. the subvector shares
1276:    memory with the original vector), and the block size of the subvector.

1278:     Input Parameters:
1279: +   X - the original vector
1280: -   is - the index set of the subvector

1282:     Output Parameters:
1283: +   contig - PETSC_TRUE if the index set refers to contiguous entries on this process, else PETSC_FALSE
1284: .   start  - start of contiguous block, as an offset from the start of the ownership range of the original vector
1285: -   blocksize - the block size of the subvector

1287: */
1288: PetscErrorCode VecGetSubVectorContiguityAndBS_Private(Vec X, IS is, PetscBool *contig, PetscInt *start, PetscInt *blocksize)
1289: {
1290:   PetscInt  gstart, gend, lstart;
1291:   PetscBool red[2] = {PETSC_TRUE /*contiguous*/, PETSC_TRUE /*validVBS*/};
1292:   PetscInt  n, N, ibs, vbs, bs = -1;

1294:   ISGetLocalSize(is, &n);
1295:   ISGetSize(is, &N);
1296:   ISGetBlockSize(is, &ibs);
1297:   VecGetBlockSize(X, &vbs);
1298:   VecGetOwnershipRange(X, &gstart, &gend);
1299:   ISContiguousLocal(is, gstart, gend, &lstart, &red[0]);
1300:   /* block size is given by IS if ibs > 1; otherwise, check the vector */
1301:   if (ibs > 1) {
1302:     MPIU_Allreduce(MPI_IN_PLACE, red, 1, MPIU_BOOL, MPI_LAND, PetscObjectComm((PetscObject)is));
1303:     bs = ibs;
1304:   } else {
1305:     if (n % vbs || vbs == 1) red[1] = PETSC_FALSE; /* this process invalidate the collectiveness of block size */
1306:     MPIU_Allreduce(MPI_IN_PLACE, red, 2, MPIU_BOOL, MPI_LAND, PetscObjectComm((PetscObject)is));
1307:     if (red[0] && red[1]) bs = vbs; /* all processes have a valid block size and the access will be contiguous */
1308:   }

1310:   *contig    = red[0];
1311:   *start     = lstart;
1312:   *blocksize = bs;
1313:   return 0;
1314: }

1316: /* A helper function for VecGetSubVector, to be used when we have to build a standalone subvector through VecScatter

1318:     Input Parameters:
1319: +   X - the original vector
1320: .   is - the index set of the subvector
1321: -   bs - the block size of the subvector, gotten from VecGetSubVectorContiguityAndBS_Private()

1323:     Output Parameters:
1324: .   Z  - the subvector, which will compose the VecScatter context on output
1325: */
1326: PetscErrorCode VecGetSubVectorThroughVecScatter_Private(Vec X, IS is, PetscInt bs, Vec *Z)
1327: {
1328:   PetscInt   n, N;
1329:   VecScatter vscat;
1330:   Vec        Y;

1332:   ISGetLocalSize(is, &n);
1333:   ISGetSize(is, &N);
1334:   VecCreate(PetscObjectComm((PetscObject)is), &Y);
1335:   VecSetSizes(Y, n, N);
1336:   VecSetBlockSize(Y, bs);
1337:   VecSetType(Y, ((PetscObject)X)->type_name);
1338:   VecScatterCreate(X, is, Y, NULL, &vscat);
1339:   VecScatterBegin(vscat, X, Y, INSERT_VALUES, SCATTER_FORWARD);
1340:   VecScatterEnd(vscat, X, Y, INSERT_VALUES, SCATTER_FORWARD);
1341:   PetscObjectCompose((PetscObject)Y, "VecGetSubVector_Scatter", (PetscObject)vscat);
1342:   VecScatterDestroy(&vscat);
1343:   *Z = Y;
1344:   return 0;
1345: }

1347: /*@
1348:    VecGetSubVector - Gets a vector representing part of another vector

1350:    Collective on X and IS

1352:    Input Parameters:
1353: + X - vector from which to extract a subvector
1354: - is - index set representing portion of X to extract

1356:    Output Parameter:
1357: . Y - subvector corresponding to is

1359:    Level: advanced

1361:    Notes:
1362:    The subvector Y should be returned with VecRestoreSubVector().
1363:    X and is must be defined on the same communicator

1365:    This function may return a subvector without making a copy, therefore it is not safe to use the original vector while
1366:    modifying the subvector.  Other non-overlapping subvectors can still be obtained from X using this function.
1367:    The resulting subvector inherits the block size from the IS if greater than one. Otherwise, the block size is guessed from the block size of the original vec.

1369: .seealso: `MatCreateSubMatrix()`
1370: @*/
1371: PetscErrorCode VecGetSubVector(Vec X, IS is, Vec *Y)
1372: {
1373:   Vec Z;

1379:   if (X->ops->getsubvector) {
1380:     PetscUseTypeMethod(X, getsubvector, is, &Z);
1381:   } else { /* Default implementation currently does no caching */
1382:     PetscBool contig;
1383:     PetscInt  n, N, start, bs;

1385:     ISGetLocalSize(is, &n);
1386:     ISGetSize(is, &N);
1387:     VecGetSubVectorContiguityAndBS_Private(X, is, &contig, &start, &bs);
1388:     if (contig) { /* We can do a no-copy implementation */
1389:       const PetscScalar *x;
1390:       PetscInt           state = 0;
1391:       PetscBool          isstd, iscuda, iship;

1393:       PetscObjectTypeCompareAny((PetscObject)X, &isstd, VECSEQ, VECMPI, VECSTANDARD, "");
1394:       PetscObjectTypeCompareAny((PetscObject)X, &iscuda, VECSEQCUDA, VECMPICUDA, "");
1395:       PetscObjectTypeCompareAny((PetscObject)X, &iship, VECSEQHIP, VECMPIHIP, "");
1396:       if (iscuda) {
1397: #if defined(PETSC_HAVE_CUDA)
1398:         const PetscScalar *x_d;
1399:         PetscMPIInt        size;
1400:         PetscOffloadMask   flg;

1402:         VecCUDAGetArrays_Private(X, &x, &x_d, &flg);
1405:         if (x) x += start;
1406:         if (x_d) x_d += start;
1407:         MPI_Comm_size(PetscObjectComm((PetscObject)X), &size);
1408:         if (size == 1) {
1409:           VecCreateSeqCUDAWithArrays(PetscObjectComm((PetscObject)X), bs, n, x, x_d, &Z);
1410:         } else {
1411:           VecCreateMPICUDAWithArrays(PetscObjectComm((PetscObject)X), bs, n, N, x, x_d, &Z);
1412:         }
1413:         Z->offloadmask = flg;
1414: #endif
1415:       } else if (iship) {
1416: #if defined(PETSC_HAVE_HIP)
1417:         const PetscScalar *x_d;
1418:         PetscMPIInt        size;
1419:         PetscOffloadMask   flg;

1421:         VecHIPGetArrays_Private(X, &x, &x_d, &flg);
1424:         if (x) x += start;
1425:         if (x_d) x_d += start;
1426:         MPI_Comm_size(PetscObjectComm((PetscObject)X), &size);
1427:         if (size == 1) {
1428:           VecCreateSeqHIPWithArrays(PetscObjectComm((PetscObject)X), bs, n, x, x_d, &Z);
1429:         } else {
1430:           VecCreateMPIHIPWithArrays(PetscObjectComm((PetscObject)X), bs, n, N, x, x_d, &Z);
1431:         }
1432:         Z->offloadmask = flg;
1433: #endif
1434:       } else if (isstd) {
1435:         PetscMPIInt size;

1437:         MPI_Comm_size(PetscObjectComm((PetscObject)X), &size);
1438:         VecGetArrayRead(X, &x);
1439:         if (x) x += start;
1440:         if (size == 1) {
1441:           VecCreateSeqWithArray(PetscObjectComm((PetscObject)X), bs, n, x, &Z);
1442:         } else {
1443:           VecCreateMPIWithArray(PetscObjectComm((PetscObject)X), bs, n, N, x, &Z);
1444:         }
1445:         VecRestoreArrayRead(X, &x);
1446:       } else { /* default implementation: use place array */
1447:         VecGetArrayRead(X, &x);
1448:         VecCreate(PetscObjectComm((PetscObject)X), &Z);
1449:         VecSetType(Z, ((PetscObject)X)->type_name);
1450:         VecSetSizes(Z, n, N);
1451:         VecSetBlockSize(Z, bs);
1452:         VecPlaceArray(Z, x ? x + start : NULL);
1453:         VecRestoreArrayRead(X, &x);
1454:       }

1456:       /* this is relevant only in debug mode */
1457:       VecLockGet(X, &state);
1458:       if (state) VecLockReadPush(Z);
1459:       Z->ops->placearray   = NULL;
1460:       Z->ops->replacearray = NULL;
1461:     } else { /* Have to create a scatter and do a copy */
1462:       VecGetSubVectorThroughVecScatter_Private(X, is, bs, &Z);
1463:     }
1464:   }
1465:   /* Record the state when the subvector was gotten so we know whether its values need to be put back */
1466:   if (VecGetSubVectorSavedStateId < 0) PetscObjectComposedDataRegister(&VecGetSubVectorSavedStateId);
1467:   PetscObjectComposedDataSetInt((PetscObject)Z, VecGetSubVectorSavedStateId, 1);
1468:   *Y = Z;
1469:   return 0;
1470: }

1472: /*@
1473:    VecRestoreSubVector - Restores a subvector extracted using VecGetSubVector()

1475:    Collective on IS

1477:    Input Parameters:
1478: + X - vector from which subvector was obtained
1479: . is - index set representing the subset of X
1480: - Y - subvector being restored

1482:    Level: advanced

1484: .seealso: `VecGetSubVector()`
1485: @*/
1486: PetscErrorCode VecRestoreSubVector(Vec X, IS is, Vec *Y)
1487: {
1488:   PETSC_UNUSED PetscObjectState dummystate = 0;
1489:   PetscBool                     unchanged;


1497:   if (X->ops->restoresubvector) PetscUseTypeMethod(X, restoresubvector, is, Y);
1498:   else {
1499:     PetscObjectComposedDataGetInt((PetscObject)*Y, VecGetSubVectorSavedStateId, dummystate, unchanged);
1500:     if (!unchanged) { /* If Y's state has not changed since VecGetSubVector(), we only need to destroy Y */
1501:       VecScatter scatter;
1502:       PetscInt   state;

1504:       VecLockGet(X, &state);

1507:       PetscObjectQuery((PetscObject)*Y, "VecGetSubVector_Scatter", (PetscObject *)&scatter);
1508:       if (scatter) {
1509:         VecScatterBegin(scatter, *Y, X, INSERT_VALUES, SCATTER_REVERSE);
1510:         VecScatterEnd(scatter, *Y, X, INSERT_VALUES, SCATTER_REVERSE);
1511:       } else {
1512:         PetscBool iscuda, iship;
1513:         PetscObjectTypeCompareAny((PetscObject)X, &iscuda, VECSEQCUDA, VECMPICUDA, "");
1514:         PetscObjectTypeCompareAny((PetscObject)X, &iship, VECSEQHIP, VECMPIHIP, "");

1516:         if (iscuda) {
1517: #if defined(PETSC_HAVE_CUDA)
1518:           PetscOffloadMask ymask = (*Y)->offloadmask;

1520:           /* The offloadmask of X dictates where to move memory
1521:               If X GPU data is valid, then move Y data on GPU if needed
1522:               Otherwise, move back to the CPU */
1523:           switch (X->offloadmask) {
1524:           case PETSC_OFFLOAD_BOTH:
1525:             if (ymask == PETSC_OFFLOAD_CPU) {
1526:               VecCUDAResetArray(*Y);
1527:             } else if (ymask == PETSC_OFFLOAD_GPU) {
1528:               X->offloadmask = PETSC_OFFLOAD_GPU;
1529:             }
1530:             break;
1531:           case PETSC_OFFLOAD_GPU:
1532:             if (ymask == PETSC_OFFLOAD_CPU) VecCUDAResetArray(*Y);
1533:             break;
1534:           case PETSC_OFFLOAD_CPU:
1535:             if (ymask == PETSC_OFFLOAD_GPU) VecResetArray(*Y);
1536:             break;
1537:           case PETSC_OFFLOAD_UNALLOCATED:
1538:           case PETSC_OFFLOAD_KOKKOS:
1539:             SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "This should not happen");
1540:           }
1541: #endif
1542:         } else if (iship) {
1543: #if defined(PETSC_HAVE_HIP)
1544:           PetscOffloadMask ymask = (*Y)->offloadmask;

1546:           /* The offloadmask of X dictates where to move memory
1547:               If X GPU data is valid, then move Y data on GPU if needed
1548:               Otherwise, move back to the CPU */
1549:           switch (X->offloadmask) {
1550:           case PETSC_OFFLOAD_BOTH:
1551:             if (ymask == PETSC_OFFLOAD_CPU) {
1552:               VecHIPResetArray(*Y);
1553:             } else if (ymask == PETSC_OFFLOAD_GPU) {
1554:               X->offloadmask = PETSC_OFFLOAD_GPU;
1555:             }
1556:             break;
1557:           case PETSC_OFFLOAD_GPU:
1558:             if (ymask == PETSC_OFFLOAD_CPU) VecHIPResetArray(*Y);
1559:             break;
1560:           case PETSC_OFFLOAD_CPU:
1561:             if (ymask == PETSC_OFFLOAD_GPU) VecResetArray(*Y);
1562:             break;
1563:           case PETSC_OFFLOAD_UNALLOCATED:
1564:           case PETSC_OFFLOAD_KOKKOS:
1565:             SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "This should not happen");
1566:           }
1567: #endif
1568:         } else {
1569:           /* If OpenCL vecs updated the device memory, this triggers a copy on the CPU */
1570:           VecResetArray(*Y);
1571:         }
1572:         PetscObjectStateIncrease((PetscObject)X);
1573:       }
1574:     }
1575:   }
1576:   VecDestroy(Y);
1577:   return 0;
1578: }

1580: /*@
1581:    VecCreateLocalVector - Creates a vector object suitable for use with VecGetLocalVector() and friends. You must call VecDestroy() when the
1582:    vector is no longer needed.

1584:    Not collective.

1586:    Input parameter:
1587: .  v - The vector for which the local vector is desired.

1589:    Output parameter:
1590: .  w - Upon exit this contains the local vector.

1592:    Level: beginner

1594: .seealso: `VecGetLocalVectorRead()`, `VecRestoreLocalVectorRead()`, `VecGetLocalVector()`, `VecRestoreLocalVector()`
1595: @*/
1596: PetscErrorCode VecCreateLocalVector(Vec v, Vec *w)
1597: {
1598:   PetscMPIInt size;

1602:   MPI_Comm_size(PetscObjectComm((PetscObject)v), &size);
1603:   if (size == 1) VecDuplicate(v, w);
1604:   else if (v->ops->createlocalvector) PetscUseTypeMethod(v, createlocalvector, w);
1605:   else {
1606:     VecType  type;
1607:     PetscInt n;

1609:     VecCreate(PETSC_COMM_SELF, w);
1610:     VecGetLocalSize(v, &n);
1611:     VecSetSizes(*w, n, n);
1612:     VecGetBlockSize(v, &n);
1613:     VecSetBlockSize(*w, n);
1614:     VecGetType(v, &type);
1615:     VecSetType(*w, type);
1616:   }
1617:   return 0;
1618: }

1620: /*@
1621:    VecGetLocalVectorRead - Maps the local portion of a vector into a
1622:    vector.  You must call VecRestoreLocalVectorRead() when the local
1623:    vector is no longer needed.

1625:    Not collective.

1627:    Input parameter:
1628: .  v - The vector for which the local vector is desired.

1630:    Output parameter:
1631: .  w - Upon exit this contains the local vector.

1633:    Level: beginner

1635:    Notes:
1636:    This function is similar to VecGetArrayRead() which maps the local
1637:    portion into a raw pointer.  VecGetLocalVectorRead() is usually
1638:    almost as efficient as VecGetArrayRead() but in certain circumstances
1639:    VecGetLocalVectorRead() can be much more efficient than
1640:    VecGetArrayRead().  This is because the construction of a contiguous
1641:    array representing the vector data required by VecGetArrayRead() can
1642:    be an expensive operation for certain vector types.  For example, for
1643:    GPU vectors VecGetArrayRead() requires that the data between device
1644:    and host is synchronized.

1646:    Unlike VecGetLocalVector(), this routine is not collective and
1647:    preserves cached information.

1649: .seealso: `VecCreateLocalVector()`, `VecRestoreLocalVectorRead()`, `VecGetLocalVector()`, `VecGetArrayRead()`, `VecGetArray()`
1650: @*/
1651: PetscErrorCode VecGetLocalVectorRead(Vec v, Vec w)
1652: {
1655:   VecCheckSameLocalSize(v, 1, w, 2);
1656:   if (v->ops->getlocalvectorread) {
1657:     PetscUseTypeMethod(v, getlocalvectorread, w);
1658:   } else {
1659:     PetscScalar *a;

1661:     VecGetArrayRead(v, (const PetscScalar **)&a);
1662:     VecPlaceArray(w, a);
1663:   }
1664:   PetscObjectStateIncrease((PetscObject)w);
1665:   VecLockReadPush(v);
1666:   VecLockReadPush(w);
1667:   return 0;
1668: }

1670: /*@
1671:    VecRestoreLocalVectorRead - Unmaps the local portion of a vector
1672:    previously mapped into a vector using VecGetLocalVectorRead().

1674:    Not collective.

1676:    Input parameter:
1677: +  v - The local portion of this vector was previously mapped into w using VecGetLocalVectorRead().
1678: -  w - The vector into which the local portion of v was mapped.

1680:    Level: beginner

1682: .seealso: `VecCreateLocalVector()`, `VecGetLocalVectorRead()`, `VecGetLocalVector()`, `VecGetArrayRead()`, `VecGetArray()`
1683: @*/
1684: PetscErrorCode VecRestoreLocalVectorRead(Vec v, Vec w)
1685: {
1688:   if (v->ops->restorelocalvectorread) {
1689:     PetscUseTypeMethod(v, restorelocalvectorread, w);
1690:   } else {
1691:     const PetscScalar *a;

1693:     VecGetArrayRead(w, &a);
1694:     VecRestoreArrayRead(v, &a);
1695:     VecResetArray(w);
1696:   }
1697:   VecLockReadPop(v);
1698:   VecLockReadPop(w);
1699:   PetscObjectStateIncrease((PetscObject)w);
1700:   return 0;
1701: }

1703: /*@
1704:    VecGetLocalVector - Maps the local portion of a vector into a
1705:    vector.

1707:    Collective on v, not collective on w.

1709:    Input parameter:
1710: .  v - The vector for which the local vector is desired.

1712:    Output parameter:
1713: .  w - Upon exit this contains the local vector.

1715:    Level: beginner

1717:    Notes:
1718:    This function is similar to VecGetArray() which maps the local
1719:    portion into a raw pointer.  VecGetLocalVector() is usually about as
1720:    efficient as VecGetArray() but in certain circumstances
1721:    VecGetLocalVector() can be much more efficient than VecGetArray().
1722:    This is because the construction of a contiguous array representing
1723:    the vector data required by VecGetArray() can be an expensive
1724:    operation for certain vector types.  For example, for GPU vectors
1725:    VecGetArray() requires that the data between device and host is
1726:    synchronized.

1728: .seealso: `VecCreateLocalVector()`, `VecRestoreLocalVector()`, `VecGetLocalVectorRead()`, `VecGetArrayRead()`, `VecGetArray()`
1729: @*/
1730: PetscErrorCode VecGetLocalVector(Vec v, Vec w)
1731: {
1734:   VecCheckSameLocalSize(v, 1, w, 2);
1735:   if (v->ops->getlocalvector) {
1736:     PetscUseTypeMethod(v, getlocalvector, w);
1737:   } else {
1738:     PetscScalar *a;

1740:     VecGetArray(v, &a);
1741:     VecPlaceArray(w, a);
1742:   }
1743:   PetscObjectStateIncrease((PetscObject)w);
1744:   return 0;
1745: }

1747: /*@
1748:    VecRestoreLocalVector - Unmaps the local portion of a vector
1749:    previously mapped into a vector using VecGetLocalVector().

1751:    Logically collective.

1753:    Input parameter:
1754: +  v - The local portion of this vector was previously mapped into w using VecGetLocalVector().
1755: -  w - The vector into which the local portion of v was mapped.

1757:    Level: beginner

1759: .seealso: `VecCreateLocalVector()`, `VecGetLocalVector()`, `VecGetLocalVectorRead()`, `VecRestoreLocalVectorRead()`, `LocalVectorRead()`, `VecGetArrayRead()`, `VecGetArray()`
1760: @*/
1761: PetscErrorCode VecRestoreLocalVector(Vec v, Vec w)
1762: {
1765:   if (v->ops->restorelocalvector) {
1766:     PetscUseTypeMethod(v, restorelocalvector, w);
1767:   } else {
1768:     PetscScalar *a;
1769:     VecGetArray(w, &a);
1770:     VecRestoreArray(v, &a);
1771:     VecResetArray(w);
1772:   }
1773:   PetscObjectStateIncrease((PetscObject)w);
1774:   PetscObjectStateIncrease((PetscObject)v);
1775:   return 0;
1776: }

1778: /*@C
1779:    VecGetArray - Returns a pointer to a contiguous array that contains this
1780:    processor's portion of the vector data. For the standard PETSc
1781:    vectors, VecGetArray() returns a pointer to the local data array and
1782:    does not use any copies. If the underlying vector data is not stored
1783:    in a contiguous array this routine will copy the data to a contiguous
1784:    array and return a pointer to that. You MUST call VecRestoreArray()
1785:    when you no longer need access to the array.

1787:    Logically Collective on Vec

1789:    Input Parameter:
1790: .  x - the vector

1792:    Output Parameter:
1793: .  a - location to put pointer to the array

1795:    Fortran Note:
1796:    This routine is used differently from Fortran 77
1797: $    Vec         x
1798: $    PetscScalar x_array(1)
1799: $    PetscOffset i_x
1800: $    PetscErrorCode ierr
1801: $       call VecGetArray(x,x_array,i_x,ierr)
1802: $
1803: $   Access first local entry in vector with
1804: $      value = x_array(i_x + 1)
1805: $
1806: $      ...... other code
1807: $       call VecRestoreArray(x,x_array,i_x,ierr)
1808:    For Fortran 90 see VecGetArrayF90()

1810:    See the Fortran chapter of the users manual and
1811:    petsc/src/snes/tutorials/ex5f.F for details.

1813:    Level: beginner

1815: .seealso: `VecRestoreArray()`, `VecGetArrayRead()`, `VecGetArrays()`, `VecGetArrayF90()`, `VecGetArrayReadF90()`, `VecPlaceArray()`, `VecGetArray2d()`,
1816:           `VecGetArrayPair()`, `VecRestoreArrayPair()`, `VecGetArrayWrite()`, `VecRestoreArrayWrite()`
1817: @*/
1818: PetscErrorCode VecGetArray(Vec x, PetscScalar **a)
1819: {
1821:   VecSetErrorIfLocked(x, 1);
1822:   if (x->ops->getarray) { /* The if-else order matters! VECNEST, VECCUDA etc should have ops->getarray while VECCUDA etc are petscnative */
1823:     PetscUseTypeMethod(x, getarray, a);
1824:   } else if (x->petscnative) { /* VECSTANDARD */
1825:     *a = *((PetscScalar **)x->data);
1826:   } else SETERRQ(PetscObjectComm((PetscObject)x), PETSC_ERR_SUP, "Cannot get array for vector type \"%s\"", ((PetscObject)x)->type_name);
1827:   return 0;
1828: }

1830: /*@C
1831:    VecRestoreArray - Restores a vector after VecGetArray() has been called.

1833:    Logically Collective on Vec

1835:    Input Parameters:
1836: +  x - the vector
1837: -  a - location of pointer to array obtained from VecGetArray()

1839:    Level: beginner

1841: .seealso: `VecGetArray()`, `VecRestoreArrayRead()`, `VecRestoreArrays()`, `VecRestoreArrayF90()`, `VecRestoreArrayReadF90()`, `VecPlaceArray()`, `VecRestoreArray2d()`,
1842:           `VecGetArrayPair()`, `VecRestoreArrayPair()`
1843: @*/
1844: PetscErrorCode VecRestoreArray(Vec x, PetscScalar **a)
1845: {
1848:   if (x->ops->restorearray) {
1849:     PetscUseTypeMethod(x, restorearray, a);
1851:   if (a) *a = NULL;
1852:   PetscObjectStateIncrease((PetscObject)x);
1853:   return 0;
1854: }
1855: /*@C
1856:    VecGetArrayRead - Get read-only pointer to contiguous array containing this processor's portion of the vector data.

1858:    Not Collective

1860:    Input Parameter:
1861: .  x - the vector

1863:    Output Parameter:
1864: .  a - the array

1866:    Level: beginner

1868:    Notes:
1869:    The array must be returned using a matching call to VecRestoreArrayRead().

1871:    Unlike VecGetArray(), this routine is not collective and preserves cached information like vector norms.

1873:    Standard PETSc vectors use contiguous storage so that this routine does not perform a copy.  Other vector
1874:    implementations may require a copy, but must such implementations should cache the contiguous representation so that
1875:    only one copy is performed when this routine is called multiple times in sequence.

1877: .seealso: `VecGetArray()`, `VecRestoreArray()`, `VecGetArrayPair()`, `VecRestoreArrayPair()`
1878: @*/
1879: PetscErrorCode VecGetArrayRead(Vec x, const PetscScalar **a)
1880: {
1883:   if (x->ops->getarrayread) {
1884:     PetscUseTypeMethod(x, getarrayread, a);
1885:   } else if (x->ops->getarray) {
1886:     /* VECNEST, VECCUDA, VECKOKKOS etc */
1887:     PetscUseTypeMethod(x, getarray, (PetscScalar **)a);
1888:   } else if (x->petscnative) {
1889:     /* VECSTANDARD */
1890:     *a = *((PetscScalar **)x->data);
1891:   } else SETERRQ(PetscObjectComm((PetscObject)x), PETSC_ERR_SUP, "Cannot get array read for vector type \"%s\"", ((PetscObject)x)->type_name);
1892:   return 0;
1893: }

1895: /*@C
1896:    VecRestoreArrayRead - Restore array obtained with VecGetArrayRead()

1898:    Not Collective

1900:    Input Parameters:
1901: +  vec - the vector
1902: -  array - the array

1904:    Level: beginner

1906: .seealso: `VecGetArray()`, `VecRestoreArray()`, `VecGetArrayPair()`, `VecRestoreArrayPair()`
1907: @*/
1908: PetscErrorCode VecRestoreArrayRead(Vec x, const PetscScalar **a)
1909: {
1912:   if (x->petscnative) { /* VECSTANDARD, VECCUDA, VECKOKKOS etc */
1913:     /* nothing */
1914:   } else if (x->ops->restorearrayread) { /* VECNEST */
1915:     PetscUseTypeMethod(x, restorearrayread, a);
1916:   } else { /* No one? */
1917:     PetscUseTypeMethod(x, restorearray, (PetscScalar **)a);
1918:   }
1919:   if (a) *a = NULL;
1920:   return 0;
1921: }

1923: /*@C
1924:    VecGetArrayWrite - Returns a pointer to a contiguous array that WILL contains this
1925:    processor's portion of the vector data. The values in this array are NOT valid, the routine calling this
1926:    routine is responsible for putting values into the array; any values it does not set will be invalid

1928:    Logically Collective on Vec

1930:    Input Parameter:
1931: .  x - the vector

1933:    Output Parameter:
1934: .  a - location to put pointer to the array

1936:    Level: intermediate

1938:    This is for vectors associate with GPUs, the vector is not copied up before giving access. If you need correct
1939:    values in the array use VecGetArray()

1941: .seealso: `VecRestoreArray()`, `VecGetArrayRead()`, `VecGetArrays()`, `VecGetArrayF90()`, `VecGetArrayReadF90()`, `VecPlaceArray()`, `VecGetArray2d()`,
1942:           `VecGetArrayPair()`, `VecRestoreArrayPair()`, `VecGetArray()`, `VecRestoreArrayWrite()`
1943: @*/
1944: PetscErrorCode VecGetArrayWrite(Vec x, PetscScalar **a)
1945: {
1948:   VecSetErrorIfLocked(x, 1);
1949:   if (x->ops->getarraywrite) {
1950:     PetscUseTypeMethod(x, getarraywrite, a);
1951:   } else {
1952:     VecGetArray(x, a);
1953:   }
1954:   return 0;
1955: }

1957: /*@C
1958:    VecRestoreArrayWrite - Restores a vector after VecGetArrayWrite() has been called.

1960:    Logically Collective on Vec

1962:    Input Parameters:
1963: +  x - the vector
1964: -  a - location of pointer to array obtained from VecGetArray()

1966:    Level: beginner

1968: .seealso: `VecGetArray()`, `VecRestoreArrayRead()`, `VecRestoreArrays()`, `VecRestoreArrayF90()`, `VecRestoreArrayReadF90()`, `VecPlaceArray()`, `VecRestoreArray2d()`,
1969:           `VecGetArrayPair()`, `VecRestoreArrayPair()`, `VecGetArrayWrite()`
1970: @*/
1971: PetscErrorCode VecRestoreArrayWrite(Vec x, PetscScalar **a)
1972: {
1975:   if (x->ops->restorearraywrite) {
1976:     PetscUseTypeMethod(x, restorearraywrite, a);
1977:   } else if (x->ops->restorearray) {
1978:     PetscUseTypeMethod(x, restorearray, a);
1979:   }
1980:   if (a) *a = NULL;
1981:   PetscObjectStateIncrease((PetscObject)x);
1982:   return 0;
1983: }

1985: /*@C
1986:    VecGetArrays - Returns a pointer to the arrays in a set of vectors
1987:    that were created by a call to VecDuplicateVecs().  You MUST call
1988:    VecRestoreArrays() when you no longer need access to the array.

1990:    Logically Collective on Vec

1992:    Input Parameters:
1993: +  x - the vectors
1994: -  n - the number of vectors

1996:    Output Parameter:
1997: .  a - location to put pointer to the array

1999:    Fortran Note:
2000:    This routine is not supported in Fortran.

2002:    Level: intermediate

2004: .seealso: `VecGetArray()`, `VecRestoreArrays()`
2005: @*/
2006: PetscErrorCode VecGetArrays(const Vec x[], PetscInt n, PetscScalar **a[])
2007: {
2008:   PetscInt      i;
2009:   PetscScalar **q;

2015:   PetscMalloc1(n, &q);
2016:   for (i = 0; i < n; ++i) VecGetArray(x[i], &q[i]);
2017:   *a = q;
2018:   return 0;
2019: }

2021: /*@C
2022:    VecRestoreArrays - Restores a group of vectors after VecGetArrays()
2023:    has been called.

2025:    Logically Collective on Vec

2027:    Input Parameters:
2028: +  x - the vector
2029: .  n - the number of vectors
2030: -  a - location of pointer to arrays obtained from VecGetArrays()

2032:    Notes:
2033:    For regular PETSc vectors this routine does not involve any copies. For
2034:    any special vectors that do not store local vector data in a contiguous
2035:    array, this routine will copy the data back into the underlying
2036:    vector data structure from the arrays obtained with VecGetArrays().

2038:    Fortran Note:
2039:    This routine is not supported in Fortran.

2041:    Level: intermediate

2043: .seealso: `VecGetArrays()`, `VecRestoreArray()`
2044: @*/
2045: PetscErrorCode VecRestoreArrays(const Vec x[], PetscInt n, PetscScalar **a[])
2046: {
2047:   PetscInt      i;
2048:   PetscScalar **q = *a;


2054:   for (i = 0; i < n; ++i) VecRestoreArray(x[i], &q[i]);
2055:   PetscFree(q);
2056:   return 0;
2057: }

2059: /*@C
2060:    VecGetArrayAndMemType - Like VecGetArray(), but if this is a standard device vector (e.g., VECCUDA), the returned pointer will be a device
2061:    pointer to the device memory that contains this processor's portion of the vector data. Device data is guaranteed to have the latest value.
2062:    Otherwise, when this is a host vector (e.g., VECMPI), this routine functions the same as VecGetArray() and returns a host pointer.

2064:    For VECKOKKOS, if Kokkos is configured without device (e.g., use serial or openmp), per this function, the vector works like VECSEQ/VECMPI;
2065:    otherwise, it works like VECCUDA or VECHIP etc.

2067:    Logically Collective on Vec

2069:    Input Parameter:
2070: .  x - the vector

2072:    Output Parameters:
2073: +  a - location to put pointer to the array
2074: -  mtype - memory type of the array

2076:    Level: beginner

2078: .seealso: `VecRestoreArrayAndMemType()`, `VecGetArrayReadAndMemType()`, `VecGetArrayWriteAndMemType()`, `VecRestoreArray()`, `VecGetArrayRead()`, `VecGetArrays()`, `VecGetArrayF90()`, `VecGetArrayReadF90()`,
2079:           `VecPlaceArray()`, `VecGetArray2d()`, `VecGetArrayPair()`, `VecRestoreArrayPair()`, `VecGetArrayWrite()`, `VecRestoreArrayWrite()`
2080: @*/
2081: PetscErrorCode VecGetArrayAndMemType(Vec x, PetscScalar **a, PetscMemType *mtype)
2082: {
2087:   VecSetErrorIfLocked(x, 1);
2088:   if (x->ops->getarrayandmemtype) {
2089:     /* VECCUDA, VECKOKKOS etc */
2090:     PetscUseTypeMethod(x, getarrayandmemtype, a, mtype);
2091:   } else {
2092:     /* VECSTANDARD, VECNEST, VECVIENNACL */
2093:     VecGetArray(x, a);
2094:     if (mtype) *mtype = PETSC_MEMTYPE_HOST;
2095:   }
2096:   return 0;
2097: }

2099: /*@C
2100:    VecRestoreArrayAndMemType - Restores a vector after VecGetArrayAndMemType() has been called.

2102:    Logically Collective on Vec

2104:    Input Parameters:
2105: +  x - the vector
2106: -  a - location of pointer to array obtained from VecGetArrayAndMemType()

2108:    Level: beginner

2110: .seealso: `VecGetArrayAndMemType()`, `VecGetArray()`, `VecRestoreArrayRead()`, `VecRestoreArrays()`, `VecRestoreArrayF90()`, `VecRestoreArrayReadF90()`,
2111:           `VecPlaceArray()`, `VecRestoreArray2d()`, `VecGetArrayPair()`, `VecRestoreArrayPair()`
2112: @*/
2113: PetscErrorCode VecRestoreArrayAndMemType(Vec x, PetscScalar **a)
2114: {
2118:   if (x->ops->restorearrayandmemtype) {
2119:     /* VECCUDA, VECKOKKOS etc */
2120:     PetscUseTypeMethod(x, restorearrayandmemtype, a);
2121:   } else {
2122:     /* VECNEST, VECVIENNACL */
2123:     VecRestoreArray(x, a);
2124:   } /* VECSTANDARD does nothing */
2125:   if (a) *a = NULL;
2126:   PetscObjectStateIncrease((PetscObject)x);
2127:   return 0;
2128: }

2130: /*@C
2131:    VecGetArrayReadAndMemType - Like VecGetArrayRead(), but if the input vector is a device vector, it will return a read-only device pointer. The returned pointer is guarenteed to point to up-to-date data. For host vectors, it functions as VecGetArrayRead().

2133:    Not Collective

2135:    Input Parameter:
2136: .  x - the vector

2138:    Output Parameters:
2139: +  a - the array
2140: -  mtype - memory type of the array

2142:    Level: beginner

2144:    Notes:
2145:    The array must be returned using a matching call to VecRestoreArrayReadAndMemType().

2147: .seealso: `VecRestoreArrayReadAndMemType()`, `VecGetArrayAndMemType()`, `VecGetArrayWriteAndMemType()`, `VecGetArray()`, `VecRestoreArray()`, `VecGetArrayPair()`, `VecRestoreArrayPair()`, `VecGetArrayAndMemType()`
2148: @*/
2149: PetscErrorCode VecGetArrayReadAndMemType(Vec x, const PetscScalar **a, PetscMemType *mtype)
2150: {
2155:   if (x->ops->getarrayreadandmemtype) {
2156:     /* VECCUDA/VECHIP though they are also petscnative */
2157:     PetscUseTypeMethod(x, getarrayreadandmemtype, a, mtype);
2158:   } else if (x->ops->getarrayandmemtype) {
2159:     /* VECKOKKOS */
2160:     PetscUseTypeMethod(x, getarrayandmemtype, (PetscScalar **)a, mtype);
2161:   } else {
2162:     VecGetArrayRead(x, a);
2163:     if (mtype) *mtype = PETSC_MEMTYPE_HOST;
2164:   }
2165:   return 0;
2166: }

2168: /*@C
2169:    VecRestoreArrayReadAndMemType - Restore array obtained with VecGetArrayReadAndMemType()

2171:    Not Collective

2173:    Input Parameters:
2174: +  vec - the vector
2175: -  array - the array

2177:    Level: beginner

2179: .seealso: `VecGetArrayReadAndMemType()`, `VecRestoreArrayAndMemType()`, `VecRestoreArrayWriteAndMemType()`, `VecGetArray()`, `VecRestoreArray()`, `VecGetArrayPair()`, `VecRestoreArrayPair()`
2180: @*/
2181: PetscErrorCode VecRestoreArrayReadAndMemType(Vec x, const PetscScalar **a)
2182: {
2186:   if (x->ops->restorearrayreadandmemtype) {
2187:     /* VECCUDA/VECHIP */
2188:     PetscUseTypeMethod(x, restorearrayreadandmemtype, a);
2189:   } else if (!x->petscnative) {
2190:     /* VECNEST */
2191:     VecRestoreArrayRead(x, a);
2192:   }
2193:   if (a) *a = NULL;
2194:   return 0;
2195: }

2197: /*@C
2198:    VecGetArrayWriteAndMemType - Like VecGetArrayWrite(), but if this is a device vector it will aways return
2199:     a device pointer to the device memory that contains this processor's portion of the vector data.

2201:    Not Collective

2203:    Input Parameter:
2204: .  x - the vector

2206:    Output Parameters:
2207: +  a - the array
2208: -  mtype - memory type of the array

2210:    Level: beginner

2212:    Notes:
2213:    The array must be returned using a matching call to VecRestoreArrayWriteAndMemType(), where it will label the device memory as most recent.

2215: .seealso: `VecRestoreArrayWriteAndMemType()`, `VecGetArrayReadAndMemType()`, `VecGetArrayAndMemType()`, `VecGetArray()`, `VecRestoreArray()`, `VecGetArrayPair()`, `VecRestoreArrayPair()`,
2216: @*/
2217: PetscErrorCode VecGetArrayWriteAndMemType(Vec x, PetscScalar **a, PetscMemType *mtype)
2218: {
2221:   VecSetErrorIfLocked(x, 1);
2224:   if (x->ops->getarraywriteandmemtype) {
2225:     /* VECCUDA, VECHIP, VECKOKKOS etc, though they are also petscnative */
2226:     PetscUseTypeMethod(x, getarraywriteandmemtype, a, mtype);
2227:   } else if (x->ops->getarrayandmemtype) {
2228:     VecGetArrayAndMemType(x, a, mtype);
2229:   } else {
2230:     /* VECNEST, VECVIENNACL */
2231:     VecGetArrayWrite(x, a);
2232:     if (mtype) *mtype = PETSC_MEMTYPE_HOST;
2233:   }
2234:   return 0;
2235: }

2237: /*@C
2238:    VecRestoreArrayWriteAndMemType - Restore array obtained with VecGetArrayWriteAndMemType()

2240:    Not Collective

2242:    Input Parameters:
2243: +  vec - the vector
2244: -  array - the array

2246:    Level: beginner

2248: .seealso: `VecGetArrayWriteAndMemType()`, `VecRestoreArrayAndMemType()`, `VecGetArray()`, `VecRestoreArray()`, `VecGetArrayPair()`, `VecRestoreArrayPair()`
2249: @*/
2250: PetscErrorCode VecRestoreArrayWriteAndMemType(Vec x, PetscScalar **a)
2251: {
2254:   VecSetErrorIfLocked(x, 1);
2256:   if (x->ops->restorearraywriteandmemtype) {
2257:     /* VECCUDA/VECHIP */
2258:     PetscMemType PETSC_UNUSED mtype; // since this function doesn't accept a memtype?
2259:     PetscUseTypeMethod(x, restorearraywriteandmemtype, a, &mtype);
2260:   } else if (x->ops->restorearrayandmemtype) {
2261:     VecRestoreArrayAndMemType(x, a);
2262:   } else {
2263:     VecRestoreArray(x, a);
2264:   }
2265:   if (a) *a = NULL;
2266:   return 0;
2267: }

2269: /*@
2270:    VecPlaceArray - Allows one to replace the array in a vector with an
2271:    array provided by the user. This is useful to avoid copying an array
2272:    into a vector.

2274:    Not Collective

2276:    Input Parameters:
2277: +  vec - the vector
2278: -  array - the array

2280:    Notes:
2281:    You can return to the original array with a call to `VecResetArray()`. `vec` does not take
2282:    ownership of `array` in any way. The user must free `array` themselves but be careful not to
2283:    do so before the vector has either been destroyed, had its original array restored with
2284:    `VecResetArray()` or permanently replaced with `VecReplaceArray()`.

2286:    Level: developer

2288: .seealso: `VecGetArray()`, `VecRestoreArray()`, `VecReplaceArray()`, `VecResetArray()`

2290: @*/
2291: PetscErrorCode VecPlaceArray(Vec vec, const PetscScalar array[])
2292: {
2296:   PetscUseTypeMethod(vec, placearray, array);
2297:   PetscObjectStateIncrease((PetscObject)vec);
2298:   return 0;
2299: }

2301: /*@C
2302:    VecReplaceArray - Allows one to replace the array in a vector with an
2303:    array provided by the user. This is useful to avoid copying an array
2304:    into a vector.

2306:    Not Collective

2308:    Input Parameters:
2309: +  vec - the vector
2310: -  array - the array

2312:    Notes:
2313:    This permanently replaces the array and frees the memory associated
2314:    with the old array.

2316:    The memory passed in MUST be obtained with PetscMalloc() and CANNOT be
2317:    freed by the user. It will be freed when the vector is destroyed.

2319:    Not supported from Fortran

2321:    Level: developer

2323: .seealso: `VecGetArray()`, `VecRestoreArray()`, `VecPlaceArray()`, `VecResetArray()`

2325: @*/
2326: PetscErrorCode VecReplaceArray(Vec vec, const PetscScalar array[])
2327: {
2330:   PetscUseTypeMethod(vec, replacearray, array);
2331:   PetscObjectStateIncrease((PetscObject)vec);
2332:   return 0;
2333: }

2335: /*@C
2336:    VecCUDAGetArray - Provides access to the CUDA buffer inside a vector.

2338:    This function has semantics similar to VecGetArray():  the pointer
2339:    returned by this function points to a consistent view of the vector
2340:    data.  This may involve a copy operation of data from the host to the
2341:    device if the data on the device is out of date.  If the device
2342:    memory hasn't been allocated previously it will be allocated as part
2343:    of this function call.  VecCUDAGetArray() assumes that
2344:    the user will modify the vector data.  This is similar to
2345:    intent(inout) in fortran.

2347:    The CUDA device pointer has to be released by calling
2348:    VecCUDARestoreArray().  Upon restoring the vector data
2349:    the data on the host will be marked as out of date.  A subsequent
2350:    access of the host data will thus incur a data transfer from the
2351:    device to the host.

2353:    Input Parameter:
2354: .  v - the vector

2356:    Output Parameter:
2357: .  a - the CUDA device pointer

2359:    Fortran note:
2360:    This function is not currently available from Fortran.

2362:    Level: intermediate

2364: .seealso: `VecCUDARestoreArray()`, `VecCUDAGetArrayRead()`, `VecCUDAGetArrayWrite()`, `VecGetArray()`, `VecGetArrayRead()`
2365: @*/
2366: PETSC_EXTERN PetscErrorCode VecCUDAGetArray(Vec v, PetscScalar **a)
2367: {
2369: #if defined(PETSC_HAVE_CUDA)
2370:   {
2371:     VecCUDACopyToGPU(v);
2372:     *a = ((Vec_CUDA *)v->spptr)->GPUarray;
2373:   }
2374: #endif
2375:   return 0;
2376: }

2378: /*@C
2379:    VecCUDARestoreArray - Restore a CUDA device pointer previously acquired with VecCUDAGetArray().

2381:    This marks the host data as out of date.  Subsequent access to the
2382:    vector data on the host side with for instance VecGetArray() incurs a
2383:    data transfer.

2385:    Input Parameters:
2386: +  v - the vector
2387: -  a - the CUDA device pointer.  This pointer is invalid after
2388:        VecCUDARestoreArray() returns.

2390:    Fortran note:
2391:    This function is not currently available from Fortran.

2393:    Level: intermediate

2395: .seealso: `VecCUDAGetArray()`, `VecCUDAGetArrayRead()`, `VecCUDAGetArrayWrite()`, `VecGetArray()`, `VecRestoreArray()`, `VecGetArrayRead()`
2396: @*/
2397: PETSC_EXTERN PetscErrorCode VecCUDARestoreArray(Vec v, PetscScalar **a)
2398: {
2400: #if defined(PETSC_HAVE_CUDA)
2401:   v->offloadmask = PETSC_OFFLOAD_GPU;
2402: #endif
2403:   PetscObjectStateIncrease((PetscObject)v);
2404:   return 0;
2405: }

2407: /*@C
2408:    VecCUDAGetArrayRead - Provides read access to the CUDA buffer inside a vector.

2410:    This function is analogous to VecGetArrayRead():  The pointer
2411:    returned by this function points to a consistent view of the vector
2412:    data.  This may involve a copy operation of data from the host to the
2413:    device if the data on the device is out of date.  If the device
2414:    memory hasn't been allocated previously it will be allocated as part
2415:    of this function call.  VecCUDAGetArrayRead() assumes that the
2416:    user will not modify the vector data.  This is analgogous to
2417:    intent(in) in Fortran.

2419:    The CUDA device pointer has to be released by calling
2420:    VecCUDARestoreArrayRead().  If the data on the host side was
2421:    previously up to date it will remain so, i.e. data on both the device
2422:    and the host is up to date.  Accessing data on the host side does not
2423:    incur a device to host data transfer.

2425:    Input Parameter:
2426: .  v - the vector

2428:    Output Parameter:
2429: .  a - the CUDA pointer.

2431:    Fortran note:
2432:    This function is not currently available from Fortran.

2434:    Level: intermediate

2436: .seealso: `VecCUDARestoreArrayRead()`, `VecCUDAGetArray()`, `VecCUDAGetArrayWrite()`, `VecGetArray()`, `VecGetArrayRead()`
2437: @*/
2438: PETSC_EXTERN PetscErrorCode VecCUDAGetArrayRead(Vec v, const PetscScalar **a)
2439: {
2440:   VecCUDAGetArray(v, (PetscScalar **)a);
2441:   return 0;
2442: }

2444: /*@C
2445:    VecCUDARestoreArrayRead - Restore a CUDA device pointer previously acquired with VecCUDAGetArrayRead().

2447:    If the data on the host side was previously up to date it will remain
2448:    so, i.e. data on both the device and the host is up to date.
2449:    Accessing data on the host side e.g. with VecGetArray() does not
2450:    incur a device to host data transfer.

2452:    Input Parameters:
2453: +  v - the vector
2454: -  a - the CUDA device pointer.  This pointer is invalid after
2455:        VecCUDARestoreArrayRead() returns.

2457:    Fortran note:
2458:    This function is not currently available from Fortran.

2460:    Level: intermediate

2462: .seealso: `VecCUDAGetArrayRead()`, `VecCUDAGetArrayWrite()`, `VecCUDAGetArray()`, `VecGetArray()`, `VecRestoreArray()`, `VecGetArrayRead()`
2463: @*/
2464: PETSC_EXTERN PetscErrorCode VecCUDARestoreArrayRead(Vec v, const PetscScalar **a)
2465: {
2467:   *a = NULL;
2468:   return 0;
2469: }

2471: /*@C
2472:    VecCUDAGetArrayWrite - Provides write access to the CUDA buffer inside a vector.

2474:    The data pointed to by the device pointer is uninitialized.  The user
2475:    may not read from this data.  Furthermore, the entire array needs to
2476:    be filled by the user to obtain well-defined behaviour.  The device
2477:    memory will be allocated by this function if it hasn't been allocated
2478:    previously.  This is analogous to intent(out) in Fortran.

2480:    The device pointer needs to be released with
2481:    VecCUDARestoreArrayWrite().  When the pointer is released the
2482:    host data of the vector is marked as out of data.  Subsequent access
2483:    of the host data with e.g. VecGetArray() incurs a device to host data
2484:    transfer.

2486:    Input Parameter:
2487: .  v - the vector

2489:    Output Parameter:
2490: .  a - the CUDA pointer

2492:    Fortran note:
2493:    This function is not currently available from Fortran.

2495:    Level: advanced

2497: .seealso: `VecCUDARestoreArrayWrite()`, `VecCUDAGetArray()`, `VecCUDAGetArrayRead()`, `VecCUDAGetArrayWrite()`, `VecGetArray()`, `VecGetArrayRead()`
2498: @*/
2499: PETSC_EXTERN PetscErrorCode VecCUDAGetArrayWrite(Vec v, PetscScalar **a)
2500: {
2502: #if defined(PETSC_HAVE_CUDA)
2503:   {
2504:     VecCUDAAllocateCheck(v);
2505:     *a = ((Vec_CUDA *)v->spptr)->GPUarray;
2506:   }
2507: #endif
2508:   return 0;
2509: }

2511: /*@C
2512:    VecCUDARestoreArrayWrite - Restore a CUDA device pointer previously acquired with VecCUDAGetArrayWrite().

2514:    Data on the host will be marked as out of date.  Subsequent access of
2515:    the data on the host side e.g. with VecGetArray() will incur a device
2516:    to host data transfer.

2518:    Input Parameters:
2519: +  v - the vector
2520: -  a - the CUDA device pointer.  This pointer is invalid after
2521:        VecCUDARestoreArrayWrite() returns.

2523:    Fortran note:
2524:    This function is not currently available from Fortran.

2526:    Level: intermediate

2528: .seealso: `VecCUDAGetArrayWrite()`, `VecCUDAGetArray()`, `VecCUDAGetArrayRead()`, `VecCUDAGetArrayWrite()`, `VecGetArray()`, `VecRestoreArray()`, `VecGetArrayRead()`
2529: @*/
2530: PETSC_EXTERN PetscErrorCode VecCUDARestoreArrayWrite(Vec v, PetscScalar **a)
2531: {
2533: #if defined(PETSC_HAVE_CUDA)
2534:   v->offloadmask = PETSC_OFFLOAD_GPU;
2535:   if (a) *a = NULL;
2536: #endif
2537:   PetscObjectStateIncrease((PetscObject)v);
2538:   return 0;
2539: }

2541: /*@C
2542:    VecCUDAPlaceArray - Allows one to replace the GPU array in a vector with a
2543:    GPU array provided by the user. This is useful to avoid copying an
2544:    array into a vector.

2546:    Not Collective

2548:    Input Parameters:
2549: +  vec - the vector
2550: -  array - the GPU array

2552:    Notes:
2553:    You can return to the original GPU array with a call to VecCUDAResetArray()
2554:    It is not possible to use VecCUDAPlaceArray() and VecPlaceArray() at the
2555:    same time on the same vector.

2557:    `vec` does not take ownership of `array` in any way. The user must free `array` themselves
2558:    but be careful not to do so before the vector has either been destroyed, had its original
2559:    array restored with `VecCUDAResetArray()` or permanently replaced with
2560:    `VecCUDAReplaceArray()`.

2562:    Level: developer

2564: .seealso: `VecPlaceArray()`, `VecGetArray()`, `VecRestoreArray()`, `VecReplaceArray()`, `VecResetArray()`, `VecCUDAResetArray()`, `VecCUDAReplaceArray()`

2566: @*/
2567: PetscErrorCode VecCUDAPlaceArray(Vec vin, const PetscScalar a[])
2568: {
2570: #if defined(PETSC_HAVE_CUDA)
2571:   VecCUDACopyToGPU(vin);
2573:   ((Vec_Seq *)vin->data)->unplacedarray = (PetscScalar *)((Vec_CUDA *)vin->spptr)->GPUarray; /* save previous GPU array so reset can bring it back */
2574:   ((Vec_CUDA *)vin->spptr)->GPUarray    = (PetscScalar *)a;
2575:   vin->offloadmask                      = PETSC_OFFLOAD_GPU;
2576: #endif
2577:   PetscObjectStateIncrease((PetscObject)vin);
2578:   return 0;
2579: }

2581: /*@C
2582:    VecCUDAReplaceArray - Allows one to replace the GPU array in a vector
2583:    with a GPU array provided by the user. This is useful to avoid copying
2584:    a GPU array into a vector.

2586:    Not Collective

2588:    Input Parameters:
2589: +  vec - the vector
2590: -  array - the GPU array

2592:    Notes:
2593:    This permanently replaces the GPU array and frees the memory associated
2594:    with the old GPU array.

2596:    The memory passed in CANNOT be freed by the user. It will be freed
2597:    when the vector is destroyed.

2599:    Not supported from Fortran

2601:    Level: developer

2603: .seealso: `VecGetArray()`, `VecRestoreArray()`, `VecPlaceArray()`, `VecResetArray()`, `VecCUDAResetArray()`, `VecCUDAPlaceArray()`, `VecReplaceArray()`

2605: @*/
2606: PetscErrorCode VecCUDAReplaceArray(Vec vin, const PetscScalar a[])
2607: {
2608: #if defined(PETSC_HAVE_CUDA)
2609: #endif

2612: #if defined(PETSC_HAVE_CUDA)
2613:   if (((Vec_CUDA *)vin->spptr)->GPUarray_allocated) cudaFree(((Vec_CUDA *)vin->spptr)->GPUarray_allocated);
2614:   ((Vec_CUDA *)vin->spptr)->GPUarray_allocated = ((Vec_CUDA *)vin->spptr)->GPUarray = (PetscScalar *)a;
2615:   vin->offloadmask                                                                  = PETSC_OFFLOAD_GPU;
2616: #endif
2617:   PetscObjectStateIncrease((PetscObject)vin);
2618:   return 0;
2619: }

2621: /*@C
2622:    VecCUDAResetArray - Resets a vector to use its default memory. Call this
2623:    after the use of VecCUDAPlaceArray().

2625:    Not Collective

2627:    Input Parameters:
2628: .  vec - the vector

2630:    Level: developer

2632: .seealso: `VecGetArray()`, `VecRestoreArray()`, `VecReplaceArray()`, `VecPlaceArray()`, `VecResetArray()`, `VecCUDAPlaceArray()`, `VecCUDAReplaceArray()`

2634: @*/
2635: PetscErrorCode VecCUDAResetArray(Vec vin)
2636: {
2638: #if defined(PETSC_HAVE_CUDA)
2639:   VecCUDACopyToGPU(vin);
2640:   ((Vec_CUDA *)vin->spptr)->GPUarray    = (PetscScalar *)((Vec_Seq *)vin->data)->unplacedarray;
2641:   ((Vec_Seq *)vin->data)->unplacedarray = 0;
2642:   vin->offloadmask                      = PETSC_OFFLOAD_GPU;
2643: #endif
2644:   PetscObjectStateIncrease((PetscObject)vin);
2645:   return 0;
2646: }

2648: /*@C
2649:    VecHIPGetArray - Provides access to the HIP buffer inside a vector.

2651:    This function has semantics similar to VecGetArray():  the pointer
2652:    returned by this function points to a consistent view of the vector
2653:    data.  This may involve a copy operation of data from the host to the
2654:    device if the data on the device is out of date.  If the device
2655:    memory hasn't been allocated previously it will be allocated as part
2656:    of this function call.  VecHIPGetArray() assumes that
2657:    the user will modify the vector data.  This is similar to
2658:    intent(inout) in fortran.

2660:    The HIP device pointer has to be released by calling
2661:    VecHIPRestoreArray().  Upon restoring the vector data
2662:    the data on the host will be marked as out of date.  A subsequent
2663:    access of the host data will thus incur a data transfer from the
2664:    device to the host.

2666:    Input Parameter:
2667: .  v - the vector

2669:    Output Parameter:
2670: .  a - the HIP device pointer

2672:    Fortran note:
2673:    This function is not currently available from Fortran.

2675:    Level: intermediate

2677: .seealso: `VecHIPRestoreArray()`, `VecHIPGetArrayRead()`, `VecHIPGetArrayWrite()`, `VecGetArray()`, `VecGetArrayRead()`
2678: @*/
2679: PETSC_EXTERN PetscErrorCode VecHIPGetArray(Vec v, PetscScalar **a)
2680: {
2682: #if defined(PETSC_HAVE_HIP)
2683:   *a = 0;
2684:   VecHIPCopyToGPU(v);
2685:   *a = ((Vec_HIP *)v->spptr)->GPUarray;
2686: #endif
2687:   return 0;
2688: }

2690: /*@C
2691:    VecHIPRestoreArray - Restore a HIP device pointer previously acquired with VecHIPGetArray().

2693:    This marks the host data as out of date.  Subsequent access to the
2694:    vector data on the host side with for instance VecGetArray() incurs a
2695:    data transfer.

2697:    Input Parameters:
2698: +  v - the vector
2699: -  a - the HIP device pointer.  This pointer is invalid after
2700:        VecHIPRestoreArray() returns.

2702:    Fortran note:
2703:    This function is not currently available from Fortran.

2705:    Level: intermediate

2707: .seealso: `VecHIPGetArray()`, `VecHIPGetArrayRead()`, `VecHIPGetArrayWrite()`, `VecGetArray()`, `VecRestoreArray()`, `VecGetArrayRead()`
2708: @*/
2709: PETSC_EXTERN PetscErrorCode VecHIPRestoreArray(Vec v, PetscScalar **a)
2710: {
2712: #if defined(PETSC_HAVE_HIP)
2713:   v->offloadmask = PETSC_OFFLOAD_GPU;
2714: #endif

2716:   PetscObjectStateIncrease((PetscObject)v);
2717:   return 0;
2718: }

2720: /*@C
2721:    VecHIPGetArrayRead - Provides read access to the HIP buffer inside a vector.

2723:    This function is analogous to VecGetArrayRead():  The pointer
2724:    returned by this function points to a consistent view of the vector
2725:    data.  This may involve a copy operation of data from the host to the
2726:    device if the data on the device is out of date.  If the device
2727:    memory hasn't been allocated previously it will be allocated as part
2728:    of this function call.  VecHIPGetArrayRead() assumes that the
2729:    user will not modify the vector data.  This is analgogous to
2730:    intent(in) in Fortran.

2732:    The HIP device pointer has to be released by calling
2733:    VecHIPRestoreArrayRead().  If the data on the host side was
2734:    previously up to date it will remain so, i.e. data on both the device
2735:    and the host is up to date.  Accessing data on the host side does not
2736:    incur a device to host data transfer.

2738:    Input Parameter:
2739: .  v - the vector

2741:    Output Parameter:
2742: .  a - the HIP pointer.

2744:    Fortran note:
2745:    This function is not currently available from Fortran.

2747:    Level: intermediate

2749: .seealso: `VecHIPRestoreArrayRead()`, `VecHIPGetArray()`, `VecHIPGetArrayWrite()`, `VecGetArray()`, `VecGetArrayRead()`
2750: @*/
2751: PETSC_EXTERN PetscErrorCode VecHIPGetArrayRead(Vec v, const PetscScalar **a)
2752: {
2754: #if defined(PETSC_HAVE_HIP)
2755:   *a = 0;
2756:   VecHIPCopyToGPU(v);
2757:   *a = ((Vec_HIP *)v->spptr)->GPUarray;
2758: #endif
2759:   return 0;
2760: }

2762: /*@C
2763:    VecHIPRestoreArrayRead - Restore a HIP device pointer previously acquired with VecHIPGetArrayRead().

2765:    If the data on the host side was previously up to date it will remain
2766:    so, i.e. data on both the device and the host is up to date.
2767:    Accessing data on the host side e.g. with VecGetArray() does not
2768:    incur a device to host data transfer.

2770:    Input Parameters:
2771: +  v - the vector
2772: -  a - the HIP device pointer.  This pointer is invalid after
2773:        VecHIPRestoreArrayRead() returns.

2775:    Fortran note:
2776:    This function is not currently available from Fortran.

2778:    Level: intermediate

2780: .seealso: `VecHIPGetArrayRead()`, `VecHIPGetArrayWrite()`, `VecHIPGetArray()`, `VecGetArray()`, `VecRestoreArray()`, `VecGetArrayRead()`
2781: @*/
2782: PETSC_EXTERN PetscErrorCode VecHIPRestoreArrayRead(Vec v, const PetscScalar **a)
2783: {
2785:   *a = NULL;
2786:   return 0;
2787: }

2789: /*@C
2790:    VecHIPGetArrayWrite - Provides write access to the HIP buffer inside a vector.

2792:    The data pointed to by the device pointer is uninitialized.  The user
2793:    may not read from this data.  Furthermore, the entire array needs to
2794:    be filled by the user to obtain well-defined behaviour.  The device
2795:    memory will be allocated by this function if it hasn't been allocated
2796:    previously.  This is analogous to intent(out) in Fortran.

2798:    The device pointer needs to be released with
2799:    VecHIPRestoreArrayWrite().  When the pointer is released the
2800:    host data of the vector is marked as out of data.  Subsequent access
2801:    of the host data with e.g. VecGetArray() incurs a device to host data
2802:    transfer.

2804:    Input Parameter:
2805: .  v - the vector

2807:    Output Parameter:
2808: .  a - the HIP pointer

2810:    Fortran note:
2811:    This function is not currently available from Fortran.

2813:    Level: advanced

2815: .seealso: `VecHIPRestoreArrayWrite()`, `VecHIPGetArray()`, `VecHIPGetArrayRead()`, `VecHIPGetArrayWrite()`, `VecGetArray()`, `VecGetArrayRead()`
2816: @*/
2817: PETSC_EXTERN PetscErrorCode VecHIPGetArrayWrite(Vec v, PetscScalar **a)
2818: {
2820: #if defined(PETSC_HAVE_HIP)
2821:   *a = 0;
2822:   VecHIPAllocateCheck(v);
2823:   *a = ((Vec_HIP *)v->spptr)->GPUarray;
2824: #endif
2825:   return 0;
2826: }

2828: /*@C
2829:    VecHIPRestoreArrayWrite - Restore a HIP device pointer previously acquired with VecHIPGetArrayWrite().

2831:    Data on the host will be marked as out of date.  Subsequent access of
2832:    the data on the host side e.g. with VecGetArray() will incur a device
2833:    to host data transfer.

2835:    Input Parameters:
2836: +  v - the vector
2837: -  a - the HIP device pointer.  This pointer is invalid after
2838:        VecHIPRestoreArrayWrite() returns.

2840:    Fortran note:
2841:    This function is not currently available from Fortran.

2843:    Level: intermediate

2845: .seealso: `VecHIPGetArrayWrite()`, `VecHIPGetArray()`, `VecHIPGetArrayRead()`, `VecHIPGetArrayWrite()`, `VecGetArray()`, `VecRestoreArray()`, `VecGetArrayRead()`
2846: @*/
2847: PETSC_EXTERN PetscErrorCode VecHIPRestoreArrayWrite(Vec v, PetscScalar **a)
2848: {
2850: #if defined(PETSC_HAVE_HIP)
2851:   v->offloadmask = PETSC_OFFLOAD_GPU;
2852: #endif

2854:   PetscObjectStateIncrease((PetscObject)v);
2855:   return 0;
2856: }

2858: /*@C
2859:    VecHIPPlaceArray - Allows one to replace the GPU array in a vector with a
2860:    GPU array provided by the user. This is useful to avoid copying an
2861:    array into a vector.

2863:    Not Collective

2865:    Input Parameters:
2866: +  vec - the vector
2867: -  array - the GPU array

2869:    Notes:
2870:    You can return to the original GPU array with a call to VecHIPResetArray()
2871:    It is not possible to use VecHIPPlaceArray() and VecPlaceArray() at the
2872:    same time on the same vector.

2874:    `vec` does not take ownership of `array` in any way. The user must free `array` themselves
2875:    but be careful not to do so before the vector has either been destroyed, had its original
2876:    array restored with `VecHIPResetArray()` or permanently replaced with
2877:    `VecHIPReplaceArray()`.

2879:    Level: developer

2881: .seealso: `VecPlaceArray()`, `VecGetArray()`, `VecRestoreArray()`, `VecReplaceArray()`, `VecResetArray()`, `VecHIPResetArray()`, `VecHIPReplaceArray()`

2883: @*/
2884: PetscErrorCode VecHIPPlaceArray(Vec vin, const PetscScalar a[])
2885: {
2887: #if defined(PETSC_HAVE_HIP)
2888:   VecHIPCopyToGPU(vin);
2890:   ((Vec_Seq *)vin->data)->unplacedarray = (PetscScalar *)((Vec_HIP *)vin->spptr)->GPUarray; /* save previous GPU array so reset can bring it back */
2891:   ((Vec_HIP *)vin->spptr)->GPUarray     = (PetscScalar *)a;
2892:   vin->offloadmask                      = PETSC_OFFLOAD_GPU;
2893: #endif
2894:   PetscObjectStateIncrease((PetscObject)vin);
2895:   return 0;
2896: }

2898: /*@C
2899:    VecHIPReplaceArray - Allows one to replace the GPU array in a vector
2900:    with a GPU array provided by the user. This is useful to avoid copying
2901:    a GPU array into a vector.

2903:    Not Collective

2905:    Input Parameters:
2906: +  vec - the vector
2907: -  array - the GPU array

2909:    Notes:
2910:    This permanently replaces the GPU array and frees the memory associated
2911:    with the old GPU array.

2913:    The memory passed in CANNOT be freed by the user. It will be freed
2914:    when the vector is destroyed.

2916:    Not supported from Fortran

2918:    Level: developer

2920: .seealso: `VecGetArray()`, `VecRestoreArray()`, `VecPlaceArray()`, `VecResetArray()`, `VecHIPResetArray()`, `VecHIPPlaceArray()`, `VecReplaceArray()`

2922: @*/
2923: PetscErrorCode VecHIPReplaceArray(Vec vin, const PetscScalar a[])
2924: {
2926: #if defined(PETSC_HAVE_HIP)
2927:   hipFree(((Vec_HIP *)vin->spptr)->GPUarray);
2928:   ((Vec_HIP *)vin->spptr)->GPUarray = (PetscScalar *)a;
2929:   vin->offloadmask                  = PETSC_OFFLOAD_GPU;
2930: #endif
2931:   PetscObjectStateIncrease((PetscObject)vin);
2932:   return 0;
2933: }

2935: /*@C
2936:    VecHIPResetArray - Resets a vector to use its default memory. Call this
2937:    after the use of VecHIPPlaceArray().

2939:    Not Collective

2941:    Input Parameters:
2942: .  vec - the vector

2944:    Level: developer

2946: .seealso: `VecGetArray()`, `VecRestoreArray()`, `VecReplaceArray()`, `VecPlaceArray()`, `VecResetArray()`, `VecHIPPlaceArray()`, `VecHIPReplaceArray()`

2948: @*/
2949: PetscErrorCode VecHIPResetArray(Vec vin)
2950: {
2952: #if defined(PETSC_HAVE_HIP)
2953:   VecHIPCopyToGPU(vin);
2954:   ((Vec_HIP *)vin->spptr)->GPUarray     = (PetscScalar *)((Vec_Seq *)vin->data)->unplacedarray;
2955:   ((Vec_Seq *)vin->data)->unplacedarray = 0;
2956:   vin->offloadmask                      = PETSC_OFFLOAD_GPU;
2957: #endif
2958:   PetscObjectStateIncrease((PetscObject)vin);
2959:   return 0;
2960: }

2962: /*MC
2963:     VecDuplicateVecsF90 - Creates several vectors of the same type as an existing vector
2964:     and makes them accessible via a Fortran90 pointer.

2966:     Synopsis:
2967:     VecDuplicateVecsF90(Vec x,PetscInt n,{Vec, pointer :: y(:)},integer ierr)

2969:     Collective on Vec

2971:     Input Parameters:
2972: +   x - a vector to mimic
2973: -   n - the number of vectors to obtain

2975:     Output Parameters:
2976: +   y - Fortran90 pointer to the array of vectors
2977: -   ierr - error code

2979:     Example of Usage:
2980: .vb
2981: #include <petsc/finclude/petscvec.h>
2982:     use petscvec

2984:     Vec x
2985:     Vec, pointer :: y(:)
2986:     ....
2987:     call VecDuplicateVecsF90(x,2,y,ierr)
2988:     call VecSet(y(2),alpha,ierr)
2989:     call VecSet(y(2),alpha,ierr)
2990:     ....
2991:     call VecDestroyVecsF90(2,y,ierr)
2992: .ve

2994:     Notes:
2995:     Not yet supported for all F90 compilers

2997:     Use VecDestroyVecsF90() to free the space.

2999:     Level: beginner

3001: .seealso: `VecDestroyVecsF90()`, `VecDuplicateVecs()`

3003: M*/

3005: /*MC
3006:     VecRestoreArrayF90 - Restores a vector to a usable state after a call to
3007:     VecGetArrayF90().

3009:     Synopsis:
3010:     VecRestoreArrayF90(Vec x,{Scalar, pointer :: xx_v(:)},integer ierr)

3012:     Logically Collective on Vec

3014:     Input Parameters:
3015: +   x - vector
3016: -   xx_v - the Fortran90 pointer to the array

3018:     Output Parameter:
3019: .   ierr - error code

3021:     Example of Usage:
3022: .vb
3023: #include <petsc/finclude/petscvec.h>
3024:     use petscvec

3026:     PetscScalar, pointer :: xx_v(:)
3027:     ....
3028:     call VecGetArrayF90(x,xx_v,ierr)
3029:     xx_v(3) = a
3030:     call VecRestoreArrayF90(x,xx_v,ierr)
3031: .ve

3033:     Level: beginner

3035: .seealso: `VecGetArrayF90()`, `VecGetArray()`, `VecRestoreArray()`, `VecRestoreArrayReadF90()`

3037: M*/

3039: /*MC
3040:     VecDestroyVecsF90 - Frees a block of vectors obtained with VecDuplicateVecsF90().

3042:     Synopsis:
3043:     VecDestroyVecsF90(PetscInt n,{Vec, pointer :: x(:)},PetscErrorCode ierr)

3045:     Collective on Vec

3047:     Input Parameters:
3048: +   n - the number of vectors previously obtained
3049: -   x - pointer to array of vector pointers

3051:     Output Parameter:
3052: .   ierr - error code

3054:     Notes:
3055:     Not yet supported for all F90 compilers

3057:     Level: beginner

3059: .seealso: `VecDestroyVecs()`, `VecDuplicateVecsF90()`

3061: M*/

3063: /*MC
3064:     VecGetArrayF90 - Accesses a vector array from Fortran90. For default PETSc
3065:     vectors, VecGetArrayF90() returns a pointer to the local data array. Otherwise,
3066:     this routine is implementation dependent. You MUST call VecRestoreArrayF90()
3067:     when you no longer need access to the array.

3069:     Synopsis:
3070:     VecGetArrayF90(Vec x,{Scalar, pointer :: xx_v(:)},integer ierr)

3072:     Logically Collective on Vec

3074:     Input Parameter:
3075: .   x - vector

3077:     Output Parameters:
3078: +   xx_v - the Fortran90 pointer to the array
3079: -   ierr - error code

3081:     Example of Usage:
3082: .vb
3083: #include <petsc/finclude/petscvec.h>
3084:     use petscvec

3086:     PetscScalar, pointer :: xx_v(:)
3087:     ....
3088:     call VecGetArrayF90(x,xx_v,ierr)
3089:     xx_v(3) = a
3090:     call VecRestoreArrayF90(x,xx_v,ierr)
3091: .ve

3093:     If you ONLY intend to read entries from the array and not change any entries you should use VecGetArrayReadF90().

3095:     Level: beginner

3097: .seealso: `VecRestoreArrayF90()`, `VecGetArray()`, `VecRestoreArray()`, `VecGetArrayReadF90()`

3099: M*/

3101: /*MC
3102:     VecGetArrayReadF90 - Accesses a read only array from Fortran90. For default PETSc
3103:     vectors, VecGetArrayF90() returns a pointer to the local data array. Otherwise,
3104:     this routine is implementation dependent. You MUST call VecRestoreArrayReadF90()
3105:     when you no longer need access to the array.

3107:     Synopsis:
3108:     VecGetArrayReadF90(Vec x,{Scalar, pointer :: xx_v(:)},integer ierr)

3110:     Logically Collective on Vec

3112:     Input Parameter:
3113: .   x - vector

3115:     Output Parameters:
3116: +   xx_v - the Fortran90 pointer to the array
3117: -   ierr - error code

3119:     Example of Usage:
3120: .vb
3121: #include <petsc/finclude/petscvec.h>
3122:     use petscvec

3124:     PetscScalar, pointer :: xx_v(:)
3125:     ....
3126:     call VecGetArrayReadF90(x,xx_v,ierr)
3127:     a = xx_v(3)
3128:     call VecRestoreArrayReadF90(x,xx_v,ierr)
3129: .ve

3131:     If you intend to write entries into the array you must use VecGetArrayF90().

3133:     Level: beginner

3135: .seealso: `VecRestoreArrayReadF90()`, `VecGetArray()`, `VecRestoreArray()`, `VecGetArrayRead()`, `VecRestoreArrayRead()`, `VecGetArrayF90()`

3137: M*/

3139: /*MC
3140:     VecRestoreArrayReadF90 - Restores a readonly vector to a usable state after a call to
3141:     VecGetArrayReadF90().

3143:     Synopsis:
3144:     VecRestoreArrayReadF90(Vec x,{Scalar, pointer :: xx_v(:)},integer ierr)

3146:     Logically Collective on Vec

3148:     Input Parameters:
3149: +   x - vector
3150: -   xx_v - the Fortran90 pointer to the array

3152:     Output Parameter:
3153: .   ierr - error code

3155:     Example of Usage:
3156: .vb
3157: #include <petsc/finclude/petscvec.h>
3158:     use petscvec

3160:     PetscScalar, pointer :: xx_v(:)
3161:     ....
3162:     call VecGetArrayReadF90(x,xx_v,ierr)
3163:     a = xx_v(3)
3164:     call VecRestoreArrayReadF90(x,xx_v,ierr)
3165: .ve

3167:     Level: beginner

3169: .seealso: `VecGetArrayReadF90()`, `VecGetArray()`, `VecRestoreArray()`, `VecGetArrayRead()`, `VecRestoreArrayRead()`, `VecRestoreArrayF90()`

3171: M*/

3173: /*@C
3174:    VecGetArray2d - Returns a pointer to a 2d contiguous array that contains this
3175:    processor's portion of the vector data.  You MUST call VecRestoreArray2d()
3176:    when you no longer need access to the array.

3178:    Logically Collective

3180:    Input Parameters:
3181: +  x - the vector
3182: .  m - first dimension of two dimensional array
3183: .  n - second dimension of two dimensional array
3184: .  mstart - first index you will use in first coordinate direction (often 0)
3185: -  nstart - first index in the second coordinate direction (often 0)

3187:    Output Parameter:
3188: .  a - location to put pointer to the array

3190:    Level: developer

3192:   Notes:
3193:    For a vector obtained from DMCreateLocalVector() mstart and nstart are likely
3194:    obtained from the corner indices obtained from DMDAGetGhostCorners() while for
3195:    DMCreateGlobalVector() they are the corner indices from DMDAGetCorners(). In both cases
3196:    the arguments from DMDAGet[Ghost]Corners() are reversed in the call to VecGetArray2d().

3198:    For standard PETSc vectors this is an inexpensive call; it does not copy the vector values.

3200: .seealso: `VecGetArray()`, `VecRestoreArray()`, `VecGetArrays()`, `VecGetArrayF90()`, `VecPlaceArray()`,
3201:           `VecRestoreArray2d()`, `DMDAVecGetArray()`, `DMDAVecRestoreArray()`, `VecGetArray3d()`, `VecRestoreArray3d()`,
3202:           `VecGetArray1d()`, `VecRestoreArray1d()`, `VecGetArray4d()`, `VecRestoreArray4d()`
3203: @*/
3204: PetscErrorCode VecGetArray2d(Vec x, PetscInt m, PetscInt n, PetscInt mstart, PetscInt nstart, PetscScalar **a[])
3205: {
3206:   PetscInt     i, N;
3207:   PetscScalar *aa;

3212:   VecGetLocalSize(x, &N);
3214:   VecGetArray(x, &aa);

3216:   PetscMalloc1(m, a);
3217:   for (i = 0; i < m; i++) (*a)[i] = aa + i * n - nstart;
3218:   *a -= mstart;
3219:   return 0;
3220: }

3222: /*@C
3223:    VecGetArray2dWrite - Returns a pointer to a 2d contiguous array that will contain this
3224:    processor's portion of the vector data.  You MUST call VecRestoreArray2dWrite()
3225:    when you no longer need access to the array.

3227:    Logically Collective

3229:    Input Parameters:
3230: +  x - the vector
3231: .  m - first dimension of two dimensional array
3232: .  n - second dimension of two dimensional array
3233: .  mstart - first index you will use in first coordinate direction (often 0)
3234: -  nstart - first index in the second coordinate direction (often 0)

3236:    Output Parameter:
3237: .  a - location to put pointer to the array

3239:    Level: developer

3241:   Notes:
3242:    For a vector obtained from DMCreateLocalVector() mstart and nstart are likely
3243:    obtained from the corner indices obtained from DMDAGetGhostCorners() while for
3244:    DMCreateGlobalVector() they are the corner indices from DMDAGetCorners(). In both cases
3245:    the arguments from DMDAGet[Ghost]Corners() are reversed in the call to VecGetArray2d().

3247:    For standard PETSc vectors this is an inexpensive call; it does not copy the vector values.

3249: .seealso: `VecGetArray()`, `VecRestoreArray()`, `VecGetArrays()`, `VecGetArrayF90()`, `VecPlaceArray()`,
3250:           `VecRestoreArray2d()`, `DMDAVecGetArray()`, `DMDAVecRestoreArray()`, `VecGetArray3d()`, `VecRestoreArray3d()`,
3251:           `VecGetArray1d()`, `VecRestoreArray1d()`, `VecGetArray4d()`, `VecRestoreArray4d()`
3252: @*/
3253: PetscErrorCode VecGetArray2dWrite(Vec x, PetscInt m, PetscInt n, PetscInt mstart, PetscInt nstart, PetscScalar **a[])
3254: {
3255:   PetscInt     i, N;
3256:   PetscScalar *aa;

3261:   VecGetLocalSize(x, &N);
3263:   VecGetArrayWrite(x, &aa);

3265:   PetscMalloc1(m, a);
3266:   for (i = 0; i < m; i++) (*a)[i] = aa + i * n - nstart;
3267:   *a -= mstart;
3268:   return 0;
3269: }

3271: /*@C
3272:    VecRestoreArray2d - Restores a vector after VecGetArray2d() has been called.

3274:    Logically Collective

3276:    Input Parameters:
3277: +  x - the vector
3278: .  m - first dimension of two dimensional array
3279: .  n - second dimension of the two dimensional array
3280: .  mstart - first index you will use in first coordinate direction (often 0)
3281: .  nstart - first index in the second coordinate direction (often 0)
3282: -  a - location of pointer to array obtained from VecGetArray2d()

3284:    Level: developer

3286:    Notes:
3287:    For regular PETSc vectors this routine does not involve any copies. For
3288:    any special vectors that do not store local vector data in a contiguous
3289:    array, this routine will copy the data back into the underlying
3290:    vector data structure from the array obtained with VecGetArray().

3292:    This routine actually zeros out the a pointer.

3294: .seealso: `VecGetArray()`, `VecRestoreArray()`, `VecRestoreArrays()`, `VecRestoreArrayF90()`, `VecPlaceArray()`,
3295:           `VecGetArray2d()`, `VecGetArray3d()`, `VecRestoreArray3d()`, `DMDAVecGetArray()`, `DMDAVecRestoreArray()`
3296:           `VecGetArray1d()`, `VecRestoreArray1d()`, `VecGetArray4d()`, `VecRestoreArray4d()`
3297: @*/
3298: PetscErrorCode VecRestoreArray2d(Vec x, PetscInt m, PetscInt n, PetscInt mstart, PetscInt nstart, PetscScalar **a[])
3299: {
3300:   void *dummy;

3305:   dummy = (void *)(*a + mstart);
3306:   PetscFree(dummy);
3307:   VecRestoreArray(x, NULL);
3308:   return 0;
3309: }

3311: /*@C
3312:    VecRestoreArray2dWrite - Restores a vector after VecGetArray2dWrite() has been called.

3314:    Logically Collective

3316:    Input Parameters:
3317: +  x - the vector
3318: .  m - first dimension of two dimensional array
3319: .  n - second dimension of the two dimensional array
3320: .  mstart - first index you will use in first coordinate direction (often 0)
3321: .  nstart - first index in the second coordinate direction (often 0)
3322: -  a - location of pointer to array obtained from VecGetArray2d()

3324:    Level: developer

3326:    Notes:
3327:    For regular PETSc vectors this routine does not involve any copies. For
3328:    any special vectors that do not store local vector data in a contiguous
3329:    array, this routine will copy the data back into the underlying
3330:    vector data structure from the array obtained with VecGetArray().

3332:    This routine actually zeros out the a pointer.

3334: .seealso: `VecGetArray()`, `VecRestoreArray()`, `VecRestoreArrays()`, `VecRestoreArrayF90()`, `VecPlaceArray()`,
3335:           `VecGetArray2d()`, `VecGetArray3d()`, `VecRestoreArray3d()`, `DMDAVecGetArray()`, `DMDAVecRestoreArray()`
3336:           `VecGetArray1d()`, `VecRestoreArray1d()`, `VecGetArray4d()`, `VecRestoreArray4d()`
3337: @*/
3338: PetscErrorCode VecRestoreArray2dWrite(Vec x, PetscInt m, PetscInt n, PetscInt mstart, PetscInt nstart, PetscScalar **a[])
3339: {
3340:   void *dummy;

3345:   dummy = (void *)(*a + mstart);
3346:   PetscFree(dummy);
3347:   VecRestoreArrayWrite(x, NULL);
3348:   return 0;
3349: }

3351: /*@C
3352:    VecGetArray1d - Returns a pointer to a 1d contiguous array that contains this
3353:    processor's portion of the vector data.  You MUST call VecRestoreArray1d()
3354:    when you no longer need access to the array.

3356:    Logically Collective

3358:    Input Parameters:
3359: +  x - the vector
3360: .  m - first dimension of two dimensional array
3361: -  mstart - first index you will use in first coordinate direction (often 0)

3363:    Output Parameter:
3364: .  a - location to put pointer to the array

3366:    Level: developer

3368:   Notes:
3369:    For a vector obtained from DMCreateLocalVector() mstart are likely
3370:    obtained from the corner indices obtained from DMDAGetGhostCorners() while for
3371:    DMCreateGlobalVector() they are the corner indices from DMDAGetCorners().

3373:    For standard PETSc vectors this is an inexpensive call; it does not copy the vector values.

3375: .seealso: `VecGetArray()`, `VecRestoreArray()`, `VecGetArrays()`, `VecGetArrayF90()`, `VecPlaceArray()`,
3376:           `VecRestoreArray2d()`, `DMDAVecGetArray()`, `DMDAVecRestoreArray()`, `VecGetArray3d()`, `VecRestoreArray3d()`,
3377:           `VecGetArray2d()`, `VecRestoreArray1d()`, `VecGetArray4d()`, `VecRestoreArray4d()`
3378: @*/
3379: PetscErrorCode VecGetArray1d(Vec x, PetscInt m, PetscInt mstart, PetscScalar *a[])
3380: {
3381:   PetscInt N;

3386:   VecGetLocalSize(x, &N);
3388:   VecGetArray(x, a);
3389:   *a -= mstart;
3390:   return 0;
3391: }

3393: /*@C
3394:    VecGetArray1dWrite - Returns a pointer to a 1d contiguous array that will contain this
3395:    processor's portion of the vector data.  You MUST call VecRestoreArray1dWrite()
3396:    when you no longer need access to the array.

3398:    Logically Collective

3400:    Input Parameters:
3401: +  x - the vector
3402: .  m - first dimension of two dimensional array
3403: -  mstart - first index you will use in first coordinate direction (often 0)

3405:    Output Parameter:
3406: .  a - location to put pointer to the array

3408:    Level: developer

3410:   Notes:
3411:    For a vector obtained from DMCreateLocalVector() mstart are likely
3412:    obtained from the corner indices obtained from DMDAGetGhostCorners() while for
3413:    DMCreateGlobalVector() they are the corner indices from DMDAGetCorners().

3415:    For standard PETSc vectors this is an inexpensive call; it does not copy the vector values.

3417: .seealso: `VecGetArray()`, `VecRestoreArray()`, `VecGetArrays()`, `VecGetArrayF90()`, `VecPlaceArray()`,
3418:           `VecRestoreArray2d()`, `DMDAVecGetArray()`, `DMDAVecRestoreArray()`, `VecGetArray3d()`, `VecRestoreArray3d()`,
3419:           `VecGetArray2d()`, `VecRestoreArray1d()`, `VecGetArray4d()`, `VecRestoreArray4d()`
3420: @*/
3421: PetscErrorCode VecGetArray1dWrite(Vec x, PetscInt m, PetscInt mstart, PetscScalar *a[])
3422: {
3423:   PetscInt N;

3428:   VecGetLocalSize(x, &N);
3430:   VecGetArrayWrite(x, a);
3431:   *a -= mstart;
3432:   return 0;
3433: }

3435: /*@C
3436:    VecRestoreArray1d - Restores a vector after VecGetArray1d() has been called.

3438:    Logically Collective

3440:    Input Parameters:
3441: +  x - the vector
3442: .  m - first dimension of two dimensional array
3443: .  mstart - first index you will use in first coordinate direction (often 0)
3444: -  a - location of pointer to array obtained from VecGetArray21()

3446:    Level: developer

3448:    Notes:
3449:    For regular PETSc vectors this routine does not involve any copies. For
3450:    any special vectors that do not store local vector data in a contiguous
3451:    array, this routine will copy the data back into the underlying
3452:    vector data structure from the array obtained with VecGetArray1d().

3454:    This routine actually zeros out the a pointer.

3456: .seealso: `VecGetArray()`, `VecRestoreArray()`, `VecRestoreArrays()`, `VecRestoreArrayF90()`, `VecPlaceArray()`,
3457:           `VecGetArray2d()`, `VecGetArray3d()`, `VecRestoreArray3d()`, `DMDAVecGetArray()`, `DMDAVecRestoreArray()`
3458:           `VecGetArray1d()`, `VecRestoreArray2d()`, `VecGetArray4d()`, `VecRestoreArray4d()`
3459: @*/
3460: PetscErrorCode VecRestoreArray1d(Vec x, PetscInt m, PetscInt mstart, PetscScalar *a[])
3461: {
3464:   VecRestoreArray(x, NULL);
3465:   return 0;
3466: }

3468: /*@C
3469:    VecRestoreArray1dWrite - Restores a vector after VecGetArray1dWrite() has been called.

3471:    Logically Collective

3473:    Input Parameters:
3474: +  x - the vector
3475: .  m - first dimension of two dimensional array
3476: .  mstart - first index you will use in first coordinate direction (often 0)
3477: -  a - location of pointer to array obtained from VecGetArray21()

3479:    Level: developer

3481:    Notes:
3482:    For regular PETSc vectors this routine does not involve any copies. For
3483:    any special vectors that do not store local vector data in a contiguous
3484:    array, this routine will copy the data back into the underlying
3485:    vector data structure from the array obtained with VecGetArray1d().

3487:    This routine actually zeros out the a pointer.

3489: .seealso: `VecGetArray()`, `VecRestoreArray()`, `VecRestoreArrays()`, `VecRestoreArrayF90()`, `VecPlaceArray()`,
3490:           `VecGetArray2d()`, `VecGetArray3d()`, `VecRestoreArray3d()`, `DMDAVecGetArray()`, `DMDAVecRestoreArray()`
3491:           `VecGetArray1d()`, `VecRestoreArray2d()`, `VecGetArray4d()`, `VecRestoreArray4d()`
3492: @*/
3493: PetscErrorCode VecRestoreArray1dWrite(Vec x, PetscInt m, PetscInt mstart, PetscScalar *a[])
3494: {
3497:   VecRestoreArrayWrite(x, NULL);
3498:   return 0;
3499: }

3501: /*@C
3502:    VecGetArray3d - Returns a pointer to a 3d contiguous array that contains this
3503:    processor's portion of the vector data.  You MUST call VecRestoreArray3d()
3504:    when you no longer need access to the array.

3506:    Logically Collective

3508:    Input Parameters:
3509: +  x - the vector
3510: .  m - first dimension of three dimensional array
3511: .  n - second dimension of three dimensional array
3512: .  p - third dimension of three dimensional array
3513: .  mstart - first index you will use in first coordinate direction (often 0)
3514: .  nstart - first index in the second coordinate direction (often 0)
3515: -  pstart - first index in the third coordinate direction (often 0)

3517:    Output Parameter:
3518: .  a - location to put pointer to the array

3520:    Level: developer

3522:   Notes:
3523:    For a vector obtained from DMCreateLocalVector() mstart, nstart, and pstart are likely
3524:    obtained from the corner indices obtained from DMDAGetGhostCorners() while for
3525:    DMCreateGlobalVector() they are the corner indices from DMDAGetCorners(). In both cases
3526:    the arguments from DMDAGet[Ghost]Corners() are reversed in the call to VecGetArray3d().

3528:    For standard PETSc vectors this is an inexpensive call; it does not copy the vector values.

3530: .seealso: `VecGetArray()`, `VecRestoreArray()`, `VecGetArrays()`, `VecGetArrayF90()`, `VecPlaceArray()`,
3531:           `VecRestoreArray2d()`, `DMDAVecGetarray()`, `DMDAVecRestoreArray()`, `VecGetArray3d()`, `VecRestoreArray3d()`,
3532:           `VecGetArray1d()`, `VecRestoreArray1d()`, `VecGetArray4d()`, `VecRestoreArray4d()`
3533: @*/
3534: PetscErrorCode VecGetArray3d(Vec x, PetscInt m, PetscInt n, PetscInt p, PetscInt mstart, PetscInt nstart, PetscInt pstart, PetscScalar ***a[])
3535: {
3536:   PetscInt     i, N, j;
3537:   PetscScalar *aa, **b;

3542:   VecGetLocalSize(x, &N);
3544:   VecGetArray(x, &aa);

3546:   PetscMalloc(m * sizeof(PetscScalar **) + m * n * sizeof(PetscScalar *), a);
3547:   b = (PetscScalar **)((*a) + m);
3548:   for (i = 0; i < m; i++) (*a)[i] = b + i * n - nstart;
3549:   for (i = 0; i < m; i++)
3550:     for (j = 0; j < n; j++) b[i * n + j] = aa + i * n * p + j * p - pstart;
3551:   *a -= mstart;
3552:   return 0;
3553: }

3555: /*@C
3556:    VecGetArray3dWrite - Returns a pointer to a 3d contiguous array that will contain this
3557:    processor's portion of the vector data.  You MUST call VecRestoreArray3dWrite()
3558:    when you no longer need access to the array.

3560:    Logically Collective

3562:    Input Parameters:
3563: +  x - the vector
3564: .  m - first dimension of three dimensional array
3565: .  n - second dimension of three dimensional array
3566: .  p - third dimension of three dimensional array
3567: .  mstart - first index you will use in first coordinate direction (often 0)
3568: .  nstart - first index in the second coordinate direction (often 0)
3569: -  pstart - first index in the third coordinate direction (often 0)

3571:    Output Parameter:
3572: .  a - location to put pointer to the array

3574:    Level: developer

3576:   Notes:
3577:    For a vector obtained from DMCreateLocalVector() mstart, nstart, and pstart are likely
3578:    obtained from the corner indices obtained from DMDAGetGhostCorners() while for
3579:    DMCreateGlobalVector() they are the corner indices from DMDAGetCorners(). In both cases
3580:    the arguments from DMDAGet[Ghost]Corners() are reversed in the call to VecGetArray3d().

3582:    For standard PETSc vectors this is an inexpensive call; it does not copy the vector values.

3584: .seealso: `VecGetArray()`, `VecRestoreArray()`, `VecGetArrays()`, `VecGetArrayF90()`, `VecPlaceArray()`,
3585:           `VecRestoreArray2d()`, `DMDAVecGetarray()`, `DMDAVecRestoreArray()`, `VecGetArray3d()`, `VecRestoreArray3d()`,
3586:           `VecGetArray1d()`, `VecRestoreArray1d()`, `VecGetArray4d()`, `VecRestoreArray4d()`
3587: @*/
3588: PetscErrorCode VecGetArray3dWrite(Vec x, PetscInt m, PetscInt n, PetscInt p, PetscInt mstart, PetscInt nstart, PetscInt pstart, PetscScalar ***a[])
3589: {
3590:   PetscInt     i, N, j;
3591:   PetscScalar *aa, **b;

3596:   VecGetLocalSize(x, &N);
3598:   VecGetArrayWrite(x, &aa);

3600:   PetscMalloc(m * sizeof(PetscScalar **) + m * n * sizeof(PetscScalar *), a);
3601:   b = (PetscScalar **)((*a) + m);
3602:   for (i = 0; i < m; i++) (*a)[i] = b + i * n - nstart;
3603:   for (i = 0; i < m; i++)
3604:     for (j = 0; j < n; j++) b[i * n + j] = aa + i * n * p + j * p - pstart;

3606:   *a -= mstart;
3607:   return 0;
3608: }

3610: /*@C
3611:    VecRestoreArray3d - Restores a vector after VecGetArray3d() has been called.

3613:    Logically Collective

3615:    Input Parameters:
3616: +  x - the vector
3617: .  m - first dimension of three dimensional array
3618: .  n - second dimension of the three dimensional array
3619: .  p - third dimension of the three dimensional array
3620: .  mstart - first index you will use in first coordinate direction (often 0)
3621: .  nstart - first index in the second coordinate direction (often 0)
3622: .  pstart - first index in the third coordinate direction (often 0)
3623: -  a - location of pointer to array obtained from VecGetArray3d()

3625:    Level: developer

3627:    Notes:
3628:    For regular PETSc vectors this routine does not involve any copies. For
3629:    any special vectors that do not store local vector data in a contiguous
3630:    array, this routine will copy the data back into the underlying
3631:    vector data structure from the array obtained with VecGetArray().

3633:    This routine actually zeros out the a pointer.

3635: .seealso: `VecGetArray()`, `VecRestoreArray()`, `VecRestoreArrays()`, `VecRestoreArrayF90()`, `VecPlaceArray()`,
3636:           `VecGetArray2d()`, `VecGetArray3d()`, `VecRestoreArray3d()`, `DMDAVecGetArray()`, `DMDAVecRestoreArray()`
3637:           `VecGetArray1d()`, `VecRestoreArray1d()`, `VecGetArray4d()`, `VecRestoreArray4d()`, `VecGet`
3638: @*/
3639: PetscErrorCode VecRestoreArray3d(Vec x, PetscInt m, PetscInt n, PetscInt p, PetscInt mstart, PetscInt nstart, PetscInt pstart, PetscScalar ***a[])
3640: {
3641:   void *dummy;

3646:   dummy = (void *)(*a + mstart);
3647:   PetscFree(dummy);
3648:   VecRestoreArray(x, NULL);
3649:   return 0;
3650: }

3652: /*@C
3653:    VecRestoreArray3dWrite - Restores a vector after VecGetArray3dWrite() has been called.

3655:    Logically Collective

3657:    Input Parameters:
3658: +  x - the vector
3659: .  m - first dimension of three dimensional array
3660: .  n - second dimension of the three dimensional array
3661: .  p - third dimension of the three dimensional array
3662: .  mstart - first index you will use in first coordinate direction (often 0)
3663: .  nstart - first index in the second coordinate direction (often 0)
3664: .  pstart - first index in the third coordinate direction (often 0)
3665: -  a - location of pointer to array obtained from VecGetArray3d()

3667:    Level: developer

3669:    Notes:
3670:    For regular PETSc vectors this routine does not involve any copies. For
3671:    any special vectors that do not store local vector data in a contiguous
3672:    array, this routine will copy the data back into the underlying
3673:    vector data structure from the array obtained with VecGetArray().

3675:    This routine actually zeros out the a pointer.

3677: .seealso: `VecGetArray()`, `VecRestoreArray()`, `VecRestoreArrays()`, `VecRestoreArrayF90()`, `VecPlaceArray()`,
3678:           `VecGetArray2d()`, `VecGetArray3d()`, `VecRestoreArray3d()`, `DMDAVecGetArray()`, `DMDAVecRestoreArray()`
3679:           `VecGetArray1d()`, `VecRestoreArray1d()`, `VecGetArray4d()`, `VecRestoreArray4d()`, `VecGet`
3680: @*/
3681: PetscErrorCode VecRestoreArray3dWrite(Vec x, PetscInt m, PetscInt n, PetscInt p, PetscInt mstart, PetscInt nstart, PetscInt pstart, PetscScalar ***a[])
3682: {
3683:   void *dummy;

3688:   dummy = (void *)(*a + mstart);
3689:   PetscFree(dummy);
3690:   VecRestoreArrayWrite(x, NULL);
3691:   return 0;
3692: }

3694: /*@C
3695:    VecGetArray4d - Returns a pointer to a 4d contiguous array that contains this
3696:    processor's portion of the vector data.  You MUST call VecRestoreArray4d()
3697:    when you no longer need access to the array.

3699:    Logically Collective

3701:    Input Parameters:
3702: +  x - the vector
3703: .  m - first dimension of four dimensional array
3704: .  n - second dimension of four dimensional array
3705: .  p - third dimension of four dimensional array
3706: .  q - fourth dimension of four dimensional array
3707: .  mstart - first index you will use in first coordinate direction (often 0)
3708: .  nstart - first index in the second coordinate direction (often 0)
3709: .  pstart - first index in the third coordinate direction (often 0)
3710: -  qstart - first index in the fourth coordinate direction (often 0)

3712:    Output Parameter:
3713: .  a - location to put pointer to the array

3715:    Level: beginner

3717:   Notes:
3718:    For a vector obtained from DMCreateLocalVector() mstart, nstart, and pstart are likely
3719:    obtained from the corner indices obtained from DMDAGetGhostCorners() while for
3720:    DMCreateGlobalVector() they are the corner indices from DMDAGetCorners(). In both cases
3721:    the arguments from DMDAGet[Ghost]Corners() are reversed in the call to VecGetArray3d().

3723:    For standard PETSc vectors this is an inexpensive call; it does not copy the vector values.

3725: .seealso: `VecGetArray()`, `VecRestoreArray()`, `VecGetArrays()`, `VecGetArrayF90()`, `VecPlaceArray()`,
3726:           `VecRestoreArray2d()`, `DMDAVecGetarray()`, `DMDAVecRestoreArray()`, `VecGetArray3d()`, `VecRestoreArray3d()`,
3727:           `VecGetArray1d()`, `VecRestoreArray1d()`, `VecGetArray4d()`, `VecRestoreArray4d()`
3728: @*/
3729: PetscErrorCode VecGetArray4d(Vec x, PetscInt m, PetscInt n, PetscInt p, PetscInt q, PetscInt mstart, PetscInt nstart, PetscInt pstart, PetscInt qstart, PetscScalar ****a[])
3730: {
3731:   PetscInt     i, N, j, k;
3732:   PetscScalar *aa, ***b, **c;

3737:   VecGetLocalSize(x, &N);
3739:   VecGetArray(x, &aa);

3741:   PetscMalloc(m * sizeof(PetscScalar ***) + m * n * sizeof(PetscScalar **) + m * n * p * sizeof(PetscScalar *), a);
3742:   b = (PetscScalar ***)((*a) + m);
3743:   c = (PetscScalar **)(b + m * n);
3744:   for (i = 0; i < m; i++) (*a)[i] = b + i * n - nstart;
3745:   for (i = 0; i < m; i++)
3746:     for (j = 0; j < n; j++) b[i * n + j] = c + i * n * p + j * p - pstart;
3747:   for (i = 0; i < m; i++)
3748:     for (j = 0; j < n; j++)
3749:       for (k = 0; k < p; k++) c[i * n * p + j * p + k] = aa + i * n * p * q + j * p * q + k * q - qstart;
3750:   *a -= mstart;
3751:   return 0;
3752: }

3754: /*@C
3755:    VecGetArray4dWrite - Returns a pointer to a 4d contiguous array that will contain this
3756:    processor's portion of the vector data.  You MUST call VecRestoreArray4dWrite()
3757:    when you no longer need access to the array.

3759:    Logically Collective

3761:    Input Parameters:
3762: +  x - the vector
3763: .  m - first dimension of four dimensional array
3764: .  n - second dimension of four dimensional array
3765: .  p - third dimension of four dimensional array
3766: .  q - fourth dimension of four dimensional array
3767: .  mstart - first index you will use in first coordinate direction (often 0)
3768: .  nstart - first index in the second coordinate direction (often 0)
3769: .  pstart - first index in the third coordinate direction (often 0)
3770: -  qstart - first index in the fourth coordinate direction (often 0)

3772:    Output Parameter:
3773: .  a - location to put pointer to the array

3775:    Level: beginner

3777:   Notes:
3778:    For a vector obtained from DMCreateLocalVector() mstart, nstart, and pstart are likely
3779:    obtained from the corner indices obtained from DMDAGetGhostCorners() while for
3780:    DMCreateGlobalVector() they are the corner indices from DMDAGetCorners(). In both cases
3781:    the arguments from DMDAGet[Ghost]Corners() are reversed in the call to VecGetArray3d().

3783:    For standard PETSc vectors this is an inexpensive call; it does not copy the vector values.

3785: .seealso: `VecGetArray()`, `VecRestoreArray()`, `VecGetArrays()`, `VecGetArrayF90()`, `VecPlaceArray()`,
3786:           `VecRestoreArray2d()`, `DMDAVecGetarray()`, `DMDAVecRestoreArray()`, `VecGetArray3d()`, `VecRestoreArray3d()`,
3787:           `VecGetArray1d()`, `VecRestoreArray1d()`, `VecGetArray4d()`, `VecRestoreArray4d()`
3788: @*/
3789: PetscErrorCode VecGetArray4dWrite(Vec x, PetscInt m, PetscInt n, PetscInt p, PetscInt q, PetscInt mstart, PetscInt nstart, PetscInt pstart, PetscInt qstart, PetscScalar ****a[])
3790: {
3791:   PetscInt     i, N, j, k;
3792:   PetscScalar *aa, ***b, **c;

3797:   VecGetLocalSize(x, &N);
3799:   VecGetArrayWrite(x, &aa);

3801:   PetscMalloc(m * sizeof(PetscScalar ***) + m * n * sizeof(PetscScalar **) + m * n * p * sizeof(PetscScalar *), a);
3802:   b = (PetscScalar ***)((*a) + m);
3803:   c = (PetscScalar **)(b + m * n);
3804:   for (i = 0; i < m; i++) (*a)[i] = b + i * n - nstart;
3805:   for (i = 0; i < m; i++)
3806:     for (j = 0; j < n; j++) b[i * n + j] = c + i * n * p + j * p - pstart;
3807:   for (i = 0; i < m; i++)
3808:     for (j = 0; j < n; j++)
3809:       for (k = 0; k < p; k++) c[i * n * p + j * p + k] = aa + i * n * p * q + j * p * q + k * q - qstart;
3810:   *a -= mstart;
3811:   return 0;
3812: }

3814: /*@C
3815:    VecRestoreArray4d - Restores a vector after VecGetArray3d() has been called.

3817:    Logically Collective

3819:    Input Parameters:
3820: +  x - the vector
3821: .  m - first dimension of four dimensional array
3822: .  n - second dimension of the four dimensional array
3823: .  p - third dimension of the four dimensional array
3824: .  q - fourth dimension of the four dimensional array
3825: .  mstart - first index you will use in first coordinate direction (often 0)
3826: .  nstart - first index in the second coordinate direction (often 0)
3827: .  pstart - first index in the third coordinate direction (often 0)
3828: .  qstart - first index in the fourth coordinate direction (often 0)
3829: -  a - location of pointer to array obtained from VecGetArray4d()

3831:    Level: beginner

3833:    Notes:
3834:    For regular PETSc vectors this routine does not involve any copies. For
3835:    any special vectors that do not store local vector data in a contiguous
3836:    array, this routine will copy the data back into the underlying
3837:    vector data structure from the array obtained with VecGetArray().

3839:    This routine actually zeros out the a pointer.

3841: .seealso: `VecGetArray()`, `VecRestoreArray()`, `VecRestoreArrays()`, `VecRestoreArrayF90()`, `VecPlaceArray()`,
3842:           `VecGetArray2d()`, `VecGetArray3d()`, `VecRestoreArray3d()`, `DMDAVecGetArray()`, `DMDAVecRestoreArray()`
3843:           `VecGetArray1d()`, `VecRestoreArray1d()`, `VecGetArray4d()`, `VecRestoreArray4d()`, `VecGet`
3844: @*/
3845: PetscErrorCode VecRestoreArray4d(Vec x, PetscInt m, PetscInt n, PetscInt p, PetscInt q, PetscInt mstart, PetscInt nstart, PetscInt pstart, PetscInt qstart, PetscScalar ****a[])
3846: {
3847:   void *dummy;

3852:   dummy = (void *)(*a + mstart);
3853:   PetscFree(dummy);
3854:   VecRestoreArray(x, NULL);
3855:   return 0;
3856: }

3858: /*@C
3859:    VecRestoreArray4dWrite - Restores a vector after VecGetArray3dWrite() has been called.

3861:    Logically Collective

3863:    Input Parameters:
3864: +  x - the vector
3865: .  m - first dimension of four dimensional array
3866: .  n - second dimension of the four dimensional array
3867: .  p - third dimension of the four dimensional array
3868: .  q - fourth dimension of the four dimensional array
3869: .  mstart - first index you will use in first coordinate direction (often 0)
3870: .  nstart - first index in the second coordinate direction (often 0)
3871: .  pstart - first index in the third coordinate direction (often 0)
3872: .  qstart - first index in the fourth coordinate direction (often 0)
3873: -  a - location of pointer to array obtained from VecGetArray4d()

3875:    Level: beginner

3877:    Notes:
3878:    For regular PETSc vectors this routine does not involve any copies. For
3879:    any special vectors that do not store local vector data in a contiguous
3880:    array, this routine will copy the data back into the underlying
3881:    vector data structure from the array obtained with VecGetArray().

3883:    This routine actually zeros out the a pointer.

3885: .seealso: `VecGetArray()`, `VecRestoreArray()`, `VecRestoreArrays()`, `VecRestoreArrayF90()`, `VecPlaceArray()`,
3886:           `VecGetArray2d()`, `VecGetArray3d()`, `VecRestoreArray3d()`, `DMDAVecGetArray()`, `DMDAVecRestoreArray()`
3887:           `VecGetArray1d()`, `VecRestoreArray1d()`, `VecGetArray4d()`, `VecRestoreArray4d()`, `VecGet`
3888: @*/
3889: PetscErrorCode VecRestoreArray4dWrite(Vec x, PetscInt m, PetscInt n, PetscInt p, PetscInt q, PetscInt mstart, PetscInt nstart, PetscInt pstart, PetscInt qstart, PetscScalar ****a[])
3890: {
3891:   void *dummy;

3896:   dummy = (void *)(*a + mstart);
3897:   PetscFree(dummy);
3898:   VecRestoreArrayWrite(x, NULL);
3899:   return 0;
3900: }

3902: /*@C
3903:    VecGetArray2dRead - Returns a pointer to a 2d contiguous array that contains this
3904:    processor's portion of the vector data.  You MUST call VecRestoreArray2dRead()
3905:    when you no longer need access to the array.

3907:    Logically Collective

3909:    Input Parameters:
3910: +  x - the vector
3911: .  m - first dimension of two dimensional array
3912: .  n - second dimension of two dimensional array
3913: .  mstart - first index you will use in first coordinate direction (often 0)
3914: -  nstart - first index in the second coordinate direction (often 0)

3916:    Output Parameter:
3917: .  a - location to put pointer to the array

3919:    Level: developer

3921:   Notes:
3922:    For a vector obtained from DMCreateLocalVector() mstart and nstart are likely
3923:    obtained from the corner indices obtained from DMDAGetGhostCorners() while for
3924:    DMCreateGlobalVector() they are the corner indices from DMDAGetCorners(). In both cases
3925:    the arguments from DMDAGet[Ghost]Corners() are reversed in the call to VecGetArray2d().

3927:    For standard PETSc vectors this is an inexpensive call; it does not copy the vector values.

3929: .seealso: `VecGetArray()`, `VecRestoreArray()`, `VecGetArrays()`, `VecGetArrayF90()`, `VecPlaceArray()`,
3930:           `VecRestoreArray2d()`, `DMDAVecGetArray()`, `DMDAVecRestoreArray()`, `VecGetArray3d()`, `VecRestoreArray3d()`,
3931:           `VecGetArray1d()`, `VecRestoreArray1d()`, `VecGetArray4d()`, `VecRestoreArray4d()`
3932: @*/
3933: PetscErrorCode VecGetArray2dRead(Vec x, PetscInt m, PetscInt n, PetscInt mstart, PetscInt nstart, PetscScalar **a[])
3934: {
3935:   PetscInt           i, N;
3936:   const PetscScalar *aa;

3941:   VecGetLocalSize(x, &N);
3943:   VecGetArrayRead(x, &aa);

3945:   PetscMalloc1(m, a);
3946:   for (i = 0; i < m; i++) (*a)[i] = (PetscScalar *)aa + i * n - nstart;
3947:   *a -= mstart;
3948:   return 0;
3949: }

3951: /*@C
3952:    VecRestoreArray2dRead - Restores a vector after VecGetArray2dRead() has been called.

3954:    Logically Collective

3956:    Input Parameters:
3957: +  x - the vector
3958: .  m - first dimension of two dimensional array
3959: .  n - second dimension of the two dimensional array
3960: .  mstart - first index you will use in first coordinate direction (often 0)
3961: .  nstart - first index in the second coordinate direction (often 0)
3962: -  a - location of pointer to array obtained from VecGetArray2d()

3964:    Level: developer

3966:    Notes:
3967:    For regular PETSc vectors this routine does not involve any copies. For
3968:    any special vectors that do not store local vector data in a contiguous
3969:    array, this routine will copy the data back into the underlying
3970:    vector data structure from the array obtained with VecGetArray().

3972:    This routine actually zeros out the a pointer.

3974: .seealso: `VecGetArray()`, `VecRestoreArray()`, `VecRestoreArrays()`, `VecRestoreArrayF90()`, `VecPlaceArray()`,
3975:           `VecGetArray2d()`, `VecGetArray3d()`, `VecRestoreArray3d()`, `DMDAVecGetArray()`, `DMDAVecRestoreArray()`
3976:           `VecGetArray1d()`, `VecRestoreArray1d()`, `VecGetArray4d()`, `VecRestoreArray4d()`
3977: @*/
3978: PetscErrorCode VecRestoreArray2dRead(Vec x, PetscInt m, PetscInt n, PetscInt mstart, PetscInt nstart, PetscScalar **a[])
3979: {
3980:   void *dummy;

3985:   dummy = (void *)(*a + mstart);
3986:   PetscFree(dummy);
3987:   VecRestoreArrayRead(x, NULL);
3988:   return 0;
3989: }

3991: /*@C
3992:    VecGetArray1dRead - Returns a pointer to a 1d contiguous array that contains this
3993:    processor's portion of the vector data.  You MUST call VecRestoreArray1dRead()
3994:    when you no longer need access to the array.

3996:    Logically Collective

3998:    Input Parameters:
3999: +  x - the vector
4000: .  m - first dimension of two dimensional array
4001: -  mstart - first index you will use in first coordinate direction (often 0)

4003:    Output Parameter:
4004: .  a - location to put pointer to the array

4006:    Level: developer

4008:   Notes:
4009:    For a vector obtained from DMCreateLocalVector() mstart are likely
4010:    obtained from the corner indices obtained from DMDAGetGhostCorners() while for
4011:    DMCreateGlobalVector() they are the corner indices from DMDAGetCorners().

4013:    For standard PETSc vectors this is an inexpensive call; it does not copy the vector values.

4015: .seealso: `VecGetArray()`, `VecRestoreArray()`, `VecGetArrays()`, `VecGetArrayF90()`, `VecPlaceArray()`,
4016:           `VecRestoreArray2d()`, `DMDAVecGetArray()`, `DMDAVecRestoreArray()`, `VecGetArray3d()`, `VecRestoreArray3d()`,
4017:           `VecGetArray2d()`, `VecRestoreArray1d()`, `VecGetArray4d()`, `VecRestoreArray4d()`
4018: @*/
4019: PetscErrorCode VecGetArray1dRead(Vec x, PetscInt m, PetscInt mstart, PetscScalar *a[])
4020: {
4021:   PetscInt N;

4026:   VecGetLocalSize(x, &N);
4028:   VecGetArrayRead(x, (const PetscScalar **)a);
4029:   *a -= mstart;
4030:   return 0;
4031: }

4033: /*@C
4034:    VecRestoreArray1dRead - Restores a vector after VecGetArray1dRead() has been called.

4036:    Logically Collective

4038:    Input Parameters:
4039: +  x - the vector
4040: .  m - first dimension of two dimensional array
4041: .  mstart - first index you will use in first coordinate direction (often 0)
4042: -  a - location of pointer to array obtained from VecGetArray21()

4044:    Level: developer

4046:    Notes:
4047:    For regular PETSc vectors this routine does not involve any copies. For
4048:    any special vectors that do not store local vector data in a contiguous
4049:    array, this routine will copy the data back into the underlying
4050:    vector data structure from the array obtained with VecGetArray1dRead().

4052:    This routine actually zeros out the a pointer.

4054: .seealso: `VecGetArray()`, `VecRestoreArray()`, `VecRestoreArrays()`, `VecRestoreArrayF90()`, `VecPlaceArray()`,
4055:           `VecGetArray2d()`, `VecGetArray3d()`, `VecRestoreArray3d()`, `DMDAVecGetArray()`, `DMDAVecRestoreArray()`
4056:           `VecGetArray1d()`, `VecRestoreArray2d()`, `VecGetArray4d()`, `VecRestoreArray4d()`
4057: @*/
4058: PetscErrorCode VecRestoreArray1dRead(Vec x, PetscInt m, PetscInt mstart, PetscScalar *a[])
4059: {
4062:   VecRestoreArrayRead(x, NULL);
4063:   return 0;
4064: }

4066: /*@C
4067:    VecGetArray3dRead - Returns a pointer to a 3d contiguous array that contains this
4068:    processor's portion of the vector data.  You MUST call VecRestoreArray3dRead()
4069:    when you no longer need access to the array.

4071:    Logically Collective

4073:    Input Parameters:
4074: +  x - the vector
4075: .  m - first dimension of three dimensional array
4076: .  n - second dimension of three dimensional array
4077: .  p - third dimension of three dimensional array
4078: .  mstart - first index you will use in first coordinate direction (often 0)
4079: .  nstart - first index in the second coordinate direction (often 0)
4080: -  pstart - first index in the third coordinate direction (often 0)

4082:    Output Parameter:
4083: .  a - location to put pointer to the array

4085:    Level: developer

4087:   Notes:
4088:    For a vector obtained from DMCreateLocalVector() mstart, nstart, and pstart are likely
4089:    obtained from the corner indices obtained from DMDAGetGhostCorners() while for
4090:    DMCreateGlobalVector() they are the corner indices from DMDAGetCorners(). In both cases
4091:    the arguments from DMDAGet[Ghost]Corners() are reversed in the call to VecGetArray3dRead().

4093:    For standard PETSc vectors this is an inexpensive call; it does not copy the vector values.

4095: .seealso: `VecGetArray()`, `VecRestoreArray()`, `VecGetArrays()`, `VecGetArrayF90()`, `VecPlaceArray()`,
4096:           `VecRestoreArray2d()`, `DMDAVecGetarray()`, `DMDAVecRestoreArray()`, `VecGetArray3d()`, `VecRestoreArray3d()`,
4097:           `VecGetArray1d()`, `VecRestoreArray1d()`, `VecGetArray4d()`, `VecRestoreArray4d()`
4098: @*/
4099: PetscErrorCode VecGetArray3dRead(Vec x, PetscInt m, PetscInt n, PetscInt p, PetscInt mstart, PetscInt nstart, PetscInt pstart, PetscScalar ***a[])
4100: {
4101:   PetscInt           i, N, j;
4102:   const PetscScalar *aa;
4103:   PetscScalar      **b;

4108:   VecGetLocalSize(x, &N);
4110:   VecGetArrayRead(x, &aa);

4112:   PetscMalloc(m * sizeof(PetscScalar **) + m * n * sizeof(PetscScalar *), a);
4113:   b = (PetscScalar **)((*a) + m);
4114:   for (i = 0; i < m; i++) (*a)[i] = b + i * n - nstart;
4115:   for (i = 0; i < m; i++)
4116:     for (j = 0; j < n; j++) b[i * n + j] = (PetscScalar *)aa + i * n * p + j * p - pstart;
4117:   *a -= mstart;
4118:   return 0;
4119: }

4121: /*@C
4122:    VecRestoreArray3dRead - Restores a vector after VecGetArray3dRead() has been called.

4124:    Logically Collective

4126:    Input Parameters:
4127: +  x - the vector
4128: .  m - first dimension of three dimensional array
4129: .  n - second dimension of the three dimensional array
4130: .  p - third dimension of the three dimensional array
4131: .  mstart - first index you will use in first coordinate direction (often 0)
4132: .  nstart - first index in the second coordinate direction (often 0)
4133: .  pstart - first index in the third coordinate direction (often 0)
4134: -  a - location of pointer to array obtained from VecGetArray3dRead()

4136:    Level: developer

4138:    Notes:
4139:    For regular PETSc vectors this routine does not involve any copies. For
4140:    any special vectors that do not store local vector data in a contiguous
4141:    array, this routine will copy the data back into the underlying
4142:    vector data structure from the array obtained with VecGetArray().

4144:    This routine actually zeros out the a pointer.

4146: .seealso: `VecGetArray()`, `VecRestoreArray()`, `VecRestoreArrays()`, `VecRestoreArrayF90()`, `VecPlaceArray()`,
4147:           `VecGetArray2d()`, `VecGetArray3d()`, `VecRestoreArray3d()`, `DMDAVecGetArray()`, `DMDAVecRestoreArray()`
4148:           `VecGetArray1d()`, `VecRestoreArray1d()`, `VecGetArray4d()`, `VecRestoreArray4d()`, `VecGet`
4149: @*/
4150: PetscErrorCode VecRestoreArray3dRead(Vec x, PetscInt m, PetscInt n, PetscInt p, PetscInt mstart, PetscInt nstart, PetscInt pstart, PetscScalar ***a[])
4151: {
4152:   void *dummy;

4157:   dummy = (void *)(*a + mstart);
4158:   PetscFree(dummy);
4159:   VecRestoreArrayRead(x, NULL);
4160:   return 0;
4161: }

4163: /*@C
4164:    VecGetArray4dRead - Returns a pointer to a 4d contiguous array that contains this
4165:    processor's portion of the vector data.  You MUST call VecRestoreArray4dRead()
4166:    when you no longer need access to the array.

4168:    Logically Collective

4170:    Input Parameters:
4171: +  x - the vector
4172: .  m - first dimension of four dimensional array
4173: .  n - second dimension of four dimensional array
4174: .  p - third dimension of four dimensional array
4175: .  q - fourth dimension of four dimensional array
4176: .  mstart - first index you will use in first coordinate direction (often 0)
4177: .  nstart - first index in the second coordinate direction (often 0)
4178: .  pstart - first index in the third coordinate direction (often 0)
4179: -  qstart - first index in the fourth coordinate direction (often 0)

4181:    Output Parameter:
4182: .  a - location to put pointer to the array

4184:    Level: beginner

4186:   Notes:
4187:    For a vector obtained from DMCreateLocalVector() mstart, nstart, and pstart are likely
4188:    obtained from the corner indices obtained from DMDAGetGhostCorners() while for
4189:    DMCreateGlobalVector() they are the corner indices from DMDAGetCorners(). In both cases
4190:    the arguments from DMDAGet[Ghost]Corners() are reversed in the call to VecGetArray3d().

4192:    For standard PETSc vectors this is an inexpensive call; it does not copy the vector values.

4194: .seealso: `VecGetArray()`, `VecRestoreArray()`, `VecGetArrays()`, `VecGetArrayF90()`, `VecPlaceArray()`,
4195:           `VecRestoreArray2d()`, `DMDAVecGetarray()`, `DMDAVecRestoreArray()`, `VecGetArray3d()`, `VecRestoreArray3d()`,
4196:           `VecGetArray1d()`, `VecRestoreArray1d()`, `VecGetArray4d()`, `VecRestoreArray4d()`
4197: @*/
4198: PetscErrorCode VecGetArray4dRead(Vec x, PetscInt m, PetscInt n, PetscInt p, PetscInt q, PetscInt mstart, PetscInt nstart, PetscInt pstart, PetscInt qstart, PetscScalar ****a[])
4199: {
4200:   PetscInt           i, N, j, k;
4201:   const PetscScalar *aa;
4202:   PetscScalar     ***b, **c;

4207:   VecGetLocalSize(x, &N);
4209:   VecGetArrayRead(x, &aa);

4211:   PetscMalloc(m * sizeof(PetscScalar ***) + m * n * sizeof(PetscScalar **) + m * n * p * sizeof(PetscScalar *), a);
4212:   b = (PetscScalar ***)((*a) + m);
4213:   c = (PetscScalar **)(b + m * n);
4214:   for (i = 0; i < m; i++) (*a)[i] = b + i * n - nstart;
4215:   for (i = 0; i < m; i++)
4216:     for (j = 0; j < n; j++) b[i * n + j] = c + i * n * p + j * p - pstart;
4217:   for (i = 0; i < m; i++)
4218:     for (j = 0; j < n; j++)
4219:       for (k = 0; k < p; k++) c[i * n * p + j * p + k] = (PetscScalar *)aa + i * n * p * q + j * p * q + k * q - qstart;
4220:   *a -= mstart;
4221:   return 0;
4222: }

4224: /*@C
4225:    VecRestoreArray4dRead - Restores a vector after VecGetArray3d() has been called.

4227:    Logically Collective

4229:    Input Parameters:
4230: +  x - the vector
4231: .  m - first dimension of four dimensional array
4232: .  n - second dimension of the four dimensional array
4233: .  p - third dimension of the four dimensional array
4234: .  q - fourth dimension of the four dimensional array
4235: .  mstart - first index you will use in first coordinate direction (often 0)
4236: .  nstart - first index in the second coordinate direction (often 0)
4237: .  pstart - first index in the third coordinate direction (often 0)
4238: .  qstart - first index in the fourth coordinate direction (often 0)
4239: -  a - location of pointer to array obtained from VecGetArray4dRead()

4241:    Level: beginner

4243:    Notes:
4244:    For regular PETSc vectors this routine does not involve any copies. For
4245:    any special vectors that do not store local vector data in a contiguous
4246:    array, this routine will copy the data back into the underlying
4247:    vector data structure from the array obtained with VecGetArray().

4249:    This routine actually zeros out the a pointer.

4251: .seealso: `VecGetArray()`, `VecRestoreArray()`, `VecRestoreArrays()`, `VecRestoreArrayF90()`, `VecPlaceArray()`,
4252:           `VecGetArray2d()`, `VecGetArray3d()`, `VecRestoreArray3d()`, `DMDAVecGetArray()`, `DMDAVecRestoreArray()`
4253:           `VecGetArray1d()`, `VecRestoreArray1d()`, `VecGetArray4d()`, `VecRestoreArray4d()`, `VecGet`
4254: @*/
4255: PetscErrorCode VecRestoreArray4dRead(Vec x, PetscInt m, PetscInt n, PetscInt p, PetscInt q, PetscInt mstart, PetscInt nstart, PetscInt pstart, PetscInt qstart, PetscScalar ****a[])
4256: {
4257:   void *dummy;

4262:   dummy = (void *)(*a + mstart);
4263:   PetscFree(dummy);
4264:   VecRestoreArrayRead(x, NULL);
4265:   return 0;
4266: }

4268: #if defined(PETSC_USE_DEBUG)

4270: /*@
4271:    VecLockGet  - Gets the current lock status of a vector

4273:    Logically Collective on Vec

4275:    Input Parameter:
4276: .  x - the vector

4278:    Output Parameter:
4279: .  state - greater than zero indicates the vector is locked for read; less then zero indicates the vector is
4280:            locked for write; equal to zero means the vector is unlocked, that is, it is free to read or write.

4282:    Level: beginner

4284: .seealso: `VecRestoreArray()`, `VecGetArrayRead()`, `VecLockReadPush()`, `VecLockReadPop()`
4285: @*/
4286: PetscErrorCode VecLockGet(Vec x, PetscInt *state)
4287: {
4289:   *state = x->lock;
4290:   return 0;
4291: }

4293: PetscErrorCode VecLockGetLocation(Vec x, const char *file[], const char *func[], int *line)
4294: {
4299:   {
4300:     const int index = x->lockstack.currentsize - 1;

4303:     *file = x->lockstack.file[index];
4304:     *func = x->lockstack.function[index];
4305:     *line = x->lockstack.line[index];
4306:   }
4307:   return 0;
4308: }

4310: /*@
4311:    VecLockReadPush  - Pushes a read-only lock on a vector to prevent it from writing

4313:    Logically Collective on Vec

4315:    Input Parameter:
4316: .  x - the vector

4318:    Notes:
4319:     If this is set then calls to VecGetArray() or VecSetValues() or any other routines that change the vectors values will fail.

4321:     The call can be nested, i.e., called multiple times on the same vector, but each VecLockReadPush(x) has to have one matching
4322:     VecLockReadPop(x), which removes the latest read-only lock.

4324:    Level: beginner

4326: .seealso: `VecRestoreArray()`, `VecGetArrayRead()`, `VecLockReadPop()`, `VecLockGet()`
4327: @*/
4328: PetscErrorCode VecLockReadPush(Vec x)
4329: {
4330:   const char *file, *func;
4331:   int         index, line;

4335:   if ((index = petscstack.currentsize - 2) == -1) {
4336:     // vec was locked "outside" of petsc, either in user-land or main. the error message will
4337:     // now show this function as the culprit, but it will include the stacktrace
4338:     file = "unknown user-file";
4339:     func = "unknown_user_function";
4340:     line = 0;
4341:   } else {
4343:     file = petscstack.file[index];
4344:     func = petscstack.function[index];
4345:     line = petscstack.line[index];
4346:   }
4347:   PetscStackPush_Private(x->lockstack, file, func, line, petscstack.petscroutine[index], PETSC_FALSE);
4348:   return 0;
4349: }

4351: /*@
4352:    VecLockReadPop  - Pops a read-only lock from a vector

4354:    Logically Collective on Vec

4356:    Input Parameter:
4357: .  x - the vector

4359:    Level: beginner

4361: .seealso: `VecRestoreArray()`, `VecGetArrayRead()`, `VecLockReadPush()`, `VecLockGet()`
4362: @*/
4363: PetscErrorCode VecLockReadPop(Vec x)
4364: {
4367:   {
4368:     const char *previous = x->lockstack.function[x->lockstack.currentsize - 1];

4370:     PetscStackPop_Private(x->lockstack, previous);
4371:   }
4372:   return 0;
4373: }

4375: /*@C
4376:    VecLockWriteSet  - Lock or unlock a vector for exclusive read/write access

4378:    Logically Collective on Vec

4380:    Input Parameters:
4381: +  x   - the vector
4382: -  flg - PETSC_TRUE to lock the vector for exclusive read/write access; PETSC_FALSE to unlock it.

4384:    Notes:
4385:     The function is usefull in split-phase computations, which usually have a begin phase and an end phase.
4386:     One can call VecLockWriteSet(x,PETSC_TRUE) in the begin phase to lock a vector for exclusive
4387:     access, and call VecLockWriteSet(x,PETSC_FALSE) in the end phase to unlock the vector from exclusive
4388:     access. In this way, one is ensured no other operations can access the vector in between. The code may like

4390:        VecGetArray(x,&xdata); // begin phase
4391:        VecLockWriteSet(v,PETSC_TRUE);

4393:        Other operations, which can not acceess x anymore (they can access xdata, of course)

4395:        VecRestoreArray(x,&vdata); // end phase
4396:        VecLockWriteSet(v,PETSC_FALSE);

4398:     The call can not be nested on the same vector, in other words, one can not call VecLockWriteSet(x,PETSC_TRUE)
4399:     again before calling VecLockWriteSet(v,PETSC_FALSE).

4401:    Level: beginner

4403: .seealso: `VecRestoreArray()`, `VecGetArrayRead()`, `VecLockReadPush()`, `VecLockReadPop()`, `VecLockGet()`
4404: @*/
4405: PetscErrorCode VecLockWriteSet(Vec x, PetscBool flg)
4406: {
4408:   if (flg) {
4411:     x->lock = -1;
4412:   } else {
4414:     x->lock = 0;
4415:   }
4416:   return 0;
4417: }

4419: /*@
4420:    VecLockPush  - Pushes a read-only lock on a vector to prevent it from writing

4422:    Level: deprecated

4424: .seealso: `VecLockReadPush()`
4425: @*/
4426: PetscErrorCode VecLockPush(Vec x)
4427: {
4428:   VecLockReadPush(x);
4429:   return 0;
4430: }

4432: /*@
4433:    VecLockPop  - Pops a read-only lock from a vector

4435:    Level: deprecated

4437: .seealso: `VecLockReadPop()`
4438: @*/
4439: PetscErrorCode VecLockPop(Vec x)
4440: {
4441:   VecLockReadPop(x);
4442:   return 0;
4443: }

4445: #endif