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
7 changes: 4 additions & 3 deletions docs/advanced/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ of HSE), build Libxc 7.1.0 or later with `-DENABLE_CUDA=ON`. The ABACUS option
`ENABLE_LIBXC_CUDA` defaults to `OFF`. Configure ABACUS with
`-DUSE_CUDA=ON -DENABLE_LIBXC=ON -DENABLE_LIBXC_CUDA=ON -DLibxc_DIR=<prefix>/lib/cmake/Libxc`, then set
`device gpu` and `device_xc gpu` in INPUT. `device_xc` defaults to `cpu`, which
also works with a CUDA-enabled Libxc. Built-in functionals, meta-GGA, response
kernels and separate force/stress evaluations continue to use the CPU. This
option does not select the exact-exchange backend.
also works with a CUDA-enabled Libxc. Built-in functionals (except noncollinear
PBE potential evaluation when the density PW basis runs on CUDA), meta-GGA,
response kernels and separate force/stress evaluations continue to use the CPU.
This option does not select the exact-exchange backend.

For HSE forces, Libxc 7.1.0 exceeded a host/device agreement tolerance of
`1e-6 eV/Å` on a displaced Si64 cell. The WPBEH exchange term loses precision
Expand Down
2 changes: 2 additions & 0 deletions source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,7 @@ if(USE_CUDA)
source_hsolver/kernels/cuda/bpcg_kernel_op.cu
source_hsolver/module_linalg/kernels/cuda/linear_op.cu
source_estate/kernels/cuda/elecstate_op.cu
source_estate/kernels/cuda/hartree_op.cu
source_base/module_device/cuda/memory_op.cu
source_pw/module_pwdft/kernels/cuda/force_op.cu
source_pw/module_pwdft/kernels/cuda/stress_op.cu
Expand All @@ -703,6 +704,7 @@ if(USE_CUDA)
source_base/kernels/cuda/math_kernel_op.cu
source_base/kernels/cuda/math_kernel_op_vec.cu
source_hamilt/module_xc/kernels/cuda/xc_functional_op.cu
source_hamilt/module_xc/kernels/cuda/ncgga_sf_op.cu
source_pw/module_ofdft/kernels/cuda/kedf_wt_gpu.cu
source_pw/module_pwdft/kernels/cuda/cal_density_real_op.cu
source_pw/module_pwdft/kernels/cuda/mul_potential_op.cu
Expand Down
11 changes: 7 additions & 4 deletions source/source_base/module_fft/fft_bundle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,11 @@ void FFT_Bundle::initfft(int nx_in,
fft_double->initfft(nx_in, ny_in, nz_in);
#elif defined(__CUDA)
fft_float = make_unique<FFT_CUDA<float>>();
fft_float->initfft(nx_in, ny_in, nz_in);
fft_float->initfft(nx_in, ny_in, nz_in, lixy_in, rixy_in, ns_in, nplane_in, nproc_in,
gamma_only_in, xprime_in);
fft_double = make_unique<FFT_CUDA<double>>();
fft_double->initfft(nx_in, ny_in, nz_in);
fft_double->initfft(nx_in, ny_in, nz_in, lixy_in, rixy_in, ns_in, nplane_in, nproc_in,
gamma_only_in, xprime_in);
#endif
}else{
ModuleBase::WARNING_QUIT("FFT_Bundle", "Please set the device to cpu or gpu or dsp");
Expand Down Expand Up @@ -337,11 +339,12 @@ std::complex<double>* FFT_Bundle::get_auxg_data() const
template <>
std::complex<float>* FFT_Bundle::get_auxr_3d_data() const
{
return fft_float->get_auxr_3d_data();
return fft_float == nullptr ? nullptr : fft_float->get_auxr_3d_data();
}
template <>
std::complex<double>* FFT_Bundle::get_auxr_3d_data() const
{
return fft_double->get_auxr_3d_data();
return fft_double == nullptr ? nullptr : fft_double->get_auxr_3d_data();
}

} // namespace ModuleBase
129 changes: 127 additions & 2 deletions source/source_base/module_fft/fft_cuda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,111 @@
#include "source_base/module_device/memory_op.h"
#include "source_base/module_device/device_check.h"

#include <algorithm>
#include <cassert>

namespace ModuleBase
{
namespace
{
// Grids smaller than this are dominated by the extra kernel launches of the
// pruned transform, so the regular cuFFT path is cheaper.
constexpr int pruned_fft_min_grid_points = 1 << 20;
// Keep one quarter of the x planes inactive before using the pruned plan.
constexpr int pruned_fft_active_numerator = 3;
constexpr int pruned_fft_active_denominator = 4;
} // namespace

template <typename FPTYPE>
void FFT_CUDA<FPTYPE>::initfft(int nx_in, int ny_in, int nz_in)
{
this->nx = nx_in;
this->ny = ny_in;
this->nz = nz_in;
}

template <typename FPTYPE>
void FFT_CUDA<FPTYPE>::initfft(int nx_in,
int ny_in,
int nz_in,
int lixy_in,
int rixy_in,
int /*ns_in*/,
int /*nplane_in*/,
int nproc_in,
bool gamma_only_in,
bool xprime_in)
{
this->nx = nx_in;
this->ny = ny_in;
this->nz = nz_in;
this->lixy_index = lixy_in;
this->rixy_index = rixy_in;
this->nproc = nproc_in;
this->gamma_only = gamma_only_in;
this->xprime = xprime_in;
}

template <typename FPTYPE>
bool FFT_CUDA<FPTYPE>::use_pruned_gpu_fft() const
{
return false;
}

template <>
bool FFT_CUDA<double>::use_pruned_gpu_fft() const
{
// Small grids are dominated by the extra kernel launches. Sparse large
// grids avoid the y-z transforms in the unoccupied middle x slabs.
return this->xprime && !this->gamma_only && this->nproc == 1
&& this->nx * this->ny * this->nz >= pruned_fft_min_grid_points
&& pruned_fft_active_denominator * (this->lixy_index + 1 + this->nx - this->rixy_index)
<= pruned_fft_active_numerator * this->nx;
}

template <typename FPTYPE>
void FFT_CUDA<FPTYPE>::init_gpu_pruned_plan()
{
}

template <>
void FFT_CUDA<double>::init_gpu_pruned_plan()
{
int x_size = this->nx;
int yz_size[2] = {this->ny, this->nz};
const int plane = this->ny * this->nz;
CHECK_CUFFT(cufftPlanMany(&this->z_x_handle, 1, &x_size, &x_size, plane, 1,
&x_size, plane, 1, CUFFT_Z2Z, plane));
CHECK_CUFFT(cufftPlanMany(&this->z_yz_left_handle, 2, yz_size, yz_size, 1, plane,
yz_size, 1, plane, CUFFT_Z2Z, this->lixy_index + 1));
if (this->rixy_index < this->nx)
{
CHECK_CUFFT(cufftPlanMany(&this->z_yz_right_handle, 2, yz_size, yz_size, 1, plane,
yz_size, 1, plane, CUFFT_Z2Z, this->nx - this->rixy_index));
}
}

template <>
void FFT_CUDA<float>::setupFFT()
{
cufftPlan3d(&c_handle, this->nx, this->ny, this->nz, CUFFT_C2C);
CHECK_CUFFT(cufftPlan3d(&this->c_handle, this->nx, this->ny, this->nz, CUFFT_C2C));
resmem_cd_op()(this->c_auxr_3d, this->nx * this->ny * this->nz);
}

template <>
void FFT_CUDA<double>::setupFFT()
{
cufftPlan3d(&z_handle, this->nx, this->ny, this->nz, CUFFT_Z2Z);
if (this->use_pruned_gpu_fft())
{
this->init_gpu_pruned_plan();
}
else
{
CHECK_CUFFT(cufftPlan3d(&this->z_handle, this->nx, this->ny, this->nz, CUFFT_Z2Z));
}
resmem_zd_op()(this->z_auxr_3d, this->nx * this->ny * this->nz);
}

template <>
void FFT_CUDA<float>::cleanFFT()
{
Expand All @@ -32,6 +116,7 @@ void FFT_CUDA<float>::cleanFFT()
c_handle = {};
}
}

template <>
void FFT_CUDA<double>::cleanFFT()
{
Expand All @@ -40,7 +125,16 @@ void FFT_CUDA<double>::cleanFFT()
cufftDestroy(z_handle);
z_handle = {};
}
for (cufftHandle* handle : {&z_x_handle, &z_yz_left_handle, &z_yz_right_handle})
{
if (*handle)
{
cufftDestroy(*handle);
*handle = {};
}
}
}

template <>
void FFT_CUDA<float>::clear()
{
Expand All @@ -51,6 +145,7 @@ void FFT_CUDA<float>::clear()
c_auxr_3d = nullptr;
}
}

template <>
void FFT_CUDA<double>::clear()
{
Expand All @@ -70,14 +165,29 @@ void FFT_CUDA<float>::fft3D_forward(std::complex<float>* in, std::complex<float>
reinterpret_cast<cufftComplex*>(out),
CUFFT_FORWARD));
}

template <>
void FFT_CUDA<double>::fft3D_forward(std::complex<double>* in, std::complex<double>* out) const
{
if (this->z_x_handle)
{
cufftDoubleComplex* input = reinterpret_cast<cufftDoubleComplex*>(in);
cufftDoubleComplex* output = reinterpret_cast<cufftDoubleComplex*>(out);
CHECK_CUFFT(cufftExecZ2Z(this->z_x_handle, input, output, CUFFT_FORWARD));
CHECK_CUFFT(cufftExecZ2Z(this->z_yz_left_handle, output, output, CUFFT_FORWARD));
if (this->z_yz_right_handle)
{
cufftDoubleComplex* right = output + this->rixy_index * this->ny * this->nz;
CHECK_CUFFT(cufftExecZ2Z(this->z_yz_right_handle, right, right, CUFFT_FORWARD));
}
return;
}
CHECK_CUFFT(cufftExecZ2Z(this->z_handle,
reinterpret_cast<cufftDoubleComplex*>(in),
reinterpret_cast<cufftDoubleComplex*>(out),
CUFFT_FORWARD));
}

template <>
void FFT_CUDA<float>::fft3D_backward(std::complex<float>* in, std::complex<float>* out) const
{
Expand All @@ -90,16 +200,31 @@ void FFT_CUDA<float>::fft3D_backward(std::complex<float>* in, std::complex<float
template <>
void FFT_CUDA<double>::fft3D_backward(std::complex<double>* in, std::complex<double>* out) const
{
if (this->z_x_handle)
{
cufftDoubleComplex* input = reinterpret_cast<cufftDoubleComplex*>(in);
cufftDoubleComplex* output = reinterpret_cast<cufftDoubleComplex*>(out);
CHECK_CUFFT(cufftExecZ2Z(this->z_yz_left_handle, input, input, CUFFT_INVERSE));
if (this->z_yz_right_handle)
{
cufftDoubleComplex* right = input + this->rixy_index * this->ny * this->nz;
CHECK_CUFFT(cufftExecZ2Z(this->z_yz_right_handle, right, right, CUFFT_INVERSE));
}
CHECK_CUFFT(cufftExecZ2Z(this->z_x_handle, input, output, CUFFT_INVERSE));
return;
}
CHECK_CUFFT(cufftExecZ2Z(this->z_handle,
reinterpret_cast<cufftDoubleComplex*>(in),
reinterpret_cast<cufftDoubleComplex*>(out),
CUFFT_INVERSE));
}

template <>
std::complex<float>* FFT_CUDA<float>::get_auxr_3d_data() const
{
return this->c_auxr_3d;
}

template <>
std::complex<double>* FFT_CUDA<double>::get_auxr_3d_data() const
{
Expand Down
23 changes: 23 additions & 0 deletions source/source_base/module_fft/fft_cuda.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ class FFT_CUDA : public FFT_BASE<FPTYPE>
void initfft(int nx_in,
int ny_in,
int nz_in) override;

void initfft(int nx_in,
int ny_in,
int nz_in,
int lixy_in,
int rixy_in,
int ns_in,
int nplane_in,
int nproc_in,
bool gamma_only_in,
bool xprime_in) override;

/**
* @brief Get the real space data
Expand Down Expand Up @@ -57,10 +68,22 @@ class FFT_CUDA : public FFT_BASE<FPTYPE>
private:
cufftHandle c_handle = {};
cufftHandle z_handle = {};
cufftHandle z_x_handle = {};
cufftHandle z_yz_left_handle = {};
cufftHandle z_yz_right_handle = {};

std::complex<float>* c_auxr_3d = nullptr; // fft space
std::complex<double>* z_auxr_3d = nullptr; // fft space

int lixy_index = 0;
int rixy_index = 0;
int nproc = 1;
bool gamma_only = false;
bool xprime = true;

bool use_pruned_gpu_fft() const;
void init_gpu_pruned_plan();

};

} // namespace ModuleBase
Expand Down
68 changes: 68 additions & 0 deletions source/source_basis/module_pw/kernels/cuda/pw_op.cu
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,74 @@ namespace ModulePW {

#define THREADS_PER_BLOCK 256

template <typename Real>
__global__ void pack_real_grid(int size, const Real* input, thrust::complex<Real>* grid)
{
const int index = blockIdx.x * blockDim.x + threadIdx.x;
if (index < size)
{
grid[index] = thrust::complex<Real>(input[index], 0);
}
}

template <typename Real>
__global__ void extract_real_grid(int size, bool add, Real factor, const thrust::complex<Real>* grid, Real* output)
{
const int index = blockIdx.x * blockDim.x + threadIdx.x;
if (index < size)
{
output[index] = add ? output[index] + factor * grid[index].real() : grid[index].real();
}
}

template <typename Real>
void pack_real_grid_gpu(int size, const Real* input, std::complex<Real>* grid)
{
pack_real_grid<<<(size + THREADS_PER_BLOCK - 1) / THREADS_PER_BLOCK, THREADS_PER_BLOCK>>>(
size, input, reinterpret_cast<thrust::complex<Real>*>(grid));
CHECK_CUDA_SYNC();
}

template <typename Real>
void extract_real_grid_gpu(int size, bool add, Real factor, const std::complex<Real>* grid, Real* output)
{
extract_real_grid<<<(size + THREADS_PER_BLOCK - 1) / THREADS_PER_BLOCK, THREADS_PER_BLOCK>>>(
size, add, factor, reinterpret_cast<const thrust::complex<Real>*>(grid), output);
CHECK_CUDA_SYNC();
}

template void pack_real_grid_gpu<float>(int, const float*, std::complex<float>*);
template void pack_real_grid_gpu<double>(int, const double*, std::complex<double>*);
template void extract_real_grid_gpu<float>(int, bool, float, const std::complex<float>*, float*);
template void extract_real_grid_gpu<double>(int, bool, double, const std::complex<double>*, double*);

template <typename Real>
__global__ void gather_fft_coefficients(int size, bool add, Real scale, const int* index,
const thrust::complex<Real>* grid, thrust::complex<Real>* output)
{
const int ig = blockIdx.x * blockDim.x + threadIdx.x;
if (ig < size)
{
const thrust::complex<Real> value = scale * grid[index[ig]];
output[ig] = add ? output[ig] + value : value;
}
}

template <typename Real>
void gather_fft_coefficients_gpu(int size, bool add, Real scale, const int* index,
const std::complex<Real>* grid, std::complex<Real>* output)
{
gather_fft_coefficients<<<(size + THREADS_PER_BLOCK - 1) / THREADS_PER_BLOCK, THREADS_PER_BLOCK>>>(
size, add, scale, index, reinterpret_cast<const thrust::complex<Real>*>(grid),
reinterpret_cast<thrust::complex<Real>*>(output));
CHECK_CUDA_SYNC();
}

template void gather_fft_coefficients_gpu<float>(int, bool, float, const int*, const std::complex<float>*,
std::complex<float>*);
template void gather_fft_coefficients_gpu<double>(int, bool, double, const int*, const std::complex<double>*,
std::complex<double>*);

template<class FPTYPE>
__global__ void set_3d_fft_box(
const int npwk,
Expand Down
Loading
Loading