From d5204898231b2c46675c1fc3b1eeb041ca013194 Mon Sep 17 00:00:00 2001 From: sunwen Date: Thu, 9 Nov 2023 17:56:13 +0800 Subject: [PATCH] Add CudaMatrix block, setBlock, setBlockValue. --- src/CudaMatrix.cpp | 263 +++++++++++++++++++++++++++++++++++++- src/CudaMatrixPrivate.cu | 10 ++ src/CudaMatrixPrivate.cuh | 5 + 3 files changed, 271 insertions(+), 7 deletions(-) diff --git a/src/CudaMatrix.cpp b/src/CudaMatrix.cpp index d28e4fc..092a2a7 100644 --- a/src/CudaMatrix.cpp +++ b/src/CudaMatrix.cpp @@ -160,10 +160,6 @@ CudaMatrix CudaMatrix::block(int aDim,int aBeginIndex, int aEndIndex) const std::cerr<<"CudaMatrix block only support 1D-3D data!"<1) - { - aDim = 1; - } if (aBeginIndex>=getDimSize(aDim) || aBeginIndex<0) { @@ -220,7 +216,6 @@ CudaMatrix CudaMatrix::block(int aDim,int aBeginIndex, int aEndIndex) const return CudaMatrix::fromRawData(dataOutput,getDimSize(0),dimLength,getDimSize(2),getValueType()); } case 2: - default: { int copySize = dimLength*sliceStride; cudaMemcpy(dataOutput, @@ -228,19 +223,273 @@ CudaMatrix CudaMatrix::block(int aDim,int aBeginIndex, int aEndIndex) const sizeof(float) * copySize*getValueType(), cudaMemcpyDeviceToDevice); return CudaMatrix::fromRawData(dataOutput,getDimSize(0),getDimSize(1),dimLength,getValueType()); } + default: + { + return CudaMatrix(); + } } } -bool CudaMatrix::setBlockValue(int aDim,int aBeginIndx, int aEndIndex,float value) +bool CudaMatrix::setBlockValue(int aDim,int aBeginIndex, int aEndIndex,float aValue) { if(aDim>2 ) { - std::cerr<<"CudaMatrix block only support 1D-3D data!"<=getDimSize(aDim) || aBeginIndex<0) + { + std::cerr<<"CudaMatrix setblockValue BeginIndx error!BeginIndx:"<=getDimSize(aDim) || aEndIndex<0) + { + std::cerr<<"CudaMatrix setblockValue EndIndex error!EndIndex:"< aValue) +{ + if(getValueType() != Complex) + { + std::cerr<<"CudaMatrix setBlockComplexValue only support complex matrix"<2 ) + { + std::cerr<<"CudaMatrix setBlockComplexValue only support 1D-3D data!"<=getDimSize(aDim) || aBeginIndex<0) + { + std::cerr<<"CudaMatrix setBlockComplexValue BeginIndx error!BeginIndx:"<=getDimSize(aDim) || aEndIndex<0) + { + std::cerr<<"CudaMatrix setBlockComplexValue EndIndex error!EndIndex:"<2 ) + { + std::cerr<<"CudaMatrix setBlock only support 1D-3D data!"<=getDimSize(aDim) || aBeginIndex<0) + { + std::cerr<<"CudaMatrix setBlock BeginIndx error!BeginIndx:"<=getDimSize(aDim) || aEndIndex<0) + { + std::cerr<<"CudaMatrix setBlock EndIndex error!EndIndex:"< aValue) +{ + thrust::fill(thrust::device, (std::complex*)aBegin, (std::complex*)aEnd, aValue); +} + diff --git a/src/CudaMatrixPrivate.cuh b/src/CudaMatrixPrivate.cuh index 4d8c91f..2853310 100644 --- a/src/CudaMatrixPrivate.cuh +++ b/src/CudaMatrixPrivate.cuh @@ -2,6 +2,8 @@ #ifndef __CUDAMATRIX_CUH__ #define __CUDAMATRIX_CUH__ +#include + void unaryAdd(float* in1, float* in2, float* out, unsigned long length); void unaryAdd(float* in1, const float& in2, float* out, unsigned long length); void unaryMul(float* in1, float* in2, float* out, unsigned long length); @@ -17,5 +19,8 @@ void unaryDiv(const float& in1, float* in2, float* out, unsigned long length); void unarySub(float* in1, const float& in2, float* out, unsigned long length); void unaryDiv(float* in1, const float& in2, float* out, unsigned long length); +void thrustFill(float* aBegin, float* aEnd, float aValue); +void thrustFill(float* aBegin, float* aEnd, std::complex aValue); + #endif // __CUDAMATRIX_H__ \ No newline at end of file