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: */

  8: #ifndef __VECIMPL_H

 11:  #include petscvec.h

 14: /*S
 15:      PetscLayout - defines layout of vectors and matrices across processes (which rows are owned by which processes)

 17:    Level: developer


 20: .seealso:  PetscLayoutCreate(), PetscLayoutDestroy()
 21: S*/
 22: typedef struct _p_PetscLayout* PetscLayout;
 23: struct _p_PetscLayout{
 24:   MPI_Comm  comm;
 25:   PetscInt  n,N;         /* local, global vector size */
 26:   PetscInt  rstart,rend; /* local start, local end + 1 */
 27:   PetscInt  *range;      /* the offset of each processor */
 28:   PetscInt  bs;          /* number of elements in each block (generally for multi-component problems) Do NOT multiply above numbers by bs */
 29:   PetscInt  refcnt;      /* MPI Vecs obtained with VecDuplicate() and from MatGetVecs() reuse map of input object */
 30: };

 32: EXTERN PetscErrorCode PetscLayoutCreate(MPI_Comm,PetscLayout*);
 33: EXTERN PetscErrorCode PetscLayoutSetUp(PetscLayout);
 34: EXTERN PetscErrorCode PetscLayoutDestroy(PetscLayout);
 35: EXTERN PetscErrorCode PetscLayoutCopy(PetscLayout,PetscLayout*);
 36: EXTERN PetscErrorCode  PetscLayoutSetLocalSize(PetscLayout,PetscInt);
 37: EXTERN PetscErrorCode  PetscLayoutGetLocalSize(PetscLayout,PetscInt *);
 38: PetscPolymorphicFunction(PetscLayoutGetLocalSize,(PetscLayout m),(m,&s),PetscInt,s)
 39: EXTERN PetscErrorCode  PetscLayoutSetSize(PetscLayout,PetscInt);
 40: EXTERN PetscErrorCode  PetscLayoutGetSize(PetscLayout,PetscInt *);
 41: PetscPolymorphicFunction(PetscLayoutGetSize,(PetscLayout m),(m,&s),PetscInt,s)
 42: EXTERN PetscErrorCode  PetscLayoutSetBlockSize(PetscLayout,PetscInt);
 43: EXTERN PetscErrorCode  PetscLayoutGetBlockSize(PetscLayout,PetscInt*);
 44: EXTERN PetscErrorCode  PetscLayoutGetRange(PetscLayout,PetscInt *,PetscInt *);
 45: EXTERN PetscErrorCode  PetscLayoutGetRanges(PetscLayout,const PetscInt *[]);

 47: /* ----------------------------------------------------------------------------*/

 49: typedef struct _VecOps *VecOps;
 50: struct _VecOps {
 51:   PetscErrorCode (*duplicate)(Vec,Vec*);         /* get single vector */
 52:   PetscErrorCode (*duplicatevecs)(Vec,PetscInt,Vec**);     /* get array of vectors */
 53:   PetscErrorCode (*destroyvecs)(Vec[],PetscInt);           /* free array of vectors */
 54:   PetscErrorCode (*dot)(Vec,Vec,PetscScalar*);             /* z = x^H * y */
 55:   PetscErrorCode (*mdot)(Vec,PetscInt,const Vec[],PetscScalar*); /* z[j] = x dot y[j] */
 56:   PetscErrorCode (*norm)(Vec,NormType,PetscReal*);        /* z = sqrt(x^H * x) */
 57:   PetscErrorCode (*tdot)(Vec,Vec,PetscScalar*);             /* x'*y */
 58:   PetscErrorCode (*mtdot)(Vec,PetscInt,const Vec[],PetscScalar*);/* z[j] = x dot y[j] */
 59:   PetscErrorCode (*scale)(Vec,PetscScalar);                 /* x = alpha * x   */
 60:   PetscErrorCode (*copy)(Vec,Vec);                     /* y = x */
 61:   PetscErrorCode (*set)(Vec,PetscScalar);                        /* y = alpha  */
 62:   PetscErrorCode (*swap)(Vec,Vec);                               /* exchange x and y */
 63:   PetscErrorCode (*axpy)(Vec,PetscScalar,Vec);                   /* y = y + alpha * x */
 64:   PetscErrorCode (*axpby)(Vec,PetscScalar,PetscScalar,Vec);      /* y = alpha * x + beta * y*/
 65:   PetscErrorCode (*maxpy)(Vec,PetscInt,const PetscScalar*,Vec*); /* y = y + alpha[j] x[j] */
 66:   PetscErrorCode (*aypx)(Vec,PetscScalar,Vec);                   /* y = x + alpha * y */
 67:   PetscErrorCode (*waxpy)(Vec,PetscScalar,Vec,Vec);         /* w = y + alpha * x */
 68:   PetscErrorCode (*axpbypcz)(Vec,PetscScalar,PetscScalar,PetscScalar,Vec,Vec);   /* z = alpha * x + beta *y + gamma *z*/
 69:   PetscErrorCode (*pointwisemult)(Vec,Vec,Vec);        /* w = x .* y */
 70:   PetscErrorCode (*pointwisedivide)(Vec,Vec,Vec);      /* w = x ./ y */
 71:   PetscErrorCode (*setvalues)(Vec,PetscInt,const PetscInt[],const PetscScalar[],InsertMode);
 72:   PetscErrorCode (*assemblybegin)(Vec);                /* start global assembly */
 73:   PetscErrorCode (*assemblyend)(Vec);                  /* end global assembly */
 74:   PetscErrorCode (*getarray)(Vec,PetscScalar**);            /* get data array */
 75:   PetscErrorCode (*getsize)(Vec,PetscInt*);
 76:   PetscErrorCode (*getlocalsize)(Vec,PetscInt*);
 77:   PetscErrorCode (*restorearray)(Vec,PetscScalar**);        /* restore data array */
 78:   PetscErrorCode (*max)(Vec,PetscInt*,PetscReal*);      /* z = max(x); idx=index of max(x) */
 79:   PetscErrorCode (*min)(Vec,PetscInt*,PetscReal*);      /* z = min(x); idx=index of min(x) */
 80:   PetscErrorCode (*setrandom)(Vec,PetscRandom);         /* set y[j] = random numbers */
 81:   PetscErrorCode (*setoption)(Vec,VecOption,PetscTruth);
 82:   PetscErrorCode (*setvaluesblocked)(Vec,PetscInt,const PetscInt[],const PetscScalar[],InsertMode);
 83:   PetscErrorCode (*destroy)(Vec);
 84:   PetscErrorCode (*view)(Vec,PetscViewer);
 85:   PetscErrorCode (*placearray)(Vec,const PetscScalar*);     /* place data array */
 86:   PetscErrorCode (*replacearray)(Vec,const PetscScalar*);     /* replace data array */
 87:   PetscErrorCode (*dot_local)(Vec,Vec,PetscScalar*);
 88:   PetscErrorCode (*tdot_local)(Vec,Vec,PetscScalar*);
 89:   PetscErrorCode (*norm_local)(Vec,NormType,PetscReal*);
 90:   PetscErrorCode (*mdot_local)(Vec,PetscInt,const Vec[],PetscScalar*);
 91:   PetscErrorCode (*mtdot_local)(Vec,PetscInt,const Vec[],PetscScalar*);
 92:   PetscErrorCode (*loadintovector)(PetscViewer,Vec);
 93:   PetscErrorCode (*loadintovectornative)(PetscViewer,Vec);
 94:   PetscErrorCode (*reciprocal)(Vec);
 95:   PetscErrorCode (*viewnative)(Vec,PetscViewer);
 96:   PetscErrorCode (*conjugate)(Vec);
 97:   PetscErrorCode (*setlocaltoglobalmapping)(Vec,ISLocalToGlobalMapping);
 98:   PetscErrorCode (*setvalueslocal)(Vec,PetscInt,const PetscInt *,const PetscScalar *,InsertMode);
 99:   PetscErrorCode (*resetarray)(Vec);      /* vector points to its original array, i.e. undoes any VecPlaceArray() */
100:   PetscErrorCode (*setfromoptions)(Vec);
101:   PetscErrorCode (*maxpointwisedivide)(Vec,Vec,PetscReal*);      /* m = max abs(x ./ y) */
102:   PetscErrorCode (*load)(PetscViewer,const VecType,Vec*);
103:   PetscErrorCode (*pointwisemax)(Vec,Vec,Vec);
104:   PetscErrorCode (*pointwisemaxabs)(Vec,Vec,Vec);
105:   PetscErrorCode (*pointwisemin)(Vec,Vec,Vec);
106:   PetscErrorCode (*getvalues)(Vec,PetscInt,const PetscInt[],PetscScalar[]);
107:   PetscErrorCode (*sqrt)(Vec);
108:   PetscErrorCode (*abs)(Vec);
109:   PetscErrorCode (*exp)(Vec);
110:   PetscErrorCode (*log)(Vec);
111:   PetscErrorCode (*shift)(Vec);
112:   PetscErrorCode (*create)(Vec);
113:   PetscErrorCode (*dotnorm2)(Vec,Vec,PetscScalar*,PetscScalar*);
114: };

116: /* 
117:     The stash is used to temporarily store inserted vec values that 
118:   belong to another processor. During the assembly phase the stashed 
119:   values are moved to the correct processor and 
120: */

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

150: struct _p_Vec {
151:   PETSCHEADER(struct _VecOps);
152:   PetscLayout            map;
153:   void                   *data;     /* implementation-specific data */
154:   ISLocalToGlobalMapping mapping;   /* mapping used in VecSetValuesLocal() */
155:   ISLocalToGlobalMapping bmapping;  /* mapping used in VecSetValuesBlockedLocal() */
156:   PetscTruth             array_gotten;
157:   VecStash               stash,bstash; /* used for storing off-proc values during assembly */
158:   PetscTruth             petscnative;  /* means the ->data starts with VECHEADER and can use VecGetArrayFast()*/
159: };

161: #define VecGetArray(x,a)     ((x)->petscnative ? (*(a) = *((PetscScalar **)(x)->data),0) : VecGetArray_Private((x),(a)))
162: #define VecRestoreArray(x,a) ((x)->petscnative ? PetscObjectStateIncrease((PetscObject)x) : VecRestoreArray_Private((x),(a)))

164: /*
165:      Common header shared by array based vectors, 
166:    currently Vec_Seq and Vec_MPI
167: */
168: #define VECHEADER                          \
169:   PetscScalar *array;                      \
170:   PetscScalar *array_allocated;                        /* if the array was allocated by PETSc this is its pointer */  \
171:   PetscScalar *unplacedarray;                           /* if one called VecPlaceArray(), this is where it stashed the original */

173: /* Default obtain and release vectors; can be used by any implementation */
174: EXTERN PetscErrorCode VecDuplicateVecs_Default(Vec,PetscInt,Vec *[]);
175: EXTERN PetscErrorCode VecDestroyVecs_Default(Vec [],PetscInt);
176: EXTERN PetscErrorCode VecLoadIntoVector_Default(PetscViewer,Vec);


180: /* --------------------------------------------------------------------*/
181: /*                                                                     */
182: /* Defines the data structures used in the Vec Scatter operations      */

184: typedef enum { VEC_SCATTER_SEQ_GENERAL,VEC_SCATTER_SEQ_STRIDE,
185:                VEC_SCATTER_MPI_GENERAL,VEC_SCATTER_MPI_TOALL,
186:                VEC_SCATTER_MPI_TOONE} VecScatterType;

188: /* 
189:    These scatters are for the purely local case.
190: */
191: typedef struct {
192:   VecScatterType type;
193:   PetscInt       n;                    /* number of components to scatter */
194:   PetscInt       *vslots;              /* locations of components */
195:   /*
196:        The next three fields are used in parallel scatters, they contain 
197:        optimization in the special case that the "to" vector and the "from" 
198:        vector are the same, so one only needs copy components that truly 
199:        copies instead of just y[idx[i]] = y[jdx[i]] where idx[i] == jdx[i].
200:   */
201:   PetscTruth     nonmatching_computed;
202:   PetscInt       n_nonmatching;        /* number of "from"s  != "to"s */
203:   PetscInt       *slots_nonmatching;   /* locations of "from"s  != "to"s */
204:   PetscTruth     is_copy;
205:   PetscInt       copy_start;   /* local scatter is a copy starting at copy_start */
206:   PetscInt       copy_length;
207: } VecScatter_Seq_General;

209: typedef struct {
210:   VecScatterType type;
211:   PetscInt       n;
212:   PetscInt       first;
213:   PetscInt       step;
214: } VecScatter_Seq_Stride;

216: /*
217:    This scatter is for a global vector copied (completely) to each processor (or all to one)
218: */
219: typedef struct {
220:   VecScatterType type;
221:   PetscMPIInt    *count;        /* elements of vector on each processor */
222:   PetscMPIInt    *displx;
223:   PetscScalar    *work1;
224:   PetscScalar    *work2;
225: } VecScatter_MPI_ToAll;

227: /*
228:    This is the general parallel scatter
229: */
230: typedef struct {
231:   VecScatterType         type;
232:   PetscInt               n;        /* number of processors to send/receive */
233:   PetscInt               *starts;  /* starting point in indices and values for each proc*/
234:   PetscInt               *indices; /* list of all components sent or received */
235:   PetscMPIInt            *procs;   /* processors we are communicating with in scatter */
236:   MPI_Request            *requests,*rev_requests;
237:   PetscScalar            *values;  /* buffer for all sends or receives */
238:   VecScatter_Seq_General local;    /* any part that happens to be local */
239:   MPI_Status             *sstatus,*rstatus;
240:   PetscTruth             use_readyreceiver;
241:   PetscInt               bs;
242:   PetscTruth             sendfirst;
243:   PetscTruth             contiq;
244:   /* for MPI_Alltoallv() approach */
245:   PetscTruth             use_alltoallv;
246:   PetscMPIInt            *counts,*displs;
247:   /* for MPI_Alltoallw() approach */
248:   PetscTruth             use_alltoallw;
249: #if defined(PETSC_HAVE_MPI_ALLTOALLW)
250:   PetscMPIInt            *wcounts,*wdispls;
251:   MPI_Datatype           *types;
252: #endif
253:   PetscTruth             use_window;
254: #if defined(PETSC_HAVE_MPI_WIN_CREATE)
255:   MPI_Win                window;
256:   PetscInt               *winstarts;    /* displacements in the processes I am putting to */
257: #endif
258: } VecScatter_MPI_General;

260: struct _p_VecScatter {
261:   PETSCHEADER(int);
262:   PetscInt       to_n,from_n;
263:   PetscTruth     inuse;                /* prevents corruption from mixing two scatters */
264:   PetscTruth     beginandendtogether;  /* indicates that the scatter begin and end  function are called together, VecScatterEnd()
265:                                           is then treated as a nop */
266:   PetscTruth     packtogether;         /* packs all the messages before sending, same with receive */
267:   PetscTruth     reproduce;            /* always receive the ghost points in the same order of processes */
268:   PetscErrorCode (*begin)(VecScatter,Vec,Vec,InsertMode,ScatterMode);
269:   PetscErrorCode (*end)(VecScatter,Vec,Vec,InsertMode,ScatterMode);
270:   PetscErrorCode (*copy)(VecScatter,VecScatter);
271:   PetscErrorCode (*destroy)(VecScatter);
272:   PetscErrorCode (*view)(VecScatter,PetscViewer);
273:   void           *fromdata,*todata;
274: };

276: EXTERN PetscErrorCode VecStashCreate_Private(MPI_Comm,PetscInt,VecStash*);
277: EXTERN PetscErrorCode VecStashDestroy_Private(VecStash*);
278: EXTERN PetscErrorCode VecStashExpand_Private(VecStash*,PetscInt);
279: EXTERN PetscErrorCode VecStashScatterEnd_Private(VecStash*);
280: EXTERN PetscErrorCode VecStashSetInitialSize_Private(VecStash*,PetscInt);
281: EXTERN PetscErrorCode VecStashGetInfo_Private(VecStash*,PetscInt*,PetscInt*);
282: EXTERN PetscErrorCode VecStashScatterBegin_Private(VecStash*,PetscInt*);
283: EXTERN 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: EXTERN PetscErrorCode VecReciprocal_Default(Vec);


337: #if defined(PETSC_HAVE_MATLAB_ENGINE)
339: EXTERN PetscErrorCode VecMatlabEnginePut_Default(PetscObject,void*);
340: EXTERN PetscErrorCode VecMatlabEngineGet_Default(PetscObject,void*);
342: #endif

345: #endif