diff options
author | Stanislav Kirillov <staskirillov@gmail.com> | 2022-02-10 16:46:08 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:46:08 +0300 |
commit | cb68f224c46a8ee52ac3fdd2a32534b8bb8dc134 (patch) | |
tree | 1a2c5ffcf89eb53ecd79dbc9bc0a195c27404d0c /library/cpp/threading/poor_man_openmp/thread_helper.h | |
parent | 92fe2b1e7bc79f7b95adef61714fc003f6ea4a1c (diff) | |
download | ydb-cb68f224c46a8ee52ac3fdd2a32534b8bb8dc134.tar.gz |
Restoring authorship annotation for Stanislav Kirillov <staskirillov@gmail.com>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/threading/poor_man_openmp/thread_helper.h')
-rw-r--r-- | library/cpp/threading/poor_man_openmp/thread_helper.h | 78 |
1 files changed, 39 insertions, 39 deletions
diff --git a/library/cpp/threading/poor_man_openmp/thread_helper.h b/library/cpp/threading/poor_man_openmp/thread_helper.h index d51cda8daa..0ecee0590b 100644 --- a/library/cpp/threading/poor_man_openmp/thread_helper.h +++ b/library/cpp/threading/poor_man_openmp/thread_helper.h @@ -1,45 +1,45 @@ -#pragma once - +#pragma once + #include <util/thread/pool.h> -#include <util/generic/utility.h> +#include <util/generic/utility.h> #include <util/generic/yexception.h> -#include <util/system/info.h> -#include <util/system/atomic.h> -#include <util/system/condvar.h> -#include <util/system/mutex.h> +#include <util/system/info.h> +#include <util/system/atomic.h> +#include <util/system/condvar.h> +#include <util/system/mutex.h> #include <util/stream/output.h> - + #include <functional> #include <cstdlib> - + class TMtpQueueHelper { -public: +public: TMtpQueueHelper() { - SetThreadCount(NSystemInfo::CachedNumberOfCpus()); - } + SetThreadCount(NSystemInfo::CachedNumberOfCpus()); + } IThreadPool* Get() { - return q.Get(); - } - size_t GetThreadCount() { - return ThreadCount; - } - void SetThreadCount(size_t threads) { - ThreadCount = threads; + return q.Get(); + } + size_t GetThreadCount() { + return ThreadCount; + } + void SetThreadCount(size_t threads) { + ThreadCount = threads; q = CreateThreadPool(ThreadCount); - } + } static TMtpQueueHelper& Instance(); -private: - size_t ThreadCount; +private: + size_t ThreadCount; TAutoPtr<IThreadPool> q; -}; - +}; + namespace NYmp { - inline void SetThreadCount(size_t threads) { + inline void SetThreadCount(size_t threads) { TMtpQueueHelper::Instance().SetThreadCount(threads); - } - + } + inline size_t GetThreadCount() { return TMtpQueueHelper::Instance().GetThreadCount(); } @@ -50,9 +50,9 @@ namespace NYmp { size_t threadCount = TMtpQueueHelper::Instance().GetThreadCount(); IThreadPool* queue = TMtpQueueHelper::Instance().Get(); - TCondVar cv; - TMutex mutex; - TAtomic counter = threadCount; + TCondVar cv; + TMutex mutex; + TAtomic counter = threadCount; std::exception_ptr err; for (size_t i = 0; i < threadCount; ++i) { @@ -68,12 +68,12 @@ namespace NYmp { } currentChunkStart += chunkSize * threadCount; - } + } } catch (...) { with_lock (mutex) { err = std::current_exception(); } - } + } with_lock (mutex) { if (AtomicDecrement(counter) == 0) { @@ -81,25 +81,25 @@ namespace NYmp { cv.Signal(); } } - }); - } + }); + } with_lock (mutex) { while (AtomicGet(counter) > 0) { cv.WaitI(mutex); } - } + } if (err) { std::rethrow_exception(err); } - } - + } + template <typename T> inline void ParallelForStaticAutoChunk(T begin, T end, std::function<void(T)> func) { const size_t taskSize = end - begin; const size_t threadCount = TMtpQueueHelper::Instance().GetThreadCount(); - ParallelForStaticChunk(begin, end, (taskSize + threadCount - 1) / threadCount, func); - } + ParallelForStaticChunk(begin, end, (taskSize + threadCount - 1) / threadCount, func); + } } |