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:   PetscInt  n,N;         /* local, global vector size */
 17:   PetscInt  rstart,rend; /* local start, local end + 1 */
 18:   PetscInt  *range;      /* the offset of each processor */
 19:   PetscInt  bs;          /* number of elements in each block (generally for multi-component problems */
 20: } PetscMap;

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

 35: /* ----------------------------------------------------------------------------*/

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

 95: /* 
 96:     The stash is used to temporarily store inserted vec values that 
 97:   belong to another processor. During the assembly phase the stashed 
 98:   values are moved to the correct processor and 
 99: */

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

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

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

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

150: /* Default obtain and release vectors; can be used by any implementation */
151: EXTERN PetscErrorCode VecDuplicateVecs_Default(Vec,PetscInt,Vec *[]);
152: EXTERN PetscErrorCode VecDestroyVecs_Default(Vec [],PetscInt);
153: EXTERN PetscErrorCode VecLoadIntoVector_Default(PetscViewer,Vec);


157: /* --------------------------------------------------------------------*/
158: /*                                                                     */
159: /* Defines the data structures used in the Vec Scatter operations      */

161: typedef enum { VEC_SCATTER_SEQ_GENERAL,VEC_SCATTER_SEQ_STRIDE,
162:                VEC_SCATTER_MPI_GENERAL,VEC_SCATTER_MPI_TOALL,
163:                VEC_SCATTER_MPI_TOONE} VecScatterType;

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

186: typedef struct {
187:   VecScatterType type;
188:   PetscInt       n;
189:   PetscInt       first;
190:   PetscInt       step;
191: } VecScatter_Seq_Stride;

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

203: /*
204:    This is the general parallel scatter
205: */
206: typedef struct {
207:   VecScatterType         type;
208:   PetscInt               n;        /* number of processors to send/receive */
209:   PetscInt               *starts;  /* starting point in indices and values for each proc*/
210:   PetscInt               *indices; /* list of all components sent or received */
211:   PetscMPIInt            *procs;   /* processors we are communicating with in scatter */
212:   MPI_Request            *requests,*rev_requests;
213:   PetscScalar            *values;  /* buffer for all sends or receives */
214:   VecScatter_Seq_General local;    /* any part that happens to be local */
215:   MPI_Status             *sstatus,*rstatus;
216:   PetscTruth             use_readyreceiver;
217:   PetscInt               bs;
218:   PetscTruth             sendfirst;
219:   PetscTruth             contiq;
220:   /* for MPI_Alltoallv() approach */
221:   PetscTruth             use_alltoallv;
222:   PetscMPIInt            *counts,*displs;
223:   /* for MPI_Alltoallw() approach */
224:   PetscTruth             use_alltoallw;
225:   PetscMPIInt            *wcounts,*wdispls;
226:   MPI_Datatype           *types;
227: } VecScatter_MPI_General;

229: struct _p_VecScatter {
230:   PETSCHEADER(int);
231:   PetscInt       to_n,from_n;
232:   PetscTruth     inuse;                /* prevents corruption from mixing two scatters */
233:   PetscTruth     beginandendtogether;  /* indicates that the scatter begin and end  function are called together, VecScatterEnd()
234:                                           is then treated as a nop */
235:   PetscTruth     packtogether;         /* packs all the messages before sending, same with receive */
236:   PetscErrorCode (*begin)(Vec,Vec,InsertMode,ScatterMode,VecScatter);
237:   PetscErrorCode (*end)(Vec,Vec,InsertMode,ScatterMode,VecScatter);
238:   PetscErrorCode (*copy)(VecScatter,VecScatter);
239:   PetscErrorCode (*destroy)(VecScatter);
240:   PetscErrorCode (*view)(VecScatter,PetscViewer);
241:   void           *fromdata,*todata;
242: };

244: EXTERN PetscErrorCode VecStashCreate_Private(MPI_Comm,PetscInt,VecStash*);
245: EXTERN PetscErrorCode VecStashDestroy_Private(VecStash*);
246: EXTERN PetscErrorCode VecStashExpand_Private(VecStash*,PetscInt);
247: EXTERN PetscErrorCode VecStashScatterEnd_Private(VecStash*);
248: EXTERN PetscErrorCode VecStashSetInitialSize_Private(VecStash*,PetscInt);
249: EXTERN PetscErrorCode VecStashGetInfo_Private(VecStash*,PetscInt*,PetscInt*);
250: EXTERN PetscErrorCode VecStashScatterBegin_Private(VecStash*,PetscInt*);
251: EXTERN PetscErrorCode VecStashScatterGetMesg_Private(VecStash*,PetscMPIInt*,PetscInt**,PetscScalar**,PetscInt*);

253: /*
254:   VecStashValue_Private - inserts a single value into the stash.

256:   Input Parameters:
257:   stash  - the stash
258:   idx    - the global of the inserted value
259:   values - the value inserted
260: */
261: #define VecStashValue_Private(stash,row,value) \
262: {  \
263:   /* Check and see if we have sufficient memory */ \
264:   if (((stash)->n + 1) > (stash)->nmax) { \
265:     VecStashExpand_Private(stash,1); \
266:   } \
267:   (stash)->idx[(stash)->n]   = row; \
268:   (stash)->array[(stash)->n] = value; \
269:   (stash)->n++; \
270: }

272: /*
273:   VecStashValuesBlocked_Private - inserts 1 block of values into the stash. 

275:   Input Parameters:
276:   stash  - the stash
277:   idx    - the global block index
278:   values - the values inserted
279: */
280: #define VecStashValuesBlocked_Private(stash,row,values) \
281: { \
282:   PetscInt    jj,stash_bs=(stash)->bs; \
283:   PetscScalar *array; \
284:   if (((stash)->n+1) > (stash)->nmax) { \
285:     VecStashExpand_Private(stash,1); \
286:   } \
287:   array = (stash)->array + stash_bs*(stash)->n; \
288:   (stash)->idx[(stash)->n]   = row; \
289:   for (jj=0; jj<stash_bs; jj++) { array[jj] = values[jj];} \
290:   (stash)->n++; \
291: }

293: EXTERN PetscErrorCode VecReciprocal_Default(Vec);


301: #if defined(PETSC_HAVE_MATLAB)
303: EXTERN PetscErrorCode VecMatlabEnginePut_Default(PetscObject,void*);
304: EXTERN PetscErrorCode VecMatlabEngineGet_Default(PetscObject,void*);
306: #endif

309: #endif