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/vecimpl.h>

  7: PetscInt VecGetSubVectorSavedStateId = -1;

  9: #if PetscDefined(USE_DEBUG)
 10: // this is a no-op '0' macro in optimized builds
 11: PetscErrorCode VecValidValues_Internal(Vec vec, PetscInt argnum, PetscBool begin)
 12: {
 13:   PetscFunctionBegin;
 14:   if (vec->petscnative || vec->ops->getarray) {
 15:     PetscInt           n;
 16:     const PetscScalar *x;
 17:     PetscOffloadMask   mask;

 19:     PetscCall(VecGetOffloadMask(vec, &mask));
 20:     if (!PetscOffloadHost(mask)) PetscFunctionReturn(PETSC_SUCCESS);
 21:     PetscCall(VecGetLocalSize(vec, &n));
 22:     PetscCall(VecGetArrayRead(vec, &x));
 23:     for (PetscInt i = 0; i < n; i++) {
 24:       if (begin) {
 25:         PetscCheck(!PetscIsInfOrNanScalar(x[i]), PETSC_COMM_SELF, PETSC_ERR_FP, "Vec entry at local location %" PetscInt_FMT " is not-a-number or infinite at beginning of function: Parameter number %" PetscInt_FMT, i, argnum);
 26:       } else {
 27:         PetscCheck(!PetscIsInfOrNanScalar(x[i]), PETSC_COMM_SELF, PETSC_ERR_FP, "Vec entry at local location %" PetscInt_FMT " is not-a-number or infinite at end of function: Parameter number %" PetscInt_FMT, i, argnum);
 28:       }
 29:     }
 30:     PetscCall(VecRestoreArrayRead(vec, &x));
 31:   }
 32:   PetscFunctionReturn(PETSC_SUCCESS);
 33: }
 34: #endif

 36: /*@
 37:   VecMaxPointwiseDivide - Computes the maximum of the componentwise division `max = max_i abs(x[i]/y[i])`.

 39:   Logically Collective

 41:   Input Parameters:
 42: + x - the numerators
 43: - y - the denominators

 45:   Output Parameter:
 46: . max - the result

 48:   Level: advanced

 50:   Notes:
 51:   `x` and `y` may be the same vector

 53:   if a particular `y[i]` is zero, it is treated as 1 in the above formula

 55: .seealso: [](ch_vectors), `Vec`, `VecPointwiseDivide()`, `VecPointwiseMult()`, `VecPointwiseMax()`, `VecPointwiseMin()`, `VecPointwiseMaxAbs()`
 56: @*/
 57: PetscErrorCode VecMaxPointwiseDivide(Vec x, Vec y, PetscReal *max)
 58: {
 59:   PetscFunctionBegin;
 62:   PetscAssertPointer(max, 3);
 65:   PetscCheckSameTypeAndComm(x, 1, y, 2);
 66:   VecCheckSameSize(x, 1, y, 2);
 67:   VecCheckAssembled(x);
 68:   VecCheckAssembled(y);
 69:   PetscCall(VecLockReadPush(x));
 70:   PetscCall(VecLockReadPush(y));
 71:   PetscUseTypeMethod(x, maxpointwisedivide, y, max);
 72:   PetscCall(VecLockReadPop(x));
 73:   PetscCall(VecLockReadPop(y));
 74:   PetscFunctionReturn(PETSC_SUCCESS);
 75: }

 77: /*@
 78:   VecDot - Computes the vector dot product.

 80:   Collective

 82:   Input Parameters:
 83: + x - first vector
 84: - y - second vector

 86:   Output Parameter:
 87: . val - the dot product

 89:   Level: intermediate

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

100:   Use `VecTDot()` for the indefinite form
101: .vb
102:   val = (x,y) = y^T x,
103: .ve
104:   where y^T denotes the transpose of y.

106: .seealso: [](ch_vectors), `Vec`, `VecMDot()`, `VecTDot()`, `VecNorm()`, `VecDotBegin()`, `VecDotEnd()`, `VecDotRealPart()`
107: @*/
108: PetscErrorCode VecDot(Vec x, Vec y, PetscScalar *val)
109: {
110:   PetscFunctionBegin;
113:   PetscAssertPointer(val, 3);
116:   PetscCheckSameTypeAndComm(x, 1, y, 2);
117:   VecCheckSameSize(x, 1, y, 2);
118:   VecCheckAssembled(x);
119:   VecCheckAssembled(y);

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

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

134:   Collective

136:   Input Parameters:
137: + x - first vector
138: - y - second vector

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

143:   Level: intermediate

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

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

150:   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
151:   the space R^{2n} (that is a vector of 2n components with the real or imaginary part of the complex numbers for components)

153:   Developer Notes:
154:   This is not currently optimized to compute only the real part of the dot product.

156: .seealso: [](ch_vectors), `Vec`, `VecMDot()`, `VecTDot()`, `VecNorm()`, `VecDotBegin()`, `VecDotEnd()`, `VecDot()`, `VecDotNorm2()`
157: @*/
158: PetscErrorCode VecDotRealPart(Vec x, Vec y, PetscReal *val)
159: {
160:   PetscScalar fdot;

162:   PetscFunctionBegin;
163:   PetscCall(VecDot(x, y, &fdot));
164:   *val = PetscRealPart(fdot);
165:   PetscFunctionReturn(PETSC_SUCCESS);
166: }

168: /*@
169:   VecNorm  - Computes the vector norm.

171:   Collective

173:   Input Parameters:
174: + x    - the vector
175: - type - the type of the norm requested

177:   Output Parameter:
178: . val - the norm

180:   Level: intermediate

182:   Notes:
183:   See `NormType` for descriptions of each norm.

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

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

196: .seealso: [](ch_vectors), `Vec`, `NormType`, `VecDot()`, `VecTDot()`, `VecDotBegin()`, `VecDotEnd()`, `VecNormAvailable()`,
197:           `VecNormBegin()`, `VecNormEnd()`, `NormType()`
198: @*/
199: PetscErrorCode VecNorm(Vec x, NormType type, PetscReal *val)
200: {
201:   PetscBool flg = PETSC_TRUE;

203:   PetscFunctionBegin;
206:   VecCheckAssembled(x);
208:   PetscAssertPointer(val, 3);

210:   PetscCall(VecNormAvailable(x, type, &flg, val));
211:   // check that all MPI processes call this routine together and have same availability
212:   if (PetscDefined(USE_DEBUG)) {
213:     PetscMPIInt b0 = (PetscMPIInt)flg, b1[2], b2[2];
214:     b1[0]          = -b0;
215:     b1[1]          = b0;
216:     PetscCallMPI(MPIU_Allreduce(b1, b2, 2, MPI_INT, MPI_MAX, PetscObjectComm((PetscObject)x)));
217:     PetscCheck(-b2[0] == b2[1], PetscObjectComm((PetscObject)x), PETSC_ERR_ARG_WRONGSTATE, "Some MPI processes have cached %s norm, others do not. This may happen when some MPI processes call VecGetArray() and some others do not.", NormTypes[type]);
218:     if (flg) {
219:       PetscReal b1[2], b2[2];
220:       b1[0] = -(*val);
221:       b1[1] = *val;
222:       PetscCallMPI(MPIU_Allreduce(b1, b2, 2, MPIU_REAL, MPIU_MAX, PetscObjectComm((PetscObject)x)));
223:       PetscCheck((PetscIsNanReal(b2[0]) && PetscIsNanReal(b2[1])) || (-b2[0] == b2[1]), PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Difference in cached %s norms: local %g", NormTypes[type], (double)*val);
224:     }
225:   }
226:   if (flg) PetscFunctionReturn(PETSC_SUCCESS);

228:   PetscCall(VecLockReadPush(x));
229:   PetscCall(PetscLogEventBegin(VEC_Norm, x, 0, 0, 0));
230:   PetscUseTypeMethod(x, norm, type, val);
231:   PetscCall(PetscLogEventEnd(VEC_Norm, x, 0, 0, 0));
232:   PetscCall(VecLockReadPop(x));

234:   if (type != NORM_1_AND_2) PetscCall(PetscObjectComposedDataSetReal((PetscObject)x, NormIds[type], *val));
235:   PetscFunctionReturn(PETSC_SUCCESS);
236: }

238: /*@
239:   VecNormAvailable  - Returns the vector norm if it is already known. That is, it has been previously computed and cached in the vector

241:   Not Collective

243:   Input Parameters:
244: + x    - the vector
245: - type - one of `NORM_1` (sum_i |x[i]|), `NORM_2` sqrt(sum_i (x[i])^2), `NORM_INFINITY` max_i |x[i]|.  Also available
246:           `NORM_1_AND_2`, which computes both norms and stores them
247:           in a two element array.

249:   Output Parameters:
250: + available - `PETSC_TRUE` if the val returned is valid
251: - val       - the norm

253:   Level: intermediate

255:   Developer Notes:
256:   `PETSC_HAVE_SLOW_BLAS_NORM2` will cause a C (loop unrolled) version of the norm to be used, rather
257:   than the BLAS. This should probably only be used when one is using the FORTRAN BLAS routines
258:   (as opposed to vendor provided) because the FORTRAN BLAS `NRM2()` routine is very slow.

260: .seealso: [](ch_vectors), `Vec`, `VecDot()`, `VecTDot()`, `VecNorm()`, `VecDotBegin()`, `VecDotEnd()`,
261:           `VecNormBegin()`, `VecNormEnd()`
262: @*/
263: PetscErrorCode VecNormAvailable(Vec x, NormType type, PetscBool *available, PetscReal *val)
264: {
265:   PetscFunctionBegin;
268:   PetscAssertPointer(available, 3);
269:   PetscAssertPointer(val, 4);

271:   if (type == NORM_1_AND_2) {
272:     *available = PETSC_FALSE;
273:   } else {
274:     PetscCall(PetscObjectComposedDataGetReal((PetscObject)x, NormIds[type], *val, *available));
275:   }
276:   PetscFunctionReturn(PETSC_SUCCESS);
277: }

279: /*@
280:   VecNormalize - Normalizes a vector by its 2-norm.

282:   Collective

284:   Input Parameter:
285: . x - the vector

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

290:   Level: intermediate

292: .seealso: [](ch_vectors), `Vec`, `VecNorm()`, `NORM_2`, `NormType`
293: @*/
294: PetscErrorCode VecNormalize(Vec x, PetscReal *val)
295: {
296:   PetscReal norm;

298:   PetscFunctionBegin;
301:   PetscCall(VecSetErrorIfLocked(x, 1));
302:   if (val) PetscAssertPointer(val, 2);
303:   PetscCall(PetscLogEventBegin(VEC_Normalize, x, 0, 0, 0));
304:   PetscCall(VecNorm(x, NORM_2, &norm));
305:   if (norm == 0.0) PetscCall(PetscInfo(x, "Vector of zero norm can not be normalized; Returning only the zero norm\n"));
306:   else if (PetscIsInfOrNanReal(norm)) PetscCall(PetscInfo(x, "Vector with Inf or Nan norm can not be normalized; Returning only the norm\n"));
307:   else {
308:     PetscScalar s = 1.0 / norm;
309:     PetscCall(VecScale(x, s));
310:   }
311:   PetscCall(PetscLogEventEnd(VEC_Normalize, x, 0, 0, 0));
312:   if (val) *val = norm;
313:   PetscFunctionReturn(PETSC_SUCCESS);
314: }

316: /*@
317:   VecMax - Determines the vector component with maximum real part and its location.

319:   Collective

321:   Input Parameter:
322: . x - the vector

324:   Output Parameters:
325: + p   - the index of `val` (pass `NULL` if you don't want this) in the vector
326: - val - the maximum component

328:   Level: intermediate

330:   Notes:
331:   Returns the value `PETSC_MIN_REAL` and negative `p` if the vector is of length 0.

333:   Returns the smallest index with the maximum value

335:   Developer Note:
336:   The Nag Fortran compiler does not like the symbol name VecMax

338: .seealso: [](ch_vectors), `Vec`, `VecNorm()`, `VecMin()`
339: @*/
340: PetscErrorCode VecMax(Vec x, PetscInt *p, PetscReal *val)
341: {
342:   PetscFunctionBegin;
345:   VecCheckAssembled(x);
346:   if (p) PetscAssertPointer(p, 2);
347:   PetscAssertPointer(val, 3);
348:   PetscCall(VecLockReadPush(x));
349:   PetscCall(PetscLogEventBegin(VEC_Max, x, 0, 0, 0));
350:   PetscUseTypeMethod(x, max, p, val);
351:   PetscCall(PetscLogEventEnd(VEC_Max, x, 0, 0, 0));
352:   PetscCall(VecLockReadPop(x));
353:   PetscFunctionReturn(PETSC_SUCCESS);
354: }

356: /*@
357:   VecMin - Determines the vector component with minimum real part and its location.

359:   Collective

361:   Input Parameter:
362: . x - the vector

364:   Output Parameters:
365: + p   - the index of `val` (pass `NULL` if you don't want this location) in the vector
366: - val - the minimum component

368:   Level: intermediate

370:   Notes:
371:   Returns the value `PETSC_MAX_REAL` and negative `p` if the vector is of length 0.

373:   This returns the smallest index with the minimum value

375:   Developer Note:
376:   The Nag Fortran compiler does not like the symbol name VecMin

378: .seealso: [](ch_vectors), `Vec`, `VecMax()`
379: @*/
380: PetscErrorCode VecMin(Vec x, PetscInt *p, PetscReal *val)
381: {
382:   PetscFunctionBegin;
385:   VecCheckAssembled(x);
386:   if (p) PetscAssertPointer(p, 2);
387:   PetscAssertPointer(val, 3);
388:   PetscCall(VecLockReadPush(x));
389:   PetscCall(PetscLogEventBegin(VEC_Min, x, 0, 0, 0));
390:   PetscUseTypeMethod(x, min, p, val);
391:   PetscCall(PetscLogEventEnd(VEC_Min, x, 0, 0, 0));
392:   PetscCall(VecLockReadPop(x));
393:   PetscFunctionReturn(PETSC_SUCCESS);
394: }

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

400:   Collective

402:   Input Parameters:
403: + x - first vector
404: - y - second vector

406:   Output Parameter:
407: . val - the dot product

409:   Level: intermediate

411:   Notes for Users of Complex Numbers:
412:   For complex vectors, `VecTDot()` computes the indefinite form
413: .vb
414:   val = (x,y) = y^T x,
415: .ve
416:   where y^T denotes the transpose of y.

418:   Use `VecDot()` for the inner product
419: .vb
420:   val = (x,y) = y^H x,
421: .ve
422:   where y^H denotes the conjugate transpose of y.

424: .seealso: [](ch_vectors), `Vec`, `VecDot()`, `VecMTDot()`
425: @*/
426: PetscErrorCode VecTDot(Vec x, Vec y, PetscScalar *val)
427: {
428:   PetscFunctionBegin;
431:   PetscAssertPointer(val, 3);
434:   PetscCheckSameTypeAndComm(x, 1, y, 2);
435:   VecCheckSameSize(x, 1, y, 2);
436:   VecCheckAssembled(x);
437:   VecCheckAssembled(y);

439:   PetscCall(VecLockReadPush(x));
440:   PetscCall(VecLockReadPush(y));
441:   PetscCall(PetscLogEventBegin(VEC_TDot, x, y, 0, 0));
442:   PetscUseTypeMethod(x, tdot, y, val);
443:   PetscCall(PetscLogEventEnd(VEC_TDot, x, y, 0, 0));
444:   PetscCall(VecLockReadPop(x));
445:   PetscCall(VecLockReadPop(y));
446:   PetscFunctionReturn(PETSC_SUCCESS);
447: }

449: PetscErrorCode VecScaleAsync_Private(Vec x, PetscScalar alpha, PetscDeviceContext dctx)
450: {
451:   PetscReal   norms[4];
452:   PetscBool   flgs[4];
453:   PetscScalar one = 1.0;

455:   PetscFunctionBegin;
458:   VecCheckAssembled(x);
459:   PetscCall(VecSetErrorIfLocked(x, 1));
461:   if (alpha == one) PetscFunctionReturn(PETSC_SUCCESS);

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

466:   PetscCall(PetscLogEventBegin(VEC_Scale, x, 0, 0, 0));
467:   VecMethodDispatch(x, dctx, VecAsyncFnName(Scale), scale, (Vec, PetscScalar, PetscDeviceContext), alpha);
468:   PetscCall(PetscLogEventEnd(VEC_Scale, x, 0, 0, 0));

470:   PetscCall(PetscObjectStateIncrease((PetscObject)x));
471:   /* put the scaled stashed norms back into the Vec */
472:   for (PetscInt i = 0; i < 4; i++) {
473:     PetscReal ar = PetscAbsScalar(alpha);
474:     if (flgs[i]) PetscCall(PetscObjectComposedDataSetReal((PetscObject)x, NormIds[i], ar * norms[i]));
475:   }
476:   PetscFunctionReturn(PETSC_SUCCESS);
477: }

479: /*@
480:   VecScale - Scales a vector.

482:   Logically Collective

484:   Input Parameters:
485: + x     - the vector
486: - alpha - the scalar

488:   Level: intermediate

490:   Note:
491:   For a vector with n components, `VecScale()` computes  x[i] = alpha * x[i], for i=1,...,n.

493: .seealso: [](ch_vectors), `Vec`, `VecSet()`
494: @*/
495: PetscErrorCode VecScale(Vec x, PetscScalar alpha)
496: {
497:   PetscFunctionBegin;
498:   PetscCall(VecScaleAsync_Private(x, alpha, NULL));
499:   PetscFunctionReturn(PETSC_SUCCESS);
500: }

502: PetscErrorCode VecSetAsync_Private(Vec x, PetscScalar alpha, PetscDeviceContext dctx)
503: {
504:   PetscFunctionBegin;
507:   VecCheckAssembled(x);
509:   PetscCall(VecSetErrorIfLocked(x, 1));

511:   if (alpha == 0) {
512:     PetscReal norm;
513:     PetscBool set;

515:     PetscCall(VecNormAvailable(x, NORM_2, &set, &norm));
516:     if (set == PETSC_TRUE && norm == 0) PetscFunctionReturn(PETSC_SUCCESS);
517:   }
518:   PetscCall(PetscLogEventBegin(VEC_Set, x, 0, 0, 0));
519:   VecMethodDispatch(x, dctx, VecAsyncFnName(Set), set, (Vec, PetscScalar, PetscDeviceContext), alpha);
520:   PetscCall(PetscLogEventEnd(VEC_Set, x, 0, 0, 0));
521:   PetscCall(PetscObjectStateIncrease((PetscObject)x));

523:   /*  norms can be simply set (if |alpha|*N not too large) */
524:   {
525:     PetscReal      val = PetscAbsScalar(alpha);
526:     const PetscInt N   = x->map->N;

528:     if (N == 0) {
529:       PetscCall(PetscObjectComposedDataSetReal((PetscObject)x, NormIds[NORM_1], 0.0));
530:       PetscCall(PetscObjectComposedDataSetReal((PetscObject)x, NormIds[NORM_INFINITY], 0.0));
531:       PetscCall(PetscObjectComposedDataSetReal((PetscObject)x, NormIds[NORM_2], 0.0));
532:       PetscCall(PetscObjectComposedDataSetReal((PetscObject)x, NormIds[NORM_FROBENIUS], 0.0));
533:     } else if (val > PETSC_MAX_REAL / N) {
534:       PetscCall(PetscObjectComposedDataSetReal((PetscObject)x, NormIds[NORM_INFINITY], val));
535:     } else {
536:       PetscCall(PetscObjectComposedDataSetReal((PetscObject)x, NormIds[NORM_1], N * val));
537:       PetscCall(PetscObjectComposedDataSetReal((PetscObject)x, NormIds[NORM_INFINITY], val));
538:       val *= PetscSqrtReal((PetscReal)N);
539:       PetscCall(PetscObjectComposedDataSetReal((PetscObject)x, NormIds[NORM_2], val));
540:       PetscCall(PetscObjectComposedDataSetReal((PetscObject)x, NormIds[NORM_FROBENIUS], val));
541:     }
542:   }
543:   PetscFunctionReturn(PETSC_SUCCESS);
544: }

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

549:   Logically Collective

551:   Input Parameters:
552: + x     - the vector
553: - alpha - the scalar

555:   Level: beginner

557:   Notes:
558:   For a vector of dimension n, `VecSet()` sets x[i] = alpha, for i=1,...,n,
559:   so that all vector entries then equal the identical
560:   scalar value, `alpha`.  Use the more general routine
561:   `VecSetValues()` to set different vector entries.

563:   You CANNOT call this after you have called `VecSetValues()` but before you call
564:   `VecAssemblyBegin()`

566:   If `alpha` is zero and the norm of the vector is known to be zero then this skips the unneeded zeroing process

568: .seealso: [](ch_vectors), `Vec`, `VecSetValues()`, `VecSetValuesBlocked()`, `VecSetRandom()`
569: @*/
570: PetscErrorCode VecSet(Vec x, PetscScalar alpha)
571: {
572:   PetscFunctionBegin;
573:   PetscCall(VecSetAsync_Private(x, alpha, NULL));
574:   PetscFunctionReturn(PETSC_SUCCESS);
575: }

577: PetscErrorCode VecAXPYAsync_Private(Vec y, PetscScalar alpha, Vec x, PetscDeviceContext dctx)
578: {
579:   PetscFunctionBegin;
584:   PetscCheckSameTypeAndComm(x, 3, y, 1);
585:   VecCheckSameSize(x, 3, y, 1);
586:   VecCheckAssembled(x);
587:   VecCheckAssembled(y);
589:   if (alpha == (PetscScalar)0.0) PetscFunctionReturn(PETSC_SUCCESS);
590:   PetscCall(VecSetErrorIfLocked(y, 1));
591:   if (x == y) {
592:     PetscCall(VecScale(y, alpha + 1.0));
593:     PetscFunctionReturn(PETSC_SUCCESS);
594:   }
595:   PetscCall(VecLockReadPush(x));
596:   PetscCall(PetscLogEventBegin(VEC_AXPY, x, y, 0, 0));
597:   VecMethodDispatch(y, dctx, VecAsyncFnName(AXPY), axpy, (Vec, PetscScalar, Vec, PetscDeviceContext), alpha, x);
598:   PetscCall(PetscLogEventEnd(VEC_AXPY, x, y, 0, 0));
599:   PetscCall(VecLockReadPop(x));
600:   PetscCall(PetscObjectStateIncrease((PetscObject)y));
601:   PetscFunctionReturn(PETSC_SUCCESS);
602: }
603: /*@
604:   VecAXPY - Computes `y = alpha x + y`.

606:   Logically Collective

608:   Input Parameters:
609: + alpha - the scalar
610: . x     - vector scale by `alpha`
611: - y     - vector accumulated into

613:   Output Parameter:
614: . y - output vector

616:   Level: intermediate

618:   Notes:
619:   This routine is optimized for alpha of 0.0, otherwise it calls the BLAS routine
620: .vb
621:     VecAXPY(y,alpha,x)                   y = alpha x           +      y
622:     VecAYPX(y,beta,x)                    y =       x           + beta y
623:     VecAXPBY(y,alpha,beta,x)             y = alpha x           + beta y
624:     VecWAXPY(w,alpha,x,y)                w = alpha x           +      y
625:     VecAXPBYPCZ(z,alpha,beta,gamma,x,y)  z = alpha x           + beta y + gamma z
626:     VecMAXPY(y,nv,alpha[],x[])           y = sum alpha[i] x[i] +      y
627: .ve

629: .seealso: [](ch_vectors), `Vec`, `VecAYPX()`, `VecMAXPY()`, `VecWAXPY()`, `VecAXPBYPCZ()`, `VecAXPBY()`
630: @*/
631: PetscErrorCode VecAXPY(Vec y, PetscScalar alpha, Vec x)
632: {
633:   PetscFunctionBegin;
634:   PetscCall(VecAXPYAsync_Private(y, alpha, x, NULL));
635:   PetscFunctionReturn(PETSC_SUCCESS);
636: }

638: PetscErrorCode VecAYPXAsync_Private(Vec y, PetscScalar beta, Vec x, PetscDeviceContext dctx)
639: {
640:   PetscFunctionBegin;
645:   PetscCheckSameTypeAndComm(x, 3, y, 1);
646:   VecCheckSameSize(x, 1, y, 3);
647:   VecCheckAssembled(x);
648:   VecCheckAssembled(y);
650:   PetscCall(VecSetErrorIfLocked(y, 1));
651:   if (x == y) {
652:     PetscCall(VecScale(y, beta + 1.0));
653:     PetscFunctionReturn(PETSC_SUCCESS);
654:   }
655:   PetscCall(VecLockReadPush(x));
656:   if (beta == (PetscScalar)0.0) {
657:     PetscCall(VecCopy(x, y));
658:   } else {
659:     PetscCall(PetscLogEventBegin(VEC_AYPX, x, y, 0, 0));
660:     VecMethodDispatch(y, dctx, VecAsyncFnName(AYPX), aypx, (Vec, PetscScalar, Vec, PetscDeviceContext), beta, x);
661:     PetscCall(PetscLogEventEnd(VEC_AYPX, x, y, 0, 0));
662:     PetscCall(PetscObjectStateIncrease((PetscObject)y));
663:   }
664:   PetscCall(VecLockReadPop(x));
665:   PetscFunctionReturn(PETSC_SUCCESS);
666: }

668: /*@
669:   VecAYPX - Computes `y = x + beta y`.

671:   Logically Collective

673:   Input Parameters:
674: + beta - the scalar
675: . x    - the unscaled vector
676: - y    - the vector to be scaled

678:   Output Parameter:
679: . y - output vector

681:   Level: intermediate

683:   Developer Notes:
684:   The implementation is optimized for `beta` of -1.0, 0.0, and 1.0

686: .seealso: [](ch_vectors), `Vec`, `VecMAXPY()`, `VecWAXPY()`, `VecAXPY()`, `VecAXPBYPCZ()`, `VecAXPBY()`
687: @*/
688: PetscErrorCode VecAYPX(Vec y, PetscScalar beta, Vec x)
689: {
690:   PetscFunctionBegin;
691:   PetscCall(VecAYPXAsync_Private(y, beta, x, NULL));
692:   PetscFunctionReturn(PETSC_SUCCESS);
693: }

695: PetscErrorCode VecAXPBYAsync_Private(Vec y, PetscScalar alpha, PetscScalar beta, Vec x, PetscDeviceContext dctx)
696: {
697:   PetscFunctionBegin;
702:   PetscCheckSameTypeAndComm(x, 4, y, 1);
703:   VecCheckSameSize(y, 1, x, 4);
704:   VecCheckAssembled(x);
705:   VecCheckAssembled(y);
708:   if (alpha == (PetscScalar)0.0 && beta == (PetscScalar)1.0) PetscFunctionReturn(PETSC_SUCCESS);
709:   if (x == y) {
710:     PetscCall(VecScale(y, alpha + beta));
711:     PetscFunctionReturn(PETSC_SUCCESS);
712:   }

714:   PetscCall(VecSetErrorIfLocked(y, 1));
715:   PetscCall(VecLockReadPush(x));
716:   PetscCall(PetscLogEventBegin(VEC_AXPY, y, x, 0, 0));
717:   VecMethodDispatch(y, dctx, VecAsyncFnName(AXPBY), axpby, (Vec, PetscScalar, PetscScalar, Vec, PetscDeviceContext), alpha, beta, x);
718:   PetscCall(PetscLogEventEnd(VEC_AXPY, y, x, 0, 0));
719:   PetscCall(PetscObjectStateIncrease((PetscObject)y));
720:   PetscCall(VecLockReadPop(x));
721:   PetscFunctionReturn(PETSC_SUCCESS);
722: }

724: /*@
725:   VecAXPBY - Computes `y = alpha x + beta y`.

727:   Logically Collective

729:   Input Parameters:
730: + alpha - first scalar
731: . beta  - second scalar
732: . x     - the first scaled vector
733: - y     - the second scaled vector

735:   Output Parameter:
736: . y - output vector

738:   Level: intermediate

740:   Developer Notes:
741:   The implementation is optimized for `alpha` and/or `beta` values of 0.0 and 1.0

743: .seealso: [](ch_vectors), `Vec`, `VecAYPX()`, `VecMAXPY()`, `VecWAXPY()`, `VecAXPY()`, `VecAXPBYPCZ()`
744: @*/
745: PetscErrorCode VecAXPBY(Vec y, PetscScalar alpha, PetscScalar beta, Vec x)
746: {
747:   PetscFunctionBegin;
748:   PetscCall(VecAXPBYAsync_Private(y, alpha, beta, x, NULL));
749:   PetscFunctionReturn(PETSC_SUCCESS);
750: }

752: PetscErrorCode VecAXPBYPCZAsync_Private(Vec z, PetscScalar alpha, PetscScalar beta, PetscScalar gamma, Vec x, Vec y, PetscDeviceContext dctx)
753: {
754:   PetscFunctionBegin;
761:   PetscCheckSameTypeAndComm(x, 5, y, 6);
762:   PetscCheckSameTypeAndComm(x, 5, z, 1);
763:   VecCheckSameSize(x, 5, y, 6);
764:   VecCheckSameSize(x, 5, z, 1);
765:   PetscCheck(x != y && x != z, PetscObjectComm((PetscObject)x), PETSC_ERR_ARG_IDN, "x, y, and z must be different vectors");
766:   PetscCheck(y != z, PetscObjectComm((PetscObject)y), PETSC_ERR_ARG_IDN, "x, y, and z must be different vectors");
767:   VecCheckAssembled(x);
768:   VecCheckAssembled(y);
769:   VecCheckAssembled(z);
773:   if (alpha == (PetscScalar)0.0 && beta == (PetscScalar)0.0 && gamma == (PetscScalar)1.0) PetscFunctionReturn(PETSC_SUCCESS);

775:   PetscCall(VecSetErrorIfLocked(z, 1));
776:   PetscCall(VecLockReadPush(x));
777:   PetscCall(VecLockReadPush(y));
778:   PetscCall(PetscLogEventBegin(VEC_AXPBYPCZ, x, y, z, 0));
779:   VecMethodDispatch(z, dctx, VecAsyncFnName(AXPBYPCZ), axpbypcz, (Vec, PetscScalar, PetscScalar, PetscScalar, Vec, Vec, PetscDeviceContext), alpha, beta, gamma, x, y);
780:   PetscCall(PetscLogEventEnd(VEC_AXPBYPCZ, x, y, z, 0));
781:   PetscCall(PetscObjectStateIncrease((PetscObject)z));
782:   PetscCall(VecLockReadPop(x));
783:   PetscCall(VecLockReadPop(y));
784:   PetscFunctionReturn(PETSC_SUCCESS);
785: }
786: /*@
787:   VecAXPBYPCZ - Computes `z = alpha x + beta y + gamma z`

789:   Logically Collective

791:   Input Parameters:
792: + alpha - first scalar
793: . beta  - second scalar
794: . gamma - third scalar
795: . x     - first vector
796: . y     - second vector
797: - z     - third vector

799:   Output Parameter:
800: . z - output vector

802:   Level: intermediate

804:   Note:
805:   `x`, `y` and `z` must be different vectors

807:   Developer Notes:
808:   The implementation is optimized for `alpha` of 1.0 and `gamma` of 1.0 or 0.0

810: .seealso: [](ch_vectors), `Vec`, `VecAYPX()`, `VecMAXPY()`, `VecWAXPY()`, `VecAXPY()`, `VecAXPBY()`
811: @*/
812: PetscErrorCode VecAXPBYPCZ(Vec z, PetscScalar alpha, PetscScalar beta, PetscScalar gamma, Vec x, Vec y)
813: {
814:   PetscFunctionBegin;
815:   PetscCall(VecAXPBYPCZAsync_Private(z, alpha, beta, gamma, x, y, NULL));
816:   PetscFunctionReturn(PETSC_SUCCESS);
817: }

819: PetscErrorCode VecWAXPYAsync_Private(Vec w, PetscScalar alpha, Vec x, Vec y, PetscDeviceContext dctx)
820: {
821:   PetscFunctionBegin;
828:   PetscCheckSameTypeAndComm(x, 3, y, 4);
829:   PetscCheckSameTypeAndComm(y, 4, w, 1);
830:   VecCheckSameSize(x, 3, y, 4);
831:   VecCheckSameSize(x, 3, w, 1);
832:   PetscCheck(w != y, PETSC_COMM_SELF, PETSC_ERR_SUP, "Result vector w cannot be same as input vector y, suggest VecAXPY()");
833:   PetscCheck(w != x, PETSC_COMM_SELF, PETSC_ERR_SUP, "Result vector w cannot be same as input vector x, suggest VecAYPX()");
834:   VecCheckAssembled(x);
835:   VecCheckAssembled(y);
837:   PetscCall(VecSetErrorIfLocked(w, 1));

839:   PetscCall(VecLockReadPush(x));
840:   PetscCall(VecLockReadPush(y));
841:   if (alpha == (PetscScalar)0.0) {
842:     PetscCall(VecCopyAsync_Private(y, w, dctx));
843:   } else {
844:     PetscCall(PetscLogEventBegin(VEC_WAXPY, x, y, w, 0));
845:     VecMethodDispatch(w, dctx, VecAsyncFnName(WAXPY), waxpy, (Vec, PetscScalar, Vec, Vec, PetscDeviceContext), alpha, x, y);
846:     PetscCall(PetscLogEventEnd(VEC_WAXPY, x, y, w, 0));
847:     PetscCall(PetscObjectStateIncrease((PetscObject)w));
848:   }
849:   PetscCall(VecLockReadPop(x));
850:   PetscCall(VecLockReadPop(y));
851:   PetscFunctionReturn(PETSC_SUCCESS);
852: }

854: /*@
855:   VecWAXPY - Computes `w = alpha x + y`.

857:   Logically Collective

859:   Input Parameters:
860: + alpha - the scalar
861: . x     - first vector, multiplied by `alpha`
862: - y     - second vector

864:   Output Parameter:
865: . w - the result

867:   Level: intermediate

869:   Note:
870:   `w` cannot be either `x` or `y`, but `x` and `y` can be the same

872:   Developer Notes:
873:   The implementation is optimized for alpha of -1.0, 0.0, and 1.0

875: .seealso: [](ch_vectors), `Vec`, `VecAXPY()`, `VecAYPX()`, `VecAXPBY()`, `VecMAXPY()`, `VecAXPBYPCZ()`
876: @*/
877: PetscErrorCode VecWAXPY(Vec w, PetscScalar alpha, Vec x, Vec y)
878: {
879:   PetscFunctionBegin;
880:   PetscCall(VecWAXPYAsync_Private(w, alpha, x, y, NULL));
881:   PetscFunctionReturn(PETSC_SUCCESS);
882: }

884: /*@
885:   VecSetValues - Inserts or adds values into certain locations of a vector.

887:   Not Collective

889:   Input Parameters:
890: + x    - vector to insert in
891: . ni   - number of elements to add
892: . ix   - indices where to add
893: . y    - array of values
894: - iora - either `INSERT_VALUES` to replace the current values or `ADD_VALUES` to add values to any existing entries

896:   Level: beginner

898:   Notes:
899: .vb
900:    `VecSetValues()` sets x[ix[i]] = y[i], for i=0,...,ni-1.
901: .ve

903:   Calls to `VecSetValues()` with the `INSERT_VALUES` and `ADD_VALUES`
904:   options cannot be mixed without intervening calls to the assembly
905:   routines.

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

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

912:   If you call `VecSetOption`(x, `VEC_IGNORE_NEGATIVE_INDICES`,`PETSC_TRUE`),
913:   negative indices may be passed in ix. These rows are
914:   simply ignored. This allows easily inserting element load matrices
915:   with homogeneous Dirichlet boundary conditions that you don't want represented
916:   in the vector.

918:   Fortran Note:
919:   If any of `ix` and `y` are scalars pass them using, for example,
920: .vb
921:   call VecSetValues(mat, one, [ix], [y], INSERT_VALUES, ierr)
922: .ve

924: .seealso: [](ch_vectors), `Vec`, `VecAssemblyBegin()`, `VecAssemblyEnd()`, `VecSetValuesLocal()`,
925:           `VecSetValue()`, `VecSetValuesBlocked()`, `InsertMode`, `INSERT_VALUES`, `ADD_VALUES`, `VecGetValues()`
926: @*/
927: PetscErrorCode VecSetValues(Vec x, PetscInt ni, const PetscInt ix[], const PetscScalar y[], InsertMode iora)
928: {
929:   PetscFunctionBeginHot;
931:   if (!ni) PetscFunctionReturn(PETSC_SUCCESS);
932:   PetscAssertPointer(ix, 3);
933:   PetscAssertPointer(y, 4);

936:   PetscCall(PetscLogEventBegin(VEC_SetValues, x, 0, 0, 0));
937:   PetscUseTypeMethod(x, setvalues, ni, ix, y, iora);
938:   PetscCall(PetscLogEventEnd(VEC_SetValues, x, 0, 0, 0));
939:   PetscCall(PetscObjectStateIncrease((PetscObject)x));
940:   PetscFunctionReturn(PETSC_SUCCESS);
941: }

943: /*@
944:   VecGetValues - Gets values from certain locations of a vector. Currently
945:   can only get values on the same processor on which they are owned

947:   Not Collective

949:   Input Parameters:
950: + x  - vector to get values from
951: . ni - number of elements to get
952: - ix - indices where to get them from (in global 1d numbering)

954:   Output Parameter:
955: . y - array of values, must be passed in with a length of `ni`

957:   Level: beginner

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

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

964:   `VecAssemblyBegin()` and `VecAssemblyEnd()`  MUST be called before calling this if `VecSetValues()` or related routine has been called

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

968:   If you call `VecSetOption`(x, `VEC_IGNORE_NEGATIVE_INDICES`,`PETSC_TRUE`),
969:   negative indices may be passed in ix. These rows are
970:   simply ignored.

972: .seealso: [](ch_vectors), `Vec`, `VecAssemblyBegin()`, `VecAssemblyEnd()`, `VecSetValues()`
973: @*/
974: PetscErrorCode VecGetValues(Vec x, PetscInt ni, const PetscInt ix[], PetscScalar y[])
975: {
976:   PetscFunctionBegin;
978:   if (!ni) PetscFunctionReturn(PETSC_SUCCESS);
979:   PetscAssertPointer(ix, 3);
980:   PetscAssertPointer(y, 4);
982:   VecCheckAssembled(x);
983:   PetscUseTypeMethod(x, getvalues, ni, ix, y);
984:   PetscFunctionReturn(PETSC_SUCCESS);
985: }

987: /*@
988:   VecSetValuesBlocked - Inserts or adds blocks of values into certain locations of a vector.

990:   Not Collective

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

999:   Level: intermediate

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

1005:   Calls to `VecSetValuesBlocked()` 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 `VecSetValuesBlocked()` have been completed.

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

1014:   Negative indices may be passed in ix, these rows are
1015:   simply ignored. This allows easily inserting element load matrices
1016:   with homogeneous Dirichlet boundary conditions that you don't want represented
1017:   in the vector.

1019:   Fortran Note:
1020:   If any of `ix` and `y` are scalars pass them using, for example,
1021: .vb
1022:   call VecSetValuesBlocked(mat, one, [ix], [y], INSERT_VALUES, ierr)
1023: .ve

1025: .seealso: [](ch_vectors), `Vec`, `VecAssemblyBegin()`, `VecAssemblyEnd()`, `VecSetValuesBlockedLocal()`,
1026:           `VecSetValues()`
1027: @*/
1028: PetscErrorCode VecSetValuesBlocked(Vec x, PetscInt ni, const PetscInt ix[], const PetscScalar y[], InsertMode iora)
1029: {
1030:   PetscFunctionBeginHot;
1032:   if (!ni) PetscFunctionReturn(PETSC_SUCCESS);
1033:   PetscAssertPointer(ix, 3);
1034:   PetscAssertPointer(y, 4);

1037:   PetscCall(PetscLogEventBegin(VEC_SetValues, x, 0, 0, 0));
1038:   PetscUseTypeMethod(x, setvaluesblocked, ni, ix, y, iora);
1039:   PetscCall(PetscLogEventEnd(VEC_SetValues, x, 0, 0, 0));
1040:   PetscCall(PetscObjectStateIncrease((PetscObject)x));
1041:   PetscFunctionReturn(PETSC_SUCCESS);
1042: }

1044: /*@
1045:   VecSetValuesLocal - Inserts or adds values into certain locations of a vector,
1046:   using a local ordering of the nodes.

1048:   Not Collective

1050:   Input Parameters:
1051: + x    - vector to insert in
1052: . ni   - number of elements to add
1053: . ix   - indices where to add
1054: . y    - array of values
1055: - iora - either `INSERT_VALUES` replaces existing entries with new values, `ADD_VALUES` adds values to any existing entries

1057:   Level: intermediate

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

1062:   Calls to `VecSetValuesLocal()` with the `INSERT_VALUES` and `ADD_VALUES`
1063:   options cannot be mixed without intervening calls to the assembly
1064:   routines.

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

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

1071:   Fortran Note:
1072:   If any of `ix` and `y` are scalars pass them using, for example,
1073: .vb
1074:   call VecSetValuesLocal(mat, one, [ix], [y], INSERT_VALUES, ierr)
1075: .ve

1077: .seealso: [](ch_vectors), `Vec`, `VecAssemblyBegin()`, `VecAssemblyEnd()`, `VecSetValues()`, `VecSetLocalToGlobalMapping()`,
1078:           `VecSetValuesBlockedLocal()`
1079: @*/
1080: PetscErrorCode VecSetValuesLocal(Vec x, PetscInt ni, const PetscInt ix[], const PetscScalar y[], InsertMode iora)
1081: {
1082:   PetscInt lixp[128], *lix = lixp;

1084:   PetscFunctionBeginHot;
1086:   if (!ni) PetscFunctionReturn(PETSC_SUCCESS);
1087:   PetscAssertPointer(ix, 3);
1088:   PetscAssertPointer(y, 4);

1091:   PetscCall(PetscLogEventBegin(VEC_SetValues, x, 0, 0, 0));
1092:   if (!x->ops->setvalueslocal) {
1093:     if (PetscUnlikely(!x->map->mapping && x->ops->getlocaltoglobalmapping)) PetscUseTypeMethod(x, getlocaltoglobalmapping, &x->map->mapping);
1094:     if (x->map->mapping) {
1095:       if (ni > 128) PetscCall(PetscMalloc1(ni, &lix));
1096:       PetscCall(ISLocalToGlobalMappingApply(x->map->mapping, ni, (PetscInt *)ix, lix));
1097:       PetscUseTypeMethod(x, setvalues, ni, lix, y, iora);
1098:       if (ni > 128) PetscCall(PetscFree(lix));
1099:     } else PetscUseTypeMethod(x, setvalues, ni, ix, y, iora);
1100:   } else PetscUseTypeMethod(x, setvalueslocal, ni, ix, y, iora);
1101:   PetscCall(PetscLogEventEnd(VEC_SetValues, x, 0, 0, 0));
1102:   PetscCall(PetscObjectStateIncrease((PetscObject)x));
1103:   PetscFunctionReturn(PETSC_SUCCESS);
1104: }

1106: /*@
1107:   VecSetValuesBlockedLocal - Inserts or adds values into certain locations of a vector,
1108:   using a local ordering of the nodes.

1110:   Not Collective

1112:   Input Parameters:
1113: + x    - vector to insert in
1114: . ni   - number of blocks to add
1115: . ix   - indices where to add in block count, not element count
1116: . y    - array of values
1117: - iora - either `INSERT_VALUES` replaces existing entries with new values, `ADD_VALUES` adds values to any existing entries

1119:   Level: intermediate

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

1125:   Calls to `VecSetValuesBlockedLocal()` with the `INSERT_VALUES` and `ADD_VALUES`
1126:   options cannot be mixed without intervening calls to the assembly
1127:   routines.

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

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

1134:   Fortran Note:
1135:   If any of `ix` and `y` are scalars pass them using, for example,
1136: .vb
1137:   call VecSetValuesBlockedLocal(mat, one, [ix], [y], INSERT_VALUES, ierr)
1138: .ve

1140: .seealso: [](ch_vectors), `Vec`, `VecAssemblyBegin()`, `VecAssemblyEnd()`, `VecSetValues()`, `VecSetValuesBlocked()`,
1141:           `VecSetLocalToGlobalMapping()`
1142: @*/
1143: PetscErrorCode VecSetValuesBlockedLocal(Vec x, PetscInt ni, const PetscInt ix[], const PetscScalar y[], InsertMode iora)
1144: {
1145:   PetscInt lixp[128], *lix = lixp;

1147:   PetscFunctionBeginHot;
1149:   if (!ni) PetscFunctionReturn(PETSC_SUCCESS);
1150:   PetscAssertPointer(ix, 3);
1151:   PetscAssertPointer(y, 4);
1153:   PetscCall(PetscLogEventBegin(VEC_SetValues, x, 0, 0, 0));
1154:   if (PetscUnlikely(!x->map->mapping && x->ops->getlocaltoglobalmapping)) PetscUseTypeMethod(x, getlocaltoglobalmapping, &x->map->mapping);
1155:   if (x->map->mapping) {
1156:     if (ni > 128) PetscCall(PetscMalloc1(ni, &lix));
1157:     PetscCall(ISLocalToGlobalMappingApplyBlock(x->map->mapping, ni, (PetscInt *)ix, lix));
1158:     PetscUseTypeMethod(x, setvaluesblocked, ni, lix, y, iora);
1159:     if (ni > 128) PetscCall(PetscFree(lix));
1160:   } else {
1161:     PetscUseTypeMethod(x, setvaluesblocked, ni, ix, y, iora);
1162:   }
1163:   PetscCall(PetscLogEventEnd(VEC_SetValues, x, 0, 0, 0));
1164:   PetscCall(PetscObjectStateIncrease((PetscObject)x));
1165:   PetscFunctionReturn(PETSC_SUCCESS);
1166: }

1168: static PetscErrorCode VecMXDot_Private(Vec x, PetscInt nv, const Vec y[], PetscScalar result[], PetscErrorCode (*mxdot)(Vec, PetscInt, const Vec[], PetscScalar[]), PetscLogEvent event)
1169: {
1170:   PetscFunctionBegin;
1173:   VecCheckAssembled(x);
1175:   if (!nv) PetscFunctionReturn(PETSC_SUCCESS);
1176:   PetscAssertPointer(y, 3);
1177:   for (PetscInt i = 0; i < nv; ++i) {
1180:     PetscCheckSameTypeAndComm(x, 1, y[i], 3);
1181:     VecCheckSameSize(x, 1, y[i], 3);
1182:     VecCheckAssembled(y[i]);
1183:     PetscCall(VecLockReadPush(y[i]));
1184:   }
1185:   PetscAssertPointer(result, 4);

1188:   PetscCall(VecLockReadPush(x));
1189:   PetscCall(PetscLogEventBegin(event, x, *y, 0, 0));
1190:   PetscCall((*mxdot)(x, nv, y, result));
1191:   PetscCall(PetscLogEventEnd(event, x, *y, 0, 0));
1192:   PetscCall(VecLockReadPop(x));
1193:   for (PetscInt i = 0; i < nv; ++i) PetscCall(VecLockReadPop(y[i]));
1194:   PetscFunctionReturn(PETSC_SUCCESS);
1195: }

1197: /*@
1198:   VecMTDot - Computes indefinite vector multiple dot products.
1199:   That is, it does NOT use the complex conjugate.

1201:   Collective

1203:   Input Parameters:
1204: + x  - one vector
1205: . nv - number of vectors
1206: - y  - array of vectors.  Note that vectors are pointers

1208:   Output Parameter:
1209: . val - array of the dot products

1211:   Level: intermediate

1213:   Notes for Users of Complex Numbers:
1214:   For complex vectors, `VecMTDot()` computes the indefinite form
1215: .vb
1216:   val = (x,y) = y^T x,
1217: .ve
1218:   where y^T denotes the transpose of y.

1220:   Use `VecMDot()` for the inner product
1221: .vb
1222:   val = (x,y) = y^H x,
1223: .ve
1224:   where y^H denotes the conjugate transpose of y.

1226: .seealso: [](ch_vectors), `Vec`, `VecMDot()`, `VecTDot()`
1227: @*/
1228: PetscErrorCode VecMTDot(Vec x, PetscInt nv, const Vec y[], PetscScalar val[])
1229: {
1230:   PetscFunctionBegin;
1232:   PetscCall(VecMXDot_Private(x, nv, y, val, x->ops->mtdot, VEC_MTDot));
1233:   PetscFunctionReturn(PETSC_SUCCESS);
1234: }

1236: /*@
1237:   VecMDot - Computes multiple vector dot products.

1239:   Collective

1241:   Input Parameters:
1242: + x  - one vector
1243: . nv - number of vectors
1244: - y  - array of vectors.

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

1249:   Level: intermediate

1251:   Notes for Users of Complex Numbers:
1252:   For complex vectors, `VecMDot()` computes
1253: .vb
1254:   val = (x,y) = y^H x,
1255: .ve
1256:   where y^H denotes the conjugate transpose of y.

1258:   Use `VecMTDot()` for the indefinite form
1259: .vb
1260:   val = (x,y) = y^T x,
1261: .ve
1262:   where y^T denotes the transpose of y.

1264:   Note:
1265:   The implementation may use BLAS 2 operations when the vectors `y` have been obtained with `VecDuplicateVecs()`

1267: .seealso: [](ch_vectors), `Vec`, `VecMTDot()`, `VecDot()`, `VecDuplicateVecs()`
1268: @*/
1269: PetscErrorCode VecMDot(Vec x, PetscInt nv, const Vec y[], PetscScalar val[])
1270: {
1271:   PetscFunctionBegin;
1273:   PetscCall(VecMXDot_Private(x, nv, y, val, x->ops->mdot, VEC_MDot));
1274:   PetscFunctionReturn(PETSC_SUCCESS);
1275: }

1277: PetscErrorCode VecMAXPYAsync_Private(Vec y, PetscInt nv, const PetscScalar alpha[], Vec x[], PetscDeviceContext dctx)
1278: {
1279:   PetscFunctionBegin;
1281:   VecCheckAssembled(y);
1283:   PetscCall(VecSetErrorIfLocked(y, 1));
1284:   PetscCheck(nv >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of vectors (given %" PetscInt_FMT ") cannot be negative", nv);
1285:   if (nv) {
1286:     PetscInt zeros = 0;

1288:     PetscAssertPointer(alpha, 3);
1289:     PetscAssertPointer(x, 4);
1290:     for (PetscInt i = 0; i < nv; ++i) {
1294:       PetscCheckSameTypeAndComm(y, 1, x[i], 4);
1295:       VecCheckSameSize(y, 1, x[i], 4);
1296:       PetscCheck(y != x[i], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Array of vectors 'x' cannot contain y, found x[%" PetscInt_FMT "] == y", i);
1297:       VecCheckAssembled(x[i]);
1298:       PetscCall(VecLockReadPush(x[i]));
1299:       zeros += alpha[i] == (PetscScalar)0.0;
1300:     }

1302:     if (zeros < nv) {
1303:       PetscCall(PetscLogEventBegin(VEC_MAXPY, y, *x, 0, 0));
1304:       VecMethodDispatch(y, dctx, VecAsyncFnName(MAXPY), maxpy, (Vec, PetscInt, const PetscScalar[], Vec[], PetscDeviceContext), nv, alpha, x);
1305:       PetscCall(PetscLogEventEnd(VEC_MAXPY, y, *x, 0, 0));
1306:       PetscCall(PetscObjectStateIncrease((PetscObject)y));
1307:     }

1309:     for (PetscInt i = 0; i < nv; ++i) PetscCall(VecLockReadPop(x[i]));
1310:   }
1311:   PetscFunctionReturn(PETSC_SUCCESS);
1312: }

1314: /*@
1315:   VecMAXPY - Computes `y = y + sum alpha[i] x[i]`

1317:   Logically Collective

1319:   Input Parameters:
1320: + nv    - number of scalars and `x` vectors
1321: . alpha - array of scalars
1322: . y     - one vector
1323: - x     - array of vectors

1325:   Level: intermediate

1327:   Notes:
1328:   `y` cannot be any of the `x` vectors

1330:   The implementation may use BLAS 2 operations when the vectors `y` have been obtained with `VecDuplicateVecs()`

1332: .seealso: [](ch_vectors), `Vec`, `VecMAXPBY()`,`VecAYPX()`, `VecWAXPY()`, `VecAXPY()`, `VecAXPBYPCZ()`, `VecAXPBY()`, `VecDuplicateVecs()`
1333: @*/
1334: PetscErrorCode VecMAXPY(Vec y, PetscInt nv, const PetscScalar alpha[], Vec x[])
1335: {
1336:   PetscFunctionBegin;
1337:   PetscCall(VecMAXPYAsync_Private(y, nv, alpha, x, NULL));
1338:   PetscFunctionReturn(PETSC_SUCCESS);
1339: }

1341: /*@
1342:   VecMAXPBY - Computes `y = beta y + sum alpha[i] x[i]`

1344:   Logically Collective

1346:   Input Parameters:
1347: + nv    - number of scalars and `x` vectors
1348: . alpha - array of scalars
1349: . beta  - scalar
1350: . y     - one vector
1351: - x     - array of vectors

1353:   Level: intermediate

1355:   Note:
1356:   `y` cannot be any of the `x` vectors.

1358:   Developer Notes:
1359:   This is a convenience routine, but implementations might be able to optimize it, for example, when `beta` is zero.

1361: .seealso: [](ch_vectors), `Vec`, `VecMAXPY()`, `VecAYPX()`, `VecWAXPY()`, `VecAXPY()`, `VecAXPBYPCZ()`, `VecAXPBY()`
1362: @*/
1363: PetscErrorCode VecMAXPBY(Vec y, PetscInt nv, const PetscScalar alpha[], PetscScalar beta, Vec x[])
1364: {
1365:   PetscFunctionBegin;
1367:   VecCheckAssembled(y);
1369:   PetscCall(VecSetErrorIfLocked(y, 1));
1370:   PetscCheck(nv >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of vectors (given %" PetscInt_FMT ") cannot be negative", nv);

1373:   if (y->ops->maxpby) {
1374:     PetscInt zeros = 0;

1376:     if (nv) {
1377:       PetscAssertPointer(alpha, 3);
1378:       PetscAssertPointer(x, 5);
1379:     }

1381:     for (PetscInt i = 0; i < nv; ++i) { // scan all alpha[]
1385:       PetscCheckSameTypeAndComm(y, 1, x[i], 5);
1386:       VecCheckSameSize(y, 1, x[i], 5);
1387:       PetscCheck(y != x[i], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Array of vectors 'x' cannot contain y, found x[%" PetscInt_FMT "] == y", i);
1388:       VecCheckAssembled(x[i]);
1389:       PetscCall(VecLockReadPush(x[i]));
1390:       zeros += alpha[i] == (PetscScalar)0.0;
1391:     }

1393:     if (zeros < nv) { // has nonzero alpha
1394:       PetscCall(PetscLogEventBegin(VEC_MAXPY, y, *x, 0, 0));
1395:       PetscUseTypeMethod(y, maxpby, nv, alpha, beta, x);
1396:       PetscCall(PetscLogEventEnd(VEC_MAXPY, y, *x, 0, 0));
1397:       PetscCall(PetscObjectStateIncrease((PetscObject)y));
1398:     } else {
1399:       PetscCall(VecScale(y, beta));
1400:     }

1402:     for (PetscInt i = 0; i < nv; ++i) PetscCall(VecLockReadPop(x[i]));
1403:   } else { // no maxpby
1404:     if (beta == 0.0) PetscCall(VecSet(y, 0.0));
1405:     else PetscCall(VecScale(y, beta));
1406:     PetscCall(VecMAXPY(y, nv, alpha, x));
1407:   }
1408:   PetscFunctionReturn(PETSC_SUCCESS);
1409: }

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

1416:   Collective

1418:   Input Parameters:
1419: + nx - number of vectors to be concatenated
1420: - X  - array containing the vectors to be concatenated in the order of concatenation

1422:   Output Parameters:
1423: + Y    - concatenated vector
1424: - x_is - array of index sets corresponding to the concatenated components of `Y` (pass `NULL` if not needed)

1426:   Level: advanced

1428:   Notes:
1429:   Concatenation is similar to the functionality of a `VECNEST` object; they both represent combination of
1430:   different vector spaces. However, concatenated vectors do not store any information about their
1431:   sub-vectors and own their own data. Consequently, this function provides index sets to enable the
1432:   manipulation of data in the concatenated vector that corresponds to the original components at creation.

1434:   This is a useful tool for outer loop algorithms, particularly constrained optimizers, where the solver
1435:   has to operate on combined vector spaces and cannot utilize `VECNEST` objects due to incompatibility with
1436:   bound projections.

1438: .seealso: [](ch_vectors), `Vec`, `VECNEST`, `VECSCATTER`, `VecScatterCreate()`
1439: @*/
1440: PetscErrorCode VecConcatenate(PetscInt nx, const Vec X[], Vec *Y, IS *x_is[])
1441: {
1442:   MPI_Comm comm;
1443:   VecType  vec_type;
1444:   Vec      Ytmp, Xtmp;
1445:   IS      *is_tmp;
1446:   PetscInt i, shift = 0, Xnl, Xng, Xbegin;

1448:   PetscFunctionBegin;
1452:   PetscAssertPointer(Y, 3);

1454:   if ((*X)->ops->concatenate) {
1455:     /* use the dedicated concatenation function if available */
1456:     PetscCall((*(*X)->ops->concatenate)(nx, X, Y, x_is));
1457:   } else {
1458:     /* loop over vectors and start creating IS */
1459:     comm = PetscObjectComm((PetscObject)*X);
1460:     PetscCall(VecGetType(*X, &vec_type));
1461:     PetscCall(PetscMalloc1(nx, &is_tmp));
1462:     for (i = 0; i < nx; i++) {
1463:       PetscCall(VecGetSize(X[i], &Xng));
1464:       PetscCall(VecGetLocalSize(X[i], &Xnl));
1465:       PetscCall(VecGetOwnershipRange(X[i], &Xbegin, NULL));
1466:       PetscCall(ISCreateStride(comm, Xnl, shift + Xbegin, 1, &is_tmp[i]));
1467:       shift += Xng;
1468:     }
1469:     /* create the concatenated vector */
1470:     PetscCall(VecCreate(comm, &Ytmp));
1471:     PetscCall(VecSetType(Ytmp, vec_type));
1472:     PetscCall(VecSetSizes(Ytmp, PETSC_DECIDE, shift));
1473:     PetscCall(VecSetUp(Ytmp));
1474:     /* copy data from X array to Y and return */
1475:     for (i = 0; i < nx; i++) {
1476:       PetscCall(VecGetSubVector(Ytmp, is_tmp[i], &Xtmp));
1477:       PetscCall(VecCopy(X[i], Xtmp));
1478:       PetscCall(VecRestoreSubVector(Ytmp, is_tmp[i], &Xtmp));
1479:     }
1480:     *Y = Ytmp;
1481:     if (x_is) {
1482:       *x_is = is_tmp;
1483:     } else {
1484:       for (i = 0; i < nx; i++) PetscCall(ISDestroy(&is_tmp[i]));
1485:       PetscCall(PetscFree(is_tmp));
1486:     }
1487:   }
1488:   PetscFunctionReturn(PETSC_SUCCESS);
1489: }

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

1494:     Input Parameters:
1495: +   X - the original vector
1496: -   is - the index set of the subvector

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

1503: */
1504: PetscErrorCode VecGetSubVectorContiguityAndBS_Private(Vec X, IS is, PetscBool *contig, PetscInt *start, PetscInt *blocksize)
1505: {
1506:   PetscInt  gstart, gend, lstart;
1507:   PetscBool red[2] = {PETSC_TRUE /*contiguous*/, PETSC_TRUE /*validVBS*/};
1508:   PetscInt  n, N, ibs, vbs, bs = 1;

1510:   PetscFunctionBegin;
1511:   PetscCall(ISGetLocalSize(is, &n));
1512:   PetscCall(ISGetSize(is, &N));
1513:   PetscCall(ISGetBlockSize(is, &ibs));
1514:   PetscCall(VecGetBlockSize(X, &vbs));
1515:   PetscCall(VecGetOwnershipRange(X, &gstart, &gend));
1516:   PetscCall(ISContiguousLocal(is, gstart, gend, &lstart, &red[0]));
1517:   /* block size is given by IS if ibs > 1; otherwise, check the vector */
1518:   if (ibs > 1) {
1519:     PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, red, 1, MPIU_BOOL, MPI_LAND, PetscObjectComm((PetscObject)is)));
1520:     bs = ibs;
1521:   } else {
1522:     if (n % vbs || vbs == 1) red[1] = PETSC_FALSE; /* this process invalidate the collectiveness of block size */
1523:     PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, red, 2, MPIU_BOOL, MPI_LAND, PetscObjectComm((PetscObject)is)));
1524:     if (red[0] && red[1]) bs = vbs; /* all processes have a valid block size and the access will be contiguous */
1525:   }

1527:   *contig    = red[0];
1528:   *start     = lstart;
1529:   *blocksize = bs;
1530:   PetscFunctionReturn(PETSC_SUCCESS);
1531: }

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

1535:     Input Parameters:
1536: +   X - the original vector
1537: .   is - the index set of the subvector
1538: -   bs - the block size of the subvector, gotten from VecGetSubVectorContiguityAndBS_Private()

1540:     Output Parameter:
1541: .   Z  - the subvector, which will compose the VecScatter context on output
1542: */
1543: PetscErrorCode VecGetSubVectorThroughVecScatter_Private(Vec X, IS is, PetscInt bs, Vec *Z)
1544: {
1545:   PetscInt   n, N;
1546:   VecScatter vscat;
1547:   Vec        Y;

1549:   PetscFunctionBegin;
1550:   PetscCall(ISGetLocalSize(is, &n));
1551:   PetscCall(ISGetSize(is, &N));
1552:   PetscCall(VecCreate(PetscObjectComm((PetscObject)is), &Y));
1553:   PetscCall(VecSetSizes(Y, n, N));
1554:   PetscCall(VecSetBlockSize(Y, bs));
1555:   PetscCall(VecSetType(Y, ((PetscObject)X)->type_name));
1556:   PetscCall(VecScatterCreate(X, is, Y, NULL, &vscat));
1557:   PetscCall(VecScatterBegin(vscat, X, Y, INSERT_VALUES, SCATTER_FORWARD));
1558:   PetscCall(VecScatterEnd(vscat, X, Y, INSERT_VALUES, SCATTER_FORWARD));
1559:   PetscCall(PetscObjectCompose((PetscObject)Y, "VecGetSubVector_Scatter", (PetscObject)vscat));
1560:   PetscCall(VecScatterDestroy(&vscat));
1561:   *Z = Y;
1562:   PetscFunctionReturn(PETSC_SUCCESS);
1563: }

1565: /*@
1566:   VecGetSubVector - Gets a vector representing part of another vector

1568:   Collective

1570:   Input Parameters:
1571: + X  - vector from which to extract a subvector
1572: - is - index set representing portion of `X` to extract

1574:   Output Parameter:
1575: . Y - subvector corresponding to `is`

1577:   Level: advanced

1579:   Notes:
1580:   The subvector `Y` should be returned with `VecRestoreSubVector()`.
1581:   `X` and `is` must be defined on the same communicator

1583:   Changes to the subvector will be reflected in the `X` vector on the call to `VecRestoreSubVector()`.

1585:   This function may return a subvector without making a copy, therefore it is not safe to use the original vector while
1586:   modifying the subvector.  Other non-overlapping subvectors can still be obtained from `X` using this function.

1588:   The resulting subvector inherits the block size from `is` if greater than one. Otherwise, the block size is guessed from the block size of the original `X`.

1590: .seealso: [](ch_vectors), `Vec`, `IS`, `VECNEST`, `MatCreateSubMatrix()`
1591: @*/
1592: PetscErrorCode VecGetSubVector(Vec X, IS is, Vec *Y)
1593: {
1594:   Vec Z;

1596:   PetscFunctionBegin;
1599:   PetscCheckSameComm(X, 1, is, 2);
1600:   PetscAssertPointer(Y, 3);
1601:   if (X->ops->getsubvector) {
1602:     PetscUseTypeMethod(X, getsubvector, is, &Z);
1603:   } else { /* Default implementation currently does no caching */
1604:     PetscBool contig;
1605:     PetscInt  n, N, start, bs;

1607:     PetscCall(ISGetLocalSize(is, &n));
1608:     PetscCall(ISGetSize(is, &N));
1609:     PetscCall(VecGetSubVectorContiguityAndBS_Private(X, is, &contig, &start, &bs));
1610:     if (contig) { /* We can do a no-copy implementation */
1611:       const PetscScalar *x;
1612:       PetscInt           state = 0;
1613:       PetscBool          isstd, iscuda, iship;

1615:       PetscCall(PetscObjectTypeCompareAny((PetscObject)X, &isstd, VECSEQ, VECMPI, VECSTANDARD, ""));
1616:       PetscCall(PetscObjectTypeCompareAny((PetscObject)X, &iscuda, VECSEQCUDA, VECMPICUDA, ""));
1617:       PetscCall(PetscObjectTypeCompareAny((PetscObject)X, &iship, VECSEQHIP, VECMPIHIP, ""));
1618:       if (iscuda) {
1619: #if defined(PETSC_HAVE_CUDA)
1620:         const PetscScalar *x_d;
1621:         PetscMPIInt        size;
1622:         PetscOffloadMask   flg;

1624:         PetscCall(VecCUDAGetArrays_Private(X, &x, &x_d, &flg));
1625:         PetscCheck(flg != PETSC_OFFLOAD_UNALLOCATED, PETSC_COMM_SELF, PETSC_ERR_SUP, "Not for PETSC_OFFLOAD_UNALLOCATED");
1626:         PetscCheck(!n || x || x_d, PETSC_COMM_SELF, PETSC_ERR_SUP, "Missing vector data");
1627:         if (x) x += start;
1628:         if (x_d) x_d += start;
1629:         PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)X), &size));
1630:         if (size == 1) {
1631:           PetscCall(VecCreateSeqCUDAWithArrays(PetscObjectComm((PetscObject)X), bs, n, x, x_d, &Z));
1632:         } else {
1633:           PetscCall(VecCreateMPICUDAWithArrays(PetscObjectComm((PetscObject)X), bs, n, N, x, x_d, &Z));
1634:         }
1635:         Z->offloadmask = flg;
1636: #endif
1637:       } else if (iship) {
1638: #if defined(PETSC_HAVE_HIP)
1639:         const PetscScalar *x_d;
1640:         PetscMPIInt        size;
1641:         PetscOffloadMask   flg;

1643:         PetscCall(VecHIPGetArrays_Private(X, &x, &x_d, &flg));
1644:         PetscCheck(flg != PETSC_OFFLOAD_UNALLOCATED, PETSC_COMM_SELF, PETSC_ERR_SUP, "Not for PETSC_OFFLOAD_UNALLOCATED");
1645:         PetscCheck(!n || x || x_d, PETSC_COMM_SELF, PETSC_ERR_SUP, "Missing vector data");
1646:         if (x) x += start;
1647:         if (x_d) x_d += start;
1648:         PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)X), &size));
1649:         if (size == 1) {
1650:           PetscCall(VecCreateSeqHIPWithArrays(PetscObjectComm((PetscObject)X), bs, n, x, x_d, &Z));
1651:         } else {
1652:           PetscCall(VecCreateMPIHIPWithArrays(PetscObjectComm((PetscObject)X), bs, n, N, x, x_d, &Z));
1653:         }
1654:         Z->offloadmask = flg;
1655: #endif
1656:       } else if (isstd) {
1657:         PetscMPIInt size;

1659:         PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)X), &size));
1660:         PetscCall(VecGetArrayRead(X, &x));
1661:         if (x) x += start;
1662:         if (size == 1) {
1663:           PetscCall(VecCreateSeqWithArray(PetscObjectComm((PetscObject)X), bs, n, x, &Z));
1664:         } else {
1665:           PetscCall(VecCreateMPIWithArray(PetscObjectComm((PetscObject)X), bs, n, N, x, &Z));
1666:         }
1667:         PetscCall(VecRestoreArrayRead(X, &x));
1668:       } else { /* default implementation: use place array */
1669:         PetscCall(VecGetArrayRead(X, &x));
1670:         PetscCall(VecCreate(PetscObjectComm((PetscObject)X), &Z));
1671:         PetscCall(VecSetType(Z, ((PetscObject)X)->type_name));
1672:         PetscCall(VecSetSizes(Z, n, N));
1673:         PetscCall(VecSetBlockSize(Z, bs));
1674:         PetscCall(VecPlaceArray(Z, PetscSafePointerPlusOffset(x, start)));
1675:         PetscCall(VecRestoreArrayRead(X, &x));
1676:       }

1678:       /* this is relevant only in debug mode */
1679:       PetscCall(VecLockGet(X, &state));
1680:       if (state) PetscCall(VecLockReadPush(Z));
1681:       Z->ops->placearray   = NULL;
1682:       Z->ops->replacearray = NULL;
1683:     } else { /* Have to create a scatter and do a copy */
1684:       PetscCall(VecGetSubVectorThroughVecScatter_Private(X, is, bs, &Z));
1685:     }
1686:   }
1687:   /* Record the state when the subvector was gotten so we know whether its values need to be put back */
1688:   if (VecGetSubVectorSavedStateId < 0) PetscCall(PetscObjectComposedDataRegister(&VecGetSubVectorSavedStateId));
1689:   PetscCall(PetscObjectComposedDataSetInt((PetscObject)Z, VecGetSubVectorSavedStateId, 1));
1690:   *Y = Z;
1691:   PetscFunctionReturn(PETSC_SUCCESS);
1692: }

1694: /*@
1695:   VecRestoreSubVector - Restores a subvector extracted using `VecGetSubVector()`

1697:   Collective

1699:   Input Parameters:
1700: + X  - vector from which subvector was obtained
1701: . is - index set representing the subset of `X`
1702: - Y  - subvector being restored

1704:   Level: advanced

1706: .seealso: [](ch_vectors), `Vec`, `IS`, `VecGetSubVector()`
1707: @*/
1708: PetscErrorCode VecRestoreSubVector(Vec X, IS is, Vec *Y)
1709: {
1710:   PETSC_UNUSED PetscObjectState dummystate = 0;
1711:   PetscBool                     unchanged;

1713:   PetscFunctionBegin;
1716:   PetscCheckSameComm(X, 1, is, 2);
1717:   PetscAssertPointer(Y, 3);

1720:   if (X->ops->restoresubvector) PetscUseTypeMethod(X, restoresubvector, is, Y);
1721:   else {
1722:     PetscCall(PetscObjectComposedDataGetInt((PetscObject)*Y, VecGetSubVectorSavedStateId, dummystate, unchanged));
1723:     if (!unchanged) { /* If Y's state has not changed since VecGetSubVector(), we only need to destroy Y */
1724:       VecScatter scatter;
1725:       PetscInt   state;

1727:       PetscCall(VecLockGet(X, &state));
1728:       PetscCheck(state == 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Vec X is locked for read-only or read/write access");

1730:       PetscCall(PetscObjectQuery((PetscObject)*Y, "VecGetSubVector_Scatter", (PetscObject *)&scatter));
1731:       if (scatter) {
1732:         PetscCall(VecScatterBegin(scatter, *Y, X, INSERT_VALUES, SCATTER_REVERSE));
1733:         PetscCall(VecScatterEnd(scatter, *Y, X, INSERT_VALUES, SCATTER_REVERSE));
1734:       } else {
1735:         PetscBool iscuda, iship;
1736:         PetscCall(PetscObjectTypeCompareAny((PetscObject)X, &iscuda, VECSEQCUDA, VECMPICUDA, ""));
1737:         PetscCall(PetscObjectTypeCompareAny((PetscObject)X, &iship, VECSEQHIP, VECMPIHIP, ""));

1739:         if (iscuda) {
1740: #if defined(PETSC_HAVE_CUDA)
1741:           PetscOffloadMask ymask = (*Y)->offloadmask;

1743:           /* The offloadmask of X dictates where to move memory
1744:               If X GPU data is valid, then move Y data on GPU if needed
1745:               Otherwise, move back to the CPU */
1746:           switch (X->offloadmask) {
1747:           case PETSC_OFFLOAD_BOTH:
1748:             if (ymask == PETSC_OFFLOAD_CPU) {
1749:               PetscCall(VecCUDAResetArray(*Y));
1750:             } else if (ymask == PETSC_OFFLOAD_GPU) {
1751:               X->offloadmask = PETSC_OFFLOAD_GPU;
1752:             }
1753:             break;
1754:           case PETSC_OFFLOAD_GPU:
1755:             if (ymask == PETSC_OFFLOAD_CPU) PetscCall(VecCUDAResetArray(*Y));
1756:             break;
1757:           case PETSC_OFFLOAD_CPU:
1758:             if (ymask == PETSC_OFFLOAD_GPU) PetscCall(VecResetArray(*Y));
1759:             break;
1760:           case PETSC_OFFLOAD_UNALLOCATED:
1761:           case PETSC_OFFLOAD_KOKKOS:
1762:             SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "This should not happen");
1763:           }
1764: #endif
1765:         } else if (iship) {
1766: #if defined(PETSC_HAVE_HIP)
1767:           PetscOffloadMask ymask = (*Y)->offloadmask;

1769:           /* The offloadmask of X dictates where to move memory
1770:               If X GPU data is valid, then move Y data on GPU if needed
1771:               Otherwise, move back to the CPU */
1772:           switch (X->offloadmask) {
1773:           case PETSC_OFFLOAD_BOTH:
1774:             if (ymask == PETSC_OFFLOAD_CPU) {
1775:               PetscCall(VecHIPResetArray(*Y));
1776:             } else if (ymask == PETSC_OFFLOAD_GPU) {
1777:               X->offloadmask = PETSC_OFFLOAD_GPU;
1778:             }
1779:             break;
1780:           case PETSC_OFFLOAD_GPU:
1781:             if (ymask == PETSC_OFFLOAD_CPU) PetscCall(VecHIPResetArray(*Y));
1782:             break;
1783:           case PETSC_OFFLOAD_CPU:
1784:             if (ymask == PETSC_OFFLOAD_GPU) PetscCall(VecResetArray(*Y));
1785:             break;
1786:           case PETSC_OFFLOAD_UNALLOCATED:
1787:           case PETSC_OFFLOAD_KOKKOS:
1788:             SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "This should not happen");
1789:           }
1790: #endif
1791:         } else {
1792:           /* If OpenCL vecs updated the device memory, this triggers a copy on the CPU */
1793:           PetscCall(VecResetArray(*Y));
1794:         }
1795:         PetscCall(PetscObjectStateIncrease((PetscObject)X));
1796:       }
1797:     }
1798:   }
1799:   PetscCall(VecDestroy(Y));
1800:   PetscFunctionReturn(PETSC_SUCCESS);
1801: }

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

1807:   Not Collective.

1809:   Input Parameter:
1810: . v - The vector for which the local vector is desired.

1812:   Output Parameter:
1813: . w - Upon exit this contains the local vector.

1815:   Level: beginner

1817: .seealso: [](ch_vectors), `Vec`, `VecGetLocalVectorRead()`, `VecRestoreLocalVectorRead()`, `VecGetLocalVector()`, `VecRestoreLocalVector()`
1818: @*/
1819: PetscErrorCode VecCreateLocalVector(Vec v, Vec *w)
1820: {
1821:   VecType  roottype;
1822:   PetscInt n;

1824:   PetscFunctionBegin;
1826:   PetscAssertPointer(w, 2);
1827:   if (v->ops->createlocalvector) {
1828:     PetscUseTypeMethod(v, createlocalvector, w);
1829:     PetscFunctionReturn(PETSC_SUCCESS);
1830:   }
1831:   PetscCall(VecGetRootType_Private(v, &roottype));
1832:   PetscCall(VecCreate(PETSC_COMM_SELF, w));
1833:   PetscCall(VecGetLocalSize(v, &n));
1834:   PetscCall(VecSetSizes(*w, n, n));
1835:   PetscCall(VecGetBlockSize(v, &n));
1836:   PetscCall(VecSetBlockSize(*w, n));
1837:   PetscCall(VecSetType(*w, roottype));
1838:   PetscFunctionReturn(PETSC_SUCCESS);
1839: }

1841: /*@
1842:   VecGetLocalVectorRead - Maps the local portion of a vector into a
1843:   vector.

1845:   Not Collective.

1847:   Input Parameter:
1848: . v - The vector for which the local vector is desired.

1850:   Output Parameter:
1851: . w - Upon exit this contains the local vector.

1853:   Level: beginner

1855:   Notes:
1856:   You must call `VecRestoreLocalVectorRead()` when the local
1857:   vector is no longer needed.

1859:   This function is similar to `VecGetArrayRead()` which maps the local
1860:   portion into a raw pointer.  `VecGetLocalVectorRead()` is usually
1861:   almost as efficient as `VecGetArrayRead()` but in certain circumstances
1862:   `VecGetLocalVectorRead()` can be much more efficient than
1863:   `VecGetArrayRead()`.  This is because the construction of a contiguous
1864:   array representing the vector data required by `VecGetArrayRead()` can
1865:   be an expensive operation for certain vector types.  For example, for
1866:   GPU vectors `VecGetArrayRead()` requires that the data between device
1867:   and host is synchronized.

1869:   Unlike `VecGetLocalVector()`, this routine is not collective and
1870:   preserves cached information.

1872: .seealso: [](ch_vectors), `Vec`, `VecCreateLocalVector()`, `VecRestoreLocalVectorRead()`, `VecGetLocalVector()`, `VecGetArrayRead()`, `VecGetArray()`
1873: @*/
1874: PetscErrorCode VecGetLocalVectorRead(Vec v, Vec w)
1875: {
1876:   PetscFunctionBegin;
1879:   VecCheckSameLocalSize(v, 1, w, 2);
1880:   if (v->ops->getlocalvectorread) {
1881:     PetscUseTypeMethod(v, getlocalvectorread, w);
1882:   } else {
1883:     PetscScalar *a;

1885:     PetscCall(VecGetArrayRead(v, (const PetscScalar **)&a));
1886:     PetscCall(VecPlaceArray(w, a));
1887:   }
1888:   PetscCall(PetscObjectStateIncrease((PetscObject)w));
1889:   PetscCall(VecLockReadPush(v));
1890:   PetscCall(VecLockReadPush(w));
1891:   PetscFunctionReturn(PETSC_SUCCESS);
1892: }

1894: /*@
1895:   VecRestoreLocalVectorRead - Unmaps the local portion of a vector
1896:   previously mapped into a vector using `VecGetLocalVectorRead()`.

1898:   Not Collective.

1900:   Input Parameters:
1901: + v - The local portion of this vector was previously mapped into `w` using `VecGetLocalVectorRead()`.
1902: - w - The vector into which the local portion of `v` was mapped.

1904:   Level: beginner

1906: .seealso: [](ch_vectors), `Vec`, `VecCreateLocalVector()`, `VecGetLocalVectorRead()`, `VecGetLocalVector()`, `VecGetArrayRead()`, `VecGetArray()`
1907: @*/
1908: PetscErrorCode VecRestoreLocalVectorRead(Vec v, Vec w)
1909: {
1910:   PetscFunctionBegin;
1913:   if (v->ops->restorelocalvectorread) {
1914:     PetscUseTypeMethod(v, restorelocalvectorread, w);
1915:   } else {
1916:     const PetscScalar *a;

1918:     PetscCall(VecGetArrayRead(w, &a));
1919:     PetscCall(VecRestoreArrayRead(v, &a));
1920:     PetscCall(VecResetArray(w));
1921:   }
1922:   PetscCall(VecLockReadPop(v));
1923:   PetscCall(VecLockReadPop(w));
1924:   PetscCall(PetscObjectStateIncrease((PetscObject)w));
1925:   PetscFunctionReturn(PETSC_SUCCESS);
1926: }

1928: /*@
1929:   VecGetLocalVector - Maps the local portion of a vector into a
1930:   vector.

1932:   Collective

1934:   Input Parameter:
1935: . v - The vector for which the local vector is desired.

1937:   Output Parameter:
1938: . w - Upon exit this contains the local vector.

1940:   Level: beginner

1942:   Notes:
1943:   You must call `VecRestoreLocalVector()` when the local
1944:   vector is no longer needed.

1946:   This function is similar to `VecGetArray()` which maps the local
1947:   portion into a raw pointer.  `VecGetLocalVector()` is usually about as
1948:   efficient as `VecGetArray()` but in certain circumstances
1949:   `VecGetLocalVector()` can be much more efficient than `VecGetArray()`.
1950:   This is because the construction of a contiguous array representing
1951:   the vector data required by `VecGetArray()` can be an expensive
1952:   operation for certain vector types.  For example, for GPU vectors
1953:   `VecGetArray()` requires that the data between device and host is
1954:   synchronized.

1956: .seealso: [](ch_vectors), `Vec`, `VecCreateLocalVector()`, `VecRestoreLocalVector()`, `VecGetLocalVectorRead()`, `VecGetArrayRead()`, `VecGetArray()`
1957: @*/
1958: PetscErrorCode VecGetLocalVector(Vec v, Vec w)
1959: {
1960:   PetscFunctionBegin;
1963:   VecCheckSameLocalSize(v, 1, w, 2);
1964:   if (v->ops->getlocalvector) {
1965:     PetscUseTypeMethod(v, getlocalvector, w);
1966:   } else {
1967:     PetscScalar *a;

1969:     PetscCall(VecGetArray(v, &a));
1970:     PetscCall(VecPlaceArray(w, a));
1971:   }
1972:   PetscCall(PetscObjectStateIncrease((PetscObject)w));
1973:   PetscFunctionReturn(PETSC_SUCCESS);
1974: }

1976: /*@
1977:   VecRestoreLocalVector - Unmaps the local portion of a vector
1978:   previously mapped into a vector using `VecGetLocalVector()`.

1980:   Logically Collective.

1982:   Input Parameters:
1983: + v - The local portion of this vector was previously mapped into `w` using `VecGetLocalVector()`.
1984: - w - The vector into which the local portion of `v` was mapped.

1986:   Level: beginner

1988: .seealso: [](ch_vectors), `Vec`, `VecCreateLocalVector()`, `VecGetLocalVector()`, `VecGetLocalVectorRead()`, `VecRestoreLocalVectorRead()`, `LocalVectorRead()`, `VecGetArrayRead()`, `VecGetArray()`
1989: @*/
1990: PetscErrorCode VecRestoreLocalVector(Vec v, Vec w)
1991: {
1992:   PetscFunctionBegin;
1995:   if (v->ops->restorelocalvector) {
1996:     PetscUseTypeMethod(v, restorelocalvector, w);
1997:   } else {
1998:     PetscScalar *a;
1999:     PetscCall(VecGetArray(w, &a));
2000:     PetscCall(VecRestoreArray(v, &a));
2001:     PetscCall(VecResetArray(w));
2002:   }
2003:   PetscCall(PetscObjectStateIncrease((PetscObject)w));
2004:   PetscCall(PetscObjectStateIncrease((PetscObject)v));
2005:   PetscFunctionReturn(PETSC_SUCCESS);
2006: }

2008: /*@C
2009:   VecGetArray - Returns a pointer to a contiguous array that contains this
2010:   MPI processes's portion of the vector data

2012:   Logically Collective

2014:   Input Parameter:
2015: . x - the vector

2017:   Output Parameter:
2018: . a - location to put pointer to the array

2020:   Level: beginner

2022:   Notes:
2023:   For the standard PETSc vectors, `VecGetArray()` returns a pointer to the local data array and
2024:   does not use any copies. If the underlying vector data is not stored in a contiguous array
2025:   this routine will copy the data to a contiguous array and return a pointer to that. You MUST
2026:   call `VecRestoreArray()` when you no longer need access to the array.

2028:   Fortran Note:
2029: .vb
2030:   PetscScalar, pointer :: a(:)
2031: .ve

2033: .seealso: [](ch_vectors), `Vec`, `VecRestoreArray()`, `VecGetArrayRead()`, `VecGetArrays()`, `VecPlaceArray()`, `VecGetArray2d()`,
2034:           `VecGetArrayPair()`, `VecRestoreArrayPair()`, `VecGetArrayWrite()`, `VecRestoreArrayWrite()`
2035: @*/
2036: PetscErrorCode VecGetArray(Vec x, PetscScalar *a[])
2037: {
2038:   PetscFunctionBegin;
2040:   PetscCall(VecSetErrorIfLocked(x, 1));
2041:   if (x->ops->getarray) { /* The if-else order matters! VECNEST, VECCUDA etc should have ops->getarray while VECCUDA etc are petscnative */
2042:     PetscUseTypeMethod(x, getarray, a);
2043:   } else if (x->petscnative) { /* VECSTANDARD */
2044:     *a = *((PetscScalar **)x->data);
2045:   } else SETERRQ(PetscObjectComm((PetscObject)x), PETSC_ERR_SUP, "Cannot get array for vector type \"%s\"", ((PetscObject)x)->type_name);
2046:   PetscFunctionReturn(PETSC_SUCCESS);
2047: }

2049: /*@C
2050:   VecRestoreArray - Restores a vector after `VecGetArray()` has been called and the array is no longer needed

2052:   Logically Collective

2054:   Input Parameters:
2055: + x - the vector
2056: - a - location of pointer to array obtained from `VecGetArray()`

2058:   Level: beginner

2060: .seealso: [](ch_vectors), `Vec`, `VecGetArray()`, `VecRestoreArrayRead()`, `VecRestoreArrays()`, `VecPlaceArray()`, `VecRestoreArray2d()`,
2061:           `VecGetArrayPair()`, `VecRestoreArrayPair()`
2062: @*/
2063: PetscErrorCode VecRestoreArray(Vec x, PetscScalar *a[])
2064: {
2065:   PetscFunctionBegin;
2067:   if (a) PetscAssertPointer(a, 2);
2068:   if (x->ops->restorearray) {
2069:     PetscUseTypeMethod(x, restorearray, a);
2070:   } else PetscCheck(x->petscnative, PetscObjectComm((PetscObject)x), PETSC_ERR_SUP, "Cannot restore array for vector type \"%s\"", ((PetscObject)x)->type_name);
2071:   if (a) *a = NULL;
2072:   PetscCall(PetscObjectStateIncrease((PetscObject)x));
2073:   PetscFunctionReturn(PETSC_SUCCESS);
2074: }
2075: /*@C
2076:   VecGetArrayRead - Get read-only pointer to contiguous array containing this processor's portion of the vector data.

2078:   Not Collective

2080:   Input Parameter:
2081: . x - the vector

2083:   Output Parameter:
2084: . a - the array

2086:   Level: beginner

2088:   Notes:
2089:   The array must be returned using a matching call to `VecRestoreArrayRead()`.

2091:   Unlike `VecGetArray()`, preserves cached information like vector norms.

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

2097: .seealso: [](ch_vectors), `Vec`, `VecGetArray()`, `VecRestoreArray()`, `VecGetArrayPair()`, `VecRestoreArrayPair()`
2098: @*/
2099: PetscErrorCode VecGetArrayRead(Vec x, const PetscScalar *a[])
2100: {
2101:   PetscFunctionBegin;
2103:   PetscAssertPointer(a, 2);
2104:   if (x->ops->getarrayread) {
2105:     PetscUseTypeMethod(x, getarrayread, a);
2106:   } else if (x->ops->getarray) {
2107:     PetscObjectState state;

2109:     /* VECNEST, VECCUDA, VECKOKKOS etc */
2110:     // x->ops->getarray may bump the object state, but since we know this is a read-only get
2111:     // we can just undo that
2112:     PetscCall(PetscObjectStateGet((PetscObject)x, &state));
2113:     PetscUseTypeMethod(x, getarray, (PetscScalar **)a);
2114:     PetscCall(PetscObjectStateSet((PetscObject)x, state));
2115:   } else if (x->petscnative) {
2116:     /* VECSTANDARD */
2117:     *a = *((PetscScalar **)x->data);
2118:   } else SETERRQ(PetscObjectComm((PetscObject)x), PETSC_ERR_SUP, "Cannot get array read for vector type \"%s\"", ((PetscObject)x)->type_name);
2119:   PetscFunctionReturn(PETSC_SUCCESS);
2120: }

2122: /*@C
2123:   VecRestoreArrayRead - Restore array obtained with `VecGetArrayRead()`

2125:   Not Collective

2127:   Input Parameters:
2128: + x - the vector
2129: - a - the array

2131:   Level: beginner

2133: .seealso: [](ch_vectors), `Vec`, `VecGetArray()`, `VecRestoreArray()`, `VecGetArrayPair()`, `VecRestoreArrayPair()`
2134: @*/
2135: PetscErrorCode VecRestoreArrayRead(Vec x, const PetscScalar *a[])
2136: {
2137:   PetscFunctionBegin;
2139:   if (a) PetscAssertPointer(a, 2);
2140:   if (x->petscnative) { /* VECSTANDARD, VECCUDA, VECKOKKOS etc */
2141:     /* nothing */
2142:   } else if (x->ops->restorearrayread) { /* VECNEST */
2143:     PetscUseTypeMethod(x, restorearrayread, a);
2144:   } else { /* No one? */
2145:     PetscObjectState state;

2147:     // x->ops->restorearray may bump the object state, but since we know this is a read-restore
2148:     // we can just undo that
2149:     PetscCall(PetscObjectStateGet((PetscObject)x, &state));
2150:     PetscUseTypeMethod(x, restorearray, (PetscScalar **)a);
2151:     PetscCall(PetscObjectStateSet((PetscObject)x, state));
2152:   }
2153:   if (a) *a = NULL;
2154:   PetscFunctionReturn(PETSC_SUCCESS);
2155: }

2157: /*@C
2158:   VecGetArrayWrite - Returns a pointer to a contiguous array that WILL contain this
2159:   MPI processes's portion of the vector data.

2161:   Logically Collective

2163:   Input Parameter:
2164: . x - the vector

2166:   Output Parameter:
2167: . a - location to put pointer to the array

2169:   Level: intermediate

2171:   Note:
2172:   The values in this array are NOT valid, the caller of this routine is responsible for putting
2173:   values into the array; any values it does not set will be invalid.

2175:   The array must be returned using a matching call to `VecRestoreArrayWrite()`.

2177:   For vectors associated with GPUs, the host and device vectors are not synchronized before
2178:   giving access. If you need correct values in the array use `VecGetArray()`

2180: .seealso: [](ch_vectors), `Vec`, `VecRestoreArray()`, `VecGetArrayRead()`, `VecGetArrays()`, `VecPlaceArray()`, `VecGetArray2d()`,
2181:           `VecGetArrayPair()`, `VecRestoreArrayPair()`, `VecGetArray()`, `VecRestoreArrayWrite()`
2182: @*/
2183: PetscErrorCode VecGetArrayWrite(Vec x, PetscScalar *a[])
2184: {
2185:   PetscFunctionBegin;
2187:   PetscAssertPointer(a, 2);
2188:   PetscCall(VecSetErrorIfLocked(x, 1));
2189:   if (x->ops->getarraywrite) {
2190:     PetscUseTypeMethod(x, getarraywrite, a);
2191:   } else {
2192:     PetscCall(VecGetArray(x, a));
2193:   }
2194:   PetscFunctionReturn(PETSC_SUCCESS);
2195: }

2197: /*@C
2198:   VecRestoreArrayWrite - Restores a vector after `VecGetArrayWrite()` has been called.

2200:   Logically Collective

2202:   Input Parameters:
2203: + x - the vector
2204: - a - location of pointer to array obtained from `VecGetArray()`

2206:   Level: beginner

2208: .seealso: [](ch_vectors), `Vec`, `VecGetArray()`, `VecRestoreArrayRead()`, `VecRestoreArrays()`, `VecPlaceArray()`, `VecRestoreArray2d()`,
2209:           `VecGetArrayPair()`, `VecRestoreArrayPair()`, `VecGetArrayWrite()`
2210: @*/
2211: PetscErrorCode VecRestoreArrayWrite(Vec x, PetscScalar *a[])
2212: {
2213:   PetscFunctionBegin;
2215:   if (a) PetscAssertPointer(a, 2);
2216:   if (x->ops->restorearraywrite) {
2217:     PetscUseTypeMethod(x, restorearraywrite, a);
2218:   } else if (x->ops->restorearray) {
2219:     PetscUseTypeMethod(x, restorearray, a);
2220:   }
2221:   if (a) *a = NULL;
2222:   PetscCall(PetscObjectStateIncrease((PetscObject)x));
2223:   PetscFunctionReturn(PETSC_SUCCESS);
2224: }

2226: /*@C
2227:   VecGetArrays - Returns a pointer to the arrays in a set of vectors
2228:   that were created by a call to `VecDuplicateVecs()`.

2230:   Logically Collective; No Fortran Support

2232:   Input Parameters:
2233: + x - the vectors
2234: - n - the number of vectors

2236:   Output Parameter:
2237: . a - location to put pointer to the array

2239:   Level: intermediate

2241:   Note:
2242:   You MUST call `VecRestoreArrays()` when you no longer need access to the arrays.

2244: .seealso: [](ch_vectors), `Vec`, `VecGetArray()`, `VecRestoreArrays()`
2245: @*/
2246: PetscErrorCode VecGetArrays(const Vec x[], PetscInt n, PetscScalar **a[])
2247: {
2248:   PetscInt      i;
2249:   PetscScalar **q;

2251:   PetscFunctionBegin;
2252:   PetscAssertPointer(x, 1);
2254:   PetscAssertPointer(a, 3);
2255:   PetscCheck(n > 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Must get at least one array n = %" PetscInt_FMT, n);
2256:   PetscCall(PetscMalloc1(n, &q));
2257:   for (i = 0; i < n; ++i) PetscCall(VecGetArray(x[i], &q[i]));
2258:   *a = q;
2259:   PetscFunctionReturn(PETSC_SUCCESS);
2260: }

2262: /*@C
2263:   VecRestoreArrays - Restores a group of vectors after `VecGetArrays()`
2264:   has been called.

2266:   Logically Collective; No Fortran Support

2268:   Input Parameters:
2269: + x - the vector
2270: . n - the number of vectors
2271: - a - location of pointer to arrays obtained from `VecGetArrays()`

2273:   Notes:
2274:   For regular PETSc vectors this routine does not involve any copies. For
2275:   any special vectors that do not store local vector data in a contiguous
2276:   array, this routine will copy the data back into the underlying
2277:   vector data structure from the arrays obtained with `VecGetArrays()`.

2279:   Level: intermediate

2281: .seealso: [](ch_vectors), `Vec`, `VecGetArrays()`, `VecRestoreArray()`
2282: @*/
2283: PetscErrorCode VecRestoreArrays(const Vec x[], PetscInt n, PetscScalar **a[])
2284: {
2285:   PetscInt      i;
2286:   PetscScalar **q = *a;

2288:   PetscFunctionBegin;
2289:   PetscAssertPointer(x, 1);
2291:   PetscAssertPointer(a, 3);

2293:   for (i = 0; i < n; ++i) PetscCall(VecRestoreArray(x[i], &q[i]));
2294:   PetscCall(PetscFree(q));
2295:   PetscFunctionReturn(PETSC_SUCCESS);
2296: }

2298: /*@C
2299:   VecGetArrayAndMemType - Like `VecGetArray()`, but if this is a standard device vector (e.g.,
2300:   `VECCUDA`), the returned pointer will be a device pointer to the device memory that contains
2301:   this MPI processes's portion of the vector data.

2303:   Logically Collective; No Fortran Support

2305:   Input Parameter:
2306: . x - the vector

2308:   Output Parameters:
2309: + a     - location to put pointer to the array
2310: - mtype - memory type of the array

2312:   Level: beginner

2314:   Note:
2315:   Device data is guaranteed to have the latest value. Otherwise, when this is a host vector
2316:   (e.g., `VECMPI`), this routine functions the same as `VecGetArray()` and returns a host
2317:   pointer.

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

2323:   Use `VecRestoreArrayAndMemType()` when the array access is no longer needed.

2325: .seealso: [](ch_vectors), `Vec`, `VecRestoreArrayAndMemType()`, `VecGetArrayReadAndMemType()`, `VecGetArrayWriteAndMemType()`, `VecRestoreArray()`, `VecGetArrayRead()`, `VecGetArrays()`,
2326:           `VecPlaceArray()`, `VecGetArray2d()`, `VecGetArrayPair()`, `VecRestoreArrayPair()`, `VecGetArrayWrite()`, `VecRestoreArrayWrite()`
2327: @*/
2328: PetscErrorCode VecGetArrayAndMemType(Vec x, PetscScalar *a[], PetscMemType *mtype)
2329: {
2330:   PetscFunctionBegin;
2333:   PetscAssertPointer(a, 2);
2334:   if (mtype) PetscAssertPointer(mtype, 3);
2335:   PetscCall(VecSetErrorIfLocked(x, 1));
2336:   if (x->ops->getarrayandmemtype) {
2337:     /* VECCUDA, VECKOKKOS etc */
2338:     PetscUseTypeMethod(x, getarrayandmemtype, a, mtype);
2339:   } else {
2340:     /* VECSTANDARD, VECNEST, VECVIENNACL */
2341:     PetscCall(VecGetArray(x, a));
2342:     if (mtype) *mtype = PETSC_MEMTYPE_HOST;
2343:   }
2344:   PetscFunctionReturn(PETSC_SUCCESS);
2345: }

2347: /*@C
2348:   VecRestoreArrayAndMemType - Restores a vector after `VecGetArrayAndMemType()` has been called.

2350:   Logically Collective; No Fortran Support

2352:   Input Parameters:
2353: + x - the vector
2354: - a - location of pointer to array obtained from `VecGetArrayAndMemType()`

2356:   Level: beginner

2358: .seealso: [](ch_vectors), `Vec`, `VecGetArrayAndMemType()`, `VecGetArray()`, `VecRestoreArrayRead()`, `VecRestoreArrays()`,
2359:           `VecPlaceArray()`, `VecRestoreArray2d()`, `VecGetArrayPair()`, `VecRestoreArrayPair()`
2360: @*/
2361: PetscErrorCode VecRestoreArrayAndMemType(Vec x, PetscScalar *a[])
2362: {
2363:   PetscFunctionBegin;
2366:   if (a) PetscAssertPointer(a, 2);
2367:   if (x->ops->restorearrayandmemtype) {
2368:     /* VECCUDA, VECKOKKOS etc */
2369:     PetscUseTypeMethod(x, restorearrayandmemtype, a);
2370:   } else {
2371:     /* VECNEST, VECVIENNACL */
2372:     PetscCall(VecRestoreArray(x, a));
2373:   } /* VECSTANDARD does nothing */
2374:   if (a) *a = NULL;
2375:   PetscCall(PetscObjectStateIncrease((PetscObject)x));
2376:   PetscFunctionReturn(PETSC_SUCCESS);
2377: }

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

2383:   Not Collective; No Fortran Support

2385:   Input Parameter:
2386: . x - the vector

2388:   Output Parameters:
2389: + a     - the array
2390: - mtype - memory type of the array

2392:   Level: beginner

2394:   Notes:
2395:   The array must be returned using a matching call to `VecRestoreArrayReadAndMemType()`.

2397: .seealso: [](ch_vectors), `Vec`, `VecRestoreArrayReadAndMemType()`, `VecGetArrayAndMemType()`, `VecGetArrayWriteAndMemType()`, `VecGetArray()`, `VecRestoreArray()`, `VecGetArrayPair()`, `VecRestoreArrayPair()`
2398: @*/
2399: PetscErrorCode VecGetArrayReadAndMemType(Vec x, const PetscScalar *a[], PetscMemType *mtype)
2400: {
2401:   PetscFunctionBegin;
2404:   PetscAssertPointer(a, 2);
2405:   if (mtype) PetscAssertPointer(mtype, 3);
2406:   if (x->ops->getarrayreadandmemtype) {
2407:     /* VECCUDA/VECHIP though they are also petscnative */
2408:     PetscUseTypeMethod(x, getarrayreadandmemtype, a, mtype);
2409:   } else if (x->ops->getarrayandmemtype) {
2410:     /* VECKOKKOS */
2411:     PetscObjectState state;

2413:     // see VecGetArrayRead() for why
2414:     PetscCall(PetscObjectStateGet((PetscObject)x, &state));
2415:     PetscUseTypeMethod(x, getarrayandmemtype, (PetscScalar **)a, mtype);
2416:     PetscCall(PetscObjectStateSet((PetscObject)x, state));
2417:   } else {
2418:     PetscCall(VecGetArrayRead(x, a));
2419:     if (mtype) *mtype = PETSC_MEMTYPE_HOST;
2420:   }
2421:   PetscFunctionReturn(PETSC_SUCCESS);
2422: }

2424: /*@C
2425:   VecRestoreArrayReadAndMemType - Restore array obtained with `VecGetArrayReadAndMemType()`

2427:   Not Collective; No Fortran Support

2429:   Input Parameters:
2430: + x - the vector
2431: - a - the array

2433:   Level: beginner

2435: .seealso: [](ch_vectors), `Vec`, `VecGetArrayReadAndMemType()`, `VecRestoreArrayAndMemType()`, `VecRestoreArrayWriteAndMemType()`, `VecGetArray()`, `VecRestoreArray()`, `VecGetArrayPair()`, `VecRestoreArrayPair()`
2436: @*/
2437: PetscErrorCode VecRestoreArrayReadAndMemType(Vec x, const PetscScalar *a[])
2438: {
2439:   PetscFunctionBegin;
2442:   if (a) PetscAssertPointer(a, 2);
2443:   if (x->ops->restorearrayreadandmemtype) {
2444:     /* VECCUDA/VECHIP */
2445:     PetscUseTypeMethod(x, restorearrayreadandmemtype, a);
2446:   } else if (!x->petscnative) {
2447:     /* VECNEST */
2448:     PetscCall(VecRestoreArrayRead(x, a));
2449:   }
2450:   if (a) *a = NULL;
2451:   PetscFunctionReturn(PETSC_SUCCESS);
2452: }

2454: /*@C
2455:   VecGetArrayWriteAndMemType - Like `VecGetArrayWrite()`, but if this is a device vector it will always return
2456:   a device pointer to the device memory that contains this processor's portion of the vector data.

2458:   Logically Collective; No Fortran Support

2460:   Input Parameter:
2461: . x - the vector

2463:   Output Parameters:
2464: + a     - the array
2465: - mtype - memory type of the array

2467:   Level: beginner

2469:   Note:
2470:   The array must be returned using a matching call to `VecRestoreArrayWriteAndMemType()`, where it will label the device memory as most recent.

2472: .seealso: [](ch_vectors), `Vec`, `VecRestoreArrayWriteAndMemType()`, `VecGetArrayReadAndMemType()`, `VecGetArrayAndMemType()`, `VecGetArray()`, `VecRestoreArray()`, `VecGetArrayPair()`, `VecRestoreArrayPair()`,
2473: @*/
2474: PetscErrorCode VecGetArrayWriteAndMemType(Vec x, PetscScalar *a[], PetscMemType *mtype)
2475: {
2476:   PetscFunctionBegin;
2479:   PetscCall(VecSetErrorIfLocked(x, 1));
2480:   PetscAssertPointer(a, 2);
2481:   if (mtype) PetscAssertPointer(mtype, 3);
2482:   if (x->ops->getarraywriteandmemtype) {
2483:     /* VECCUDA, VECHIP, VECKOKKOS etc, though they are also petscnative */
2484:     PetscUseTypeMethod(x, getarraywriteandmemtype, a, mtype);
2485:   } else if (x->ops->getarrayandmemtype) {
2486:     PetscCall(VecGetArrayAndMemType(x, a, mtype));
2487:   } else {
2488:     /* VECNEST, VECVIENNACL */
2489:     PetscCall(VecGetArrayWrite(x, a));
2490:     if (mtype) *mtype = PETSC_MEMTYPE_HOST;
2491:   }
2492:   PetscFunctionReturn(PETSC_SUCCESS);
2493: }

2495: /*@C
2496:   VecRestoreArrayWriteAndMemType - Restore array obtained with `VecGetArrayWriteAndMemType()`

2498:   Logically Collective; No Fortran Support

2500:   Input Parameters:
2501: + x - the vector
2502: - a - the array

2504:   Level: beginner

2506: .seealso: [](ch_vectors), `Vec`, `VecGetArrayWriteAndMemType()`, `VecRestoreArrayAndMemType()`, `VecGetArray()`, `VecRestoreArray()`, `VecGetArrayPair()`, `VecRestoreArrayPair()`
2507: @*/
2508: PetscErrorCode VecRestoreArrayWriteAndMemType(Vec x, PetscScalar *a[])
2509: {
2510:   PetscFunctionBegin;
2513:   PetscCall(VecSetErrorIfLocked(x, 1));
2514:   if (a) PetscAssertPointer(a, 2);
2515:   if (x->ops->restorearraywriteandmemtype) {
2516:     /* VECCUDA/VECHIP */
2517:     PetscMemType PETSC_UNUSED mtype; // since this function doesn't accept a memtype?
2518:     PetscUseTypeMethod(x, restorearraywriteandmemtype, a, &mtype);
2519:   } else if (x->ops->restorearrayandmemtype) {
2520:     PetscCall(VecRestoreArrayAndMemType(x, a));
2521:   } else {
2522:     PetscCall(VecRestoreArray(x, a));
2523:   }
2524:   if (a) *a = NULL;
2525:   PetscFunctionReturn(PETSC_SUCCESS);
2526: }

2528: /*@
2529:   VecPlaceArray - Allows one to replace the array in a vector with an
2530:   array provided by the user. This is useful to avoid copying an array
2531:   into a vector.

2533:   Logically Collective; No Fortran Support

2535:   Input Parameters:
2536: + vec   - the vector
2537: - array - the array

2539:   Level: developer

2541:   Notes:
2542:   Adding `const` to `array` was an oversight, as subsequent operations on `vec` would
2543:   likely modify the data in `array`. However, we have kept it to avoid breaking APIs.

2545:   Use `VecReplaceArray()` instead to permanently replace the array

2547:   You can return to the original array with a call to `VecResetArray()`. `vec` does not take
2548:   ownership of `array` in any way.

2550:   The user must free `array` themselves but be careful not to
2551:   do so before the vector has either been destroyed, had its original array restored with
2552:   `VecResetArray()` or permanently replaced with `VecReplaceArray()`.

2554: .seealso: [](ch_vectors), `Vec`, `VecGetArray()`, `VecRestoreArray()`, `VecReplaceArray()`, `VecResetArray()`
2555: @*/
2556: PetscErrorCode VecPlaceArray(Vec vec, const PetscScalar array[])
2557: {
2558:   PetscFunctionBegin;
2561:   if (array) PetscAssertPointer(array, 2);
2562:   PetscUseTypeMethod(vec, placearray, array);
2563:   PetscCall(PetscObjectStateIncrease((PetscObject)vec));
2564:   PetscFunctionReturn(PETSC_SUCCESS);
2565: }

2567: /*@C
2568:   VecReplaceArray - Allows one to replace the array in a vector with an
2569:   array provided by the user. This is useful to avoid copying an array
2570:   into a vector.

2572:   Logically Collective; No Fortran Support

2574:   Input Parameters:
2575: + vec   - the vector
2576: - array - the array

2578:   Level: developer

2580:   Notes:
2581:   Adding `const` to `array` was an oversight, as subsequent operations on `vec` would
2582:   likely modify the data in `array`. However, we have kept it to avoid breaking APIs.

2584:   This permanently replaces the array and frees the memory associated
2585:   with the old array. Use `VecPlaceArray()` to temporarily replace the array.

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

2590: .seealso: [](ch_vectors), `Vec`, `VecGetArray()`, `VecRestoreArray()`, `VecPlaceArray()`, `VecResetArray()`
2591: @*/
2592: PetscErrorCode VecReplaceArray(Vec vec, const PetscScalar array[])
2593: {
2594:   PetscFunctionBegin;
2597:   PetscUseTypeMethod(vec, replacearray, array);
2598:   PetscCall(PetscObjectStateIncrease((PetscObject)vec));
2599:   PetscFunctionReturn(PETSC_SUCCESS);
2600: }

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

2607:   Logically Collective

2609:   Input Parameters:
2610: + x      - the vector
2611: . m      - first dimension of two dimensional array
2612: . n      - second dimension of two dimensional array
2613: . mstart - first index you will use in first coordinate direction (often 0)
2614: - nstart - first index in the second coordinate direction (often 0)

2616:   Output Parameter:
2617: . a - location to put pointer to the array

2619:   Level: developer

2621:   Notes:
2622:   For a vector obtained from `DMCreateLocalVector()` `mstart` and `nstart` are likely
2623:   obtained from the corner indices obtained from `DMDAGetGhostCorners()` while for
2624:   `DMCreateGlobalVector()` they are the corner indices from `DMDAGetCorners()`. In both cases
2625:   the arguments from `DMDAGet[Ghost]Corners()` are reversed in the call to `VecGetArray2d()`.

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

2629: .seealso: [](ch_vectors), `Vec`, `VecGetArray()`, `VecRestoreArray()`, `VecGetArrays()`, `VecPlaceArray()`,
2630:           `VecRestoreArray2d()`, `DMDAVecGetArray()`, `DMDAVecRestoreArray()`, `VecGetArray3d()`, `VecRestoreArray3d()`,
2631:           `VecGetArray1d()`, `VecRestoreArray1d()`, `VecGetArray4d()`, `VecRestoreArray4d()`
2632: @*/
2633: PetscErrorCode VecGetArray2d(Vec x, PetscInt m, PetscInt n, PetscInt mstart, PetscInt nstart, PetscScalar **a[])
2634: {
2635:   PetscInt     i, N;
2636:   PetscScalar *aa;

2638:   PetscFunctionBegin;
2640:   PetscAssertPointer(a, 6);
2642:   PetscCall(VecGetLocalSize(x, &N));
2643:   PetscCheck(m * n == N, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Local array size %" PetscInt_FMT " does not match 2d array dimensions %" PetscInt_FMT " by %" PetscInt_FMT, N, m, n);
2644:   PetscCall(VecGetArray(x, &aa));

2646:   PetscCall(PetscMalloc1(m, a));
2647:   for (i = 0; i < m; i++) (*a)[i] = aa + i * n - nstart;
2648:   *a -= mstart;
2649:   PetscFunctionReturn(PETSC_SUCCESS);
2650: }

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

2657:   Logically Collective

2659:   Input Parameters:
2660: + x      - the vector
2661: . m      - first dimension of two dimensional array
2662: . n      - second dimension of two dimensional array
2663: . mstart - first index you will use in first coordinate direction (often 0)
2664: - nstart - first index in the second coordinate direction (often 0)

2666:   Output Parameter:
2667: . a - location to put pointer to the array

2669:   Level: developer

2671:   Notes:
2672:   For a vector obtained from `DMCreateLocalVector()` `mstart` and `nstart` are likely
2673:   obtained from the corner indices obtained from `DMDAGetGhostCorners()` while for
2674:   `DMCreateGlobalVector()` they are the corner indices from `DMDAGetCorners()`. In both cases
2675:   the arguments from `DMDAGet[Ghost]Corners()` are reversed in the call to `VecGetArray2d()`.

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

2679: .seealso: [](ch_vectors), `Vec`, `VecGetArray()`, `VecRestoreArray()`, `VecGetArrays()`, `VecPlaceArray()`,
2680:           `VecRestoreArray2d()`, `DMDAVecGetArray()`, `DMDAVecRestoreArray()`, `VecGetArray3d()`, `VecRestoreArray3d()`,
2681:           `VecGetArray1d()`, `VecRestoreArray1d()`, `VecGetArray4d()`, `VecRestoreArray4d()`
2682: @*/
2683: PetscErrorCode VecGetArray2dWrite(Vec x, PetscInt m, PetscInt n, PetscInt mstart, PetscInt nstart, PetscScalar **a[])
2684: {
2685:   PetscInt     i, N;
2686:   PetscScalar *aa;

2688:   PetscFunctionBegin;
2690:   PetscAssertPointer(a, 6);
2692:   PetscCall(VecGetLocalSize(x, &N));
2693:   PetscCheck(m * n == N, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Local array size %" PetscInt_FMT " does not match 2d array dimensions %" PetscInt_FMT " by %" PetscInt_FMT, N, m, n);
2694:   PetscCall(VecGetArrayWrite(x, &aa));

2696:   PetscCall(PetscMalloc1(m, a));
2697:   for (i = 0; i < m; i++) (*a)[i] = aa + i * n - nstart;
2698:   *a -= mstart;
2699:   PetscFunctionReturn(PETSC_SUCCESS);
2700: }

2702: /*@C
2703:   VecRestoreArray2d - Restores a vector after `VecGetArray2d()` has been called.

2705:   Logically Collective

2707:   Input Parameters:
2708: + x      - the vector
2709: . m      - first dimension of two dimensional array
2710: . n      - second dimension of the two dimensional array
2711: . mstart - first index you will use in first coordinate direction (often 0)
2712: . nstart - first index in the second coordinate direction (often 0)
2713: - a      - location of pointer to array obtained from `VecGetArray2d()`

2715:   Level: developer

2717:   Notes:
2718:   For regular PETSc vectors this routine does not involve any copies. For
2719:   any special vectors that do not store local vector data in a contiguous
2720:   array, this routine will copy the data back into the underlying
2721:   vector data structure from the array obtained with `VecGetArray()`.

2723:   This routine actually zeros out the `a` pointer.

2725: .seealso: [](ch_vectors), `Vec`, `VecGetArray()`, `VecRestoreArray()`, `VecRestoreArrays()`, `VecPlaceArray()`,
2726:           `VecGetArray2d()`, `VecGetArray3d()`, `VecRestoreArray3d()`, `DMDAVecGetArray()`, `DMDAVecRestoreArray()`
2727:           `VecGetArray1d()`, `VecRestoreArray1d()`, `VecGetArray4d()`, `VecRestoreArray4d()`
2728: @*/
2729: PetscErrorCode VecRestoreArray2d(Vec x, PetscInt m, PetscInt n, PetscInt mstart, PetscInt nstart, PetscScalar **a[])
2730: {
2731:   void *dummy;

2733:   PetscFunctionBegin;
2735:   PetscAssertPointer(a, 6);
2737:   dummy = (void *)(*a + mstart);
2738:   PetscCall(PetscFree(dummy));
2739:   PetscCall(VecRestoreArray(x, NULL));
2740:   *a = NULL;
2741:   PetscFunctionReturn(PETSC_SUCCESS);
2742: }

2744: /*@C
2745:   VecRestoreArray2dWrite - Restores a vector after `VecGetArray2dWrite()` has been called.

2747:   Logically Collective

2749:   Input Parameters:
2750: + x      - the vector
2751: . m      - first dimension of two dimensional array
2752: . n      - second dimension of the two dimensional array
2753: . mstart - first index you will use in first coordinate direction (often 0)
2754: . nstart - first index in the second coordinate direction (often 0)
2755: - a      - location of pointer to array obtained from `VecGetArray2d()`

2757:   Level: developer

2759:   Notes:
2760:   For regular PETSc vectors this routine does not involve any copies. For
2761:   any special vectors that do not store local vector data in a contiguous
2762:   array, this routine will copy the data back into the underlying
2763:   vector data structure from the array obtained with `VecGetArray()`.

2765:   This routine actually zeros out the `a` pointer.

2767: .seealso: [](ch_vectors), `Vec`, `VecGetArray()`, `VecRestoreArray()`, `VecRestoreArrays()`, `VecPlaceArray()`,
2768:           `VecGetArray2d()`, `VecGetArray3d()`, `VecRestoreArray3d()`, `DMDAVecGetArray()`, `DMDAVecRestoreArray()`
2769:           `VecGetArray1d()`, `VecRestoreArray1d()`, `VecGetArray4d()`, `VecRestoreArray4d()`
2770: @*/
2771: PetscErrorCode VecRestoreArray2dWrite(Vec x, PetscInt m, PetscInt n, PetscInt mstart, PetscInt nstart, PetscScalar **a[])
2772: {
2773:   void *dummy;

2775:   PetscFunctionBegin;
2777:   PetscAssertPointer(a, 6);
2779:   dummy = (void *)(*a + mstart);
2780:   PetscCall(PetscFree(dummy));
2781:   PetscCall(VecRestoreArrayWrite(x, NULL));
2782:   PetscFunctionReturn(PETSC_SUCCESS);
2783: }

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

2790:   Logically Collective

2792:   Input Parameters:
2793: + x      - the vector
2794: . m      - first dimension of two dimensional array
2795: - mstart - first index you will use in first coordinate direction (often 0)

2797:   Output Parameter:
2798: . a - location to put pointer to the array

2800:   Level: developer

2802:   Notes:
2803:   For a vector obtained from `DMCreateLocalVector()` `mstart` is likely
2804:   obtained from the corner indices obtained from `DMDAGetGhostCorners()` while for
2805:   `DMCreateGlobalVector()` they are the corner indices from `DMDAGetCorners()`.

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

2809: .seealso: [](ch_vectors), `Vec`, `VecGetArray()`, `VecRestoreArray()`, `VecGetArrays()`, `VecPlaceArray()`,
2810:           `VecRestoreArray2d()`, `DMDAVecGetArray()`, `DMDAVecRestoreArray()`, `VecGetArray3d()`, `VecRestoreArray3d()`,
2811:           `VecGetArray2d()`, `VecRestoreArray1d()`, `VecGetArray4d()`, `VecRestoreArray4d()`
2812: @*/
2813: PetscErrorCode VecGetArray1d(Vec x, PetscInt m, PetscInt mstart, PetscScalar *a[])
2814: {
2815:   PetscInt N;

2817:   PetscFunctionBegin;
2819:   PetscAssertPointer(a, 4);
2821:   PetscCall(VecGetLocalSize(x, &N));
2822:   PetscCheck(m == N, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Local array size %" PetscInt_FMT " does not match 1d array dimensions %" PetscInt_FMT, N, m);
2823:   PetscCall(VecGetArray(x, a));
2824:   *a -= mstart;
2825:   PetscFunctionReturn(PETSC_SUCCESS);
2826: }

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

2833:   Logically Collective

2835:   Input Parameters:
2836: + x      - the vector
2837: . m      - first dimension of two dimensional array
2838: - mstart - first index you will use in first coordinate direction (often 0)

2840:   Output Parameter:
2841: . a - location to put pointer to the array

2843:   Level: developer

2845:   Notes:
2846:   For a vector obtained from `DMCreateLocalVector()` `mstart` is likely
2847:   obtained from the corner indices obtained from `DMDAGetGhostCorners()` while for
2848:   `DMCreateGlobalVector()` they are the corner indices from `DMDAGetCorners()`.

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

2852: .seealso: [](ch_vectors), `Vec`, `VecGetArray()`, `VecRestoreArray()`, `VecGetArrays()`, `VecPlaceArray()`,
2853:           `VecRestoreArray2d()`, `DMDAVecGetArray()`, `DMDAVecRestoreArray()`, `VecGetArray3d()`, `VecRestoreArray3d()`,
2854:           `VecGetArray2d()`, `VecRestoreArray1d()`, `VecGetArray4d()`, `VecRestoreArray4d()`
2855: @*/
2856: PetscErrorCode VecGetArray1dWrite(Vec x, PetscInt m, PetscInt mstart, PetscScalar *a[])
2857: {
2858:   PetscInt N;

2860:   PetscFunctionBegin;
2862:   PetscAssertPointer(a, 4);
2864:   PetscCall(VecGetLocalSize(x, &N));
2865:   PetscCheck(m == N, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Local array size %" PetscInt_FMT " does not match 1d array dimensions %" PetscInt_FMT, N, m);
2866:   PetscCall(VecGetArrayWrite(x, a));
2867:   *a -= mstart;
2868:   PetscFunctionReturn(PETSC_SUCCESS);
2869: }

2871: /*@C
2872:   VecRestoreArray1d - Restores a vector after `VecGetArray1d()` has been called.

2874:   Logically Collective

2876:   Input Parameters:
2877: + x      - the vector
2878: . m      - first dimension of two dimensional array
2879: . mstart - first index you will use in first coordinate direction (often 0)
2880: - a      - location of pointer to array obtained from `VecGetArray1d()`

2882:   Level: developer

2884:   Notes:
2885:   For regular PETSc vectors this routine does not involve any copies. For
2886:   any special vectors that do not store local vector data in a contiguous
2887:   array, this routine will copy the data back into the underlying
2888:   vector data structure from the array obtained with `VecGetArray1d()`.

2890:   This routine actually zeros out the `a` pointer.

2892: .seealso: [](ch_vectors), `Vec`, `VecGetArray()`, `VecRestoreArray()`, `VecRestoreArrays()`, `VecPlaceArray()`,
2893:           `VecGetArray2d()`, `VecGetArray3d()`, `VecRestoreArray3d()`, `DMDAVecGetArray()`, `DMDAVecRestoreArray()`
2894:           `VecGetArray1d()`, `VecRestoreArray2d()`, `VecGetArray4d()`, `VecRestoreArray4d()`
2895: @*/
2896: PetscErrorCode VecRestoreArray1d(Vec x, PetscInt m, PetscInt mstart, PetscScalar *a[])
2897: {
2898:   PetscFunctionBegin;
2901:   PetscCall(VecRestoreArray(x, NULL));
2902:   *a = NULL;
2903:   PetscFunctionReturn(PETSC_SUCCESS);
2904: }

2906: /*@C
2907:   VecRestoreArray1dWrite - Restores a vector after `VecGetArray1dWrite()` has been called.

2909:   Logically Collective

2911:   Input Parameters:
2912: + x      - the vector
2913: . m      - first dimension of two dimensional array
2914: . mstart - first index you will use in first coordinate direction (often 0)
2915: - a      - location of pointer to array obtained from `VecGetArray1d()`

2917:   Level: developer

2919:   Notes:
2920:   For regular PETSc vectors this routine does not involve any copies. For
2921:   any special vectors that do not store local vector data in a contiguous
2922:   array, this routine will copy the data back into the underlying
2923:   vector data structure from the array obtained with `VecGetArray1d()`.

2925:   This routine actually zeros out the `a` pointer.

2927: .seealso: [](ch_vectors), `Vec`, `VecGetArray()`, `VecRestoreArray()`, `VecRestoreArrays()`, `VecPlaceArray()`,
2928:           `VecGetArray2d()`, `VecGetArray3d()`, `VecRestoreArray3d()`, `DMDAVecGetArray()`, `DMDAVecRestoreArray()`
2929:           `VecGetArray1d()`, `VecRestoreArray2d()`, `VecGetArray4d()`, `VecRestoreArray4d()`
2930: @*/
2931: PetscErrorCode VecRestoreArray1dWrite(Vec x, PetscInt m, PetscInt mstart, PetscScalar *a[])
2932: {
2933:   PetscFunctionBegin;
2936:   PetscCall(VecRestoreArrayWrite(x, NULL));
2937:   *a = NULL;
2938:   PetscFunctionReturn(PETSC_SUCCESS);
2939: }

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

2946:   Logically Collective

2948:   Input Parameters:
2949: + x      - the vector
2950: . m      - first dimension of three dimensional array
2951: . n      - second dimension of three dimensional array
2952: . p      - third dimension of three dimensional array
2953: . mstart - first index you will use in first coordinate direction (often 0)
2954: . nstart - first index in the second coordinate direction (often 0)
2955: - pstart - first index in the third coordinate direction (often 0)

2957:   Output Parameter:
2958: . a - location to put pointer to the array

2960:   Level: developer

2962:   Notes:
2963:   For a vector obtained from `DMCreateLocalVector()` `mstart`, `nstart`, and `pstart` are likely
2964:   obtained from the corner indices obtained from `DMDAGetGhostCorners()` while for
2965:   `DMCreateGlobalVector()` they are the corner indices from `DMDAGetCorners()`. In both cases
2966:   the arguments from `DMDAGet[Ghost]Corners()` are reversed in the call to `VecGetArray3d()`.

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

2970: .seealso: [](ch_vectors), `Vec`, `VecGetArray()`, `VecRestoreArray()`, `VecGetArrays()`, `VecPlaceArray()`,
2971:           `VecRestoreArray2d()`, `DMDAVecGetarray()`, `DMDAVecRestoreArray()`, `VecRestoreArray3d()`,
2972:           `VecGetArray1d()`, `VecRestoreArray1d()`, `VecGetArray4d()`, `VecRestoreArray4d()`
2973: @*/
2974: PetscErrorCode VecGetArray3d(Vec x, PetscInt m, PetscInt n, PetscInt p, PetscInt mstart, PetscInt nstart, PetscInt pstart, PetscScalar ***a[])
2975: {
2976:   PetscInt     i, N, j;
2977:   PetscScalar *aa, **b;

2979:   PetscFunctionBegin;
2981:   PetscAssertPointer(a, 8);
2983:   PetscCall(VecGetLocalSize(x, &N));
2984:   PetscCheck(m * n * p == N, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Local array size %" PetscInt_FMT " does not match 3d array dimensions %" PetscInt_FMT " by %" PetscInt_FMT " by %" PetscInt_FMT, N, m, n, p);
2985:   PetscCall(VecGetArray(x, &aa));

2987:   PetscCall(PetscMalloc(m * sizeof(PetscScalar **) + m * n * sizeof(PetscScalar *), a));
2988:   b = (PetscScalar **)((*a) + m);
2989:   for (i = 0; i < m; i++) (*a)[i] = b + i * n - nstart;
2990:   for (i = 0; i < m; i++)
2991:     for (j = 0; j < n; j++) b[i * n + j] = PetscSafePointerPlusOffset(aa, i * n * p + j * p - pstart);
2992:   *a -= mstart;
2993:   PetscFunctionReturn(PETSC_SUCCESS);
2994: }

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

3001:   Logically Collective

3003:   Input Parameters:
3004: + x      - the vector
3005: . m      - first dimension of three dimensional array
3006: . n      - second dimension of three dimensional array
3007: . p      - third dimension of three dimensional array
3008: . mstart - first index you will use in first coordinate direction (often 0)
3009: . nstart - first index in the second coordinate direction (often 0)
3010: - pstart - first index in the third coordinate direction (often 0)

3012:   Output Parameter:
3013: . a - location to put pointer to the array

3015:   Level: developer

3017:   Notes:
3018:   For a vector obtained from `DMCreateLocalVector()` `mstart`, `nstart`, and `pstart` are likely
3019:   obtained from the corner indices obtained from `DMDAGetGhostCorners()` while for
3020:   `DMCreateGlobalVector()` they are the corner indices from `DMDAGetCorners()`. In both cases
3021:   the arguments from `DMDAGet[Ghost]Corners()` are reversed in the call to `VecGetArray3d()`.

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

3025: .seealso: [](ch_vectors), `Vec`, `VecGetArray()`, `VecRestoreArray()`, `VecGetArrays()`, `VecPlaceArray()`,
3026:           `VecRestoreArray2d()`, `DMDAVecGetarray()`, `DMDAVecRestoreArray()`, `VecGetArray3d()`, `VecRestoreArray3d()`,
3027:           `VecGetArray1d()`, `VecRestoreArray1d()`, `VecGetArray4d()`, `VecRestoreArray4d()`
3028: @*/
3029: PetscErrorCode VecGetArray3dWrite(Vec x, PetscInt m, PetscInt n, PetscInt p, PetscInt mstart, PetscInt nstart, PetscInt pstart, PetscScalar ***a[])
3030: {
3031:   PetscInt     i, N, j;
3032:   PetscScalar *aa, **b;

3034:   PetscFunctionBegin;
3036:   PetscAssertPointer(a, 8);
3038:   PetscCall(VecGetLocalSize(x, &N));
3039:   PetscCheck(m * n * p == N, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Local array size %" PetscInt_FMT " does not match 3d array dimensions %" PetscInt_FMT " by %" PetscInt_FMT " by %" PetscInt_FMT, N, m, n, p);
3040:   PetscCall(VecGetArrayWrite(x, &aa));

3042:   PetscCall(PetscMalloc(m * sizeof(PetscScalar **) + m * n * sizeof(PetscScalar *), a));
3043:   b = (PetscScalar **)((*a) + m);
3044:   for (i = 0; i < m; i++) (*a)[i] = b + i * n - nstart;
3045:   for (i = 0; i < m; i++)
3046:     for (j = 0; j < n; j++) b[i * n + j] = aa + i * n * p + j * p - pstart;

3048:   *a -= mstart;
3049:   PetscFunctionReturn(PETSC_SUCCESS);
3050: }

3052: /*@C
3053:   VecRestoreArray3d - Restores a vector after `VecGetArray3d()` has been called.

3055:   Logically Collective

3057:   Input Parameters:
3058: + x      - the vector
3059: . m      - first dimension of three dimensional array
3060: . n      - second dimension of the three dimensional array
3061: . p      - third dimension of the three dimensional array
3062: . mstart - first index you will use in first coordinate direction (often 0)
3063: . nstart - first index in the second coordinate direction (often 0)
3064: . pstart - first index in the third coordinate direction (often 0)
3065: - a      - location of pointer to array obtained from VecGetArray3d()

3067:   Level: developer

3069:   Notes:
3070:   For regular PETSc vectors this routine does not involve any copies. For
3071:   any special vectors that do not store local vector data in a contiguous
3072:   array, this routine will copy the data back into the underlying
3073:   vector data structure from the array obtained with `VecGetArray()`.

3075:   This routine actually zeros out the `a` pointer.

3077: .seealso: [](ch_vectors), `Vec`, `VecGetArray()`, `VecRestoreArray()`, `VecRestoreArrays()`, `VecPlaceArray()`,
3078:           `VecGetArray2d()`, `VecGetArray3d()`, `DMDAVecGetArray()`, `DMDAVecRestoreArray()`
3079:           `VecGetArray1d()`, `VecRestoreArray1d()`, `VecGetArray4d()`, `VecRestoreArray4d()`
3080: @*/
3081: PetscErrorCode VecRestoreArray3d(Vec x, PetscInt m, PetscInt n, PetscInt p, PetscInt mstart, PetscInt nstart, PetscInt pstart, PetscScalar ***a[])
3082: {
3083:   void *dummy;

3085:   PetscFunctionBegin;
3087:   PetscAssertPointer(a, 8);
3089:   dummy = (void *)(*a + mstart);
3090:   PetscCall(PetscFree(dummy));
3091:   PetscCall(VecRestoreArray(x, NULL));
3092:   *a = NULL;
3093:   PetscFunctionReturn(PETSC_SUCCESS);
3094: }

3096: /*@C
3097:   VecRestoreArray3dWrite - Restores a vector after `VecGetArray3dWrite()` has been called.

3099:   Logically Collective

3101:   Input Parameters:
3102: + x      - the vector
3103: . m      - first dimension of three dimensional array
3104: . n      - second dimension of the three dimensional array
3105: . p      - third dimension of the three dimensional array
3106: . mstart - first index you will use in first coordinate direction (often 0)
3107: . nstart - first index in the second coordinate direction (often 0)
3108: . pstart - first index in the third coordinate direction (often 0)
3109: - a      - location of pointer to array obtained from VecGetArray3d()

3111:   Level: developer

3113:   Notes:
3114:   For regular PETSc vectors this routine does not involve any copies. For
3115:   any special vectors that do not store local vector data in a contiguous
3116:   array, this routine will copy the data back into the underlying
3117:   vector data structure from the array obtained with `VecGetArray()`.

3119:   This routine actually zeros out the `a` pointer.

3121: .seealso: [](ch_vectors), `Vec`, `VecGetArray()`, `VecRestoreArray()`, `VecRestoreArrays()`, `VecPlaceArray()`,
3122:           `VecGetArray2d()`, `VecGetArray3d()`, `VecRestoreArray3d()`, `DMDAVecGetArray()`, `DMDAVecRestoreArray()`
3123:           `VecGetArray1d()`, `VecRestoreArray1d()`, `VecGetArray4d()`, `VecRestoreArray4d()`
3124: @*/
3125: PetscErrorCode VecRestoreArray3dWrite(Vec x, PetscInt m, PetscInt n, PetscInt p, PetscInt mstart, PetscInt nstart, PetscInt pstart, PetscScalar ***a[])
3126: {
3127:   void *dummy;

3129:   PetscFunctionBegin;
3131:   PetscAssertPointer(a, 8);
3133:   dummy = (void *)(*a + mstart);
3134:   PetscCall(PetscFree(dummy));
3135:   PetscCall(VecRestoreArrayWrite(x, NULL));
3136:   *a = NULL;
3137:   PetscFunctionReturn(PETSC_SUCCESS);
3138: }

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

3145:   Logically Collective

3147:   Input Parameters:
3148: + x      - the vector
3149: . m      - first dimension of four dimensional array
3150: . n      - second dimension of four dimensional array
3151: . p      - third dimension of four dimensional array
3152: . q      - fourth dimension of four dimensional array
3153: . mstart - first index you will use in first coordinate direction (often 0)
3154: . nstart - first index in the second coordinate direction (often 0)
3155: . pstart - first index in the third coordinate direction (often 0)
3156: - qstart - first index in the fourth coordinate direction (often 0)

3158:   Output Parameter:
3159: . a - location to put pointer to the array

3161:   Level: developer

3163:   Notes:
3164:   For a vector obtained from `DMCreateLocalVector()` `mstart`, `nstart`, and `pstart` are likely
3165:   obtained from the corner indices obtained from `DMDAGetGhostCorners()` while for
3166:   `DMCreateGlobalVector()` they are the corner indices from `DMDAGetCorners()`. In both cases
3167:   the arguments from `DMDAGet[Ghost]Corners()` are reversed in the call to `VecGetArray3d()`.

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

3171: .seealso: [](ch_vectors), `Vec`, `VecGetArray()`, `VecRestoreArray()`, `VecGetArrays()`, `VecPlaceArray()`,
3172:           `VecRestoreArray2d()`, `DMDAVecGetarray()`, `DMDAVecRestoreArray()`, `VecGetArray3d()`, `VecRestoreArray3d()`,
3173:           `VecGetArray1d()`, `VecRestoreArray1d()`, `VecRestoreArray4d()`
3174: @*/
3175: PetscErrorCode VecGetArray4d(Vec x, PetscInt m, PetscInt n, PetscInt p, PetscInt q, PetscInt mstart, PetscInt nstart, PetscInt pstart, PetscInt qstart, PetscScalar ****a[])
3176: {
3177:   PetscInt     i, N, j, k;
3178:   PetscScalar *aa, ***b, **c;

3180:   PetscFunctionBegin;
3182:   PetscAssertPointer(a, 10);
3184:   PetscCall(VecGetLocalSize(x, &N));
3185:   PetscCheck(m * n * p * q == N, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Local array size %" PetscInt_FMT " does not match 4d array dimensions %" PetscInt_FMT " by %" PetscInt_FMT " by %" PetscInt_FMT " by %" PetscInt_FMT, N, m, n, p, q);
3186:   PetscCall(VecGetArray(x, &aa));

3188:   PetscCall(PetscMalloc(m * sizeof(PetscScalar ***) + m * n * sizeof(PetscScalar **) + m * n * p * sizeof(PetscScalar *), a));
3189:   b = (PetscScalar ***)((*a) + m);
3190:   c = (PetscScalar **)(b + m * n);
3191:   for (i = 0; i < m; i++) (*a)[i] = b + i * n - nstart;
3192:   for (i = 0; i < m; i++)
3193:     for (j = 0; j < n; j++) b[i * n + j] = c + i * n * p + j * p - pstart;
3194:   for (i = 0; i < m; i++)
3195:     for (j = 0; j < n; j++)
3196:       for (k = 0; k < p; k++) c[i * n * p + j * p + k] = aa + i * n * p * q + j * p * q + k * q - qstart;
3197:   *a -= mstart;
3198:   PetscFunctionReturn(PETSC_SUCCESS);
3199: }

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

3206:   Logically Collective

3208:   Input Parameters:
3209: + x      - the vector
3210: . m      - first dimension of four dimensional array
3211: . n      - second dimension of four dimensional array
3212: . p      - third dimension of four dimensional array
3213: . q      - fourth dimension of four dimensional array
3214: . mstart - first index you will use in first coordinate direction (often 0)
3215: . nstart - first index in the second coordinate direction (often 0)
3216: . pstart - first index in the third coordinate direction (often 0)
3217: - qstart - first index in the fourth coordinate direction (often 0)

3219:   Output Parameter:
3220: . a - location to put pointer to the array

3222:   Level: developer

3224:   Notes:
3225:   For a vector obtained from `DMCreateLocalVector()` `mstart`, `nstart`, and `pstart` are likely
3226:   obtained from the corner indices obtained from `DMDAGetGhostCorners()` while for
3227:   `DMCreateGlobalVector()` they are the corner indices from `DMDAGetCorners()`. In both cases
3228:   the arguments from `DMDAGet[Ghost]Corners()` are reversed in the call to `VecGetArray3d()`.

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

3232: .seealso: [](ch_vectors), `Vec`, `VecGetArray()`, `VecRestoreArray()`, `VecGetArrays()`, `VecPlaceArray()`,
3233:           `VecRestoreArray2d()`, `DMDAVecGetarray()`, `DMDAVecRestoreArray()`, `VecGetArray3d()`, `VecRestoreArray3d()`,
3234:           `VecGetArray1d()`, `VecRestoreArray1d()`, `VecGetArray4d()`, `VecRestoreArray4d()`
3235: @*/
3236: PetscErrorCode VecGetArray4dWrite(Vec x, PetscInt m, PetscInt n, PetscInt p, PetscInt q, PetscInt mstart, PetscInt nstart, PetscInt pstart, PetscInt qstart, PetscScalar ****a[])
3237: {
3238:   PetscInt     i, N, j, k;
3239:   PetscScalar *aa, ***b, **c;

3241:   PetscFunctionBegin;
3243:   PetscAssertPointer(a, 10);
3245:   PetscCall(VecGetLocalSize(x, &N));
3246:   PetscCheck(m * n * p * q == N, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Local array size %" PetscInt_FMT " does not match 4d array dimensions %" PetscInt_FMT " by %" PetscInt_FMT " by %" PetscInt_FMT " by %" PetscInt_FMT, N, m, n, p, q);
3247:   PetscCall(VecGetArrayWrite(x, &aa));

3249:   PetscCall(PetscMalloc(m * sizeof(PetscScalar ***) + m * n * sizeof(PetscScalar **) + m * n * p * sizeof(PetscScalar *), a));
3250:   b = (PetscScalar ***)((*a) + m);
3251:   c = (PetscScalar **)(b + m * n);
3252:   for (i = 0; i < m; i++) (*a)[i] = b + i * n - nstart;
3253:   for (i = 0; i < m; i++)
3254:     for (j = 0; j < n; j++) b[i * n + j] = c + i * n * p + j * p - pstart;
3255:   for (i = 0; i < m; i++)
3256:     for (j = 0; j < n; j++)
3257:       for (k = 0; k < p; k++) c[i * n * p + j * p + k] = aa + i * n * p * q + j * p * q + k * q - qstart;
3258:   *a -= mstart;
3259:   PetscFunctionReturn(PETSC_SUCCESS);
3260: }

3262: /*@C
3263:   VecRestoreArray4d - Restores a vector after `VecGetArray4d()` has been called.

3265:   Logically Collective

3267:   Input Parameters:
3268: + x      - the vector
3269: . m      - first dimension of four dimensional array
3270: . n      - second dimension of the four dimensional array
3271: . p      - third dimension of the four dimensional array
3272: . q      - fourth dimension of the four dimensional array
3273: . mstart - first index you will use in first coordinate direction (often 0)
3274: . nstart - first index in the second coordinate direction (often 0)
3275: . pstart - first index in the third coordinate direction (often 0)
3276: . qstart - first index in the fourth coordinate direction (often 0)
3277: - a      - location of pointer to array obtained from VecGetArray4d()

3279:   Level: developer

3281:   Notes:
3282:   For regular PETSc vectors this routine does not involve any copies. For
3283:   any special vectors that do not store local vector data in a contiguous
3284:   array, this routine will copy the data back into the underlying
3285:   vector data structure from the array obtained with `VecGetArray()`.

3287:   This routine actually zeros out the `a` pointer.

3289: .seealso: [](ch_vectors), `Vec`, `VecGetArray()`, `VecRestoreArray()`, `VecRestoreArrays()`, `VecPlaceArray()`,
3290:           `VecGetArray2d()`, `VecGetArray3d()`, `VecRestoreArray3d()`, `DMDAVecGetArray()`, `DMDAVecRestoreArray()`
3291:           `VecGetArray1d()`, `VecRestoreArray1d()`, `VecGetArray4d()`
3292: @*/
3293: PetscErrorCode VecRestoreArray4d(Vec x, PetscInt m, PetscInt n, PetscInt p, PetscInt q, PetscInt mstart, PetscInt nstart, PetscInt pstart, PetscInt qstart, PetscScalar ****a[])
3294: {
3295:   void *dummy;

3297:   PetscFunctionBegin;
3299:   PetscAssertPointer(a, 10);
3301:   dummy = (void *)(*a + mstart);
3302:   PetscCall(PetscFree(dummy));
3303:   PetscCall(VecRestoreArray(x, NULL));
3304:   *a = NULL;
3305:   PetscFunctionReturn(PETSC_SUCCESS);
3306: }

3308: /*@C
3309:   VecRestoreArray4dWrite - Restores a vector after `VecGetArray4dWrite()` has been called.

3311:   Logically Collective

3313:   Input Parameters:
3314: + x      - the vector
3315: . m      - first dimension of four dimensional array
3316: . n      - second dimension of the four dimensional array
3317: . p      - third dimension of the four dimensional array
3318: . q      - fourth dimension of the four dimensional array
3319: . mstart - first index you will use in first coordinate direction (often 0)
3320: . nstart - first index in the second coordinate direction (often 0)
3321: . pstart - first index in the third coordinate direction (often 0)
3322: . qstart - first index in the fourth coordinate direction (often 0)
3323: - a      - location of pointer to array obtained from `VecGetArray4d()`

3325:   Level: developer

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

3333:   This routine actually zeros out the `a` pointer.

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

3343:   PetscFunctionBegin;
3345:   PetscAssertPointer(a, 10);
3347:   dummy = (void *)(*a + mstart);
3348:   PetscCall(PetscFree(dummy));
3349:   PetscCall(VecRestoreArrayWrite(x, NULL));
3350:   *a = NULL;
3351:   PetscFunctionReturn(PETSC_SUCCESS);
3352: }

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

3359:   Logically Collective

3361:   Input Parameters:
3362: + x      - the vector
3363: . m      - first dimension of two dimensional array
3364: . n      - second dimension of two dimensional array
3365: . mstart - first index you will use in first coordinate direction (often 0)
3366: - nstart - first index in the second coordinate direction (often 0)

3368:   Output Parameter:
3369: . a - location to put pointer to the array

3371:   Level: developer

3373:   Notes:
3374:   For a vector obtained from `DMCreateLocalVector()` `mstart` and `nstart` are likely
3375:   obtained from the corner indices obtained from `DMDAGetGhostCorners()` while for
3376:   `DMCreateGlobalVector()` they are the corner indices from `DMDAGetCorners()`. In both cases
3377:   the arguments from `DMDAGet[Ghost]Corners()` are reversed in the call to `VecGetArray2d()`.

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

3381: .seealso: [](ch_vectors), `Vec`, `VecGetArray()`, `VecRestoreArray()`, `VecGetArrays()`, `VecPlaceArray()`,
3382:           `VecRestoreArray2d()`, `DMDAVecGetArray()`, `DMDAVecRestoreArray()`, `VecGetArray3d()`, `VecRestoreArray3d()`,
3383:           `VecGetArray1d()`, `VecRestoreArray1d()`, `VecGetArray4d()`, `VecRestoreArray4d()`
3384: @*/
3385: PetscErrorCode VecGetArray2dRead(Vec x, PetscInt m, PetscInt n, PetscInt mstart, PetscInt nstart, PetscScalar **a[])
3386: {
3387:   PetscInt           i, N;
3388:   const PetscScalar *aa;

3390:   PetscFunctionBegin;
3392:   PetscAssertPointer(a, 6);
3394:   PetscCall(VecGetLocalSize(x, &N));
3395:   PetscCheck(m * n == N, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Local array size %" PetscInt_FMT " does not match 2d array dimensions %" PetscInt_FMT " by %" PetscInt_FMT, N, m, n);
3396:   PetscCall(VecGetArrayRead(x, &aa));

3398:   PetscCall(PetscMalloc1(m, a));
3399:   for (i = 0; i < m; i++) (*a)[i] = (PetscScalar *)aa + i * n - nstart;
3400:   *a -= mstart;
3401:   PetscFunctionReturn(PETSC_SUCCESS);
3402: }

3404: /*@C
3405:   VecRestoreArray2dRead - Restores a vector after `VecGetArray2dRead()` has been called.

3407:   Logically Collective

3409:   Input Parameters:
3410: + x      - the vector
3411: . m      - first dimension of two dimensional array
3412: . n      - second dimension of the two dimensional array
3413: . mstart - first index you will use in first coordinate direction (often 0)
3414: . nstart - first index in the second coordinate direction (often 0)
3415: - a      - location of pointer to array obtained from VecGetArray2d()

3417:   Level: developer

3419:   Notes:
3420:   For regular PETSc vectors this routine does not involve any copies. For
3421:   any special vectors that do not store local vector data in a contiguous
3422:   array, this routine will copy the data back into the underlying
3423:   vector data structure from the array obtained with `VecGetArray()`.

3425:   This routine actually zeros out the `a` pointer.

3427: .seealso: [](ch_vectors), `Vec`, `VecGetArray()`, `VecRestoreArray()`, `VecRestoreArrays()`, `VecPlaceArray()`,
3428:           `VecGetArray2d()`, `VecGetArray3d()`, `VecRestoreArray3d()`, `DMDAVecGetArray()`, `DMDAVecRestoreArray()`
3429:           `VecGetArray1d()`, `VecRestoreArray1d()`, `VecGetArray4d()`, `VecRestoreArray4d()`
3430: @*/
3431: PetscErrorCode VecRestoreArray2dRead(Vec x, PetscInt m, PetscInt n, PetscInt mstart, PetscInt nstart, PetscScalar **a[])
3432: {
3433:   void *dummy;

3435:   PetscFunctionBegin;
3437:   PetscAssertPointer(a, 6);
3439:   dummy = (void *)(*a + mstart);
3440:   PetscCall(PetscFree(dummy));
3441:   PetscCall(VecRestoreArrayRead(x, NULL));
3442:   *a = NULL;
3443:   PetscFunctionReturn(PETSC_SUCCESS);
3444: }

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

3451:   Logically Collective

3453:   Input Parameters:
3454: + x      - the vector
3455: . m      - first dimension of two dimensional array
3456: - mstart - first index you will use in first coordinate direction (often 0)

3458:   Output Parameter:
3459: . a - location to put pointer to the array

3461:   Level: developer

3463:   Notes:
3464:   For a vector obtained from `DMCreateLocalVector()` `mstart` is likely
3465:   obtained from the corner indices obtained from `DMDAGetGhostCorners()` while for
3466:   `DMCreateGlobalVector()` they are the corner indices from `DMDAGetCorners()`.

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

3470: .seealso: [](ch_vectors), `Vec`, `VecGetArray()`, `VecRestoreArray()`, `VecGetArrays()`, `VecPlaceArray()`,
3471:           `VecRestoreArray2d()`, `DMDAVecGetArray()`, `DMDAVecRestoreArray()`, `VecGetArray3d()`, `VecRestoreArray3d()`,
3472:           `VecGetArray2d()`, `VecRestoreArray1d()`, `VecGetArray4d()`, `VecRestoreArray4d()`
3473: @*/
3474: PetscErrorCode VecGetArray1dRead(Vec x, PetscInt m, PetscInt mstart, PetscScalar *a[])
3475: {
3476:   PetscInt N;

3478:   PetscFunctionBegin;
3480:   PetscAssertPointer(a, 4);
3482:   PetscCall(VecGetLocalSize(x, &N));
3483:   PetscCheck(m == N, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Local array size %" PetscInt_FMT " does not match 1d array dimensions %" PetscInt_FMT, N, m);
3484:   PetscCall(VecGetArrayRead(x, (const PetscScalar **)a));
3485:   *a -= mstart;
3486:   PetscFunctionReturn(PETSC_SUCCESS);
3487: }

3489: /*@C
3490:   VecRestoreArray1dRead - Restores a vector after `VecGetArray1dRead()` has been called.

3492:   Logically Collective

3494:   Input Parameters:
3495: + x      - the vector
3496: . m      - first dimension of two dimensional array
3497: . mstart - first index you will use in first coordinate direction (often 0)
3498: - a      - location of pointer to array obtained from `VecGetArray1dRead()`

3500:   Level: developer

3502:   Notes:
3503:   For regular PETSc vectors this routine does not involve any copies. For
3504:   any special vectors that do not store local vector data in a contiguous
3505:   array, this routine will copy the data back into the underlying
3506:   vector data structure from the array obtained with `VecGetArray1dRead()`.

3508:   This routine actually zeros out the `a` pointer.

3510: .seealso: [](ch_vectors), `Vec`, `VecGetArray()`, `VecRestoreArray()`, `VecRestoreArrays()`, `VecPlaceArray()`,
3511:           `VecGetArray2d()`, `VecGetArray3d()`, `VecRestoreArray3d()`, `DMDAVecGetArray()`, `DMDAVecRestoreArray()`
3512:           `VecGetArray1d()`, `VecRestoreArray2d()`, `VecGetArray4d()`, `VecRestoreArray4d()`
3513: @*/
3514: PetscErrorCode VecRestoreArray1dRead(Vec x, PetscInt m, PetscInt mstart, PetscScalar *a[])
3515: {
3516:   PetscFunctionBegin;
3519:   PetscCall(VecRestoreArrayRead(x, NULL));
3520:   *a = NULL;
3521:   PetscFunctionReturn(PETSC_SUCCESS);
3522: }

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

3529:   Logically Collective

3531:   Input Parameters:
3532: + x      - the vector
3533: . m      - first dimension of three dimensional array
3534: . n      - second dimension of three dimensional array
3535: . p      - third dimension of three dimensional array
3536: . mstart - first index you will use in first coordinate direction (often 0)
3537: . nstart - first index in the second coordinate direction (often 0)
3538: - pstart - first index in the third coordinate direction (often 0)

3540:   Output Parameter:
3541: . a - location to put pointer to the array

3543:   Level: developer

3545:   Notes:
3546:   For a vector obtained from `DMCreateLocalVector()` `mstart`, `nstart`, and `pstart` are likely
3547:   obtained from the corner indices obtained from `DMDAGetGhostCorners()` while for
3548:   `DMCreateGlobalVector()` they are the corner indices from `DMDAGetCorners()`. In both cases
3549:   the arguments from `DMDAGet[Ghost]Corners()` are reversed in the call to `VecGetArray3dRead()`.

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

3553: .seealso: [](ch_vectors), `Vec`, `VecGetArray()`, `VecRestoreArray()`, `VecGetArrays()`, `VecPlaceArray()`,
3554:           `VecRestoreArray2d()`, `DMDAVecGetarray()`, `DMDAVecRestoreArray()`, `VecGetArray3d()`, `VecRestoreArray3d()`,
3555:           `VecGetArray1d()`, `VecRestoreArray1d()`, `VecGetArray4d()`, `VecRestoreArray4d()`
3556: @*/
3557: PetscErrorCode VecGetArray3dRead(Vec x, PetscInt m, PetscInt n, PetscInt p, PetscInt mstart, PetscInt nstart, PetscInt pstart, PetscScalar ***a[])
3558: {
3559:   PetscInt           i, N, j;
3560:   const PetscScalar *aa;
3561:   PetscScalar      **b;

3563:   PetscFunctionBegin;
3565:   PetscAssertPointer(a, 8);
3567:   PetscCall(VecGetLocalSize(x, &N));
3568:   PetscCheck(m * n * p == N, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Local array size %" PetscInt_FMT " does not match 3d array dimensions %" PetscInt_FMT " by %" PetscInt_FMT " by %" PetscInt_FMT, N, m, n, p);
3569:   PetscCall(VecGetArrayRead(x, &aa));

3571:   PetscCall(PetscMalloc(m * sizeof(PetscScalar **) + m * n * sizeof(PetscScalar *), a));
3572:   b = (PetscScalar **)((*a) + m);
3573:   for (i = 0; i < m; i++) (*a)[i] = b + i * n - nstart;
3574:   for (i = 0; i < m; i++)
3575:     for (j = 0; j < n; j++) b[i * n + j] = PetscSafePointerPlusOffset((PetscScalar *)aa, i * n * p + j * p - pstart);
3576:   *a -= mstart;
3577:   PetscFunctionReturn(PETSC_SUCCESS);
3578: }

3580: /*@C
3581:   VecRestoreArray3dRead - Restores a vector after `VecGetArray3dRead()` has been called.

3583:   Logically Collective

3585:   Input Parameters:
3586: + x      - the vector
3587: . m      - first dimension of three dimensional array
3588: . n      - second dimension of the three dimensional array
3589: . p      - third dimension of the three dimensional array
3590: . mstart - first index you will use in first coordinate direction (often 0)
3591: . nstart - first index in the second coordinate direction (often 0)
3592: . pstart - first index in the third coordinate direction (often 0)
3593: - a      - location of pointer to array obtained from `VecGetArray3dRead()`

3595:   Level: developer

3597:   Notes:
3598:   For regular PETSc vectors this routine does not involve any copies. For
3599:   any special vectors that do not store local vector data in a contiguous
3600:   array, this routine will copy the data back into the underlying
3601:   vector data structure from the array obtained with `VecGetArray()`.

3603:   This routine actually zeros out the `a` pointer.

3605: .seealso: [](ch_vectors), `Vec`, `VecGetArray()`, `VecRestoreArray()`, `VecRestoreArrays()`, `VecPlaceArray()`,
3606:           `VecGetArray2d()`, `VecGetArray3d()`, `VecRestoreArray3d()`, `DMDAVecGetArray()`, `DMDAVecRestoreArray()`
3607:           `VecGetArray1d()`, `VecRestoreArray1d()`, `VecGetArray4d()`, `VecRestoreArray4d()`
3608: @*/
3609: PetscErrorCode VecRestoreArray3dRead(Vec x, PetscInt m, PetscInt n, PetscInt p, PetscInt mstart, PetscInt nstart, PetscInt pstart, PetscScalar ***a[])
3610: {
3611:   void *dummy;

3613:   PetscFunctionBegin;
3615:   PetscAssertPointer(a, 8);
3617:   dummy = (void *)(*a + mstart);
3618:   PetscCall(PetscFree(dummy));
3619:   PetscCall(VecRestoreArrayRead(x, NULL));
3620:   *a = NULL;
3621:   PetscFunctionReturn(PETSC_SUCCESS);
3622: }

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

3629:   Logically Collective

3631:   Input Parameters:
3632: + x      - the vector
3633: . m      - first dimension of four dimensional array
3634: . n      - second dimension of four dimensional array
3635: . p      - third dimension of four dimensional array
3636: . q      - fourth dimension of four dimensional array
3637: . mstart - first index you will use in first coordinate direction (often 0)
3638: . nstart - first index in the second coordinate direction (often 0)
3639: . pstart - first index in the third coordinate direction (often 0)
3640: - qstart - first index in the fourth coordinate direction (often 0)

3642:   Output Parameter:
3643: . a - location to put pointer to the array

3645:   Level: beginner

3647:   Notes:
3648:   For a vector obtained from `DMCreateLocalVector()` `mstart`, `nstart`, and `pstart` are likely
3649:   obtained from the corner indices obtained from `DMDAGetGhostCorners()` while for
3650:   `DMCreateGlobalVector()` they are the corner indices from `DMDAGetCorners()`. In both cases
3651:   the arguments from `DMDAGet[Ghost]Corners()` are reversed in the call to `VecGetArray3d()`.

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

3655: .seealso: [](ch_vectors), `Vec`, `VecGetArray()`, `VecRestoreArray()`, `VecGetArrays()`, `VecPlaceArray()`,
3656:           `VecRestoreArray2d()`, `DMDAVecGetarray()`, `DMDAVecRestoreArray()`, `VecGetArray3d()`, `VecRestoreArray3d()`,
3657:           `VecGetArray1d()`, `VecRestoreArray1d()`, `VecGetArray4d()`, `VecRestoreArray4d()`
3658: @*/
3659: PetscErrorCode VecGetArray4dRead(Vec x, PetscInt m, PetscInt n, PetscInt p, PetscInt q, PetscInt mstart, PetscInt nstart, PetscInt pstart, PetscInt qstart, PetscScalar ****a[])
3660: {
3661:   PetscInt           i, N, j, k;
3662:   const PetscScalar *aa;
3663:   PetscScalar     ***b, **c;

3665:   PetscFunctionBegin;
3667:   PetscAssertPointer(a, 10);
3669:   PetscCall(VecGetLocalSize(x, &N));
3670:   PetscCheck(m * n * p * q == N, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Local array size %" PetscInt_FMT " does not match 4d array dimensions %" PetscInt_FMT " by %" PetscInt_FMT " by %" PetscInt_FMT " by %" PetscInt_FMT, N, m, n, p, q);
3671:   PetscCall(VecGetArrayRead(x, &aa));

3673:   PetscCall(PetscMalloc(m * sizeof(PetscScalar ***) + m * n * sizeof(PetscScalar **) + m * n * p * sizeof(PetscScalar *), a));
3674:   b = (PetscScalar ***)((*a) + m);
3675:   c = (PetscScalar **)(b + m * n);
3676:   for (i = 0; i < m; i++) (*a)[i] = b + i * n - nstart;
3677:   for (i = 0; i < m; i++)
3678:     for (j = 0; j < n; j++) b[i * n + j] = c + i * n * p + j * p - pstart;
3679:   for (i = 0; i < m; i++)
3680:     for (j = 0; j < n; j++)
3681:       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;
3682:   *a -= mstart;
3683:   PetscFunctionReturn(PETSC_SUCCESS);
3684: }

3686: /*@C
3687:   VecRestoreArray4dRead - Restores a vector after `VecGetArray4d()` has been called.

3689:   Logically Collective

3691:   Input Parameters:
3692: + x      - the vector
3693: . m      - first dimension of four dimensional array
3694: . n      - second dimension of the four dimensional array
3695: . p      - third dimension of the four dimensional array
3696: . q      - fourth dimension of the four dimensional array
3697: . mstart - first index you will use in first coordinate direction (often 0)
3698: . nstart - first index in the second coordinate direction (often 0)
3699: . pstart - first index in the third coordinate direction (often 0)
3700: . qstart - first index in the fourth coordinate direction (often 0)
3701: - a      - location of pointer to array obtained from `VecGetArray4dRead()`

3703:   Level: beginner

3705:   Notes:
3706:   For regular PETSc vectors this routine does not involve any copies. For
3707:   any special vectors that do not store local vector data in a contiguous
3708:   array, this routine will copy the data back into the underlying
3709:   vector data structure from the array obtained with `VecGetArray()`.

3711:   This routine actually zeros out the `a` pointer.

3713: .seealso: [](ch_vectors), `Vec`, `VecGetArray()`, `VecRestoreArray()`, `VecRestoreArrays()`, `VecPlaceArray()`,
3714:           `VecGetArray2d()`, `VecGetArray3d()`, `VecRestoreArray3d()`, `DMDAVecGetArray()`, `DMDAVecRestoreArray()`
3715:           `VecGetArray1d()`, `VecRestoreArray1d()`, `VecGetArray4d()`, `VecRestoreArray4d()`
3716: @*/
3717: PetscErrorCode VecRestoreArray4dRead(Vec x, PetscInt m, PetscInt n, PetscInt p, PetscInt q, PetscInt mstart, PetscInt nstart, PetscInt pstart, PetscInt qstart, PetscScalar ****a[])
3718: {
3719:   void *dummy;

3721:   PetscFunctionBegin;
3723:   PetscAssertPointer(a, 10);
3725:   dummy = (void *)(*a + mstart);
3726:   PetscCall(PetscFree(dummy));
3727:   PetscCall(VecRestoreArrayRead(x, NULL));
3728:   *a = NULL;
3729:   PetscFunctionReturn(PETSC_SUCCESS);
3730: }

3732: /*@
3733:   VecLockGet - Get the current lock status of a vector

3735:   Logically Collective

3737:   Input Parameter:
3738: . x - the vector

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

3744:   Level: advanced

3746: .seealso: [](ch_vectors), `Vec`, `VecRestoreArray()`, `VecGetArrayRead()`, `VecLockReadPush()`, `VecLockReadPop()`
3747: @*/
3748: PetscErrorCode VecLockGet(Vec x, PetscInt *state)
3749: {
3750:   PetscFunctionBegin;
3752:   PetscAssertPointer(state, 2);
3753:   *state = x->lock;
3754:   PetscFunctionReturn(PETSC_SUCCESS);
3755: }

3757: PetscErrorCode VecLockGetLocation(Vec x, const char *file[], const char *func[], int *line)
3758: {
3759:   PetscFunctionBegin;
3761:   PetscAssertPointer(file, 2);
3762:   PetscAssertPointer(func, 3);
3763:   PetscAssertPointer(line, 4);
3764: #if PetscDefined(USE_DEBUG) && !PetscDefined(HAVE_THREADSAFETY)
3765:   {
3766:     const int index = x->lockstack.currentsize - 1;

3768:     *file = index < 0 ? NULL : x->lockstack.file[index];
3769:     *func = index < 0 ? NULL : x->lockstack.function[index];
3770:     *line = index < 0 ? 0 : x->lockstack.line[index];
3771:   }
3772: #else
3773:   *file = NULL;
3774:   *func = NULL;
3775:   *line = 0;
3776: #endif
3777:   PetscFunctionReturn(PETSC_SUCCESS);
3778: }

3780: /*@
3781:   VecLockReadPush - Push a read-only lock on a vector to prevent it from being written to

3783:   Logically Collective

3785:   Input Parameter:
3786: . x - the vector

3788:   Level: intermediate

3790:   Notes:
3791:   If this is set then calls to `VecGetArray()` or `VecSetValues()` or any other routines that change the vectors values will generate an error.

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

3796: .seealso: [](ch_vectors), `Vec`, `VecRestoreArray()`, `VecGetArrayRead()`, `VecLockReadPop()`, `VecLockGet()`
3797: @*/
3798: PetscErrorCode VecLockReadPush(Vec x)
3799: {
3800:   PetscFunctionBegin;
3802:   PetscCheck(x->lock++ >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Vector is already locked for exclusive write access but you want to read it");
3803: #if PetscDefined(USE_DEBUG) && !PetscDefined(HAVE_THREADSAFETY)
3804:   {
3805:     const char *file, *func;
3806:     int         index, line;

3808:     if ((index = petscstack.currentsize - 2) < 0) {
3809:       // vec was locked "outside" of petsc, either in user-land or main. the error message will
3810:       // now show this function as the culprit, but it will include the stacktrace
3811:       file = "unknown user-file";
3812:       func = "unknown_user_function";
3813:       line = 0;
3814:     } else {
3815:       file = petscstack.file[index];
3816:       func = petscstack.function[index];
3817:       line = petscstack.line[index];
3818:     }
3819:     PetscStackPush_Private(x->lockstack, file, func, line, petscstack.petscroutine[index], PETSC_FALSE);
3820:   }
3821: #endif
3822:   PetscFunctionReturn(PETSC_SUCCESS);
3823: }

3825: /*@
3826:   VecLockReadPop - Pop a read-only lock from a vector

3828:   Logically Collective

3830:   Input Parameter:
3831: . x - the vector

3833:   Level: intermediate

3835: .seealso: [](ch_vectors), `Vec`, `VecRestoreArray()`, `VecGetArrayRead()`, `VecLockReadPush()`, `VecLockGet()`
3836: @*/
3837: PetscErrorCode VecLockReadPop(Vec x)
3838: {
3839:   PetscFunctionBegin;
3841:   PetscCheck(--x->lock >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Vector has been unlocked from read-only access too many times");
3842: #if PetscDefined(USE_DEBUG) && !PetscDefined(HAVE_THREADSAFETY)
3843:   {
3844:     const char *previous = x->lockstack.function[x->lockstack.currentsize - 1];

3846:     PetscStackPop_Private(x->lockstack, previous);
3847:   }
3848: #endif
3849:   PetscFunctionReturn(PETSC_SUCCESS);
3850: }

3852: /*@
3853:   VecLockWriteSet - Lock or unlock a vector for exclusive read/write access

3855:   Logically Collective

3857:   Input Parameters:
3858: + x   - the vector
3859: - flg - `PETSC_TRUE` to lock the vector for exclusive read/write access; `PETSC_FALSE` to unlock it.

3861:   Level: intermediate

3863:   Notes:
3864:   The function is useful in split-phase computations, which usually have a begin phase and an end phase.
3865:   One can call `VecLockWriteSet`(x,`PETSC_TRUE`) in the begin phase to lock a vector for exclusive
3866:   access, and call `VecLockWriteSet`(x,`PETSC_FALSE`) in the end phase to unlock the vector from exclusive
3867:   access. In this way, one is ensured no other operations can access the vector in between. The code may like

3869: .vb
3870:        VecGetArray(x,&xdata); // begin phase
3871:        VecLockWriteSet(v,PETSC_TRUE);

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

3875:        VecRestoreArray(x,&vdata); // end phase
3876:        VecLockWriteSet(v,PETSC_FALSE);
3877: .ve

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

3882: .seealso: [](ch_vectors), `Vec`, `VecRestoreArray()`, `VecGetArrayRead()`, `VecLockReadPush()`, `VecLockReadPop()`, `VecLockGet()`
3883: @*/
3884: PetscErrorCode VecLockWriteSet(Vec x, PetscBool flg)
3885: {
3886:   PetscFunctionBegin;
3888:   if (flg) {
3889:     PetscCheck(x->lock <= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Vector is already locked for read-only access but you want to write it");
3890:     PetscCheck(x->lock >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Vector is already locked for exclusive write access but you want to write it");
3891:     x->lock = -1;
3892:   } else {
3893:     PetscCheck(x->lock == -1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Vector is not locked for exclusive write access but you want to unlock it from that");
3894:     x->lock = 0;
3895:   }
3896:   PetscFunctionReturn(PETSC_SUCCESS);
3897: }