Actual source code: petsc-vecimpl.h
petsc-3.5.1 2014-07-24
2: /*
3: This private file should not be included in users' code.
4: Defines the fields shared by all vector implementations.
6: */
8: #ifndef __VECIMPL_H
11: #include <petscvec.h>
12: #include <petsc-private/petscimpl.h>
13: #include <petscviewer.h>
16: /* ----------------------------------------------------------------------------*/
18: typedef struct _VecOps *VecOps;
19: struct _VecOps {
20: PetscErrorCode (*duplicate)(Vec,Vec*); /* get single vector */
21: PetscErrorCode (*duplicatevecs)(Vec,PetscInt,Vec**); /* get array of vectors */
22: PetscErrorCode (*destroyvecs)(PetscInt,Vec[]); /* free array of vectors */
23: PetscErrorCode (*dot)(Vec,Vec,PetscScalar*); /* z = x^H * y */
24: PetscErrorCode (*mdot)(Vec,PetscInt,const Vec[],PetscScalar*); /* z[j] = x dot y[j] */
25: PetscErrorCode (*norm)(Vec,NormType,PetscReal*); /* z = sqrt(x^H * x) */
26: PetscErrorCode (*tdot)(Vec,Vec,PetscScalar*); /* x'*y */
27: PetscErrorCode (*mtdot)(Vec,PetscInt,const Vec[],PetscScalar*);/* z[j] = x dot y[j] */
28: PetscErrorCode (*scale)(Vec,PetscScalar); /* x = alpha * x */
29: PetscErrorCode (*copy)(Vec,Vec); /* y = x */
30: PetscErrorCode (*set)(Vec,PetscScalar); /* y = alpha */
31: PetscErrorCode (*swap)(Vec,Vec); /* exchange x and y */
32: PetscErrorCode (*axpy)(Vec,PetscScalar,Vec); /* y = y + alpha * x */
33: PetscErrorCode (*axpby)(Vec,PetscScalar,PetscScalar,Vec); /* y = alpha * x + beta * y*/
34: PetscErrorCode (*maxpy)(Vec,PetscInt,const PetscScalar*,Vec*); /* y = y + alpha[j] x[j] */
35: PetscErrorCode (*aypx)(Vec,PetscScalar,Vec); /* y = x + alpha * y */
36: PetscErrorCode (*waxpy)(Vec,PetscScalar,Vec,Vec); /* w = y + alpha * x */
37: PetscErrorCode (*axpbypcz)(Vec,PetscScalar,PetscScalar,PetscScalar,Vec,Vec); /* z = alpha * x + beta *y + gamma *z*/
38: PetscErrorCode (*pointwisemult)(Vec,Vec,Vec); /* w = x .* y */
39: PetscErrorCode (*pointwisedivide)(Vec,Vec,Vec); /* w = x ./ y */
40: PetscErrorCode (*setvalues)(Vec,PetscInt,const PetscInt[],const PetscScalar[],InsertMode);
41: PetscErrorCode (*assemblybegin)(Vec); /* start global assembly */
42: PetscErrorCode (*assemblyend)(Vec); /* end global assembly */
43: PetscErrorCode (*getarray)(Vec,PetscScalar**); /* get data array */
44: PetscErrorCode (*getsize)(Vec,PetscInt*);
45: PetscErrorCode (*getlocalsize)(Vec,PetscInt*);
46: PetscErrorCode (*restorearray)(Vec,PetscScalar**); /* restore data array */
47: PetscErrorCode (*max)(Vec,PetscInt*,PetscReal*); /* z = max(x); idx=index of max(x) */
48: PetscErrorCode (*min)(Vec,PetscInt*,PetscReal*); /* z = min(x); idx=index of min(x) */
49: PetscErrorCode (*setrandom)(Vec,PetscRandom); /* set y[j] = random numbers */
50: PetscErrorCode (*setoption)(Vec,VecOption,PetscBool );
51: PetscErrorCode (*setvaluesblocked)(Vec,PetscInt,const PetscInt[],const PetscScalar[],InsertMode);
52: PetscErrorCode (*destroy)(Vec);
53: PetscErrorCode (*view)(Vec,PetscViewer);
54: PetscErrorCode (*placearray)(Vec,const PetscScalar*); /* place data array */
55: PetscErrorCode (*replacearray)(Vec,const PetscScalar*); /* replace data array */
56: PetscErrorCode (*dot_local)(Vec,Vec,PetscScalar*);
57: PetscErrorCode (*tdot_local)(Vec,Vec,PetscScalar*);
58: PetscErrorCode (*norm_local)(Vec,NormType,PetscReal*);
59: PetscErrorCode (*mdot_local)(Vec,PetscInt,const Vec[],PetscScalar*);
60: PetscErrorCode (*mtdot_local)(Vec,PetscInt,const Vec[],PetscScalar*);
61: PetscErrorCode (*load)(Vec,PetscViewer);
62: PetscErrorCode (*reciprocal)(Vec);
63: PetscErrorCode (*conjugate)(Vec);
64: PetscErrorCode (*setlocaltoglobalmapping)(Vec,ISLocalToGlobalMapping);
65: PetscErrorCode (*setvalueslocal)(Vec,PetscInt,const PetscInt *,const PetscScalar *,InsertMode);
66: PetscErrorCode (*resetarray)(Vec); /* vector points to its original array, i.e. undoes any VecPlaceArray() */
67: PetscErrorCode (*setfromoptions)(Vec);
68: PetscErrorCode (*maxpointwisedivide)(Vec,Vec,PetscReal*); /* m = max abs(x ./ y) */
69: PetscErrorCode (*pointwisemax)(Vec,Vec,Vec);
70: PetscErrorCode (*pointwisemaxabs)(Vec,Vec,Vec);
71: PetscErrorCode (*pointwisemin)(Vec,Vec,Vec);
72: PetscErrorCode (*getvalues)(Vec,PetscInt,const PetscInt[],PetscScalar[]);
73: PetscErrorCode (*sqrt)(Vec);
74: PetscErrorCode (*abs)(Vec);
75: PetscErrorCode (*exp)(Vec);
76: PetscErrorCode (*log)(Vec);
77: PetscErrorCode (*shift)(Vec);
78: PetscErrorCode (*create)(Vec);
79: PetscErrorCode (*stridegather)(Vec,PetscInt,Vec,InsertMode);
80: PetscErrorCode (*stridescatter)(Vec,PetscInt,Vec,InsertMode);
81: PetscErrorCode (*dotnorm2)(Vec,Vec,PetscScalar*,PetscScalar*);
82: PetscErrorCode (*getsubvector)(Vec,IS,Vec*);
83: PetscErrorCode (*restoresubvector)(Vec,IS,Vec*);
84: PetscErrorCode (*getarrayread)(Vec,const PetscScalar**);
85: PetscErrorCode (*restorearrayread)(Vec,const PetscScalar**);
86: PetscErrorCode (*stridesubsetgather)(Vec,PetscInt,const PetscInt[],const PetscInt[],Vec,InsertMode);
87: PetscErrorCode (*stridesubsetscatter)(Vec,PetscInt,const PetscInt[],const PetscInt[],Vec,InsertMode);
88: };
90: /*
91: The stash is used to temporarily store inserted vec values that
92: belong to another processor. During the assembly phase the stashed
93: values are moved to the correct processor and
94: */
96: typedef struct {
97: PetscInt nmax; /* maximum stash size */
98: PetscInt umax; /* max stash size user wants */
99: PetscInt oldnmax; /* the nmax value used previously */
100: PetscInt n; /* stash size */
101: PetscInt bs; /* block size of the stash */
102: PetscInt reallocs; /* preserve the no of mallocs invoked */
103: PetscInt *idx; /* global row numbers in stash */
104: PetscScalar *array; /* array to hold stashed values */
105: /* The following variables are used for communication */
106: MPI_Comm comm;
107: PetscMPIInt size,rank;
108: PetscMPIInt tag1,tag2;
109: MPI_Request *send_waits; /* array of send requests */
110: MPI_Request *recv_waits; /* array of receive requests */
111: MPI_Status *send_status; /* array of send status */
112: PetscInt nsends,nrecvs; /* numbers of sends and receives */
113: PetscScalar *svalues,*rvalues; /* sending and receiving data */
114: PetscInt *sindices,*rindices;
115: PetscInt rmax; /* maximum message length */
116: PetscInt *nprocs; /* tmp data used both during scatterbegin and end */
117: PetscInt nprocessed; /* number of messages already processed */
118: PetscBool donotstash;
119: PetscBool ignorenegidx; /* ignore negative indices passed into VecSetValues/VetGetValues */
120: InsertMode insertmode;
121: PetscInt *bowners;
122: } VecStash;
124: struct _p_Vec {
125: PETSCHEADER(struct _VecOps);
126: PetscLayout map;
127: void *data; /* implementation-specific data */
128: PetscBool array_gotten;
129: VecStash stash,bstash; /* used for storing off-proc values during assembly */
130: PetscBool petscnative; /* means the ->data starts with VECHEADER and can use VecGetArrayFast()*/
131: #if defined(PETSC_HAVE_CUSP)
132: PetscCUSPFlag valid_GPU_array; /* indicates where the most recently modified vector data is (GPU or CPU) */
133: void *spptr; /* if we're using CUSP, then this is the special pointer to the array on the GPU */
134: #endif
135: #if defined(PETSC_HAVE_VIENNACL)
136: PetscViennaCLFlag valid_GPU_array; /* indicates where the most recently modified vector data is (GPU or CPU) */
137: void *spptr; /* if we're using ViennaCL, then this is the special pointer to the array on the GPU */
138: #endif
139: };
141: PETSC_EXTERN PetscLogEvent VEC_View, VEC_Max, VEC_Min, VEC_DotBarrier, VEC_Dot, VEC_MDotBarrier, VEC_MDot, VEC_TDot, VEC_MTDot;
142: PETSC_EXTERN PetscLogEvent VEC_Norm, VEC_Normalize, VEC_Scale, VEC_Copy, VEC_Set, VEC_AXPY, VEC_AYPX, VEC_WAXPY, VEC_MAXPY;
143: PETSC_EXTERN PetscLogEvent VEC_AssemblyEnd, VEC_PointwiseMult, VEC_SetValues, VEC_Load, VEC_ScatterBarrier, VEC_ScatterBegin, VEC_ScatterEnd;
144: PETSC_EXTERN PetscLogEvent VEC_SetRandom, VEC_ReduceArithmetic, VEC_ReduceBarrier, VEC_ReduceCommunication;
145: PETSC_EXTERN PetscLogEvent VEC_ReduceBegin,VEC_ReduceEnd;
146: PETSC_EXTERN PetscLogEvent VEC_Swap, VEC_AssemblyBegin, VEC_NormBarrier, VEC_DotNormBarrier, VEC_DotNorm, VEC_AXPBYPCZ, VEC_Ops;
147: PETSC_EXTERN PetscLogEvent VEC_CUSPCopyToGPU, VEC_CUSPCopyFromGPU;
148: PETSC_EXTERN PetscLogEvent VEC_CUSPCopyToGPUSome, VEC_CUSPCopyFromGPUSome;
149: PETSC_EXTERN PetscLogEvent VEC_ViennaCLCopyToGPU, VEC_ViennaCLCopyFromGPU;
151: #if defined(PETSC_HAVE_CUSP)
152: PETSC_EXTERN PetscErrorCode VecCUSPAllocateCheckHost(Vec v);
153: PETSC_EXTERN PetscErrorCode VecCUSPCopyFromGPU(Vec v);
154: #endif
156: #if defined(PETSC_HAVE_VIENNACL)
157: PETSC_EXTERN PetscErrorCode VecViennaCLAllocateCheckHost(Vec v);
158: PETSC_EXTERN PetscErrorCode VecViennaCLCopyFromGPU(Vec v);
159: #endif
162: /*
163: Common header shared by array based vectors,
164: currently Vec_Seq and Vec_MPI
165: */
166: #define VECHEADER \
167: PetscScalar *array; \
168: PetscScalar *array_allocated; /* if the array was allocated by PETSc this is its pointer */ \
169: PetscScalar *unplacedarray; /* if one called VecPlaceArray(), this is where it stashed the original */
171: /* Default obtain and release vectors; can be used by any implementation */
172: PETSC_INTERN PetscErrorCode VecDuplicateVecs_Default(Vec,PetscInt,Vec *[]);
173: PETSC_INTERN PetscErrorCode VecDestroyVecs_Default(PetscInt,Vec []);
174: PETSC_INTERN PetscErrorCode VecLoad_Binary(Vec, PetscViewer);
175: PETSC_EXTERN PetscErrorCode VecLoad_Default(Vec, PetscViewer);
177: PETSC_EXTERN PetscInt NormIds[7]; /* map from NormType to IDs used to cache/retreive values of norms */
179: /* --------------------------------------------------------------------*/
180: /* */
181: /* Defines the data structures used in the Vec Scatter operations */
183: typedef enum { VEC_SCATTER_SEQ_GENERAL,VEC_SCATTER_SEQ_STRIDE,
184: VEC_SCATTER_MPI_GENERAL,VEC_SCATTER_MPI_TOALL,
185: VEC_SCATTER_MPI_TOONE} VecScatterType;
187: /*
188: These scatters are for the purely local case.
189: */
190: typedef struct {
191: VecScatterType type;
192: PetscInt n; /* number of components to scatter */
193: PetscInt *vslots; /* locations of components */
194: /*
195: The next three fields are used in parallel scatters, they contain
196: optimization in the special case that the "to" vector and the "from"
197: vector are the same, so one only needs copy components that truly
198: copies instead of just y[idx[i]] = y[jdx[i]] where idx[i] == jdx[i].
199: */
200: PetscBool nonmatching_computed;
201: PetscInt n_nonmatching; /* number of "from"s != "to"s */
202: PetscInt *slots_nonmatching; /* locations of "from"s != "to"s */
203: PetscBool is_copy;
204: PetscInt copy_start; /* local scatter is a copy starting at copy_start */
205: PetscInt copy_length;
206: } VecScatter_Seq_General;
208: typedef struct {
209: VecScatterType type;
210: PetscInt n;
211: PetscInt first;
212: PetscInt step;
213: } VecScatter_Seq_Stride;
215: /*
216: This scatter is for a global vector copied (completely) to each processor (or all to one)
217: */
218: typedef struct {
219: VecScatterType type;
220: PetscMPIInt *count; /* elements of vector on each processor */
221: PetscMPIInt *displx;
222: PetscScalar *work1;
223: PetscScalar *work2;
224: } VecScatter_MPI_ToAll;
226: /*
227: This is the general parallel scatter
228: */
229: typedef struct {
230: VecScatterType type;
231: PetscInt n; /* number of processors to send/receive */
232: PetscInt *starts; /* starting point in indices and values for each proc*/
233: PetscInt *indices; /* list of all components sent or received */
234: PetscMPIInt *procs; /* processors we are communicating with in scatter */
235: MPI_Request *requests,*rev_requests;
236: PetscScalar *values; /* buffer for all sends or receives */
237: VecScatter_Seq_General local; /* any part that happens to be local */
238: MPI_Status *sstatus,*rstatus;
239: PetscBool use_readyreceiver;
240: PetscInt bs;
241: PetscBool sendfirst;
242: PetscBool contiq;
243: /* for MPI_Alltoallv() approach */
244: PetscBool use_alltoallv;
245: PetscMPIInt *counts,*displs;
246: /* for MPI_Alltoallw() approach */
247: PetscBool use_alltoallw;
248: #if defined(PETSC_HAVE_MPI_ALLTOALLW)
249: PetscMPIInt *wcounts,*wdispls;
250: MPI_Datatype *types;
251: #endif
252: PetscBool use_window;
253: #if defined(PETSC_HAVE_MPI_WIN_CREATE)
254: MPI_Win window;
255: PetscInt *winstarts; /* displacements in the processes I am putting to */
256: #endif
257: } VecScatter_MPI_General;
259: struct _p_VecScatter {
260: PETSCHEADER(int);
261: PetscInt to_n,from_n;
262: PetscBool inuse; /* prevents corruption from mixing two scatters */
263: PetscBool beginandendtogether; /* indicates that the scatter begin and end function are called together, VecScatterEnd()
264: is then treated as a nop */
265: PetscBool packtogether; /* packs all the messages before sending, same with receive */
266: PetscBool reproduce; /* always receive the ghost points in the same order of processes */
267: PetscErrorCode (*begin)(VecScatter,Vec,Vec,InsertMode,ScatterMode);
268: PetscErrorCode (*end)(VecScatter,Vec,Vec,InsertMode,ScatterMode);
269: PetscErrorCode (*copy)(VecScatter,VecScatter);
270: PetscErrorCode (*destroy)(VecScatter);
271: PetscErrorCode (*view)(VecScatter,PetscViewer);
272: void *fromdata,*todata;
273: void *spptr;
274: };
276: PETSC_INTERN PetscErrorCode VecStashCreate_Private(MPI_Comm,PetscInt,VecStash*);
277: PETSC_INTERN PetscErrorCode VecStashDestroy_Private(VecStash*);
278: PETSC_INTERN PetscErrorCode VecStashExpand_Private(VecStash*,PetscInt);
279: PETSC_INTERN PetscErrorCode VecStashScatterEnd_Private(VecStash*);
280: PETSC_INTERN PetscErrorCode VecStashSetInitialSize_Private(VecStash*,PetscInt);
281: PETSC_INTERN PetscErrorCode VecStashGetInfo_Private(VecStash*,PetscInt*,PetscInt*);
282: PETSC_INTERN PetscErrorCode VecStashScatterBegin_Private(VecStash*,PetscInt*);
283: PETSC_INTERN PetscErrorCode VecStashScatterGetMesg_Private(VecStash*,PetscMPIInt*,PetscInt**,PetscScalar**,PetscInt*);
285: /*
286: VecStashValue_Private - inserts a single value into the stash.
288: Input Parameters:
289: stash - the stash
290: idx - the global of the inserted value
291: values - the value inserted
292: */
293: PETSC_STATIC_INLINE PetscErrorCode VecStashValue_Private(VecStash *stash,PetscInt row,PetscScalar value)
294: {
296: /* Check and see if we have sufficient memory */
297: if (((stash)->n + 1) > (stash)->nmax) {
298: VecStashExpand_Private(stash,1);
299: }
300: (stash)->idx[(stash)->n] = row;
301: (stash)->array[(stash)->n] = value;
302: (stash)->n++;
303: return 0;
304: }
306: /*
307: VecStashValuesBlocked_Private - inserts 1 block of values into the stash.
309: Input Parameters:
310: stash - the stash
311: idx - the global block index
312: values - the values inserted
313: */
314: PETSC_STATIC_INLINE PetscErrorCode VecStashValuesBlocked_Private(VecStash *stash,PetscInt row,PetscScalar *values)
315: {
316: PetscInt jj,stash_bs=(stash)->bs;
317: PetscScalar *array;
319: if (((stash)->n+1) > (stash)->nmax) {
320: VecStashExpand_Private(stash,1);
321: }
322: array = (stash)->array + stash_bs*(stash)->n;
323: (stash)->idx[(stash)->n] = row;
324: for (jj=0; jj<stash_bs; jj++) { array[jj] = values[jj];}
325: (stash)->n++;
326: return 0;
327: }
329: PETSC_INTERN PetscErrorCode VecStrideGather_Default(Vec,PetscInt,Vec,InsertMode);
330: PETSC_INTERN PetscErrorCode VecStrideScatter_Default(Vec,PetscInt,Vec,InsertMode);
331: PETSC_INTERN PetscErrorCode VecReciprocal_Default(Vec);
332: PETSC_INTERN PetscErrorCode VecStrideSubSetGather_Default(Vec,PetscInt,const PetscInt[],const PetscInt[],Vec,InsertMode);
333: PETSC_INTERN PetscErrorCode VecStrideSubSetScatter_Default(Vec,PetscInt,const PetscInt[],const PetscInt[],Vec,InsertMode);
335: #if defined(PETSC_HAVE_MATLAB_ENGINE)
336: PETSC_EXTERN PetscErrorCode VecMatlabEnginePut_Default(PetscObject,void*);
337: PETSC_EXTERN PetscErrorCode VecMatlabEngineGet_Default(PetscObject,void*);
338: #endif
341: /* Reset __FUNCT__ in case the user does not define it themselves */
345: #endif