-
Notifications
You must be signed in to change notification settings - Fork 51
feat: Implemented GlobalThreadPool for async functions #603
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
b37a2cc
aa40e59
c42df70
1d89f5f
c39e2a8
b01235d
5d4e588
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -73,4 +73,10 @@ timesteps | |
| Timesteps | ||
| denoises | ||
| denoise | ||
| denoising | ||
| denoising | ||
| threadpool | ||
| chrono | ||
| setpriority | ||
| errno | ||
| ifdef | ||
| elif | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| // GlobalThreadPool.h | ||
| #pragma once | ||
|
|
||
| #include <executorch/extension/threadpool/cpuinfo_utils.h> | ||
| #include <memory> | ||
| #include <mutex> | ||
| #include <optional> | ||
| #include <rnexecutorch/Log.h> | ||
| #include <rnexecutorch/threads/HighPerformanceThreadPool.h> | ||
|
|
||
| namespace rnexecutorch::threads { | ||
|
|
||
| class GlobalThreadPool { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider removing this class and leaving all functions inside the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
class Singleton {
public:
Singleton(const Singleton&) = delete;
Singleton& operator=(const Singleton&) = delete;
Singleton(Singleton&&) = delete;
Singleton& operator=(Singleton&&) = delete;
static Singleton& getInstance() {
static std::once_flag initFlag;
if (!instance) {
throw std::logic_error("Instance not initialized.");
}
return *instance;
}
static void initialize(int arg) {
static std::once_flag initFlag;
std::call_once(initFlag, [arg]() {
instance = std::make_unique<Singleton>(arg);
});
}
private:
int value;
Singleton(int val) : value(val) {}
static std::unique_ptr<Singleton> instance;
};
std::unique_ptr<Singleton> Singleton::instance = nullptr;
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you meant
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently,
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That was my idea when creating this, |
||
| public: | ||
| GlobalThreadPool() = delete; | ||
mkopcins marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| GlobalThreadPool(const GlobalThreadPool &) = delete; | ||
| GlobalThreadPool &operator=(const GlobalThreadPool &) = delete; | ||
| GlobalThreadPool(GlobalThreadPool &&) = delete; | ||
| GlobalThreadPool &operator=(GlobalThreadPool &&) = delete; | ||
|
|
||
| static HighPerformanceThreadPool &get() { | ||
| if (!instance) { | ||
| initialize(); | ||
| } | ||
| return *instance; | ||
| } | ||
|
|
||
| static void initialize(std::optional<uint32_t> numThreads = std::nullopt, | ||
| ThreadConfig config = {}) { | ||
| std::call_once(initFlag, [&numThreads, config]() { | ||
| if (!numThreads) { | ||
| numThreads = | ||
| ::executorch::extension::cpuinfo::get_num_performant_cores(); | ||
| } | ||
|
|
||
| log(rnexecutorch::LOG_LEVEL::Info, "Initializing global thread pool with", | ||
| numThreads, "threads"); | ||
| instance = std::make_unique<HighPerformanceThreadPool>(numThreads.value(), | ||
| config); | ||
| }); | ||
| } | ||
|
|
||
| // Convenience methods that mirror std::thread interface | ||
| template <typename Func, typename... Args> | ||
| static auto async(Func &&func, Args &&...args) { | ||
| return get().submit(std::forward<Func>(func), std::forward<Args>(args)...); | ||
| } | ||
|
|
||
| template <typename Func, typename... Args> | ||
| static auto async_high_priority(Func &&func, Args &&...args) { | ||
| return get().submitWithPriority(Priority::HIGH, std::forward<Func>(func), | ||
| std::forward<Args>(args)...); | ||
| } | ||
|
|
||
| // Fire and forget (like std::thread{}.detach()) | ||
| template <typename Func, typename... Args> | ||
| static void detach(Func &&func, Args &&...args) { | ||
| get().submitDetached(std::forward<Func>(func), std::forward<Args>(args)...); | ||
| } | ||
|
|
||
| // Execute and wait (like std::thread{}.join()) | ||
| template <typename Func, typename... Args> | ||
| static auto execute(Func &&func, Args &&...args) { | ||
| return get().execute(std::forward<Func>(func), std::forward<Args>(args)...); | ||
| } | ||
|
|
||
| static void shutdown() { | ||
| if (instance) { | ||
| instance->shutdown(); | ||
| instance.reset(); | ||
| } | ||
| } | ||
|
|
||
| private: | ||
| inline static std::unique_ptr<HighPerformanceThreadPool> instance; | ||
| inline static std::once_flag initFlag; | ||
| }; | ||
|
|
||
| } // namespace rnexecutorch::threads | ||
Uh oh!
There was an error while loading. Please reload this page.