Actual source code: vecimpl.h
petsc-3.6.2 2015-10-02
Report Typos and Errors

      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>

     15: PETSC_EXTERN PetscBool VecRegisterAllCalled;
     16: PETSC_EXTERN PetscErrorCode VecRegisterAll(void);

     18: /* ----------------------------------------------------------------------------*/

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

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

    104: typedef struct {
    105:   PetscInt      nmax;                   /* maximum stash size */
    106:   PetscInt      umax;                   /* max stash size user wants */
    107:   PetscInt      oldnmax;                /* the nmax value used previously */
    108:   PetscInt      n;                      /* stash size */
    109:   PetscInt      bs;                     /* block size of the stash */
    110:   PetscInt      reallocs;               /* preserve the no of mallocs invoked */
    111:   PetscInt      *idx;                   /* global row numbers in stash */
    112:   PetscScalar   *array;                 /* array to hold stashed values */
    113:   /* The following variables are used for communication */
    114:   MPI_Comm      comm;
    115:   PetscMPIInt   size,rank;
    116:   PetscMPIInt   tag1,tag2;
    117:   MPI_Request   *send_waits;            /* array of send requests */
    118:   MPI_Request   *recv_waits;            /* array of receive requests */
    119:   MPI_Status    *send_status;           /* array of send status */
    120:   PetscInt      nsends,nrecvs;          /* numbers of sends and receives */
    121:   PetscScalar   *svalues,*rvalues;      /* sending and receiving data */
    122:   PetscInt      *sindices,*rindices;
    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:   PetscBool     donotstash;
    127:   PetscBool     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:   PetscLayout            map;
    135:   void                   *data;     /* implementation-specific data */
    136:   PetscBool              array_gotten;
    137:   VecStash               stash,bstash; /* used for storing off-proc values during assembly */
    138:   PetscBool              petscnative;  /* means the ->data starts with VECHEADER and can use VecGetArrayFast()*/
    139:   PetscInt               lock;   /* vector is locked to read only */
    140: #if defined(PETSC_HAVE_CUSP)
    141:   PetscCUSPFlag          valid_GPU_array;    /* indicates where the most recently modified vector data is (GPU or CPU) */
    142:   void                   *spptr; /* if we're using CUSP, then this is the special pointer to the array on the GPU */
    143: #endif
    144: #if defined(PETSC_HAVE_VIENNACL)
    145:   PetscViennaCLFlag      valid_GPU_array;    /* indicates where the most recently modified vector data is (GPU or CPU) */
    146:   void                   *spptr; /* if we're using ViennaCL, then this is the special pointer to the array on the GPU */
    147: #endif
    148: };

    150: PETSC_EXTERN PetscLogEvent VEC_View, VEC_Max, VEC_Min, VEC_DotBarrier, VEC_Dot, VEC_MDotBarrier, VEC_MDot, VEC_TDot, VEC_MTDot;
    151: PETSC_EXTERN PetscLogEvent VEC_Norm, VEC_Normalize, VEC_Scale, VEC_Copy, VEC_Set, VEC_AXPY, VEC_AYPX, VEC_WAXPY, VEC_MAXPY;
    152: PETSC_EXTERN PetscLogEvent VEC_AssemblyEnd, VEC_PointwiseMult, VEC_SetValues, VEC_Load, VEC_ScatterBarrier, VEC_ScatterBegin, VEC_ScatterEnd;
    153: PETSC_EXTERN PetscLogEvent VEC_SetRandom, VEC_ReduceArithmetic, VEC_ReduceBarrier, VEC_ReduceCommunication;
    154: PETSC_EXTERN PetscLogEvent VEC_ReduceBegin,VEC_ReduceEnd;
    155: PETSC_EXTERN PetscLogEvent VEC_Swap, VEC_AssemblyBegin, VEC_NormBarrier, VEC_DotNormBarrier, VEC_DotNorm, VEC_AXPBYPCZ, VEC_Ops;
    156: PETSC_EXTERN PetscLogEvent VEC_CUSPCopyToGPU, VEC_CUSPCopyFromGPU;
    157: PETSC_EXTERN PetscLogEvent VEC_CUSPCopyToGPUSome, VEC_CUSPCopyFromGPUSome;
    158: PETSC_EXTERN PetscLogEvent VEC_ViennaCLCopyToGPU,     VEC_ViennaCLCopyFromGPU;

    160: #if defined(PETSC_HAVE_CUSP)
    161: PETSC_EXTERN PetscErrorCode VecCUSPAllocateCheckHost(Vec v);
    162: PETSC_EXTERN PetscErrorCode VecCUSPCopyFromGPU(Vec v);
    163: #endif

    165: #if defined(PETSC_HAVE_VIENNACL)
    166: PETSC_EXTERN PetscErrorCode VecViennaCLAllocateCheckHost(Vec v);
    167: PETSC_EXTERN PetscErrorCode VecViennaCLCopyFromGPU(Vec v);
    168: #endif

    171: /*
    172:      Common header shared by array based vectors,
    173:    currently Vec_Seq and Vec_MPI
    174: */
    175: #define VECHEADER                          \
    176:   PetscScalar *array;                      \
    177:   PetscScalar *array_allocated;                        /* if the array was allocated by PETSc this is its pointer */  \
    178:   PetscScalar *unplacedarray;                           /* if one called VecPlaceArray(), this is where it stashed the original */

    180: /* Default obtain and release vectors; can be used by any implementation */
    181: PETSC_INTERN PetscErrorCode VecDuplicateVecs_Default(Vec,PetscInt,Vec *[]);
    182: PETSC_INTERN PetscErrorCode VecDestroyVecs_Default(PetscInt,Vec []);
    183: PETSC_INTERN PetscErrorCode VecLoad_Binary(Vec, PetscViewer);
    184: PETSC_EXTERN PetscErrorCode VecLoad_Default(Vec, PetscViewer);

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

    188: /* --------------------------------------------------------------------*/
    189: /*                                                                     */
    190: /* Defines the data structures used in the Vec Scatter operations      */

    192: typedef enum { VEC_SCATTER_SEQ_GENERAL,VEC_SCATTER_SEQ_STRIDE,
    193:                VEC_SCATTER_MPI_GENERAL,VEC_SCATTER_MPI_TOALL,
    194:                VEC_SCATTER_MPI_TOONE} VecScatterType;

    196: #define VECSCATTER_IMPL_HEADER \
    197:       VecScatterType type;

    199: typedef struct {
    200:   VECSCATTER_IMPL_HEADER
    201: } VecScatter_Common;

    203: /*
    204:    These scatters are for the purely local case.
    205: */
    206: typedef struct {
    207:   VECSCATTER_IMPL_HEADER
    208:   PetscInt       n;                    /* number of components to scatter */
    209:   PetscInt       *vslots;              /* locations of components */
    210:   /*
    211:        The next three fields are used in parallel scatters, they contain
    212:        optimization in the special case that the "to" vector and the "from"
    213:        vector are the same, so one only needs copy components that truly
    214:        copies instead of just y[idx[i]] = y[jdx[i]] where idx[i] == jdx[i].
    215:   */
    216:   PetscBool      nonmatching_computed;
    217:   PetscInt       n_nonmatching;        /* number of "from"s  != "to"s */
    218:   PetscInt       *slots_nonmatching;   /* locations of "from"s  != "to"s */
    219:   PetscBool      is_copy;
    220:   PetscInt       copy_start;   /* local scatter is a copy starting at copy_start */
    221:   PetscInt       copy_length;
    222: } VecScatter_Seq_General;

    224: typedef struct {
    225:   VECSCATTER_IMPL_HEADER
    226:   PetscInt       n;
    227:   PetscInt       first;
    228:   PetscInt       step;
    229: } VecScatter_Seq_Stride;

    231: /*
    232:    This scatter is for a global vector copied (completely) to each processor (or all to one)
    233: */
    234: typedef struct {
    235:   VECSCATTER_IMPL_HEADER
    236:   PetscMPIInt    *count;        /* elements of vector on each processor */
    237:   PetscMPIInt    *displx;
    238:   PetscScalar    *work1;
    239:   PetscScalar    *work2;
    240: } VecScatter_MPI_ToAll;

    242: /*
    243:    This is the general parallel scatter
    244: */
    245: typedef struct {
    246:   VECSCATTER_IMPL_HEADER
    247:   PetscInt               n;        /* number of processors to send/receive */
    248:   PetscInt               *starts;  /* starting point in indices and values for each proc*/
    249:   PetscInt               *indices; /* list of all components sent or received */
    250:   PetscMPIInt            *procs;   /* processors we are communicating with in scatter */
    251:   MPI_Request            *requests,*rev_requests;
    252:   PetscScalar            *values;  /* buffer for all sends or receives */
    253:   VecScatter_Seq_General local;    /* any part that happens to be local */
    254:   MPI_Status             *sstatus,*rstatus;
    255:   PetscBool              use_readyreceiver;
    256:   PetscInt               bs;
    257:   PetscBool              sendfirst;
    258:   PetscBool              contiq;
    259:   /* for MPI_Alltoallv() approach */
    260:   PetscBool              use_alltoallv;
    261:   PetscMPIInt            *counts,*displs;
    262:   /* for MPI_Alltoallw() approach */
    263:   PetscBool              use_alltoallw;
    264: #if defined(PETSC_HAVE_MPI_ALLTOALLW)
    265:   PetscMPIInt            *wcounts,*wdispls;
    266:   MPI_Datatype           *types;
    267: #endif
    268:   PetscBool              use_window;
    269: #if defined(PETSC_HAVE_MPI_WIN_CREATE)
    270:   MPI_Win                window;
    271:   PetscInt               *winstarts;    /* displacements in the processes I am putting to */
    272: #endif
    273: } VecScatter_MPI_General;

    276: PETSC_INTERN PetscErrorCode VecScatterGetTypes_Private(VecScatter,VecScatterType*,VecScatterType*);
    277: PETSC_INTERN PetscErrorCode VecScatterIsSequential_Private(VecScatter_Common*,PetscBool*);

    279: typedef struct _VecScatterOps *VecScatterOps;
    280: struct _VecScatterOps {
    281:   PetscErrorCode (*begin)(VecScatter,Vec,Vec,InsertMode,ScatterMode);
    282:   PetscErrorCode (*end)(VecScatter,Vec,Vec,InsertMode,ScatterMode);
    283:   PetscErrorCode (*copy)(VecScatter,VecScatter);
    284:   PetscErrorCode (*destroy)(VecScatter);
    285:   PetscErrorCode (*view)(VecScatter,PetscViewer);
    286:   PetscErrorCode (*viewfromoptions)(VecScatter,const char prefix[],const char name[]);
    287:   PetscErrorCode (*remap)(VecScatter,PetscInt *,PetscInt*);
    288:   PetscErrorCode (*getmerged)(VecScatter,PetscBool *);
    289: };

    291: struct _p_VecScatter {
    292:   PETSCHEADER(struct _VecScatterOps);
    293:   PetscInt       to_n,from_n;
    294:   PetscBool      inuse;                /* prevents corruption from mixing two scatters */
    295:   PetscBool      beginandendtogether;  /* indicates that the scatter begin and end  function are called together, VecScatterEnd()
    296:                                           is then treated as a nop */
    297:   PetscBool      packtogether;         /* packs all the messages before sending, same with receive */
    298:   PetscBool      reproduce;            /* always receive the ghost points in the same order of processes */
    299:   void           *fromdata,*todata;
    300:   void           *spptr;
    301: };

    303: PETSC_INTERN PetscErrorCode VecStashCreate_Private(MPI_Comm,PetscInt,VecStash*);
    304: PETSC_INTERN PetscErrorCode VecStashDestroy_Private(VecStash*);
    305: PETSC_INTERN PetscErrorCode VecStashExpand_Private(VecStash*,PetscInt);
    306: PETSC_INTERN PetscErrorCode VecStashScatterEnd_Private(VecStash*);
    307: PETSC_INTERN PetscErrorCode VecStashSetInitialSize_Private(VecStash*,PetscInt);
    308: PETSC_INTERN PetscErrorCode VecStashGetInfo_Private(VecStash*,PetscInt*,PetscInt*);
    309: PETSC_INTERN PetscErrorCode VecStashScatterBegin_Private(VecStash*,PetscInt*);
    310: PETSC_INTERN PetscErrorCode VecStashScatterGetMesg_Private(VecStash*,PetscMPIInt*,PetscInt**,PetscScalar**,PetscInt*);

    312: /*
    313:   VecStashValue_Private - inserts a single value into the stash.

    315:   Input Parameters:
    316:   stash  - the stash
    317:   idx    - the global of the inserted value
    318:   values - the value inserted
    319: */
    320: PETSC_STATIC_INLINE PetscErrorCode VecStashValue_Private(VecStash *stash,PetscInt row,PetscScalar value)
    321: {
    323:   /* Check and see if we have sufficient memory */
    324:   if (((stash)->n + 1) > (stash)->nmax) {
    325:     VecStashExpand_Private(stash,1);
    326:   }
    327:   (stash)->idx[(stash)->n]   = row;
    328:   (stash)->array[(stash)->n] = value;
    329:   (stash)->n++;
    330:   return 0;
    331: }

    333: /*
    334:   VecStashValuesBlocked_Private - inserts 1 block of values into the stash.

    336:   Input Parameters:
    337:   stash  - the stash
    338:   idx    - the global block index
    339:   values - the values inserted
    340: */
    341: PETSC_STATIC_INLINE PetscErrorCode VecStashValuesBlocked_Private(VecStash *stash,PetscInt row,PetscScalar *values)
    342: {
    343:   PetscInt       jj,stash_bs=(stash)->bs;
    344:   PetscScalar    *array;
    346:   if (((stash)->n+1) > (stash)->nmax) {
    347:     VecStashExpand_Private(stash,1);
    348:   }
    349:   array = (stash)->array + stash_bs*(stash)->n;
    350:   (stash)->idx[(stash)->n]   = row;
    351:   for (jj=0; jj<stash_bs; jj++) { array[jj] = values[jj];}
    352:   (stash)->n++;
    353:   return 0;
    354: }

    356: PETSC_INTERN PetscErrorCode VecStrideGather_Default(Vec,PetscInt,Vec,InsertMode);
    357: PETSC_INTERN PetscErrorCode VecStrideScatter_Default(Vec,PetscInt,Vec,InsertMode);
    358: PETSC_INTERN PetscErrorCode VecReciprocal_Default(Vec);
    359: PETSC_INTERN PetscErrorCode VecStrideSubSetGather_Default(Vec,PetscInt,const PetscInt[],const PetscInt[],Vec,InsertMode);
    360: PETSC_INTERN PetscErrorCode VecStrideSubSetScatter_Default(Vec,PetscInt,const PetscInt[],const PetscInt[],Vec,InsertMode);

    362: #if defined(PETSC_HAVE_MATLAB_ENGINE)
    363: PETSC_EXTERN PetscErrorCode VecMatlabEnginePut_Default(PetscObject,void*);
    364: PETSC_EXTERN PetscErrorCode VecMatlabEngineGet_Default(PetscObject,void*);
    365: #endif

    367: PETSC_EXTERN PetscErrorCode PetscSectionGetField_Internal(PetscSection, PetscSection, Vec, PetscInt, PetscInt, PetscInt, IS *, Vec *);
    368: PETSC_EXTERN PetscErrorCode PetscSectionRestoreField_Internal(PetscSection, PetscSection, Vec, PetscInt, PetscInt, PetscInt, IS *, Vec *);

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

    374: #endif