Skip to content
Draft
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
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ cmake_dependent_option(ENABLE_LIBRI "Enable LibRI for hybrid functional" OFF
cmake_dependent_option(
ENABLE_LIBRI_GPU "Enable experimental LibRI CUDA contractions through DDLA"
OFF "ENABLE_LIBRI;USE_CUDA" OFF)
cmake_dependent_option(ENABLE_DDLA "Enable the LibDDLA LCAO eigensolver" OFF
"ENABLE_LCAO;ENABLE_MPI" OFF)
cmake_dependent_option(ENABLE_PEXSI "Enable PEXSI for LCAO" OFF
"ENABLE_LCAO;ENABLE_MPI" OFF)
cmake_dependent_option(ENABLE_MLALGO "Enable the machine learning algorithms"
Expand Down Expand Up @@ -293,6 +295,10 @@ if(ENABLE_PEXSI)
set(CMAKE_CXX_STANDARD 14)
endif()

if(ENABLE_DDLA)
find_package(LibDDLA 0.0.7 REQUIRED CONFIG)
endif()

if(ENABLE_MPI)
if(NOT CMAKE_CROSSCOMPILING)
# FindMPI runs a probe executable to determine the MPI library version,
Expand Down
8 changes: 8 additions & 0 deletions cmake/CollectBuildInfoVars.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,12 @@ else()
set(ABACUS_ELPA_VERSION "no")
endif()

if(ENABLE_DDLA)
set(ABACUS_DDLA_VERSION "yes (v${LibDDLA_VERSION})")
else()
set(ABACUS_DDLA_VERSION "no")
endif()

if(ENABLE_LIBXC AND Libxc_VERSION)
set(ABACUS_LIBXC_VERSION "yes (v${Libxc_VERSION})")
elseif(ENABLE_LIBXC)
Expand Down Expand Up @@ -432,6 +438,7 @@ list(APPEND CMAKE_FIND_PACKAGES_LIST " ROCm Found=${ROCM_FOUND}")
list(APPEND CMAKE_FIND_PACKAGES_LIST " ELPA Found=${ELPA_FOUND}")
list(APPEND CMAKE_FIND_PACKAGES_LIST " Libxc Found=${Libxc_FOUND}")
list(APPEND CMAKE_FIND_PACKAGES_LIST " PEXSI Found=${PEXSI_FOUND}")
list(APPEND CMAKE_FIND_PACKAGES_LIST " LibDDLA Found=${LibDDLA_FOUND}")
list(APPEND CMAKE_FIND_PACKAGES_LIST " LibRI Found=${LIBRI_FOUND}")
foreach(package_line ${CMAKE_FIND_PACKAGES_LIST})
set(ABACUS_CMAKE_FIND_PACKAGES "${ABACUS_CMAKE_FIND_PACKAGES}${package_line}")
Expand Down Expand Up @@ -462,6 +469,7 @@ message(" -------------------------------------------------------------------")
message(" Math Libraries : BLAS = ${ABACUS_BLAS_VENDOR}")
message(" FFTW = ${ABACUS_FFTW_VERSION}")
message(" ELPA = ${ABACUS_ELPA_VERSION}")
message(" LibDDLA = ${ABACUS_DDLA_VERSION}")
if(ENABLE_ABACUS_LIBM)
message(" Internal libmath enabled")
endif()
Expand Down
22 changes: 22 additions & 0 deletions docs/advanced/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,28 @@ Currently supported math functions:
cmake -B build -DENABLE_ABACUS_LIBM=1
```

## Build with the optional LibDDLA eigensolver

LibDDLA >= 0.0.7 can provide the distributed LCAO generalized eigensolver.
It is disabled by default. To enable it, configure ABACUS with
`-DENABLE_DDLA=ON -DENABLE_MPI=ON -DENABLE_LCAO=ON` and add the installed
LibDDLA prefix to `CMAKE_PREFIX_PATH` (or set `LibDDLA_DIR` to its CMake package directory).
The existing native solvers do not require LibDDLA.

Select `ks_solver ddla` and `kpar 1` in INPUT. Both real Gamma-only and complex
k-point matrices are supported. Use `device cpu` for CPU execution. For
`device gpu`, build ABACUS with CUDA or ROCm and link a GPU-enabled LibDDLA
installation; use one MPI rank per visible GPU on each node. An unavailable
backend is rejected rather than falling back silently. GPU solves currently
use the full MPI process grid, with a common visible-device list for all ranks
on each node.

This initial integration is confined to diagonalization: H/S are copied into
the selected backend for each solve, and eigenvectors return to the existing
host density workflow. It adds no DDLA state to density, RT, LR/BSE or
postprocessing. K-point pooling (`kpar>1`) and persistent device storage across
solves are outside this contribution.

## Build with PEXSI support

ABACUS supports the PEXSI library for gamma only LCAO calculations. PEXSI version >=2.0.0 is required.
Expand Down
2 changes: 2 additions & 0 deletions source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ set(_abacus_feature_definitions
$<$<BOOL:${ENABLE_ELPA}>:__ELPA>
$<$<BOOL:${ENABLE_FFT_TWO_CENTER}>:__FFT_TWO_CENTER>
$<$<BOOL:${ENABLE_PEXSI}>:__PEXSI>
$<$<BOOL:${ENABLE_DDLA}>:__DDLA>
$<$<BOOL:${DEBUG_INFO}>:__DEBUG>
$<$<BOOL:${ENABLE_MPI}>:__MPI>
$<$<BOOL:${USE_DSP}>:__DSP>
Expand Down Expand Up @@ -293,6 +294,7 @@ set(_abacus_feature_libs
$<$<BOOL:${ENABLE_JSON}>:nlohmann_json::nlohmann_json>
$<$<BOOL:${ENABLE_ELPA}>:ELPA::ELPA>
$<$<BOOL:${ENABLE_PEXSI}>:PEXSI::PEXSI>
$<$<BOOL:${ENABLE_DDLA}>:ddla::ddla>
$<$<BOOL:${ENABLE_CNPY}>:cnpy>
$<$<BOOL:${ENABLE_CNPY}>:ZLIB::ZLIB>
$<$<BOOL:${ENABLE_LIBRI}>:LibRI::LibRI>
Expand Down
6 changes: 6 additions & 0 deletions source/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,12 @@ ifdef TensorFlow_DIR
INCLUDES += -I${TensorFlow_INCLUDE_DIR}
endif

ifdef DDLA_DIR
INCLUDES += -I${DDLA_DIR}/include
LIBS += -L${DDLA_DIR}/lib64 -L${DDLA_DIR}/lib -lddla
HONG += -D__DDLA
endif

ifdef PEXSI_DIR
OBJS_ABACUS += ${OBJS_HSOLVER_PEXSI}
INCLUDES += -I${PEXSI_DIR}/include -I${PARMETIS_DIR}/include -I${DSUPERLU_DIR}/include
Expand Down
1 change: 1 addition & 0 deletions source/Makefile.Objects
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,7 @@ OBJS_HSOLVER_LCAO=hsolver_lcao.o\
diag_lcao.o\
diag_sk_cut.o\
diago_scalapack.o\
diago_ddla.o\
diago_lapack.o\
diago_elpa.o\
diago_elpa_native.o\
Expand Down
3 changes: 3 additions & 0 deletions source/Makefile.vars
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,6 @@ ENABLE_MLALGO ?= OFF
# ON: use gnu compiler and check segmental defaults
# OFF: nothing
#======================================================================

# Optional LibDDLA >= 0.0.7 LCAO eigensolver (MPI build only):
# DDLA_DIR = /path/to/libddla
2 changes: 1 addition & 1 deletion source/source_base/global_function.h
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ T ddot_real(const int& dim, const std::complex<T>* psi_L, const std::complex<T>*
static inline bool IS_COLUMN_MAJOR_KS_SOLVER(std::string ks_solver)
{
return ks_solver == "genelpa" || ks_solver == "elpa" || ks_solver == "scalapack_gvx" || ks_solver == "cusolver"
|| ks_solver == "cusolvermp" || ks_solver == "pexsi" || ks_solver == "lapack";
|| ks_solver == "cusolvermp" || ks_solver == "pexsi" || ks_solver == "lapack" || ks_solver == "ddla";
}

} // namespace GlobalFunc
Expand Down
1 change: 1 addition & 0 deletions source/source_estate/elecstate_print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ void print_scf_iterinfo(const std::string& ks_solver,
{"dav_subspace", "DS"},
{"lobpcg", "LB"},
{"scalapack_gvx", "GV"},
{"ddla", "DD"},
{"cusolver", "CU"},
{"bpcg", "BP"},
{"pexsi", "PE"},
Expand Down
3 changes: 3 additions & 0 deletions source/source_hsolver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ if(ENABLE_LCAO)
if(ENABLE_MPI)
list(APPEND objects diago_scalapack.cpp parallel_k2d.cpp)
endif()
if(ENABLE_DDLA)
list(APPEND objects diago_ddla.cpp)
endif()
if(ENABLE_ELPA)
list(APPEND objects diago_elpa.cpp diago_elpa_native.cpp)
endif()
Expand Down
11 changes: 11 additions & 0 deletions source/source_hsolver/diag_lcao.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include "diag_lcao.h"

#include "diago_lapack.h"
#if defined(__DDLA) && defined(__MPI)
#include "diago_ddla.h"
#endif
#include "source_base/parallel_blkcyc.h"
#include "source_base/tool_quit.h"

Expand Down Expand Up @@ -98,6 +101,14 @@ void diag_lcao(ModuleBase::MatrixBlock<T>& h,
else
#endif
#endif
#if defined(__DDLA) && defined(__MPI)
if (method == "ddla")
{
DiagoDdla<T> solver(order, bands, options.use_gpu);
solver.diag(h, s, psi, eigenvalues);
}
else
#endif
if (method == "lapack")
{
DiagoLapack<T> solver(order, bands);
Expand Down
201 changes: 201 additions & 0 deletions source/source_hsolver/diago_ddla.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
#include "diago_ddla.h"

#if defined(__DDLA) && defined(__MPI)
#include "source_base/module_device/device.h"
#include "source_base/parallel_blkcyc.h"
#include "source_base/parallel_common.h"
#include "source_base/parallel_device.h"
#include "source_base/timer.h"
#include "source_base/tool_quit.h"

#include <ddla/ddla.h>
#include <ddla/ddla_vector.h>

#if defined(__CUDA)
#include <cuda_runtime_api.h>
#elif defined(__ROCM)
#include <hip/hip_runtime_api.h>
#endif

#include <algorithm>
#include <complex>
#include <cstddef>
#include <exception>
#include <stdexcept>
#include <string>
#include <vector>

namespace hsolver
{
namespace
{
// The handle outlives every backend buffer, including during stack unwinding.
class Handle
{
public:
ddla::DdlaHandle_t value = nullptr;

~Handle()
{
ddla::ddlaDestroy(value);
}

Handle() = default;
Handle(const Handle&) = delete;
Handle& operator=(const Handle&) = delete;
};

void check_gpu_sharing(const ddla::DdlaHandle_t& handle, const MPI_Comm comm)
{
int devices = 0;
ddla::DDLA_CHECK(ddla::ddlaGetDeviceCount(handle, &devices));
const int local_rank = base_device::information::get_node_rank_with_mpi_shared(MPI_COMM_WORLD);
double invalid = local_rank >= devices ? 1.0 : 0.0;
Parallel_Common::reduce_data(&invalid, 1, comm);
if (invalid != 0.0)
{
throw std::runtime_error("GPU DDLA requires one MPI rank per visible GPU on each node");
}
}

void check_gpu_binding(const MPI_Comm comm)
{
const int bound = base_device::DeviceContext::instance().get_device_id();
int current = -1;
#if defined(__CUDA)
if (cudaGetDevice(&current) != cudaSuccess)
{
throw std::runtime_error("cannot query the current CUDA device");
}
#elif defined(__ROCM)
if (hipGetDevice(&current) != hipSuccess)
{
throw std::runtime_error("cannot query the current HIP device");
}
#endif
double invalid = bound >= 0 && bound != current ? 1.0 : 0.0;
Parallel_Common::reduce_data(&invalid, 1, comm);
if (invalid != 0.0)
{
throw std::runtime_error("LibDDLA changed the GPU selected by ABACUS");
}
}
} // namespace

template <typename T>
void DiagoDdla<T>::diag(ModuleBase::MatrixBlock<T>& h,
ModuleBase::MatrixBlock<T>& s,
psi::Psi<T>& vectors,
double* eigenvalues)
{
ModuleBase::timer::start("DiagoDdla", "diag");
int info = 0;
try
{
if (order_ <= 0 || bands_ <= 0 || bands_ > order_ || h.desc == nullptr || s.desc == nullptr)
{
throw std::invalid_argument("invalid DDLA problem dimensions or missing BLACS descriptors");
}
const int* desc = h.desc;
const int* desc_end = desc + 9;
if (!std::equal(desc, desc_end, s.desc) || desc[1] < 0 || desc[4] <= 0 || desc[2] != order_ || desc[3] != order_
|| desc[4] != desc[5] || desc[6] != 0 || desc[7] != 0)
{
throw std::invalid_argument("DDLA requires matching square, source-zero block-cyclic H/S layouts");
}
// Borrow the caller's BLACS grid, including its communicator. No grid
// or backend state escapes this numerical solve.
Parallel_Blkcyclic layout;
layout.bind_descriptor(order_, order_, desc[4], desc[1]);
const MPI_Comm comm = layout.comm();
const int rows = layout.nrloc();
const int cols = layout.ncloc();
if (h.row != rows || h.col != cols || s.row != rows || s.col != cols || desc[8] < std::max(1, rows))
{
throw std::invalid_argument("DDLA matrix blocks do not match their descriptors");
}
const ddla::DdlaBackend backend = use_gpu_ ? ddla::DdlaBackend::GPU : ddla::DdlaBackend::CPU;
if (!ddla::ddlaBackendAvailable(backend))
{
throw std::runtime_error("the linked LibDDLA installation does not support the selected CPU/GPU backend");
}
if (use_gpu_)
{
#if !defined(__CUDA) && !defined(__ROCM)
throw std::runtime_error("GPU DDLA requires a CUDA/ROCm-enabled ABACUS build");
#endif
const int grid_size = Parallel_Common::comm_size(comm);
const int world_size = Parallel_Common::comm_size(MPI_COMM_WORLD);
if (grid_size != world_size)
{
throw std::runtime_error("GPU DDLA currently requires the full MPI process grid and kpar=1");
}
}
Handle handle;
ddla::DDLA_CHECK(ddla::ddlaInit(handle.value, backend));
// kpar=1 keeps every rank in the same solve. The GPU guard must run
// before creating CCL communicators, which cannot share a GPU.
if (use_gpu_)
{
check_gpu_sharing(handle.value, comm);
}
const int process_rows = layout.nrproc();
const int process_cols = layout.ncproc();
ddla::DDLA_CHECK(ddla::ddlaSet(handle.value, comm, process_rows, process_cols, 'R'));
if (use_gpu_)
{
check_gpu_binding(comm);
}

const std::size_t leading = desc[8];
const std::size_t storage = leading * std::max(1, cols);
const std::size_t pitch = leading * sizeof(T);
const std::size_t width = static_cast<std::size_t>(rows) * sizeof(T);
ddla::vector<T> h_work(handle.value, storage);
ddla::vector<T> s_work(handle.value, storage);
if (rows > 0 && cols > 0)
{
ddla::DDLA_CHECK(ddla::ddlaMemcpy2D(handle.value, h_work.data(), pitch, h.p, pitch, width, cols));
ddla::DDLA_CHECK(ddla::ddlaMemcpy2D(handle.value, s_work.data(), pitch, s.p, pitch, width, cols));
}
std::vector<double> values(order_);
ddla::phegvd(handle.value, 'V', 'U', order_, bands_, h_work.data(), desc,
s_work.data(), desc, values.data(), h_work.data(), desc, info);
if (info == 0)
{
Parallel_Blkcyclic output;
output.bind_descriptor(order_, bands_, desc[4], desc[1]);
const int output_cols = output.ncloc();
if (rows > 0 && output_cols > 0)
{
ddla::DDLA_CHECK(ddla::ddlaMemcpy2D(handle.value, vectors.get_pointer(), width,
h_work.data(), pitch, width, output_cols));
}
// The host density workflow may consume Psi immediately.
ddla::DDLA_CHECK(ddla::ddlaSynchronize(handle.value));
const double* values_begin = values.data();
const double* values_end = values_begin + bands_;
std::copy(values_begin, values_end, eigenvalues);
}
}
catch (const std::exception& error)
{
const std::string message = std::string("LibDDLA generalized eigensolve failed: ") + error.what();
ModuleBase::WARNING_QUIT("DiagoDdla::diag", message);
}
if (info != 0)
{
std::string message = "LibDDLA phegvd returned info=" + std::to_string(info);
if (info > order_)
{
message += "; overlap matrix is not positive definite";
}
ModuleBase::WARNING_QUIT("DiagoDdla::diag", message);
}
ModuleBase::timer::end("DiagoDdla", "diag");
}

template class DiagoDdla<double>;
template class DiagoDdla<std::complex<double>>;
} // namespace hsolver
#endif
Loading
Loading