From d9e4fc5a0d83b536730ea377fd4c078aaf634f78 Mon Sep 17 00:00:00 2001 From: kradchen Date: Fri, 28 Apr 2023 13:22:07 +0800 Subject: [PATCH] Fix std and malloc --- src/Function.cpp | 3 ++- src/Function2D.cpp | 18 ++++++++---------- src/Matrix.cpp | 5 +---- 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/src/Function.cpp b/src/Function.cpp index 3716780..59ee631 100644 --- a/src/Function.cpp +++ b/src/Function.cpp @@ -132,7 +132,8 @@ namespace Aurora { double *malloc(size_t size, bool complex) { if (!complex) return (double *) mkl_malloc(size * sizeof(double), 64); - return (double *) mkl_malloc(size * sizeof(std::complex), 64); + size_t complex_size = size * sizeof(std::complex); + return (double *) mkl_malloc(complex_size, 64); } void free(void* ptr){ diff --git a/src/Function2D.cpp b/src/Function2D.cpp index fb0e94f..059a013 100644 --- a/src/Function2D.cpp +++ b/src/Function2D.cpp @@ -124,21 +124,18 @@ Matrix Aurora::std(const Matrix &aMatrix) { return Matrix(); } - Matrix src = aMatrix.isComplex() ? Aurora::abs(aMatrix) : aMatrix; + Matrix src = aMatrix.isComplex() ? Aurora::abs(aMatrix) : aMatrix.deepCopy(); int calc_size = src.getDimSize(0) == 1 ? src.getDimSize(1) : src.getDimSize(0); int col = src.getDimSize(0) == 1?1:src.getDimSize(1) ; - double *temp = malloc(aMatrix.getDataSize(), aMatrix.getValueType()==Complex); - cblas_dcopy(aMatrix.getDataSize() * aMatrix.getValueType(),aMatrix.getData(),1,temp,1); auto std = Aurora::malloc(col); for (int i = 0; i < col; ++i) { - double *p = temp + i * calc_size; + double *p = src.getData() + i * calc_size; double mean = cblas_dasum(calc_size, p, 1) / calc_size; vdSubI(calc_size, p, 1, &mean, 0, p, 1); vdSqr(calc_size, p, p); std[i] = cblas_dasum(calc_size, p, 1) / (calc_size - 1); } - Aurora::free(temp); - vdSqrt(src.getDimSize(1), std, std); + vdSqrt(col, std, std); return Matrix::New(std, 1, col, aMatrix.getDimSize(2)); } @@ -644,7 +641,8 @@ Matrix Aurora::ifft(const Matrix &aMatrix) { MKL_LONG status; //创建 Descriptor, 精度 double , 输入类型实数, 维度1 - status = DftiCreateDescriptor(&my_desc_handle, DFTI_DOUBLE, DFTI_COMPLEX, 1, aMatrix.getDimSize(0)); + int size = aMatrix.getDimSize(0); + status = DftiCreateDescriptor(&my_desc_handle, DFTI_DOUBLE, DFTI_COMPLEX, 1, size); if (status != DFTI_NO_ERROR) goto error; //通过 setValue 配置Descriptor //使用单独的输出数据缓存 @@ -652,14 +650,14 @@ Matrix Aurora::ifft(const Matrix &aMatrix) { if (status != DFTI_NO_ERROR) goto error; //设置DFTI_BACKWARD_SCALE !!!很关键,不然值不对 - status = DftiSetValue(my_desc_handle, DFTI_BACKWARD_SCALE, 1.0f / aMatrix.getDimSize(0)); + status = DftiSetValue(my_desc_handle, DFTI_BACKWARD_SCALE, 1.0f / size); if (status != DFTI_NO_ERROR) goto error; - status = DftiSetValue(my_desc_handle,DFTI_INPUT_DISTANCE,aMatrix.getDimSize(0)); + status = DftiSetValue(my_desc_handle,DFTI_INPUT_DISTANCE,size); if (status != DFTI_NO_ERROR) goto error; //每个傅里叶变换的输出距离 - status = DftiSetValue(my_desc_handle,DFTI_OUTPUT_DISTANCE,aMatrix.getDimSize(0)); + status = DftiSetValue(my_desc_handle,DFTI_OUTPUT_DISTANCE,size); if (status != DFTI_NO_ERROR) goto error; //傅里叶变换的数量 diff --git a/src/Matrix.cpp b/src/Matrix.cpp index d8a7b8a..2afd276 100644 --- a/src/Matrix.cpp +++ b/src/Matrix.cpp @@ -349,10 +349,7 @@ namespace Aurora { Matrix Matrix::deepCopy() const { double *newBuffer = malloc(getDataSize(), getValueType()==Complex); - // size_t data_size = getDataSize() * getValueType(); -cblas_dcopy(getDataSize() * getValueType(),getData(),1,newBuffer,1); - // std::memcpy(newBuffer,getData(),data_size*sizeof(double)); - + cblas_dcopy(getDataSize() * getValueType(),getData(),1,newBuffer,1); return New(newBuffer, getDimSize(0), getDimSize(1),