Actual source code: mpicusp.cu

petsc-3.7.3 2016-07-24
Report Typos and Errors
  2: /*
  3:    This file contains routines for Parallel vector operations.
  4:  */
  5: #define PETSC_SKIP_COMPLEX
  6: #define PETSC_SKIP_SPINLOCK

  8: #include <petscconf.h>
  9: #include <../src/vec/vec/impls/mpi/pvecimpl.h>   /*I  "petscvec.h"   I*/
 10: #include <../src/vec/vec/impls/seq/seqcusp/cuspvecimpl.h>

 14: PetscErrorCode VecDestroy_MPICUSP(Vec v)
 15: {

 19:   try {
 20:     if (v->spptr) {
 21:       delete ((Vec_CUSP*)v->spptr)->GPUarray;
 22:       delete (Vec_CUSP*) v->spptr;
 23:     }
 24:   } catch(char * ex) {
 25:     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUSP error: %s", ex);
 26:   }
 27:   VecDestroy_MPI(v);
 28:   return(0);
 29: }

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

 39:   if (type == NORM_2 || type == NORM_FROBENIUS) {
 40:     VecNorm_SeqCUSP(xin,NORM_2,&work);
 41:     work *= work;
 42:     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:     VecNorm_SeqCUSP(xin,NORM_1,&work);
 47:     /* Find the global max */
 48:     MPIU_Allreduce(&work,z,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)xin));
 49:   } else if (type == NORM_INFINITY) {
 50:     /* Find the local max */
 51:     VecNorm_SeqCUSP(xin,NORM_INFINITY,&work);
 52:     /* Find the global max */
 53:     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:     VecNorm_SeqCUSP(xin,NORM_1,temp);
 57:     VecNorm_SeqCUSP(xin,NORM_2,temp+1);
 58:     temp[1] = temp[1]*temp[1];
 59:     MPIU_Allreduce(temp,z,2,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)xin));
 60:     z[1] = PetscSqrtReal(z[1]);
 61:   }
 62:   return(0);
 63: }

 67: PetscErrorCode VecDot_MPICUSP(Vec xin,Vec yin,PetscScalar *z)
 68: {
 69:   PetscScalar    sum,work;

 73:   VecDot_SeqCUSP(xin,yin,&work);
 74:   MPIU_Allreduce(&work,&sum,1,MPIU_SCALAR,MPIU_SUM,PetscObjectComm((PetscObject)xin));
 75:   *z   = sum;
 76:   return(0);
 77: }

 81: PetscErrorCode VecTDot_MPICUSP(Vec xin,Vec yin,PetscScalar *z)
 82: {
 83:   PetscScalar    sum,work;

 87:   VecTDot_SeqCUSP(xin,yin,&work);
 88:   MPIU_Allreduce(&work,&sum,1,MPIU_SCALAR,MPIU_SUM,PetscObjectComm((PetscObject)xin));
 89:   *z   = sum;
 90:   return(0);
 91: }

 95: PetscErrorCode VecMDot_MPICUSP(Vec xin,PetscInt nv,const Vec y[],PetscScalar *z)
 96: {
 97:   PetscScalar    awork[128],*work = awork;

101:   if (nv > 128) {
102:     PetscMalloc1(nv,&work);
103:   }
104:   VecMDot_SeqCUSP(xin,nv,y,work);
105:   MPIU_Allreduce(work,z,nv,MPIU_SCALAR,MPIU_SUM,PetscObjectComm((PetscObject)xin));
106:   if (nv > 128) {
107:     PetscFree(work);
108:   }
109:   return(0);
110: }

112: /*MC
113:    VECMPICUSP - VECMPICUSP = "mpicusp" - The basic parallel vector, modified to use CUSP

115:    Options Database Keys:
116: . -vec_type mpicusp - sets the vector type to VECMPICUSP during a call to VecSetFromOptions()

118:   Level: beginner

120: .seealso: VecCreate(), VecSetType(), VecSetFromOptions(), VecCreateMpiWithArray(), VECMPI, VecType, VecCreateMPI(), VecCreateMpi()
121: M*/


126: PetscErrorCode VecDuplicate_MPICUSP(Vec win,Vec *v)
127: {
129:   Vec_MPI        *vw,*w = (Vec_MPI*)win->data;
130:   PetscScalar    *array;

133:   VecCreate(PetscObjectComm((PetscObject)win),v);
134:   PetscLayoutReference(win->map,&(*v)->map);

136:   VecCreate_MPI_Private(*v,PETSC_FALSE,w->nghost,0);
137:   vw   = (Vec_MPI*)(*v)->data;
138:   PetscMemcpy((*v)->ops,win->ops,sizeof(struct _VecOps));

140:   /* save local representation of the parallel vector (and scatter) if it exists */
141:   if (w->localrep) {
142:     VecGetArray(*v,&array);
143:     VecCreateSeqWithArray(PETSC_COMM_SELF,1,win->map->n+w->nghost,array,&vw->localrep);
144:     PetscMemcpy(vw->localrep->ops,w->localrep->ops,sizeof(struct _VecOps));
145:     VecRestoreArray(*v,&array);
146:     PetscLogObjectParent((PetscObject)*v,(PetscObject)vw->localrep);
147:     vw->localupdate = w->localupdate;
148:     if (vw->localupdate) {
149:       PetscObjectReference((PetscObject)vw->localupdate);
150:     }
151:   }

153:   /* New vector should inherit stashing property of parent */
154:   (*v)->stash.donotstash   = win->stash.donotstash;
155:   (*v)->stash.ignorenegidx = win->stash.ignorenegidx;

157:   /* change type_name appropriately */
158:   PetscObjectChangeTypeName((PetscObject)(*v),VECMPICUSP);

160:   PetscObjectListDuplicate(((PetscObject)win)->olist,&((PetscObject)(*v))->olist);
161:   PetscFunctionListDuplicate(((PetscObject)win)->qlist,&((PetscObject)(*v))->qlist);
162:   (*v)->map->bs   = PetscAbs(win->map->bs);
163:   (*v)->bstash.bs = win->bstash.bs;
164:   return(0);
165: }

169: PetscErrorCode VecDotNorm2_MPICUSP(Vec s,Vec t,PetscScalar *dp,PetscScalar *nm)
170: {
172:   PetscScalar    work[2],sum[2];

175:   VecDotNorm2_SeqCUSP(s,t,work,work+1);
176:   MPIU_Allreduce(&work,&sum,2,MPIU_SCALAR,MPIU_SUM,PetscObjectComm((PetscObject)s));
177:   *dp  = sum[0];
178:   *nm  = sum[1];
179:   return(0);
180: }

184: PETSC_EXTERN PetscErrorCode VecCreate_MPICUSP(Vec vv)
185: {

189:   VecCreate_MPI_Private(vv,PETSC_FALSE,0,0);
190:   PetscObjectChangeTypeName((PetscObject)vv,VECMPICUSP);

192:   vv->ops->dotnorm2               = VecDotNorm2_MPICUSP;
193:   vv->ops->waxpy                  = VecWAXPY_SeqCUSP;
194:   vv->ops->duplicate              = VecDuplicate_MPICUSP;
195:   vv->ops->dot                    = VecDot_MPICUSP;
196:   vv->ops->mdot                   = VecMDot_MPICUSP;
197:   vv->ops->tdot                   = VecTDot_MPICUSP;
198:   vv->ops->norm                   = VecNorm_MPICUSP;
199:   vv->ops->scale                  = VecScale_SeqCUSP;
200:   vv->ops->copy                   = VecCopy_SeqCUSP;
201:   vv->ops->set                    = VecSet_SeqCUSP;
202:   vv->ops->swap                   = VecSwap_SeqCUSP;
203:   vv->ops->axpy                   = VecAXPY_SeqCUSP;
204:   vv->ops->axpby                  = VecAXPBY_SeqCUSP;
205:   vv->ops->maxpy                  = VecMAXPY_SeqCUSP;
206:   vv->ops->aypx                   = VecAYPX_SeqCUSP;
207:   vv->ops->axpbypcz               = VecAXPBYPCZ_SeqCUSP;
208:   vv->ops->pointwisemult          = VecPointwiseMult_SeqCUSP;
209:   vv->ops->setrandom              = VecSetRandom_SeqCUSP;
210:   vv->ops->placearray             = VecPlaceArray_SeqCUSP;
211:   vv->ops->replacearray           = VecReplaceArray_SeqCUSP;
212:   vv->ops->resetarray             = VecResetArray_SeqCUSP;
213:   vv->ops->dot_local              = VecDot_SeqCUSP;
214:   vv->ops->tdot_local             = VecTDot_SeqCUSP;
215:   vv->ops->norm_local             = VecNorm_SeqCUSP;
216:   vv->ops->mdot_local             = VecMDot_SeqCUSP;
217:   vv->ops->destroy                = VecDestroy_MPICUSP;
218:   vv->ops->pointwisedivide        = VecPointwiseDivide_SeqCUSP;
219:   vv->ops->getlocalvector         = VecGetLocalVector_SeqCUSP;
220:   vv->ops->restorelocalvector     = VecRestoreLocalVector_SeqCUSP;
221:   vv->ops->getlocalvectorread     = VecGetLocalVector_SeqCUSP;
222:   vv->ops->restorelocalvectorread = VecRestoreLocalVector_SeqCUSP;
223:   VecCUSPAllocateCheck(vv);CHKERRCUSP(ierr);
224:   vv->valid_GPU_array      = PETSC_CUSP_GPU;
225:   VecSet(vv,0.0);
226:   return(0);
227: }

231: PETSC_EXTERN PetscErrorCode VecCreate_CUSP(Vec v)
232: {
234:   PetscMPIInt    size;

237:   MPI_Comm_size(PetscObjectComm((PetscObject)v),&size);
238:   if (size == 1) {
239:     VecSetType(v,VECSEQCUSP);
240:   } else {
241:     VecSetType(v,VECMPICUSP);
242:   }
243:   return(0);
244: }