Actual source code: petsc-vecimpl.h
petsc-3.4.2 2013-07-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:   PetscViewer            viewonassembly;   /* if -vec_view is set in VecSetFromOptions() then these variables are used to implement it */
    130:   PetscViewerFormat      viewformatonassembly;
    131: #if defined(PETSC_HAVE_CUSP)
    132:   PetscCUSPFlag          valid_GPU_array;    /* indicates where the most recently modified vector data is (GPU or CPU) */
    133:   void                   *spptr; /* if we're using CUSP, then this is the special pointer to the array on the GPU */
    134: #endif
    135: };

    137: PETSC_EXTERN PetscLogEvent VEC_View, VEC_Max, VEC_Min, VEC_DotBarrier, VEC_Dot, VEC_MDotBarrier, VEC_MDot, VEC_TDot, VEC_MTDot;
    138: PETSC_EXTERN PetscLogEvent VEC_Norm, VEC_Normalize, VEC_Scale, VEC_Copy, VEC_Set, VEC_AXPY, VEC_AYPX, VEC_WAXPY, VEC_MAXPY;
    139: PETSC_EXTERN PetscLogEvent VEC_AssemblyEnd, VEC_PointwiseMult, VEC_SetValues, VEC_Load, VEC_ScatterBarrier, VEC_ScatterBegin, VEC_ScatterEnd;
    140: PETSC_EXTERN PetscLogEvent VEC_SetRandom, VEC_ReduceArithmetic, VEC_ReduceBarrier, VEC_ReduceCommunication;
    141: PETSC_EXTERN PetscLogEvent VEC_ReduceBegin,VEC_ReduceEnd;
    142: PETSC_EXTERN PetscLogEvent VEC_Swap, VEC_AssemblyBegin, VEC_NormBarrier, VEC_DotNormBarrier, VEC_DotNorm, VEC_AXPBYPCZ, VEC_Ops;
    143: PETSC_EXTERN PetscLogEvent VEC_CUSPCopyToGPU, VEC_CUSPCopyFromGPU;
    144: PETSC_EXTERN PetscLogEvent VEC_CUSPCopyToGPUSome, VEC_CUSPCopyFromGPUSome;

    146: #if defined(PETSC_HAVE_CUSP)
    147: PETSC_EXTERN PetscErrorCode VecCUSPAllocateCheckHost(Vec v);
    148: PETSC_EXTERN PetscErrorCode VecCUSPCopyFromGPU(Vec v);
    149: #endif

    152: /*
    153:      Common header shared by array based vectors,
    154:    currently Vec_Seq and Vec_MPI
    155: */
    156: #define VECHEADER                          \
    157:   PetscScalar *array;                      \
    158:   PetscScalar *array_allocated;                        /* if the array was allocated by PETSc this is its pointer */  \
    159:   PetscScalar *unplacedarray;                           /* if one called VecPlaceArray(), this is where it stashed the original */

    161: /* Default obtain and release vectors; can be used by any implementation */
    162: PETSC_INTERN PetscErrorCode VecDuplicateVecs_Default(Vec,PetscInt,Vec *[]);
    163: PETSC_INTERN PetscErrorCode VecDestroyVecs_Default(PetscInt,Vec []);
    164: PETSC_INTERN PetscErrorCode VecLoad_Binary(Vec, PetscViewer);
    165: PETSC_INTERN PetscErrorCode VecLoad_Default(Vec, PetscViewer);

    167: PETSC_EXTERN PetscInt  NormIds[7];  /* map from NormType to IDs used to cache/retreive values of norms */

    169: /* --------------------------------------------------------------------*/
    170: /*                                                                     */
    171: /* Defines the data structures used in the Vec Scatter operations      */

    173: typedef enum { VEC_SCATTER_SEQ_GENERAL,VEC_SCATTER_SEQ_STRIDE,
    174:                VEC_SCATTER_MPI_GENERAL,VEC_SCATTER_MPI_TOALL,
    175:                VEC_SCATTER_MPI_TOONE} VecScatterType;

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

    198: typedef struct {
    199:   VecScatterType type;
    200:   PetscInt       n;
    201:   PetscInt       first;
    202:   PetscInt       step;
    203: } VecScatter_Seq_Stride;

    205: /*
    206:    This scatter is for a global vector copied (completely) to each processor (or all to one)
    207: */
    208: typedef struct {
    209:   VecScatterType type;
    210:   PetscMPIInt    *count;        /* elements of vector on each processor */
    211:   PetscMPIInt    *displx;
    212:   PetscScalar    *work1;
    213:   PetscScalar    *work2;
    214: } VecScatter_MPI_ToAll;

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

    249: struct _p_VecScatter {
    250:   PETSCHEADER(int);
    251:   PetscInt       to_n,from_n;
    252:   PetscBool      inuse;                /* prevents corruption from mixing two scatters */
    253:   PetscBool      beginandendtogether;  /* indicates that the scatter begin and end  function are called together, VecScatterEnd()
    254:                                           is then treated as a nop */
    255:   PetscBool      packtogether;         /* packs all the messages before sending, same with receive */
    256:   PetscBool      reproduce;            /* always receive the ghost points in the same order of processes */
    257:   PetscErrorCode (*begin)(VecScatter,Vec,Vec,InsertMode,ScatterMode);
    258:   PetscErrorCode (*end)(VecScatter,Vec,Vec,InsertMode,ScatterMode);
    259:   PetscErrorCode (*copy)(VecScatter,VecScatter);
    260:   PetscErrorCode (*destroy)(VecScatter);
    261:   PetscErrorCode (*view)(VecScatter,PetscViewer);
    262:   void           *fromdata,*todata;
    263:   void           *spptr;
    264: };

    266: PETSC_INTERN PetscErrorCode VecStashCreate_Private(MPI_Comm,PetscInt,VecStash*);
    267: PETSC_INTERN PetscErrorCode VecStashDestroy_Private(VecStash*);
    268: PETSC_INTERN PetscErrorCode VecStashExpand_Private(VecStash*,PetscInt);
    269: PETSC_INTERN PetscErrorCode VecStashScatterEnd_Private(VecStash*);
    270: PETSC_INTERN PetscErrorCode VecStashSetInitialSize_Private(VecStash*,PetscInt);
    271: PETSC_INTERN PetscErrorCode VecStashGetInfo_Private(VecStash*,PetscInt*,PetscInt*);
    272: PETSC_INTERN PetscErrorCode VecStashScatterBegin_Private(VecStash*,PetscInt*);
    273: PETSC_INTERN PetscErrorCode VecStashScatterGetMesg_Private(VecStash*,PetscMPIInt*,PetscInt**,PetscScalar**,PetscInt*);

    275: /*
    276:   VecStashValue_Private - inserts a single value into the stash.

    278:   Input Parameters:
    279:   stash  - the stash
    280:   idx    - the global of the inserted value
    281:   values - the value inserted
    282: */
    283: PETSC_STATIC_INLINE PetscErrorCode VecStashValue_Private(VecStash *stash,PetscInt row,PetscScalar value)
    284: {
    286:   /* Check and see if we have sufficient memory */
    287:   if (((stash)->n + 1) > (stash)->nmax) {
    288:     VecStashExpand_Private(stash,1);
    289:   }
    290:   (stash)->idx[(stash)->n]   = row;
    291:   (stash)->array[(stash)->n] = value;
    292:   (stash)->n++;
    293:   return 0;
    294: }

    296: /*
    297:   VecStashValuesBlocked_Private - inserts 1 block of values into the stash.

    299:   Input Parameters:
    300:   stash  - the stash
    301:   idx    - the global block index
    302:   values - the values inserted
    303: */
    304: PETSC_STATIC_INLINE PetscErrorCode VecStashValuesBlocked_Private(VecStash *stash,PetscInt row,PetscScalar *values)
    305: {
    306:   PetscInt       jj,stash_bs=(stash)->bs;
    307:   PetscScalar    *array;
    309:   if (((stash)->n+1) > (stash)->nmax) {
    310:     VecStashExpand_Private(stash,1);
    311:   }
    312:   array = (stash)->array + stash_bs*(stash)->n;
    313:   (stash)->idx[(stash)->n]   = row;
    314:   for (jj=0; jj<stash_bs; jj++) { array[jj] = values[jj];}
    315:   (stash)->n++;
    316:   return 0;
    317: }

    319: PETSC_INTERN PetscErrorCode VecStrideGather_Default(Vec,PetscInt,Vec,InsertMode);
    320: PETSC_INTERN PetscErrorCode VecStrideScatter_Default(Vec,PetscInt,Vec,InsertMode);
    321: PETSC_INTERN PetscErrorCode VecReciprocal_Default(Vec);

    323: #if defined(PETSC_HAVE_MATLAB_ENGINE)
    324: PETSC_EXTERN PetscErrorCode VecMatlabEnginePut_Default(PetscObject,void*);
    325: PETSC_EXTERN PetscErrorCode VecMatlabEngineGet_Default(PetscObject,void*);
    326: #endif

    329: /* Reset __FUNCT__ in case the user does not define it themselves */

    333: #endif