From 044de7be5f6de783a7791d97240b8792ec351d82 Mon Sep 17 00:00:00 2001 From: Krad Date: Thu, 20 Apr 2023 09:32:58 +0800 Subject: [PATCH] Fix Matrix free bug(with mkl_malloc) --- src/Matrix.cpp | 31 ++++++++++++++++++++----------- src/Matrix.h | 31 ++++++++++++++++--------------- 2 files changed, 36 insertions(+), 26 deletions(-) diff --git a/src/Matrix.cpp b/src/Matrix.cpp index ffc788b..78a8041 100644 --- a/src/Matrix.cpp +++ b/src/Matrix.cpp @@ -118,6 +118,14 @@ namespace Aurora { Matrix::Matrix(std::shared_ptr aData, std::vector aInfo) : mData(aData), mInfo(aInfo) { } + + Matrix::Matrix(const Matrix::MatrixSlice& slice) { + auto temp = slice.toMatrix(); + this->mData = temp.mData; + this->mInfo = temp.mInfo; + this->mValueType = temp.mValueType; + } + bool Matrix::isNull() const { return !mData || mInfo.empty(); } @@ -185,7 +193,7 @@ namespace Aurora { vector.push_back(rows); if (cols > 0)vector.push_back(cols); if (slices > 0)vector.push_back(slices); - Matrix ret({data, std::default_delete()}, vector); + Matrix ret({data, free}, vector); if (type != Normal)ret.setValueType(type); return ret; } @@ -471,39 +479,40 @@ namespace Aurora { return *this; } - Matrix Matrix::MatrixSlice::toMatrix() { - auto data = (double *) mkl_malloc(mSize*(mSize2>0?mSize2:1) * sizeof(double)*mType, 64); - auto matrix = Matrix::New(data,mSize,mSize2,0,mType); + Matrix Matrix::MatrixSlice::toMatrix() const { + double * data = (double *) mkl_malloc(mSize*(mSize2>0?mSize2:1) * sizeof(double)*mType, 64); + switch (mSliceMode) { case 2:{ if (mType== Normal) { cblas_dcopy_batch_strided(mSize, mData, mStride, - mStride2,matrix.getData(), 1, matrix.getDimSize(0), mSize2); + mStride2,data, 1, mSize, mSize2); } else { cblas_zcopy_batch_strided(mSize, (std::complex *) mData, mStride, mStride2, - (std::complex *) matrix.getData(), 1, matrix.getDimSize(0), + (std::complex *) data, 1, mSize, mSize2); } break; } case 1:{ if (mType== Normal){ - cblas_dcopy(mSize,mData,mStride,matrix.getData(),1); + cblas_dcopy(mSize,mData,mStride,data,1); } else { cblas_zcopy(mSize, (std::complex *) mData, mStride, - (std::complex *) matrix.getData(), 1); + (std::complex *) data, 1); } break; } case 0: default:{ - matrix.getData()[0]= mData[0]; - if (mType != Normal) matrix.getData()[1] = mData[1]; + data[0]= mData[0]; + if (mType != Normal) data[1] = mData[1]; } } - return matrix; + + return Matrix::New(data,mSize,mSize2,0,mType); } } diff --git a/src/Matrix.h b/src/Matrix.h index 203df62..c271d83 100644 --- a/src/Matrix.h +++ b/src/Matrix.h @@ -14,16 +14,6 @@ namespace Aurora { class Matrix { public: - - explicit Matrix(std::shared_ptr aData = std::shared_ptr(), - std::vector aInfo = std::vector()); - - static Matrix New(double *data, int rows, int cols = 0, int slices = 0, ValueType type = Normal); - - static Matrix New(double *data, const Matrix &shapeMatrix); - - static Matrix New(const Matrix &shapeMatrix); - /** * 内部类MatrixSlice,用于切片操作 */ @@ -32,16 +22,27 @@ namespace Aurora { MatrixSlice(int aSize,int aStride, double* aData,ValueType aType = Normal,int SliceMode = 1,int aSize2 = 0, int aStride2 = 0); MatrixSlice& operator=(const MatrixSlice& slice); MatrixSlice& operator=(const Matrix& matrix); - Matrix toMatrix(); + Matrix toMatrix() const; private: int mSliceMode = 0;//0 scalar, 1 vector, 2 Matrix double* mData; - int mSize; - int mSize2; - int mStride; - int mStride2; + int mSize=0; + int mSize2=0; + int mStride=1; + int mStride2=0; ValueType mType; + friend class Matrix; }; + explicit Matrix(std::shared_ptr aData = std::shared_ptr(), + std::vector aInfo = std::vector()); + + explicit Matrix(const Matrix::MatrixSlice& slice); + + static Matrix New(double *data, int rows, int cols = 0, int slices = 0, ValueType type = Normal); + + static Matrix New(double *data, const Matrix &shapeMatrix); + + static Matrix New(const Matrix &shapeMatrix); Matrix getDataFromDims2(int aColumn);