Actual source code: vecimpl.h

  1: #pragma once

  3: /*
  4:   This private file should not be included in users' code.  Defines the fields shared by all
  5:   vector implementations.
  6: */

  8: #include <petscvec.h>
  9: #include <petsc/private/petscimpl.h>

 11: PETSC_INTERN PetscBool   VecRegisterAllCalled;
 12: PETSC_EXTERN MPI_Op      MPIU_MAXLOC;
 13: PETSC_EXTERN MPI_Op      MPIU_MINLOC;
 14: PETSC_INTERN MPI_Op      VecStrideMax_Op;
 15: PETSC_INTERN MPI_Op      VecStrideMin_Op;
 16: PETSC_INTERN void MPIAPI VecStrideMax_Local(void *, void *, PetscMPIInt *, MPI_Datatype *);
 17: PETSC_INTERN void MPIAPI VecStrideMin_Local(void *, void *, PetscMPIInt *, MPI_Datatype *);

 19: /* ----------------------------------------------------------------------------*/

 21: typedef struct _VecOps *VecOps;
 22: struct _VecOps {
 23:   PetscErrorCode (*duplicate)(Vec, Vec *);                                          /* get single vector */
 24:   PetscErrorCode (*duplicatevecs)(Vec, PetscInt, Vec **);                           /* get array of vectors */
 25:   PetscErrorCode (*destroyvecs)(PetscInt, Vec[]);                                   /* free array of vectors */
 26:   PetscErrorCode (*dot)(Vec, Vec, PetscScalar *);                                   /* z = x^H * y */
 27:   PetscErrorCode (*mdot)(Vec, PetscInt, const Vec[], PetscScalar *);                /* z[j] = x dot y[j] */
 28:   PetscErrorCode (*norm)(Vec, NormType, PetscReal *);                               /* z = sqrt(x^H * x) */
 29:   PetscErrorCode (*tdot)(Vec, Vec, PetscScalar *);                                  /* x'*y */
 30:   PetscErrorCode (*mtdot)(Vec, PetscInt, const Vec[], PetscScalar *);               /* z[j] = x dot y[j] */
 31:   PetscErrorCode (*scale)(Vec, PetscScalar);                                        /* x = alpha * x   */
 32:   PetscErrorCode (*copy)(Vec, Vec);                                                 /* y = x */
 33:   PetscErrorCode (*set)(Vec, PetscScalar);                                          /* y = alpha  */
 34:   PetscErrorCode (*swap)(Vec, Vec);                                                 /* exchange x and y */
 35:   PetscErrorCode (*axpy)(Vec, PetscScalar, Vec);                                    /* y = y + alpha * x */
 36:   PetscErrorCode (*axpby)(Vec, PetscScalar, PetscScalar, Vec);                      /* y = alpha * x + beta * y*/
 37:   PetscErrorCode (*maxpy)(Vec, PetscInt, const PetscScalar *, Vec *);               /* y = y + alpha[j] x[j] */
 38:   PetscErrorCode (*aypx)(Vec, PetscScalar, Vec);                                    /* y = x + alpha * y */
 39:   PetscErrorCode (*waxpy)(Vec, PetscScalar, Vec, Vec);                              /* w = y + alpha * x */
 40:   PetscErrorCode (*axpbypcz)(Vec, PetscScalar, PetscScalar, PetscScalar, Vec, Vec); /* z = alpha * x + beta *y + gamma *z*/
 41:   PetscErrorCode (*pointwisemult)(Vec, Vec, Vec);                                   /* w = x .* y */
 42:   PetscErrorCode (*pointwisedivide)(Vec, Vec, Vec);                                 /* w = x ./ y */
 43:   PetscErrorCode (*setvalues)(Vec, PetscInt, const PetscInt[], const PetscScalar[], InsertMode);
 44:   PetscErrorCode (*assemblybegin)(Vec);            /* start global assembly */
 45:   PetscErrorCode (*assemblyend)(Vec);              /* end global assembly */
 46:   PetscErrorCode (*getarray)(Vec, PetscScalar **); /* get data array */
 47:   PetscErrorCode (*getsize)(Vec, PetscInt *);
 48:   PetscErrorCode (*getlocalsize)(Vec, PetscInt *);
 49:   PetscErrorCode (*restorearray)(Vec, PetscScalar **); /* restore data array */
 50:   PetscErrorCode (*max)(Vec, PetscInt *, PetscReal *); /* z = max(x); idx=index of max(x) */
 51:   PetscErrorCode (*min)(Vec, PetscInt *, PetscReal *); /* z = min(x); idx=index of min(x) */
 52:   PetscErrorCode (*setrandom)(Vec, PetscRandom);       /* set y[j] = random numbers */
 53:   PetscErrorCode (*setoption)(Vec, VecOption, PetscBool);
 54:   PetscErrorCode (*setvaluesblocked)(Vec, PetscInt, const PetscInt[], const PetscScalar[], InsertMode);
 55:   PetscErrorCode (*destroy)(Vec);
 56:   PetscErrorCode (*view)(Vec, PetscViewer);
 57:   PetscErrorCode (*placearray)(Vec, const PetscScalar *);   /* place data array */
 58:   PetscErrorCode (*replacearray)(Vec, const PetscScalar *); /* replace data array */
 59:   PetscErrorCode (*dot_local)(Vec, Vec, PetscScalar *);
 60:   PetscErrorCode (*tdot_local)(Vec, Vec, PetscScalar *);
 61:   PetscErrorCode (*norm_local)(Vec, NormType, PetscReal *);
 62:   PetscErrorCode (*mdot_local)(Vec, PetscInt, const Vec[], PetscScalar *);
 63:   PetscErrorCode (*mtdot_local)(Vec, PetscInt, const Vec[], PetscScalar *);
 64:   PetscErrorCode (*load)(Vec, PetscViewer);
 65:   PetscErrorCode (*reciprocal)(Vec);
 66:   PetscErrorCode (*conjugate)(Vec);
 67:   PetscErrorCode (*setlocaltoglobalmapping)(Vec, ISLocalToGlobalMapping);
 68:   PetscErrorCode (*getlocaltoglobalmapping)(Vec, ISLocalToGlobalMapping *);
 69:   PetscErrorCode (*resetarray)(Vec); /* vector points to its original array, i.e. undoes any VecPlaceArray() */
 70:   PetscErrorCode (*setfromoptions)(Vec, PetscOptionItems);
 71:   PetscErrorCode (*maxpointwisedivide)(Vec, Vec, PetscReal *); /* m = max abs(x ./ y) */
 72:   PetscErrorCode (*pointwisemax)(Vec, Vec, Vec);
 73:   PetscErrorCode (*pointwisemaxabs)(Vec, Vec, Vec);
 74:   PetscErrorCode (*pointwisemin)(Vec, Vec, Vec);
 75:   PetscErrorCode (*getvalues)(Vec, PetscInt, const PetscInt[], PetscScalar[]);
 76:   PetscErrorCode (*sqrt)(Vec);
 77:   PetscErrorCode (*abs)(Vec);
 78:   PetscErrorCode (*exp)(Vec);
 79:   PetscErrorCode (*log)(Vec);
 80:   PetscErrorCode (*shift)(Vec, PetscScalar);
 81:   PetscErrorCode (*create)(Vec);
 82:   PetscErrorCode (*stridegather)(Vec, PetscInt, Vec, InsertMode);
 83:   PetscErrorCode (*stridescatter)(Vec, PetscInt, Vec, InsertMode);
 84:   PetscErrorCode (*dotnorm2)(Vec, Vec, PetscScalar *, PetscScalar *);
 85:   PetscErrorCode (*getsubvector)(Vec, IS, Vec *);
 86:   PetscErrorCode (*restoresubvector)(Vec, IS, Vec *);
 87:   PetscErrorCode (*getarrayread)(Vec, const PetscScalar **);
 88:   PetscErrorCode (*restorearrayread)(Vec, const PetscScalar **);
 89:   PetscErrorCode (*stridesubsetgather)(Vec, PetscInt, const PetscInt[], const PetscInt[], Vec, InsertMode);
 90:   PetscErrorCode (*stridesubsetscatter)(Vec, PetscInt, const PetscInt[], const PetscInt[], Vec, InsertMode);
 91:   PetscErrorCode (*viewnative)(Vec, PetscViewer);
 92:   PetscErrorCode (*loadnative)(Vec, PetscViewer);
 93:   PetscErrorCode (*createlocalvector)(Vec, Vec *);
 94:   PetscErrorCode (*getlocalvector)(Vec, Vec);
 95:   PetscErrorCode (*restorelocalvector)(Vec, Vec);
 96:   PetscErrorCode (*getlocalvectorread)(Vec, Vec);
 97:   PetscErrorCode (*restorelocalvectorread)(Vec, Vec);
 98:   PetscErrorCode (*bindtocpu)(Vec, PetscBool);
 99:   PetscErrorCode (*getarraywrite)(Vec, PetscScalar **);
100:   PetscErrorCode (*restorearraywrite)(Vec, PetscScalar **);
101:   PetscErrorCode (*getarrayandmemtype)(Vec, PetscScalar **, PetscMemType *);
102:   PetscErrorCode (*restorearrayandmemtype)(Vec, PetscScalar **);
103:   PetscErrorCode (*getarrayreadandmemtype)(Vec, const PetscScalar **, PetscMemType *);
104:   PetscErrorCode (*restorearrayreadandmemtype)(Vec, const PetscScalar **);
105:   PetscErrorCode (*getarraywriteandmemtype)(Vec, PetscScalar **, PetscMemType *);
106:   PetscErrorCode (*restorearraywriteandmemtype)(Vec, PetscScalar **, PetscMemType *);
107:   PetscErrorCode (*concatenate)(PetscInt, const Vec[], Vec *, IS *[]);
108:   PetscErrorCode (*sum)(Vec, PetscScalar *);
109:   PetscErrorCode (*setpreallocationcoo)(Vec, PetscCount, const PetscInt[]);
110:   PetscErrorCode (*setvaluescoo)(Vec, const PetscScalar[], InsertMode);
111:   PetscErrorCode (*errorwnorm)(Vec, Vec, Vec, NormType, PetscReal, Vec, PetscReal, Vec, PetscReal, PetscReal *, PetscInt *, PetscReal *, PetscInt *, PetscReal *, PetscInt *);
112:   PetscErrorCode (*maxpby)(Vec, PetscInt, const PetscScalar *, PetscScalar, Vec *); /* y = beta y + alpha[j] x[j] */
113:   PetscErrorCode (*setstdbasis)(Vec, PetscInt);
114:   PetscErrorCode (*getarrayandmemtypeasync)(Vec, PetscMemoryAccessMode, PetscScalar **, PetscMemType *);
115: };

117: #if defined(offsetof) && (defined(__cplusplus) || (PETSC_C_VERSION >= 23))
118: static_assert(offsetof(struct _VecOps, duplicate) == sizeof(PetscErrorCodeFn *) * VECOP_DUPLICATE, "");
119: static_assert(offsetof(struct _VecOps, set) == sizeof(PetscErrorCodeFn *) * VECOP_SET, "");
120: static_assert(offsetof(struct _VecOps, view) == sizeof(PetscErrorCodeFn *) * VECOP_VIEW, "");
121: static_assert(offsetof(struct _VecOps, load) == sizeof(PetscErrorCodeFn *) * VECOP_LOAD, "");
122: static_assert(offsetof(struct _VecOps, viewnative) == sizeof(PetscErrorCodeFn *) * VECOP_VIEWNATIVE, "");
123: static_assert(offsetof(struct _VecOps, loadnative) == sizeof(PetscErrorCodeFn *) * VECOP_LOADNATIVE, "");
124: #endif

126: /*
127:     The stash is used to temporarily store inserted vec values that
128:   belong to another processor. During the assembly phase the stashed
129:   values are moved to the correct processor and
130: */

132: typedef struct {
133:   PetscInt     nmax;     /* maximum stash size */
134:   PetscInt     umax;     /* max stash size user wants */
135:   PetscInt     oldnmax;  /* the nmax value used previously */
136:   PetscInt     n;        /* stash size */
137:   PetscInt     bs;       /* block size of the stash */
138:   PetscInt     reallocs; /* preserve the no of mallocs invoked */
139:   PetscInt    *idx;      /* global row numbers in stash */
140:   PetscScalar *array;    /* array to hold stashed values */
141:   /* The following variables are used for communication */
142:   MPI_Comm     comm;
143:   PetscMPIInt  size, rank;
144:   PetscMPIInt  tag1, tag2;
145:   MPI_Request *send_waits;        /* array of send requests */
146:   MPI_Request *recv_waits;        /* array of receive requests */
147:   MPI_Status  *send_status;       /* array of send status */
148:   PetscMPIInt  nsends, nrecvs;    /* numbers of sends and receives */
149:   PetscScalar *svalues, *rvalues; /* sending and receiving data */
150:   PetscInt    *sindices, *rindices;
151:   PetscInt     rmax;       /* maximum message length */
152:   PetscInt    *nprocs;     /* tmp data used both during scatterbegin and end */
153:   PetscMPIInt  nprocessed; /* number of messages already processed */
154:   PetscBool    donotstash;
155:   PetscBool    ignorenegidx; /* ignore negative indices passed into VecSetValues/VetGetValues */
156:   InsertMode   insertmode;
157:   PetscInt    *bowners;
158: } VecStash;

160: struct _p_Vec {
161:   PETSCHEADER(struct _VecOps);
162:   PetscLayout map;
163:   void       *data; /* implementation-specific data */
164:   PetscBool   array_gotten;
165:   VecStash    stash, bstash; /* used for storing off-proc values during assembly */
166:   PetscBool   petscnative;   /* means the ->data starts with VECHEADER and can use VecGetArrayFast()*/
167:   PetscInt    lock;          /* lock state. vector can be free (=0), locked for read (>0) or locked for write(<0) */
168: #if PetscDefined(USE_DEBUG)
169:   PetscStack lockstack; /* the file,func,line of where locks are added */
170: #endif
171:   PetscOffloadMask offloadmask; /* a mask which indicates where the valid vector data is (GPU, CPU or both) */
172: #if PetscDefined(HAVE_DEVICE)
173:   void     *spptr; /* this is the special pointer to the array on the GPU */
174:   PetscBool boundtocpu;
175:   PetscBool bindingpropagates;
176:   size_t    minimum_bytes_pinned_memory; /* minimum data size in bytes for which pinned memory will be allocated */
177:   PetscBool pinned_memory;               /* PETSC_TRUE if the current host allocation has been made from pinned memory. */
178: #endif
179:   char *defaultrandtype;
180: };

182: PETSC_EXTERN PetscLogEvent VEC_SetRandom;
183: PETSC_EXTERN PetscLogEvent VEC_View;
184: PETSC_EXTERN PetscLogEvent VEC_Max;
185: PETSC_EXTERN PetscLogEvent VEC_Min;
186: PETSC_EXTERN PetscLogEvent VEC_Dot;
187: PETSC_EXTERN PetscLogEvent VEC_MDot;
188: PETSC_EXTERN PetscLogEvent VEC_TDot;
189: PETSC_EXTERN PetscLogEvent VEC_MTDot;
190: PETSC_EXTERN PetscLogEvent VEC_Norm;
191: PETSC_EXTERN PetscLogEvent VEC_Normalize;
192: PETSC_EXTERN PetscLogEvent VEC_Scale;
193: PETSC_EXTERN PetscLogEvent VEC_Shift;
194: PETSC_EXTERN PetscLogEvent VEC_Copy;
195: PETSC_EXTERN PetscLogEvent VEC_Set;
196: PETSC_EXTERN PetscLogEvent VEC_AXPY;
197: PETSC_EXTERN PetscLogEvent VEC_AYPX;
198: PETSC_EXTERN PetscLogEvent VEC_WAXPY;
199: PETSC_EXTERN PetscLogEvent VEC_MAXPY;
200: PETSC_EXTERN PetscLogEvent VEC_AssemblyEnd;
201: PETSC_EXTERN PetscLogEvent VEC_PointwiseMult;
202: PETSC_EXTERN PetscLogEvent VEC_PointwiseDivide;
203: PETSC_EXTERN PetscLogEvent VEC_Reciprocal;
204: PETSC_EXTERN PetscLogEvent VEC_SetValues;
205: PETSC_EXTERN PetscLogEvent VEC_SetPreallocateCOO;
206: PETSC_EXTERN PetscLogEvent VEC_SetValuesCOO;
207: PETSC_EXTERN PetscLogEvent VEC_Load;
208: PETSC_EXTERN PetscLogEvent VEC_ScatterBegin;
209: PETSC_EXTERN PetscLogEvent VEC_ScatterEnd;
210: PETSC_EXTERN PetscLogEvent VEC_ReduceArithmetic;
211: PETSC_EXTERN PetscLogEvent VEC_ReduceCommunication;
212: PETSC_EXTERN PetscLogEvent VEC_ReduceBegin;
213: PETSC_EXTERN PetscLogEvent VEC_ReduceEnd;
214: PETSC_EXTERN PetscLogEvent VEC_Swap;
215: PETSC_EXTERN PetscLogEvent VEC_AssemblyBegin;
216: PETSC_EXTERN PetscLogEvent VEC_DotNorm2;
217: PETSC_EXTERN PetscLogEvent VEC_AXPBYPCZ;
218: PETSC_EXTERN PetscLogEvent VEC_Ops;
219: PETSC_EXTERN PetscLogEvent VEC_ViennaCLCopyToGPU;
220: PETSC_EXTERN PetscLogEvent VEC_ViennaCLCopyFromGPU;
221: PETSC_EXTERN PetscLogEvent VEC_CUDACopyToGPU;
222: PETSC_EXTERN PetscLogEvent VEC_CUDACopyFromGPU;
223: PETSC_EXTERN PetscLogEvent VEC_HIPCopyToGPU;
224: PETSC_EXTERN PetscLogEvent VEC_HIPCopyFromGPU;

226: PETSC_SINGLE_LIBRARY_INTERN PetscErrorCode VecView_Seq(Vec, PetscViewer);
227: #if PetscDefined(HAVE_VIENNACL)
228: PETSC_EXTERN PetscErrorCode VecViennaCLAllocateCheckHost(Vec v);
229: PETSC_EXTERN PetscErrorCode VecViennaCLCopyFromGPU(Vec v);
230: #endif

232: /*
233:      Common header shared by array based vectors,
234:    currently Vec_Seq and Vec_MPI
235: */
236: #define VECHEADER \
237:   PetscScalar *array; \
238:   PetscScalar *array_allocated; /* if the array was allocated by PETSc this is its pointer */ \
239:   PetscScalar *unplacedarray;   /* if one called VecPlaceArray(), this is where it stashed the original */

241: /* Get Root type of vector. e.g. VECSEQ -> VECSTANDARD, VECMPICUDA -> VECCUDA */
242: PETSC_EXTERN PetscErrorCode VecGetRootType_Private(Vec, VecType *);

244: /* Default obtain and release vectors; can be used by any implementation */
245: PETSC_INTERN PetscErrorCode VecDuplicateVecs_Default(Vec, PetscInt, Vec *[]);
246: PETSC_INTERN PetscErrorCode VecDestroyVecs_Default(PetscInt, Vec[]);
247: PETSC_INTERN PetscErrorCode VecView_Binary(Vec, PetscViewer);

249: PETSC_SINGLE_LIBRARY_INTERN PetscErrorCode VecLoad_Default(Vec, PetscViewer);

251: PETSC_INTERN PetscInt NormIds[4]; /* map from NormType to IDs used to cache/retrieve values of norms, 1_AND_2 is excluded */

253: PETSC_INTERN PetscErrorCode VecStashCreate_Private(MPI_Comm, PetscInt, VecStash *);
254: PETSC_INTERN PetscErrorCode VecStashDestroy_Private(VecStash *);
255: PETSC_INTERN PetscErrorCode VecStashScatterEnd_Private(VecStash *);
256: PETSC_INTERN PetscErrorCode VecStashSetInitialSize_Private(VecStash *, PetscInt);
257: PETSC_INTERN PetscErrorCode VecStashGetInfo_Private(VecStash *, PetscInt *, PetscInt *);
258: PETSC_INTERN PetscErrorCode VecStashScatterBegin_Private(VecStash *, const PetscInt *);
259: PETSC_INTERN PetscErrorCode VecStashScatterGetMesg_Private(VecStash *, PetscMPIInt *, PetscInt **, PetscScalar **, PetscInt *);
260: PETSC_INTERN PetscErrorCode VecStashSortCompress_Private(VecStash *);
261: PETSC_INTERN PetscErrorCode VecStashGetOwnerList_Private(VecStash *, PetscLayout, PetscMPIInt *, PetscMPIInt **);

263: PETSC_INTERN PetscErrorCode VecStashExpand_Private(VecStash *, PetscInt);

265: /*
266:   VecStashValue_Private - inserts a single value into the stash.

268:   Input Parameters:
269:   stash  - the stash
270:   idx    - the global of the inserted value
271:   values - the value inserted
272: */
273: static inline PetscErrorCode VecStashValue_Private(VecStash *stash, PetscInt row, PetscScalar value)
274: {
275:   /* Check and see if we have sufficient memory */
276:   PetscFunctionBegin;
277:   if ((stash->n + 1) > stash->nmax) PetscCall(VecStashExpand_Private(stash, 1));
278:   stash->idx[stash->n]   = row;
279:   stash->array[stash->n] = value;
280:   stash->n++;
281:   PetscFunctionReturn(PETSC_SUCCESS);
282: }

284: /*
285:   VecStashValuesBlocked_Private - inserts 1 block of values into the stash.

287:   Input Parameters:
288:   stash  - the stash
289:   idx    - the global block index
290:   values - the values inserted
291: */
292: static inline PetscErrorCode VecStashValuesBlocked_Private(VecStash *stash, PetscInt row, PetscScalar *values)
293: {
294:   PetscInt     stash_bs = stash->bs;
295:   PetscScalar *array;

297:   PetscFunctionBegin;
298:   if ((stash->n + 1) > stash->nmax) PetscCall(VecStashExpand_Private(stash, 1));
299:   array                = stash->array + stash_bs * stash->n;
300:   stash->idx[stash->n] = row;
301:   if (values) PetscCall(PetscArraycpy(array, values, stash_bs));
302:   else PetscCall(PetscArrayzero(array, stash_bs));
303:   stash->n++;
304:   PetscFunctionReturn(PETSC_SUCCESS);
305: }

307: PETSC_INTERN PetscErrorCode VecStrideGather_Default(Vec, PetscInt, Vec, InsertMode);
308: PETSC_INTERN PetscErrorCode VecStrideScatter_Default(Vec, PetscInt, Vec, InsertMode);
309: PETSC_INTERN PetscErrorCode VecStrideSubSetGather_Default(Vec, PetscInt, const PetscInt[], const PetscInt[], Vec, InsertMode);
310: PETSC_INTERN PetscErrorCode VecStrideSubSetScatter_Default(Vec, PetscInt, const PetscInt[], const PetscInt[], Vec, InsertMode);

312: PETSC_INTERN PetscErrorCode VecReciprocal_Default(Vec);
313: #if PetscDefined(HAVE_MATLAB)
314: PETSC_EXTERN PetscErrorCode VecMatlabEnginePut_Default(PetscObject, void *);
315: PETSC_EXTERN PetscErrorCode VecMatlabEngineGet_Default(PetscObject, void *);
316: #endif

318: PETSC_SINGLE_LIBRARY_INTERN PetscErrorCode PetscSectionGetField_Internal(PetscSection, PetscSection, Vec, PetscInt, PetscInt, PetscInt, IS *, Vec *);
319: PETSC_SINGLE_LIBRARY_INTERN PetscErrorCode PetscSectionRestoreField_Internal(PetscSection, PetscSection, Vec, PetscInt, PetscInt, PetscInt, IS *, Vec *);

321: #define VecCheckSameLocalSize(x, ar1, y, ar2) \
322:   do { \
323:     PetscCheck((x)->map->n == (y)->map->n, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Incompatible vector local lengths parameter # %d local size %" PetscInt_FMT " != parameter # %d local size %" PetscInt_FMT, ar1, (x)->map->n, ar2, (y)->map->n); \
324:   } while (0)

326: #define VecCheckSameSize(x, ar1, y, ar2) \
327:   do { \
328:     PetscCheck((x)->map->N == (y)->map->N, PetscObjectComm((PetscObject)(x)), PETSC_ERR_ARG_INCOMP, "Incompatible vector global lengths parameter # %d global size %" PetscInt_FMT " != parameter # %d global size %" PetscInt_FMT, ar1, (x)->map->N, ar2, \
329:                (y)->map->N); \
330:     VecCheckSameLocalSize(x, ar1, y, ar2); \
331:   } while (0)

333: #define VecCheckLocalSize(x, ar1, n) \
334:   do { \
335:     PetscCheck((x)->map->n == (n), PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Incorrect vector local size: parameter # %d local size %" PetscInt_FMT " != %" PetscInt_FMT, ar1, (x)->map->n, n); \
336:   } while (0)

338: #define VecCheckSize(x, ar1, n, N) \
339:   do { \
340:     PetscCheck((x)->map->N == (N), PetscObjectComm((PetscObject)(x)), PETSC_ERR_ARG_INCOMP, "Incorrect vector global size: parameter # %d global size %" PetscInt_FMT " != %" PetscInt_FMT, ar1, (x)->map->N, N); \
341:     VecCheckLocalSize(x, ar1, n); \
342:   } while (0)

344: typedef struct _VecTaggerOps *VecTaggerOps;
345: struct _VecTaggerOps {
346:   PetscErrorCode (*create)(VecTagger);
347:   PetscErrorCode (*destroy)(VecTagger);
348:   PetscErrorCode (*setfromoptions)(VecTagger, PetscOptionItems);
349:   PetscErrorCode (*setup)(VecTagger);
350:   PetscErrorCode (*view)(VecTagger, PetscViewer);
351:   PetscErrorCode (*computeboxes)(VecTagger, Vec, PetscInt *, VecTaggerBox **, PetscBool *);
352:   PetscErrorCode (*computeis)(VecTagger, Vec, IS *, PetscBool *);
353: };
354: struct _p_VecTagger {
355:   PETSCHEADER(struct _VecTaggerOps);
356:   void     *data;
357:   PetscInt  blocksize;
358:   PetscBool invert;
359:   PetscBool setupcalled;
360: };

362: PETSC_INTERN PetscBool      VecTaggerRegisterAllCalled;
363: PETSC_INTERN PetscErrorCode VecTaggerComputeIS_FromBoxes(VecTagger, Vec, IS *, PetscBool *);
364: PETSC_INTERN PetscMPIInt    Petsc_Reduction_keyval;

366: PETSC_INTERN PetscInt                      VecGetSubVectorSavedStateId;
367: PETSC_INTERN PetscErrorCode                VecGetSubVectorContiguityAndBS_Private(Vec, IS, PetscBool *, PetscInt *, PetscInt *);
368: PETSC_INTERN PetscErrorCode                VecGetSubVectorThroughVecScatter_Private(Vec, IS, PetscInt, Vec *);
369: PETSC_SINGLE_LIBRARY_INTERN PetscErrorCode VecScatterSynchronizeKokkos_Private(VecScatter, Vec, Vec, PetscBool);

371: #if PetscDefined(HAVE_CUDA)
372: PETSC_INTERN PetscErrorCode VecCreate_CUDA(Vec);
373: PETSC_INTERN PetscErrorCode VecCreate_SeqCUDA(Vec);
374: PETSC_INTERN PetscErrorCode VecCreate_MPICUDA(Vec);
375: PETSC_INTERN PetscErrorCode VecCUDAGetArrays_Private(Vec, const PetscScalar **, const PetscScalar **, PetscOffloadMask *);
376: PETSC_INTERN PetscErrorCode VecConvert_Seq_SeqCUDA_inplace(Vec);
377: PETSC_INTERN PetscErrorCode VecConvert_MPI_MPICUDA_inplace(Vec);
378: #endif

380: #if PetscDefined(HAVE_HIP)
381: PETSC_INTERN PetscErrorCode VecCreate_HIP(Vec);
382: PETSC_INTERN PetscErrorCode VecCreate_SeqHIP(Vec);
383: PETSC_INTERN PetscErrorCode VecCreate_MPIHIP(Vec);
384: PETSC_INTERN PetscErrorCode VecHIPGetArrays_Private(Vec, const PetscScalar **, const PetscScalar **, PetscOffloadMask *);
385: PETSC_INTERN PetscErrorCode VecConvert_Seq_SeqHIP_inplace(Vec);
386: PETSC_INTERN PetscErrorCode VecConvert_MPI_MPIHIP_inplace(Vec);
387: #endif

389: #if PetscDefined(HAVE_KOKKOS)
390: PETSC_INTERN PetscErrorCode VecCreateMPIKokkosWithArrays_Private(MPI_Comm, PetscInt, PetscInt, PetscInt, const PetscScalar *, const PetscScalar *, Vec *);
391: PETSC_INTERN PetscErrorCode VecConvert_Seq_SeqKokkos_inplace(Vec, PetscScalar *);
392: PETSC_INTERN PetscErrorCode VecConvert_MPI_MPIKokkos_inplace(Vec);
393: #endif

395: PETSC_SINGLE_LIBRARY_INTERN PetscErrorCode VecCreateWithLayout_Private(PetscLayout, Vec *);
396: PETSC_SINGLE_LIBRARY_INTERN PetscErrorCode VecCreateSeqWithLayoutAndArray_Private(PetscLayout, const PetscScalar *, Vec *);
397: PETSC_SINGLE_LIBRARY_INTERN PetscErrorCode VecCreateMPIWithLayoutAndArray_Private(PetscLayout, const PetscScalar *, Vec *);

399: /* std::upper_bound(): Given a sorted array, return index of the first element in range [first,last) whose value
400:    is greater than value, or last if there is no such element.
401: */
402: static inline PetscErrorCode PetscSortedIntUpperBound(const PetscInt *array, PetscCount first, PetscCount last, PetscInt value, PetscCount *upper)
403: {
404:   PetscCount count = last - first;

406:   PetscFunctionBegin;
407:   while (count > 0) {
408:     const PetscCount step = count / 2;
409:     PetscCount       it   = first + step;

411:     if (value < array[it]) {
412:       count = step;
413:     } else {
414:       first = it + 1;
415:       count -= step + 1;
416:     }
417:   }
418:   *upper = first;
419:   PetscFunctionReturn(PETSC_SUCCESS);
420: }

422: #define VEC_ASYNC_FN_NAME(Base) "Vec" Base "Async_Private_C"
423: #define VecAsyncFnName(Base)    VEC_##Base##_ASYNC_FN_NAME

425: #define VEC_Abs_ASYNC_FN_NAME             VEC_ASYNC_FN_NAME("Abs")
426: #define VEC_AXPBY_ASYNC_FN_NAME           VEC_ASYNC_FN_NAME("AXPBY")
427: #define VEC_AXPBYPCZ_ASYNC_FN_NAME        VEC_ASYNC_FN_NAME("AXPBYPCZ")
428: #define VEC_AXPY_ASYNC_FN_NAME            VEC_ASYNC_FN_NAME("AXPY")
429: #define VEC_AYPX_ASYNC_FN_NAME            VEC_ASYNC_FN_NAME("AYPX")
430: #define VEC_Conjugate_ASYNC_FN_NAME       VEC_ASYNC_FN_NAME("Conjugate")
431: #define VEC_Copy_ASYNC_FN_NAME            VEC_ASYNC_FN_NAME("Copy")
432: #define VEC_Exp_ASYNC_FN_NAME             VEC_ASYNC_FN_NAME("Exp")
433: #define VEC_Log_ASYNC_FN_NAME             VEC_ASYNC_FN_NAME("Log")
434: #define VEC_MAXPY_ASYNC_FN_NAME           VEC_ASYNC_FN_NAME("MAXPY")
435: #define VEC_PointwiseDivide_ASYNC_FN_NAME VEC_ASYNC_FN_NAME("PointwiseDivide")
436: #define VEC_PointwiseMax_ASYNC_FN_NAME    VEC_ASYNC_FN_NAME("PointwiseMax")
437: #define VEC_PointwiseMaxAbs_ASYNC_FN_NAME VEC_ASYNC_FN_NAME("PointwiseMaxAbs")
438: #define VEC_PointwiseMin_ASYNC_FN_NAME    VEC_ASYNC_FN_NAME("PointwiseMin")
439: #define VEC_PointwiseMult_ASYNC_FN_NAME   VEC_ASYNC_FN_NAME("PointwiseMult")
440: #define VEC_PointwiseSign_ASYNC_FN_NAME   VEC_ASYNC_FN_NAME("PointwiseSign")
441: #define VEC_Reciprocal_ASYNC_FN_NAME      VEC_ASYNC_FN_NAME("Reciprocal")
442: #define VEC_Scale_ASYNC_FN_NAME           VEC_ASYNC_FN_NAME("Scale")
443: #define VEC_Set_ASYNC_FN_NAME             VEC_ASYNC_FN_NAME("Set")
444: #define VEC_Shift_ASYNC_FN_NAME           VEC_ASYNC_FN_NAME("Shift")
445: #define VEC_SqrtAbs_ASYNC_FN_NAME         VEC_ASYNC_FN_NAME("SqrtAbs")
446: #define VEC_Swap_ASYNC_FN_NAME            VEC_ASYNC_FN_NAME("Swap")
447: #define VEC_WAXPY_ASYNC_FN_NAME           VEC_ASYNC_FN_NAME("WAXPY")
448: #define VEC_SetStdBasis_ASYNC_FN_NAME     VEC_ASYNC_FN_NAME("SetStdBasis")

450: PETSC_INTERN PetscErrorCode VecAbsAsync_Private(Vec, PetscDeviceContext);
451: PETSC_INTERN PetscErrorCode VecAXPBYAsync_Private(Vec, PetscScalar, PetscScalar, Vec, PetscDeviceContext);
452: PETSC_INTERN PetscErrorCode VecAXPBYPCZAsync_Private(Vec, PetscScalar, PetscScalar, PetscScalar, Vec, Vec, PetscDeviceContext);
453: PETSC_INTERN PetscErrorCode VecAXPYAsync_Private(Vec, PetscScalar, Vec, PetscDeviceContext);
454: PETSC_INTERN PetscErrorCode VecAYPXAsync_Private(Vec, PetscScalar, Vec, PetscDeviceContext);
455: PETSC_INTERN PetscErrorCode VecConjugateAsync_Private(Vec, PetscDeviceContext);
456: PETSC_INTERN PetscErrorCode VecCopyAsync_Private(Vec, Vec, PetscDeviceContext);
457: PETSC_INTERN PetscErrorCode VecExpAsync_Private(Vec, PetscDeviceContext);
458: PETSC_INTERN PetscErrorCode VecLogAsync_Private(Vec, PetscDeviceContext);
459: PETSC_INTERN PetscErrorCode VecMAXPYAsync_Private(Vec, PetscInt, const PetscScalar *, Vec[], PetscDeviceContext);
460: PETSC_INTERN PetscErrorCode VecPointwiseDivideAsync_Private(Vec, Vec, Vec, PetscDeviceContext);
461: PETSC_INTERN PetscErrorCode VecPointwiseMaxAsync_Private(Vec, Vec, Vec, PetscDeviceContext);
462: PETSC_INTERN PetscErrorCode VecPointwiseMaxAbsAsync_Private(Vec, Vec, Vec, PetscDeviceContext);
463: PETSC_INTERN PetscErrorCode VecPointwiseMinAsync_Private(Vec, Vec, Vec, PetscDeviceContext);
464: PETSC_INTERN PetscErrorCode VecPointwiseMultAsync_Private(Vec, Vec, Vec, PetscDeviceContext);
465: PETSC_INTERN PetscErrorCode VecPointwiseSignAsync_Private(Vec, Vec, VecSignMode, PetscDeviceContext);
466: PETSC_INTERN PetscErrorCode VecReciprocalAsync_Private(Vec, PetscDeviceContext);
467: PETSC_INTERN PetscErrorCode VecScaleAsync_Private(Vec, PetscScalar, PetscDeviceContext);
468: PETSC_INTERN PetscErrorCode VecSetAsync_Private(Vec, PetscScalar, PetscDeviceContext);
469: PETSC_INTERN PetscErrorCode VecShiftAsync_Private(Vec, PetscScalar, PetscDeviceContext);
470: PETSC_INTERN PetscErrorCode VecSqrtAbsAsync_Private(Vec, PetscDeviceContext);
471: PETSC_INTERN PetscErrorCode VecSwapAsync_Private(Vec, Vec, PetscDeviceContext);
472: PETSC_INTERN PetscErrorCode VecWAXPYAsync_Private(Vec, PetscScalar, Vec, Vec, PetscDeviceContext);

474: #define VecMethodDispatch(v, dctx, async_name, name, async_arg_types, ...) \
475:   do { \
476:     PetscErrorCode(*_8_f) async_arg_types = NULL; \
477:     if (dctx) PetscCall(PetscObjectQueryFunction((PetscObject)(v), async_name, &_8_f)); \
478:     if (_8_f) { \
479:       PetscCall((*_8_f)(v, __VA_ARGS__, dctx)); \
480:     } else { \
481:       PetscUseTypeMethod(v, name, __VA_ARGS__); \
482:     } \
483:   } while (0)

485: // return in lda the vector's local size aligned to  bytes, where lda is an integer pointer
486: #define VecGetLocalSizeAligned(v, alignment, lda) \
487:   do { \
488:     PetscInt     n = (v)->map->n; \
489:     const size_t s = (alignment) / sizeof(PetscScalar); \
490:     *(lda)         = ((n + s - 1) / s) * s; \
491:   } while (0)

493: #define VecSignZeroToZero_Private(y) (((y) > 0) - ((y) < 0))

495: #define VecSignZeroToSignedUnit_Private(y) PetscCopysignReal(1.0, (y))

497: #define VecSignZeroToSignedZero_Private(y) (PetscCopysignReal(1.0, (y)) * ((y) != 0.0))