Actual source code: cudavecimpl.h

petsc-master 2019-09-20
Report Typos and Errors

  4: #if defined(__CUDACC__)

  6:  #include <petscvec.h>
  7:  #include <petsccublas.h>
  8:  #include <petsc/private/vecimpl.h>

 10: #include <cublas_v2.h>

 12: #define WaitForGPU() PetscCUDASynchronize ? cudaDeviceSynchronize() : 0

 14: #endif

 16: typedef struct {
 17:   PetscScalar  *GPUarray;           /* this always holds the GPU data */
 18:   PetscScalar  *GPUarray_allocated; /* if the array was allocated by PETSc this is its pointer */
 19:   cudaStream_t stream;              /* A stream for doing asynchronous data transfers */
 20:   PetscBool    hostDataRegisteredAsPageLocked;
 21: } Vec_CUDA;


 24: #include <cuda_runtime.h>

 26: PETSC_INTERN PetscErrorCode VecDotNorm2_SeqCUDA(Vec,Vec,PetscScalar*, PetscScalar*);
 27: PETSC_INTERN PetscErrorCode VecPointwiseDivide_SeqCUDA(Vec,Vec,Vec);
 28: PETSC_INTERN PetscErrorCode VecWAXPY_SeqCUDA(Vec,PetscScalar,Vec,Vec);
 29: PETSC_INTERN PetscErrorCode VecMDot_SeqCUDA(Vec,PetscInt,const Vec[],PetscScalar*);
 30: PETSC_INTERN PetscErrorCode VecSet_SeqCUDA(Vec,PetscScalar);
 31: PETSC_INTERN PetscErrorCode VecMAXPY_SeqCUDA(Vec,PetscInt,const PetscScalar*,Vec*);
 32: PETSC_INTERN PetscErrorCode VecAXPBYPCZ_SeqCUDA(Vec,PetscScalar,PetscScalar,PetscScalar,Vec,Vec);
 33: PETSC_INTERN PetscErrorCode VecPointwiseMult_SeqCUDA(Vec,Vec,Vec);
 34: PETSC_INTERN PetscErrorCode VecPlaceArray_SeqCUDA(Vec,const PetscScalar*);
 35: PETSC_INTERN PetscErrorCode VecResetArray_SeqCUDA(Vec);
 36: PETSC_INTERN PetscErrorCode VecReplaceArray_SeqCUDA(Vec,const PetscScalar*);
 37: PETSC_INTERN PetscErrorCode VecDot_SeqCUDA(Vec,Vec,PetscScalar*);
 38: PETSC_INTERN PetscErrorCode VecTDot_SeqCUDA(Vec,Vec,PetscScalar*);
 39: PETSC_INTERN PetscErrorCode VecScale_SeqCUDA(Vec,PetscScalar);
 40: PETSC_EXTERN PetscErrorCode VecCopy_SeqCUDA(Vec,Vec);
 41: PETSC_INTERN PetscErrorCode VecSwap_SeqCUDA(Vec,Vec);
 42: PETSC_EXTERN PetscErrorCode VecAXPY_SeqCUDA(Vec,PetscScalar,Vec);
 43: PETSC_INTERN PetscErrorCode VecAXPBY_SeqCUDA(Vec,PetscScalar,PetscScalar,Vec);
 44: PETSC_INTERN PetscErrorCode VecDuplicate_SeqCUDA(Vec,Vec*);
 45: PETSC_INTERN PetscErrorCode VecConjugate_SeqCUDA(Vec xin);
 46: PETSC_INTERN PetscErrorCode VecNorm_SeqCUDA(Vec,NormType,PetscReal*);
 47: PETSC_INTERN PetscErrorCode VecCUDACopyToGPU(Vec);
 48: PETSC_INTERN PetscErrorCode VecCUDAAllocateCheck(Vec);
 49: PETSC_EXTERN PetscErrorCode VecCreate_SeqCUDA(Vec);
 50: PETSC_INTERN PetscErrorCode VecCreate_SeqCUDA_Private(Vec,const PetscScalar*);
 51: PETSC_INTERN PetscErrorCode VecCreate_MPICUDA(Vec);
 52: PETSC_INTERN PetscErrorCode VecCreate_MPICUDA_Private(Vec,PetscBool,PetscInt,const PetscScalar*);
 53: PETSC_INTERN PetscErrorCode VecCreate_CUDA(Vec);
 54: PETSC_INTERN PetscErrorCode VecDestroy_SeqCUDA(Vec);
 55: PETSC_INTERN PetscErrorCode VecDestroy_MPICUDA(Vec);
 56: PETSC_INTERN PetscErrorCode VecAYPX_SeqCUDA(Vec,PetscScalar,Vec);
 57: PETSC_INTERN PetscErrorCode VecSetRandom_SeqCUDA(Vec,PetscRandom);
 58: PETSC_INTERN PetscErrorCode VecGetLocalVector_SeqCUDA(Vec,Vec);
 59: PETSC_INTERN PetscErrorCode VecRestoreLocalVector_SeqCUDA(Vec,Vec);
 60: PETSC_INTERN PetscErrorCode VecCopy_SeqCUDA_Private(Vec xin,Vec yin);
 61: PETSC_INTERN PetscErrorCode VecSetRandom_SeqCUDA_Private(Vec xin,PetscRandom r);
 62: PETSC_INTERN PetscErrorCode VecDestroy_SeqCUDA_Private(Vec v);
 63: PETSC_INTERN PetscErrorCode VecResetArray_SeqCUDA_Private(Vec vin);
 64: PETSC_INTERN PetscErrorCode VecCUDACopyToGPU_Public(Vec);
 65: PETSC_INTERN PetscErrorCode VecCUDAAllocateCheck_Public(Vec);
 66: PETSC_INTERN PetscErrorCode VecCUDACopyToGPUSome(Vec,PetscCUDAIndices,ScatterMode);
 67: PETSC_INTERN PetscErrorCode VecCUDACopyFromGPUSome(Vec,PetscCUDAIndices,ScatterMode);

 69: PETSC_INTERN PetscErrorCode VecScatterCUDAIndicesCreate_PtoP(PetscInt, PetscInt*,PetscInt, PetscInt*,PetscCUDAIndices*);
 70: PETSC_INTERN PetscErrorCode VecScatterCUDAIndicesCreate_StoS(PetscInt,PetscInt,PetscInt,PetscInt,PetscInt,PetscInt*,PetscInt*,PetscCUDAIndices*);
 71: PETSC_INTERN PetscErrorCode VecScatterCUDAIndicesDestroy(PetscCUDAIndices*);
 72: PETSC_INTERN PetscErrorCode VecScatterCUDA_StoS(Vec,Vec,PetscCUDAIndices,InsertMode,ScatterMode);

 74: typedef enum {VEC_SCATTER_CUDA_STOS, VEC_SCATTER_CUDA_PTOP} VecCUDAScatterType;
 75: typedef enum {VEC_SCATTER_CUDA_GENERAL, VEC_SCATTER_CUDA_STRIDED} VecCUDASequentialScatterMode;

 77: struct  _p_VecScatterCUDAIndices_PtoP {
 78:   PetscInt ns;
 79:   PetscInt sendLowestIndex;
 80:   PetscInt nr;
 81:   PetscInt recvLowestIndex;
 82: };

 84: struct  _p_VecScatterCUDAIndices_StoS {
 85:   /* from indices data */
 86:   PetscInt *fslots;
 87:   PetscInt fromFirst;
 88:   PetscInt fromStep;
 89:   VecCUDASequentialScatterMode fromMode;

 91:   /* to indices data */
 92:   PetscInt *tslots;
 93:   PetscInt toFirst;
 94:   PetscInt toStep;
 95:   VecCUDASequentialScatterMode toMode;

 97:   PetscInt n;
 98:   PetscInt MAX_BLOCKS;
 99:   PetscInt MAX_CORESIDENT_THREADS;
100:   cudaStream_t stream;
101: };

103: struct  _p_PetscCUDAIndices {
104:   void * scatter;
105:   VecCUDAScatterType scatterType;
106: };

108: /* complex single */
109: #if defined(PETSC_USE_COMPLEX)
110: #if defined(PETSC_USE_REAL_SINGLE)
111: #define cublasXaxpy(a,b,c,d,e,f,g) cublasCaxpy((a),(b),(cuComplex*)(c),(cuComplex*)(d),(e),(cuComplex*)(f),(g))
112: #define cublasXscal(a,b,c,d,e)     cublasCscal((a),(b),(cuComplex*)(c),(cuComplex*)(d),(e))
113: #define cublasXdotu(a,b,c,d,e,f,g) cublasCdotu((a),(b),(cuComplex*)(c),(d),(cuComplex*)(e),(f),(cuComplex*)(g))
114: #define cublasXdot(a,b,c,d,e,f,g)  cublasCdotc((a),(b),(cuComplex*)(c),(d),(cuComplex*)(e),(f),(cuComplex*)(g))
115: #define cublasXswap(a,b,c,d,e,f)   cublasCswap((a),(b),(cuComplex*)(c),(d),(cuComplex*)(e),(f))
116: #define cublasXnrm2(a,b,c,d,e)     cublasScnrm2((a),(b),(cuComplex*)(c),(d),(e))
117: #define cublasIXamax(a,b,c,d,e)    cublasIcamax((a),(b),(cuComplex*)(c),(d),(e))
118: #define cublasXasum(a,b,c,d,e)     cublasScasum((a),(b),(cuComplex*)(c),(d),(e))
119: #define cublasXgemv(a,b,c,d,e,f,g,h,i,j,k,l) cublasCgemv((a),(b),(c),(d),(cuComplex*)(e),(cuComplex*)(f),(g),(cuComplex*)(h),(i),(cuComplex*)(j),(cuComplex*)(k),(l))
120: #define cublasXgemm(a,b,c,d,e,f,g,h,i,j,k,l,m,n) cublasCgemm((a),(b),(c),(d),(e),(f),(cuComplex*)(g),(cuComplex*)(h),(i),(cuComplex*)(j),(k),(cuComplex*)(l),(cuComplex*)(m),(n))
121: #else /* complex double */
122: #define cublasXaxpy(a,b,c,d,e,f,g) cublasZaxpy((a),(b),(cuDoubleComplex*)(c),(cuDoubleComplex*)(d),(e),(cuDoubleComplex*)(f),(g))
123: #define cublasXscal(a,b,c,d,e)     cublasZscal((a),(b),(cuDoubleComplex*)(c),(cuDoubleComplex*)(d),(e))
124: #define cublasXdotu(a,b,c,d,e,f,g) cublasZdotu((a),(b),(cuDoubleComplex*)(c),(d),(cuDoubleComplex*)(e),(f),(cuDoubleComplex*)(g))
125: #define cublasXdot(a,b,c,d,e,f,g)  cublasZdotc((a),(b),(cuDoubleComplex*)(c),(d),(cuDoubleComplex*)(e),(f),(cuDoubleComplex*)(g))
126: #define cublasXswap(a,b,c,d,e,f)   cublasZswap((a),(b),(cuDoubleComplex*)(c),(d),(cuDoubleComplex*)(e),(f))
127: #define cublasXnrm2(a,b,c,d,e)     cublasDznrm2((a),(b),(cuDoubleComplex*)(c),(d),(e))
128: #define cublasIXamax(a,b,c,d,e)    cublasIzamax((a),(b),(cuDoubleComplex*)(c),(d),(e))
129: #define cublasXasum(a,b,c,d,e)     cublasDzasum((a),(b),(cuDoubleComplex*)(c),(d),(e))
130: #define cublasXgemv(a,b,c,d,e,f,g,h,i,j,k,l) cublasZgemv((a),(b),(c),(d),(cuDoubleComplex*)(e),(cuDoubleComplex*)(f),(g),(cuDoubleComplex*)(h),(i),(cuDoubleComplex*)(j),(cuDoubleComplex*)(k),(l))
131: #define cublasXgemm(a,b,c,d,e,f,g,h,i,j,k,l,m,n) cublasZgemm((a),(b),(c),(d),(e),(f),(cuDoubleComplex*)(g),(cuDoubleComplex*)(h),(i),(cuDoubleComplex*)(j),(k),(cuDoubleComplex*)(l),(cuDoubleComplex*)(m),(n))
132: #endif
133: #else /* real single */
134: #if defined(PETSC_USE_REAL_SINGLE)
135: #define cublasXaxpy  cublasSaxpy
136: #define cublasXscal  cublasSscal
137: #define cublasXdotu  cublasSdot
138: #define cublasXdot   cublasSdot
139: #define cublasXswap  cublasSswap
140: #define cublasXnrm2  cublasSnrm2
141: #define cublasIXamax cublasIsamax
142: #define cublasXasum  cublasSasum
143: #define cublasXgemv  cublasSgemv
144: #define cublasXgemm  cublasSgemm
145: #else /* real double */
146: #define cublasXaxpy  cublasDaxpy
147: #define cublasXscal  cublasDscal
148: #define cublasXdotu  cublasDdot
149: #define cublasXdot   cublasDdot
150: #define cublasXswap  cublasDswap
151: #define cublasXnrm2  cublasDnrm2
152: #define cublasIXamax cublasIdamax
153: #define cublasXasum  cublasDasum
154: #define cublasXgemv  cublasDgemv
155: #define cublasXgemm  cublasDgemm
156: #endif
157: #endif

159: #endif