tbb_local_executor.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #include "tbb_local_executor.h"
  2. template <bool RespectTls>
  3. void NPar::TTbbLocalExecutor<RespectTls>::SubmitAsyncTasks(TLocallyExecutableFunction exec, int firstId, int lastId) {
  4. for (int i = firstId; i < lastId; ++i) {
  5. Group.run([=] { exec(i); });
  6. }
  7. }
  8. template <bool RespectTls>
  9. int NPar::TTbbLocalExecutor<RespectTls>::GetThreadCount() const noexcept {
  10. return NumberOfTbbThreads - 1;
  11. }
  12. template <bool RespectTls>
  13. int NPar::TTbbLocalExecutor<RespectTls>::GetWorkerThreadId() const noexcept {
  14. return TbbArena.execute([] {
  15. return tbb::this_task_arena::current_thread_index();
  16. });
  17. }
  18. template <bool RespectTls>
  19. void NPar::TTbbLocalExecutor<RespectTls>::Exec(TIntrusivePtr<ILocallyExecutable> exec, int id, int flags) {
  20. if (flags & WAIT_COMPLETE) {
  21. exec->LocalExec(id);
  22. } else {
  23. TbbArena.execute([=] {
  24. SubmitAsyncTasks([=] (int id) { exec->LocalExec(id); }, id, id + 1);
  25. });
  26. }
  27. }
  28. template <bool RespectTls>
  29. void NPar::TTbbLocalExecutor<RespectTls>::ExecRange(TIntrusivePtr<ILocallyExecutable> exec, int firstId, int lastId, int flags) {
  30. if (flags & WAIT_COMPLETE) {
  31. TbbArena.execute([=] {
  32. if (RespectTls) {
  33. tbb::this_task_arena::isolate([=]{
  34. tbb::parallel_for(firstId, lastId, [=] (int id) { exec->LocalExec(id); });
  35. });
  36. } else {
  37. tbb::parallel_for(firstId, lastId, [=] (int id) { exec->LocalExec(id); });
  38. }
  39. });
  40. } else {
  41. TbbArena.execute([=] {
  42. SubmitAsyncTasks([=] (int id) { exec->LocalExec(id); }, firstId, lastId);
  43. });
  44. }
  45. }
  46. template class NPar::TTbbLocalExecutor<true>;
  47. template class NPar::TTbbLocalExecutor<false>;