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
14 changes: 14 additions & 0 deletions docs/advanced/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ Dependency: [Libxc](https://tddft.org/programs/libxc/) >= 5.1.7 .
cmake -B build -DENABLE_LIBXC=ON
```

For CUDA evaluation of the Libxc LDA/GGA potential (including the semilocal part
of HSE), build Libxc 7.1.0 or later with `-DENABLE_CUDA=ON`. Configure ABACUS with
`-DUSE_CUDA=ON -DENABLE_LIBXC=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.

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
at small gradients. Its original logarithm is present in the 7.1.0 through
7.1.2 releases. The Si64 forces agreed within `1.1e-10 eV/Å` using the
[corrected Libxc development revision](https://gitlab.com/libxc/libxc/-/blob/0a70f0eab900537913f4fdf94077b55fa019c2c1/python/gga_exc/gga_x_wpbeh.py#L325).

## Build with ML-ALGO

If machine learning algorithms, including DeePKS and machine learning based kinetic energy density functional (ML-KEDF) for OFDFT, is required for DFT calculation, the following prerequisites and steps are needed:
Expand Down
5 changes: 5 additions & 0 deletions source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ set(_abacus_feature_definitions
$<$<BOOL:${ENABLE_DFTD4}>:__DFTD4>
$<$<BOOL:${MATH_INFO}>:GATHER_INFO>)

if(USE_CUDA AND ENABLE_LIBXC AND Libxc_ENABLE_CUDA AND
Libxc_VERSION VERSION_GREATER_EQUAL "7.1.0")
Comment on lines +72 to +73

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, export an cmake dependent option ENABLE_LIBXC_CUDA, and then

if(ENABLE_LIBXC_CUDA AND Libxc_VERSION VERSION_LESS "7.1.0")
  message(
    FATAL_ERROR
    "Enabling LibXC with CUDA support requires LibXC >= 7.1.0; found version ${Libxc_VERSION}"
  )
endif()

Then the if condition here in source/CMakeLists.txt can be simply if(ENABLE_LIBXC_CUDA)

list(APPEND _abacus_feature_definitions ABACUS_LIBXC_CUDA)
endif()

if(ABACUS_COMMIT_INFO_INCLUDE_DIR)
list(APPEND _abacus_feature_definitions COMMIT_INFO)
endif()
Expand Down
3 changes: 2 additions & 1 deletion source/source_estate/module_pot/pot_xc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ void PotXC::cal_v_eff(const Charge*const chg, const UnitCell*const ucell, Module
domag_z_,
gga_grad_,
hybrid_alpha,
hse_omega);
hse_omega,
xc_gpu_);
*(this->etxc_) = std::get<0>(etxc_vtxc_v);
*(this->vtxc_) = std::get<1>(etxc_vtxc_v);
v_eff += std::get<2>(etxc_vtxc_v);
Expand Down
4 changes: 3 additions & 1 deletion source/source_estate/module_pot/pot_xc.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ class PotXC : public PotBase
PotXC(const bool domag,
const bool domag_z,
const int gga_grad,
const bool xc_gpu,
const ModulePW::PW_Basis* rho_basis_in,
double* etxc_in,
double* vtxc_in,
ModuleBase::matrix* vofk_in = nullptr)
: vofk(vofk_in), etxc_(etxc_in), vtxc_(vtxc_in),
domag_(domag), domag_z_(domag_z), gga_grad_(gga_grad)
domag_(domag), domag_z_(domag_z), gga_grad_(gga_grad), xc_gpu_(xc_gpu)
{
this->rho_basis_ = rho_basis_in;
this->dynamic_mode = true;
Expand All @@ -35,6 +36,7 @@ class PotXC : public PotBase
const bool domag_;
const bool domag_z_;
const int gga_grad_;
const bool xc_gpu_;
};

} // namespace elecstate
Expand Down
4 changes: 3 additions & 1 deletion source/source_estate/module_pot/potential_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ PotBase* Potential::get_pot_type(const std::string& pot_type, const Input_para&
}
else if (pot_type == "xc")
{
return new PotXC(PARAM.globalv.domag, PARAM.globalv.domag_z, input.gga_grad, this->rho_basis_, this->etxc_, this->vtxc_, &(this->vofk_eff));
const bool xc_gpu = input.device_xc == "gpu";
return new PotXC(PARAM.globalv.domag, PARAM.globalv.domag_z, input.gga_grad, xc_gpu,
this->rho_basis_, this->etxc_, this->vtxc_, &(this->vofk_eff));
}
else if (pot_type == "pcc" || pot_type == "pcc_2d")
{
Expand Down
31 changes: 31 additions & 0 deletions source/source_hamilt/module_xc/libxc_abacus.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,22 @@ namespace XC_Functional_Libxc
const double hybrid_alpha,
const double hse_omega);

extern std::vector<xc_func_type> init_func(
const std::vector<int> &func_id,
const int xc_polarized,
const double hybrid_alpha,
const double hse_omega,
const bool use_gpu);

// Host arrays in/out; the bounded CUDA workspace is released on return.
void xc_cuda_exc_vxc(xc_func_type& func,
const int nrxx,
const double* rho,
const double* sigma,
double* exc,
double* vrho,
double* vsigma);

/**
* @brief Per-component external parameters that init_func would apply.
*
Expand Down Expand Up @@ -105,6 +121,21 @@ namespace XC_Functional_Libxc
const double hybrid_alpha,
const double hse_omega);

extern std::tuple<double, double, ModuleBase::matrix> v_xc_libxc(
const std::vector<int> &func_id,
const int &nrxx, // number of real-space grid
const double &omega, // volume of cell
const double tpiba,
const Charge* const chr, // charge density
const int nspin,
const bool domag,
const bool domag_z,
const int gga_grad,
const std::map<int, double>* scaling_factor,
const double hybrid_alpha,
const double hse_omega,
const bool use_gpu);

// Reciprocal-metric derivative of the exact gga_grad=2 Libxc energy
// graph. The returned lower-triangular tensor is the unnormalized local
// grid sum; Stress_Func performs the pool reduction and divides by nxyz.
Expand Down
23 changes: 23 additions & 0 deletions source/source_hamilt/module_xc/libxc_host.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef LIBXC_HOST_H
#define LIBXC_HOST_H

#include <xc.h>

#if defined(ABACUS_LIBXC_CUDA) && !defined(XC_FLAGS_ON_HOST)
#error "CMake found CUDA Libxc >= 7.1.0 but xc.h lacks host/device selection. Check the compiler include path."
#endif

namespace XC_Functional_Libxc
{
// A CUDA-enabled Libxc defaults to device pointers. Keep host callers explicit.
inline int init_func_host(xc_func_type* func, const int id, const int spin)
{
#ifdef XC_FLAGS_ON_HOST
return xc_func_init_flags(func, id, spin, XC_FLAGS_ON_HOST);
#else
return xc_func_init(func, id, spin);
#endif
}
} // namespace XC_Functional_Libxc

#endif
179 changes: 143 additions & 36 deletions source/source_hamilt/module_xc/libxc_pot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "source_base/parallel_reduce.h"
#include "source_base/timer.h"
#include "source_base/tool_title.h"
#include "source_base/module_device/memory_op.h"
#ifdef __EXX
#include "source_hamilt/module_xc/exx_info.h"
#endif
Expand All @@ -14,6 +15,78 @@

#include <vector>
#include <complex>
#include <memory>

void XC_Functional_Libxc::xc_cuda_exc_vxc(xc_func_type& func,
const int nrxx,
const double* rho,
const double* sigma,
double* exc,
double* vrho,
double* vsigma)
{
ModuleBase::timer::start("Libxc", "xc_cuda_exc_vxc");
#if defined(__CUDA) && defined(ABACUS_LIBXC_CUDA)
if (nrxx > 0)
{
const bool is_lda = func.info->family == XC_FAMILY_LDA;
const bool is_gga = func.info->family == XC_FAMILY_GGA || func.info->family == XC_FAMILY_HYB_GGA;
if (!is_lda && !is_gga)
{
throw std::domain_error("Libxc CUDA evaluation supports LDA and GGA potentials only.");
}
// At most 5.5 MiB per rank for polarized GGA; Libxc's mix workspace
// is also bounded by this batch size. No grid-sized cache is retained.
const int capacity = std::min(nrxx, 65536);
const int nspin = func.dim.rho;
const int nsigma = func.dim.sigma;
const std::size_t rho_count = static_cast<std::size_t>(capacity) * nspin;
const std::size_t sigma_count = static_cast<std::size_t>(capacity) * nsigma;
const std::size_t count = 2 * rho_count + 2 * sigma_count + capacity;
double* buffer = nullptr;
resmem_dd_op()(buffer, count);
std::unique_ptr<double, delmem_dd_op> workspace(buffer);
double* d_rho = buffer;
double* d_sigma = d_rho + rho_count;
double* d_exc = d_sigma + sigma_count;
double* d_vrho = d_exc + capacity;
double* d_vsigma = d_vrho + rho_count;
for (int start = 0; start < nrxx;)
{
const int remaining = nrxx - start;
const int points = std::min(capacity, remaining);
const std::size_t rho_offset = static_cast<std::size_t>(start) * nspin;
const std::size_t sigma_offset = static_cast<std::size_t>(start) * nsigma;
const std::size_t nrho = static_cast<std::size_t>(points) * nspin;
const std::size_t nsig = static_cast<std::size_t>(points) * nsigma;
const double* rho_in = rho + rho_offset;
double* exc_out = exc + start;
double* vrho_out = vrho + rho_offset;
syncmem_d2d_h2d_op()(d_rho, rho_in, nrho);
if (is_lda)
{
xc_lda_exc_vxc(&func, points, d_rho, d_exc, d_vrho);
}
else
{
const double* sigma_in = sigma + sigma_offset;
double* vsigma_out = vsigma + sigma_offset;
syncmem_d2d_h2d_op()(d_sigma, sigma_in, nsig);
xc_gga_exc_vxc(&func, points, d_rho, d_sigma, d_exc, d_vrho, d_vsigma);
syncmem_d2d_d2h_op()(vsigma_out, d_vsigma, nsig);
}
// Blocking copies complete Libxc's default-stream kernels before
// the host consumes derivatives or reuses the device workspace.
syncmem_d2d_d2h_op()(exc_out, d_exc, points);
syncmem_d2d_d2h_op()(vrho_out, d_vrho, nrho);
start += points;
}
}
#else
throw std::domain_error("Libxc CUDA evaluation is unavailable in this build.");
#endif
ModuleBase::timer::end("Libxc", "xc_cuda_exc_vxc");
}

void XC_Functional_Libxc::gradcorr_ncgga_sf_libxc(const std::vector<int>& func_id,
const std::size_t nrxx,
Expand Down Expand Up @@ -194,6 +267,26 @@ std::tuple<double, double, ModuleBase::matrix> XC_Functional_Libxc::v_xc_libxc(
const std::map<int, double>* scaling_factor,
const double hybrid_alpha,
const double hse_omega)
{
return v_xc_libxc(func_id, nrxx, omega, tpiba, chr, nspin_in, domag, domag_z, gga_grad,
scaling_factor, hybrid_alpha, hse_omega, false);
}

std::tuple<double, double, ModuleBase::matrix> XC_Functional_Libxc::v_xc_libxc( // Peize Lin update for nspin==4 at
// 2023.01.14
const std::vector<int>& func_id,
const int& nrxx, // number of real-space grid
const double& omega, // volume of cell
const double tpiba,
const Charge* const chr,
const int nspin_in,
const bool domag,
const bool domag_z,
const int gga_grad,
const std::map<int, double>* scaling_factor,
const double hybrid_alpha,
const double hse_omega,
const bool use_gpu)
{
ModuleBase::TITLE("XC_Functional_Libxc", "v_xc_libxc");
ModuleBase::timer::start("XC_Functional_Libxc", "v_xc_libxc");
Expand All @@ -213,11 +306,13 @@ std::tuple<double, double, ModuleBase::matrix> XC_Functional_Libxc::v_xc_libxc(
// https://www.tddft.org/programs/libxc/manual/libxc-5.1.x/
//----------------------------------------------------------

const int spin_mode = nspin == 1 ? XC_UNPOLARIZED : XC_POLARIZED;
std::vector<xc_func_type> funcs = XC_Functional_Libxc::init_func(
/* func_id = */ func_id,
/* xc_polarized = */ (1 == nspin) ? XC_UNPOLARIZED : XC_POLARIZED,
/* xc_polarized = */ spin_mode,
/* hybrid_alpha = */ hybrid_alpha,
/* hse_omega = */ hse_omega);
/* hse_omega = */ hse_omega,
use_gpu);

const bool is_gga = [&funcs]() {
for (xc_func_type& func: funcs)
Expand Down Expand Up @@ -314,49 +409,61 @@ std::tuple<double, double, ModuleBase::matrix> XC_Functional_Libxc::v_xc_libxc(
std::vector<double> vsigma(nrxx * ((1 == nspin) ? 1 : 3));

ModuleBase::timer::start("Libxc", "xc_lda/gga_exc_vxc");
switch (func.info->family)
if (use_gpu)
{
const double* rho_data = rho.data();
const double* sigma_data = sigma.data();
double* exc_data = exc.data();
double* vrho_data = vrho.data();
double* vsigma_data = vsigma.data();
xc_cuda_exc_vxc(func, nrxx, rho_data, sigma_data, exc_data, vrho_data, vsigma_data);
}
else
{
case XC_FAMILY_LDA: {
constexpr int nr_batch_size = 1024;
switch (func.info->family)
{
case XC_FAMILY_LDA: {
constexpr int nr_batch_size = 1024;
#ifdef _OPENMP
#pragma omp parallel for schedule(static)
#endif
for (int ir_start = 0; ir_start < nrxx; ir_start += nr_batch_size)
{
const int ir_end = std::min(ir_start + nr_batch_size, nrxx);
const int nrxx_thread = ir_end - ir_start;
xc_lda_exc_vxc(&func,
nrxx_thread,
rho.data() + ir_start * nspin,
exc.data() + ir_start,
vrho.data() + ir_start * nspin);
for (int ir_start = 0; ir_start < nrxx; ir_start += nr_batch_size)
{
const int ir_end = std::min(ir_start + nr_batch_size, nrxx);
const int nrxx_thread = ir_end - ir_start;
xc_lda_exc_vxc(&func,
nrxx_thread,
rho.data() + ir_start * nspin,
exc.data() + ir_start,
vrho.data() + ir_start * nspin);
}
break;
}
break;
}
case XC_FAMILY_GGA:
case XC_FAMILY_HYB_GGA: {
constexpr int nr_batch_size = 1024;
case XC_FAMILY_GGA:
case XC_FAMILY_HYB_GGA: {
constexpr int nr_batch_size = 1024;
#ifdef _OPENMP
#pragma omp parallel for schedule(static)
#endif
for (int ir_start = 0; ir_start < nrxx; ir_start += nr_batch_size)
{
const int ir_end = std::min(ir_start + nr_batch_size, nrxx);
const int nrxx_thread = ir_end - ir_start;
xc_gga_exc_vxc(&func,
nrxx_thread,
rho.data() + ir_start * nspin,
sigma.data() + ir_start * ((1 == nspin) ? 1 : 3),
exc.data() + ir_start,
vrho.data() + ir_start * nspin,
vsigma.data() + ir_start * ((1 == nspin) ? 1 : 3));
for (int ir_start = 0; ir_start < nrxx; ir_start += nr_batch_size)
{
const int ir_end = std::min(ir_start + nr_batch_size, nrxx);
const int nrxx_thread = ir_end - ir_start;
xc_gga_exc_vxc(&func,
nrxx_thread,
rho.data() + ir_start * nspin,
sigma.data() + ir_start * ((1 == nspin) ? 1 : 3),
exc.data() + ir_start,
vrho.data() + ir_start * nspin,
vsigma.data() + ir_start * ((1 == nspin) ? 1 : 3));
}
break;
}
default: {
throw std::domain_error("func.info->family =" + std::to_string(func.info->family) + " unfinished in "
+ std::string(__FILE__) + " line " + std::to_string(__LINE__));
}
}
break;
}
default: {
throw std::domain_error("func.info->family =" + std::to_string(func.info->family) + " unfinished in "
+ std::string(__FILE__) + " line " + std::to_string(__LINE__));
}
}
ModuleBase::timer::end("Libxc", "xc_lda/gga_exc_vxc");

Expand Down
Loading
Loading