From 360ad3ffc8bdd26990d795c47b530ee531207164 Mon Sep 17 00:00:00 2001 From: luoxiaoc Date: Mon, 5 Jun 2023 13:44:30 +0800 Subject: [PATCH 1/2] add SelectorSleepNs Signed-off-by: luoxiaoc --- .../runtime/_c_message_infrastructure/csrc/core/utils.h | 9 +++++++++ .../runtime/_c_message_infrastructure/csrc/port_proxy.h | 5 ++++- .../magma/runtime/message_infrastructure/__init__.py | 4 ++-- src/lava/magma/runtime/message_infrastructure/ports.py | 5 +++-- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/lava/magma/runtime/_c_message_infrastructure/csrc/core/utils.h b/src/lava/magma/runtime/_c_message_infrastructure/csrc/core/utils.h index 5e62c2e13..bcec8d994 100644 --- a/src/lava/magma/runtime/_c_message_infrastructure/csrc/core/utils.h +++ b/src/lava/magma/runtime/_c_message_infrastructure/csrc/core/utils.h @@ -145,6 +145,15 @@ static void Sleep() { std::this_thread::sleep_for(std::chrono::nanoseconds(SLEEP_NS)); #endif } + +static void Sleep(int64_t ns) { +#if defined(ENABLE_MM_PAUSE) + _mm_pause(); +#else + std::this_thread::sleep_for(std::chrono::nanoseconds(ns)); +#endif +} + } #if defined(DDS_CHANNEL) diff --git a/src/lava/magma/runtime/_c_message_infrastructure/csrc/port_proxy.h b/src/lava/magma/runtime/_c_message_infrastructure/csrc/port_proxy.h index 1d0e9d3e7..2011c9819 100644 --- a/src/lava/magma/runtime/_c_message_infrastructure/csrc/port_proxy.h +++ b/src/lava/magma/runtime/_c_message_infrastructure/csrc/port_proxy.h @@ -93,13 +93,16 @@ using RecvPortProxyList = std::vector; class Selector { public: pybind11::object Select(std::vector> *args) { + py::function>> *args, int64_t sleep_ns) { while (true) { for (auto it = args->begin(); it != args->end(); ++it) { if (std::get<0>(*it)->Probe()) { return std::get<1>(*it)(); } } + if (sleep_ns > 0) { + helper::Sleep(sleep_ns); + } } } }; diff --git a/src/lava/magma/runtime/message_infrastructure/__init__.py b/src/lava/magma/runtime/message_infrastructure/__init__.py index e2da0ea69..2d7060616 100644 --- a/src/lava/magma/runtime/message_infrastructure/__init__.py +++ b/src/lava/magma/runtime/message_infrastructure/__init__.py @@ -86,11 +86,11 @@ def load_library(): AbstractTransferPort, # noqa # nosec support_grpc_channel, support_fastdds_channel, - support_cyclonedds_channel, - CPPSelector) + support_cyclonedds_channel) ChannelQueueSize = 1 SyncChannelBytes = 128 + SelectorSleepNs = 1 from .ports import ( # noqa # nosec SendPort, # noqa # nosec diff --git a/src/lava/magma/runtime/message_infrastructure/ports.py b/src/lava/magma/runtime/message_infrastructure/ports.py index 2f6af8ba5..cb60c0a88 100644 --- a/src/lava/magma/runtime/message_infrastructure/ports.py +++ b/src/lava/magma/runtime/message_infrastructure/ports.py @@ -3,7 +3,7 @@ # See: https://spdx.org/licenses/ from lava.magma.runtime.message_infrastructure \ - import ChannelQueueSize, CPPSelector + import ChannelQueueSize, SelectorSleepNs from lava.magma.runtime.message_infrastructure.MessageInfrastructurePywrapper \ import Channel as CppChannel from lava.magma.runtime.message_infrastructure.MessageInfrastructurePywrapper \ @@ -14,6 +14,7 @@ support_cyclonedds_channel, AbstractTransferPort, ChannelType, + CPPSelector, RecvPort) import numpy as np @@ -23,7 +24,7 @@ class Selector(CPPSelector): def select(self, *args: ty.Tuple[RecvPort, ty.Callable[[], ty.Any]]): - return super().select(args) + return super().select(args, SelectorSleepNs) class SendPort(AbstractTransferPort): From 446e2feeff1d86f3fdd0bac4eb0465675eb395d7 Mon Sep 17 00:00:00 2001 From: luoxiaoc Date: Mon, 5 Jun 2023 15:33:58 +0800 Subject: [PATCH 2/2] change default sleep to 1ms Signed-off-by: luoxiaoc --- .../runtime/_c_message_infrastructure/csrc/core/utils.h | 6 +++--- .../runtime/_c_message_infrastructure/csrc/port_proxy.h | 6 ++---- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/lava/magma/runtime/_c_message_infrastructure/csrc/core/utils.h b/src/lava/magma/runtime/_c_message_infrastructure/csrc/core/utils.h index bcec8d994..6a2bf7e19 100644 --- a/src/lava/magma/runtime/_c_message_infrastructure/csrc/core/utils.h +++ b/src/lava/magma/runtime/_c_message_infrastructure/csrc/core/utils.h @@ -18,7 +18,7 @@ #endif #define MAX_ARRAY_DIMS (5) -#define SLEEP_NS (1) +#define SLEEP_MS (1) #define LAVA_SIZEOF_CHAR (sizeof(char)) #define LAVA_SIZEOF_UCHAR (LAVA_SIZEOF_CHAR) @@ -142,7 +142,7 @@ static void Sleep() { #if defined(ENABLE_MM_PAUSE) _mm_pause(); #else - std::this_thread::sleep_for(std::chrono::nanoseconds(SLEEP_NS)); + std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_MS)); #endif } @@ -154,7 +154,7 @@ static void Sleep(int64_t ns) { #endif } -} +} // namespace helper #if defined(DDS_CHANNEL) // Default Parameters diff --git a/src/lava/magma/runtime/_c_message_infrastructure/csrc/port_proxy.h b/src/lava/magma/runtime/_c_message_infrastructure/csrc/port_proxy.h index 2011c9819..b5547b166 100644 --- a/src/lava/magma/runtime/_c_message_infrastructure/csrc/port_proxy.h +++ b/src/lava/magma/runtime/_c_message_infrastructure/csrc/port_proxy.h @@ -93,16 +93,14 @@ using RecvPortProxyList = std::vector; class Selector { public: pybind11::object Select(std::vector> *args, int64_t sleep_ns) { + py::function>> *args, const int64_t sleep_ns) { while (true) { for (auto it = args->begin(); it != args->end(); ++it) { if (std::get<0>(*it)->Probe()) { return std::get<1>(*it)(); } } - if (sleep_ns > 0) { - helper::Sleep(sleep_ns); - } + helper::Sleep(sleep_ns); } } };