Actual source code: rvector.c

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

 14: PETSC_EXTERN PetscErrorCode VecValidValues(Vec vec,PetscInt argnum,PetscBool begin)
 15: {
 16: #if defined(PETSC_USE_DEBUG)
 17:   PetscErrorCode    ierr;
 18:   PetscInt          n,i;
 19:   const PetscScalar *x;

 22: #if defined(PETSC_HAVE_CUDA) || defined(PETSC_HAVE_VIENNACL)
 23:   if ((vec->petscnative || vec->ops->getarray) && (vec->offloadmask & PETSC_OFFLOAD_CPU)) {
 24: #else
 25:   if (vec->petscnative || vec->ops->getarray) {
 26: #endif
 27:     VecGetLocalSize(vec,&n);
 28:     VecGetArrayRead(vec,&x);
 29:     for (i=0; i<n; i++) {
 30:       if (begin) {
 31:         if (PetscIsInfOrNanScalar(x[i])) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FP,"Vec entry at local location %D is not-a-number or infinite at beginning of function: Parameter number %D",i,argnum);
 32:       } else {
 33:         if (PetscIsInfOrNanScalar(x[i])) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FP,"Vec entry at local location %D is not-a-number or infinite at end of function: Parameter number %D",i,argnum);
 34:       }
 35:     }
 36:     VecRestoreArrayRead(vec,&x);
 37:   }
 38: #else
 40: #endif
 41:   return(0);
 42: }

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

 47:    Logically Collective on Vec

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

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

 55:    Level: advanced

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

 61: .seealso: VecPointwiseDivide(), VecPointwiseMult(), VecPointwiseMax(), VecPointwiseMin(), VecPointwiseMaxAbs()
 62: @*/
 63: PetscErrorCode  VecMaxPointwiseDivide(Vec x,Vec y,PetscReal *max)
 64: {

 74:   VecCheckSameSize(x,1,y,2);
 75:   (*x->ops->maxpointwisedivide)(x,y,max);
 76:   return(0);
 77: }

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

 82:    Collective on Vec

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

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

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

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

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

106:    Level: intermediate


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

122:   VecCheckSameSize(x,1,y,2);

124:   PetscLogEventBegin(VEC_Dot,x,y,0,0);
125:   (*x->ops->dot)(x,y,val);
126:   PetscLogEventEnd(VEC_Dot,x,y,0,0);
127:   return(0);
128: }

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

133:    Collective on Vec

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

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

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

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

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

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

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

156:    Level: intermediate


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

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

172: /*@
173:    VecNorm  - Computes the vector norm.

175:    Collective on Vec

177:    Input Parameters:
178: +  x - the vector
179: -  type - one of NORM_1, NORM_2, NORM_INFINITY.  Also available
180:           NORM_1_AND_2, which computes both norms and stores them
181:           in a two element array.

183:    Output Parameter:
184: .  val - the norm

186:    Notes:
187: $     NORM_1 denotes sum_i |x_i|
188: $     NORM_2 denotes sqrt(sum_i |x_i|^2)
189: $     NORM_INFINITY denotes max_i |x_i|

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

196:    Level: intermediate

198:    Performance Issues:
199: $    per-processor memory bandwidth
200: $    interprocessor latency
201: $    work load inbalance that causes certain processes to arrive much earlier than others


204: .seealso: VecDot(), VecTDot(), VecNorm(), VecDotBegin(), VecDotEnd(), VecNormAvailable(),
205:           VecNormBegin(), VecNormEnd()

207: @*/

209: PetscErrorCode  VecNorm(Vec x,NormType type,PetscReal *val)
210: {
211:   PetscBool      flg;


219:   /*
220:    * Cached data?
221:    */
222:   if (type!=NORM_1_AND_2) {
223:     PetscObjectComposedDataGetReal((PetscObject)x,NormIds[type],*val,flg);
224:     if (flg) return(0);
225:   }
226:   PetscLogEventBegin(VEC_Norm,x,0,0,0);
227:   (*x->ops->norm)(x,type,val);
228:   PetscLogEventEnd(VEC_Norm,x,0,0,0);
229:   if (type!=NORM_1_AND_2) {
230:     PetscObjectComposedDataSetReal((PetscObject)x,NormIds[type],*val);
231:   }
232:   return(0);
233: }

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

238:    Not Collective

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

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

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

255:    Level: intermediate

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

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


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

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


281:   *available = PETSC_FALSE;
282:   if (type!=NORM_1_AND_2) {
283:     PetscObjectComposedDataGetReal((PetscObject)x,NormIds[type],*val,*available);
284:   }
285:   return(0);
286: }

288: /*@
289:    VecNormalize - Normalizes a vector by 2-norm.

291:    Collective on Vec

293:    Input Parameters:
294: +  x - the vector

296:    Output Parameter:
297: .  x - the normalized vector
298: -  val - the vector norm before normalization

300:    Level: intermediate


303: @*/
304: PetscErrorCode  VecNormalize(Vec x,PetscReal *val)
305: {
307:   PetscReal      norm;

312:   PetscLogEventBegin(VEC_Normalize,x,0,0,0);
313:   VecNorm(x,NORM_2,&norm);
314:   if (norm == 0.0) {
315:     PetscInfo(x,"Vector of zero norm can not be normalized; Returning only the zero norm\n");
316:   } else if (norm != 1.0) {
317:     PetscScalar tmp = 1.0/norm;
318:     VecScale(x,tmp);
319:   }
320:   if (val) *val = norm;
321:   PetscLogEventEnd(VEC_Normalize,x,0,0,0);
322:   return(0);
323: }

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

328:    Collective on Vec

330:    Input Parameter:
331: .  x - the vector

333:    Output Parameters:
334: +  p - the location of val (pass NULL if you don't want this)
335: -  val - the maximum component

337:    Notes:
338:    Returns the value PETSC_MIN_REAL and p = -1 if the vector is of length 0.

340:    Returns the smallest index with the maximum value
341:    Level: intermediate


344: .seealso: VecNorm(), VecMin()
345: @*/
346: PetscErrorCode  VecMax(Vec x,PetscInt *p,PetscReal *val)
347: {

354:   PetscLogEventBegin(VEC_Max,x,0,0,0);
355:   (*x->ops->max)(x,p,val);
356:   PetscLogEventEnd(VEC_Max,x,0,0,0);
357:   return(0);
358: }

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

363:    Collective on Vec

365:    Input Parameters:
366: .  x - the vector

368:    Output Parameter:
369: +  p - the location of val (pass NULL if you don't want this location)
370: -  val - the minimum component

372:    Level: intermediate

374:    Notes:
375:    Returns the value PETSC_MAX_REAL and p = -1 if the vector is of length 0.

377:    This returns the smallest index with the minumum value


380: .seealso: VecMax()
381: @*/
382: PetscErrorCode  VecMin(Vec x,PetscInt *p,PetscReal *val)
383: {

390:   PetscLogEventBegin(VEC_Min,x,0,0,0);
391:   (*x->ops->min)(x,p,val);
392:   PetscLogEventEnd(VEC_Min,x,0,0,0);
393:   return(0);
394: }

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

400:    Collective on Vec

402:    Input Parameters:
403: .  x, y - the vectors

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

408:    Notes for Users of Complex Numbers:
409:    For complex vectors, VecTDot() computes the indefinite form
410: $     val = (x,y) = y^T x,
411:    where y^T denotes the transpose of y.

413:    Use VecDot() for the inner product
414: $     val = (x,y) = y^H x,
415:    where y^H denotes the conjugate transpose of y.

417:    Level: intermediate

419: .seealso: VecDot(), VecMTDot()
420: @*/
421: PetscErrorCode  VecTDot(Vec x,Vec y,PetscScalar *val)
422: {

432:   VecCheckSameSize(x,1,y,2);

434:   PetscLogEventBegin(VEC_TDot,x,y,0,0);
435:   (*x->ops->tdot)(x,y,val);
436:   PetscLogEventEnd(VEC_TDot,x,y,0,0);
437:   return(0);
438: }

440: /*@
441:    VecScale - Scales a vector.

443:    Not collective on Vec

445:    Input Parameters:
446: +  x - the vector
447: -  alpha - the scalar

449:    Output Parameter:
450: .  x - the scaled vector

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

456:    Level: intermediate


459: @*/
460: PetscErrorCode  VecScale(Vec x, PetscScalar alpha)
461: {
462:   PetscReal      norms[4] = {0.0,0.0,0.0, 0.0};
463:   PetscBool      flgs[4];
465:   PetscInt       i;

470:   if (x->stash.insertmode != NOT_SET_VALUES) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled vector");
471:   PetscLogEventBegin(VEC_Scale,x,0,0,0);
472:   if (alpha != (PetscScalar)1.0) {
473:     VecSetErrorIfLocked(x,1);
474:     /* get current stashed norms */
475:     for (i=0; i<4; i++) {
476:       PetscObjectComposedDataGetReal((PetscObject)x,NormIds[i],norms[i],flgs[i]);
477:     }
478:     (*x->ops->scale)(x,alpha);
479:     PetscObjectStateIncrease((PetscObject)x);
480:     /* put the scaled stashed norms back into the Vec */
481:     for (i=0; i<4; i++) {
482:       if (flgs[i]) {
483:         PetscObjectComposedDataSetReal((PetscObject)x,NormIds[i],PetscAbsScalar(alpha)*norms[i]);
484:       }
485:     }
486:   }
487:   PetscLogEventEnd(VEC_Scale,x,0,0,0);
488:   return(0);
489: }

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

494:    Logically Collective on Vec

496:    Input Parameters:
497: +  x  - the vector
498: -  alpha - the scalar

500:    Output Parameter:
501: .  x  - the vector

503:    Note:
504:    For a vector of dimension n, VecSet() computes
505: $     x[i] = alpha, for i=1,...,n,
506:    so that all vector entries then equal the identical
507:    scalar value, alpha.  Use the more general routine
508:    VecSetValues() to set different vector entries.

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

513:    Level: beginner

515: .seealso VecSetValues(), VecSetValuesBlocked(), VecSetRandom()

517: @*/
518: PetscErrorCode  VecSet(Vec x,PetscScalar alpha)
519: {
520:   PetscReal      val;

526:   if (x->stash.insertmode != NOT_SET_VALUES) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"You cannot call this after you have called VecSetValues() but\n before you have called VecAssemblyBegin/End()");
528:   VecSetErrorIfLocked(x,1);

530:   PetscLogEventBegin(VEC_Set,x,0,0,0);
531:   (*x->ops->set)(x,alpha);
532:   PetscLogEventEnd(VEC_Set,x,0,0,0);
533:   PetscObjectStateIncrease((PetscObject)x);

535:   /*  norms can be simply set (if |alpha|*N not too large) */
536:   val  = PetscAbsScalar(alpha);
537:   if (x->map->N == 0) {
538:     PetscObjectComposedDataSetReal((PetscObject)x,NormIds[NORM_1],0.0l);
539:     PetscObjectComposedDataSetReal((PetscObject)x,NormIds[NORM_INFINITY],0.0);
540:     PetscObjectComposedDataSetReal((PetscObject)x,NormIds[NORM_2],0.0);
541:     PetscObjectComposedDataSetReal((PetscObject)x,NormIds[NORM_FROBENIUS],0.0);
542:   } else if (val > PETSC_MAX_REAL/x->map->N) {
543:     PetscObjectComposedDataSetReal((PetscObject)x,NormIds[NORM_INFINITY],val);
544:   } else {
545:     PetscObjectComposedDataSetReal((PetscObject)x,NormIds[NORM_1],x->map->N * val);
546:     PetscObjectComposedDataSetReal((PetscObject)x,NormIds[NORM_INFINITY],val);
547:     val  = PetscSqrtReal((PetscReal)x->map->N) * val;
548:     PetscObjectComposedDataSetReal((PetscObject)x,NormIds[NORM_2],val);
549:     PetscObjectComposedDataSetReal((PetscObject)x,NormIds[NORM_FROBENIUS],val);
550:   }
551:   return(0);
552: }


555: /*@
556:    VecAXPY - Computes y = alpha x + y.

558:    Logically Collective on Vec

560:    Input Parameters:
561: +  alpha - the scalar
562: -  x, y  - the vectors

564:    Output Parameter:
565: .  y - output vector

567:    Level: intermediate

569:    Notes:
570:     x and y MUST be different vectors
571:     This routine is optimized for alpha of 0.0, otherwise it calls the BLAS routine

573: $    VecAXPY(y,alpha,x)                   y = alpha x           +      y
574: $    VecAYPX(y,beta,x)                    y =       x           + beta y
575: $    VecAXPBY(y,alpha,beta,x)             y = alpha x           + beta y
576: $    VecWAXPY(w,alpha,x,y)                w = alpha x           +      y
577: $    VecAXPBYPCZ(w,alpha,beta,gamma,x,y)  z = alpha x           + beta y + gamma z
578: $    VecMAXPY(y,nv,alpha[],x[])           y = sum alpha[i] x[i] +      y


581: .seealso:  VecAYPX(), VecMAXPY(), VecWAXPY(), VecAXPBYPCZ(), VecAXPBY()
582: @*/
583: PetscErrorCode  VecAXPY(Vec y,PetscScalar alpha,Vec x)
584: {

593:   VecCheckSameSize(x,1,y,3);
594:   if (x == y) SETERRQ(PetscObjectComm((PetscObject)x),PETSC_ERR_ARG_IDN,"x and y cannot be the same vector");
596:   if (alpha == (PetscScalar)0.0) return(0);
597:   VecSetErrorIfLocked(y,1);

599:   VecLockReadPush(x);
600:   PetscLogEventBegin(VEC_AXPY,x,y,0,0);
601:   (*y->ops->axpy)(y,alpha,x);
602:   PetscLogEventEnd(VEC_AXPY,x,y,0,0);
603:   VecLockReadPop(x);
604:   PetscObjectStateIncrease((PetscObject)y);
605:   return(0);
606: }

608: /*@
609:    VecAXPBY - Computes y = alpha x + beta y.

611:    Logically Collective on Vec

613:    Input Parameters:
614: +  alpha,beta - the scalars
615: -  x, y  - the vectors

617:    Output Parameter:
618: .  y - output vector

620:    Level: intermediate

622:    Notes:
623:     x and y MUST be different vectors
624:     The implementation is optimized for alpha and/or beta values of 0.0 and 1.0


627: .seealso: VecAYPX(), VecMAXPY(), VecWAXPY(), VecAXPY(), VecAXPBYPCZ()
628: @*/
629: PetscErrorCode  VecAXPBY(Vec y,PetscScalar alpha,PetscScalar beta,Vec x)
630: {

639:   VecCheckSameSize(y,1,x,4);
640:   if (x == y) SETERRQ(PetscObjectComm((PetscObject)x),PETSC_ERR_ARG_IDN,"x and y cannot be the same vector");
643:   if (alpha == (PetscScalar)0.0 && beta == (PetscScalar)1.0) return(0);
644:   VecSetErrorIfLocked(y,1);
645:   PetscLogEventBegin(VEC_AXPY,x,y,0,0);
646:   (*y->ops->axpby)(y,alpha,beta,x);
647:   PetscLogEventEnd(VEC_AXPY,x,y,0,0);
648:   PetscObjectStateIncrease((PetscObject)y);
649:   return(0);
650: }

652: /*@
653:    VecAXPBYPCZ - Computes z = alpha x + beta y + gamma z

655:    Logically Collective on Vec

657:    Input Parameters:
658: +  alpha,beta, gamma - the scalars
659: -  x, y, z  - the vectors

661:    Output Parameter:
662: .  z - output vector

664:    Level: intermediate

666:    Notes:
667:     x, y and z must be different vectors
668:     The implementation is optimized for alpha of 1.0 and gamma of 1.0 or 0.0


671: .seealso:  VecAYPX(), VecMAXPY(), VecWAXPY(), VecAXPY(), VecAXPBY()
672: @*/
673: PetscErrorCode  VecAXPBYPCZ(Vec z,PetscScalar alpha,PetscScalar beta,PetscScalar gamma,Vec x,Vec y)
674: {

686:   VecCheckSameSize(x,1,y,5);
687:   VecCheckSameSize(x,1,z,6);
688:   if (x == y || x == z) SETERRQ(PetscObjectComm((PetscObject)x),PETSC_ERR_ARG_IDN,"x, y, and z must be different vectors");
689:   if (y == z) SETERRQ(PetscObjectComm((PetscObject)y),PETSC_ERR_ARG_IDN,"x, y, and z must be different vectors");
693:   if (alpha == (PetscScalar)0.0 && beta == (PetscScalar)0.0 && gamma == (PetscScalar)1.0) return(0);
694:   VecSetErrorIfLocked(z,1);

696:   PetscLogEventBegin(VEC_AXPBYPCZ,x,y,z,0);
697:   (*y->ops->axpbypcz)(z,alpha,beta,gamma,x,y);
698:   PetscLogEventEnd(VEC_AXPBYPCZ,x,y,z,0);
699:   PetscObjectStateIncrease((PetscObject)z);
700:   return(0);
701: }

703: /*@
704:    VecAYPX - Computes y = x + beta y.

706:    Logically Collective on Vec

708:    Input Parameters:
709: +  beta - the scalar
710: -  x, y  - the vectors

712:    Output Parameter:
713: .  y - output vector

715:    Level: intermediate

717:    Notes:
718:     x and y MUST be different vectors
719:     The implementation is optimized for beta of -1.0, 0.0, and 1.0


722: .seealso:  VecMAXPY(), VecWAXPY(), VecAXPY(), VecAXPBYPCZ(), VecAXPBY()
723: @*/
724: PetscErrorCode  VecAYPX(Vec y,PetscScalar beta,Vec x)
725: {

734:   VecCheckSameSize(x,1,y,3);
735:   if (x == y) SETERRQ(PetscObjectComm((PetscObject)x),PETSC_ERR_ARG_IDN,"x and y must be different vectors");
737:   VecSetErrorIfLocked(y,1);

739:   PetscLogEventBegin(VEC_AYPX,x,y,0,0);
740:    (*y->ops->aypx)(y,beta,x);
741:   PetscLogEventEnd(VEC_AYPX,x,y,0,0);
742:   PetscObjectStateIncrease((PetscObject)y);
743:   return(0);
744: }


747: /*@
748:    VecWAXPY - Computes w = alpha x + y.

750:    Logically Collective on Vec

752:    Input Parameters:
753: +  alpha - the scalar
754: -  x, y  - the vectors

756:    Output Parameter:
757: .  w - the result

759:    Level: intermediate

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


766: .seealso: VecAXPY(), VecAYPX(), VecAXPBY(), VecMAXPY(), VecAXPBYPCZ()
767: @*/
768: PetscErrorCode  VecWAXPY(Vec w,PetscScalar alpha,Vec x,Vec y)
769: {

781:   VecCheckSameSize(x,3,y,4);
782:   VecCheckSameSize(x,3,w,1);
783:   if (w == y) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Result vector w cannot be same as input vector y, suggest VecAXPY()");
784:   if (w == x) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Result vector w cannot be same as input vector x, suggest VecAYPX()");
786:   VecSetErrorIfLocked(w,1);

788:   PetscLogEventBegin(VEC_WAXPY,x,y,w,0);
789:    (*w->ops->waxpy)(w,alpha,x,y);
790:   PetscLogEventEnd(VEC_WAXPY,x,y,w,0);
791:   PetscObjectStateIncrease((PetscObject)w);
792:   return(0);
793: }


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

799:    Not Collective

801:    Input Parameters:
802: +  x - vector to insert in
803: .  ni - number of elements to add
804: .  ix - indices where to add
805: .  y - array of values
806: -  iora - either INSERT_VALUES or ADD_VALUES, where
807:    ADD_VALUES adds values to any existing entries, and
808:    INSERT_VALUES replaces existing entries with new values

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

813:    Calls to VecSetValues() with the INSERT_VALUES and ADD_VALUES
814:    options cannot be mixed without intervening calls to the assembly
815:    routines.

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

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

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

828:    Level: beginner

830: .seealso:  VecAssemblyBegin(), VecAssemblyEnd(), VecSetValuesLocal(),
831:            VecSetValue(), VecSetValuesBlocked(), InsertMode, INSERT_VALUES, ADD_VALUES, VecGetValues()
832: @*/
833: PetscErrorCode  VecSetValues(Vec x,PetscInt ni,const PetscInt ix[],const PetscScalar y[],InsertMode iora)
834: {

839:   if (!ni) return(0);

844:   PetscLogEventBegin(VEC_SetValues,x,0,0,0);
845:   (*x->ops->setvalues)(x,ni,ix,y,iora);
846:   PetscLogEventEnd(VEC_SetValues,x,0,0,0);
847:   PetscObjectStateIncrease((PetscObject)x);
848:   return(0);
849: }

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

855:     Not Collective

857:    Input Parameters:
858: +  x - vector to get values from
859: .  ni - number of elements to get
860: -  ix - indices where to get them from (in global 1d numbering)

862:    Output Parameter:
863: .   y - array of values

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

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

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

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

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

878:    Level: beginner

880: .seealso:  VecAssemblyBegin(), VecAssemblyEnd(), VecSetValues()
881: @*/
882: PetscErrorCode  VecGetValues(Vec x,PetscInt ni,const PetscInt ix[],PetscScalar y[])
883: {

888:   if (!ni) return(0);
892:   (*x->ops->getvalues)(x,ni,ix,y);
893:   return(0);
894: }

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

899:    Not Collective

901:    Input Parameters:
902: +  x - vector to insert in
903: .  ni - number of blocks to add
904: .  ix - indices where to add in block count, rather than element count
905: .  y - array of values
906: -  iora - either INSERT_VALUES or ADD_VALUES, where
907:    ADD_VALUES adds values to any existing entries, and
908:    INSERT_VALUES replaces existing entries with new values

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

914:    Calls to VecSetValuesBlocked() with the INSERT_VALUES and ADD_VALUES
915:    options cannot be mixed without intervening calls to the assembly
916:    routines.

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

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

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

928:    Level: intermediate

930: .seealso:  VecAssemblyBegin(), VecAssemblyEnd(), VecSetValuesBlockedLocal(),
931:            VecSetValues()
932: @*/
933: PetscErrorCode  VecSetValuesBlocked(Vec x,PetscInt ni,const PetscInt ix[],const PetscScalar y[],InsertMode iora)
934: {

939:   if (!ni) return(0);

944:   PetscLogEventBegin(VEC_SetValues,x,0,0,0);
945:   (*x->ops->setvaluesblocked)(x,ni,ix,y,iora);
946:   PetscLogEventEnd(VEC_SetValues,x,0,0,0);
947:   PetscObjectStateIncrease((PetscObject)x);
948:   return(0);
949: }


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

956:    Not Collective

958:    Input Parameters:
959: +  x - vector to insert in
960: .  ni - number of elements to add
961: .  ix - indices where to add
962: .  y - array of values
963: -  iora - either INSERT_VALUES or ADD_VALUES, where
964:    ADD_VALUES adds values to any existing entries, and
965:    INSERT_VALUES replaces existing entries with new values

967:    Level: intermediate

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

972:    Calls to VecSetValues() with the INSERT_VALUES and ADD_VALUES
973:    options cannot be mixed without intervening calls to the assembly
974:    routines.

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

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

981: .seealso:  VecAssemblyBegin(), VecAssemblyEnd(), VecSetValues(), VecSetLocalToGlobalMapping(),
982:            VecSetValuesBlockedLocal()
983: @*/
984: PetscErrorCode  VecSetValuesLocal(Vec x,PetscInt ni,const PetscInt ix[],const PetscScalar y[],InsertMode iora)
985: {
987:   PetscInt       lixp[128],*lix = lixp;

991:   if (!ni) return(0);

996:   PetscLogEventBegin(VEC_SetValues,x,0,0,0);
997:   if (!x->ops->setvalueslocal) {
998:     if (!x->map->mapping) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Local to global never set with VecSetLocalToGlobalMapping()");
999:     if (ni > 128) {
1000:       PetscMalloc1(ni,&lix);
1001:     }
1002:     ISLocalToGlobalMappingApply(x->map->mapping,ni,(PetscInt*)ix,lix);
1003:     (*x->ops->setvalues)(x,ni,lix,y,iora);
1004:     if (ni > 128) {
1005:       PetscFree(lix);
1006:     }
1007:   } else {
1008:     (*x->ops->setvalueslocal)(x,ni,ix,y,iora);
1009:   }
1010:   PetscLogEventEnd(VEC_SetValues,x,0,0,0);
1011:   PetscObjectStateIncrease((PetscObject)x);
1012:   return(0);
1013: }

1015: /*@
1016:    VecSetValuesBlockedLocal - Inserts or adds values into certain locations of a vector,
1017:    using a local ordering of the nodes.

1019:    Not Collective

1021:    Input Parameters:
1022: +  x - vector to insert in
1023: .  ni - number of blocks to add
1024: .  ix - indices where to add in block count, not element count
1025: .  y - array of values
1026: -  iora - either INSERT_VALUES or ADD_VALUES, where
1027:    ADD_VALUES adds values to any existing entries, and
1028:    INSERT_VALUES replaces existing entries with new values

1030:    Level: intermediate

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

1036:    Calls to VecSetValuesBlockedLocal() with the INSERT_VALUES and ADD_VALUES
1037:    options cannot be mixed without intervening calls to the assembly
1038:    routines.

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

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


1046: .seealso:  VecAssemblyBegin(), VecAssemblyEnd(), VecSetValues(), VecSetValuesBlocked(),
1047:            VecSetLocalToGlobalMapping()
1048: @*/
1049: PetscErrorCode  VecSetValuesBlockedLocal(Vec x,PetscInt ni,const PetscInt ix[],const PetscScalar y[],InsertMode iora)
1050: {
1052:   PetscInt       lixp[128],*lix = lixp;

1056:   if (!ni) return(0);
1060:   if (ni > 128) {
1061:     PetscMalloc1(ni,&lix);
1062:   }

1064:   PetscLogEventBegin(VEC_SetValues,x,0,0,0);
1065:   ISLocalToGlobalMappingApplyBlock(x->map->mapping,ni,(PetscInt*)ix,lix);
1066:   (*x->ops->setvaluesblocked)(x,ni,lix,y,iora);
1067:   PetscLogEventEnd(VEC_SetValues,x,0,0,0);
1068:   if (ni > 128) {
1069:     PetscFree(lix);
1070:   }
1071:   PetscObjectStateIncrease((PetscObject)x);
1072:   return(0);
1073: }

1075: /*@
1076:    VecMTDot - Computes indefinite vector multiple dot products.
1077:    That is, it does NOT use the complex conjugate.

1079:    Collective on Vec

1081:    Input Parameters:
1082: +  x - one vector
1083: .  nv - number of vectors
1084: -  y - array of vectors.  Note that vectors are pointers

1086:    Output Parameter:
1087: .  val - array of the dot products

1089:    Notes for Users of Complex Numbers:
1090:    For complex vectors, VecMTDot() computes the indefinite form
1091: $      val = (x,y) = y^T x,
1092:    where y^T denotes the transpose of y.

1094:    Use VecMDot() for the inner product
1095: $      val = (x,y) = y^H x,
1096:    where y^H denotes the conjugate transpose of y.

1098:    Level: intermediate


1101: .seealso: VecMDot(), VecTDot()
1102: @*/
1103: PetscErrorCode  VecMTDot(Vec x,PetscInt nv,const Vec y[],PetscScalar val[])
1104: {

1110:   if (!nv) return(0);
1117:   VecCheckSameSize(x,1,*y,3);

1119:   PetscLogEventBegin(VEC_MTDot,x,*y,0,0);
1120:   (*x->ops->mtdot)(x,nv,y,val);
1121:   PetscLogEventEnd(VEC_MTDot,x,*y,0,0);
1122:   return(0);
1123: }

1125: /*@
1126:    VecMDot - Computes vector multiple dot products.

1128:    Collective on Vec

1130:    Input Parameters:
1131: +  x - one vector
1132: .  nv - number of vectors
1133: -  y - array of vectors.

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

1138:    Notes for Users of Complex Numbers:
1139:    For complex vectors, VecMDot() computes
1140: $     val = (x,y) = y^H x,
1141:    where y^H denotes the conjugate transpose of y.

1143:    Use VecMTDot() for the indefinite form
1144: $     val = (x,y) = y^T x,
1145:    where y^T denotes the transpose of y.

1147:    Level: intermediate


1150: .seealso: VecMTDot(), VecDot()
1151: @*/
1152: PetscErrorCode  VecMDot(Vec x,PetscInt nv,const Vec y[],PetscScalar val[])
1153: {

1159:   if (!nv) return(0);
1160:   if (nv < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Number of vectors (given %D) cannot be negative",nv);
1167:   VecCheckSameSize(x,1,*y,3);

1169:   PetscLogEventBegin(VEC_MDot,x,*y,0,0);
1170:   (*x->ops->mdot)(x,nv,y,val);
1171:   PetscLogEventEnd(VEC_MDot,x,*y,0,0);
1172:   return(0);
1173: }

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

1178:    Logically Collective on Vec

1180:    Input Parameters:
1181: +  nv - number of scalars and x-vectors
1182: .  alpha - array of scalars
1183: .  y - one vector
1184: -  x - array of vectors

1186:    Level: intermediate

1188:    Notes:
1189:     y cannot be any of the x vectors

1191: .seealso:  VecAYPX(), VecWAXPY(), VecAXPY(), VecAXPBYPCZ(), VecAXPBY()
1192: @*/
1193: PetscErrorCode  VecMAXPY(Vec y,PetscInt nv,const PetscScalar alpha[],Vec x[])
1194: {
1196:   PetscInt       i;
1197:   PetscBool      nonzero;

1202:   if (!nv) return(0);
1203:   if (nv < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Number of vectors (given %D) cannot be negative",nv);
1210:   VecCheckSameSize(y,1,*x,4);
1212:   for (i=0, nonzero = PETSC_FALSE; i<nv && !nonzero; i++) nonzero = (PetscBool)(nonzero || alpha[i] != (PetscScalar)0.0);
1213:   if (!nonzero) return(0);
1214:   VecSetErrorIfLocked(y,1);
1215:   PetscLogEventBegin(VEC_MAXPY,*x,y,0,0);
1216:   (*y->ops->maxpy)(y,nv,alpha,x);
1217:   PetscLogEventEnd(VEC_MAXPY,*x,y,0,0);
1218:   PetscObjectStateIncrease((PetscObject)y);
1219:   return(0);
1220: }

1222: /*@
1223:    VecGetSubVector - Gets a vector representing part of another vector

1225:    Collective on X and IS

1227:    Input Arguments:
1228: + X - vector from which to extract a subvector
1229: - is - index set representing portion of X to extract

1231:    Output Arguments:
1232: . Y - subvector corresponding to is

1234:    Level: advanced

1236:    Notes:
1237:    The subvector Y should be returned with VecRestoreSubVector().
1238:    X and is must be defined on the same communicator

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

1244: .seealso: MatCreateSubMatrix()
1245: @*/
1246: PetscErrorCode  VecGetSubVector(Vec X,IS is,Vec *Y)
1247: {
1248:   PetscErrorCode   ierr;
1249:   Vec              Z;

1256:   if (X->ops->getsubvector) {
1257:     (*X->ops->getsubvector)(X,is,&Z);
1258:   } else { /* Default implementation currently does no caching */
1259:     PetscInt  gstart,gend,start;
1260:     PetscBool red[2] = { PETSC_TRUE, PETSC_TRUE };
1261:     PetscInt  n,N,ibs,vbs,bs = -1;

1263:     ISGetLocalSize(is,&n);
1264:     ISGetSize(is,&N);
1265:     ISGetBlockSize(is,&ibs);
1266:     VecGetBlockSize(X,&vbs);
1267:     VecGetOwnershipRange(X,&gstart,&gend);
1268:     ISContiguousLocal(is,gstart,gend,&start,&red[0]);
1269:     /* block size is given by IS if ibs > 1; otherwise, check the vector */
1270:     if (ibs > 1) {
1271:       MPIU_Allreduce(MPI_IN_PLACE,red,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)is));
1272:       bs   = ibs;
1273:     } else {
1274:       if (n%vbs || vbs == 1) red[1] = PETSC_FALSE; /* this process invalidate the collectiveness of block size */
1275:       MPIU_Allreduce(MPI_IN_PLACE,red,2,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)is));
1276:       if (red[0] && red[1]) bs = vbs; /* all processes have a valid block size and the access will be contiguous */
1277:     }
1278:     if (red[0]) { /* We can do a no-copy implementation */
1279:       const PetscScalar *x;
1280:       PetscInt          state = 0;
1281:       PetscBool         isstd;
1282: #if defined(PETSC_HAVE_CUDA)
1283:       PetscBool         iscuda;
1284: #endif

1286:       PetscObjectTypeCompareAny((PetscObject)X,&isstd,VECSEQ,VECMPI,VECSTANDARD,"");
1287: #if defined(PETSC_HAVE_CUDA)
1288:       PetscObjectTypeCompareAny((PetscObject)X,&iscuda,VECSEQCUDA,VECMPICUDA,"");
1289:       if (iscuda) {
1290:         const PetscScalar *x_d;
1291:         PetscMPIInt       size;
1292:         PetscOffloadMask  flg;

1294:         VecCUDAGetArrays_Private(X,&x,&x_d,&flg);
1295:         if (flg == PETSC_OFFLOAD_UNALLOCATED) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not for PETSC_OFFLOAD_UNALLOCATED");
1296:         if (n && !x && !x_d) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Missing vector data");
1297:         if (x) x += start;
1298:         if (x_d) x_d += start;
1299:         MPI_Comm_size(PetscObjectComm((PetscObject)X),&size);
1300:         if (size == 1) {
1301:           VecCreateSeqCUDAWithArrays(PetscObjectComm((PetscObject)X),bs,n,x,x_d,&Z);
1302:         } else {
1303:           VecCreateMPICUDAWithArrays(PetscObjectComm((PetscObject)X),bs,n,N,x,x_d,&Z);
1304:         }
1305:         Z->offloadmask = flg;
1306:       } else if (isstd) {
1307: #else
1308:       if (isstd) { /* standard CPU: use CreateWithArray pattern */
1309: #endif
1310:         PetscMPIInt size;

1312:         MPI_Comm_size(PetscObjectComm((PetscObject)X),&size);
1313:         VecGetArrayRead(X,&x);
1314:         if (x) x += start;
1315:         if (size == 1) {
1316:           VecCreateSeqWithArray(PetscObjectComm((PetscObject)X),bs,n,x,&Z);
1317:         } else {
1318:           VecCreateMPIWithArray(PetscObjectComm((PetscObject)X),bs,n,N,x,&Z);
1319:         }
1320:         VecRestoreArrayRead(X,&x);
1321:       } else { /* default implementation: use place array */
1322:         VecGetArrayRead(X,&x);
1323:         VecCreate(PetscObjectComm((PetscObject)X),&Z);
1324:         VecSetType(Z,((PetscObject)X)->type_name);
1325:         VecSetSizes(Z,n,N);
1326:         VecSetBlockSize(Z,bs);
1327:         VecPlaceArray(Z,x ? x+start : NULL);
1328:         VecRestoreArrayRead(X,&x);
1329:       }

1331:       /* this is relevant only in debug mode */
1332:       VecLockGet(X,&state);
1333:       if (state) {
1334:         VecLockReadPush(Z);
1335:       }
1336:       Z->ops->placearray = NULL;
1337:       Z->ops->replacearray = NULL;
1338:     } else { /* Have to create a scatter and do a copy */
1339:       VecScatter scatter;

1341:       VecCreate(PetscObjectComm((PetscObject)is),&Z);
1342:       VecSetSizes(Z,n,N);
1343:       VecSetBlockSize(Z,bs);
1344:       VecSetType(Z,((PetscObject)X)->type_name);
1345:       VecScatterCreate(X,is,Z,NULL,&scatter);
1346:       VecScatterBegin(scatter,X,Z,INSERT_VALUES,SCATTER_FORWARD);
1347:       VecScatterEnd(scatter,X,Z,INSERT_VALUES,SCATTER_FORWARD);
1348:       PetscObjectCompose((PetscObject)Z,"VecGetSubVector_Scatter",(PetscObject)scatter);
1349:       VecScatterDestroy(&scatter);
1350:     }
1351:   }
1352:   /* Record the state when the subvector was gotten so we know whether its values need to be put back */
1353:   if (VecGetSubVectorSavedStateId < 0) {PetscObjectComposedDataRegister(&VecGetSubVectorSavedStateId);}
1354:   PetscObjectComposedDataSetInt((PetscObject)Z,VecGetSubVectorSavedStateId,1);
1355:   *Y   = Z;
1356:   return(0);
1357: }

1359: /*@
1360:    VecRestoreSubVector - Restores a subvector extracted using VecGetSubVector()

1362:    Collective on IS

1364:    Input Arguments:
1365: + X - vector from which subvector was obtained
1366: . is - index set representing the subset of X
1367: - Y - subvector being restored

1369:    Level: advanced

1371: .seealso: VecGetSubVector()
1372: @*/
1373: PetscErrorCode  VecRestoreSubVector(Vec X,IS is,Vec *Y)
1374: {

1383:   if (X->ops->restoresubvector) {
1384:     (*X->ops->restoresubvector)(X,is,Y);
1385:   } else {
1386:     PETSC_UNUSED PetscObjectState dummystate = 0;
1387:     PetscBool valid;

1389:     PetscObjectComposedDataGetInt((PetscObject)*Y,VecGetSubVectorSavedStateId,dummystate,valid);
1390:     if (!valid) {
1391:       VecScatter scatter;
1392:       PetscInt   state;

1394:       VecLockGet(X,&state);
1395:       if (state != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Vec X is locked for read-only or read/write access");

1397:       PetscObjectQuery((PetscObject)*Y,"VecGetSubVector_Scatter",(PetscObject*)&scatter);
1398:       if (scatter) {
1399:         VecScatterBegin(scatter,*Y,X,INSERT_VALUES,SCATTER_REVERSE);
1400:         VecScatterEnd(scatter,*Y,X,INSERT_VALUES,SCATTER_REVERSE);
1401:       } else {
1402: #if defined(PETSC_HAVE_CUDA)
1403:         PetscBool iscuda;

1405:         PetscObjectTypeCompareAny((PetscObject)*Y,&iscuda,VECSEQCUDA,VECMPICUDA,"");
1406:         if (iscuda) {
1407:           PetscOffloadMask ymask = (*Y)->offloadmask;

1409:           /* The offloadmask of X dictates where to move memory
1410:              If X GPU data is valid, then move Y data on GPU if needed
1411:              Otherwise, move back to the CPU */
1412:           switch (X->offloadmask) {
1413:           case PETSC_OFFLOAD_BOTH:
1414:             if (ymask == PETSC_OFFLOAD_CPU) {
1415:               VecCUDAResetArray(*Y);
1416:             } else if (ymask == PETSC_OFFLOAD_GPU) {
1417:               X->offloadmask = PETSC_OFFLOAD_GPU;
1418:             }
1419:             break;
1420:           case PETSC_OFFLOAD_GPU:
1421:             if (ymask == PETSC_OFFLOAD_CPU) {
1422:               VecCUDAResetArray(*Y);
1423:             }
1424:             break;
1425:           case PETSC_OFFLOAD_CPU:
1426:             if (ymask == PETSC_OFFLOAD_GPU) {
1427:               VecResetArray(*Y);
1428:             }
1429:             break;
1430:           case PETSC_OFFLOAD_UNALLOCATED:
1431:           case PETSC_OFFLOAD_VECKOKKOS:
1432:             SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"This should not happen");
1433:             break;
1434:           }
1435:         } else {
1436: #else
1437:         {
1438: #endif
1439:           /* If OpenCL vecs updated the device memory, this triggers a copy on the CPU */
1440:           VecResetArray(*Y);
1441:         }
1442:         PetscObjectStateIncrease((PetscObject)X);
1443:       }
1444:     }
1445:     VecDestroy(Y);
1446:   }
1447:   return(0);
1448: }

1450: /*@
1451:    VecGetLocalVectorRead - Maps the local portion of a vector into a
1452:    vector.  You must call VecRestoreLocalVectorRead() when the local
1453:    vector is no longer needed.

1455:    Not collective.

1457:    Input parameter:
1458: .  v - The vector for which the local vector is desired.

1460:    Output parameter:
1461: .  w - Upon exit this contains the local vector.

1463:    Level: beginner

1465:    Notes:
1466:    This function is similar to VecGetArrayRead() which maps the local
1467:    portion into a raw pointer.  VecGetLocalVectorRead() is usually
1468:    almost as efficient as VecGetArrayRead() but in certain circumstances
1469:    VecGetLocalVectorRead() can be much more efficient than
1470:    VecGetArrayRead().  This is because the construction of a contiguous
1471:    array representing the vector data required by VecGetArrayRead() can
1472:    be an expensive operation for certain vector types.  For example, for
1473:    GPU vectors VecGetArrayRead() requires that the data between device
1474:    and host is synchronized.

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

1479: .seealso: VecRestoreLocalVectorRead(), VecGetLocalVector(), VecGetArrayRead(), VecGetArray()
1480: @*/
1481: PetscErrorCode VecGetLocalVectorRead(Vec v,Vec w)
1482: {
1484:   PetscScalar    *a;

1489:   VecCheckSameLocalSize(v,1,w,2);
1490:   if (v->ops->getlocalvectorread) {
1491:     (*v->ops->getlocalvectorread)(v,w);
1492:   } else {
1493:     VecGetArrayRead(v,(const PetscScalar**)&a);
1494:     VecPlaceArray(w,a);
1495:   }
1496:   return(0);
1497: }

1499: /*@
1500:    VecRestoreLocalVectorRead - Unmaps the local portion of a vector
1501:    previously mapped into a vector using VecGetLocalVectorRead().

1503:    Not collective.

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

1509:    Level: beginner

1511: .seealso: VecGetLocalVectorRead(), VecGetLocalVector(), VecGetArrayRead(), VecGetArray()
1512: @*/
1513: PetscErrorCode VecRestoreLocalVectorRead(Vec v,Vec w)
1514: {
1516:   PetscScalar    *a;

1521:   if (v->ops->restorelocalvectorread) {
1522:     (*v->ops->restorelocalvectorread)(v,w);
1523:   } else {
1524:     VecGetArrayRead(w,(const PetscScalar**)&a);
1525:     VecRestoreArrayRead(v,(const PetscScalar**)&a);
1526:     VecResetArray(w);
1527:   }
1528:   return(0);
1529: }

1531: /*@
1532:    VecGetLocalVector - Maps the local portion of a vector into a
1533:    vector.

1535:    Collective on v, not collective on w.

1537:    Input parameter:
1538: .  v - The vector for which the local vector is desired.

1540:    Output parameter:
1541: .  w - Upon exit this contains the local vector.

1543:    Level: beginner

1545:    Notes:
1546:    This function is similar to VecGetArray() which maps the local
1547:    portion into a raw pointer.  VecGetLocalVector() is usually about as
1548:    efficient as VecGetArray() but in certain circumstances
1549:    VecGetLocalVector() can be much more efficient than VecGetArray().
1550:    This is because the construction of a contiguous array representing
1551:    the vector data required by VecGetArray() can be an expensive
1552:    operation for certain vector types.  For example, for GPU vectors
1553:    VecGetArray() requires that the data between device and host is
1554:    synchronized.

1556: .seealso: VecRestoreLocalVector(), VecGetLocalVectorRead(), VecGetArrayRead(), VecGetArray()
1557: @*/
1558: PetscErrorCode VecGetLocalVector(Vec v,Vec w)
1559: {
1561:   PetscScalar    *a;

1566:   VecCheckSameLocalSize(v,1,w,2);
1567:   if (v->ops->getlocalvector) {
1568:     (*v->ops->getlocalvector)(v,w);
1569:   } else {
1570:     VecGetArray(v,&a);
1571:     VecPlaceArray(w,a);
1572:   }
1573:   return(0);
1574: }

1576: /*@
1577:    VecRestoreLocalVector - Unmaps the local portion of a vector
1578:    previously mapped into a vector using VecGetLocalVector().

1580:    Logically collective.

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

1586:    Level: beginner

1588: .seealso: VecGetLocalVector(), VecGetLocalVectorRead(), VecRestoreLocalVectorRead(), LocalVectorRead(), VecGetArrayRead(), VecGetArray()
1589: @*/
1590: PetscErrorCode VecRestoreLocalVector(Vec v,Vec w)
1591: {
1593:   PetscScalar    *a;

1598:   if (v->ops->restorelocalvector) {
1599:     (*v->ops->restorelocalvector)(v,w);
1600:   } else {
1601:     VecGetArray(w,&a);
1602:     VecRestoreArray(v,&a);
1603:     VecResetArray(w);
1604:   }
1605:   return(0);
1606: }

1608: /*@C
1609:    VecGetArray - Returns a pointer to a contiguous array that contains this
1610:    processor's portion of the vector data. For the standard PETSc
1611:    vectors, VecGetArray() returns a pointer to the local data array and
1612:    does not use any copies. If the underlying vector data is not stored
1613:    in a contiguous array this routine will copy the data to a contiguous
1614:    array and return a pointer to that. You MUST call VecRestoreArray()
1615:    when you no longer need access to the array.

1617:    Logically Collective on Vec

1619:    Input Parameter:
1620: .  x - the vector

1622:    Output Parameter:
1623: .  a - location to put pointer to the array

1625:    Fortran Note:
1626:    This routine is used differently from Fortran 77
1627: $    Vec         x
1628: $    PetscScalar x_array(1)
1629: $    PetscOffset i_x
1630: $    PetscErrorCode ierr
1631: $       call VecGetArray(x,x_array,i_x,ierr)
1632: $
1633: $   Access first local entry in vector with
1634: $      value = x_array(i_x + 1)
1635: $
1636: $      ...... other code
1637: $       call VecRestoreArray(x,x_array,i_x,ierr)
1638:    For Fortran 90 see VecGetArrayF90()

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

1643:    Level: beginner

1645: .seealso: VecRestoreArray(), VecGetArrayRead(), VecGetArrays(), VecGetArrayF90(), VecGetArrayReadF90(), VecPlaceArray(), VecGetArray2d(),
1646:           VecGetArrayPair(), VecRestoreArrayPair(), VecGetArrayWrite(), VecRestoreArrayWrite()
1647: @*/
1648: PetscErrorCode VecGetArray(Vec x,PetscScalar **a)
1649: {

1654:   VecSetErrorIfLocked(x,1);
1655:   if (x->ops->getarray) { /* The if-else order matters! VECNEST, VECCUDA etc should have ops->getarray while VECCUDA etc are petscnative */
1656:     (*x->ops->getarray)(x,a);
1657:   } else if (x->petscnative) { /* VECSTANDARD */
1658:     *a = *((PetscScalar**)x->data);
1659:   } else SETERRQ1(PetscObjectComm((PetscObject)x),PETSC_ERR_SUP,"Cannot get array for vector type \"%s\"",((PetscObject)x)->type_name);
1660:   return(0);
1661: }

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

1666:    Logically Collective on Vec

1668:    Input Parameters:
1669: +  x - the vector
1670: -  a - location of pointer to array obtained from VecGetArray()

1672:    Level: beginner

1674: .seealso: VecGetArray(), VecRestoreArrayRead(), VecRestoreArrays(), VecRestoreArrayF90(), VecRestoreArrayReadF90(), VecPlaceArray(), VecRestoreArray2d(),
1675:           VecGetArrayPair(), VecRestoreArrayPair()
1676: @*/
1677: PetscErrorCode VecRestoreArray(Vec x,PetscScalar **a)
1678: {

1683:   if (x->ops->restorearray) { /* VECNEST, VECCUDA etc */
1684:     (*x->ops->restorearray)(x,a);
1685:   } else if (x->petscnative) { /* VECSTANDARD */
1686:     /* nothing */
1687:   } else SETERRQ1(PetscObjectComm((PetscObject)x),PETSC_ERR_SUP,"Cannot restore array for vector type \"%s\"",((PetscObject)x)->type_name);
1688:   if (a) *a = NULL;
1689:   PetscObjectStateIncrease((PetscObject)x);
1690:   return(0);
1691: }
1692: /*@C
1693:    VecGetArrayRead - Get read-only pointer to contiguous array containing this processor's portion of the vector data.

1695:    Not Collective

1697:    Input Parameter:
1698: .  x - the vector

1700:    Output Parameter:
1701: .  a - the array

1703:    Level: beginner

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

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

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

1714: .seealso: VecGetArray(), VecRestoreArray(), VecGetArrayPair(), VecRestoreArrayPair()
1715: @*/
1716: PetscErrorCode VecGetArrayRead(Vec x,const PetscScalar **a)
1717: {

1722:   if (x->ops->getarrayread) { /* VECNODE; TODO: Delete this line after we remove VECNODE */
1723:     (*x->ops->getarrayread)(x,a);
1724:   } else if (x->ops->getarray) { /* VECNEST, VECCUDA, VECKOKKOS etc */
1725:     (*x->ops->getarray)(x,(PetscScalar**)a);
1726:   } else if (x->petscnative) { /* VECSTANDARD */
1727:     *a = *((PetscScalar**)x->data);
1728:   } else SETERRQ1(PetscObjectComm((PetscObject)x),PETSC_ERR_SUP,"Cannot get array read for vector type \"%s\"",((PetscObject)x)->type_name);
1729:   return(0);
1730: }

1732: /*@C
1733:    VecRestoreArrayRead - Restore array obtained with VecGetArrayRead()

1735:    Not Collective

1737:    Input Parameters:
1738: +  vec - the vector
1739: -  array - the array

1741:    Level: beginner

1743: .seealso: VecGetArray(), VecRestoreArray(), VecGetArrayPair(), VecRestoreArrayPair()
1744: @*/
1745: PetscErrorCode VecRestoreArrayRead(Vec x,const PetscScalar **a)
1746: {

1751:   if (x->petscnative) { /* VECSTANDARD, VECCUDA, VECKOKKOS etc */
1752:     /* nothing */
1753:   } else if (x->ops->restorearrayread) { /* VECNEST */
1754:     (*x->ops->restorearrayread)(x,a);
1755:   } else { /* No one? */
1756:     (*x->ops->restorearray)(x,(PetscScalar**)a);
1757:   }
1758:   if (a) *a = NULL;
1759:   return(0);
1760: }

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

1767:    Logically Collective on Vec

1769:    Input Parameter:
1770: .  x - the vector

1772:    Output Parameter:
1773: .  a - location to put pointer to the array

1775:    Level: intermediate

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

1780:    Concepts: vector^accessing local values

1782: .seealso: VecRestoreArray(), VecGetArrayRead(), VecGetArrays(), VecGetArrayF90(), VecGetArrayReadF90(), VecPlaceArray(), VecGetArray2d(),
1783:           VecGetArrayPair(), VecRestoreArrayPair(), VecGetArray(), VecRestoreArrayWrite()
1784: @*/
1785: PetscErrorCode VecGetArrayWrite(Vec x,PetscScalar **a)
1786: {

1791:   VecSetErrorIfLocked(x,1);
1792:   if (x->ops->getarraywrite) {
1793:     (*x->ops->getarraywrite)(x,a);
1794:   } else {
1795:     VecGetArray(x,a);
1796:   }
1797:   return(0);
1798: }

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

1803:    Logically Collective on Vec

1805:    Input Parameters:
1806: +  x - the vector
1807: -  a - location of pointer to array obtained from VecGetArray()

1809:    Level: beginner

1811: .seealso: VecGetArray(), VecRestoreArrayRead(), VecRestoreArrays(), VecRestoreArrayF90(), VecRestoreArrayReadF90(), VecPlaceArray(), VecRestoreArray2d(),
1812:           VecGetArrayPair(), VecRestoreArrayPair(), VecGetArrayWrite()
1813: @*/
1814: PetscErrorCode VecRestoreArrayWrite(Vec x,PetscScalar **a)
1815: {

1820:   if (x->ops->restorearraywrite) {
1821:     (*x->ops->restorearraywrite)(x,a);
1822:   } else if (x->ops->restorearray) {
1823:     (*x->ops->restorearray)(x,a);
1824:   }
1825:   if (a) *a = NULL;
1826:   PetscObjectStateIncrease((PetscObject)x);
1827:   return(0);
1828: }

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

1835:    Logically Collective on Vec

1837:    Input Parameters:
1838: +  x - the vectors
1839: -  n - the number of vectors

1841:    Output Parameter:
1842: .  a - location to put pointer to the array

1844:    Fortran Note:
1845:    This routine is not supported in Fortran.

1847:    Level: intermediate

1849: .seealso: VecGetArray(), VecRestoreArrays()
1850: @*/
1851: PetscErrorCode  VecGetArrays(const Vec x[],PetscInt n,PetscScalar **a[])
1852: {
1854:   PetscInt       i;
1855:   PetscScalar    **q;

1861:   if (n <= 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Must get at least one array n = %D",n);
1862:   PetscMalloc1(n,&q);
1863:   for (i=0; i<n; ++i) {
1864:     VecGetArray(x[i],&q[i]);
1865:   }
1866:   *a = q;
1867:   return(0);
1868: }

1870: /*@C
1871:    VecRestoreArrays - Restores a group of vectors after VecGetArrays()
1872:    has been called.

1874:    Logically Collective on Vec

1876:    Input Parameters:
1877: +  x - the vector
1878: .  n - the number of vectors
1879: -  a - location of pointer to arrays obtained from VecGetArrays()

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

1887:    Fortran Note:
1888:    This routine is not supported in Fortran.

1890:    Level: intermediate

1892: .seealso: VecGetArrays(), VecRestoreArray()
1893: @*/
1894: PetscErrorCode  VecRestoreArrays(const Vec x[],PetscInt n,PetscScalar **a[])
1895: {
1897:   PetscInt       i;
1898:   PetscScalar    **q = *a;


1905:   for (i=0; i<n; ++i) {
1906:     VecRestoreArray(x[i],&q[i]);
1907:   }
1908:   PetscFree(q);
1909:   return(0);
1910: }

1912: /*@C
1913:    VecGetArrayAndMemType - Like VecGetArray(), but if this is a GPU vector and it is currently offloaded to GPU,
1914:    the returned pointer will be a GPU pointer to the GPU memory that contains this processor's portion of the
1915:    vector data. Otherwise, it functions as VecGetArray().

1917:    Logically Collective on Vec

1919:    Input Parameter:
1920: .  x - the vector

1922:    Output Parameters:
1923: +  a - location to put pointer to the array
1924: -  mtype - memory type of the array

1926:    Level: beginner

1928: .seealso: VecRestoreArrayAndMemType(), VecRestoreArrayAndMemType(), VecRestoreArray(), VecGetArrayRead(), VecGetArrays(), VecGetArrayF90(), VecGetArrayReadF90(),
1929:           VecPlaceArray(), VecGetArray2d(), VecGetArrayPair(), VecRestoreArrayPair(), VecGetArrayWrite(), VecRestoreArrayWrite()
1930: @*/
1931: PetscErrorCode VecGetArrayAndMemType(Vec x,PetscScalar **a,PetscMemType *mtype)
1932: {

1938:   VecSetErrorIfLocked(x,1);
1939:   if (x->ops->getarrayandmemtype) { /* VECCUDA, VECKOKKOS etc */
1940:     (*x->ops->getarrayandmemtype)(x,a,mtype);
1941:   } else { /* VECSTANDARD, VECNEST, VECVIENNACL */
1942:     VecGetArray(x,a);
1943:     if (mtype) *mtype = PETSC_MEMTYPE_HOST;
1944:   }
1945:   return(0);
1946: }

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

1951:    Logically Collective on Vec

1953:    Input Parameters:
1954: +  x - the vector
1955: -  a - location of pointer to array obtained from VecGetArrayAndMemType()

1957:    Level: beginner

1959: .seealso: VecGetArrayAndMemType(), VecGetArray(), VecRestoreArrayRead(), VecRestoreArrays(), VecRestoreArrayF90(), VecRestoreArrayReadF90(),
1960:           VecPlaceArray(), VecRestoreArray2d(), VecGetArrayPair(), VecRestoreArrayPair()
1961: @*/
1962: PetscErrorCode VecRestoreArrayAndMemType(Vec x,PetscScalar **a)
1963: {

1969:   if (x->ops->restorearrayandmemtype) { /* VECCUDA, VECKOKKOS etc */
1970:     (*x->ops->restorearrayandmemtype)(x,a);
1971:   } else if (x->ops->restorearray) { /* VECNEST, VECVIENNACL */
1972:     (*x->ops->restorearray)(x,a);
1973:   } /* VECSTANDARD does nothing */
1974:   if (a) *a = NULL;
1975:   PetscObjectStateIncrease((PetscObject)x);
1976:   return(0);
1977: }

1979: /*@C
1980:    VecGetArrayReadAndMemType - Like VecGetArrayRead(), but if this is a CUDA vector and it is currently offloaded to GPU,
1981:    the returned pointer will be a GPU pointer to the GPU memory that contains this processor's portion of the
1982:    vector data. Otherwise, it functions as VecGetArrayRead().

1984:    Not Collective

1986:    Input Parameter:
1987: .  x - the vector

1989:    Output Parameters:
1990: +  a - the array
1991: -  mtype - memory type of the array

1993:    Level: beginner

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


1999: .seealso: VecRestoreArrayReadAndMemType(), VecGetArray(), VecRestoreArray(), VecGetArrayPair(), VecRestoreArrayPair(), VecGetArrayAndMemType()
2000: @*/
2001: PetscErrorCode VecGetArrayReadAndMemType(Vec x,const PetscScalar **a,PetscMemType *mtype)
2002: {

2008:  #if defined(PETSC_USE_DEBUG)
2009:   if (x->ops->getarrayreadandmemtype) SETERRQ1(PetscObjectComm((PetscObject)x),PETSC_ERR_SUP,"Not expected vector type \"%s\" has ops->getarrayreadandmemtype",((PetscObject)x)->type_name);
2010:  #endif

2012:   if (x->ops->getarrayandmemtype) { /* VECCUDA, VECKOKKOS etc, though they are also petscnative */
2013:     (*x->ops->getarrayandmemtype)(x,(PetscScalar**)a,mtype);
2014:   } else if (x->ops->getarray) { /* VECNEST, VECVIENNACL */
2015:     (*x->ops->getarray)(x,(PetscScalar**)a);
2016:     if (mtype) *mtype = PETSC_MEMTYPE_HOST;
2017:   } else if (x->petscnative) { /* VECSTANDARD */
2018:     *a = *((PetscScalar**)x->data);
2019:     if (mtype) *mtype = PETSC_MEMTYPE_HOST;
2020:   } else SETERRQ1(PetscObjectComm((PetscObject)x),PETSC_ERR_SUP,"Cannot get array read in place for vector type \"%s\"",((PetscObject)x)->type_name);
2021:   return(0);
2022: }

2024: /*@C
2025:    VecRestoreArrayReadAndMemType - Restore array obtained with VecGetArrayReadAndMemType()

2027:    Not Collective

2029:    Input Parameters:
2030: +  vec - the vector
2031: -  array - the array

2033:    Level: beginner

2035: .seealso: VecGetArrayReadAndMemType(), VecRestoreArrayAndMemType(), VecGetArray(), VecRestoreArray(), VecGetArrayPair(), VecRestoreArrayPair()
2036: @*/
2037: PetscErrorCode VecRestoreArrayReadAndMemType(Vec x,const PetscScalar **a)
2038: {

2044:   if (x->petscnative) { /* VECSTANDARD, VECCUDA, VECKOKKOS, VECVIENNACL etc */
2045:     /* nothing */
2046:   } else if (x->ops->restorearrayread) { /* VECNEST */
2047:     (*x->ops->restorearrayread)(x,a);
2048:   } else SETERRQ1(PetscObjectComm((PetscObject)x),PETSC_ERR_SUP,"Cannot restore array read in place for vector type \"%s\"",((PetscObject)x)->type_name);
2049:   if (a) *a = NULL;
2050:   return(0);
2051: }

2053: /*@
2054:    VecPlaceArray - Allows one to replace the array in a vector with an
2055:    array provided by the user. This is useful to avoid copying an array
2056:    into a vector.

2058:    Not Collective

2060:    Input Parameters:
2061: +  vec - the vector
2062: -  array - the array

2064:    Notes:
2065:    You can return to the original array with a call to VecResetArray()

2067:    Level: developer

2069: .seealso: VecGetArray(), VecRestoreArray(), VecReplaceArray(), VecResetArray()

2071: @*/
2072: PetscErrorCode  VecPlaceArray(Vec vec,const PetscScalar array[])
2073: {

2080:   if (vec->ops->placearray) {
2081:     (*vec->ops->placearray)(vec,array);
2082:   } else SETERRQ(PetscObjectComm((PetscObject)vec),PETSC_ERR_SUP,"Cannot place array in this type of vector");
2083:   PetscObjectStateIncrease((PetscObject)vec);
2084:   return(0);
2085: }

2087: /*@C
2088:    VecReplaceArray - Allows one to replace the array in a vector with an
2089:    array provided by the user. This is useful to avoid copying an array
2090:    into a vector.

2092:    Not Collective

2094:    Input Parameters:
2095: +  vec - the vector
2096: -  array - the array

2098:    Notes:
2099:    This permanently replaces the array and frees the memory associated
2100:    with the old array.

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

2105:    Not supported from Fortran

2107:    Level: developer

2109: .seealso: VecGetArray(), VecRestoreArray(), VecPlaceArray(), VecResetArray()

2111: @*/
2112: PetscErrorCode  VecReplaceArray(Vec vec,const PetscScalar array[])
2113: {

2119:   if (vec->ops->replacearray) {
2120:     (*vec->ops->replacearray)(vec,array);
2121:   } else SETERRQ(PetscObjectComm((PetscObject)vec),PETSC_ERR_SUP,"Cannot replace array in this type of vector");
2122:   PetscObjectStateIncrease((PetscObject)vec);
2123:   return(0);
2124: }


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

2130:    This function has semantics similar to VecGetArray():  the pointer
2131:    returned by this function points to a consistent view of the vector
2132:    data.  This may involve a copy operation of data from the host to the
2133:    device if the data on the device is out of date.  If the device
2134:    memory hasn't been allocated previously it will be allocated as part
2135:    of this function call.  VecCUDAGetArray() assumes that
2136:    the user will modify the vector data.  This is similar to
2137:    intent(inout) in fortran.

2139:    The CUDA device pointer has to be released by calling
2140:    VecCUDARestoreArray().  Upon restoring the vector data
2141:    the data on the host will be marked as out of date.  A subsequent
2142:    access of the host data will thus incur a data transfer from the
2143:    device to the host.


2146:    Input Parameter:
2147: .  v - the vector

2149:    Output Parameter:
2150: .  a - the CUDA device pointer

2152:    Fortran note:
2153:    This function is not currently available from Fortran.

2155:    Level: intermediate

2157: .seealso: VecCUDARestoreArray(), VecCUDAGetArrayRead(), VecCUDAGetArrayWrite(), VecGetArray(), VecGetArrayRead()
2158: @*/
2159: PETSC_EXTERN PetscErrorCode VecCUDAGetArray(Vec v, PetscScalar **a)
2160: {
2163:  #if defined(PETSC_HAVE_CUDA)
2164:   {
2166:     VecCUDACopyToGPU(v);
2167:     *a   = ((Vec_CUDA*)v->spptr)->GPUarray;
2168:   }
2169:  #endif
2170:   return(0);
2171: }

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

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

2180:    Input Parameter:
2181: +  v - the vector
2182: -  a - the CUDA device pointer.  This pointer is invalid after
2183:        VecCUDARestoreArray() returns.

2185:    Fortran note:
2186:    This function is not currently available from Fortran.

2188:    Level: intermediate

2190: .seealso: VecCUDAGetArray(), VecCUDAGetArrayRead(), VecCUDAGetArrayWrite(), VecGetArray(), VecRestoreArray(), VecGetArrayRead()
2191: @*/
2192: PETSC_EXTERN PetscErrorCode VecCUDARestoreArray(Vec v, PetscScalar **a)
2193: {

2198: #if defined(PETSC_HAVE_CUDA)
2199:   v->offloadmask = PETSC_OFFLOAD_GPU;
2200: #endif
2201:   PetscObjectStateIncrease((PetscObject)v);
2202:   return(0);
2203: }

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

2208:    This function is analogous to VecGetArrayRead():  The pointer
2209:    returned by this function points to a consistent view of the vector
2210:    data.  This may involve a copy operation of data from the host to the
2211:    device if the data on the device is out of date.  If the device
2212:    memory hasn't been allocated previously it will be allocated as part
2213:    of this function call.  VecCUDAGetArrayRead() assumes that the
2214:    user will not modify the vector data.  This is analgogous to
2215:    intent(in) in Fortran.

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

2223:    Input Parameter:
2224: .  v - the vector

2226:    Output Parameter:
2227: .  a - the CUDA pointer.

2229:    Fortran note:
2230:    This function is not currently available from Fortran.

2232:    Level: intermediate

2234: .seealso: VecCUDARestoreArrayRead(), VecCUDAGetArray(), VecCUDAGetArrayWrite(), VecGetArray(), VecGetArrayRead()
2235: @*/
2236: PETSC_EXTERN PetscErrorCode VecCUDAGetArrayRead(Vec v,const PetscScalar** a)
2237: {
2240:    VecCUDAGetArray(v,(PetscScalar**)a);
2241:    return(0);
2242: }

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

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

2252:    Input Parameter:
2253: +  v - the vector
2254: -  a - the CUDA device pointer.  This pointer is invalid after
2255:        VecCUDARestoreArrayRead() returns.

2257:    Fortran note:
2258:    This function is not currently available from Fortran.

2260:    Level: intermediate

2262: .seealso: VecCUDAGetArrayRead(), VecCUDAGetArrayWrite(), VecCUDAGetArray(), VecGetArray(), VecRestoreArray(), VecGetArrayRead()
2263: @*/
2264: PETSC_EXTERN PetscErrorCode VecCUDARestoreArrayRead(Vec v, const PetscScalar **a)
2265: {
2268:   *a = NULL;
2269:   return(0);
2270: }

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

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

2281:    The device pointer needs to be released with
2282:    VecCUDARestoreArrayWrite().  When the pointer is released the
2283:    host data of the vector is marked as out of data.  Subsequent access
2284:    of the host data with e.g. VecGetArray() incurs a device to host data
2285:    transfer.


2288:    Input Parameter:
2289: .  v - the vector

2291:    Output Parameter:
2292: .  a - the CUDA pointer

2294:    Fortran note:
2295:    This function is not currently available from Fortran.

2297:    Level: advanced

2299: .seealso: VecCUDARestoreArrayWrite(), VecCUDAGetArray(), VecCUDAGetArrayRead(), VecCUDAGetArrayWrite(), VecGetArray(), VecGetArrayRead()
2300: @*/
2301: PETSC_EXTERN PetscErrorCode VecCUDAGetArrayWrite(Vec v, PetscScalar **a)
2302: {
2305:  #if defined(PETSC_HAVE_CUDA)
2306:   {
2308:     VecCUDAAllocateCheck(v);
2309:     *a   = ((Vec_CUDA*)v->spptr)->GPUarray;
2310:   }
2311:  #endif
2312:   return(0);
2313: }

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

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

2322:    Input Parameter:
2323: +  v - the vector
2324: -  a - the CUDA device pointer.  This pointer is invalid after
2325:        VecCUDARestoreArrayWrite() returns.

2327:    Fortran note:
2328:    This function is not currently available from Fortran.

2330:    Level: intermediate

2332: .seealso: VecCUDAGetArrayWrite(), VecCUDAGetArray(), VecCUDAGetArrayRead(), VecCUDAGetArrayWrite(), VecGetArray(), VecRestoreArray(), VecGetArrayRead()
2333: @*/
2334: PETSC_EXTERN PetscErrorCode VecCUDARestoreArrayWrite(Vec v, PetscScalar **a)
2335: {

2340:  #if defined(PETSC_HAVE_CUDA)
2341:   v->offloadmask = PETSC_OFFLOAD_GPU;
2342:   a              = NULL;
2343:  #endif
2344:   PetscObjectStateIncrease((PetscObject)v);
2345:   return(0);
2346: }

2348: /*@C
2349:    VecCUDAPlaceArray - Allows one to replace the GPU array in a vector with a
2350:    GPU array provided by the user. This is useful to avoid copying an
2351:    array into a vector.

2353:    Not Collective

2355:    Input Parameters:
2356: +  vec - the vector
2357: -  array - the GPU array

2359:    Notes:
2360:    You can return to the original GPU array with a call to VecCUDAResetArray()
2361:    It is not possible to use VecCUDAPlaceArray() and VecPlaceArray() at the
2362:    same time on the same vector.

2364:    Level: developer

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

2368: @*/
2369: PetscErrorCode VecCUDAPlaceArray(Vec vin,const PetscScalar a[])
2370: {

2375: #if defined(PETSC_HAVE_CUDA)
2376:   VecCUDACopyToGPU(vin);
2377:   if (((Vec_Seq*)vin->data)->unplacedarray) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"VecCUDAPlaceArray()/VecPlaceArray() was already called on this vector, without a call to VecCUDAResetArray()/VecResetArray()");
2378:   ((Vec_Seq*)vin->data)->unplacedarray  = (PetscScalar *) ((Vec_CUDA*)vin->spptr)->GPUarray; /* save previous GPU array so reset can bring it back */
2379:   ((Vec_CUDA*)vin->spptr)->GPUarray = (PetscScalar*)a;
2380:   vin->offloadmask = PETSC_OFFLOAD_GPU;
2381: #endif
2382:   PetscObjectStateIncrease((PetscObject)vin);
2383:   return(0);
2384: }

2386: /*@C
2387:    VecCUDAReplaceArray - Allows one to replace the GPU array in a vector
2388:    with a GPU array provided by the user. This is useful to avoid copying
2389:    a GPU array into a vector.

2391:    Not Collective

2393:    Input Parameters:
2394: +  vec - the vector
2395: -  array - the GPU array

2397:    Notes:
2398:    This permanently replaces the GPU array and frees the memory associated
2399:    with the old GPU array.

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

2404:    Not supported from Fortran

2406:    Level: developer

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

2410: @*/
2411: PetscErrorCode VecCUDAReplaceArray(Vec vin,const PetscScalar a[])
2412: {
2413: #if defined(PETSC_HAVE_CUDA)
2414:   cudaError_t err;
2415: #endif

2420: #if defined(PETSC_HAVE_CUDA)
2421:   err = cudaFree(((Vec_CUDA*)vin->spptr)->GPUarray);CHKERRCUDA(err);
2422:   ((Vec_CUDA*)vin->spptr)->GPUarray = (PetscScalar*)a;
2423:   vin->offloadmask = PETSC_OFFLOAD_GPU;
2424: #endif
2425:   PetscObjectStateIncrease((PetscObject)vin);
2426:   return(0);
2427: }

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

2433:    Not Collective

2435:    Input Parameters:
2436: .  vec - the vector

2438:    Level: developer

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

2442: @*/
2443: PetscErrorCode VecCUDAResetArray(Vec vin)
2444: {

2449: #if defined(PETSC_HAVE_CUDA)
2450:   VecCUDACopyToGPU(vin);
2451:   ((Vec_CUDA*)vin->spptr)->GPUarray = (PetscScalar *) ((Vec_Seq*)vin->data)->unplacedarray;
2452:   ((Vec_Seq*)vin->data)->unplacedarray = 0;
2453:   vin->offloadmask = PETSC_OFFLOAD_GPU;
2454: #endif
2455:   PetscObjectStateIncrease((PetscObject)vin);
2456:   return(0);
2457: }

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

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

2466:     Collective on Vec

2468:     Input Parameters:
2469: +   x - a vector to mimic
2470: -   n - the number of vectors to obtain

2472:     Output Parameters:
2473: +   y - Fortran90 pointer to the array of vectors
2474: -   ierr - error code

2476:     Example of Usage:
2477: .vb
2478: #include <petsc/finclude/petscvec.h>
2479:     use petscvec

2481:     Vec x
2482:     Vec, pointer :: y(:)
2483:     ....
2484:     call VecDuplicateVecsF90(x,2,y,ierr)
2485:     call VecSet(y(2),alpha,ierr)
2486:     call VecSet(y(2),alpha,ierr)
2487:     ....
2488:     call VecDestroyVecsF90(2,y,ierr)
2489: .ve

2491:     Notes:
2492:     Not yet supported for all F90 compilers

2494:     Use VecDestroyVecsF90() to free the space.

2496:     Level: beginner

2498: .seealso:  VecDestroyVecsF90(), VecDuplicateVecs()

2500: M*/

2502: /*MC
2503:     VecRestoreArrayF90 - Restores a vector to a usable state after a call to
2504:     VecGetArrayF90().

2506:     Synopsis:
2507:     VecRestoreArrayF90(Vec x,{Scalar, pointer :: xx_v(:)},integer ierr)

2509:     Logically Collective on Vec

2511:     Input Parameters:
2512: +   x - vector
2513: -   xx_v - the Fortran90 pointer to the array

2515:     Output Parameter:
2516: .   ierr - error code

2518:     Example of Usage:
2519: .vb
2520: #include <petsc/finclude/petscvec.h>
2521:     use petscvec

2523:     PetscScalar, pointer :: xx_v(:)
2524:     ....
2525:     call VecGetArrayF90(x,xx_v,ierr)
2526:     xx_v(3) = a
2527:     call VecRestoreArrayF90(x,xx_v,ierr)
2528: .ve

2530:     Level: beginner

2532: .seealso:  VecGetArrayF90(), VecGetArray(), VecRestoreArray(), UsingFortran, VecRestoreArrayReadF90()

2534: M*/

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

2539:     Synopsis:
2540:     VecDestroyVecsF90(PetscInt n,{Vec, pointer :: x(:)},PetscErrorCode ierr)

2542:     Collective on Vec

2544:     Input Parameters:
2545: +   n - the number of vectors previously obtained
2546: -   x - pointer to array of vector pointers

2548:     Output Parameter:
2549: .   ierr - error code

2551:     Notes:
2552:     Not yet supported for all F90 compilers

2554:     Level: beginner

2556: .seealso:  VecDestroyVecs(), VecDuplicateVecsF90()

2558: M*/

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

2566:     Synopsis:
2567:     VecGetArrayF90(Vec x,{Scalar, pointer :: xx_v(:)},integer ierr)

2569:     Logically Collective on Vec

2571:     Input Parameter:
2572: .   x - vector

2574:     Output Parameters:
2575: +   xx_v - the Fortran90 pointer to the array
2576: -   ierr - error code

2578:     Example of Usage:
2579: .vb
2580: #include <petsc/finclude/petscvec.h>
2581:     use petscvec

2583:     PetscScalar, pointer :: xx_v(:)
2584:     ....
2585:     call VecGetArrayF90(x,xx_v,ierr)
2586:     xx_v(3) = a
2587:     call VecRestoreArrayF90(x,xx_v,ierr)
2588: .ve

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

2592:     Level: beginner

2594: .seealso:  VecRestoreArrayF90(), VecGetArray(), VecRestoreArray(), VecGetArrayReadF90(), UsingFortran

2596: M*/

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

2604:     Synopsis:
2605:     VecGetArrayReadF90(Vec x,{Scalar, pointer :: xx_v(:)},integer ierr)

2607:     Logically Collective on Vec

2609:     Input Parameter:
2610: .   x - vector

2612:     Output Parameters:
2613: +   xx_v - the Fortran90 pointer to the array
2614: -   ierr - error code

2616:     Example of Usage:
2617: .vb
2618: #include <petsc/finclude/petscvec.h>
2619:     use petscvec

2621:     PetscScalar, pointer :: xx_v(:)
2622:     ....
2623:     call VecGetArrayReadF90(x,xx_v,ierr)
2624:     a = xx_v(3)
2625:     call VecRestoreArrayReadF90(x,xx_v,ierr)
2626: .ve

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

2630:     Level: beginner

2632: .seealso:  VecRestoreArrayReadF90(), VecGetArray(), VecRestoreArray(), VecGetArrayRead(), VecRestoreArrayRead(), VecGetArrayF90(), UsingFortran

2634: M*/

2636: /*MC
2637:     VecRestoreArrayReadF90 - Restores a readonly vector to a usable state after a call to
2638:     VecGetArrayReadF90().

2640:     Synopsis:
2641:     VecRestoreArrayReadF90(Vec x,{Scalar, pointer :: xx_v(:)},integer ierr)

2643:     Logically Collective on Vec

2645:     Input Parameters:
2646: +   x - vector
2647: -   xx_v - the Fortran90 pointer to the array

2649:     Output Parameter:
2650: .   ierr - error code

2652:     Example of Usage:
2653: .vb
2654: #include <petsc/finclude/petscvec.h>
2655:     use petscvec

2657:     PetscScalar, pointer :: xx_v(:)
2658:     ....
2659:     call VecGetArrayReadF90(x,xx_v,ierr)
2660:     a = xx_v(3)
2661:     call VecRestoreArrayReadF90(x,xx_v,ierr)
2662: .ve

2664:     Level: beginner

2666: .seealso:  VecGetArrayReadF90(), VecGetArray(), VecRestoreArray(), VecGetArrayRead(), VecRestoreArrayRead(),UsingFortran, VecRestoreArrayF90()

2668: M*/

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

2675:    Logically Collective

2677:    Input Parameter:
2678: +  x - the vector
2679: .  m - first dimension of two dimensional array
2680: .  n - second dimension of two dimensional array
2681: .  mstart - first index you will use in first coordinate direction (often 0)
2682: -  nstart - first index in the second coordinate direction (often 0)

2684:    Output Parameter:
2685: .  a - location to put pointer to the array

2687:    Level: developer

2689:   Notes:
2690:    For a vector obtained from DMCreateLocalVector() mstart and nstart are likely
2691:    obtained from the corner indices obtained from DMDAGetGhostCorners() while for
2692:    DMCreateGlobalVector() they are the corner indices from DMDAGetCorners(). In both cases
2693:    the arguments from DMDAGet[Ghost]Corners() are reversed in the call to VecGetArray2d().

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

2697: .seealso: VecGetArray(), VecRestoreArray(), VecGetArrays(), VecGetArrayF90(), VecPlaceArray(),
2698:           VecRestoreArray2d(), DMDAVecGetArray(), DMDAVecRestoreArray(), VecGetArray3d(), VecRestoreArray3d(),
2699:           VecGetArray1d(), VecRestoreArray1d(), VecGetArray4d(), VecRestoreArray4d()
2700: @*/
2701: PetscErrorCode  VecGetArray2d(Vec x,PetscInt m,PetscInt n,PetscInt mstart,PetscInt nstart,PetscScalar **a[])
2702: {
2704:   PetscInt       i,N;
2705:   PetscScalar    *aa;

2711:   VecGetLocalSize(x,&N);
2712:   if (m*n != N) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Local array size %D does not match 2d array dimensions %D by %D",N,m,n);
2713:   VecGetArray(x,&aa);

2715:   PetscMalloc1(m,a);
2716:   for (i=0; i<m; i++) (*a)[i] = aa + i*n - nstart;
2717:   *a -= mstart;
2718:   return(0);
2719: }

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

2726:    Logically Collective

2728:    Input Parameter:
2729: +  x - the vector
2730: .  m - first dimension of two dimensional array
2731: .  n - second dimension of two dimensional array
2732: .  mstart - first index you will use in first coordinate direction (often 0)
2733: -  nstart - first index in the second coordinate direction (often 0)

2735:    Output Parameter:
2736: .  a - location to put pointer to the array

2738:    Level: developer

2740:   Notes:
2741:    For a vector obtained from DMCreateLocalVector() mstart and nstart are likely
2742:    obtained from the corner indices obtained from DMDAGetGhostCorners() while for
2743:    DMCreateGlobalVector() they are the corner indices from DMDAGetCorners(). In both cases
2744:    the arguments from DMDAGet[Ghost]Corners() are reversed in the call to VecGetArray2d().

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

2748:    Concepts: vector^accessing local values as 2d array

2750: .seealso: VecGetArray(), VecRestoreArray(), VecGetArrays(), VecGetArrayF90(), VecPlaceArray(),
2751:           VecRestoreArray2d(), DMDAVecGetArray(), DMDAVecRestoreArray(), VecGetArray3d(), VecRestoreArray3d(),
2752:           VecGetArray1d(), VecRestoreArray1d(), VecGetArray4d(), VecRestoreArray4d()
2753: @*/
2754: PetscErrorCode  VecGetArray2dWrite(Vec x,PetscInt m,PetscInt n,PetscInt mstart,PetscInt nstart,PetscScalar **a[])
2755: {
2757:   PetscInt       i,N;
2758:   PetscScalar    *aa;

2764:   VecGetLocalSize(x,&N);
2765:   if (m*n != N) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Local array size %D does not match 2d array dimensions %D by %D",N,m,n);
2766:   VecGetArrayWrite(x,&aa);

2768:   PetscMalloc1(m,a);
2769:   for (i=0; i<m; i++) (*a)[i] = aa + i*n - nstart;
2770:   *a -= mstart;
2771:   return(0);
2772: }

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

2777:    Logically Collective

2779:    Input Parameters:
2780: +  x - the vector
2781: .  m - first dimension of two dimensional array
2782: .  n - second dimension of the two dimensional array
2783: .  mstart - first index you will use in first coordinate direction (often 0)
2784: .  nstart - first index in the second coordinate direction (often 0)
2785: -  a - location of pointer to array obtained from VecGetArray2d()

2787:    Level: developer

2789:    Notes:
2790:    For regular PETSc vectors this routine does not involve any copies. For
2791:    any special vectors that do not store local vector data in a contiguous
2792:    array, this routine will copy the data back into the underlying
2793:    vector data structure from the array obtained with VecGetArray().

2795:    This routine actually zeros out the a pointer.

2797: .seealso: VecGetArray(), VecRestoreArray(), VecRestoreArrays(), VecRestoreArrayF90(), VecPlaceArray(),
2798:           VecGetArray2d(), VecGetArray3d(), VecRestoreArray3d(), DMDAVecGetArray(), DMDAVecRestoreArray()
2799:           VecGetArray1d(), VecRestoreArray1d(), VecGetArray4d(), VecRestoreArray4d()
2800: @*/
2801: PetscErrorCode  VecRestoreArray2d(Vec x,PetscInt m,PetscInt n,PetscInt mstart,PetscInt nstart,PetscScalar **a[])
2802: {
2804:   void           *dummy;

2810:   dummy = (void*)(*a + mstart);
2811:   PetscFree(dummy);
2812:   VecRestoreArray(x,NULL);
2813:   return(0);
2814: }

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

2819:    Logically Collective

2821:    Input Parameters:
2822: +  x - the vector
2823: .  m - first dimension of two dimensional array
2824: .  n - second dimension of the two dimensional array
2825: .  mstart - first index you will use in first coordinate direction (often 0)
2826: .  nstart - first index in the second coordinate direction (often 0)
2827: -  a - location of pointer to array obtained from VecGetArray2d()

2829:    Level: developer

2831:    Notes:
2832:    For regular PETSc vectors this routine does not involve any copies. For
2833:    any special vectors that do not store local vector data in a contiguous
2834:    array, this routine will copy the data back into the underlying
2835:    vector data structure from the array obtained with VecGetArray().

2837:    This routine actually zeros out the a pointer.

2839: .seealso: VecGetArray(), VecRestoreArray(), VecRestoreArrays(), VecRestoreArrayF90(), VecPlaceArray(),
2840:           VecGetArray2d(), VecGetArray3d(), VecRestoreArray3d(), DMDAVecGetArray(), DMDAVecRestoreArray()
2841:           VecGetArray1d(), VecRestoreArray1d(), VecGetArray4d(), VecRestoreArray4d()
2842: @*/
2843: PetscErrorCode  VecRestoreArray2dWrite(Vec x,PetscInt m,PetscInt n,PetscInt mstart,PetscInt nstart,PetscScalar **a[])
2844: {
2846:   void           *dummy;

2852:   dummy = (void*)(*a + mstart);
2853:   PetscFree(dummy);
2854:   VecRestoreArrayWrite(x,NULL);
2855:   return(0);
2856: }

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

2863:    Logically Collective

2865:    Input Parameter:
2866: +  x - the vector
2867: .  m - first dimension of two dimensional array
2868: -  mstart - first index you will use in first coordinate direction (often 0)

2870:    Output Parameter:
2871: .  a - location to put pointer to the array

2873:    Level: developer

2875:   Notes:
2876:    For a vector obtained from DMCreateLocalVector() mstart are likely
2877:    obtained from the corner indices obtained from DMDAGetGhostCorners() while for
2878:    DMCreateGlobalVector() they are the corner indices from DMDAGetCorners().

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

2882: .seealso: VecGetArray(), VecRestoreArray(), VecGetArrays(), VecGetArrayF90(), VecPlaceArray(),
2883:           VecRestoreArray2d(), DMDAVecGetArray(), DMDAVecRestoreArray(), VecGetArray3d(), VecRestoreArray3d(),
2884:           VecGetArray2d(), VecRestoreArray1d(), VecGetArray4d(), VecRestoreArray4d()
2885: @*/
2886: PetscErrorCode  VecGetArray1d(Vec x,PetscInt m,PetscInt mstart,PetscScalar *a[])
2887: {
2889:   PetscInt       N;

2895:   VecGetLocalSize(x,&N);
2896:   if (m != N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Local array size %D does not match 1d array dimensions %D",N,m);
2897:   VecGetArray(x,a);
2898:   *a  -= mstart;
2899:   return(0);
2900: }

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

2907:    Logically Collective

2909:    Input Parameter:
2910: +  x - the vector
2911: .  m - first dimension of two dimensional array
2912: -  mstart - first index you will use in first coordinate direction (often 0)

2914:    Output Parameter:
2915: .  a - location to put pointer to the array

2917:    Level: developer

2919:   Notes:
2920:    For a vector obtained from DMCreateLocalVector() mstart are likely
2921:    obtained from the corner indices obtained from DMDAGetGhostCorners() while for
2922:    DMCreateGlobalVector() they are the corner indices from DMDAGetCorners().

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

2926: .seealso: VecGetArray(), VecRestoreArray(), VecGetArrays(), VecGetArrayF90(), VecPlaceArray(),
2927:           VecRestoreArray2d(), DMDAVecGetArray(), DMDAVecRestoreArray(), VecGetArray3d(), VecRestoreArray3d(),
2928:           VecGetArray2d(), VecRestoreArray1d(), VecGetArray4d(), VecRestoreArray4d()
2929: @*/
2930: PetscErrorCode  VecGetArray1dWrite(Vec x,PetscInt m,PetscInt mstart,PetscScalar *a[])
2931: {
2933:   PetscInt       N;

2939:   VecGetLocalSize(x,&N);
2940:   if (m != N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Local array size %D does not match 1d array dimensions %D",N,m);
2941:   VecGetArrayWrite(x,a);
2942:   *a  -= mstart;
2943:   return(0);
2944: }

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

2949:    Logically Collective

2951:    Input Parameters:
2952: +  x - the vector
2953: .  m - first dimension of two dimensional array
2954: .  mstart - first index you will use in first coordinate direction (often 0)
2955: -  a - location of pointer to array obtained from VecGetArray21()

2957:    Level: developer

2959:    Notes:
2960:    For regular PETSc vectors this routine does not involve any copies. For
2961:    any special vectors that do not store local vector data in a contiguous
2962:    array, this routine will copy the data back into the underlying
2963:    vector data structure from the array obtained with VecGetArray1d().

2965:    This routine actually zeros out the a pointer.

2967: .seealso: VecGetArray(), VecRestoreArray(), VecRestoreArrays(), VecRestoreArrayF90(), VecPlaceArray(),
2968:           VecGetArray2d(), VecGetArray3d(), VecRestoreArray3d(), DMDAVecGetArray(), DMDAVecRestoreArray()
2969:           VecGetArray1d(), VecRestoreArray2d(), VecGetArray4d(), VecRestoreArray4d()
2970: @*/
2971: PetscErrorCode  VecRestoreArray1d(Vec x,PetscInt m,PetscInt mstart,PetscScalar *a[])
2972: {

2978:   VecRestoreArray(x,NULL);
2979:   return(0);
2980: }

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

2985:    Logically Collective

2987:    Input Parameters:
2988: +  x - the vector
2989: .  m - first dimension of two dimensional array
2990: .  mstart - first index you will use in first coordinate direction (often 0)
2991: -  a - location of pointer to array obtained from VecGetArray21()

2993:    Level: developer

2995:    Notes:
2996:    For regular PETSc vectors this routine does not involve any copies. For
2997:    any special vectors that do not store local vector data in a contiguous
2998:    array, this routine will copy the data back into the underlying
2999:    vector data structure from the array obtained with VecGetArray1d().

3001:    This routine actually zeros out the a pointer.

3003:    Concepts: vector^accessing local values as 1d array

3005: .seealso: VecGetArray(), VecRestoreArray(), VecRestoreArrays(), VecRestoreArrayF90(), VecPlaceArray(),
3006:           VecGetArray2d(), VecGetArray3d(), VecRestoreArray3d(), DMDAVecGetArray(), DMDAVecRestoreArray()
3007:           VecGetArray1d(), VecRestoreArray2d(), VecGetArray4d(), VecRestoreArray4d()
3008: @*/
3009: PetscErrorCode  VecRestoreArray1dWrite(Vec x,PetscInt m,PetscInt mstart,PetscScalar *a[])
3010: {

3016:   VecRestoreArrayWrite(x,NULL);
3017:   return(0);
3018: }

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

3025:    Logically Collective

3027:    Input Parameter:
3028: +  x - the vector
3029: .  m - first dimension of three dimensional array
3030: .  n - second dimension of three dimensional array
3031: .  p - third dimension of three dimensional array
3032: .  mstart - first index you will use in first coordinate direction (often 0)
3033: .  nstart - first index in the second coordinate direction (often 0)
3034: -  pstart - first index in the third coordinate direction (often 0)

3036:    Output Parameter:
3037: .  a - location to put pointer to the array

3039:    Level: developer

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

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

3049: .seealso: VecGetArray(), VecRestoreArray(), VecGetArrays(), VecGetArrayF90(), VecPlaceArray(),
3050:           VecRestoreArray2d(), DMDAVecGetarray(), DMDAVecRestoreArray(), VecGetArray3d(), VecRestoreArray3d(),
3051:           VecGetArray1d(), VecRestoreArray1d(), VecGetArray4d(), VecRestoreArray4d()
3052: @*/
3053: PetscErrorCode  VecGetArray3d(Vec x,PetscInt m,PetscInt n,PetscInt p,PetscInt mstart,PetscInt nstart,PetscInt pstart,PetscScalar ***a[])
3054: {
3056:   PetscInt       i,N,j;
3057:   PetscScalar    *aa,**b;

3063:   VecGetLocalSize(x,&N);
3064:   if (m*n*p != N) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Local array size %D does not match 3d array dimensions %D by %D by %D",N,m,n,p);
3065:   VecGetArray(x,&aa);

3067:   PetscMalloc1(m*sizeof(PetscScalar**)+m*n,a);
3068:   b    = (PetscScalar**)((*a) + m);
3069:   for (i=0; i<m; i++) (*a)[i] = b + i*n - nstart;
3070:   for (i=0; i<m; i++)
3071:     for (j=0; j<n; j++)
3072:       b[i*n+j] = aa + i*n*p + j*p - pstart;

3074:   *a -= mstart;
3075:   return(0);
3076: }

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

3083:    Logically Collective

3085:    Input Parameter:
3086: +  x - the vector
3087: .  m - first dimension of three dimensional array
3088: .  n - second dimension of three dimensional array
3089: .  p - third dimension of three dimensional array
3090: .  mstart - first index you will use in first coordinate direction (often 0)
3091: .  nstart - first index in the second coordinate direction (often 0)
3092: -  pstart - first index in the third coordinate direction (often 0)

3094:    Output Parameter:
3095: .  a - location to put pointer to the array

3097:    Level: developer

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

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

3107:    Concepts: vector^accessing local values as 3d array

3109: .seealso: VecGetArray(), VecRestoreArray(), VecGetArrays(), VecGetArrayF90(), VecPlaceArray(),
3110:           VecRestoreArray2d(), DMDAVecGetarray(), DMDAVecRestoreArray(), VecGetArray3d(), VecRestoreArray3d(),
3111:           VecGetArray1d(), VecRestoreArray1d(), VecGetArray4d(), VecRestoreArray4d()
3112: @*/
3113: PetscErrorCode  VecGetArray3dWrite(Vec x,PetscInt m,PetscInt n,PetscInt p,PetscInt mstart,PetscInt nstart,PetscInt pstart,PetscScalar ***a[])
3114: {
3116:   PetscInt       i,N,j;
3117:   PetscScalar    *aa,**b;

3123:   VecGetLocalSize(x,&N);
3124:   if (m*n*p != N) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Local array size %D does not match 3d array dimensions %D by %D by %D",N,m,n,p);
3125:   VecGetArrayWrite(x,&aa);

3127:   PetscMalloc1(m*sizeof(PetscScalar**)+m*n,a);
3128:   b    = (PetscScalar**)((*a) + m);
3129:   for (i=0; i<m; i++) (*a)[i] = b + i*n - nstart;
3130:   for (i=0; i<m; i++)
3131:     for (j=0; j<n; j++)
3132:       b[i*n+j] = aa + i*n*p + j*p - pstart;

3134:   *a -= mstart;
3135:   return(0);
3136: }

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

3141:    Logically Collective

3143:    Input Parameters:
3144: +  x - the vector
3145: .  m - first dimension of three dimensional array
3146: .  n - second dimension of the three dimensional array
3147: .  p - third dimension of the three dimensional array
3148: .  mstart - first index you will use in first coordinate direction (often 0)
3149: .  nstart - first index in the second coordinate direction (often 0)
3150: .  pstart - first index in the third coordinate direction (often 0)
3151: -  a - location of pointer to array obtained from VecGetArray3d()

3153:    Level: developer

3155:    Notes:
3156:    For regular PETSc vectors this routine does not involve any copies. For
3157:    any special vectors that do not store local vector data in a contiguous
3158:    array, this routine will copy the data back into the underlying
3159:    vector data structure from the array obtained with VecGetArray().

3161:    This routine actually zeros out the a pointer.

3163: .seealso: VecGetArray(), VecRestoreArray(), VecRestoreArrays(), VecRestoreArrayF90(), VecPlaceArray(),
3164:           VecGetArray2d(), VecGetArray3d(), VecRestoreArray3d(), DMDAVecGetArray(), DMDAVecRestoreArray()
3165:           VecGetArray1d(), VecRestoreArray1d(), VecGetArray4d(), VecRestoreArray4d(), VecGet
3166: @*/
3167: PetscErrorCode  VecRestoreArray3d(Vec x,PetscInt m,PetscInt n,PetscInt p,PetscInt mstart,PetscInt nstart,PetscInt pstart,PetscScalar ***a[])
3168: {
3170:   void           *dummy;

3176:   dummy = (void*)(*a + mstart);
3177:   PetscFree(dummy);
3178:   VecRestoreArray(x,NULL);
3179:   return(0);
3180: }

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

3185:    Logically Collective

3187:    Input Parameters:
3188: +  x - the vector
3189: .  m - first dimension of three dimensional array
3190: .  n - second dimension of the three dimensional array
3191: .  p - third dimension of the three dimensional array
3192: .  mstart - first index you will use in first coordinate direction (often 0)
3193: .  nstart - first index in the second coordinate direction (often 0)
3194: .  pstart - first index in the third coordinate direction (often 0)
3195: -  a - location of pointer to array obtained from VecGetArray3d()

3197:    Level: developer

3199:    Notes:
3200:    For regular PETSc vectors this routine does not involve any copies. For
3201:    any special vectors that do not store local vector data in a contiguous
3202:    array, this routine will copy the data back into the underlying
3203:    vector data structure from the array obtained with VecGetArray().

3205:    This routine actually zeros out the a pointer.

3207: .seealso: VecGetArray(), VecRestoreArray(), VecRestoreArrays(), VecRestoreArrayF90(), VecPlaceArray(),
3208:           VecGetArray2d(), VecGetArray3d(), VecRestoreArray3d(), DMDAVecGetArray(), DMDAVecRestoreArray()
3209:           VecGetArray1d(), VecRestoreArray1d(), VecGetArray4d(), VecRestoreArray4d(), VecGet
3210: @*/
3211: PetscErrorCode  VecRestoreArray3dWrite(Vec x,PetscInt m,PetscInt n,PetscInt p,PetscInt mstart,PetscInt nstart,PetscInt pstart,PetscScalar ***a[])
3212: {
3214:   void           *dummy;

3220:   dummy = (void*)(*a + mstart);
3221:   PetscFree(dummy);
3222:   VecRestoreArrayWrite(x,NULL);
3223:   return(0);
3224: }

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

3231:    Logically Collective

3233:    Input Parameter:
3234: +  x - the vector
3235: .  m - first dimension of four dimensional array
3236: .  n - second dimension of four dimensional array
3237: .  p - third dimension of four dimensional array
3238: .  q - fourth dimension of four dimensional array
3239: .  mstart - first index you will use in first coordinate direction (often 0)
3240: .  nstart - first index in the second coordinate direction (often 0)
3241: .  pstart - first index in the third coordinate direction (often 0)
3242: -  qstart - first index in the fourth coordinate direction (often 0)

3244:    Output Parameter:
3245: .  a - location to put pointer to the array

3247:    Level: beginner

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

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

3257: .seealso: VecGetArray(), VecRestoreArray(), VecGetArrays(), VecGetArrayF90(), VecPlaceArray(),
3258:           VecRestoreArray2d(), DMDAVecGetarray(), DMDAVecRestoreArray(), VecGetArray3d(), VecRestoreArray3d(),
3259:           VecGetArray1d(), VecRestoreArray1d(), VecGetArray4d(), VecRestoreArray4d()
3260: @*/
3261: PetscErrorCode  VecGetArray4d(Vec x,PetscInt m,PetscInt n,PetscInt p,PetscInt q,PetscInt mstart,PetscInt nstart,PetscInt pstart,PetscInt qstart,PetscScalar ****a[])
3262: {
3264:   PetscInt       i,N,j,k;
3265:   PetscScalar    *aa,***b,**c;

3271:   VecGetLocalSize(x,&N);
3272:   if (m*n*p*q != N) SETERRQ5(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Local array size %D does not match 4d array dimensions %D by %D by %D by %D",N,m,n,p,q);
3273:   VecGetArray(x,&aa);

3275:   PetscMalloc1(m*sizeof(PetscScalar***)+m*n*sizeof(PetscScalar**)+m*n*p,a);
3276:   b    = (PetscScalar***)((*a) + m);
3277:   c    = (PetscScalar**)(b + m*n);
3278:   for (i=0; i<m; i++) (*a)[i] = b + i*n - nstart;
3279:   for (i=0; i<m; i++)
3280:     for (j=0; j<n; j++)
3281:       b[i*n+j] = c + i*n*p + j*p - pstart;
3282:   for (i=0; i<m; i++)
3283:     for (j=0; j<n; j++)
3284:       for (k=0; k<p; k++)
3285:         c[i*n*p+j*p+k] = aa + i*n*p*q + j*p*q + k*q - qstart;
3286:   *a -= mstart;
3287:   return(0);
3288: }

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

3295:    Logically Collective

3297:    Input Parameter:
3298: +  x - the vector
3299: .  m - first dimension of four dimensional array
3300: .  n - second dimension of four dimensional array
3301: .  p - third dimension of four dimensional array
3302: .  q - fourth dimension of four dimensional array
3303: .  mstart - first index you will use in first coordinate direction (often 0)
3304: .  nstart - first index in the second coordinate direction (often 0)
3305: .  pstart - first index in the third coordinate direction (often 0)
3306: -  qstart - first index in the fourth coordinate direction (often 0)

3308:    Output Parameter:
3309: .  a - location to put pointer to the array

3311:    Level: beginner

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

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

3321:    Concepts: vector^accessing local values as 3d array

3323: .seealso: VecGetArray(), VecRestoreArray(), VecGetArrays(), VecGetArrayF90(), VecPlaceArray(),
3324:           VecRestoreArray2d(), DMDAVecGetarray(), DMDAVecRestoreArray(), VecGetArray3d(), VecRestoreArray3d(),
3325:           VecGetArray1d(), VecRestoreArray1d(), VecGetArray4d(), VecRestoreArray4d()
3326: @*/
3327: PetscErrorCode  VecGetArray4dWrite(Vec x,PetscInt m,PetscInt n,PetscInt p,PetscInt q,PetscInt mstart,PetscInt nstart,PetscInt pstart,PetscInt qstart,PetscScalar ****a[])
3328: {
3330:   PetscInt       i,N,j,k;
3331:   PetscScalar    *aa,***b,**c;

3337:   VecGetLocalSize(x,&N);
3338:   if (m*n*p*q != N) SETERRQ5(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Local array size %D does not match 4d array dimensions %D by %D by %D by %D",N,m,n,p,q);
3339:   VecGetArrayWrite(x,&aa);

3341:   PetscMalloc1(m*sizeof(PetscScalar***)+m*n*sizeof(PetscScalar**)+m*n*p,a);
3342:   b    = (PetscScalar***)((*a) + m);
3343:   c    = (PetscScalar**)(b + m*n);
3344:   for (i=0; i<m; i++) (*a)[i] = b + i*n - nstart;
3345:   for (i=0; i<m; i++)
3346:     for (j=0; j<n; j++)
3347:       b[i*n+j] = c + i*n*p + j*p - pstart;
3348:   for (i=0; i<m; i++)
3349:     for (j=0; j<n; j++)
3350:       for (k=0; k<p; k++)
3351:         c[i*n*p+j*p+k] = aa + i*n*p*q + j*p*q + k*q - qstart;
3352:   *a -= mstart;
3353:   return(0);
3354: }

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

3359:    Logically Collective

3361:    Input Parameters:
3362: +  x - the vector
3363: .  m - first dimension of four dimensional array
3364: .  n - second dimension of the four dimensional array
3365: .  p - third dimension of the four dimensional array
3366: .  q - fourth dimension of the four dimensional array
3367: .  mstart - first index you will use in first coordinate direction (often 0)
3368: .  nstart - first index in the second coordinate direction (often 0)
3369: .  pstart - first index in the third coordinate direction (often 0)
3370: .  qstart - first index in the fourth coordinate direction (often 0)
3371: -  a - location of pointer to array obtained from VecGetArray4d()

3373:    Level: beginner

3375:    Notes:
3376:    For regular PETSc vectors this routine does not involve any copies. For
3377:    any special vectors that do not store local vector data in a contiguous
3378:    array, this routine will copy the data back into the underlying
3379:    vector data structure from the array obtained with VecGetArray().

3381:    This routine actually zeros out the a pointer.

3383: .seealso: VecGetArray(), VecRestoreArray(), VecRestoreArrays(), VecRestoreArrayF90(), VecPlaceArray(),
3384:           VecGetArray2d(), VecGetArray3d(), VecRestoreArray3d(), DMDAVecGetArray(), DMDAVecRestoreArray()
3385:           VecGetArray1d(), VecRestoreArray1d(), VecGetArray4d(), VecRestoreArray4d(), VecGet
3386: @*/
3387: PetscErrorCode  VecRestoreArray4d(Vec x,PetscInt m,PetscInt n,PetscInt p,PetscInt q,PetscInt mstart,PetscInt nstart,PetscInt pstart,PetscInt qstart,PetscScalar ****a[])
3388: {
3390:   void           *dummy;

3396:   dummy = (void*)(*a + mstart);
3397:   PetscFree(dummy);
3398:   VecRestoreArray(x,NULL);
3399:   return(0);
3400: }

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

3405:    Logically Collective

3407:    Input Parameters:
3408: +  x - the vector
3409: .  m - first dimension of four dimensional array
3410: .  n - second dimension of the four dimensional array
3411: .  p - third dimension of the four dimensional array
3412: .  q - fourth dimension of the four 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: .  pstart - first index in the third coordinate direction (often 0)
3416: .  qstart - first index in the fourth coordinate direction (often 0)
3417: -  a - location of pointer to array obtained from VecGetArray4d()

3419:    Level: beginner

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

3427:    This routine actually zeros out the a pointer.

3429: .seealso: VecGetArray(), VecRestoreArray(), VecRestoreArrays(), VecRestoreArrayF90(), VecPlaceArray(),
3430:           VecGetArray2d(), VecGetArray3d(), VecRestoreArray3d(), DMDAVecGetArray(), DMDAVecRestoreArray()
3431:           VecGetArray1d(), VecRestoreArray1d(), VecGetArray4d(), VecRestoreArray4d(), VecGet
3432: @*/
3433: PetscErrorCode  VecRestoreArray4dWrite(Vec x,PetscInt m,PetscInt n,PetscInt p,PetscInt q,PetscInt mstart,PetscInt nstart,PetscInt pstart,PetscInt qstart,PetscScalar ****a[])
3434: {
3436:   void           *dummy;

3442:   dummy = (void*)(*a + mstart);
3443:   PetscFree(dummy);
3444:   VecRestoreArrayWrite(x,NULL);
3445:   return(0);
3446: }

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

3453:    Logically Collective

3455:    Input Parameter:
3456: +  x - the vector
3457: .  m - first dimension of two dimensional array
3458: .  n - second dimension of two dimensional array
3459: .  mstart - first index you will use in first coordinate direction (often 0)
3460: -  nstart - first index in the second coordinate direction (often 0)

3462:    Output Parameter:
3463: .  a - location to put pointer to the array

3465:    Level: developer

3467:   Notes:
3468:    For a vector obtained from DMCreateLocalVector() mstart and nstart are likely
3469:    obtained from the corner indices obtained from DMDAGetGhostCorners() while for
3470:    DMCreateGlobalVector() they are the corner indices from DMDAGetCorners(). In both cases
3471:    the arguments from DMDAGet[Ghost]Corners() are reversed in the call to VecGetArray2d().

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

3475: .seealso: VecGetArray(), VecRestoreArray(), VecGetArrays(), VecGetArrayF90(), VecPlaceArray(),
3476:           VecRestoreArray2d(), DMDAVecGetArray(), DMDAVecRestoreArray(), VecGetArray3d(), VecRestoreArray3d(),
3477:           VecGetArray1d(), VecRestoreArray1d(), VecGetArray4d(), VecRestoreArray4d()
3478: @*/
3479: PetscErrorCode  VecGetArray2dRead(Vec x,PetscInt m,PetscInt n,PetscInt mstart,PetscInt nstart,PetscScalar **a[])
3480: {
3481:   PetscErrorCode    ierr;
3482:   PetscInt          i,N;
3483:   const PetscScalar *aa;

3489:   VecGetLocalSize(x,&N);
3490:   if (m*n != N) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Local array size %D does not match 2d array dimensions %D by %D",N,m,n);
3491:   VecGetArrayRead(x,&aa);

3493:   PetscMalloc1(m,a);
3494:   for (i=0; i<m; i++) (*a)[i] = (PetscScalar*) aa + i*n - nstart;
3495:   *a -= mstart;
3496:   return(0);
3497: }

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

3502:    Logically Collective

3504:    Input Parameters:
3505: +  x - the vector
3506: .  m - first dimension of two dimensional array
3507: .  n - second dimension of the two dimensional array
3508: .  mstart - first index you will use in first coordinate direction (often 0)
3509: .  nstart - first index in the second coordinate direction (often 0)
3510: -  a - location of pointer to array obtained from VecGetArray2d()

3512:    Level: developer

3514:    Notes:
3515:    For regular PETSc vectors this routine does not involve any copies. For
3516:    any special vectors that do not store local vector data in a contiguous
3517:    array, this routine will copy the data back into the underlying
3518:    vector data structure from the array obtained with VecGetArray().

3520:    This routine actually zeros out the a pointer.

3522: .seealso: VecGetArray(), VecRestoreArray(), VecRestoreArrays(), VecRestoreArrayF90(), VecPlaceArray(),
3523:           VecGetArray2d(), VecGetArray3d(), VecRestoreArray3d(), DMDAVecGetArray(), DMDAVecRestoreArray()
3524:           VecGetArray1d(), VecRestoreArray1d(), VecGetArray4d(), VecRestoreArray4d()
3525: @*/
3526: PetscErrorCode  VecRestoreArray2dRead(Vec x,PetscInt m,PetscInt n,PetscInt mstart,PetscInt nstart,PetscScalar **a[])
3527: {
3529:   void           *dummy;

3535:   dummy = (void*)(*a + mstart);
3536:   PetscFree(dummy);
3537:   VecRestoreArrayRead(x,NULL);
3538:   return(0);
3539: }

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

3546:    Logically Collective

3548:    Input Parameter:
3549: +  x - the vector
3550: .  m - first dimension of two dimensional array
3551: -  mstart - first index you will use in first coordinate direction (often 0)

3553:    Output Parameter:
3554: .  a - location to put pointer to the array

3556:    Level: developer

3558:   Notes:
3559:    For a vector obtained from DMCreateLocalVector() mstart are likely
3560:    obtained from the corner indices obtained from DMDAGetGhostCorners() while for
3561:    DMCreateGlobalVector() they are the corner indices from DMDAGetCorners().

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

3565: .seealso: VecGetArray(), VecRestoreArray(), VecGetArrays(), VecGetArrayF90(), VecPlaceArray(),
3566:           VecRestoreArray2d(), DMDAVecGetArray(), DMDAVecRestoreArray(), VecGetArray3d(), VecRestoreArray3d(),
3567:           VecGetArray2d(), VecRestoreArray1d(), VecGetArray4d(), VecRestoreArray4d()
3568: @*/
3569: PetscErrorCode  VecGetArray1dRead(Vec x,PetscInt m,PetscInt mstart,PetscScalar *a[])
3570: {
3572:   PetscInt       N;

3578:   VecGetLocalSize(x,&N);
3579:   if (m != N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Local array size %D does not match 1d array dimensions %D",N,m);
3580:   VecGetArrayRead(x,(const PetscScalar**)a);
3581:   *a  -= mstart;
3582:   return(0);
3583: }

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

3588:    Logically Collective

3590:    Input Parameters:
3591: +  x - the vector
3592: .  m - first dimension of two dimensional array
3593: .  mstart - first index you will use in first coordinate direction (often 0)
3594: -  a - location of pointer to array obtained from VecGetArray21()

3596:    Level: developer

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

3604:    This routine actually zeros out the a pointer.

3606: .seealso: VecGetArray(), VecRestoreArray(), VecRestoreArrays(), VecRestoreArrayF90(), VecPlaceArray(),
3607:           VecGetArray2d(), VecGetArray3d(), VecRestoreArray3d(), DMDAVecGetArray(), DMDAVecRestoreArray()
3608:           VecGetArray1d(), VecRestoreArray2d(), VecGetArray4d(), VecRestoreArray4d()
3609: @*/
3610: PetscErrorCode  VecRestoreArray1dRead(Vec x,PetscInt m,PetscInt mstart,PetscScalar *a[])
3611: {

3617:   VecRestoreArrayRead(x,NULL);
3618:   return(0);
3619: }


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

3627:    Logically Collective

3629:    Input Parameter:
3630: +  x - the vector
3631: .  m - first dimension of three dimensional array
3632: .  n - second dimension of three dimensional array
3633: .  p - third dimension of three dimensional array
3634: .  mstart - first index you will use in first coordinate direction (often 0)
3635: .  nstart - first index in the second coordinate direction (often 0)
3636: -  pstart - first index in the third coordinate direction (often 0)

3638:    Output Parameter:
3639: .  a - location to put pointer to the array

3641:    Level: developer

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

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

3651: .seealso: VecGetArray(), VecRestoreArray(), VecGetArrays(), VecGetArrayF90(), VecPlaceArray(),
3652:           VecRestoreArray2d(), DMDAVecGetarray(), DMDAVecRestoreArray(), VecGetArray3d(), VecRestoreArray3d(),
3653:           VecGetArray1d(), VecRestoreArray1d(), VecGetArray4d(), VecRestoreArray4d()
3654: @*/
3655: PetscErrorCode  VecGetArray3dRead(Vec x,PetscInt m,PetscInt n,PetscInt p,PetscInt mstart,PetscInt nstart,PetscInt pstart,PetscScalar ***a[])
3656: {
3657:   PetscErrorCode    ierr;
3658:   PetscInt          i,N,j;
3659:   const PetscScalar *aa;
3660:   PetscScalar       **b;

3666:   VecGetLocalSize(x,&N);
3667:   if (m*n*p != N) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Local array size %D does not match 3d array dimensions %D by %D by %D",N,m,n,p);
3668:   VecGetArrayRead(x,&aa);

3670:   PetscMalloc1(m*sizeof(PetscScalar**)+m*n,a);
3671:   b    = (PetscScalar**)((*a) + m);
3672:   for (i=0; i<m; i++) (*a)[i] = b + i*n - nstart;
3673:   for (i=0; i<m; i++)
3674:     for (j=0; j<n; j++)
3675:       b[i*n+j] = (PetscScalar *)aa + i*n*p + j*p - pstart;

3677:   *a -= mstart;
3678:   return(0);
3679: }

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

3684:    Logically Collective

3686:    Input Parameters:
3687: +  x - the vector
3688: .  m - first dimension of three dimensional array
3689: .  n - second dimension of the three dimensional array
3690: .  p - third dimension of the three dimensional array
3691: .  mstart - first index you will use in first coordinate direction (often 0)
3692: .  nstart - first index in the second coordinate direction (often 0)
3693: .  pstart - first index in the third coordinate direction (often 0)
3694: -  a - location of pointer to array obtained from VecGetArray3dRead()

3696:    Level: developer

3698:    Notes:
3699:    For regular PETSc vectors this routine does not involve any copies. For
3700:    any special vectors that do not store local vector data in a contiguous
3701:    array, this routine will copy the data back into the underlying
3702:    vector data structure from the array obtained with VecGetArray().

3704:    This routine actually zeros out the a pointer.

3706: .seealso: VecGetArray(), VecRestoreArray(), VecRestoreArrays(), VecRestoreArrayF90(), VecPlaceArray(),
3707:           VecGetArray2d(), VecGetArray3d(), VecRestoreArray3d(), DMDAVecGetArray(), DMDAVecRestoreArray()
3708:           VecGetArray1d(), VecRestoreArray1d(), VecGetArray4d(), VecRestoreArray4d(), VecGet
3709: @*/
3710: PetscErrorCode  VecRestoreArray3dRead(Vec x,PetscInt m,PetscInt n,PetscInt p,PetscInt mstart,PetscInt nstart,PetscInt pstart,PetscScalar ***a[])
3711: {
3713:   void           *dummy;

3719:   dummy = (void*)(*a + mstart);
3720:   PetscFree(dummy);
3721:   VecRestoreArrayRead(x,NULL);
3722:   return(0);
3723: }

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

3730:    Logically Collective

3732:    Input Parameter:
3733: +  x - the vector
3734: .  m - first dimension of four dimensional array
3735: .  n - second dimension of four dimensional array
3736: .  p - third dimension of four dimensional array
3737: .  q - fourth dimension of four dimensional array
3738: .  mstart - first index you will use in first coordinate direction (often 0)
3739: .  nstart - first index in the second coordinate direction (often 0)
3740: .  pstart - first index in the third coordinate direction (often 0)
3741: -  qstart - first index in the fourth coordinate direction (often 0)

3743:    Output Parameter:
3744: .  a - location to put pointer to the array

3746:    Level: beginner

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

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

3756: .seealso: VecGetArray(), VecRestoreArray(), VecGetArrays(), VecGetArrayF90(), VecPlaceArray(),
3757:           VecRestoreArray2d(), DMDAVecGetarray(), DMDAVecRestoreArray(), VecGetArray3d(), VecRestoreArray3d(),
3758:           VecGetArray1d(), VecRestoreArray1d(), VecGetArray4d(), VecRestoreArray4d()
3759: @*/
3760: PetscErrorCode  VecGetArray4dRead(Vec x,PetscInt m,PetscInt n,PetscInt p,PetscInt q,PetscInt mstart,PetscInt nstart,PetscInt pstart,PetscInt qstart,PetscScalar ****a[])
3761: {
3762:   PetscErrorCode    ierr;
3763:   PetscInt          i,N,j,k;
3764:   const PetscScalar *aa;
3765:   PetscScalar       ***b,**c;

3771:   VecGetLocalSize(x,&N);
3772:   if (m*n*p*q != N) SETERRQ5(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Local array size %D does not match 4d array dimensions %D by %D by %D by %D",N,m,n,p,q);
3773:   VecGetArrayRead(x,&aa);

3775:   PetscMalloc1(m*sizeof(PetscScalar***)+m*n*sizeof(PetscScalar**)+m*n*p,a);
3776:   b    = (PetscScalar***)((*a) + m);
3777:   c    = (PetscScalar**)(b + m*n);
3778:   for (i=0; i<m; i++) (*a)[i] = b + i*n - nstart;
3779:   for (i=0; i<m; i++)
3780:     for (j=0; j<n; j++)
3781:       b[i*n+j] = c + i*n*p + j*p - pstart;
3782:   for (i=0; i<m; i++)
3783:     for (j=0; j<n; j++)
3784:       for (k=0; k<p; k++)
3785:         c[i*n*p+j*p+k] = (PetscScalar*) aa + i*n*p*q + j*p*q + k*q - qstart;
3786:   *a -= mstart;
3787:   return(0);
3788: }

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

3793:    Logically Collective

3795:    Input Parameters:
3796: +  x - the vector
3797: .  m - first dimension of four dimensional array
3798: .  n - second dimension of the four dimensional array
3799: .  p - third dimension of the four dimensional array
3800: .  q - fourth dimension of the four dimensional array
3801: .  mstart - first index you will use in first coordinate direction (often 0)
3802: .  nstart - first index in the second coordinate direction (often 0)
3803: .  pstart - first index in the third coordinate direction (often 0)
3804: .  qstart - first index in the fourth coordinate direction (often 0)
3805: -  a - location of pointer to array obtained from VecGetArray4dRead()

3807:    Level: beginner

3809:    Notes:
3810:    For regular PETSc vectors this routine does not involve any copies. For
3811:    any special vectors that do not store local vector data in a contiguous
3812:    array, this routine will copy the data back into the underlying
3813:    vector data structure from the array obtained with VecGetArray().

3815:    This routine actually zeros out the a pointer.

3817: .seealso: VecGetArray(), VecRestoreArray(), VecRestoreArrays(), VecRestoreArrayF90(), VecPlaceArray(),
3818:           VecGetArray2d(), VecGetArray3d(), VecRestoreArray3d(), DMDAVecGetArray(), DMDAVecRestoreArray()
3819:           VecGetArray1d(), VecRestoreArray1d(), VecGetArray4d(), VecRestoreArray4d(), VecGet
3820: @*/
3821: PetscErrorCode  VecRestoreArray4dRead(Vec x,PetscInt m,PetscInt n,PetscInt p,PetscInt q,PetscInt mstart,PetscInt nstart,PetscInt pstart,PetscInt qstart,PetscScalar ****a[])
3822: {
3824:   void           *dummy;

3830:   dummy = (void*)(*a + mstart);
3831:   PetscFree(dummy);
3832:   VecRestoreArrayRead(x,NULL);
3833:   return(0);
3834: }

3836: #if defined(PETSC_USE_DEBUG)

3838: /*@
3839:    VecLockGet  - Gets the current lock status of a vector

3841:    Logically Collective on Vec

3843:    Input Parameter:
3844: .  x - the vector

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

3850:    Level: beginner

3852: .seealso: VecRestoreArray(), VecGetArrayRead(), VecLockReadPush(), VecLockReadPop()
3853: @*/
3854: PetscErrorCode VecLockGet(Vec x,PetscInt *state)
3855: {
3858:   *state = x->lock;
3859:   return(0);
3860: }

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

3865:    Logically Collective on Vec

3867:    Input Parameter:
3868: .  x - the vector

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

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

3876:    Level: beginner

3878: .seealso: VecRestoreArray(), VecGetArrayRead(), VecLockReadPop(), VecLockGet()
3879: @*/
3880: PetscErrorCode VecLockReadPush(Vec x)
3881: {
3884:   if (x->lock < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Vector is already locked for exclusive write access but you want to read it");
3885:   x->lock++;
3886:   return(0);
3887: }

3889: /*@
3890:    VecLockReadPop  - Pops a read-only lock from a vector

3892:    Logically Collective on Vec

3894:    Input Parameter:
3895: .  x - the vector

3897:    Level: beginner

3899: .seealso: VecRestoreArray(), VecGetArrayRead(), VecLockReadPush(), VecLockGet()
3900: @*/
3901: PetscErrorCode VecLockReadPop(Vec x)
3902: {
3905:   x->lock--;
3906:   if (x->lock < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Vector has been unlocked from read-only access too many times");
3907:   return(0);
3908: }

3910: /*@C
3911:    VecLockWriteSet_Private  - Lock or unlock a vector for exclusive read/write access

3913:    Logically Collective on Vec

3915:    Input Parameter:
3916: +  x   - the vector
3917: -  flg - PETSC_TRUE to lock the vector for writing; PETSC_FALSE to unlock it.

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


3926:        VecGetArray(x,&xdata); // begin phase
3927:        VecLockWriteSet_Private(v,PETSC_TRUE);

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

3931:        VecRestoreArray(x,&vdata); // end phase
3932:        VecLockWriteSet_Private(v,PETSC_FALSE);

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

3937:    Level: beginner

3939: .seealso: VecRestoreArray(), VecGetArrayRead(), VecLockReadPush(), VecLockReadPop(), VecLockGet()
3940: @*/
3941: PetscErrorCode VecLockWriteSet_Private(Vec x,PetscBool flg)
3942: {
3945:   if (flg) {
3946:     if (x->lock > 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Vector is already locked for read-only access but you want to write it");
3947:     else if (x->lock < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Vector is already locked for exclusive write access but you want to write it");
3948:     else x->lock = -1;
3949:   } else {
3950:     if (x->lock != -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Vector is not locked for exclusive write access but you want to unlock it from that");
3951:     x->lock = 0;
3952:   }
3953:   return(0);
3954: }

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

3959:    Level: deprecated

3961: .seealso: VecLockReadPush()
3962: @*/
3963: PetscErrorCode VecLockPush(Vec x)
3964: {
3967:   VecLockReadPush(x);
3968:   return(0);
3969: }

3971: /*@
3972:    VecLockPop  - Pops a read-only lock from a vector

3974:    Level: deprecated

3976: .seealso: VecLockReadPop()
3977: @*/
3978: PetscErrorCode VecLockPop(Vec x)
3979: {
3982:   VecLockReadPop(x);
3983:   return(0);
3984: }

3986: #endif