diff --git a/source/source_base/kernels/cuda/math_kernel_op.cu b/source/source_base/kernels/cuda/math_kernel_op.cu index fa6419fea26..35bd5af02b9 100644 --- a/source/source_base/kernels/cuda/math_kernel_op.cu +++ b/source/source_base/kernels/cuda/math_kernel_op.cu @@ -1,6 +1,5 @@ #include "source_base/module_device/memory_op.h" #include "source_base/kernels/math_kernel_op.h" -#include "source_base/tool_quit.h" #include "source_base/module_container/base/third_party/cublas.h" #include @@ -59,7 +58,7 @@ void scal_op::operator()(const int& N, std::complex* X, const int& incx) { - CHECK_CUBLAS(cublasCscal(cublas_handle, N, (float2*)alpha, (float2*)X, incx)); + container::cuBlasConnector::scal(cublas_handle, N, *alpha, X, incx); } template <> @@ -68,7 +67,7 @@ void scal_op::operator()(const int& N, std::complex* X, const int& incx) { - CHECK_CUBLAS(cublasZscal(cublas_handle, N, (double2*)alpha, (double2*)X, incx)); + container::cuBlasConnector::scal(cublas_handle, N, *alpha, X, incx); } template <> @@ -90,7 +89,7 @@ void axpy_op, base_device::DEVICE_GPU>::operator()(const int std::complex* Y, const int& incY) { - CHECK_CUBLAS(cublasCaxpy(cublas_handle, N, (float2*)alpha, (float2*)X, incX, (float2*)Y, incY)); + container::cuBlasConnector::axpy(cublas_handle, N, *alpha, X, incX, Y, incY); } template <> @@ -101,7 +100,7 @@ void axpy_op, base_device::DEVICE_GPU>::operator()(const in std::complex* Y, const int& incY) { - CHECK_CUBLAS(cublasZaxpy(cublas_handle, N, (double2*)alpha, (double2*)X, incX, (double2*)Y, incY)); + container::cuBlasConnector::axpy(cublas_handle, N, *alpha, X, incX, Y, incY); } @@ -142,26 +141,6 @@ __global__ void matrix_multiply_vector_kernel(const int m, const int n, T *a, co c[col * ldc + row] = a[col * lda + row] * b[col] * alpha; } -cublasOperation_t judge_trans_op(bool is_complex, const char& trans, const char* name) -{ - if (trans == 'N') - { - return CUBLAS_OP_N; - } - else if(trans == 'T') - { - return CUBLAS_OP_T; - } - else if(is_complex && trans == 'C') - { - return CUBLAS_OP_C; - } - else - { - ModuleBase::WARNING_QUIT(name, std::string("Unknown trans type ") + trans + std::string(" !")); - } -} - template <> void gemv_op::operator()(const char& trans, const int& m, @@ -175,7 +154,7 @@ void gemv_op::operator()(const char& trans, double* Y, const int& incy) { - cublasOperation_t cutrans = judge_trans_op(false, trans, "gemv_op"); + cublasOperation_t cutrans = GetCublasOperation(trans, false); CHECK_CUBLAS(cublasDgemv(cublas_handle, cutrans, m, n, alpha, A, lda, X, incx, beta, Y, incy)); } @@ -192,7 +171,7 @@ void gemv_op::operator()(const char& trans, float* Y, const int& incy) { - cublasOperation_t cutrans = judge_trans_op(false, trans, "gemv_op"); + cublasOperation_t cutrans = GetCublasOperation(trans, false); CHECK_CUBLAS(cublasSgemv(cublas_handle, cutrans, m, n, alpha, A, lda, X, incx, beta, Y, incy)); } @@ -211,7 +190,7 @@ void gemv_op, base_device::DEVICE_GPU>::operator()(const cha std::complex* Y, const int& incy) { - cublasOperation_t cutrans = judge_trans_op(true, trans, "gemv_op"); + cublasOperation_t cutrans = GetCublasOperation(trans, true); cuFloatComplex alpha = make_cuFloatComplex(alpha_in->real(), alpha_in->imag()); cuFloatComplex beta = make_cuFloatComplex(beta_in->real(), beta_in->imag()); CHECK_CUBLAS(cublasCgemv(cublas_handle, cutrans, m, n, &alpha, (cuFloatComplex*)A, lda, (cuFloatComplex*)X, incx, &beta, (cuFloatComplex*)Y, incy)); @@ -230,7 +209,7 @@ void gemv_op, base_device::DEVICE_GPU>::operator()(const ch std::complex* Y, const int& incy) { - cublasOperation_t cutrans = judge_trans_op(true, trans, "gemv_op"); + cublasOperation_t cutrans = GetCublasOperation(trans, true); cuDoubleComplex alpha = make_cuDoubleComplex(alpha_in->real(), alpha_in->imag()); cuDoubleComplex beta = make_cuDoubleComplex(beta_in->real(), beta_in->imag()); // icpc and nvcc have some compatible problems @@ -253,8 +232,8 @@ void gemm_op::operator()(const char& transa, float* c, const int& ldc) { - cublasOperation_t cutransA = judge_trans_op(false, transa, "gemm_op"); - cublasOperation_t cutransB = judge_trans_op(false, transb, "gemm_op"); + cublasOperation_t cutransA = GetCublasOperation(transa, false); + cublasOperation_t cutransB = GetCublasOperation(transb, false); CHECK_CUBLAS(cublasSgemm(cublas_handle, cutransA, cutransB, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc)); } @@ -273,10 +252,11 @@ void gemm_op::operator()(const char& transa, double* c, const int& ldc) { - cublasOperation_t cutransA = judge_trans_op(false, transa, "gemm_op"); - cublasOperation_t cutransB = judge_trans_op(false, transb, "gemm_op"); + cublasOperation_t cutransA = GetCublasOperation(transa, false); + cublasOperation_t cutransB = GetCublasOperation(transb, false); CHECK_CUBLAS(cublasDgemm(cublas_handle, cutransA, cutransB, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc)); } + template <> void gemm_op, base_device::DEVICE_GPU>::operator()(const char& transa, const char& transb, @@ -292,23 +272,29 @@ void gemm_op, base_device::DEVICE_GPU>::operator()(const cha std::complex* c, const int& ldc) { - cublasOperation_t cutransA = judge_trans_op(true, transa, "gemm_op"); - cublasOperation_t cutransB = judge_trans_op(true, transb, "gemm_op"); + cublasOperation_t cutransA = GetCublasOperation(transa, true); + cublasOperation_t cutransB = GetCublasOperation(transb, true); + const cuComplex alpha_cu = make_cuComplex(alpha->real(), alpha->imag()); + const cuComplex beta_cu = make_cuComplex(beta->real(), beta->imag()); + const cuComplex* a_cu = reinterpret_cast(a); + const cuComplex* b_cu = reinterpret_cast(b); + cuComplex* c_cu = reinterpret_cast(c); // Bound rounding error in long single-precision conjugate inner products. constexpr int block = 2048; if (cutransA == CUBLAS_OP_C && cutransB == CUBLAS_OP_N && k > block) { - const std::complex one(1.0f, 0.0f); + const cuComplex one = make_cuComplex(1.0f, 0.0f); for (int offset = 0; offset < k; offset += block) { + const cuComplex* block_beta = offset == 0 ? &beta_cu : &one; CHECK_CUBLAS(cublasCgemm(cublas_handle, cutransA, cutransB, m, n, std::min(block, k - offset), - (float2*)alpha, (float2*)(a + offset), lda, (float2*)(b + offset), ldb, - (float2*)(offset == 0 ? beta : &one), (float2*)c, ldc)); + &alpha_cu, a_cu + offset, lda, b_cu + offset, ldb, block_beta, c_cu, ldc)); } } else { - CHECK_CUBLAS(cublasCgemm(cublas_handle, cutransA, cutransB, m, n ,k, (float2*)alpha, (float2*)a , lda, (float2*)b, ldb, (float2*)beta, (float2*)c, ldc)); + CHECK_CUBLAS(cublasCgemm(cublas_handle, cutransA, cutransB, m, n, k, + &alpha_cu, a_cu, lda, b_cu, ldb, &beta_cu, c_cu, ldc)); } } @@ -327,12 +313,16 @@ void gemm_op, base_device::DEVICE_GPU>::operator()(const ch std::complex* c, const int& ldc) { - cublasOperation_t cutransA = judge_trans_op(true, transa, "gemm_op"); - cublasOperation_t cutransB = judge_trans_op(true, transb, "gemm_op"); + cublasOperation_t cutransA = GetCublasOperation(transa, true); + cublasOperation_t cutransB = GetCublasOperation(transb, true); if (cublas_handle == nullptr) { CHECK_CUBLAS(cublasCreate(&cublas_handle)); } - CHECK_CUBLAS(cublasZgemm(cublas_handle, cutransA, cutransB, m, n ,k, (double2*)alpha, (double2*)a , lda, (double2*)b, ldb, (double2*)beta, (double2*)c, ldc)); + const cuDoubleComplex alpha_cu = make_cuDoubleComplex(alpha->real(), alpha->imag()); + const cuDoubleComplex beta_cu = make_cuDoubleComplex(beta->real(), beta->imag()); + CHECK_CUBLAS(cublasZgemm(cublas_handle, cutransA, cutransB, m, n, k, &alpha_cu, + reinterpret_cast(a), lda, reinterpret_cast(b), ldb, + &beta_cu, reinterpret_cast(c), ldc)); } template <> @@ -380,15 +370,11 @@ void matrixTranspose_op, base_device::DEVICE_GPU>::operator( if (row == col) { - double2 ONE, ZERO; - ONE.x = 1.0; - ONE.y = 0.0; - ZERO.x = ZERO.y = 0.0; - - // use 'geam' API todo transpose. + const cuComplex one = make_cuComplex(1.0f, 0.0f); + const cuComplex zero = make_cuComplex(0.0f, 0.0f); + const cuComplex* input = reinterpret_cast(input_matrix); CHECK_CUBLAS(cublasCgeam(cublas_handle, CUBLAS_OP_T, CUBLAS_OP_N, col, row, - reinterpret_cast(&ONE), (float2*)input_matrix, col, - reinterpret_cast(&ZERO), (float2*)input_matrix, col, (float2*)device_temp, col)); + &one, input, col, &zero, input, col, reinterpret_cast(device_temp), col)); } else { int thread = 1024; diff --git a/source/source_base/module_container/ATen/kernels/cuda/blas.cu b/source/source_base/module_container/ATen/kernels/cuda/blas.cu index 3068ed155e0..c1d0596b245 100644 --- a/source/source_base/module_container/ATen/kernels/cuda/blas.cu +++ b/source/source_base/module_container/ATen/kernels/cuda/blas.cu @@ -6,29 +6,92 @@ #include #include +#include #include #include +#include #include namespace container { namespace kernels { static cublasHandle_t cublas_handle = nullptr; +static cublasHandle_t device_dot_handle = nullptr; void createGpuBlasHandle() { if (cublas_handle == nullptr) { CHECK_CUBLAS(cublasCreate(&cublas_handle)); + // Both handles use the default stream. Only dot writes scalar results on the device. + CHECK_CUBLAS(cublasCreate(&device_dot_handle)); + CHECK_CUBLAS(cublasSetPointerMode(device_dot_handle, CUBLAS_POINTER_MODE_DEVICE)); } } void destroyGpuBlasHandle() { if (cublas_handle != nullptr) { CHECK_CUBLAS(cublasDestroy(cublas_handle)); + CHECK_CUBLAS(cublasDestroy(device_dot_handle)); + device_dot_handle = nullptr; cublas_handle = nullptr; } } namespace { +bool device_result_ptr(const void* result) +{ + cudaPointerAttributes attr{}; + const cudaError_t err = cudaPointerGetAttributes(&attr, result); +#if CUDART_VERSION < 11000 + // Older runtimes report ordinary host memory as an unregistered pointer error. + if (err == cudaErrorInvalidValue) + { + cudaGetLastError(); + return false; + } +#endif + CHECK_CUDA(err); +#if CUDART_VERSION >= 10000 + return attr.type == cudaMemoryTypeDevice || attr.type == cudaMemoryTypeManaged; +#else + return attr.memoryType == cudaMemoryTypeDevice || attr.isManaged; +#endif +} + +template +void dot_result(int n, const T* x, int incx, const T* y, int incy, T* result) +{ + cublasHandle_t handle = device_result_ptr(result) ? device_dot_handle : cublas_handle; + cuBlasConnector::dot(handle, n, x, incx, y, incy, result); +} + +template +void dot_result(int n, const std::complex* x, int incx, const std::complex* y, int incy, + std::complex* result) +{ + using Native = typename std::conditional::value, cuComplex, cuDoubleComplex>::type; + if (!device_result_ptr(result)) + { + Native value{}; + cuBlasConnector::dot(cublas_handle, n, x, incx, y, incy, &value); + *result = std::complex(value.x, value.y); + return; + } + if (reinterpret_cast(result) % alignof(Native) == 0) + { + cuBlasConnector::dot(device_dot_handle, n, x, incx, y, incy, reinterpret_cast(result)); + return; + } + + // A complex member of a device struct can have only the alignment of Real. + Tensor temp(DataTypeToEnum>::value, DeviceType::GpuDevice, {1}); + Native* value = reinterpret_cast(temp.data()); + cudaStream_t stream = nullptr; + CHECK_CUBLAS(cublasGetStream(device_dot_handle, &stream)); + cuBlasConnector::dot(device_dot_handle, n, x, incx, y, incy, value); + CHECK_CUDA(cudaMemcpyAsync(result, value, sizeof(Native), cudaMemcpyDeviceToDevice, stream)); + CHECK_CUDA(cudaStreamSynchronize(stream)); +} + template __global__ void set_identity(T* matrix, const int n) { @@ -145,7 +208,7 @@ struct blas_dot { const int& incy, T* result) { - cuBlasConnector::dot(cublas_handle, n, x, incx, y, incy, result); + dot_result(n, x, incx, y, incy, result); } }; diff --git a/source/source_base/module_container/ATen/ops/einsum_op.cpp b/source/source_base/module_container/ATen/ops/einsum_op.cpp index eeb60aea2c2..c8d2424f85e 100644 --- a/source/source_base/module_container/ATen/ops/einsum_op.cpp +++ b/source/source_base/module_container/ATen/ops/einsum_op.cpp @@ -789,7 +789,20 @@ static void DoContract( Tensor& out_z) { const T alpha = static_cast(option.alpha); - const T beta = static_cast(option.beta); + const T beta = option.out ? static_cast(option.beta) : T(0); + if (out_z.NumElements() == 1) + { + // Each contiguous operand can be viewed as a row or a column to conjugate it independently. + const int k = in_x.shape().dim_size(trans_x ? 1 : 2); + const char op_x = option.conj_x ? 'C' : 'N'; + const char op_y = option.conj_y ? 'C' : 'N'; + const int lda = option.conj_x ? k : 1; + const int ldb = option.conj_y ? 1 : k; + kernels::blas_gemm()(op_x, op_y, 1, 1, k, &alpha, in_x.data(), lda, + in_y.data(), ldb, &beta, out_z.data(), 1); + return; + } + const int m = in_x.shape().dim_size(option.conj_x || trans_x ? 2 : 1); const int k = in_x.shape().dim_size(option.conj_x || trans_x ? 1 : 2); const int n = in_y.shape().dim_size(option.conj_y || trans_y ? 1 : 2); @@ -851,14 +864,8 @@ static void DoContract( // We want the output to be in row-major, so we can compute // C' = B' x A', where ' stands for transpose (not adjoint). if (batch_size == 1) { - // Dot product - if (m == 1 && n == 1 && option.conj_x != true && option.conj_y != true) { - // Dot product - // TODO: implement the Conjugate version of Dot product. - kernels::blas_dot()(k, x_device_memory_ptrs[0], 1, y_device_memory_ptrs[0], 1, z_device_memory_ptrs[0]); - } // Gemv - else if (n == 1 && option.conj_x != true) { + if (n == 1 && option.conj_x != true) { // This is a matrix*vector multiply so use GEMV to compute A * x. // Here we are multiplying in the natural order, so we have to flip // the transposition flag to compensate for the tensor being stored @@ -954,8 +961,16 @@ bool ContractOperands( else { CopyFromWithAllocate(inputs[0], output_shape, &output); } + if (output.NumElements() == 0) { + return true; + } if (lhs.NumElements() == 0 || rhs.NumElements() == 0) { - output.zero(); + if (option.out == nullptr || option.beta == 0.0f) { + output.zero(); + } else if (option.beta != 1.0f) { + TEMPLATE_BLAS_2(output.data_type(), output.device_type(), + op::mul_op()(static_cast(option.beta), output, output)) + } return true; } diff --git a/source/source_base/module_container/base/macros/cuda.h b/source/source_base/module_container/base/macros/cuda.h index 521861664a6..2b84075f492 100644 --- a/source/source_base/module_container/base/macros/cuda.h +++ b/source/source_base/module_container/base/macros/cuda.h @@ -5,6 +5,8 @@ #include #include #include +#include +#include #include "source_base/module_device/device_check.h" @@ -28,22 +30,21 @@ struct GetTypeThrust> using type = thrust::complex; /**< The return type specialization for std::complex. */ }; -static inline cublasOperation_t GetCublasOperation(const char& trans) +//! Translate a BLAS transpose flag for the operand's scalar type. +static inline cublasOperation_t GetCublasOperation(const char& trans, bool is_complex) { - cublasOperation_t cutrans = {}; - if (trans == 'N') - { - cutrans = CUBLAS_OP_N; - } - else if (trans == 'T') - { - cutrans = CUBLAS_OP_T; - } - else if (trans == 'C') + switch (trans) { - cutrans = CUBLAS_OP_C; + case 'N': case 'n': return CUBLAS_OP_N; + case 'T': case 't': return CUBLAS_OP_T; + case 'C': case 'c': return is_complex ? CUBLAS_OP_C : CUBLAS_OP_T; + default: throw std::invalid_argument(std::string("Invalid BLAS transpose flag: ") + trans); } - return cutrans; +} + +static inline cublasOperation_t GetCublasOperation(const char& trans) +{ + return GetCublasOperation(trans, true); } template @@ -92,7 +93,7 @@ static inline cublasFillMode_t cublas_fill_mode(const char& uplo) else if (uplo == 'L' || uplo == 'l') return CUBLAS_FILL_MODE_LOWER; else - throw std::runtime_error("cublas_fill_mode: unknown uplo"); + throw std::runtime_error(std::string("Invalid BLAS triangle: ") + uplo); } static inline cublasDiagType_t cublas_diag_type(const char& diag) diff --git a/source/source_base/module_container/base/third_party/blas.h b/source/source_base/module_container/base/third_party/blas.h index 1345a442cab..987f43ec3c7 100644 --- a/source/source_base/module_container/base/third_party/blas.h +++ b/source/source_base/module_container/base/third_party/blas.h @@ -1,6 +1,8 @@ #ifndef BASE_THIRD_PARTY_BLAS_H_ #define BASE_THIRD_PARTY_BLAS_H_ +#include + #include #if defined(__CUDA) @@ -335,8 +337,8 @@ void gemv_batched(const char& trans, const int& m, const int& n, template static inline void gemv_batched_strided(const char& transa, const int& m, const int& n, - const T& alpha, const T* A, const int& lda, const int& stride_a, const T* x, const int& incx, const int& stride_x, - const T& beta, T* y, const int& incy, const int& stride_y, const int& batch_size) + const T& alpha, const T* A, const int& lda, const int64_t& stride_a, const T* x, const int& incx, const int64_t& stride_x, + const T& beta, T* y, const int& incy, const int64_t& stride_y, const int& batch_size) { for (int ii = 0; ii < batch_size; ii++) { // Call the single GEMV for each pair of matrix A[ii] and vector x[ii] diff --git a/source/source_base/module_container/base/third_party/cublas.h b/source/source_base/module_container/base/third_party/cublas.h index bbf8c6f34bf..b36afaf2d3d 100644 --- a/source/source_base/module_container/base/third_party/cublas.h +++ b/source/source_base/module_container/base/third_party/cublas.h @@ -1,6 +1,8 @@ #ifndef BASE_THIRD_PARTY_CUBLAS_H_ #define BASE_THIRD_PARTY_CUBLAS_H_ +#include + #include #include #include @@ -105,14 +107,14 @@ void dot(cublasHandle_t& handle, const int& n, const double *x, const int& incx, CHECK_CUBLAS(cublasDdot(handle, n, x, incx, y, incy, result)); } static inline -void dot(cublasHandle_t& handle, const int& n, const std::complex *x, const int& incx, const std::complex *y, const int& incy, std::complex* result) +void dot(cublasHandle_t& handle, const int& n, const std::complex *x, const int& incx, const std::complex *y, const int& incy, cuComplex* result) { - CHECK_CUBLAS(cublasCdotc(handle, n, reinterpret_cast(x), incx, reinterpret_cast(y), incy, reinterpret_cast(result))); + CHECK_CUBLAS(cublasCdotc(handle, n, reinterpret_cast(x), incx, reinterpret_cast(y), incy, result)); } static inline -void dot(cublasHandle_t& handle, const int& n, const std::complex *x, const int& incx, const std::complex *y, const int& incy, std::complex* result) +void dot(cublasHandle_t& handle, const int& n, const std::complex *x, const int& incx, const std::complex *y, const int& incy, cuDoubleComplex* result) { - CHECK_CUBLAS(cublasZdotc(handle, n, reinterpret_cast(x), incx, reinterpret_cast(y), incy, reinterpret_cast(result))); + CHECK_CUBLAS(cublasZdotc(handle, n, reinterpret_cast(x), incx, reinterpret_cast(y), incy, result)); } static inline @@ -128,12 +130,14 @@ void axpy(cublasHandle_t& handle, const int& n, const double& alpha, const doubl static inline void axpy(cublasHandle_t& handle, const int& n, const std::complex& alpha, const std::complex *x, const int& incx, std::complex *y, const int& incy) { - CHECK_CUBLAS(cublasCaxpy(handle, n, reinterpret_cast(&alpha), reinterpret_cast(x), incx, reinterpret_cast(y), incy)); + const cuComplex alpha_cu = make_cuComplex(alpha.real(), alpha.imag()); + CHECK_CUBLAS(cublasCaxpy(handle, n, &alpha_cu, reinterpret_cast(x), incx, reinterpret_cast(y), incy)); } static inline void axpy(cublasHandle_t& handle, const int& n, const std::complex& alpha, const std::complex *x, const int& incx, std::complex *y, const int& incy) { - CHECK_CUBLAS(cublasZaxpy(handle, n, reinterpret_cast(&alpha), reinterpret_cast(x), incx, reinterpret_cast(y), incy)); + const cuDoubleComplex alpha_cu = make_cuDoubleComplex(alpha.real(), alpha.imag()); + CHECK_CUBLAS(cublasZaxpy(handle, n, &alpha_cu, reinterpret_cast(x), incx, reinterpret_cast(y), incy)); } static inline @@ -149,12 +153,14 @@ void scal(cublasHandle_t& handle, const int& n, const double& alpha, double *x, static inline void scal(cublasHandle_t& handle, const int& n, const std::complex& alpha, std::complex *x, const int& incx) { - CHECK_CUBLAS(cublasCscal(handle, n, reinterpret_cast(&alpha), reinterpret_cast(x), incx)); + const cuComplex alpha_cu = make_cuComplex(alpha.real(), alpha.imag()); + CHECK_CUBLAS(cublasCscal(handle, n, &alpha_cu, reinterpret_cast(x), incx)); } static inline void scal(cublasHandle_t& handle, const int& n, const std::complex& alpha, std::complex *x, const int& incx) { - CHECK_CUBLAS(cublasZscal(handle, n, reinterpret_cast(&alpha), reinterpret_cast(x), incx)); + const cuDoubleComplex alpha_cu = make_cuDoubleComplex(alpha.real(), alpha.imag()); + CHECK_CUBLAS(cublasZscal(handle, n, &alpha_cu, reinterpret_cast(x), incx)); } static inline @@ -162,30 +168,34 @@ void gemv(cublasHandle_t& handle, const char& trans, const int& m, const int& n, const float& alpha, const float *A, const int& lda, const float *x, const int& incx, const float& beta, float *y, const int& incy) { - CHECK_CUBLAS(cublasSgemv(handle, GetCublasOperation(trans), m, n, &alpha, A, lda, x, incx, &beta, y, incy)); + CHECK_CUBLAS(cublasSgemv(handle, GetCublasOperation(trans, false), m, n, &alpha, A, lda, x, incx, &beta, y, incy)); } static inline void gemv(cublasHandle_t& handle, const char& trans, const int& m, const int& n, const double& alpha, const double *A, const int& lda, const double *x, const int& incx, const double& beta, double *y, const int& incy) { - CHECK_CUBLAS(cublasDgemv(handle, GetCublasOperation(trans), m, n, &alpha, A, lda, x, incx, &beta, y, incy)); + CHECK_CUBLAS(cublasDgemv(handle, GetCublasOperation(trans, false), m, n, &alpha, A, lda, x, incx, &beta, y, incy)); } static inline void gemv(cublasHandle_t& handle, const char& trans, const int& m, const int& n, const std::complex& alpha, const std::complex *A, const int& lda, const std::complex *x, const int& incx, const std::complex& beta, std::complex *y, const int& incy) { - CHECK_CUBLAS(cublasCgemv(handle, GetCublasOperation(trans), m, n, reinterpret_cast(&alpha), - reinterpret_cast(A), lda, reinterpret_cast(x), incx, reinterpret_cast(&beta), reinterpret_cast(y), incy)); + const cuComplex alpha_cu = make_cuComplex(alpha.real(), alpha.imag()); + const cuComplex beta_cu = make_cuComplex(beta.real(), beta.imag()); + CHECK_CUBLAS(cublasCgemv(handle, GetCublasOperation(trans), m, n, &alpha_cu, + reinterpret_cast(A), lda, reinterpret_cast(x), incx, &beta_cu, reinterpret_cast(y), incy)); } static inline void gemv(cublasHandle_t& handle, const char& trans, const int& m, const int& n, const std::complex& alpha, const std::complex *A, const int& lda, const std::complex *x, const int& incx, const std::complex& beta, std::complex *y, const int& incy) { - CHECK_CUBLAS(cublasZgemv(handle, GetCublasOperation(trans), m, n, reinterpret_cast(&alpha), - reinterpret_cast(A), lda, reinterpret_cast(x), incx, reinterpret_cast(&beta), reinterpret_cast(y), incy)); + const cuDoubleComplex alpha_cu = make_cuDoubleComplex(alpha.real(), alpha.imag()); + const cuDoubleComplex beta_cu = make_cuDoubleComplex(beta.real(), beta.imag()); + CHECK_CUBLAS(cublasZgemv(handle, GetCublasOperation(trans), m, n, &alpha_cu, + reinterpret_cast(A), lda, reinterpret_cast(x), incx, &beta_cu, reinterpret_cast(y), incy)); } template @@ -203,8 +213,8 @@ void gemv_batched(cublasHandle_t& handle, const char& trans, const int& m, const template static inline void gemv_batched_strided(cublasHandle_t& handle, const char& transa, const int& m, const int& n, - const T& alpha, const T* A, const int& lda, const int& stride_a, const T* x, const int& incx, const int& stride_x, - const T& beta, T* y, const int& incy, const int& stride_y, const int& batch_size) + const T& alpha, const T* A, const int& lda, const int64_t& stride_a, const T* x, const int& incx, const int64_t& stride_x, + const T& beta, T* y, const int& incy, const int64_t& stride_y, const int& batch_size) { for (int ii = 0; ii < batch_size; ii++) { // Call the single GEMV for each pair of matrix A[ii] and vector x[ii] @@ -217,7 +227,7 @@ void gemm(cublasHandle_t& handle, const char& transa, const char& transb, const const float& alpha, const float* A, const int& lda, const float* B, const int& ldb, const float& beta, float* C, const int& ldc) { - CHECK_CUBLAS(cublasSgemm(handle, GetCublasOperation(transa), GetCublasOperation(transb), + CHECK_CUBLAS(cublasSgemm(handle, GetCublasOperation(transa, false), GetCublasOperation(transb, false), m, n, k, &alpha, A, lda, B, ldb, &beta, C, ldc)); } static inline @@ -225,7 +235,7 @@ void gemm(cublasHandle_t& handle, const char& transa, const char& transb, const const double& alpha, const double* A, const int& lda, const double* B, const int& ldb, const double& beta, double* C, const int& ldc) { - CHECK_CUBLAS(cublasDgemm(handle, GetCublasOperation(transa), GetCublasOperation(transb), + CHECK_CUBLAS(cublasDgemm(handle, GetCublasOperation(transa, false), GetCublasOperation(transb, false), m, n, k, &alpha, A, lda, B, ldb, &beta, C, ldc)); } static inline @@ -233,12 +243,14 @@ void gemm(cublasHandle_t& handle, const char& transa, const char& transb, const const std::complex& alpha, const std::complex* A, const int& lda, const std::complex* B, const int& ldb, const std::complex& beta, std::complex* C, const int& ldc) { + const cuComplex alpha_cu = make_cuComplex(alpha.real(), alpha.imag()); + const cuComplex beta_cu = make_cuComplex(beta.real(), beta.imag()); CHECK_CUBLAS(cublasCgemm(handle, GetCublasOperation(transa), GetCublasOperation(transb), m, n, k, - reinterpret_cast(&alpha), + &alpha_cu, reinterpret_cast(A), lda, reinterpret_cast(B), ldb, - reinterpret_cast(&beta), + &beta_cu, reinterpret_cast(C), ldc)); } static inline @@ -246,12 +258,14 @@ void gemm(cublasHandle_t& handle, const char& transa, const char& transb, const const std::complex& alpha, const std::complex* A, const int& lda, const std::complex* B, const int& ldb, const std::complex& beta, std::complex* C, const int& ldc) { + const cuDoubleComplex alpha_cu = make_cuDoubleComplex(alpha.real(), alpha.imag()); + const cuDoubleComplex beta_cu = make_cuDoubleComplex(beta.real(), beta.imag()); CHECK_CUBLAS(cublasZgemm(handle, GetCublasOperation(transa), GetCublasOperation(transb), m, n, k, - reinterpret_cast(&alpha), + &alpha_cu, reinterpret_cast(A), lda, reinterpret_cast(B), ldb, - reinterpret_cast(&beta), + &beta_cu, reinterpret_cast(C), ldc)); } @@ -270,10 +284,12 @@ void gemm_batched(cublasHandle_t& handle, const char& transa, const char& transb const float& alpha, float** A, const int& lda, float** B, const int& ldb, const float& beta, float** C, const int& ldc, const int& batch_size) { + const cublasOperation_t op_a = GetCublasOperation(transa, false); + const cublasOperation_t op_b = GetCublasOperation(transb, false); float** d_A = allocate_(A, batch_size); float** d_B = allocate_(B, batch_size); float** d_C = allocate_(C, batch_size); - CHECK_CUBLAS(cublasSgemmBatched(handle, GetCublasOperation(transa), GetCublasOperation(transb), + CHECK_CUBLAS(cublasSgemmBatched(handle, op_a, op_b, m, n, k, &alpha, d_A, lda, d_B, ldb, &beta, d_C, ldc, batch_size)); CHECK_CUDA(cudaFree(d_A)); CHECK_CUDA(cudaFree(d_B)); @@ -284,10 +300,12 @@ void gemm_batched(cublasHandle_t& handle, const char& transa, const char& transb const double& alpha, double** A, const int& lda, double** B, const int& ldb, const double& beta, double** C, const int& ldc, const int& batch_size) { + const cublasOperation_t op_a = GetCublasOperation(transa, false); + const cublasOperation_t op_b = GetCublasOperation(transb, false); double** d_A = allocate_(A, batch_size); double** d_B = allocate_(B, batch_size); double** d_C = allocate_(C, batch_size); - CHECK_CUBLAS(cublasDgemmBatched(handle, GetCublasOperation(transa), GetCublasOperation(transb), + CHECK_CUBLAS(cublasDgemmBatched(handle, op_a, op_b, m, n, k, &alpha, d_A, lda, d_B, ldb, &beta, d_C, ldc, batch_size)); CHECK_CUDA(cudaFree(d_A)); CHECK_CUDA(cudaFree(d_B)); @@ -298,15 +316,19 @@ void gemm_batched(cublasHandle_t& handle, const char& transa, const char& transb const std::complex& alpha, std::complex** A, const int& lda, std::complex** B, const int& ldb, const std::complex& beta, std::complex** C, const int& ldc, const int& batch_size) { + const cublasOperation_t op_a = GetCublasOperation(transa); + const cublasOperation_t op_b = GetCublasOperation(transb); std::complex** d_A = allocate_(A, batch_size); std::complex** d_B = allocate_(B, batch_size); std::complex** d_C = allocate_(C, batch_size); - CHECK_CUBLAS(cublasCgemmBatched(handle, GetCublasOperation(transa), GetCublasOperation(transb), + const cuComplex alpha_cu = make_cuComplex(alpha.real(), alpha.imag()); + const cuComplex beta_cu = make_cuComplex(beta.real(), beta.imag()); + CHECK_CUBLAS(cublasCgemmBatched(handle, op_a, op_b, m, n, k, - reinterpret_cast(&alpha), + &alpha_cu, reinterpret_cast(d_A), lda, reinterpret_cast(d_B), ldb, - reinterpret_cast(&beta), + &beta_cu, reinterpret_cast(d_C), ldc, batch_size)); CHECK_CUDA(cudaFree(d_A)); CHECK_CUDA(cudaFree(d_B)); @@ -317,15 +339,19 @@ void gemm_batched(cublasHandle_t& handle, const char& transa, const char& transb const std::complex& alpha, std::complex** A, const int& lda, std::complex** B, const int& ldb, const std::complex& beta, std::complex** C, const int& ldc, const int& batch_size) { + const cublasOperation_t op_a = GetCublasOperation(transa); + const cublasOperation_t op_b = GetCublasOperation(transb); std::complex** d_A = allocate_(A, batch_size); std::complex** d_B = allocate_(B, batch_size); std::complex** d_C = allocate_(C, batch_size); - CHECK_CUBLAS(cublasZgemmBatched(handle, GetCublasOperation(transa), GetCublasOperation(transb), + const cuDoubleComplex alpha_cu = make_cuDoubleComplex(alpha.real(), alpha.imag()); + const cuDoubleComplex beta_cu = make_cuDoubleComplex(beta.real(), beta.imag()); + CHECK_CUBLAS(cublasZgemmBatched(handle, op_a, op_b, m, n, k, - reinterpret_cast(&alpha), + &alpha_cu, reinterpret_cast(d_A), lda, reinterpret_cast(d_B), ldb, - reinterpret_cast(&beta), + &beta_cu, reinterpret_cast(d_C), ldc, batch_size)); CHECK_CUDA(cudaFree(d_A)); CHECK_CUDA(cudaFree(d_B)); @@ -339,8 +365,8 @@ void gemm_batched_strided(cublasHandle_t& handle, const char& transa, const char { CHECK_CUBLAS(cublasSgemmStridedBatched( handle, - GetCublasOperation(transa), - GetCublasOperation(transb), + GetCublasOperation(transa, false), + GetCublasOperation(transb, false), m, n, k, &alpha, A, lda, stride_a, @@ -356,8 +382,8 @@ void gemm_batched_strided(cublasHandle_t& handle, const char& transa, const char { CHECK_CUBLAS(cublasDgemmStridedBatched( handle, - GetCublasOperation(transa), - GetCublasOperation(transb), + GetCublasOperation(transa, false), + GetCublasOperation(transb, false), m, n, k, &alpha, A, lda, stride_a, @@ -371,15 +397,17 @@ void gemm_batched_strided(cublasHandle_t& handle, const char& transa, const char const std::complex& alpha, const std::complex* A, const int& lda, const int& stride_a, const std::complex* B, const int& ldb, const int& stride_b, const std::complex& beta, std::complex* C, const int& ldc, const int& stride_c, const int& batch_size) { + const cuComplex alpha_cu = make_cuComplex(alpha.real(), alpha.imag()); + const cuComplex beta_cu = make_cuComplex(beta.real(), beta.imag()); CHECK_CUBLAS(cublasCgemmStridedBatched( handle, GetCublasOperation(transa), GetCublasOperation(transb), m, n, k, - reinterpret_cast(&alpha), + &alpha_cu, reinterpret_cast(A), lda, stride_a, reinterpret_cast(B), ldb, stride_b, - reinterpret_cast(&beta), + &beta_cu, reinterpret_cast(C), ldc, stride_c, batch_size)); } @@ -388,15 +416,17 @@ void gemm_batched_strided(cublasHandle_t& handle, const char& transa, const char const std::complex& alpha, const std::complex* A, const int& lda, const int& stride_a, const std::complex* B, const int& ldb, const int& stride_b, const std::complex& beta, std::complex* C, const int& ldc, const int& stride_c, const int& batch_size) { + const cuDoubleComplex alpha_cu = make_cuDoubleComplex(alpha.real(), alpha.imag()); + const cuDoubleComplex beta_cu = make_cuDoubleComplex(beta.real(), beta.imag()); CHECK_CUBLAS(cublasZgemmStridedBatched( handle, GetCublasOperation(transa), GetCublasOperation(transb), m, n, k, - reinterpret_cast(&alpha), + &alpha_cu, reinterpret_cast(A), lda, stride_a, reinterpret_cast(B), ldb, stride_b, - reinterpret_cast(&beta), + &beta_cu, reinterpret_cast(C), ldc, stride_c, batch_size)); } diff --git a/source/source_base/module_container/base/third_party/hipblas.h b/source/source_base/module_container/base/third_party/hipblas.h index 739fbd96b23..52db82b6509 100644 --- a/source/source_base/module_container/base/third_party/hipblas.h +++ b/source/source_base/module_container/base/third_party/hipblas.h @@ -1,6 +1,8 @@ #ifndef BASE_THIRD_PARTY_HIPBLAS_H_ #define BASE_THIRD_PARTY_HIPBLAS_H_ +#include + #include #include #include @@ -117,8 +119,8 @@ void gemv_batched(hipblasHandle_t& handle, const char& trans, const int& m, cons template static inline void gemv_batched_strided(hipblasHandle_t& handle, const char& transa, const int& m, const int& n, - const T& alpha, const T* A, const int& lda, const int& stride_a, const T* x, const int& incx, const int& stride_x, - const T& beta, T* y, const int& incy, const int& stride_y, const int& batch_size) + const T& alpha, const T* A, const int& lda, const int64_t& stride_a, const T* x, const int& incx, const int64_t& stride_x, + const T& beta, T* y, const int& incy, const int64_t& stride_y, const int& batch_size) { for (int ii = 0; ii < batch_size; ii++) { // Call the single GEMV for each pair of matrix A[ii] and vector x[ii] @@ -318,4 +320,4 @@ void gemm_batched_strided(hipblasHandle_t& handle, const char& transa, const cha } // namespace hipBlasConnector } // namespace container -#endif // BASE_THIRD_PARTY_HIPBLAS_H_ \ No newline at end of file +#endif // BASE_THIRD_PARTY_HIPBLAS_H_ diff --git a/source/source_base/module_external/blas_connector_base.cpp b/source/source_base/module_external/blas_connector_base.cpp index 0c041753e25..e0b54f1d8e9 100644 --- a/source/source_base/module_external/blas_connector_base.cpp +++ b/source/source_base/module_external/blas_connector_base.cpp @@ -27,51 +27,27 @@ namespace BlasUtils{ } - cublasOperation_t judge_trans(bool is_complex, const char& trans, const char* name) + cublasOperation_t judge_trans(bool is_complex, const char& trans, const char*) { - if (trans == 'N') - { - return CUBLAS_OP_N; - } - else if(trans == 'T') - { - return CUBLAS_OP_T; - } - else if(is_complex && trans == 'C') - { - return CUBLAS_OP_C; - } - return CUBLAS_OP_N; + return GetCublasOperation(trans, is_complex); } - cublasSideMode_t judge_side(const char& trans) + cublasSideMode_t judge_side(const char& side) { - if (trans == 'L') + if (side == 'L' || side == 'l') { return CUBLAS_SIDE_LEFT; } - else if (trans == 'R') + else if (side == 'R' || side == 'r') { return CUBLAS_SIDE_RIGHT; } - return CUBLAS_SIDE_LEFT; + throw std::invalid_argument(std::string("Invalid BLAS side: ") + side); } - cublasFillMode_t judge_fill(const char& trans) + cublasFillMode_t judge_fill(const char& uplo) { - if (trans == 'F') - { - return CUBLAS_FILL_MODE_FULL; - } - else if (trans == 'U') - { - return CUBLAS_FILL_MODE_UPPER; - } - else if (trans == 'D') - { - return CUBLAS_FILL_MODE_LOWER; - } - return CUBLAS_FILL_MODE_FULL; + return cublas_fill_mode(uplo); } } // namespace BlasUtils diff --git a/source/source_base/module_external/blas_connector_matrix.cpp b/source/source_base/module_external/blas_connector_matrix.cpp index 964ce5cd9db..58b636a24ca 100644 --- a/source/source_base/module_external/blas_connector_matrix.cpp +++ b/source/source_base/module_external/blas_connector_matrix.cpp @@ -14,6 +14,7 @@ void BlasConnector::set_dsp_cluster_id(int id) #ifdef __CUDA #include +#include #include #include "cublas_v2.h" #include "source_base/kernels/math_kernel_op.h" @@ -41,9 +42,7 @@ void BlasConnector::gemm(const char transa, const char transb, const int m, cons #endif #ifdef __CUDA else if (device_type == base_device::AbacusDevice_t::GpuDevice) { - cublasOperation_t cutransA = BlasUtils::judge_trans(false, transa, "gemm_op"); - cublasOperation_t cutransB = BlasUtils::judge_trans(false, transb, "gemm_op"); - CHECK_CUBLAS(cublasSgemm(BlasUtils::cublas_handle, cutransA, cutransB, n, m, k, &alpha, b, ldb, a, lda, &beta, c, ldc)); + gemm_cm(transb, transa, n, m, k, alpha, b, ldb, a, lda, beta, c, ldc, device_type); } #endif else { @@ -79,10 +78,7 @@ void BlasConnector::gemm(const char transa, else if (device_type == base_device::AbacusDevice_t::GpuDevice) { #ifdef __CUDA - cublasOperation_t cutransA = BlasUtils::judge_trans(false, transa, "gemm_op"); - cublasOperation_t cutransB = BlasUtils::judge_trans(false, transb, "gemm_op"); - CHECK_CUBLAS( - cublasDgemm(BlasUtils::cublas_handle, cutransA, cutransB, n, m, k, &alpha, b, ldb, a, lda, &beta, c, ldc)); + gemm_cm(transb, transa, n, m, k, alpha, b, ldb, a, lda, beta, c, ldc, device_type); #endif } else { @@ -120,22 +116,7 @@ void BlasConnector::gemm(const char transa, else if (device_type == base_device::AbacusDevice_t::GpuDevice) { #ifdef __CUDA - cublasOperation_t cutransA = BlasUtils::judge_trans(false, transa, "gemm_op"); - cublasOperation_t cutransB = BlasUtils::judge_trans(false, transb, "gemm_op"); - CHECK_CUBLAS(cublasCgemm(BlasUtils::cublas_handle, - cutransA, - cutransB, - n, - m, - k, - (float2*)&alpha, - (float2*)b, - ldb, - (float2*)a, - lda, - (float2*)&beta, - (float2*)c, - ldc)); + gemm_cm(transb, transa, n, m, k, alpha, b, ldb, a, lda, beta, c, ldc, device_type); #endif } else { @@ -173,22 +154,7 @@ void BlasConnector::gemm(const char transa, else if (device_type == base_device::AbacusDevice_t::GpuDevice) { #ifdef __CUDA - cublasOperation_t cutransA = BlasUtils::judge_trans(false, transa, "gemm_op"); - cublasOperation_t cutransB = BlasUtils::judge_trans(false, transb, "gemm_op"); - CHECK_CUBLAS(cublasZgemm(BlasUtils::cublas_handle, - cutransA, - cutransB, - n, - m, - k, - (double2*)&alpha, - (double2*)b, - ldb, - (double2*)a, - lda, - (double2*)&beta, - (double2*)c, - ldc)); + gemm_cm(transb, transa, n, m, k, alpha, b, ldb, a, lda, beta, c, ldc, device_type); #endif } else { @@ -294,22 +260,8 @@ void BlasConnector::gemm_cm(const char transa, #ifdef __CUDA else if (device_type == base_device::AbacusDevice_t::GpuDevice) { - cublasOperation_t cutransA = BlasUtils::judge_trans(false, transa, "gemm_op"); - cublasOperation_t cutransB = BlasUtils::judge_trans(false, transb, "gemm_op"); - CHECK_CUBLAS(cublasCgemm(BlasUtils::cublas_handle, - cutransA, - cutransB, - m, - n, - k, - (float2*)&alpha, - (float2*)a, - lda, - (float2*)b, - ldb, - (float2*)&beta, - (float2*)c, - ldc)); + container::cuBlasConnector::gemm(BlasUtils::cublas_handle, transa, transb, m, n, k, + alpha, a, lda, b, ldb, beta, c, ldc); } #endif else { @@ -347,22 +299,8 @@ void BlasConnector::gemm_cm(const char transa, #ifdef __CUDA else if (device_type == base_device::AbacusDevice_t::GpuDevice) { - cublasOperation_t cutransA = BlasUtils::judge_trans(false, transa, "gemm_op"); - cublasOperation_t cutransB = BlasUtils::judge_trans(false, transb, "gemm_op"); - CHECK_CUBLAS(cublasZgemm(BlasUtils::cublas_handle, - cutransA, - cutransB, - m, - n, - k, - (double2*)&alpha, - (double2*)a, - lda, - (double2*)b, - ldb, - (double2*)&beta, - (double2*)c, - ldc)); + container::cuBlasConnector::gemm(BlasUtils::cublas_handle, transa, transb, m, n, k, + alpha, a, lda, b, ldb, beta, c, ldc); } #endif else { @@ -427,7 +365,12 @@ void BlasConnector::symm_cm(const char side, const char uplo, const int m, const else if (device_type == base_device::AbacusDevice_t::GpuDevice) { cublasSideMode_t sideMode = BlasUtils::judge_side(side); cublasFillMode_t fillMode = BlasUtils::judge_fill(uplo); - CHECK_CUBLAS(cublasCsymm(BlasUtils::cublas_handle, sideMode, fillMode, m, n, (float2*)&alpha, (float2*)a, lda, (float2*)b, ldb, (float2*)&beta, (float2*)c, ldc)); + const cuComplex alpha_cu = make_cuComplex(alpha.real(), alpha.imag()); + const cuComplex beta_cu = make_cuComplex(beta.real(), beta.imag()); + CHECK_CUBLAS(cublasCsymm(BlasUtils::cublas_handle, sideMode, fillMode, m, n, + &alpha_cu, reinterpret_cast(a), lda, + reinterpret_cast(b), ldb, + &beta_cu, reinterpret_cast(c), ldc)); } #endif else { @@ -448,7 +391,12 @@ void BlasConnector::symm_cm(const char side, const char uplo, const int m, const else if (device_type == base_device::AbacusDevice_t::GpuDevice) { cublasSideMode_t sideMode = BlasUtils::judge_side(side); cublasFillMode_t fillMode = BlasUtils::judge_fill(uplo); - CHECK_CUBLAS(cublasZsymm(BlasUtils::cublas_handle, sideMode, fillMode, m, n, (double2*)&alpha, (double2*)a, lda, (double2*)b, ldb, (double2*)&beta, (double2*)c, ldc)); + const cuDoubleComplex alpha_cu = make_cuDoubleComplex(alpha.real(), alpha.imag()); + const cuDoubleComplex beta_cu = make_cuDoubleComplex(beta.real(), beta.imag()); + CHECK_CUBLAS(cublasZsymm(BlasUtils::cublas_handle, sideMode, fillMode, m, n, + &alpha_cu, reinterpret_cast(a), lda, + reinterpret_cast(b), ldb, + &beta_cu, reinterpret_cast(c), ldc)); } #endif else { @@ -483,7 +431,12 @@ void BlasConnector::hemm_cm(char side, char uplo, int m, int n, else if (device_type == base_device::AbacusDevice_t::GpuDevice) { cublasSideMode_t sideMode = BlasUtils::judge_side(side); cublasFillMode_t fillMode = BlasUtils::judge_fill(uplo); - CHECK_CUBLAS(cublasChemm(BlasUtils::cublas_handle, sideMode, fillMode, m, n, (float2*)&alpha, (float2*)a, lda, (float2*)b, ldb, (float2*)&beta, (float2*)c, ldc)); + const cuComplex alpha_cu = make_cuComplex(alpha.real(), alpha.imag()); + const cuComplex beta_cu = make_cuComplex(beta.real(), beta.imag()); + CHECK_CUBLAS(cublasChemm(BlasUtils::cublas_handle, sideMode, fillMode, m, n, + &alpha_cu, reinterpret_cast(a), lda, + reinterpret_cast(b), ldb, + &beta_cu, reinterpret_cast(c), ldc)); } #endif else { @@ -504,7 +457,12 @@ void BlasConnector::hemm_cm(char side, char uplo, int m, int n, else if (device_type == base_device::AbacusDevice_t::GpuDevice) { cublasSideMode_t sideMode = BlasUtils::judge_side(side); cublasFillMode_t fillMode = BlasUtils::judge_fill(uplo); - CHECK_CUBLAS(cublasZhemm(BlasUtils::cublas_handle, sideMode, fillMode, m, n, (double2*)&alpha, (double2*)a, lda, (double2*)b, ldb, (double2*)&beta, (double2*)c, ldc)); + const cuDoubleComplex alpha_cu = make_cuDoubleComplex(alpha.real(), alpha.imag()); + const cuDoubleComplex beta_cu = make_cuDoubleComplex(beta.real(), beta.imag()); + CHECK_CUBLAS(cublasZhemm(BlasUtils::cublas_handle, sideMode, fillMode, m, n, + &alpha_cu, reinterpret_cast(a), lda, + reinterpret_cast(b), ldb, + &beta_cu, reinterpret_cast(c), ldc)); } #endif else { @@ -650,4 +608,4 @@ void BlasConnector::gemv(const char trans, const int m, const int n, else { throw std::invalid_argument("device_type = " + std::to_string(device_type) + " in " + std::string(__FILE__) + " line " + std::to_string(__LINE__)); } -} \ No newline at end of file +} diff --git a/source/source_base/module_external/blas_connector_vector.cpp b/source/source_base/module_external/blas_connector_vector.cpp index 37b4c327660..b80256be737 100644 --- a/source/source_base/module_external/blas_connector_vector.cpp +++ b/source/source_base/module_external/blas_connector_vector.cpp @@ -9,6 +9,7 @@ #ifdef __CUDA #include +#include #include #include "cublas_v2.h" #include "source_base/module_device/memory_op.h" @@ -52,7 +53,7 @@ void BlasConnector::axpy( const int n, const std::complex alpha, const st } #ifdef __CUDA else if (device_type == base_device::AbacusDevice_t::GpuDevice) { - CHECK_CUBLAS(cublasCaxpy(BlasUtils::cublas_handle, n, (float2*)&alpha, (float2*)X, incX, (float2*)Y, incY)); + container::cuBlasConnector::axpy(BlasUtils::cublas_handle, n, alpha, X, incX, Y, incY); } #endif else { @@ -67,7 +68,7 @@ void BlasConnector::axpy( const int n, const std::complex alpha, const s } #ifdef __CUDA else if (device_type == base_device::AbacusDevice_t::GpuDevice) { - CHECK_CUBLAS(cublasZaxpy(BlasUtils::cublas_handle, n, (double2*)&alpha, (double2*)X, incX, (double2*)Y, incY)); + container::cuBlasConnector::axpy(BlasUtils::cublas_handle, n, alpha, X, incX, Y, incY); } #endif else { @@ -114,7 +115,7 @@ void BlasConnector::scal( const int n, const std::complex alpha, std::com } #ifdef __CUDA else if (device_type == base_device::AbacusDevice_t::GpuDevice) { - CHECK_CUBLAS(cublasCscal(BlasUtils::cublas_handle, n, (float2*)&alpha, (float2*)X, incX)); + container::cuBlasConnector::scal(BlasUtils::cublas_handle, n, alpha, X, incX); } #endif else { @@ -129,7 +130,7 @@ void BlasConnector::scal( const int n, const std::complex alpha, std::co } #ifdef __CUDA else if (device_type == base_device::AbacusDevice_t::GpuDevice) { - CHECK_CUBLAS(cublasZscal(BlasUtils::cublas_handle, n, (double2*)&alpha, (double2*)X, incX)); + container::cuBlasConnector::scal(BlasUtils::cublas_handle, n, alpha, X, incX); } #endif else { diff --git a/source/source_lcao/module_rt/band_energy.cpp b/source/source_lcao/module_rt/band_energy.cpp index 0d1a2cd820b..a73a31d1dfc 100644 --- a/source/source_lcao/module_rt/band_energy.cpp +++ b/source/source_lcao/module_rt/band_energy.cpp @@ -7,6 +7,7 @@ #include "source_base/module_external/scalapack_connector.h" #ifdef __CUBLASMP +#include #include "kernels/cuda/band_energy_kernel.cuh" #endif @@ -163,8 +164,8 @@ void compute_ekb_tensor(const Parallel_Blkcyclic& matrix_layout, ct::TensorShape({static_cast(mo_hamilt_elems)})); void* d_mo_hamilt = static_cast(mo_hamilt_gpu.data>()); - std::complex alpha = {1.0, 0.0}; - std::complex beta = {0.0, 0.0}; + const cuDoubleComplex alpha = make_cuDoubleComplex(1.0, 0.0); + const cuDoubleComplex beta = make_cuDoubleComplex(0.0, 0.0); // 3. Matrix Descriptors Creation cublasMpMatrixDescriptor_t desc_H, desc_Psi, desc_mo_hamilt; diff --git a/source/source_lcao/module_rt/middle_hamilt.cpp b/source/source_lcao/module_rt/middle_hamilt.cpp index 44981a5fed6..6d6294d7755 100644 --- a/source/source_lcao/module_rt/middle_hamilt.cpp +++ b/source/source_lcao/module_rt/middle_hamilt.cpp @@ -7,6 +7,10 @@ #include "source_base/module_external/scalapack_connector.h" #include "source_base/timer.h" +#ifdef __CUBLASMP +#include +#endif + #include #include #include @@ -126,8 +130,8 @@ void half_Hmatrix_tensor(const Parallel_Blkcyclic& layout, cublas_res.cublasmp_grid, &desc_mat); - std::complex alpha = {0.5, 0.0}; - std::complex beta = {0.5, 0.0}; + const cuDoubleComplex alpha = make_cuDoubleComplex(0.5, 0.0); + const cuDoubleComplex beta = make_cuDoubleComplex(0.5, 0.0); size_t ws_size_dev = 0; size_t ws_size_host = 0; diff --git a/source/source_lcao/module_rt/norm_psi.cpp b/source/source_lcao/module_rt/norm_psi.cpp index 81472c4e869..026479e408a 100644 --- a/source/source_lcao/module_rt/norm_psi.cpp +++ b/source/source_lcao/module_rt/norm_psi.cpp @@ -8,6 +8,7 @@ #include "source_base/timer.h" #ifdef __CUBLASMP +#include #include "kernels/cuda/norm_psi_kernel.cuh" #endif @@ -269,8 +270,8 @@ void norm_psi_tensor(const Parallel_Blkcyclic& matrix_layout, cudaMemsetAsync(d_mo_ovlp, 0, cij_elems * sizeof(std::complex), cublas_res.stream); - std::complex alpha = {1.0, 0.0}; - std::complex beta = {0.0, 0.0}; + const cuDoubleComplex alpha = make_cuDoubleComplex(1.0, 0.0); + const cuDoubleComplex beta = make_cuDoubleComplex(0.0, 0.0); cublasMpMatrixDescriptor_t desc_S, desc_Psi, desc_mo_ovlp; diff --git a/source/source_lcao/module_rt/propagator_cn2.cpp b/source/source_lcao/module_rt/propagator_cn2.cpp index 95fca6325ed..9fe990dd804 100644 --- a/source/source_lcao/module_rt/propagator_cn2.cpp +++ b/source/source_lcao/module_rt/propagator_cn2.cpp @@ -10,6 +10,10 @@ #include "source_base/timer.h" #include "source_io/module_parameter/parameter.h" +#ifdef __CUBLASMP +#include +#endif + #include #include #include @@ -318,9 +322,9 @@ void Propagator::compute_propagator_cn2_tensor(const int nlocal, lld); // 4. Construct A (Denominator) and B (Numerator) using Geadd - std::complex one = {1.0, 0.0}; - std::complex coef_neg_i = {0.0, -0.25 * this->dt}; - std::complex coef_pos_i = {0.0, 0.25 * this->dt}; + const cuDoubleComplex one = make_cuDoubleComplex(1.0, 0.0); + const cuDoubleComplex coef_neg_i = make_cuDoubleComplex(0.0, -0.25 * this->dt); + const cuDoubleComplex coef_pos_i = make_cuDoubleComplex(0.0, 0.25 * this->dt); cudaMemcpyAsync(d_Num, d_S, len_loc * sizeof(std::complex), cudaMemcpyDeviceToDevice, cublas_res.stream); cudaMemcpyAsync(d_Den, d_S, len_loc * sizeof(std::complex), cudaMemcpyDeviceToDevice, cublas_res.stream); @@ -495,7 +499,7 @@ void Propagator::compute_propagator_cn2_tensor(const int nlocal, // 7. Solve Triangular System (TRSM) size_t ws_trsm_dev = 0, ws_trsm_host = 0; - std::complex alpha_trsm = {1.0, 0.0}; + const cuDoubleComplex alpha_trsm = make_cuDoubleComplex(1.0, 0.0); cublasMpTrsm_bufferSize(cublas_res.cublasmp_handle, CUBLAS_SIDE_LEFT, diff --git a/source/source_lcao/module_rt/upsi.cpp b/source/source_lcao/module_rt/upsi.cpp index ebc8b770d86..9528aa5240d 100644 --- a/source/source_lcao/module_rt/upsi.cpp +++ b/source/source_lcao/module_rt/upsi.cpp @@ -5,6 +5,10 @@ #include "source_base/module_external/scalapack_connector.h" #include "source_base/timer.h" +#ifdef __CUBLASMP +#include +#endif + #include #include #include @@ -148,8 +152,8 @@ void upsi_tensor(const Parallel_Blkcyclic& matrix_layout, &desc_Psi); // 5. Query workspace size for GEMM: Psi_k = 1.0 * U * Psi_old + 0.0 * Psi_k - std::complex alpha = {1.0, 0.0}; - std::complex beta = {0.0, 0.0}; + const cuDoubleComplex alpha = make_cuDoubleComplex(1.0, 0.0); + const cuDoubleComplex beta = make_cuDoubleComplex(0.0, 0.0); size_t ws_gemm_dev = 0; size_t ws_gemm_host = 0; diff --git a/tests/01_PW/scf_out_band_dos_wf/INPUT b/tests/01_PW/scf_out_band_dos_wf/INPUT index 2248c2d5ea1..18dca6f65f6 100644 --- a/tests/01_PW/scf_out_band_dos_wf/INPUT +++ b/tests/01_PW/scf_out_band_dos_wf/INPUT @@ -12,7 +12,7 @@ pseudo_dir ../../PP_ORB #Parameters (2.Iteration) ecutwfc 20 -scf_thr 1e-8 +scf_thr 1e-13 scf_nmax 100 init_chg atomic @@ -20,7 +20,7 @@ init_wfc random out_band 1 out_dos 1 out_wfc_pw 1 -pw_diag_thr 0.00001 +pw_diag_thr 1e-12 #Parameters (3.Basis) basis_type pw diff --git a/tests/01_PW/scf_out_band_dos_wf/band.txt.ref b/tests/01_PW/scf_out_band_dos_wf/band.txt.ref index c9091976f05..decb2ec08d8 100644 --- a/tests/01_PW/scf_out_band_dos_wf/band.txt.ref +++ b/tests/01_PW/scf_out_band_dos_wf/band.txt.ref @@ -1,2 +1,2 @@ - 1 0.00000000 -5.36802595 6.70427084 6.85743256 7.06566100 9.07047012 9.18721704 - 2 0.86602540 -2.86959821 -0.48990148 5.56030011 5.71333408 8.21423976 9.78665199 + 1 0.00000000 -5.36789216 6.70443738 6.85754677 7.06578719 9.07055676 9.18730685 + 2 0.86602540 -2.86949090 -0.48972283 5.56041862 5.71345957 8.21441150 9.78669842 diff --git a/tests/01_PW/scf_out_band_dos_wf/result.ref b/tests/01_PW/scf_out_band_dos_wf/result.ref index 68646a858af..05445a8cccc 100644 --- a/tests/01_PW/scf_out_band_dos_wf/result.ref +++ b/tests/01_PW/scf_out_band_dos_wf/result.ref @@ -1,16 +1,162 @@ -etotref -205.6688343917421093 -etotperatomref -102.8344171959 -totalforceref 2.063434 -totalstressref 1184.321602 +etotref -205.6688344106788406 +etotperatomref -102.8344172053 +totalforceref 2.063270 +totalstressref 1184.284713 totaldosref 12.000000 CompareBand_pass 0 -Max_wfc_1 0.9183 -Max_wfc_2 0.5000 -Max_wfc_3 0.3992 -Max_wfc_4 0.4681 -Max_wfc_5 0.5574 -Max_wfc_6 0.4435 +pw_ngtot 411 +pw_nband 6 +pw_band1_wfc_fp_norm2 1 +pw_band1_wfc_fp_power_0 0.185525372868 +pw_band1_wfc_fp_power_1 0.753693779676 +pw_band1_wfc_fp_power_2 0.657740127135 +pw_band1_wfc_fp_power_3 1.35522588169 +pw_band1_wfc_fp_power_4 0.624330474358 +pw_band1_wfc_fp_power_5 0.81655238773 +pw_band1_wfc_fp_power_6 0.761775415923 +pw_band1_wfc_fp_power_7 1.37598770526 +pw_band1_wfc_fp_cross_0_re 0.0474957742141 +pw_band1_wfc_fp_cross_0_im -0.370908979312 +pw_band1_wfc_fp_cross_1_re 0.0369431521279 +pw_band1_wfc_fp_cross_1_im 0.703114390392 +pw_band1_wfc_fp_cross_2_re 0.0672892068456 +pw_band1_wfc_fp_cross_2_im -0.941731706146 +pw_band1_wfc_fp_cross_3_re -0.298859862834 +pw_band1_wfc_fp_cross_3_im 0.869937698899 +pw_band1_wfc_fp_cross_4_re -0.63109124354 +pw_band1_wfc_fp_cross_4_im 0.333949669707 +pw_band1_wfc_fp_cross_5_re -0.148331472965 +pw_band1_wfc_fp_cross_5_im -0.774614296869 +pw_band1_wfc_fp_cross_6_re -0.977831563553 +pw_band1_wfc_fp_cross_6_im 0.303379366134 +pw_band1_wfc_fp_cross_7_re -0.242070918957 +pw_band1_wfc_fp_cross_7_im 0.443488784835 +pw_band2_wfc_fp_norm2 1 +pw_band2_wfc_fp_power_0 0.302297489002 +pw_band2_wfc_fp_power_1 0.107849328875 +pw_band2_wfc_fp_power_2 2.3917278522 +pw_band2_wfc_fp_power_3 0.450931389169 +pw_band2_wfc_fp_power_4 0.773718062567 +pw_band2_wfc_fp_power_5 3.2385799892 +pw_band2_wfc_fp_power_6 1.91004944862 +pw_band2_wfc_fp_power_7 1.37831089945 +pw_band2_wfc_fp_cross_0_re 0.150517072475 +pw_band2_wfc_fp_cross_0_im 0.0997356115086 +pw_band2_wfc_fp_cross_1_re -0.386662714445 +pw_band2_wfc_fp_cross_1_im -0.329299542925 +pw_band2_wfc_fp_cross_2_re 0.0906328855077 +pw_band2_wfc_fp_cross_2_im 1.03454861798 +pw_band2_wfc_fp_cross_3_re -0.293455298957 +pw_band2_wfc_fp_cross_3_im 0.512618521215 +pw_band2_wfc_fp_cross_4_re -1.58272753776 +pw_band2_wfc_fp_cross_4_im -0.0268584421564 +pw_band2_wfc_fp_cross_5_re 2.0312268086 +pw_band2_wfc_fp_cross_5_im -1.43525801677 +pw_band2_wfc_fp_cross_6_re 1.1054848961 +pw_band2_wfc_fp_cross_6_im 1.18766372261 +pw_band2_wfc_fp_cross_7_re 0.236689509751 +pw_band2_wfc_fp_cross_7_im 0.600531431269 +pw_band3_wfc_fp_norm2 1 +pw_band3_wfc_fp_power_0 0.680965193426 +pw_band3_wfc_fp_power_1 0.298896923962 +pw_band3_wfc_fp_power_2 0.80613101236 +pw_band3_wfc_fp_power_3 0.294248208534 +pw_band3_wfc_fp_power_4 0.655267128291 +pw_band3_wfc_fp_power_5 0.0174562918527 +pw_band3_wfc_fp_power_6 0.554369880832 +pw_band3_wfc_fp_power_7 0.195452420289 +pw_band3_wfc_fp_cross_0_re 0.333182914192 +pw_band3_wfc_fp_cross_0_im -0.304183410677 +pw_band3_wfc_fp_cross_1_re -0.375822955796 +pw_band3_wfc_fp_cross_1_im -0.315764446703 +pw_band3_wfc_fp_cross_2_re -0.162049159581 +pw_band3_wfc_fp_cross_2_im 0.459284961773 +pw_band3_wfc_fp_cross_3_re -0.0907702131538 +pw_band3_wfc_fp_cross_3_im 0.429618373694 +pw_band3_wfc_fp_cross_4_re -0.0104769955943 +pw_band3_wfc_fp_cross_4_im -0.10643667975 +pw_band3_wfc_fp_cross_5_re 0.0224390286811 +pw_band3_wfc_fp_cross_5_im 0.0957796033923 +pw_band3_wfc_fp_cross_6_re -0.158666457962 +pw_band3_wfc_fp_cross_6_im -0.28840577328 +pw_band3_wfc_fp_cross_7_re -0.116473606086 +pw_band3_wfc_fp_cross_7_im 0.345731390349 +pw_band4_wfc_fp_norm2 1 +pw_band4_wfc_fp_power_0 0.264142681081 +pw_band4_wfc_fp_power_1 0.339567920111 +pw_band4_wfc_fp_power_2 0.107104825341 +pw_band4_wfc_fp_power_3 0.338023375461 +pw_band4_wfc_fp_power_4 0.0100155907454 +pw_band4_wfc_fp_power_5 1.02012979183 +pw_band4_wfc_fp_power_6 0.0080383769202 +pw_band4_wfc_fp_power_7 1.54806624593 +pw_band4_wfc_fp_cross_0_re -0.00847503603372 +pw_band4_wfc_fp_cross_0_im 0.299370263372 +pw_band4_wfc_fp_cross_1_re 0.0862579388903 +pw_band4_wfc_fp_cross_1_im 0.170085069166 +pw_band4_wfc_fp_cross_2_re 0.171579027682 +pw_band4_wfc_fp_cross_2_im -0.0822470172678 +pw_band4_wfc_fp_cross_3_re -0.0572962639388 +pw_band4_wfc_fp_cross_3_im 0.0101312353469 +pw_band4_wfc_fp_cross_4_re -0.0942157583382 +pw_band4_wfc_fp_cross_4_im 0.0366141145319 +pw_band4_wfc_fp_cross_5_re -0.0890967160322 +pw_band4_wfc_fp_cross_5_im -0.0161852700492 +pw_band4_wfc_fp_cross_6_re -0.0673381410128 +pw_band4_wfc_fp_cross_6_im 0.0889354527011 +pw_band4_wfc_fp_cross_7_re 0.364197912705 +pw_band4_wfc_fp_cross_7_im -0.52561416369 +pw_band5_wfc_fp_norm2 1 +pw_band5_wfc_fp_power_0 1.87925309774 +pw_band5_wfc_fp_power_1 0.620139051721 +pw_band5_wfc_fp_power_2 0.301899769543 +pw_band5_wfc_fp_power_3 0.845828264238 +pw_band5_wfc_fp_power_4 1.35238337223 +pw_band5_wfc_fp_power_5 0.0499104807417 +pw_band5_wfc_fp_power_6 1.90377197218 +pw_band5_wfc_fp_power_7 0.0757552353826 +pw_band5_wfc_fp_cross_0_re -0.519333985083 +pw_band5_wfc_fp_cross_0_im 0.946409238077 +pw_band5_wfc_fp_cross_1_re 0.305297268376 +pw_band5_wfc_fp_cross_1_im -0.306616070553 +pw_band5_wfc_fp_cross_2_re -0.132104163831 +pw_band5_wfc_fp_cross_2_im -0.487753880502 +pw_band5_wfc_fp_cross_3_re -0.803110320387 +pw_band5_wfc_fp_cross_3_im 0.706327044371 +pw_band5_wfc_fp_cross_4_re -0.22768934173 +pw_band5_wfc_fp_cross_4_im 0.125122611538 +pw_band5_wfc_fp_cross_5_re -0.274845580332 +pw_band5_wfc_fp_cross_5_im 0.13956389693 +pw_band5_wfc_fp_cross_6_re 0.367806203581 +pw_band5_wfc_fp_cross_6_im 0.0945478211004 +pw_band5_wfc_fp_cross_7_re 0.154361050096 +pw_band5_wfc_fp_cross_7_im -0.344290468902 +pw_band6_wfc_fp_norm2 1 +pw_band6_wfc_fp_power_0 0.492362209109 +pw_band6_wfc_fp_power_1 1.16001307031 +pw_band6_wfc_fp_power_2 0.486234066385 +pw_band6_wfc_fp_power_3 0.403517923891 +pw_band6_wfc_fp_power_4 1.47589770612 +pw_band6_wfc_fp_power_5 0.841683376844 +pw_band6_wfc_fp_power_6 2.63809976544 +pw_band6_wfc_fp_power_7 0.57139365968 +pw_band6_wfc_fp_cross_0_re -0.48590158496 +pw_band6_wfc_fp_cross_0_im -0.578831795627 +pw_band6_wfc_fp_cross_1_re -0.487735620731 +pw_band6_wfc_fp_cross_1_im 0.571097046489 +pw_band6_wfc_fp_cross_2_re -0.00844104987903 +pw_band6_wfc_fp_cross_2_im -0.442868953156 +pw_band6_wfc_fp_cross_3_re -0.352145805936 +pw_band6_wfc_fp_cross_3_im -0.686690985531 +pw_band6_wfc_fp_cross_4_re -0.0228446438236 +pw_band6_wfc_fp_cross_4_im 1.11432342137 +pw_band6_wfc_fp_cross_5_re -0.0631386807554 +pw_band6_wfc_fp_cross_5_im 1.48877742662 +pw_band6_wfc_fp_cross_6_re -0.968746637514 +pw_band6_wfc_fp_cross_6_im 0.754270131904 +pw_band6_wfc_fp_cross_7_re -0.248550436545 +pw_band6_wfc_fp_cross_7_im -0.468567311114 pointgroupref C_1h spacegroupref C_2h nksibzref 2 -totaltimeref 0.37 +totaltimeref 0.43 diff --git a/tests/01_PW/scf_out_band_dos_wf/threshold b/tests/01_PW/scf_out_band_dos_wf/threshold new file mode 100644 index 00000000000..145f85786f9 --- /dev/null +++ b/tests/01_PW/scf_out_band_dos_wf/threshold @@ -0,0 +1 @@ +wfc_threshold 5e-6 diff --git a/tests/11_PW_GPU/scf_out_wf/INPUT b/tests/11_PW_GPU/scf_out_wf/INPUT index c44e78cd62c..4689866ede5 100644 --- a/tests/11_PW_GPU/scf_out_wf/INPUT +++ b/tests/11_PW_GPU/scf_out_wf/INPUT @@ -12,7 +12,8 @@ init_wfc random #Parameters (2.Iteration) ecutwfc 20 -scf_thr 1e-9 +scf_thr 1e-13 +pw_diag_thr 1e-12 scf_nmax 100 #Parameters (3.Basis) diff --git a/tests/11_PW_GPU/scf_out_wf/README b/tests/11_PW_GPU/scf_out_wf/README index 34d0b67a536..98b94cbf65d 100644 --- a/tests/11_PW_GPU/scf_out_wf/README +++ b/tests/11_PW_GPU/scf_out_wf/README @@ -1 +1 @@ -GPU version / test the output of wfc_pw, and verify the maximum absolute coefficient +Check the norms and phase-invariant fingerprints of the bands in wfk1_pw.txt. diff --git a/tests/11_PW_GPU/scf_out_wf/STRU b/tests/11_PW_GPU/scf_out_wf/STRU index 0041740d12b..5fa6dbfa088 100644 --- a/tests/11_PW_GPU/scf_out_wf/STRU +++ b/tests/11_PW_GPU/scf_out_wf/STRU @@ -16,4 +16,4 @@ Si // Element type 0.0 // magnetism 2 0.00 0.00 0.00 1 1 1 -0.25 0.25 0.25 1 1 1 +0.23 0.27 0.31 1 1 1 diff --git a/tests/11_PW_GPU/scf_out_wf/result.ref b/tests/11_PW_GPU/scf_out_wf/result.ref index dd4a5ac6920..679845ba0a4 100644 --- a/tests/11_PW_GPU/scf_out_wf/result.ref +++ b/tests/11_PW_GPU/scf_out_wf/result.ref @@ -1,12 +1,158 @@ -etotref -211.8032873347885 -etotperatomref -105.9016436674 -Max_wfc_1 0.9527 -Max_wfc_2 0.3675 -Max_wfc_3 0.3351 -Max_wfc_4 0.3825 -Max_wfc_5 0.4409 -Max_wfc_6 0.4752 -pointgroupref T_d -spacegroupref O_h -nksibzref 3 -totaltimeref 3.06 +etotref -211.5558975100734 +etotperatomref -105.7779487550 +pw_ngtot 411 +pw_nband 6 +pw_band1_wfc_fp_norm2 1 +pw_band1_wfc_fp_power_0 0.187553102328 +pw_band1_wfc_fp_power_1 1.082276633 +pw_band1_wfc_fp_power_2 0.883123676727 +pw_band1_wfc_fp_power_3 1.29411418163 +pw_band1_wfc_fp_power_4 0.480183052646 +pw_band1_wfc_fp_power_5 0.682935138881 +pw_band1_wfc_fp_power_6 0.73058714359 +pw_band1_wfc_fp_power_7 1.38019637753 +pw_band1_wfc_fp_cross_0_re 0.0853368299687 +pw_band1_wfc_fp_cross_0_im -0.442382148767 +pw_band1_wfc_fp_cross_1_re -0.0346015135529 +pw_band1_wfc_fp_cross_1_im 0.977029607858 +pw_band1_wfc_fp_cross_2_re -0.0197551661733 +pw_band1_wfc_fp_cross_2_im -1.06886510262 +pw_band1_wfc_fp_cross_3_re -0.270023158245 +pw_band1_wfc_fp_cross_3_im 0.740607313101 +pw_band1_wfc_fp_cross_4_re -0.510972345316 +pw_band1_wfc_fp_cross_4_im 0.258536539139 +pw_band1_wfc_fp_cross_5_re -0.189281987693 +pw_band1_wfc_fp_cross_5_im -0.680526238662 +pw_band1_wfc_fp_cross_6_re -0.959178901058 +pw_band1_wfc_fp_cross_6_im 0.297202901766 +pw_band1_wfc_fp_cross_7_re -0.236327188564 +pw_band1_wfc_fp_cross_7_im 0.450565835782 +pw_band2_wfc_fp_norm2 1 +pw_band2_wfc_fp_power_0 0.300286466043 +pw_band2_wfc_fp_power_1 0.497161148431 +pw_band2_wfc_fp_power_2 1.01094242456 +pw_band2_wfc_fp_power_3 0.160560566018 +pw_band2_wfc_fp_power_4 0.415260681644 +pw_band2_wfc_fp_power_5 0.129046952238 +pw_band2_wfc_fp_power_6 1.04106047385 +pw_band2_wfc_fp_power_7 0.0659685492215 +pw_band2_wfc_fp_cross_0_re 0.314752463421 +pw_band2_wfc_fp_cross_0_im -0.22410187658 +pw_band2_wfc_fp_cross_1_re -0.655058666446 +pw_band2_wfc_fp_cross_1_im -0.271107801999 +pw_band2_wfc_fp_cross_2_re -0.32219954902 +pw_band2_wfc_fp_cross_2_im 0.241877941348 +pw_band2_wfc_fp_cross_3_re 0.0968397648267 +pw_band2_wfc_fp_cross_3_im 0.239366977752 +pw_band2_wfc_fp_cross_4_re 0.143908278763 +pw_band2_wfc_fp_cross_4_im 0.181324385161 +pw_band2_wfc_fp_cross_5_re 0.329323471321 +pw_band2_wfc_fp_cross_5_im -0.160909081416 +pw_band2_wfc_fp_cross_6_re -0.207354065031 +pw_band2_wfc_fp_cross_6_im -0.160254612497 +pw_band2_wfc_fp_cross_7_re -0.0212137296542 +pw_band2_wfc_fp_cross_7_im 0.139138205357 +pw_band3_wfc_fp_norm2 1 +pw_band3_wfc_fp_power_0 0.848730067637 +pw_band3_wfc_fp_power_1 0.0824698638182 +pw_band3_wfc_fp_power_2 2.26885810543 +pw_band3_wfc_fp_power_3 0.265069955306 +pw_band3_wfc_fp_power_4 1.01928241894 +pw_band3_wfc_fp_power_5 1.87491909223 +pw_band3_wfc_fp_power_6 1.16707958933 +pw_band3_wfc_fp_power_7 0.786181928143 +pw_band3_wfc_fp_cross_0_re 0.231138167285 +pw_band3_wfc_fp_cross_0_im 0.128723737983 +pw_band3_wfc_fp_cross_1_re -0.421364226342 +pw_band3_wfc_fp_cross_1_im -0.0977988125503 +pw_band3_wfc_fp_cross_2_re 0.444724236571 +pw_band3_wfc_fp_cross_2_im 0.63531603947 +pw_band3_wfc_fp_cross_3_re -0.403223213901 +pw_band3_wfc_fp_cross_3_im 0.328012476903 +pw_band3_wfc_fp_cross_4_re -1.34092775884 +pw_band3_wfc_fp_cross_4_im -0.336132136526 +pw_band3_wfc_fp_cross_5_re 1.39587144246 +pw_band3_wfc_fp_cross_5_im -0.489614869372 +pw_band3_wfc_fp_cross_6_re 0.942791357021 +pw_band3_wfc_fp_cross_6_im 0.169355658203 +pw_band3_wfc_fp_cross_7_re -0.370992130944 +pw_band3_wfc_fp_cross_7_im 0.727750698952 +pw_band4_wfc_fp_norm2 1 +pw_band4_wfc_fp_power_0 0.321385701038 +pw_band4_wfc_fp_power_1 0.37227110208 +pw_band4_wfc_fp_power_2 0.270168354125 +pw_band4_wfc_fp_power_3 0.523754031003 +pw_band4_wfc_fp_power_4 0.0304074418681 +pw_band4_wfc_fp_power_5 1.92324053872 +pw_band4_wfc_fp_power_6 0.493739184833 +pw_band4_wfc_fp_power_7 2.32771548564 +pw_band4_wfc_fp_cross_0_re -0.0390406108439 +pw_band4_wfc_fp_cross_0_im 0.343683633336 +pw_band4_wfc_fp_cross_1_re 0.0315884077824 +pw_band4_wfc_fp_cross_1_im -0.315559888818 +pw_band4_wfc_fp_cross_2_re -0.141515886857 +pw_band4_wfc_fp_cross_2_im 0.348532664594 +pw_band4_wfc_fp_cross_3_re -0.0107048607332 +pw_band4_wfc_fp_cross_3_im 0.125743493699 +pw_band4_wfc_fp_cross_4_re -0.222529649858 +pw_band4_wfc_fp_cross_4_im -0.0946645647188 +pw_band4_wfc_fp_cross_5_re 0.302882195949 +pw_band4_wfc_fp_cross_5_im -0.92619738242 +pw_band4_wfc_fp_cross_6_re 0.467774676561 +pw_band4_wfc_fp_cross_6_im 0.964609350137 +pw_band4_wfc_fp_cross_7_re 0.502938860695 +pw_band4_wfc_fp_cross_7_im -0.703666807213 +pw_band5_wfc_fp_norm2 1 +pw_band5_wfc_fp_power_0 2.07733212126 +pw_band5_wfc_fp_power_1 0.272244366275 +pw_band5_wfc_fp_power_2 0.51409553914 +pw_band5_wfc_fp_power_3 0.829869042493 +pw_band5_wfc_fp_power_4 1.45571911947 +pw_band5_wfc_fp_power_5 0.0447699904246 +pw_band5_wfc_fp_power_6 1.01233557447 +pw_band5_wfc_fp_power_7 0.0417145099515 +pw_band5_wfc_fp_cross_0_re 0.372026483741 +pw_band5_wfc_fp_cross_0_im 0.653558155247 +pw_band5_wfc_fp_cross_1_re 0.340133867716 +pw_band5_wfc_fp_cross_1_im -0.155783716385 +pw_band5_wfc_fp_cross_2_re -0.0533923718377 +pw_band5_wfc_fp_cross_2_im -0.65098481353 +pw_band5_wfc_fp_cross_3_re -1.09193661591 +pw_band5_wfc_fp_cross_3_im 0.125421922479 +pw_band5_wfc_fp_cross_4_re -0.183305802699 +pw_band5_wfc_fp_cross_4_im -0.177683746405 +pw_band5_wfc_fp_cross_5_re 0.07882001202 +pw_band5_wfc_fp_cross_5_im 0.197761623377 +pw_band5_wfc_fp_cross_6_re -0.202360710503 +pw_band5_wfc_fp_cross_6_im 0.0357662584043 +pw_band5_wfc_fp_cross_7_re -0.23554349428 +pw_band5_wfc_fp_cross_7_im 0.176562039373 +pw_band6_wfc_fp_norm2 1 +pw_band6_wfc_fp_power_0 0.409858290815 +pw_band6_wfc_fp_power_1 3.60363686716 +pw_band6_wfc_fp_power_2 0.565433198558 +pw_band6_wfc_fp_power_3 0.393433845192 +pw_band6_wfc_fp_power_4 3.1336554584 +pw_band6_wfc_fp_power_5 0.18677107486 +pw_band6_wfc_fp_power_6 0.563241222011 +pw_band6_wfc_fp_power_7 0.236647508289 +pw_band6_wfc_fp_cross_0_re -0.663912128304 +pw_band6_wfc_fp_cross_0_im -1.01793965095 +pw_band6_wfc_fp_cross_1_re 0.980770398904 +pw_band6_wfc_fp_cross_1_im 1.03716215939 +pw_band6_wfc_fp_cross_2_re -0.45494412609 +pw_band6_wfc_fp_cross_2_im -0.124444363648 +pw_band6_wfc_fp_cross_3_re 0.903417386097 +pw_band6_wfc_fp_cross_3_im 0.645540969267 +pw_band6_wfc_fp_cross_4_re -0.746306925973 +pw_band6_wfc_fp_cross_4_im -0.168232489287 +pw_band6_wfc_fp_cross_5_re 0.0915430649289 +pw_band6_wfc_fp_cross_5_im 0.311154359931 +pw_band6_wfc_fp_cross_6_re 0.106725164703 +pw_band6_wfc_fp_cross_6_im 0.349140904183 +pw_band6_wfc_fp_cross_7_re -0.214952552492 +pw_band6_wfc_fp_cross_7_im -0.225360474463 +pointgroupref C_1 +spacegroupref S_2 +nksibzref 8 +totaltimeref 3.99 diff --git a/tests/11_PW_GPU/scf_out_wf/threshold b/tests/11_PW_GPU/scf_out_wf/threshold new file mode 100644 index 00000000000..145f85786f9 --- /dev/null +++ b/tests/11_PW_GPU/scf_out_wf/threshold @@ -0,0 +1 @@ +wfc_threshold 5e-6 diff --git a/tests/integrate/Autotest.sh b/tests/integrate/Autotest.sh index f2d901432d4..546626ec9ef 100755 --- a/tests/integrate/Autotest.sh +++ b/tests/integrate/Autotest.sh @@ -240,7 +240,7 @@ check_out() { compare_thr=$thr if [[ $key == ml_desc_mean_* ]]; then compare_thr=$descriptor_thr - elif [[ $key == wfi*.cube || $key == *_wfc_fp_rms || $key == *_wfc_fp_power_* || $key == *_wfc_fp_cross_* ]]; then + elif [[ $key == wfi*.cube || $key == *_wfc_fp_rms || $key == *_wfc_fp_norm2 || $key == *_wfc_fp_power_* || $key == *_wfc_fp_cross_* ]]; then compare_thr=$wfc_thr elif [[ $key == dfpt_freq_* || $key == dfpt_loto_* ]]; then # DFPT phonon frequencies (cm^-1): same-binary reruns reproduce diff --git a/tests/integrate/validation_tools/collect_wfc_props.py b/tests/integrate/validation_tools/collect_wfc_props.py new file mode 100644 index 00000000000..37aa2208530 --- /dev/null +++ b/tests/integrate/validation_tools/collect_wfc_props.py @@ -0,0 +1,108 @@ +"""Collect band norms and phase-invariant fingerprints from the first PW text frame.""" + +import argparse +import math +from pathlib import Path + + +FINGERPRINT_SKETCHES = 8 +UINT64_MASK = (1 << 64) - 1 +SPLITMIX_GOLDEN = 0x9E3779B97F4A7C15 +SPLITMIX_MIX1 = 0xBF58476D1CE4E5B9 +SPLITMIX_MIX2 = 0x94D049BB133111EB +FINGERPRINT_WEIGHTS = (1.0 + 0.0j, -1.0 + 0.0j, 0.0 + 1.0j, 0.0 - 1.0j) + + +def splitmix64(value): + value = (value + SPLITMIX_GOLDEN) & UINT64_MASK + value = ((value ^ (value >> 30)) * SPLITMIX_MIX1) & UINT64_MASK + value = ((value ^ (value >> 27)) * SPLITMIX_MIX2) & UINT64_MASK + return value ^ (value >> 31) + + +def wfc_projections(components): + """Return the raw squared norm and the phase-invariant projection products.""" + norm2 = sum( + value.real * value.real + value.imag * value.imag + for component in components + for value in component + ) + if not math.isfinite(norm2) or norm2 == 0.0: + raise ValueError("wavefunction has zero or non-finite norm") + + projections = [0.0j] * FINGERPRINT_SKETCHES + flat_index = 0 + for component in components: + for value in component: + for sketch in range(FINGERPRINT_SKETCHES): + weight_index = splitmix64(flat_index + sketch * SPLITMIX_GOLDEN) & 3 + projections[sketch] += FINGERPRINT_WEIGHTS[weight_index] * value + flat_index += 1 + + norm = math.sqrt(norm2) + normalized = [projection / norm for projection in projections] + metrics = [(f"power_{j}", abs(q) ** 2) for j, q in enumerate(normalized)] + for j, q in enumerate(normalized): + cross = q * normalized[(j + 1) % FINGERPRINT_SKETCHES].conjugate() + metrics.append((f"cross_{j}_re", cross.real)) + metrics.append((f"cross_{j}_im", cross.imag)) + return norm2, metrics + + +def read_block(lines, tag): + """Read an ABACUS text block, whose opening and closing tags are identical.""" + if next(lines, None) != tag: + raise ValueError(f"expected {tag}") + rows = [] + for line in lines: + if line == tag: + return rows + rows.append(line) + raise ValueError(f"missing closing {tag}") + + +def pw_fingerprint(stream): + lines = (line.strip() for line in stream if line.strip()) + header = next(lines, "") + columns = header.split() + values = next(lines, "").split() + if columns[6:8] != ["ngtot", "nband"] or len(values) != len(columns): + raise ValueError("invalid PW wavefunction header") + ng, nbands = map(int, values[6:8]) + if ng <= 0 or nbands <= 0: + raise ValueError(f"invalid dimensions: ngtot={ng}, nband={nbands}") + + read_block(lines, "") + g_vectors = [] + for row in read_block(lines, ""): + gx, gy, gz = map(int, row.split()) + g_vectors.append((gx, gy, gz)) + if len(g_vectors) != ng or len(set(g_vectors)) != ng: + raise ValueError(f"expected {ng} distinct G vectors") + # MPI ranks append their G vectors and coefficients in the same order. + order = sorted(range(ng), key=lambda i: g_vectors[i]) + metrics = [("pw_ngtot", ng), ("pw_nband", nbands)] + for band in range(1, nbands + 1): + rows = read_block(lines, f"< Band {band} >") + fields = [float(value) for row in rows for value in row.split()] + if len(fields) != 2 * ng: + raise ValueError(f"band {band}: expected {ng} real/imaginary pairs") + coeffs = [complex(fields[2 * i], fields[2 * i + 1]) for i in order] + norm2, products = wfc_projections([coeffs]) + prefix = f"pw_band{band}_wfc_fp_" + metrics.append((prefix + "norm2", norm2)) + metrics.extend((prefix + key, value) for key, value in products) + + # Appended ionic steps start with a new header. + if next(lines, header) != header: + raise ValueError(f"unexpected data after band {nbands}") + return metrics + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("path", type=Path) + args = parser.parse_args() + with args.path.open(encoding="utf-8") as stream: + for key, value in pw_fingerprint(stream): + print(f"{key} {value:.12g}") diff --git a/tests/integrate/validation_tools/cube_tool.py b/tests/integrate/validation_tools/cube_tool.py index a390ea62dbb..051e7e2e0c4 100755 --- a/tests/integrate/validation_tools/cube_tool.py +++ b/tests/integrate/validation_tools/cube_tool.py @@ -55,13 +55,7 @@ import sys from pathlib import Path - -FINGERPRINT_SKETCHES = 8 -UINT64_MASK = (1 << 64) - 1 -SPLITMIX_GOLDEN = 0x9E3779B97F4A7C15 -SPLITMIX_MIX1 = 0xBF58476D1CE4E5B9 -SPLITMIX_MIX2 = 0x94D049BB133111EB -FINGERPRINT_WEIGHTS = (1.0 + 0.0j, -1.0 + 0.0j, 0.0 + 1.0j, 0.0 - 1.0j) +from collect_wfc_props import wfc_projections def read_cube(path): @@ -183,13 +177,6 @@ def integrate_last_cube(path): return sum(values) * volume -def splitmix64(value): - value = (value + SPLITMIX_GOLDEN) & UINT64_MASK - value = ((value ^ (value >> 30)) * SPLITMIX_MIX1) & UINT64_MASK - value = ((value ^ (value >> 27)) * SPLITMIX_MIX2) & UINT64_MASK - return value ^ (value >> 31) - - def read_wfc_components(paths): if len(paths) == 0 or len(paths) % 2 != 0: raise ValueError("expected one or more RE IM cube path pairs") @@ -232,42 +219,16 @@ def read_wfc_components(paths): def wfc_fingerprint(paths): shape, volume, components = read_wfc_components(paths) point_count = sum(len(component) for component in components) - energy = sum( - value.real * value.real + value.imag * value.imag - for component in components - for value in component - ) - if not math.isfinite(energy) or energy == 0.0: - raise ValueError("wavefunction has zero or non-finite norm") - - projections = [0.0j] * FINGERPRINT_SKETCHES - flat_index = 0 - for component in components: - for value in component: - for sketch in range(FINGERPRINT_SKETCHES): - weight_index = splitmix64(flat_index + sketch * SPLITMIX_GOLDEN) & 3 - projections[sketch] += FINGERPRINT_WEIGHTS[weight_index] * value - flat_index += 1 - - norm = math.sqrt(energy) - normalized = [projection / norm for projection in projections] + norm2, products = wfc_projections(components) metrics = [ ("components", len(components)), ("nx", shape[0]), ("ny", shape[1]), ("nz", shape[2]), ("voxel", volume), - ("rms", math.sqrt(energy / point_count)), + ("rms", math.sqrt(norm2 / point_count)), ] - metrics.extend( - (f"power_{sketch}", abs(normalized[sketch]) ** 2) - for sketch in range(FINGERPRINT_SKETCHES) - ) - for sketch in range(FINGERPRINT_SKETCHES): - next_sketch = (sketch + 1) % FINGERPRINT_SKETCHES - cross = normalized[sketch] * normalized[next_sketch].conjugate() - metrics.append((f"cross_{sketch}_re", cross.real)) - metrics.append((f"cross_{sketch}_im", cross.imag)) + metrics.extend(products) if any(not math.isfinite(float(value)) for _, value in metrics): raise ValueError("wavefunction fingerprint contains a non-finite value") diff --git a/tests/integrate/validation_tools/props_cube.sh b/tests/integrate/validation_tools/props_cube.sh index 7b599439092..4ff82b16ec1 100644 --- a/tests/integrate/validation_tools/props_cube.sh +++ b/tests/integrate/validation_tools/props_cube.sh @@ -126,19 +126,7 @@ run_cube_props() { echo "Can't find file $full_path" exit 1 fi - awk 'BEGIN {max=0;read=0;band=1} - { - if(read==0 && $2 == "Band" && $3 == band){read=1} - else if(read==1 && $2 == "Band" && $3 == band) - {printf"Max_wfc_%d %.4f\n",band,max;read =0;band+=1;max=0} - else if(read==1) - { - for(i=1;i<=NF;i++) - { - if(sqrt($i*$i)>max) {max=sqrt($i*$i)} - } - } - }' "$full_path" >>"$props_result_file" + python3 "$PROPS_TOOLS_DIR/collect_wfc_props.py" "$full_path" >>"$props_result_file" || exit 1 fi #--------------------------------------------