Actual source code: petsc-vecimpl.h
petsc-dev 2014-02-02
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: };
88: /*
89: The stash is used to temporarily store inserted vec values that
90: belong to another processor. During the assembly phase the stashed
91: values are moved to the correct processor and
92: */
94: typedef struct {
95: PetscInt nmax; /* maximum stash size */
96: PetscInt umax; /* max stash size user wants */
97: PetscInt oldnmax; /* the nmax value used previously */
98: PetscInt n; /* stash size */
99: PetscInt bs; /* block size of the stash */
100: PetscInt reallocs; /* preserve the no of mallocs invoked */
101: PetscInt *idx; /* global row numbers in stash */
102: PetscScalar *array; /* array to hold stashed values */
103: /* The following variables are used for communication */
104: MPI_Comm comm;
105: PetscMPIInt size,rank;
106: PetscMPIInt tag1,tag2;
107: MPI_Request *send_waits; /* array of send requests */
108: MPI_Request *recv_waits; /* array of receive requests */
109: MPI_Status *send_status; /* array of send status */
110: PetscInt nsends,nrecvs; /* numbers of sends and receives */
111: PetscScalar *svalues,*rvalues; /* sending and receiving data */
112: PetscInt *sindices,*rindices;
113: PetscInt rmax; /* maximum message length */
114: PetscInt *nprocs; /* tmp data used both during scatterbegin and end */
115: PetscInt nprocessed; /* number of messages already processed */
116: PetscBool donotstash;
117: PetscBool ignorenegidx; /* ignore negative indices passed into VecSetValues/VetGetValues */
118: InsertMode insertmode;
119: PetscInt *bowners;
120: } VecStash;
122: struct _p_Vec {
123: PETSCHEADER(struct _VecOps);
124: PetscLayout map;
125: void *data; /* implementation-specific data */
126: PetscBool array_gotten;
127: VecStash stash,bstash; /* used for storing off-proc values during assembly */
128: PetscBool petscnative; /* means the ->data starts with VECHEADER and can use VecGetArrayFast()*/
129: #if defined(PETSC_HAVE_CUSP)
130: PetscCUSPFlag valid_GPU_array; /* indicates where the most recently modified vector data is (GPU or CPU) */
131: void *spptr; /* if we're using CUSP, then this is the special pointer to the array on the GPU */
132: #endif
133: #if defined(PETSC_HAVE_VIENNACL)
134: PetscViennaCLFlag valid_GPU_array; /* indicates where the most recently modified vector data is (GPU or CPU) */
135: void *spptr; /* if we're using ViennaCL, then this is the special pointer to the array on the GPU */
136: #endif
137: };
139: PETSC_EXTERN PetscLogEvent VEC_View, VEC_Max, VEC_Min, VEC_DotBarrier, VEC_Dot, VEC_MDotBarrier, VEC_MDot, VEC_TDot, VEC_MTDot;
140: PETSC_EXTERN PetscLogEvent VEC_Norm, VEC_Normalize, VEC_Scale, VEC_Copy, VEC_Set, VEC_AXPY, VEC_AYPX, VEC_WAXPY, VEC_MAXPY;
141: PETSC_EXTERN PetscLogEvent VEC_AssemblyEnd, VEC_PointwiseMult, VEC_SetValues, VEC_Load, VEC_ScatterBarrier, VEC_ScatterBegin, VEC_ScatterEnd;
142: PETSC_EXTERN PetscLogEvent VEC_SetRandom, VEC_ReduceArithmetic, VEC_ReduceBarrier, VEC_ReduceCommunication;
143: PETSC_EXTERN PetscLogEvent VEC_ReduceBegin,VEC_ReduceEnd;
144: PETSC_EXTERN PetscLogEvent VEC_Swap, VEC_AssemblyBegin, VEC_NormBarrier, VEC_DotNormBarrier, VEC_DotNorm, VEC_AXPBYPCZ, VEC_Ops;
145: PETSC_EXTERN PetscLogEvent VEC_CUSPCopyToGPU, VEC_CUSPCopyFromGPU;
146: PETSC_EXTERN PetscLogEvent VEC_CUSPCopyToGPUSome, VEC_CUSPCopyFromGPUSome;
147: PETSC_EXTERN PetscLogEvent VEC_ViennaCLCopyToGPU, VEC_ViennaCLCopyFromGPU;
149: #if defined(PETSC_HAVE_CUSP)
150: PETSC_EXTERN PetscErrorCode VecCUSPAllocateCheckHost(Vec v);
151: PETSC_EXTERN PetscErrorCode VecCUSPCopyFromGPU(Vec v);
152: #endif
154: #if defined(PETSC_HAVE_VIENNACL)
155: PETSC_EXTERN PetscErrorCode VecViennaCLAllocateCheckHost(Vec v);
156: PETSC_EXTERN PetscErrorCode VecViennaCLCopyFromGPU(Vec v);
157: #endif
160: /*
161: Common header shared by array based vectors,
162: currently Vec_Seq and Vec_MPI
163: */
164: #define VECHEADER \
165: PetscScalar *array; \
166: PetscScalar *array_allocated; /* if the array was allocated by PETSc this is its pointer */ \
167: PetscScalar *unplacedarray; /* if one called VecPlaceArray(), this is where it stashed the original */
169: /* Default obtain and release vectors; can be used by any implementation */
170: PETSC_INTERN PetscErrorCode VecDuplicateVecs_Default(Vec,PetscInt,Vec *[]);
171: PETSC_INTERN PetscErrorCode VecDestroyVecs_Default(PetscInt,Vec []);
172: PETSC_INTERN PetscErrorCode VecLoad_Binary(Vec, PetscViewer);
173: PETSC_INTERN PetscErrorCode VecLoad_Default(Vec, PetscViewer);
175: PETSC_EXTERN PetscInt NormIds[7]; /* map from NormType to IDs used to cache/retreive values of norms */
177: /* --------------------------------------------------------------------*/
178: /* */
179: /* Defines the data structures used in the Vec Scatter operations */
181: typedef enum { VEC_SCATTER_SEQ_GENERAL,VEC_SCATTER_SEQ_STRIDE,
182: VEC_SCATTER_MPI_GENERAL,VEC_SCATTER_MPI_TOALL,
183: VEC_SCATTER_MPI_TOONE} VecScatterType;
185: /*
186: These scatters are for the purely local case.
187: */
188: typedef struct {
189: VecScatterType type;
190: PetscInt n; /* number of components to scatter */
191: PetscInt *vslots; /* locations of components */
192: /*
193: The next three fields are used in parallel scatters, they contain
194: optimization in the special case that the "to" vector and the "from"
195: vector are the same, so one only needs copy components that truly
196: copies instead of just y[idx[i]] = y[jdx[i]] where idx[i] == jdx[i].
197: */
198: PetscBool nonmatching_computed;
199: PetscInt n_nonmatching; /* number of "from"s != "to"s */
200: PetscInt *slots_nonmatching; /* locations of "from"s != "to"s */
201: PetscBool is_copy;
202: PetscInt copy_start; /* local scatter is a copy starting at copy_start */
203: PetscInt copy_length;
204: } VecScatter_Seq_General;
206: typedef struct {
207: VecScatterType type;
208: PetscInt n;
209: PetscInt first;
210: PetscInt step;
211: } VecScatter_Seq_Stride;
213: /*
214: This scatter is for a global vector copied (completely) to each processor (or all to one)
215: */
216: typedef struct {
217: VecScatterType type;
218: PetscMPIInt *count; /* elements of vector on each processor */
219: PetscMPIInt *displx;
220: PetscScalar *work1;
221: PetscScalar *work2;
222: } VecScatter_MPI_ToAll;
224: /*
225: This is the general parallel scatter
226: */
227: typedef struct {
228: VecScatterType type;
229: PetscInt n; /* number of processors to send/receive */
230: PetscInt *starts; /* starting point in indices and values for each proc*/
231: PetscInt *indices; /* list of all components sent or received */
232: PetscMPIInt *procs; /* processors we are communicating with in scatter */
233: MPI_Request *requests,*rev_requests;
234: PetscScalar *values; /* buffer for all sends or receives */
235: VecScatter_Seq_General local; /* any part that happens to be local */
236: MPI_Status *sstatus,*rstatus;
237: PetscBool use_readyreceiver;
238: PetscInt bs;
239: PetscBool sendfirst;
240: PetscBool contiq;
241: /* for MPI_Alltoallv() approach */
242: PetscBool use_alltoallv;
243: PetscMPIInt *counts,*displs;
244: /* for MPI_Alltoallw() approach */
245: PetscBool use_alltoallw;
246: #if defined(PETSC_HAVE_MPI_ALLTOALLW)
247: PetscMPIInt *wcounts,*wdispls;
248: MPI_Datatype *types;
249: #endif
250: PetscBool use_window;
251: #if defined(PETSC_HAVE_MPI_WIN_CREATE)
252: MPI_Win window;
253: PetscInt *winstarts; /* displacements in the processes I am putting to */
254: #endif
255: } VecScatter_MPI_General;
257: struct _p_VecScatter {
258: PETSCHEADER(int);
259: PetscInt to_n,from_n;
260: PetscBool inuse; /* prevents corruption from mixing two scatters */
261: PetscBool beginandendtogether; /* indicates that the scatter begin and end function are called together, VecScatterEnd()
262: is then treated as a nop */
263: PetscBool packtogether; /* packs all the messages before sending, same with receive */
264: PetscBool reproduce; /* always receive the ghost points in the same order of processes */
265: PetscErrorCode (*begin)(VecScatter,Vec,Vec,InsertMode,ScatterMode);
266: PetscErrorCode (*end)(VecScatter,Vec,Vec,InsertMode,ScatterMode);
267: PetscErrorCode (*copy)(VecScatter,VecScatter);
268: PetscErrorCode (*destroy)(VecScatter);
269: PetscErrorCode (*view)(VecScatter,PetscViewer);
270: void *fromdata,*todata;
271: void *spptr;
272: };
274: PETSC_INTERN PetscErrorCode VecStashCreate_Private(MPI_Comm,PetscInt,VecStash*);
275: PETSC_INTERN PetscErrorCode VecStashDestroy_Private(VecStash*);
276: PETSC_INTERN PetscErrorCode VecStashExpand_Private(VecStash*,PetscInt);
277: PETSC_INTERN PetscErrorCode VecStashScatterEnd_Private(VecStash*);
278: PETSC_INTERN PetscErrorCode VecStashSetInitialSize_Private(VecStash*,PetscInt);
279: PETSC_INTERN PetscErrorCode VecStashGetInfo_Private(VecStash*,PetscInt*,PetscInt*);
280: PETSC_INTERN PetscErrorCode VecStashScatterBegin_Private(VecStash*,PetscInt*);
281: PETSC_INTERN PetscErrorCode VecStashScatterGetMesg_Private(VecStash*,PetscMPIInt*,PetscInt**,PetscScalar**,PetscInt*);
283: /*
284: VecStashValue_Private - inserts a single value into the stash.
286: Input Parameters:
287: stash - the stash
288: idx - the global of the inserted value
289: values - the value inserted
290: */
291: PETSC_STATIC_INLINE PetscErrorCode VecStashValue_Private(VecStash *stash,PetscInt row,PetscScalar value)
292: {
294: /* Check and see if we have sufficient memory */
295: if (((stash)->n + 1) > (stash)->nmax) {
296: VecStashExpand_Private(stash,1);
297: }
298: (stash)->idx[(stash)->n] = row;
299: (stash)->array[(stash)->n] = value;
300: (stash)->n++;
301: return 0;
302: }
304: /*
305: VecStashValuesBlocked_Private - inserts 1 block of values into the stash.
307: Input Parameters:
308: stash - the stash
309: idx - the global block index
310: values - the values inserted
311: */
312: PETSC_STATIC_INLINE PetscErrorCode VecStashValuesBlocked_Private(VecStash *stash,PetscInt row,PetscScalar *values)
313: {
314: PetscInt jj,stash_bs=(stash)->bs;
315: PetscScalar *array;
317: if (((stash)->n+1) > (stash)->nmax) {
318: VecStashExpand_Private(stash,1);
319: }
320: array = (stash)->array + stash_bs*(stash)->n;
321: (stash)->idx[(stash)->n] = row;
322: for (jj=0; jj<stash_bs; jj++) { array[jj] = values[jj];}
323: (stash)->n++;
324: return 0;
325: }
327: PETSC_INTERN PetscErrorCode VecStrideGather_Default(Vec,PetscInt,Vec,InsertMode);
328: PETSC_INTERN PetscErrorCode VecStrideScatter_Default(Vec,PetscInt,Vec,InsertMode);
329: PETSC_INTERN PetscErrorCode VecReciprocal_Default(Vec);
331: #if defined(PETSC_HAVE_MATLAB_ENGINE)
332: PETSC_EXTERN PetscErrorCode VecMatlabEnginePut_Default(PetscObject,void*);
333: PETSC_EXTERN PetscErrorCode VecMatlabEngineGet_Default(PetscObject,void*);
334: #endif
337: /* Reset __FUNCT__ in case the user does not define it themselves */
341: #endif