Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ endif()
# version. Use cmake with AMD-ROCm:
# https://rocmdocs.amd.com/en/latest/Installation_Guide/Using-CMake-with-AMD-ROCm.html
if(USE_ROCM)
set_if_higher(CMAKE_CXX_STANDARD 14)
message(
DEPRECATION
"Option USE_ROCM is deprecated and will be ignored in a future release. ")
Expand All @@ -485,7 +486,6 @@ if(USE_ROCM)
endif()
endif()
set(CMAKE_MODULE_PATH "${HIP_PATH}/cmake" ${CMAKE_MODULE_PATH})
set(HIP_HIPCC_FLAGS -fno-gpu-rdc; --std=c++14) # --amdgpu-target=gfx906

# find_package(ROCM REQUIRED)
find_package(HIP REQUIRED)
Expand Down
7 changes: 7 additions & 0 deletions docs/advanced/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ The [ELPA](https://elpa.mpcdf.mpg.de/) library provides highly efficient and hig

To build ABACUS with ELPA support, pass `-DENABLE_ELPA=ON` to CMake.

For AMD GPUs and DCUs, build ELPA 2024.03.001 or later with `--enable-amd-gpu`
and make its pkg-config directory available through `PKG_CONFIG_PATH`. Configure ABACUS
with `-DENABLE_MPI=ON -DENABLE_LCAO=ON -DENABLE_ELPA=ON -DUSE_ROCM=ON`.
Select `basis_type lcao`, `ks_solver elpa`, and `device gpu` in INPUT.
For a distributed single-k-point solve, set `kpar 1` and launch one MPI rank
per GPU.

## Build with Libxc

ABACUS use exchange-correlation functionals by default. However, for some functionals (such as HSE hybrid functional), Libxc is required.
Expand Down
21 changes: 9 additions & 12 deletions source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -380,14 +380,8 @@ if(USE_CUDA)
endif()

if(USE_ROCM)
list(
APPEND
_abacus_feature_libs
hip::host
hip::device
hip::hipfft
roc::hipblas
roc::hipsolver)
list(APPEND _abacus_feature_libs hip::host hip::hipfft roc::hipblas
roc::hipsolver)
list(APPEND _abacus_feature_include_dirs ${ROCM_PATH}/include)
endif()

Expand Down Expand Up @@ -428,7 +422,6 @@ foreach(
cusolverMp::cusolverMp
cublasMp::cublasMp
hip::host
hip::device
hip::hipfft
roc::hipblas
roc::hipsolver)
Expand Down Expand Up @@ -737,14 +730,18 @@ if(USE_ROCM)
source_hsolver/module_linalg/kernels/rocm/linear_op.hip.cu
source_estate/kernels/rocm/elecstate_op.hip.cu
source_base/module_device/rocm/memory_op.hip.cu
source_pw/module_pwdft/kernels/rocm/exx_pw_op.hip.cu
source_pw/module_pwdft/kernels/rocm/force_op.hip.cu
source_pw/module_pwdft/kernels/rocm/stress_op.hip.cu
source_cell/kernels/rocm/wf_op.hip.cu
source_pw/module_pwdft/kernels/rocm/vnl_op.hip.cu
source_base/kernels/rocm/math_kernel_op.hip.cu
source_base/kernels/rocm/math_kernel_op_vec.hip.cu
source_base/kernels/rocm/math_ylm_op.hip.cu
source_hamilt/module_xc/kernels/rocm/xc_functional_op.hip.cu)
source_hamilt/module_xc/kernels/rocm/xc_functional_op.hip.cu
HIPCC_OPTIONS
-fno-gpu-rdc
--std=c++${CMAKE_CXX_STANDARD})
endif()

if(USE_DSP)
Expand All @@ -756,8 +753,8 @@ add_library(device OBJECT ${device_srcs})
if(USE_CUDA)
target_link_libraries(device PRIVATE cusolver cublas cufft)
elseif(USE_ROCM)
target_link_libraries(device PRIVATE device_rocm hip::host hip::device
hip::hipfft roc::hipblas roc::hipsolver)
target_link_libraries(device PRIVATE device_rocm hip::host hip::hipfft
roc::hipblas roc::hipsolver)
endif()

# base uses symbols from device (memory_op and math_ylm_op).
Expand Down
6 changes: 3 additions & 3 deletions source/source_base/kernels/rocm/math_kernel_op.hip.cu
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ void matrix_mul_vector_op<double, base_device::DEVICE_GPU>::operator()(const int
double *a, const int &lda, const double *b, const double alpha, double *c, const int &ldc){
dim3 thread(16, 16, 1);
dim3 block((m + thread.x - 1) / thread.x, (n + thread.y - 1) / thread.y, 1);
hipLaunchKernelGGL(HIP_KERNEL_NAME(matrix_multiply_vector_kernel<double, double>), dim3(block, thread),
hipLaunchKernelGGL(HIP_KERNEL_NAME(matrix_multiply_vector_kernel<double, double>), block, thread, 0, 0,
m, n, a, lda, b, alpha, c, ldc);
hipCheckOnDebug();
}
Expand All @@ -505,7 +505,7 @@ void matrix_mul_vector_op<std::complex<float>, base_device::DEVICE_GPU>::operato
std::complex<float> *a, const int &lda, const float *b, const float alpha, std::complex<float> *c, const int &ldc){
dim3 thread(16, 16, 1);
dim3 block((m + thread.x - 1) / thread.x, (n + thread.y - 1) / thread.y, 1);
hipLaunchKernelGGL(HIP_KERNEL_NAME(matrix_multiply_vector_kernel<thrust::complex<float>, float>), dim3(block, thread),
hipLaunchKernelGGL(HIP_KERNEL_NAME(matrix_multiply_vector_kernel<thrust::complex<float>, float>), block, thread, 0, 0,
m, n, reinterpret_cast<thrust::complex<float>*>(a), lda,
b, alpha, reinterpret_cast<thrust::complex<float>*>(c), ldc);
hipCheckOnDebug();
Expand All @@ -517,7 +517,7 @@ void matrix_mul_vector_op<std::complex<double>, base_device::DEVICE_GPU>::operat
{
dim3 thread(16, 16, 1);
dim3 block((m + thread.x - 1) / thread.x, (n + thread.y - 1) / thread.y, 1);
hipLaunchKernelGGL(HIP_KERNEL_NAME(matrix_multiply_vector_kernel<thrust::complex<double>, double>), dim3(block, thread),
hipLaunchKernelGGL(HIP_KERNEL_NAME(matrix_multiply_vector_kernel<thrust::complex<double>, double>), block, thread, 0, 0,
m, n, reinterpret_cast<thrust::complex<double>*>(a), lda,
b, alpha, reinterpret_cast<thrust::complex<double>*>(c), ldc);
hipCheckOnDebug();
Expand Down
1 change: 1 addition & 0 deletions source/source_base/kernels/rocm/math_kernel_op_vec.hip.cu
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "source_base/kernels/math_kernel_op.h"
#include "source_base/parallel_reduce.h"

#include <base/macros/macros.h>
#include <thrust/complex.h>
Expand Down
20 changes: 17 additions & 3 deletions source/source_base/module_container/ATen/kernels/rocm/blas.hip.cu
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,26 @@ void destroyGpuBlasHandle() {

template <typename T>
struct blas_nrm2<T, DEVICE_GPU> {
T operator()(
using Real = typename GetTypeReal<T>::type;
Real operator()(
const int n,
const T *x,
const int incx)
{
T result;
Real result;
hipBlasConnector::nrm2(hipblas_handle, n, x, incx, &result);
return result;
}
};

template <typename T>
struct blas_copy<T, DEVICE_GPU> {
void operator()(const int n, const T* x, const int incx, T* y, const int incy)
{
hipBlasConnector::copy(hipblas_handle, n, x, incx, y, incy);
}
};

template <typename T>
struct blas_dot<T, DEVICE_GPU> {
void operator()(
Expand Down Expand Up @@ -209,6 +218,11 @@ struct blas_gemm_batched_strided<T, DEVICE_GPU> {
};

// Explicitly instantiate functors for the types of functor registered.
template struct blas_copy<float, DEVICE_GPU>;
template struct blas_copy<double, DEVICE_GPU>;
template struct blas_copy<std::complex<float>, DEVICE_GPU>;
template struct blas_copy<std::complex<double>, DEVICE_GPU>;

template struct blas_nrm2<float , DEVICE_GPU>;
template struct blas_nrm2<double, DEVICE_GPU>;
template struct blas_nrm2<std::complex<float> , DEVICE_GPU>;
Expand Down Expand Up @@ -260,4 +274,4 @@ template struct blas_gemm_batched_strided<std::complex<float >, DEVICE_GPU>;
template struct blas_gemm_batched_strided<std::complex<double>, DEVICE_GPU>;

} // namespace kernels
} // namespace container
} // namespace container
44 changes: 33 additions & 11 deletions source/source_base/module_container/ATen/kernels/rocm/lapack.hip.cu
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,37 @@ template <typename T>
struct lapack_heevd<T, DEVICE_GPU> {
using Real = typename GetTypeReal<T>::type;
void operator()(
const char& jobz,
const char& uplo,
const int dim,
T* Mat,
const int& dim,
const int lda,
Real* eigen_val)
{
// hipSolverConnector::heevd(hipsolver_handle, jobz, uplo, dim, Mat, dim, eigen_val);
std::vector<T> H_Mat(dim * dim, static_cast<T>(0.0));
std::vector<Real> H_eigen_val(dim, static_cast<Real>(0.0));
hipMemcpy(H_Mat.data(), Mat, sizeof(T) * H_Mat.size(), hipMemcpyDeviceToHost);
hipMemcpy(H_eigen_val.data(), eigen_val, sizeof(Real) * H_eigen_val.size(), hipMemcpyDeviceToHost);
lapack_heevd<T, DEVICE_CPU>()(jobz, uplo, H_Mat.data(), dim, H_eigen_val.data());
hipMemcpy(Mat, H_Mat.data(), sizeof(T) * H_Mat.size(), hipMemcpyHostToDevice);
hipMemcpy(eigen_val, H_eigen_val.data(), sizeof(Real) * H_eigen_val.size(), hipMemcpyHostToDevice);
std::vector<T> host_mat(static_cast<size_t>(lda) * dim);
std::vector<Real> host_eigenvalues(dim);
const size_t matrix_bytes = sizeof(T) * host_mat.size();
const size_t eigenvalue_bytes = sizeof(Real) * host_eigenvalues.size();
hipErrcheck(hipMemcpy(host_mat.data(), Mat, matrix_bytes, hipMemcpyDeviceToHost));
lapack_heevd<T, DEVICE_CPU>()(dim, host_mat.data(), lda, host_eigenvalues.data());
hipErrcheck(hipMemcpy(Mat, host_mat.data(), matrix_bytes, hipMemcpyHostToDevice));
hipErrcheck(hipMemcpy(eigen_val, host_eigenvalues.data(), eigenvalue_bytes, hipMemcpyHostToDevice));
}
};

template <typename T>
struct lapack_heevx<T, DEVICE_GPU> {
using Real = typename GetTypeReal<T>::type;
void operator()(const int n, const int lda, const T* mat, const int neig, Real* eigenvalues, T* eigenvectors)
{
std::vector<T> host_mat(static_cast<size_t>(lda) * n);
std::vector<Real> host_eigenvalues(n);
std::vector<T> host_eigenvectors(static_cast<size_t>(lda) * neig);
const size_t matrix_bytes = sizeof(T) * host_mat.size();
const size_t eigenvalue_bytes = sizeof(Real) * neig;
const size_t eigenvector_bytes = sizeof(T) * host_eigenvectors.size();
hipErrcheck(hipMemcpy(host_mat.data(), mat, matrix_bytes, hipMemcpyDeviceToHost));
lapack_heevx<T, DEVICE_CPU>()(n, lda, host_mat.data(), neig, host_eigenvalues.data(), host_eigenvectors.data());
hipErrcheck(hipMemcpy(eigenvalues, host_eigenvalues.data(), eigenvalue_bytes, hipMemcpyHostToDevice));
hipErrcheck(hipMemcpy(eigenvectors, host_eigenvectors.data(), eigenvector_bytes, hipMemcpyHostToDevice));
}
};

Expand Down Expand Up @@ -153,6 +170,11 @@ template struct lapack_heevd<double, DEVICE_GPU>;
template struct lapack_heevd<std::complex<float>, DEVICE_GPU>;
template struct lapack_heevd<std::complex<double>, DEVICE_GPU>;

template struct lapack_heevx<float, DEVICE_GPU>;
template struct lapack_heevx<double, DEVICE_GPU>;
template struct lapack_heevx<std::complex<float>, DEVICE_GPU>;
template struct lapack_heevx<std::complex<double>, DEVICE_GPU>;

template struct lapack_hegvd<float, DEVICE_GPU>;
template struct lapack_hegvd<double, DEVICE_GPU>;
template struct lapack_hegvd<std::complex<float>, DEVICE_GPU>;
Expand Down
3 changes: 2 additions & 1 deletion source/source_base/module_container/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ add_subdirectory(ATen/core)
add_subdirectory(ATen/kernels)

if(USE_ROCM)
hip_add_library(container_rocm STATIC ${ATen_ROCM_SRCS})
hip_add_library(container_rocm STATIC ${ATen_ROCM_SRCS} HIPCC_OPTIONS
-fno-gpu-rdc --std=c++${CMAKE_CXX_STANDARD})
set(ATen_ROCM_DEPENDENCY_LIBS container_rocm)
endif()

Expand Down
55 changes: 55 additions & 0 deletions source/source_base/module_container/base/third_party/hipblas.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef BASE_THIRD_PARTY_HIPBLAS_H_
#define BASE_THIRD_PARTY_HIPBLAS_H_

#include <complex>
#include <cstdint>

#include <hip/hip_runtime.h>
Expand All @@ -10,6 +11,60 @@
namespace container {
namespace hipBlasConnector {

static inline
void nrm2(hipblasHandle_t handle, int n, const float* x, int incx, float* result)
{
hipblasErrcheck(hipblasSnrm2(handle, n, x, incx, result));
}

static inline
void nrm2(hipblasHandle_t handle, int n, const double* x, int incx, double* result)
{
hipblasErrcheck(hipblasDnrm2(handle, n, x, incx, result));
}

static inline
void nrm2(hipblasHandle_t handle, int n, const std::complex<float>* x, int incx, float* result)
{
const hipblasComplex* input = reinterpret_cast<const hipblasComplex*>(x);
hipblasErrcheck(hipblasScnrm2(handle, n, input, incx, result));
}

static inline
void nrm2(hipblasHandle_t handle, int n, const std::complex<double>* x, int incx, double* result)
{
const hipblasDoubleComplex* input = reinterpret_cast<const hipblasDoubleComplex*>(x);
hipblasErrcheck(hipblasDznrm2(handle, n, input, incx, result));
}

static inline
void copy(hipblasHandle_t handle, int n, const float* x, int incx, float* y, int incy)
{
hipblasErrcheck(hipblasScopy(handle, n, x, incx, y, incy));
}

static inline
void copy(hipblasHandle_t handle, int n, const double* x, int incx, double* y, int incy)
{
hipblasErrcheck(hipblasDcopy(handle, n, x, incx, y, incy));
}

static inline
void copy(hipblasHandle_t handle, int n, const std::complex<float>* x, int incx, std::complex<float>* y, int incy)
{
const hipblasComplex* input = reinterpret_cast<const hipblasComplex*>(x);
hipblasComplex* output = reinterpret_cast<hipblasComplex*>(y);
hipblasErrcheck(hipblasCcopy(handle, n, input, incx, output, incy));
}

static inline
void copy(hipblasHandle_t handle, int n, const std::complex<double>* x, int incx, std::complex<double>* y, int incy)
{
const hipblasDoubleComplex* input = reinterpret_cast<const hipblasDoubleComplex*>(x);
hipblasDoubleComplex* output = reinterpret_cast<hipblasDoubleComplex*>(y);
hipblasErrcheck(hipblasZcopy(handle, n, input, incx, output, incy));
}

static inline
void dot(hipblasHandle_t& handle, const int& n, const float *x, const int& incx, const float *y, const int& incy, float* result)
{
Expand Down
2 changes: 1 addition & 1 deletion source/source_base/module_device/gpu_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
#define gpuDeviceCanAccessPeer hipDeviceCanAccessPeer

// Error check macro
#define gpuErrcheck CHECK_CUDA
#define gpuErrcheck hipErrcheck

#endif // __CUDA / __ROCM

Expand Down
70 changes: 70 additions & 0 deletions source/source_base/module_device/rocm/memory_op.hip.cu
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,60 @@ void synchronize_memory_op<FPTYPE, base_device::DEVICE_GPU, base_device::DEVICE_
hipErrcheck(hipMemcpy(arr_out, arr_in, sizeof(FPTYPE) * size, hipMemcpyDeviceToDevice));
}

template <typename FPTYPE>
void synchronize_memory_2d_op<FPTYPE, base_device::DEVICE_CPU, base_device::DEVICE_GPU>::operator()(
FPTYPE* arr_out,
const size_t dpitch,
const FPTYPE* arr_in,
const size_t spitch,
const size_t width,
const size_t height)
{
hipErrcheck(hipMemcpy2D(arr_out,
dpitch * sizeof(FPTYPE),
arr_in,
spitch * sizeof(FPTYPE),
width * sizeof(FPTYPE),
height,
hipMemcpyDeviceToHost));
}

template <typename FPTYPE>
void synchronize_memory_2d_op<FPTYPE, base_device::DEVICE_GPU, base_device::DEVICE_CPU>::operator()(
FPTYPE* arr_out,
const size_t dpitch,
const FPTYPE* arr_in,
const size_t spitch,
const size_t width,
const size_t height)
{
hipErrcheck(hipMemcpy2D(arr_out,
dpitch * sizeof(FPTYPE),
arr_in,
spitch * sizeof(FPTYPE),
width * sizeof(FPTYPE),
height,
hipMemcpyHostToDevice));
}

template <typename FPTYPE>
void synchronize_memory_2d_op<FPTYPE, base_device::DEVICE_GPU, base_device::DEVICE_GPU>::operator()(
FPTYPE* arr_out,
const size_t dpitch,
const FPTYPE* arr_in,
const size_t spitch,
const size_t width,
const size_t height)
{
hipErrcheck(hipMemcpy2D(arr_out,
dpitch * sizeof(FPTYPE),
arr_in,
spitch * sizeof(FPTYPE),
width * sizeof(FPTYPE),
height,
hipMemcpyDeviceToDevice));
}

template <typename FPTYPE_out, typename FPTYPE_in>
struct cast_memory_op<FPTYPE_out, FPTYPE_in, base_device::DEVICE_GPU, base_device::DEVICE_GPU> {
void operator()(FPTYPE_out* arr_out,
Expand Down Expand Up @@ -175,6 +229,22 @@ template struct set_memory_op<double, base_device::DEVICE_GPU>;
template struct set_memory_op<std::complex<float>, base_device::DEVICE_GPU>;
template struct set_memory_op<std::complex<double>, base_device::DEVICE_GPU>;

template struct synchronize_memory_2d_op<int, base_device::DEVICE_CPU, base_device::DEVICE_GPU>;
template struct synchronize_memory_2d_op<int, base_device::DEVICE_GPU, base_device::DEVICE_CPU>;
template struct synchronize_memory_2d_op<int, base_device::DEVICE_GPU, base_device::DEVICE_GPU>;
template struct synchronize_memory_2d_op<float, base_device::DEVICE_CPU, base_device::DEVICE_GPU>;
template struct synchronize_memory_2d_op<float, base_device::DEVICE_GPU, base_device::DEVICE_CPU>;
template struct synchronize_memory_2d_op<float, base_device::DEVICE_GPU, base_device::DEVICE_GPU>;
template struct synchronize_memory_2d_op<double, base_device::DEVICE_CPU, base_device::DEVICE_GPU>;
template struct synchronize_memory_2d_op<double, base_device::DEVICE_GPU, base_device::DEVICE_CPU>;
template struct synchronize_memory_2d_op<double, base_device::DEVICE_GPU, base_device::DEVICE_GPU>;
template struct synchronize_memory_2d_op<std::complex<float>, base_device::DEVICE_CPU, base_device::DEVICE_GPU>;
template struct synchronize_memory_2d_op<std::complex<float>, base_device::DEVICE_GPU, base_device::DEVICE_CPU>;
template struct synchronize_memory_2d_op<std::complex<float>, base_device::DEVICE_GPU, base_device::DEVICE_GPU>;
template struct synchronize_memory_2d_op<std::complex<double>, base_device::DEVICE_CPU, base_device::DEVICE_GPU>;
template struct synchronize_memory_2d_op<std::complex<double>, base_device::DEVICE_GPU, base_device::DEVICE_CPU>;
template struct synchronize_memory_2d_op<std::complex<double>, base_device::DEVICE_GPU, base_device::DEVICE_GPU>;

template struct synchronize_memory_op<int, base_device::DEVICE_CPU, base_device::DEVICE_GPU>;
template struct synchronize_memory_op<int, base_device::DEVICE_GPU, base_device::DEVICE_CPU>;
template struct synchronize_memory_op<int, base_device::DEVICE_GPU, base_device::DEVICE_GPU>;
Expand Down
Loading
Loading