diff --git a/src/bridge/infini/rt.hpp b/src/bridge/infini/rt.hpp index b74b2250f..c5bec7ecd 100644 --- a/src/bridge/infini/rt.hpp +++ b/src/bridge/infini/rt.hpp @@ -15,7 +15,7 @@ inline infiniStatus_t translate(::infini::rt::runtime::Error error) { } } -inline ::infini::rt::Device::Type translate_to(infiniDevice_t device) { +inline constexpr ::infini::rt::Device::Type translate_to(infiniDevice_t device) { switch (device) { case INFINI_DEVICE_CPU: return ::infini::rt::Device::Type::kCpu; @@ -38,7 +38,7 @@ inline ::infini::rt::Device::Type translate_to(infiniDevice_t device) { } } -inline infiniDevice_t translate_from(::infini::rt::Device::Type device) { +inline constexpr infiniDevice_t translate_from(::infini::rt::Device::Type device) { switch (device) { case ::infini::rt::Device::Type::kCpu: return INFINI_DEVICE_CPU; diff --git a/src/infinicore/context/context_impl.cc b/src/infinicore/context/context_impl.cc index cec1d820d..9f6aa4a66 100644 --- a/src/infinicore/context/context_impl.cc +++ b/src/infinicore/context/context_impl.cc @@ -61,46 +61,37 @@ ContextImpl &ContextImpl::singleton() { return instance; } -ContextImpl::ContextImpl() { - std::vector device_counter(static_cast(Device::Type::COUNT), 0); - - infini::rt::set_runtime_device_type(bridge::infini::rt::translate_to(INFINI_DEVICE_CPU)); - INFINICORE_CHECK_ERROR(bridge::infini::rt::translate(infini::rt::runtime::GetDeviceCount(&device_counter[static_cast(Device::Type::CPU)]))); - - runtime_table_[static_cast(Device::Type::CPU)].resize(device_counter[static_cast(Device::Type::CPU)]); - if (device_counter[static_cast(Device::Type::CPU)] > 0) { - runtime_table_[static_cast(Device::Type::CPU)][0] = std::unique_ptr(new Runtime(Device(Device::Type::CPU, 0))); - } - - if constexpr (infini::rt::DeviceEnabled::value) { - infini::rt::set_runtime_device_type(bridge::infini::rt::translate_to(INFINI_DEVICE_NVIDIA)); - INFINICORE_CHECK_ERROR(bridge::infini::rt::translate(infini::rt::runtime::GetDeviceCount(&device_counter[static_cast(Device::Type::NVIDIA)]))); - runtime_table_[static_cast(Device::Type::NVIDIA)].resize(device_counter[static_cast(Device::Type::NVIDIA)]); - if (device_counter[static_cast(Device::Type::NVIDIA)] > 0) { - runtime_table_[static_cast(Device::Type::NVIDIA)][0] = std::unique_ptr(new Runtime(Device(Device::Type::NVIDIA, 0))); - current_runtime_ = runtime_table_[static_cast(Device::Type::NVIDIA)][0].get(); - } - } - - if constexpr (infini::rt::DeviceEnabled::value) { - infini::rt::set_runtime_device_type(bridge::infini::rt::translate_to(INFINI_DEVICE_ILUVATAR)); - INFINICORE_CHECK_ERROR(bridge::infini::rt::translate(infini::rt::runtime::GetDeviceCount(&device_counter[static_cast(Device::Type::ILUVATAR)]))); - runtime_table_[static_cast(Device::Type::ILUVATAR)].resize(device_counter[static_cast(Device::Type::ILUVATAR)]); - if (device_counter[static_cast(Device::Type::ILUVATAR)] > 0) { - runtime_table_[static_cast(Device::Type::ILUVATAR)][0] = std::unique_ptr(new Runtime(Device(Device::Type::ILUVATAR, 0))); - current_runtime_ = runtime_table_[static_cast(Device::Type::ILUVATAR)][0].get(); +template +void ContextImpl::initializeRuntime() { + constexpr auto runtime_device_type = bridge::infini::rt::translate_to( + static_cast(device_type)); + static_assert(runtime_device_type != infini::rt::Device::Type::kCount, + "InfiniCore device does not map to an InfiniRT runtime"); + + if constexpr (infini::rt::DeviceEnabled::value) { + constexpr auto device_index = static_cast(device_type); + infini::rt::set_runtime_device_type(runtime_device_type); + + int device_count = 0; + INFINICORE_CHECK_ERROR(bridge::infini::rt::translate( + infini::rt::runtime::GetDeviceCount(&device_count))); + + auto &runtimes = runtime_table_[device_index]; + runtimes.resize(device_count); + if (device_count > 0) { + runtimes[0] = std::unique_ptr(new Runtime(Device(device_type, 0))); + if constexpr (device_type != Device::Type::CPU) { + current_runtime_ = runtimes[0].get(); + } } } +} - if constexpr (infini::rt::DeviceEnabled::value) { - infini::rt::set_runtime_device_type(bridge::infini::rt::translate_to(INFINI_DEVICE_ASCEND)); - INFINICORE_CHECK_ERROR(bridge::infini::rt::translate(infini::rt::runtime::GetDeviceCount(&device_counter[static_cast(Device::Type::ASCEND)]))); - runtime_table_[static_cast(Device::Type::ASCEND)].resize(device_counter[static_cast(Device::Type::ASCEND)]); - if (device_counter[static_cast(Device::Type::ASCEND)] > 0) { - runtime_table_[static_cast(Device::Type::ASCEND)][0] = std::unique_ptr(new Runtime(Device(Device::Type::ASCEND, 0))); - current_runtime_ = runtime_table_[static_cast(Device::Type::ASCEND)][0].get(); - } - } +ContextImpl::ContextImpl() { + initializeRuntime(); + initializeRuntime(); + initializeRuntime(); + initializeRuntime(); if (current_runtime_ == nullptr && !runtime_table_[static_cast(Device::Type::CPU)].empty()) { current_runtime_ = runtime_table_[static_cast(Device::Type::CPU)][0].get(); diff --git a/src/infinicore/context/context_impl.hpp b/src/infinicore/context/context_impl.hpp index 5eb7d0bff..b05a56f5d 100644 --- a/src/infinicore/context/context_impl.hpp +++ b/src/infinicore/context/context_impl.hpp @@ -8,6 +8,9 @@ namespace infinicore { class ContextImpl { private: + template + void initializeRuntime(); + // Table of runtimes for every device (type and index) std::array>, size_t(Device::Type::COUNT)> runtime_table_; // Active runtime for current thread. Can use "static thread local" because context is a process singleton.