From 6867dcb227faeb1b8ef4e3c38ed02210549a78d7 Mon Sep 17 00:00:00 2001 From: sunwen Date: Tue, 5 Dec 2023 14:37:00 +0800 Subject: [PATCH] Add deleteColumn and createCudaVectorMatrix. --- src/Function1D.cu | 47 +++++++++++++++++++++++++++++++++++ src/Function1D.cuh | 4 +++ test/Function1D_Cuda_Test.cpp | 18 ++++++++++++++ 3 files changed, 69 insertions(+) diff --git a/src/Function1D.cu b/src/Function1D.cu index 1174bd6..9de58ae 100644 --- a/src/Function1D.cu +++ b/src/Function1D.cu @@ -1433,3 +1433,50 @@ CudaMatrix Aurora::xcorr(const CudaMatrix& aMatrix1, const CudaMatrix& aMatrix2) return CudaMatrix::fromRawData(data, size); } + +CudaMatrix Aurora::deleteColumn(const CudaMatrix& aMatrix, int aColumnIndex) +{ + int rows = aMatrix.getDimSize(0); + int columns = aMatrix.getDimSize(1); + if (aColumnIndex < 0 || aColumnIndex >= columns) + { + return aMatrix; + } + + float* resultData = nullptr; + cudaMalloc((void**)&resultData, sizeof(float) * rows* (columns-1)); + if(aColumnIndex == 0) + { + cudaMemcpy(resultData, aMatrix.getData() + rows, sizeof(float) * rows* (columns-1), cudaMemcpyDeviceToDevice); + } + else if(aColumnIndex == (columns - 1)) + { + cblas_scopy(rows* (columns-1), aMatrix.getData(), 1, resultData, 1); + cudaMemcpy(resultData, aMatrix.getData(), sizeof(float) * rows* (columns-1), cudaMemcpyDeviceToDevice); + } + else + { + cudaMemcpy(resultData, aMatrix.getData(), sizeof(float) * rows * aColumnIndex, cudaMemcpyDeviceToDevice); + cudaMemcpy(resultData + rows * aColumnIndex, aMatrix.getData() + rows * (aColumnIndex + 1), sizeof(float) * rows * (columns - aColumnIndex - 1), cudaMemcpyDeviceToDevice); + } + + return CudaMatrix::fromRawData(resultData, rows, columns-1); +} + +CudaMatrix Aurora::createCudaVectorMatrix(float aStartValue, float aStepValue, float aEndValue) +{ + std::vector matrixData; + float tempValue = aStartValue; + matrixData.push_back(tempValue); + long long compare1 = std::round(aEndValue * 10e13); + long long compare2 = std::round(tempValue * 10e13); + while(std::round(tempValue* 10e13) <= compare1) + { + tempValue += aStepValue; + matrixData.push_back(tempValue); + compare2 = std::round(tempValue * 10e14); + } + matrixData.pop_back(); + + return Matrix::copyFromRawData(matrixData.data(), 1, matrixData.size()).toDeviceMatrix(); +} diff --git a/src/Function1D.cuh b/src/Function1D.cuh index 7d32e82..1d2ed50 100644 --- a/src/Function1D.cuh +++ b/src/Function1D.cuh @@ -82,6 +82,10 @@ namespace Aurora CudaMatrix reshape(const CudaMatrix& aMatrix, int aRows, int aColumns, int aSlices); CudaMatrix xcorr(const CudaMatrix& aMatrix1, const CudaMatrix& aMatrix2); + + CudaMatrix deleteColumn(const CudaMatrix& aMatrix, int aColumnIndex); + + CudaMatrix createCudaVectorMatrix(float aStartValue, float aStepValue, float aEndValue); /** * 将所有nan值设置为特定值 diff --git a/test/Function1D_Cuda_Test.cpp b/test/Function1D_Cuda_Test.cpp index d8579db..c6b3d7a 100644 --- a/test/Function1D_Cuda_Test.cpp +++ b/test/Function1D_Cuda_Test.cpp @@ -1103,3 +1103,21 @@ TEST_F(Function1D_Cuda_Test, xcorr) { EXPECT_FLOAT_AE(result[5], 48); EXPECT_FLOAT_AE(result[6], 20); } + +TEST_F(Function1D_Cuda_Test, deleteColumn) { + auto matrixHost = Aurora::Matrix::fromRawData(new float[100], 10,10,1); + for(int i=0;i<100;++i) + { + matrixHost[i] = i; + } + auto matrixDevice = matrixHost.toDeviceMatrix(); + auto result1 = Aurora::deleteColumn(matrixHost, 26); + auto result2 = Aurora::deleteColumn(matrixDevice, 26).toHostMatrix(); + EXPECT_FLOAT_AE(result1.getDimSize(0),result2.getDimSize(0)); + EXPECT_FLOAT_AE(result1.getDimSize(1),result2.getDimSize(1)); + EXPECT_FLOAT_AE(result1.getDataSize(),result2.getDataSize()); + for(int i=0;i