Actual source code: veccusp.c
petsc-3.7.1 2016-05-15
Report Typos and Errors

      1: /*
      2:  Implementation of the sequential cusp vectors.

      4:  This file contains the code that can be compiled with a C
      5:  compiler.  The companion file veccusp2.cu contains the code that
      6:  must be compiled with nvcc or a C++ compiler.
      7:  */

      9: #define PETSC_SKIP_COMPLEX
     10: #define PETSC_SKIP_SPINLOCK

     12: #include <petscconf.h>
     13: #include <petsc/private/vecimpl.h>          /*I <petscvec.h> I*/
     14: #include <../src/vec/vec/impls/dvecimpl.h>
     15: #include <../src/vec/vec/impls/seq/seqcusp/cuspvecimpl.h>

     20: /*
     21:     Allocates space for the vector array on the Host if it does not exist.
     22:     Does NOT change the PetscCUSPFlag for the vector
     23:     Does NOT zero the CUSP array
     24:  */
     25: PetscErrorCode VecCUSPAllocateCheckHost(Vec v)
     26: {
     28:   PetscScalar    *array;
     29:   Vec_Seq        *s = (Vec_Seq*)v->data;
     30:   PetscInt       n = v->map->n;

     33:   if (!s) {
     34:     PetscNewLog((PetscObject)v,&s);
     35:     v->data = s;
     36:   }
     37:   if (!s->array) {
     38:     PetscMalloc1(n,&array);
     39:     PetscLogObjectMemory((PetscObject)v,n*sizeof(PetscScalar));
     40:     s->array           = array;
     41:     s->array_allocated = array;
     42:     if (v->valid_GPU_array == PETSC_CUSP_UNALLOCATED) {
     43:       v->valid_GPU_array = PETSC_CUSP_CPU;
     44:     }
     45:   }
     46:   return(0);
     47: }

     51: PetscErrorCode VecCopy_SeqCUSP_Private(Vec xin,Vec yin)
     52: {
     53:   PetscScalar       *ya;
     54:   const PetscScalar *xa;
     55:   PetscErrorCode    ierr;

     58:   VecCUSPAllocateCheckHost(xin);
     59:   VecCUSPAllocateCheckHost(yin);
     60:   if (xin != yin) {
     61:     VecGetArrayRead(xin,&xa);
     62:     VecGetArray(yin,&ya);
     63:     PetscMemcpy(ya,xa,xin->map->n*sizeof(PetscScalar));
     64:     VecRestoreArrayRead(xin,&xa);
     65:     VecRestoreArray(yin,&ya);
     66:   }
     67:   return(0);
     68: }

     72: PetscErrorCode VecSetRandom_SeqCUSP_Private(Vec xin,PetscRandom r)
     73: {
     75:   PetscInt       n = xin->map->n,i;
     76:   PetscScalar    *xx;

     79:   VecGetArray(xin,&xx);
     80:   for (i=0; i<n; i++) {PetscRandomGetValue(r,&xx[i]);}
     81:   VecRestoreArray(xin,&xx);
     82:   return(0);
     83: }

     87: PetscErrorCode VecDestroy_SeqCUSP_Private(Vec v)
     88: {
     89:   Vec_Seq        *vs = (Vec_Seq*)v->data;

     93:   PetscObjectSAWsViewOff(v);
     94: #if defined(PETSC_USE_LOG)
     95:   PetscLogObjectState((PetscObject)v,"Length=%D",v->map->n);
     96: #endif
     97:   if (vs) {
     98:     if (vs->array_allocated) PetscFree(vs->array_allocated);
     99:     PetscFree(vs);
    100:   }
    101:   return(0);
    102: }

    106: PetscErrorCode VecResetArray_SeqCUSP_Private(Vec vin)
    107: {
    108:   Vec_Seq *v = (Vec_Seq*)vin->data;

    111:   v->array         = v->unplacedarray;
    112:   v->unplacedarray = 0;
    113:   return(0);
    114: }

    118: PetscErrorCode VecCUSPAllocateCheck_Public(Vec v)
    119: {

    123:   VecCUSPAllocateCheck(v);
    124:   return(0);
    125: }

    129: PetscErrorCode VecCUSPCopyToGPU_Public(Vec v)
    130: {

    134:   VecCUSPCopyToGPU(v);
    135:   return(0);
    136: }

    140: /*
    141:     VecCUSPCopyToGPUSome_Public - Copies certain entries down to the GPU from the CPU of a vector

    143:    Input Parameters:
    144: .    v - the vector
    145: .    indices - the requested indices, this should be created with CUSPIndicesCreate()

    147: */
    148: PetscErrorCode VecCUSPCopyToGPUSome_Public(Vec v,PetscCUSPIndices ci)
    149: {

    153:   VecCUSPCopyToGPUSome(v,ci);
    154:   return(0);
    155: }

    159: /*
    160:   VecCUSPCopyFromGPUSome_Public - Copies certain entries up to the CPU from the GPU of a vector

    162:   Input Parameters:
    163:  +    v - the vector
    164:  -    indices - the requested indices, this should be created with CUSPIndicesCreate()
    165: */
    166: PetscErrorCode VecCUSPCopyFromGPUSome_Public(Vec v,PetscCUSPIndices ci)
    167: {

    171:   VecCUSPCopyFromGPUSome(v,ci);
    172:   return(0);
    173: }

    177: PetscErrorCode VecSetRandom_SeqCUSP(Vec xin,PetscRandom r)
    178: {

    182:   VecSetRandom_SeqCUSP_Private(xin,r);
    183:   xin->valid_GPU_array = PETSC_CUSP_CPU;
    184:   return(0);
    185: }

    189: PetscErrorCode VecResetArray_SeqCUSP(Vec vin)
    190: {

    194:   VecCUSPCopyFromGPU(vin);
    195:   VecResetArray_SeqCUSP_Private(vin);
    196:   vin->valid_GPU_array = PETSC_CUSP_CPU;
    197:   return(0);
    198: }

    202: PetscErrorCode VecPlaceArray_SeqCUSP(Vec vin,const PetscScalar *a)
    203: {

    207:   VecCUSPCopyFromGPU(vin);
    208:   VecPlaceArray_Seq(vin,a);
    209:   vin->valid_GPU_array = PETSC_CUSP_CPU;
    210:   return(0);
    211: }

    215: PetscErrorCode VecReplaceArray_SeqCUSP(Vec vin,const PetscScalar *a)
    216: {

    220:   VecCUSPCopyFromGPU(vin);
    221:   VecReplaceArray_Seq(vin,a);
    222:   vin->valid_GPU_array = PETSC_CUSP_CPU;
    223:   return(0);
    224: }

    228: /*@
    229:  VecCreateSeqCUSP - Creates a standard, sequential array-style vector.

    231:  Collective on MPI_Comm

    233:  Input Parameter:
    234:  .  comm - the communicator, should be PETSC_COMM_SELF
    235:  .  n - the vector length

    237:  Output Parameter:
    238:  .  V - the vector

    240:  Notes:
    241:  Use VecDuplicate() or VecDuplicateVecs() to form additional vectors of the
    242:  same type as an existing vector.

    244:  Level: intermediate

    246:  Concepts: vectors^creating sequential

    248:  .seealso: VecCreateMPI(), VecCreate(), VecDuplicate(), VecDuplicateVecs(), VecCreateGhost()
    249:  @*/
    250: PetscErrorCode VecCreateSeqCUSP(MPI_Comm comm,PetscInt n,Vec *v)
    251: {

    255:   VecCreate(comm,v);
    256:   VecSetSizes(*v,n,n);
    257:   VecSetType(*v,VECSEQCUSP);
    258:   return(0);
    259: }

    263: PetscErrorCode VecDuplicate_SeqCUSP(Vec win,Vec *V)
    264: {

    268:   VecCreateSeqCUSP(PetscObjectComm((PetscObject)win),win->map->n,V);
    269:   PetscLayoutReference(win->map,&(*V)->map);
    270:   PetscObjectListDuplicate(((PetscObject)win)->olist,&((PetscObject)(*V))->olist);
    271:   PetscFunctionListDuplicate(((PetscObject)win)->qlist,&((PetscObject)(*V))->qlist);
    272:   (*V)->stash.ignorenegidx = win->stash.ignorenegidx;
    273:   return(0);
    274: }

    278: PETSC_EXTERN PetscErrorCode VecCreate_SeqCUSP(Vec V)
    279: {
    281:   PetscMPIInt    size;

    284:   MPI_Comm_size(PetscObjectComm((PetscObject)V),&size);
    285:   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot create VECSEQCUSP on more than one process");
    286:   VecCreate_Seq_Private(V,0);
    287:   PetscObjectChangeTypeName((PetscObject)V,VECSEQCUSP);

    289:   V->ops->dot                    = VecDot_SeqCUSP;
    290:   V->ops->norm                   = VecNorm_SeqCUSP;
    291:   V->ops->tdot                   = VecTDot_SeqCUSP;
    292:   V->ops->scale                  = VecScale_SeqCUSP;
    293:   V->ops->copy                   = VecCopy_SeqCUSP;
    294:   V->ops->set                    = VecSet_SeqCUSP;
    295:   V->ops->swap                   = VecSwap_SeqCUSP;
    296:   V->ops->axpy                   = VecAXPY_SeqCUSP;
    297:   V->ops->axpby                  = VecAXPBY_SeqCUSP;
    298:   V->ops->axpbypcz               = VecAXPBYPCZ_SeqCUSP;
    299:   V->ops->pointwisemult          = VecPointwiseMult_SeqCUSP;
    300:   V->ops->pointwisedivide        = VecPointwiseDivide_SeqCUSP;
    301:   V->ops->setrandom              = VecSetRandom_SeqCUSP;
    302:   V->ops->dot_local              = VecDot_SeqCUSP;
    303:   V->ops->tdot_local             = VecTDot_SeqCUSP;
    304:   V->ops->norm_local             = VecNorm_SeqCUSP;
    305:   V->ops->mdot_local             = VecMDot_SeqCUSP;
    306:   V->ops->maxpy                  = VecMAXPY_SeqCUSP;
    307:   V->ops->mdot                   = VecMDot_SeqCUSP;
    308:   V->ops->aypx                   = VecAYPX_SeqCUSP;
    309:   V->ops->waxpy                  = VecWAXPY_SeqCUSP;
    310:   V->ops->dotnorm2               = VecDotNorm2_SeqCUSP;
    311:   V->ops->placearray             = VecPlaceArray_SeqCUSP;
    312:   V->ops->replacearray           = VecReplaceArray_SeqCUSP;
    313:   V->ops->resetarray             = VecResetArray_SeqCUSP;
    314:   V->ops->destroy                = VecDestroy_SeqCUSP;
    315:   V->ops->duplicate              = VecDuplicate_SeqCUSP;
    316:   V->ops->conjugate              = VecConjugate_SeqCUSP;
    317:   V->ops->getlocalvector         = VecGetLocalVector_SeqCUSP;
    318:   V->ops->restorelocalvector     = VecRestoreLocalVector_SeqCUSP;
    319:   V->ops->getlocalvectorread     = VecGetLocalVector_SeqCUSP;
    320:   V->ops->restorelocalvectorread = VecRestoreLocalVector_SeqCUSP;

    322:   VecCUSPAllocateCheck(V);
    323:   V->valid_GPU_array = PETSC_CUSP_GPU;
    324:   VecSet(V,0.0);
    325:   return(0);
    326: }