Actual source code: vecimpl.h
2: /*
3: This private file should not be included in users' code.
4: Defines the fields shared by all vector implementations.
6: It is in the public include directories so that VecGetArray() (which is public) can be defined
7: */
9: #ifndef __VECIMPL_H
12: #include petscvec.h
14: typedef struct {
15: PetscInt n,N; /* local, global vector size */
16: PetscInt rstart,rend; /* local start, local end + 1 */
17: PetscInt *range; /* the offset of each processor */
18: PetscInt bs; /* number of elements in each block (generally for multi-component problems */
19: } PetscMap;
20: EXTERN PetscErrorCode PetscMapInitialize(MPI_Comm,PetscMap*);
21: EXTERN PetscErrorCode PetscMapCopy(MPI_Comm,PetscMap*,PetscMap*);
22: /* ----------------------------------------------------------------------------*/
24: typedef struct _VecOps *VecOps;
25: struct _VecOps {
26: PetscErrorCode (*duplicate)(Vec,Vec*); /* get single vector */
27: PetscErrorCode (*duplicatevecs)(Vec,PetscInt,Vec**); /* get array of vectors */
28: PetscErrorCode (*destroyvecs)(Vec[],PetscInt); /* free array of vectors */
29: PetscErrorCode (*dot)(Vec,Vec,PetscScalar*); /* z = x^H * y */
30: PetscErrorCode (*mdot)(Vec,PetscInt,const Vec[],PetscScalar*); /* z[j] = x dot y[j] */
31: PetscErrorCode (*norm)(Vec,NormType,PetscReal*); /* z = sqrt(x^H * x) */
32: PetscErrorCode (*tdot)(Vec,Vec,PetscScalar*); /* x'*y */
33: PetscErrorCode (*mtdot)(Vec,PetscInt,const Vec[],PetscScalar*);/* z[j] = x dot y[j] */
34: PetscErrorCode (*scale)(Vec,PetscScalar); /* x = alpha * x */
35: PetscErrorCode (*copy)(Vec,Vec); /* y = x */
36: PetscErrorCode (*set)(Vec,PetscScalar); /* y = alpha */
37: PetscErrorCode (*swap)(Vec,Vec); /* exchange x and y */
38: PetscErrorCode (*axpy)(Vec,PetscScalar,Vec); /* y = y + alpha * x */
39: PetscErrorCode (*axpby)(Vec,PetscScalar,PetscScalar,Vec); /* y = y + alpha * x + beta * y*/
40: PetscErrorCode (*maxpy)(Vec,PetscInt,const PetscScalar*,Vec*); /* y = y + alpha[j] x[j] */
41: PetscErrorCode (*aypx)(Vec,PetscScalar,Vec); /* y = x + alpha * y */
42: PetscErrorCode (*waxpy)(Vec,PetscScalar,Vec,Vec); /* w = y + alpha * x */
43: PetscErrorCode (*pointwisemult)(Vec,Vec,Vec); /* w = x .* y */
44: PetscErrorCode (*pointwisedivide)(Vec,Vec,Vec); /* w = x ./ y */
45: PetscErrorCode (*setvalues)(Vec,PetscInt,const PetscInt[],const PetscScalar[],InsertMode);
46: PetscErrorCode (*assemblybegin)(Vec); /* start global assembly */
47: PetscErrorCode (*assemblyend)(Vec); /* end global assembly */
48: PetscErrorCode (*getarray)(Vec,PetscScalar**); /* get data array */
49: PetscErrorCode (*getsize)(Vec,PetscInt*);
50: PetscErrorCode (*getlocalsize)(Vec,PetscInt*);
51: PetscErrorCode (*restorearray)(Vec,PetscScalar**); /* restore data array */
52: PetscErrorCode (*max)(Vec,PetscInt*,PetscReal*); /* z = max(x); idx=index of max(x) */
53: PetscErrorCode (*min)(Vec,PetscInt*,PetscReal*); /* z = min(x); idx=index of min(x) */
54: PetscErrorCode (*setrandom)(Vec,PetscRandom); /* set y[j] = random numbers */
55: PetscErrorCode (*setoption)(Vec,VecOption);
56: PetscErrorCode (*setvaluesblocked)(Vec,PetscInt,const PetscInt[],const PetscScalar[],InsertMode);
57: PetscErrorCode (*destroy)(Vec);
58: PetscErrorCode (*view)(Vec,PetscViewer);
59: PetscErrorCode (*placearray)(Vec,const PetscScalar*); /* place data array */
60: PetscErrorCode (*replacearray)(Vec,const PetscScalar*); /* replace data array */
61: PetscErrorCode (*dot_local)(Vec,Vec,PetscScalar*);
62: PetscErrorCode (*tdot_local)(Vec,Vec,PetscScalar*);
63: PetscErrorCode (*norm_local)(Vec,NormType,PetscReal*);
64: PetscErrorCode (*mdot_local)(Vec,PetscInt,const Vec[],PetscScalar*);
65: PetscErrorCode (*mtdot_local)(Vec,PetscInt,const Vec[],PetscScalar*);
66: PetscErrorCode (*loadintovector)(PetscViewer,Vec);
67: PetscErrorCode (*reciprocal)(Vec);
68: PetscErrorCode (*viewnative)(Vec,PetscViewer);
69: PetscErrorCode (*conjugate)(Vec);
70: PetscErrorCode (*setlocaltoglobalmapping)(Vec,ISLocalToGlobalMapping);
71: PetscErrorCode (*setvalueslocal)(Vec,PetscInt,const PetscInt *,const PetscScalar *,InsertMode);
72: PetscErrorCode (*resetarray)(Vec); /* vector points to its original array, i.e. undoes any VecPlaceArray() */
73: PetscErrorCode (*setfromoptions)(Vec);
74: PetscErrorCode (*maxpointwisedivide)(Vec,Vec,PetscReal*); /* m = max abs(x ./ y) */
75: PetscErrorCode (*load)(PetscViewer,VecType,Vec*);
76: PetscErrorCode (*pointwisemax)(Vec,Vec,Vec);
77: PetscErrorCode (*pointwisemaxabs)(Vec,Vec,Vec);
78: PetscErrorCode (*pointwisemin)(Vec,Vec,Vec);
79: PetscErrorCode (*getvalues)(Vec,PetscInt,const PetscInt[],PetscScalar[]);
80: };
82: /*
83: The stash is used to temporarily store inserted vec values that
84: belong to another processor. During the assembly phase the stashed
85: values are moved to the correct processor and
86: */
88: typedef struct {
89: PetscInt nmax; /* maximum stash size */
90: PetscInt umax; /* max stash size user wants */
91: PetscInt oldnmax; /* the nmax value used previously */
92: PetscInt n; /* stash size */
93: PetscInt bs; /* block size of the stash */
94: PetscInt reallocs; /* preserve the no of mallocs invoked */
95: PetscInt *idx; /* global row numbers in stash */
96: PetscScalar *array; /* array to hold stashed values */
97: /* The following variables are used for communication */
98: MPI_Comm comm;
99: PetscMPIInt size,rank;
100: PetscMPIInt tag1,tag2;
101: MPI_Request *send_waits; /* array of send requests */
102: MPI_Request *recv_waits; /* array of receive requests */
103: MPI_Status *send_status; /* array of send status */
104: PetscInt nsends,nrecvs; /* numbers of sends and receives */
105: PetscScalar *svalues,*rvalues; /* sending and receiving data */
106: PetscInt rmax; /* maximum message length */
107: PetscInt *nprocs; /* tmp data used both during scatterbegin and end */
108: PetscInt nprocessed; /* number of messages already processed */
109: PetscTruth donotstash;
110: InsertMode insertmode;
111: PetscInt *bowners;
112: } VecStash;
114: struct _p_Vec {
115: PETSCHEADER(struct _VecOps);
116: PetscMap map;
117: void *data; /* implementation-specific data */
118: ISLocalToGlobalMapping mapping; /* mapping used in VecSetValuesLocal() */
119: ISLocalToGlobalMapping bmapping; /* mapping used in VecSetValuesBlockedLocal() */
120: PetscTruth array_gotten;
121: VecStash stash,bstash; /* used for storing off-proc values during assembly */
122: PetscTruth petscnative; /* means the ->data starts with VECHEADER and can use VecGetArrayFast()*/
123: };
125: #define VecGetArray(x,a) ((x)->petscnative ? (*(a) = *((PetscScalar **)(x)->data),0) : VecGetArray_Private((x),(a)))
126: #define VecRestoreArray(x,a) ((x)->petscnative ? PetscObjectStateIncrease((PetscObject)x) : VecRestoreArray_Private((x),(a)))
128: /*
129: Common header shared by array based vectors,
130: currently Vec_Seq and Vec_MPI
131: */
132: #define VECHEADER \
133: PetscScalar *array; \
134: PetscScalar *array_allocated; /* if the array was allocated by PETSc this is its pointer */ \
135: PetscScalar *unplacedarray; /* if one called VecPlaceArray(), this is where it stashed the original */
137: /* Default obtain and release vectors; can be used by any implementation */
138: EXTERN PetscErrorCode VecDuplicateVecs_Default(Vec,PetscInt,Vec *[]);
139: EXTERN PetscErrorCode VecDestroyVecs_Default(Vec [],PetscInt);
140: EXTERN PetscErrorCode VecLoadIntoVector_Default(PetscViewer,Vec);
144: /* --------------------------------------------------------------------*/
145: /* */
146: /* Defines the data structures used in the Vec Scatter operations */
148: typedef enum { VEC_SCATTER_SEQ_GENERAL,VEC_SCATTER_SEQ_STRIDE,
149: VEC_SCATTER_MPI_GENERAL,VEC_SCATTER_MPI_TOALL,
150: VEC_SCATTER_MPI_TOONE} VecScatterType;
152: /*
153: These scatters are for the purely local case.
154: */
155: typedef struct {
156: VecScatterType type;
157: PetscInt n; /* number of components to scatter */
158: PetscInt *vslots; /* locations of components */
159: /*
160: The next three fields are used in parallel scatters, they contain
161: optimization in the special case that the "to" vector and the "from"
162: vector are the same, so one only needs copy components that truly
163: copies instead of just y[idx[i]] = y[jdx[i]] where idx[i] == jdx[i].
164: */
165: PetscTruth nonmatching_computed;
166: PetscInt n_nonmatching; /* number of "from"s != "to"s */
167: PetscInt *slots_nonmatching; /* locations of "from"s != "to"s */
168: PetscTruth is_copy;
169: PetscInt copy_start; /* local scatter is a copy starting at copy_start */
170: PetscInt copy_length;
171: } VecScatter_Seq_General;
173: typedef struct {
174: VecScatterType type;
175: PetscInt n;
176: PetscInt first;
177: PetscInt step;
178: } VecScatter_Seq_Stride;
180: /*
181: This scatter is for a global vector copied (completely) to each processor (or all to one)
182: */
183: typedef struct {
184: VecScatterType type;
185: PetscMPIInt *count; /* elements of vector on each processor */
186: PetscScalar *work1;
187: PetscScalar *work2;
188: } VecScatter_MPI_ToAll;
190: /*
191: This is the general parallel scatter
192: */
193: typedef struct {
194: VecScatterType type;
195: PetscInt n; /* number of processors to send/receive */
196: PetscInt *starts; /* starting point in indices and values for each proc*/
197: PetscInt *indices; /* list of all components sent or received */
198: PetscMPIInt *procs; /* processors we are communicating with in scatter */
199: MPI_Request *requests,*rev_requests;
200: PetscScalar *values; /* buffer for all sends or receives */
201: VecScatter_Seq_General local; /* any part that happens to be local */
202: MPI_Status *sstatus,*rstatus;
203: PetscTruth use_readyreceiver;
204: PetscInt bs;
205: PetscTruth sendfirst;
206: } VecScatter_MPI_General;
208: struct _p_VecScatter {
209: PETSCHEADER(int);
210: PetscInt to_n,from_n;
211: PetscTruth inuse; /* prevents corruption from mixing two scatters */
212: PetscTruth beginandendtogether; /* indicates that the scatter begin and end
213: function are called together, VecScatterEnd()
214: is then treated as a nop */
215: PetscTruth packtogether; /* packs all the messages before sending, same with receive */
216: PetscErrorCode (*postrecvs)(Vec,Vec,InsertMode,ScatterMode,VecScatter);
217: PetscErrorCode (*begin)(Vec,Vec,InsertMode,ScatterMode,VecScatter);
218: PetscErrorCode (*end)(Vec,Vec,InsertMode,ScatterMode,VecScatter);
219: PetscErrorCode (*copy)(VecScatter,VecScatter);
220: PetscErrorCode (*destroy)(VecScatter);
221: PetscErrorCode (*view)(VecScatter,PetscViewer);
222: void *fromdata,*todata;
223: };
225: EXTERN PetscErrorCode VecStashCreate_Private(MPI_Comm,PetscInt,VecStash*);
226: EXTERN PetscErrorCode VecStashDestroy_Private(VecStash*);
227: EXTERN PetscErrorCode VecStashExpand_Private(VecStash*,PetscInt);
228: EXTERN PetscErrorCode VecStashScatterEnd_Private(VecStash*);
229: EXTERN PetscErrorCode VecStashSetInitialSize_Private(VecStash*,PetscInt);
230: EXTERN PetscErrorCode VecStashGetInfo_Private(VecStash*,PetscInt*,PetscInt*);
231: EXTERN PetscErrorCode VecStashScatterBegin_Private(VecStash*,PetscInt*);
232: EXTERN PetscErrorCode VecStashScatterGetMesg_Private(VecStash*,PetscMPIInt*,PetscInt**,PetscScalar**,PetscInt*);
234: /*
235: VecStashValue_Private - inserts a single value into the stash.
237: Input Parameters:
238: stash - the stash
239: idx - the global of the inserted value
240: values - the value inserted
241: */
242: #define VecStashValue_Private(stash,row,value) \
243: { \
244: /* Check and see if we have sufficient memory */ \
245: if (((stash)->n + 1) > (stash)->nmax) { \
246: VecStashExpand_Private(stash,1); \
247: } \
248: (stash)->idx[(stash)->n] = row; \
249: (stash)->array[(stash)->n] = value; \
250: (stash)->n++; \
251: }
253: /*
254: VecStashValuesBlocked_Private - inserts 1 block of values into the stash.
256: Input Parameters:
257: stash - the stash
258: idx - the global block index
259: values - the values inserted
260: */
261: #define VecStashValuesBlocked_Private(stash,row,values) \
262: { \
263: PetscInt jj,stash_bs=(stash)->bs; \
264: PetscScalar *array; \
265: if (((stash)->n+1) > (stash)->nmax) { \
266: VecStashExpand_Private(stash,1); \
267: } \
268: array = (stash)->array + stash_bs*(stash)->n; \
269: (stash)->idx[(stash)->n] = row; \
270: for (jj=0; jj<stash_bs; jj++) { array[jj] = values[jj];} \
271: (stash)->n++; \
272: }
274: EXTERN PetscErrorCode VecReciprocal_Default(Vec);
282: #if defined(PETSC_HAVE_MATLAB)
284: EXTERN PetscErrorCode VecMatlabEnginePut_Default(PetscObject,void*);
285: EXTERN PetscErrorCode VecMatlabEngineGet_Default(PetscObject,void*);
287: #endif
290: #endif