tbb_local_executor.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #pragma once
  2. #include "local_executor.h"
  3. #define __TBB_TASK_ISOLATION 1
  4. #define __TBB_NO_IMPLICIT_LINKAGE 1
  5. #include <contrib/libs/tbb/include/tbb/blocked_range.h>
  6. #include <contrib/libs/tbb/include/tbb/parallel_for.h>
  7. #include <contrib/libs/tbb/include/tbb/task_arena.h>
  8. #include <contrib/libs/tbb/include/tbb/task_group.h>
  9. namespace NPar {
  10. template <bool RespectTls = false>
  11. class TTbbLocalExecutor final: public ILocalExecutor {
  12. public:
  13. TTbbLocalExecutor(int nThreads)
  14. : ILocalExecutor()
  15. , TbbArena(nThreads)
  16. , NumberOfTbbThreads(nThreads) {}
  17. ~TTbbLocalExecutor() noexcept override {}
  18. // 0-based ILocalExecutor worker thread identification
  19. virtual int GetWorkerThreadId() const noexcept override;
  20. virtual int GetThreadCount() const noexcept override;
  21. // Add task for further execution.
  22. //
  23. // @param exec Task description.
  24. // @param id Task argument.
  25. // @param flags Bitmask composed by `HIGH_PRIORITY`, `MED_PRIORITY`, `LOW_PRIORITY`
  26. // and `WAIT_COMPLETE`.
  27. virtual void Exec(TIntrusivePtr<ILocallyExecutable> exec, int id, int flags) override;
  28. // Add tasks range for further execution.
  29. //
  30. // @param exec Task description.
  31. // @param firstId, lastId Task arguments [firstId, lastId)
  32. // @param flags Same as for `Exec`.
  33. virtual void ExecRange(TIntrusivePtr<ILocallyExecutable> exec, int firstId, int lastId, int flags) override;
  34. // Submit tasks for async run
  35. void SubmitAsyncTasks(TLocallyExecutableFunction exec, int firstId, int lastId);
  36. private:
  37. mutable tbb::task_arena TbbArena;
  38. tbb::task_group Group;
  39. int NumberOfTbbThreads;
  40. };
  41. }