From 6e1dd33802ff1843896bbc875a789ba7e51ec381 Mon Sep 17 00:00:00 2001 From: Critsium-xy Date: Mon, 8 Sep 2025 14:50:53 +0800 Subject: [PATCH 1/3] Move hardware initializer out from esolver --- source/source_esolver/esolver_ks_pw.cpp | 8 +++---- source/source_main/driver.h | 3 +++ source/source_main/driver_run.cpp | 30 +++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/source/source_esolver/esolver_ks_pw.cpp b/source/source_esolver/esolver_ks_pw.cpp index 36435c4645..57737ca021 100644 --- a/source/source_esolver/esolver_ks_pw.cpp +++ b/source/source_esolver/esolver_ks_pw.cpp @@ -58,18 +58,18 @@ ESolver_KS_PW::ESolver_KS_PW() this->device = base_device::get_device_type(this->ctx); #if ((defined __CUDA) || (defined __ROCM)) - if (this->device == base_device::GpuDevice) + /*if (this->device == base_device::GpuDevice) { ModuleBase::createGpuBlasHandle(); hsolver::createGpuSolverHandle(); container::kernels::createGpuBlasHandle(); container::kernels::createGpuSolverHandle(); - } + }*/ #endif #ifdef __DSP - std::cout << " ** Initializing DSP Hardware..." << std::endl; - mtfunc::dspInitHandle(GlobalV::MY_RANK); + // std::cout << " ** Initializing DSP Hardware..." << std::endl; + // mtfunc::dspInitHandle(GlobalV::MY_RANK); #endif } diff --git a/source/source_main/driver.h b/source/source_main/driver.h index d0ef395875..bf867e2e75 100644 --- a/source/source_main/driver.h +++ b/source/source_main/driver.h @@ -37,6 +37,9 @@ class Driver // the actual calculations void driver_run(); + + // Init harewares according to Input parameters + void init_hardware(); }; #endif diff --git a/source/source_main/driver_run.cpp b/source/source_main/driver_run.cpp index 4743b95bf8..7e1d868f12 100644 --- a/source/source_main/driver_run.cpp +++ b/source/source_main/driver_run.cpp @@ -6,6 +6,17 @@ #include "source_io/para_json.h" #include "source_io/print_info.h" #include "source_md/run_md.h" +#include "source_base/module_device/device.h" +#include "source_base/module_device/memory_op.h" +#include "source_base/kernels/math_kernel_op.h" +#include "source_hsolver/kernels/dngvd_op.h" + +#include +#include + +#ifdef __DSP +#include "source_base/kernels/dsp/dsp_connector.h" +#endif /** * @brief This is the driver function which defines the workflow of ABACUS @@ -47,6 +58,8 @@ void Driver::driver_run() unitcell::check_atomic_stru(ucell, PARAM.inp.min_dist_coef); //! 2: initialize the ESolver (depends on a set-up ucell after `setup_cell`) + this->init_hardware(); + ModuleESolver::ESolver* p_esolver = ModuleESolver::init_esolver(PARAM.inp, ucell); //! 3: initialize Esolver and fill json-structure @@ -99,3 +112,20 @@ void Driver::driver_run() return; } + +void Driver::init_hardware(){ +#if ((defined __CUDA) || (defined __ROCM)) + if (PARAM.inp.device == "gpu") + { + ModuleBase::createGpuBlasHandle(); + hsolver::createGpuSolverHandle(); + container::kernels::createGpuBlasHandle(); + container::kernels::createGpuSolverHandle(); + } +#endif + +#ifdef __DSP + std::cout << " ** Initializing DSP Hardware..." << std::endl; + mtfunc::dspInitHandle(GlobalV::MY_RANK); +#endif +} From 21d1e9cb027aa4d4a65029cb445798d954015ef2 Mon Sep 17 00:00:00 2001 From: Critsium-xy Date: Mon, 8 Sep 2025 15:25:00 +0800 Subject: [PATCH 2/3] Remove useless codes --- source/source_esolver/esolver_ks_pw.cpp | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/source/source_esolver/esolver_ks_pw.cpp b/source/source_esolver/esolver_ks_pw.cpp index 57737ca021..7c5e9f217d 100644 --- a/source/source_esolver/esolver_ks_pw.cpp +++ b/source/source_esolver/esolver_ks_pw.cpp @@ -56,21 +56,6 @@ ESolver_KS_PW::ESolver_KS_PW() this->classname = "ESolver_KS_PW"; this->basisname = "PW"; this->device = base_device::get_device_type(this->ctx); - -#if ((defined __CUDA) || (defined __ROCM)) - /*if (this->device == base_device::GpuDevice) - { - ModuleBase::createGpuBlasHandle(); - hsolver::createGpuSolverHandle(); - container::kernels::createGpuBlasHandle(); - container::kernels::createGpuSolverHandle(); - }*/ -#endif - -#ifdef __DSP - // std::cout << " ** Initializing DSP Hardware..." << std::endl; - // mtfunc::dspInitHandle(GlobalV::MY_RANK); -#endif } template From 859b9ece7bdaa61e4b9e7fe67e5886d6376518a7 Mon Sep 17 00:00:00 2001 From: Critsium-xy Date: Mon, 8 Sep 2025 15:39:12 +0800 Subject: [PATCH 3/3] Remove finalize code out --- source/source_esolver/esolver_ks_pw.cpp | 15 --------------- source/source_main/driver.h | 1 + source/source_main/driver_run.cpp | 22 +++++++++++++++++++++- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/source/source_esolver/esolver_ks_pw.cpp b/source/source_esolver/esolver_ks_pw.cpp index 7c5e9f217d..1f3c23da5a 100644 --- a/source/source_esolver/esolver_ks_pw.cpp +++ b/source/source_esolver/esolver_ks_pw.cpp @@ -71,21 +71,6 @@ ESolver_KS_PW::~ESolver_KS_PW() this->pelec = nullptr; } - if (this->device == base_device::GpuDevice) - { -#if defined(__CUDA) || defined(__ROCM) - ModuleBase::destoryBLAShandle(); - hsolver::destroyGpuSolverHandle(); - container::kernels::destroyGpuBlasHandle(); - container::kernels::destroyGpuSolverHandle(); -#endif - } - -#ifdef __DSP - std::cout << " ** Closing DSP Hardware..." << std::endl; - mtfunc::dspDestoryHandle(GlobalV::MY_RANK); -#endif - if (PARAM.inp.device == "gpu" || PARAM.inp.precision == "single") { delete this->kspw_psi; diff --git a/source/source_main/driver.h b/source/source_main/driver.h index bf867e2e75..9bdcf1d285 100644 --- a/source/source_main/driver.h +++ b/source/source_main/driver.h @@ -40,6 +40,7 @@ class Driver // Init harewares according to Input parameters void init_hardware(); + void finalize_hardware(); }; #endif diff --git a/source/source_main/driver_run.cpp b/source/source_main/driver_run.cpp index 7e1d868f12..579b8fbd08 100644 --- a/source/source_main/driver_run.cpp +++ b/source/source_main/driver_run.cpp @@ -106,6 +106,7 @@ void Driver::driver_run() p_esolver->after_all_runners(ucell); ModuleESolver::clean_esolver(p_esolver); + this->finalize_hardware(); //! 6: output the json file Json::create_Json(&ucell, PARAM); @@ -113,7 +114,8 @@ void Driver::driver_run() return; } -void Driver::init_hardware(){ +void Driver::init_hardware() +{ #if ((defined __CUDA) || (defined __ROCM)) if (PARAM.inp.device == "gpu") { @@ -129,3 +131,21 @@ void Driver::init_hardware(){ mtfunc::dspInitHandle(GlobalV::MY_RANK); #endif } + +void Driver::finalize_hardware() +{ +#if defined(__CUDA) || defined(__ROCM) + if (PARAM.inp.device == "gpu") + { + ModuleBase::destoryBLAShandle(); + hsolver::destroyGpuSolverHandle(); + container::kernels::destroyGpuBlasHandle(); + container::kernels::destroyGpuSolverHandle(); + } +#endif + +#ifdef __DSP + std::cout << " ** Closing DSP Hardware..." << std::endl; + mtfunc::dspDestoryHandle(GlobalV::MY_RANK); +#endif +} \ No newline at end of file