From 1c960832041a722e7aeaab674aba164914b279d2 Mon Sep 17 00:00:00 2001 From: Chen Nuo <49788094+Cstandardlib@users.noreply.github.com> Date: Mon, 18 May 2026 16:37:37 +0800 Subject: [PATCH 1/2] refactor(device): remove dead code from DeviceContext, add dsp_count guard MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Remove unused device_type subsystem from DeviceContext: - Delete set_device_type(), get_device_type(), is_cpu(), is_gpu(), is_dsp() methods (all zero callers verified via exhaustive search) - Delete is_initialized(), is_gpu_enabled() (zero callers) - Delete device_type_ private field (only consumed by removed methods) - Delete standalone get_device_type(const DeviceContext*) function (zero callers; all 48 call sites use the template version get_device_type(const Device*)) - Delete forward declaration in device_helpers.h Add assert(PARAM.inp.dsp_count > 0) guard in driver.cpp to prevent modulo-by-zero undefined behavior. All other DeviceContext members retained (init(), get_device_id(), get_device_count(), get_local_rank() — all have active callers). Build verified with cmake --build (MPI+LCAO). --- source/source_base/module_device/device.h | 54 ------------------- .../module_device/device_helpers.h | 7 --- source/source_main/driver.cpp | 2 + 3 files changed, 2 insertions(+), 61 deletions(-) diff --git a/source/source_base/module_device/device.h b/source/source_base/module_device/device.h index 453c304bb27..d6ffd5f3f7e 100644 --- a/source/source_base/module_device/device.h +++ b/source/source_base/module_device/device.h @@ -117,18 +117,6 @@ class DeviceContext { */ void init(); - /** - * @brief Check if the DeviceContext has been initialized - * @return true if init() has been called successfully - */ - bool is_initialized() const { return initialized_; } - - /** - * @brief Check if GPU is enabled and available - * @return true if GPU device is bound and usable - */ - bool is_gpu_enabled() const { return gpu_enabled_; } - /** * @brief Get the bound GPU device ID * @return Device ID (0-based), or -1 if not initialized @@ -147,36 +135,6 @@ class DeviceContext { */ int get_local_rank() const { return local_rank_; } - /** - * @brief Set the device type (CpuDevice, GpuDevice, or DspDevice) - * @param type The device type - */ - void set_device_type(AbacusDevice_t type) { device_type_ = type; } - - /** - * @brief Get the device type - * @return AbacusDevice_t The device type - */ - AbacusDevice_t get_device_type() const { return device_type_; } - - /** - * @brief Check if the device is CPU - * @return true if the device is CPU - */ - bool is_cpu() const { return device_type_ == CpuDevice; } - - /** - * @brief Check if the device is GPU - * @return true if the device is GPU - */ - bool is_gpu() const { return device_type_ == GpuDevice; } - - /** - * @brief Check if the device is DSP - * @return true if the device is DSP - */ - bool is_dsp() const { return device_type_ == DspDevice; } - // Disable copy and assignment DeviceContext(const DeviceContext&) = delete; DeviceContext& operator=(const DeviceContext&) = delete; @@ -190,21 +148,9 @@ class DeviceContext { int device_id_ = -1; int device_count_ = 0; int local_rank_ = 0; - AbacusDevice_t device_type_ = CpuDevice; - std::mutex init_mutex_; }; -/** - * @brief Get the device type enum from DeviceContext (runtime version). - * @param ctx Pointer to DeviceContext - * @return AbacusDevice_t enum value - */ -inline AbacusDevice_t get_device_type(const DeviceContext* ctx) -{ - return ctx->get_device_type(); -} - } // end of namespace base_device #endif // MODULE_DEVICE_H_ diff --git a/source/source_base/module_device/device_helpers.h b/source/source_base/module_device/device_helpers.h index 2870eea2d78..314f1525ed7 100644 --- a/source/source_base/module_device/device_helpers.h +++ b/source/source_base/module_device/device_helpers.h @@ -21,13 +21,6 @@ namespace base_device // Forward declaration class DeviceContext; -/** - * @brief Get the device type enum from DeviceContext (runtime version). - * @param ctx Pointer to DeviceContext - * @return AbacusDevice_t enum value - */ -inline AbacusDevice_t get_device_type(const DeviceContext* ctx); - /** * @brief Get the device type enum for a given device type (compile-time version). * @tparam Device The device type (DEVICE_CPU or DEVICE_GPU) diff --git a/source/source_main/driver.cpp b/source/source_main/driver.cpp index 8727d0d4085..5a085969a26 100644 --- a/source/source_main/driver.cpp +++ b/source/source_main/driver.cpp @@ -13,6 +13,7 @@ #include "source_main/version.h" #include "source_base/parallel_global.h" #ifdef __DSP +#include #include "source_base/module_device/memory_op.h" #include "source_base/module_external/blas_connector.h" #endif @@ -130,6 +131,7 @@ void Driver::reading() #endif #ifdef __DSP + assert(PARAM.inp.dsp_count > 0); base_device::memory::set_dsp_cluster_id(GlobalV::MY_RANK % PARAM.inp.dsp_count); BlasConnector::set_dsp_cluster_id(GlobalV::MY_RANK % PARAM.inp.dsp_count); #endif From 50ec3bb0110d6b4c833d830a32710f3fd024eac1 Mon Sep 17 00:00:00 2001 From: Chen Nuo <49788094+Cstandardlib@users.noreply.github.com> Date: Mon, 18 May 2026 16:56:24 +0800 Subject: [PATCH 2/2] fix(dsp): replace assert with runtime WARNING_QUIT for dsp_count assert() is removed in release builds (NDEBUG), leaving modulo-by-zero\nunprotected. Replace with WARNING_QUIT that works in all builds.\n\nAlso remove now-unused #include from the #ifdef __DSP block.\n\nAddresses PR review feedback on #7361. --- source/source_main/driver.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/source/source_main/driver.cpp b/source/source_main/driver.cpp index 5a085969a26..93e98ee461d 100644 --- a/source/source_main/driver.cpp +++ b/source/source_main/driver.cpp @@ -13,7 +13,6 @@ #include "source_main/version.h" #include "source_base/parallel_global.h" #ifdef __DSP -#include #include "source_base/module_device/memory_op.h" #include "source_base/module_external/blas_connector.h" #endif @@ -131,7 +130,10 @@ void Driver::reading() #endif #ifdef __DSP - assert(PARAM.inp.dsp_count > 0); + if (PARAM.inp.dsp_count <= 0) + { + ModuleBase::WARNING_QUIT("driver", "dsp_count must be > 0"); + } base_device::memory::set_dsp_cluster_id(GlobalV::MY_RANK % PARAM.inp.dsp_count); BlasConnector::set_dsp_cluster_id(GlobalV::MY_RANK % PARAM.inp.dsp_count); #endif