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

 15: typedef struct {
 16:   MPI_Comm  comm;
 17:   PetscInt  n,N;         /* local, global vector size */
 18:   PetscInt  rstart,rend; /* local start, local end + 1 */
 19:   PetscInt  *range;      /* the offset of each processor */
 20:   PetscInt  bs;          /* number of elements in each block (generally for multi-component problems) Do NOT multiply above numbers by bs */
 21: } PetscMap;

 23: EXTERN PetscErrorCode PetscMapInitialize(MPI_Comm,PetscMap*);
 24: EXTERN PetscErrorCode PetscMapSetUp(PetscMap*);
 25: EXTERN PetscErrorCode PetscMapDestroy(PetscMap*);
 26: EXTERN PetscErrorCode PetscMapCopy(MPI_Comm,PetscMap*,PetscMap*);
 27: EXTERN PetscErrorCode  PetscMapSetLocalSize(PetscMap*,PetscInt);
 28: EXTERN PetscErrorCode  PetscMapGetLocalSize(PetscMap*,PetscInt *);
 29: PetscPolymorphicFunction(PetscMapGetLocalSize,(PetscMap *m),(m,&s),PetscInt,s)
 30: EXTERN PetscErrorCode  PetscMapSetSize(PetscMap*,PetscInt);
 31: EXTERN PetscErrorCode  PetscMapGetSize(PetscMap*,PetscInt *);
 32: PetscPolymorphicFunction(PetscMapGetSize,(PetscMap *m),(m,&s),PetscInt,s)
 33: EXTERN PetscErrorCode  PetscMapSetBlockSize(PetscMap*,PetscInt);
 34: EXTERN PetscErrorCode  PetscMapGetLocalRange(PetscMap*,PetscInt *,PetscInt *);
 35: EXTERN PetscErrorCode  PetscMapGetGlobalRange(PetscMap*,const PetscInt *[]);
 36: EXTERN PetscErrorCode  PetscMapSetSizeBlockSize(PetscMap*,PetscInt);
 37: EXTERN PetscErrorCode  PetscMapGetSizeBlockSize(PetscMap*,PetscInt *);

 39: /* ----------------------------------------------------------------------------*/

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

 99: /* 
100:     The stash is used to temporarily store inserted vec values that 
101:   belong to another processor. During the assembly phase the stashed 
102:   values are moved to the correct processor and 
103: */

105: typedef struct {
106:   PetscInt      nmax;                   /* maximum stash size */
107:   PetscInt      umax;                   /* max stash size user wants */
108:   PetscInt      oldnmax;                /* the nmax value used previously */
109:   PetscInt      n;                      /* stash size */
110:   PetscInt      bs;                     /* block size of the stash */
111:   PetscInt      reallocs;               /* preserve the no of mallocs invoked */
112:   PetscInt      *idx;                   /* global row numbers in stash */
113:   PetscScalar   *array;                 /* array to hold stashed values */
114:   /* The following variables are used for communication */
115:   MPI_Comm      comm;
116:   PetscMPIInt   size,rank;
117:   PetscMPIInt   tag1,tag2;
118:   MPI_Request   *send_waits;            /* array of send requests */
119:   MPI_Request   *recv_waits;            /* array of receive requests */
120:   MPI_Status    *send_status;           /* array of send status */
121:   PetscInt      nsends,nrecvs;          /* numbers of sends and receives */
122:   PetscScalar   *svalues,*rvalues;      /* sending and receiving data */
123:   PetscInt      rmax;                   /* maximum message length */
124:   PetscInt      *nprocs;                /* tmp data used both during scatterbegin and end */
125:   PetscInt      nprocessed;             /* number of messages already processed */
126:   PetscTruth    donotstash;
127:   PetscTruth    ignorenegidx;           /* ignore negative indices passed into VecSetValues/VetGetValues */
128:   InsertMode    insertmode;
129:   PetscInt      *bowners;
130: } VecStash;

132: struct _p_Vec {
133:   PETSCHEADER(struct _VecOps);
134:   PetscMap               map;
135:   void                   *data;     /* implementation-specific data */
136:   ISLocalToGlobalMapping mapping;   /* mapping used in VecSetValuesLocal() */
137:   ISLocalToGlobalMapping bmapping;  /* mapping used in VecSetValuesBlockedLocal() */
138:   PetscTruth             array_gotten;
139:   VecStash               stash,bstash; /* used for storing off-proc values during assembly */
140:   PetscTruth             petscnative;  /* means the ->data starts with VECHEADER and can use VecGetArrayFast()*/
141: };

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

146: /*
147:      Common header shared by array based vectors, 
148:    currently Vec_Seq and Vec_MPI
149: */
150: #define VECHEADER                          \
151:   PetscScalar *array;                      \
152:   PetscScalar *array_allocated;                        /* if the array was allocated by PETSc this is its pointer */  \
153:   PetscScalar *unplacedarray;                           /* if one called VecPlaceArray(), this is where it stashed the original */

155: /* Default obtain and release vectors; can be used by any implementation */
156: EXTERN PetscErrorCode VecDuplicateVecs_Default(Vec,PetscInt,Vec *[]);
157: EXTERN PetscErrorCode VecDestroyVecs_Default(Vec [],PetscInt);
158: EXTERN PetscErrorCode VecLoadIntoVector_Default(PetscViewer,Vec);


162: /* --------------------------------------------------------------------*/
163: /*                                                                     */
164: /* Defines the data structures used in the Vec Scatter operations      */

166: typedef enum { VEC_SCATTER_SEQ_GENERAL,VEC_SCATTER_SEQ_STRIDE,
167:                VEC_SCATTER_MPI_GENERAL,VEC_SCATTER_MPI_TOALL,
168:                VEC_SCATTER_MPI_TOONE} VecScatterType;

170: /* 
171:    These scatters are for the purely local case.
172: */
173: typedef struct {
174:   VecScatterType type;
175:   PetscInt       n;                    /* number of components to scatter */
176:   PetscInt       *vslots;              /* locations of components */
177:   /*
178:        The next three fields are used in parallel scatters, they contain 
179:        optimization in the special case that the "to" vector and the "from" 
180:        vector are the same, so one only needs copy components that truly 
181:        copies instead of just y[idx[i]] = y[jdx[i]] where idx[i] == jdx[i].
182:   */
183:   PetscTruth     nonmatching_computed;
184:   PetscInt       n_nonmatching;        /* number of "from"s  != "to"s */
185:   PetscInt       *slots_nonmatching;   /* locations of "from"s  != "to"s */
186:   PetscTruth     is_copy;
187:   PetscInt       copy_start;   /* local scatter is a copy starting at copy_start */
188:   PetscInt       copy_length;
189: } VecScatter_Seq_General;

191: typedef struct {
192:   VecScatterType type;
193:   PetscInt       n;
194:   PetscInt       first;
195:   PetscInt       step;
196: } VecScatter_Seq_Stride;

198: /*
199:    This scatter is for a global vector copied (completely) to each processor (or all to one)
200: */
201: typedef struct {
202:   VecScatterType type;
203:   PetscMPIInt    *count;        /* elements of vector on each processor */
204:   PetscScalar    *work1;
205:   PetscScalar    *work2;
206: } VecScatter_MPI_ToAll;

208: /*
209:    This is the general parallel scatter
210: */
211: typedef struct {
212:   VecScatterType         type;
213:   PetscInt               n;        /* number of processors to send/receive */
214:   PetscInt               *starts;  /* starting point in indices and values for each proc*/
215:   PetscInt               *indices; /* list of all components sent or received */
216:   PetscMPIInt            *procs;   /* processors we are communicating with in scatter */
217:   MPI_Request            *requests,*rev_requests;
218:   PetscScalar            *values;  /* buffer for all sends or receives */
219:   VecScatter_Seq_General local;    /* any part that happens to be local */
220:   MPI_Status             *sstatus,*rstatus;
221:   PetscTruth             use_readyreceiver;
222:   PetscInt               bs;
223:   PetscTruth             sendfirst;
224:   PetscTruth             contiq;
225:   /* for MPI_Alltoallv() approach */
226:   PetscTruth             use_alltoallv;
227:   PetscMPIInt            *counts,*displs;
228:   /* for MPI_Alltoallw() approach */
229:   PetscTruth             use_alltoallw;
230: #if defined(PETSC_HAVE_MPI_ALLTOALLW)
231:   PetscMPIInt            *wcounts,*wdispls;
232:   MPI_Datatype           *types;
233: #endif
234:   PetscTruth             use_window;
235: #if defined(PETSC_HAVE_MPI_WIN_CREATE)
236:   MPI_Win                window;
237:   PetscInt               *winstarts;    /* displacements in the processes I am putting to */
238: #endif
239: } VecScatter_MPI_General;

241: struct _p_VecScatter {
242:   PETSCHEADER(int);
243:   PetscInt       to_n,from_n;
244:   PetscTruth     inuse;                /* prevents corruption from mixing two scatters */
245:   PetscTruth     beginandendtogether;  /* indicates that the scatter begin and end  function are called together, VecScatterEnd()
246:                                           is then treated as a nop */
247:   PetscTruth     packtogether;         /* packs all the messages before sending, same with receive */
248:   PetscErrorCode (*begin)(VecScatter,Vec,Vec,InsertMode,ScatterMode);
249:   PetscErrorCode (*end)(VecScatter,Vec,Vec,InsertMode,ScatterMode);
250:   PetscErrorCode (*copy)(VecScatter,VecScatter);
251:   PetscErrorCode (*destroy)(VecScatter);
252:   PetscErrorCode (*view)(VecScatter,PetscViewer);
253:   void           *fromdata,*todata;
254: };

256: EXTERN PetscErrorCode VecStashCreate_Private(MPI_Comm,PetscInt,VecStash*);
257: EXTERN PetscErrorCode VecStashDestroy_Private(VecStash*);
258: EXTERN PetscErrorCode VecStashExpand_Private(VecStash*,PetscInt);
259: EXTERN PetscErrorCode VecStashScatterEnd_Private(VecStash*);
260: EXTERN PetscErrorCode VecStashSetInitialSize_Private(VecStash*,PetscInt);
261: EXTERN PetscErrorCode VecStashGetInfo_Private(VecStash*,PetscInt*,PetscInt*);
262: EXTERN PetscErrorCode VecStashScatterBegin_Private(VecStash*,PetscInt*);
263: EXTERN PetscErrorCode VecStashScatterGetMesg_Private(VecStash*,PetscMPIInt*,PetscInt**,PetscScalar**,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: PETSC_STATIC_INLINE PetscErrorCode VecStashValue_Private(VecStash *stash,PetscInt row,PetscScalar value)
274: {
276:   /* Check and see if we have sufficient memory */
277:   if (((stash)->n + 1) > (stash)->nmax) {
278:     VecStashExpand_Private(stash,1);
279:   }
280:   (stash)->idx[(stash)->n]   = row;
281:   (stash)->array[(stash)->n] = value;
282:   (stash)->n++;
283:   return 0;
284: }

286: /*
287:   VecStashValuesBlocked_Private - inserts 1 block of values into the stash. 

289:   Input Parameters:
290:   stash  - the stash
291:   idx    - the global block index
292:   values - the values inserted
293: */
294: PETSC_STATIC_INLINE PetscErrorCode VecStashValuesBlocked_Private(VecStash *stash,PetscInt row,PetscScalar *values)
295: {
296:   PetscInt       jj,stash_bs=(stash)->bs;
297:   PetscScalar    *array;
299:   if (((stash)->n+1) > (stash)->nmax) {
300:     VecStashExpand_Private(stash,1);
301:   }
302:   array = (stash)->array + stash_bs*(stash)->n;
303:   (stash)->idx[(stash)->n]   = row;
304:   for (jj=0; jj<stash_bs; jj++) { array[jj] = values[jj];}
305:   (stash)->n++;
306:   return 0;
307: }

309: EXTERN PetscErrorCode VecReciprocal_Default(Vec);


317: #if defined(PETSC_HAVE_MATLAB_ENGINE)
319: EXTERN PetscErrorCode VecMatlabEnginePut_Default(PetscObject,void*);
320: EXTERN PetscErrorCode VecMatlabEngineGet_Default(PetscObject,void*);
322: #endif

325: #endif