Actual source code: mpiviennacl.cxx

      1: /*
      2:    This file contains routines for Parallel vector operations.
      3:  */
      4: #include <petscconf.h>
      5: #include <../src/vec/vec/impls/mpi/pvecimpl.h>
      6: #include <../src/vec/vec/impls/seq/seqviennacl/viennaclvecimpl.h>

      8: /*MC
      9:    VECVIENNACL - VECVIENNACL = "viennacl" - A `VECSEQVIENNACL` on a single-process communicator, and `VECMPIVIENNACL` otherwise.

     11:    Options Database Keys:
     12: . -vec_type viennacl - sets the vector type to V`ECVIENNACL` during a call to `VecSetFromOptions()`

     14:   Level: beginner

     16: .seealso: `VecCreate()`, `VecSetType()`, `VecSetFromOptions()`, `VecCreateMPIWithArray()`, `VECSEQVIENNACL`, `VECMPIVIENNACL`, `VECSTANDARD`, `VecType`, `VecCreateMPI()`
     17: M*/

     19: static PetscErrorCode VecDestroy_MPIViennaCL(Vec v)
     20: {
     21:   PetscFunctionBegin;
     22:   try {
     23:     if (v->spptr) {
     24:       delete ((Vec_ViennaCL *)v->spptr)->GPUarray_allocated;
     25:       delete (Vec_ViennaCL *)v->spptr;
     26:     }
     27:   } catch (std::exception const &ex) {
     28:     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "ViennaCL error: %s", ex.what());
     29:   }
     30:   PetscCall(VecDestroy_MPI(v));
     31:   PetscFunctionReturn(PETSC_SUCCESS);
     32: }

     34: static PetscErrorCode VecNorm_MPIViennaCL(Vec xin, NormType type, PetscReal *z)
     35: {
     36:   PetscReal sum, work = 0.0;

     38:   PetscFunctionBegin;
     39:   if (type == NORM_2 || type == NORM_FROBENIUS) {
     40:     PetscCall(VecNorm_SeqViennaCL(xin, NORM_2, &work));
     41:     work *= work;
     42:     PetscCallMPI(MPIU_Allreduce(&work, &sum, 1, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)xin)));
     43:     *z = PetscSqrtReal(sum);
     44:   } else if (type == NORM_1) {
     45:     /* Find the local part */
     46:     PetscCall(VecNorm_SeqViennaCL(xin, NORM_1, &work));
     47:     /* Find the global max */
     48:     PetscCallMPI(MPIU_Allreduce(&work, z, 1, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)xin)));
     49:   } else if (type == NORM_INFINITY) {
     50:     /* Find the local max */
     51:     PetscCall(VecNorm_SeqViennaCL(xin, NORM_INFINITY, &work));
     52:     /* Find the global max */
     53:     PetscCallMPI(MPIU_Allreduce(&work, z, 1, MPIU_REAL, MPIU_MAX, PetscObjectComm((PetscObject)xin)));
     54:   } else if (type == NORM_1_AND_2) {
     55:     PetscReal temp[2];
     56:     PetscCall(VecNorm_SeqViennaCL(xin, NORM_1, temp));
     57:     PetscCall(VecNorm_SeqViennaCL(xin, NORM_2, temp + 1));
     58:     temp[1] = temp[1] * temp[1];
     59:     PetscCallMPI(MPIU_Allreduce(temp, z, 2, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)xin)));
     60:     z[1] = PetscSqrtReal(z[1]);
     61:   }
     62:   PetscFunctionReturn(PETSC_SUCCESS);
     63: }

     65: static PetscErrorCode VecDot_MPIViennaCL(Vec xin, Vec yin, PetscScalar *z)
     66: {
     67:   PetscScalar sum, work;

     69:   PetscFunctionBegin;
     70:   PetscCall(VecDot_SeqViennaCL(xin, yin, &work));
     71:   PetscCallMPI(MPIU_Allreduce(&work, &sum, 1, MPIU_SCALAR, MPIU_SUM, PetscObjectComm((PetscObject)xin)));
     72:   *z = sum;
     73:   PetscFunctionReturn(PETSC_SUCCESS);
     74: }

     76: static PetscErrorCode VecTDot_MPIViennaCL(Vec xin, Vec yin, PetscScalar *z)
     77: {
     78:   PetscScalar sum, work;

     80:   PetscFunctionBegin;
     81:   PetscCall(VecTDot_SeqViennaCL(xin, yin, &work));
     82:   PetscCallMPI(MPIU_Allreduce(&work, &sum, 1, MPIU_SCALAR, MPIU_SUM, PetscObjectComm((PetscObject)xin)));
     83:   *z = sum;
     84:   PetscFunctionReturn(PETSC_SUCCESS);
     85: }

     87: static PetscErrorCode VecMDot_MPIViennaCL(Vec xin, PetscInt nv, const Vec y[], PetscScalar *z)
     88: {
     89:   PetscScalar awork[128], *work = awork;

     91:   PetscFunctionBegin;
     92:   if (nv > 128) PetscCall(PetscMalloc1(nv, &work));
     93:   PetscCall(VecMDot_SeqViennaCL(xin, nv, y, work));
     94:   PetscCallMPI(MPIU_Allreduce(work, z, nv, MPIU_SCALAR, MPIU_SUM, PetscObjectComm((PetscObject)xin)));
     95:   if (nv > 128) PetscCall(PetscFree(work));
     96:   PetscFunctionReturn(PETSC_SUCCESS);
     97: }

     99: /*MC
    100:    VECMPIVIENNACL - VECMPIVIENNACL = "mpiviennacl" - The basic parallel vector, modified to use ViennaCL

    102:    Options Database Keys:
    103: . -vec_type mpiviennacl - sets the vector type to VECMPIVIENNACL during a call to VecSetFromOptions()

    105:   Level: beginner

    107: .seealso: `VecCreate()`, `VecSetType()`, `VecSetFromOptions()`, `VecCreateMPIWithArray()`, `VECMPI`, `VecType`, `VecCreateMPI()`
    108: M*/

    110: static PetscErrorCode VecDuplicate_MPIViennaCL(Vec win, Vec *v)
    111: {
    112:   Vec_MPI     *vw, *w = (Vec_MPI *)win->data;
    113:   PetscScalar *array;

    115:   PetscFunctionBegin;
    116:   PetscCall(VecCreate(PetscObjectComm((PetscObject)win), v));
    117:   PetscCall(PetscLayoutReference(win->map, &(*v)->map));

    119:   PetscCall(VecCreate_MPI_Private(*v, PETSC_FALSE, w->nghost, 0));
    120:   vw           = (Vec_MPI *)(*v)->data;
    121:   (*v)->ops[0] = win->ops[0];

    123:   /* save local representation of the parallel vector (and scatter) if it exists */
    124:   if (w->localrep) {
    125:     PetscCall(VecGetArray(*v, &array));
    126:     PetscCall(VecCreateSeqWithArray(PETSC_COMM_SELF, 1, win->map->n + w->nghost, array, &vw->localrep));
    127:     vw->localrep->ops[0] = w->localrep->ops[0];
    128:     PetscCall(VecRestoreArray(*v, &array));
    129:     vw->localupdate = w->localupdate;
    130:     PetscCall(PetscObjectReference((PetscObject)vw->localupdate));
    131:   }

    133:   /* New vector should inherit stashing property of parent */
    134:   (*v)->stash.donotstash   = win->stash.donotstash;
    135:   (*v)->stash.ignorenegidx = win->stash.ignorenegidx;

    137:   /* change type_name appropriately */
    138:   PetscCall(PetscObjectChangeTypeName((PetscObject)*v, VECMPIVIENNACL));

    140:   PetscCall(PetscObjectListDuplicate(((PetscObject)win)->olist, &((PetscObject)*v)->olist));
    141:   PetscCall(PetscFunctionListDuplicate(((PetscObject)win)->qlist, &((PetscObject)*v)->qlist));
    142:   (*v)->map->bs   = win->map->bs;
    143:   (*v)->bstash.bs = win->bstash.bs;
    144:   PetscFunctionReturn(PETSC_SUCCESS);
    145: }

    147: static PetscErrorCode VecDotNorm2_MPIViennaCL(Vec s, Vec t, PetscScalar *dp, PetscScalar *nm)
    148: {
    149:   PetscScalar work[2], sum[2];

    151:   PetscFunctionBegin;
    152:   PetscCall(VecDotNorm2_SeqViennaCL(s, t, work, work + 1));
    153:   PetscCallMPI(MPIU_Allreduce((void *)&work, (void *)&sum, 2, MPIU_SCALAR, MPIU_SUM, PetscObjectComm((PetscObject)s)));
    154:   *dp = sum[0];
    155:   *nm = sum[1];
    156:   PetscFunctionReturn(PETSC_SUCCESS);
    157: }

    159: static PetscErrorCode VecBindToCPU_MPIViennaCL(Vec vv, PetscBool bind)
    160: {
    161:   PetscFunctionBegin;
    162:   vv->boundtocpu = bind;

    164:   if (bind) {
    165:     PetscCall(VecViennaCLCopyFromGPU(vv));
    166:     vv->offloadmask                 = PETSC_OFFLOAD_CPU; /* since the CPU code will likely change values in the vector */
    167:     vv->ops->dotnorm2               = NULL;
    168:     vv->ops->waxpy                  = VecWAXPY_Seq;
    169:     vv->ops->dot                    = VecDot_MPI;
    170:     vv->ops->mdot                   = VecMDot_MPI;
    171:     vv->ops->tdot                   = VecTDot_MPI;
    172:     vv->ops->norm                   = VecNorm_MPI;
    173:     vv->ops->scale                  = VecScale_Seq;
    174:     vv->ops->copy                   = VecCopy_Seq;
    175:     vv->ops->set                    = VecSet_Seq;
    176:     vv->ops->swap                   = VecSwap_Seq;
    177:     vv->ops->axpy                   = VecAXPY_Seq;
    178:     vv->ops->axpby                  = VecAXPBY_Seq;
    179:     vv->ops->maxpy                  = VecMAXPY_Seq;
    180:     vv->ops->aypx                   = VecAYPX_Seq;
    181:     vv->ops->axpbypcz               = VecAXPBYPCZ_Seq;
    182:     vv->ops->pointwisemult          = VecPointwiseMult_Seq;
    183:     vv->ops->setrandom              = VecSetRandom_Seq;
    184:     vv->ops->placearray             = VecPlaceArray_Seq;
    185:     vv->ops->replacearray           = VecReplaceArray_Seq;
    186:     vv->ops->resetarray             = VecResetArray_Seq;
    187:     vv->ops->dot_local              = VecDot_Seq;
    188:     vv->ops->tdot_local             = VecTDot_Seq;
    189:     vv->ops->norm_local             = VecNorm_Seq;
    190:     vv->ops->mdot_local             = VecMDot_Seq;
    191:     vv->ops->pointwisedivide        = VecPointwiseDivide_Seq;
    192:     vv->ops->getlocalvector         = NULL;
    193:     vv->ops->restorelocalvector     = NULL;
    194:     vv->ops->getlocalvectorread     = NULL;
    195:     vv->ops->restorelocalvectorread = NULL;
    196:     vv->ops->getarraywrite          = NULL;
    197:   } else {
    198:     vv->ops->dotnorm2        = VecDotNorm2_MPIViennaCL;
    199:     vv->ops->waxpy           = VecWAXPY_SeqViennaCL;
    200:     vv->ops->duplicate       = VecDuplicate_MPIViennaCL;
    201:     vv->ops->dot             = VecDot_MPIViennaCL;
    202:     vv->ops->mdot            = VecMDot_MPIViennaCL;
    203:     vv->ops->tdot            = VecTDot_MPIViennaCL;
    204:     vv->ops->norm            = VecNorm_MPIViennaCL;
    205:     vv->ops->scale           = VecScale_SeqViennaCL;
    206:     vv->ops->copy            = VecCopy_SeqViennaCL;
    207:     vv->ops->set             = VecSet_SeqViennaCL;
    208:     vv->ops->swap            = VecSwap_SeqViennaCL;
    209:     vv->ops->axpy            = VecAXPY_SeqViennaCL;
    210:     vv->ops->axpby           = VecAXPBY_SeqViennaCL;
    211:     vv->ops->maxpy           = VecMAXPY_SeqViennaCL;
    212:     vv->ops->aypx            = VecAYPX_SeqViennaCL;
    213:     vv->ops->axpbypcz        = VecAXPBYPCZ_SeqViennaCL;
    214:     vv->ops->pointwisemult   = VecPointwiseMult_SeqViennaCL;
    215:     vv->ops->setrandom       = VecSetRandom_SeqViennaCL;
    216:     vv->ops->dot_local       = VecDot_SeqViennaCL;
    217:     vv->ops->tdot_local      = VecTDot_SeqViennaCL;
    218:     vv->ops->norm_local      = VecNorm_SeqViennaCL;
    219:     vv->ops->mdot_local      = VecMDot_SeqViennaCL;
    220:     vv->ops->destroy         = VecDestroy_MPIViennaCL;
    221:     vv->ops->pointwisedivide = VecPointwiseDivide_SeqViennaCL;
    222:     vv->ops->placearray      = VecPlaceArray_SeqViennaCL;
    223:     vv->ops->replacearray    = VecReplaceArray_SeqViennaCL;
    224:     vv->ops->resetarray      = VecResetArray_SeqViennaCL;
    225:     vv->ops->getarraywrite   = VecGetArrayWrite_SeqViennaCL;
    226:     vv->ops->getarray        = VecGetArray_SeqViennaCL;
    227:     vv->ops->restorearray    = VecRestoreArray_SeqViennaCL;
    228:   }
    229:   vv->ops->duplicatevecs = VecDuplicateVecs_Default;
    230:   PetscFunctionReturn(PETSC_SUCCESS);
    231: }

    233: PETSC_EXTERN PetscErrorCode VecCreate_MPIViennaCL(Vec vv)
    234: {
    235:   PetscFunctionBegin;
    236:   PetscCall(PetscLayoutSetUp(vv->map));
    237:   PetscCall(VecViennaCLAllocateCheck(vv));
    238:   PetscCall(VecCreate_MPIViennaCL_Private(vv, PETSC_FALSE, 0, ((Vec_ViennaCL *)vv->spptr)->GPUarray));
    239:   PetscCall(VecViennaCLAllocateCheckHost(vv));
    240:   PetscCall(VecSet(vv, 0.0));
    241:   PetscCall(VecSet_Seq(vv, 0.0));
    242:   vv->offloadmask = PETSC_OFFLOAD_BOTH;
    243:   PetscFunctionReturn(PETSC_SUCCESS);
    244: }

    246: PETSC_EXTERN PetscErrorCode VecCreate_ViennaCL(Vec v)
    247: {
    248:   PetscMPIInt size;

    250:   PetscFunctionBegin;
    251:   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)v), &size));
    252:   if (size == 1) PetscCall(VecSetType(v, VECSEQVIENNACL));
    253:   else PetscCall(VecSetType(v, VECMPIVIENNACL));
    254:   PetscFunctionReturn(PETSC_SUCCESS);
    255: }

    257: /*@C
    258:   VecCreateMPIViennaCLWithArray - Creates a parallel, array-style vector,
    259:   where the user provides the viennacl vector to store the vector values.

    261:   Collective

    263:   Input Parameters:
    264: + comm  - the MPI communicator to use
    265: . bs    - block size, same meaning as in `VecSetBlockSize()`
    266: . n     - local vector length, cannot be `PETSC_DECIDE`
    267: . N     - global vector length (or `PETSC_DECIDE` to have calculated)
    268: - array - the user provided GPU array to store the vector values

    270:   Output Parameter:
    271: . vv - the vector

    273:   Level: intermediate

    275:   Notes:
    276:   Use `VecDuplicate()` or `VecDuplicateVecs(`) to form additional vectors of the
    277:   same type as an existing vector.

    279:   If the user-provided array is `NULL`, then `VecViennaCLPlaceArray()` can be used
    280:   at a later stage to SET the array for storing the vector values.

    282:   PETSc does NOT free the array when the vector is destroyed via `VecDestroy()`.
    283:   The user should not free the array until the vector is destroyed.

    285: .seealso: `VecCreateSeqViennaCLWithArray()`, `VecCreateMPIWithArray()`, `VecCreateSeqWithArray()`,
    286:           `VecCreate()`, `VecCreateMPI()`, `VecCreateGhostWithArray()`, `VecViennaCLPlaceArray()`
    287: @*/
    288: PetscErrorCode VecCreateMPIViennaCLWithArray(MPI_Comm comm, PetscInt bs, PetscInt n, PetscInt N, const ViennaCLVector *array, Vec *vv) PeNS
    289: {
    290:   PetscFunctionBegin;
    291:   PetscCheck(n != PETSC_DECIDE, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Must set local size of vector");
    292:   PetscCall(PetscSplitOwnership(comm, &n, &N));
    293:   PetscCall(VecCreate(comm, vv));
    294:   PetscCall(VecSetSizes(*vv, n, N));
    295:   PetscCall(VecSetBlockSize(*vv, bs));
    296:   PetscCall(VecCreate_MPIViennaCL_Private(*vv, PETSC_FALSE, 0, array));
    297:   PetscFunctionReturn(PETSC_SUCCESS);
    298: }

    300: /*@C
    301:   VecCreateMPIViennaCLWithArrays - Creates a parallel, array-style vector,
    302:   where the user provides the ViennaCL vector to store the vector values.

    304:   Collective

    306:   Input Parameters:
    307: + comm        - the MPI communicator to use
    308: . bs          - block size, same meaning as VecSetBlockSize()
    309: . n           - local vector length, cannot be PETSC_DECIDE
    310: . N           - global vector length (or PETSC_DECIDE to have calculated)
    311: . cpuarray    - the user provided CPU array to store the vector values
    312: - viennaclvec - ViennaCL vector where the Vec entries are to be stored on the device.

    314:   Output Parameter:
    315: . vv - the vector

    317:   Notes:
    318:   If both cpuarray and viennaclvec are provided, the caller must ensure that
    319:   the provided arrays have identical values.

    321:   Use VecDuplicate() or VecDuplicateVecs() to form additional vectors of the
    322:   same type as an existing vector.

    324:   PETSc does NOT free the provided arrays when the vector is destroyed via
    325:   VecDestroy(). The user should not free the array until the vector is
    326:   destroyed.

    328:   Level: intermediate

    330: .seealso: `VecCreateSeqViennaCLWithArrays()`, `VecCreateMPIWithArray()`,
    331:           `VecCreate()`, `VecDuplicate()`, `VecDuplicateVecs()`, `VecCreateGhost()`,
    332:           `VecCreateMPI()`, `VecCreateGhostWithArray()`, `VecViennaCLPlaceArray()`,
    333:           `VecPlaceArray()`, `VecCreateMPICUDAWithArrays()`,
    334:           `VecViennaCLAllocateCheckHost()`
    335: @*/
    336: PetscErrorCode VecCreateMPIViennaCLWithArrays(MPI_Comm comm, PetscInt bs, PetscInt n, PetscInt N, const PetscScalar cpuarray[], const ViennaCLVector *viennaclvec, Vec *vv) PeNS
    337: {
    338:   PetscFunctionBegin;
    339:   PetscCall(VecCreateMPIViennaCLWithArray(comm, bs, n, N, viennaclvec, vv));
    340:   if (cpuarray && viennaclvec) {
    341:     Vec_MPI *s         = (Vec_MPI *)((*vv)->data);
    342:     s->array           = (PetscScalar *)cpuarray;
    343:     (*vv)->offloadmask = PETSC_OFFLOAD_BOTH;
    344:   } else if (cpuarray) {
    345:     Vec_MPI *s         = (Vec_MPI *)((*vv)->data);
    346:     s->array           = (PetscScalar *)cpuarray;
    347:     (*vv)->offloadmask = PETSC_OFFLOAD_CPU;
    348:   } else if (viennaclvec) {
    349:     (*vv)->offloadmask = PETSC_OFFLOAD_GPU;
    350:   } else {
    351:     (*vv)->offloadmask = PETSC_OFFLOAD_UNALLOCATED;
    352:   }
    353:   PetscFunctionReturn(PETSC_SUCCESS);
    354: }

    356: PetscErrorCode VecCreate_MPIViennaCL_Private(Vec vv, PetscBool alloc, PetscInt nghost, const ViennaCLVector *array)
    357: {
    358:   Vec_ViennaCL *vecviennacl;

    360:   PetscFunctionBegin;
    361:   PetscCall(VecCreate_MPI_Private(vv, PETSC_FALSE, 0, 0));
    362:   PetscCall(PetscObjectChangeTypeName((PetscObject)vv, VECMPIVIENNACL));

    364:   PetscCall(VecBindToCPU_MPIViennaCL(vv, PETSC_FALSE));
    365:   vv->ops->bindtocpu = VecBindToCPU_MPIViennaCL;

    367:   if (alloc && !array) {
    368:     PetscCall(VecViennaCLAllocateCheck(vv));
    369:     PetscCall(VecViennaCLAllocateCheckHost(vv));
    370:     PetscCall(VecSet(vv, 0.0));
    371:     PetscCall(VecSet_Seq(vv, 0.0));
    372:     vv->offloadmask = PETSC_OFFLOAD_BOTH;
    373:   }
    374:   if (array) {
    375:     if (!vv->spptr) vv->spptr = new Vec_ViennaCL;
    376:     vecviennacl                     = (Vec_ViennaCL *)vv->spptr;
    377:     vecviennacl->GPUarray_allocated = 0;
    378:     vecviennacl->GPUarray           = (ViennaCLVector *)array;
    379:     vv->offloadmask                 = PETSC_OFFLOAD_UNALLOCATED;
    380:   }
    381:   PetscFunctionReturn(PETSC_SUCCESS);
    382: }