ThreadJobManager.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <Tests/Framework/Framework.h>
  6. #include <AnKi/Util/ThreadJobManager.h>
  7. #include <AnKi/Util/HighRezTimer.h>
  8. #include <AnKi/Util/System.h>
  9. #include <AnKi/Util/Tracer.h>
  10. using namespace anki;
  11. ANKI_TEST(Util, ThreadJobManager)
  12. {
  13. DefaultMemoryPool::allocateSingleton(allocAligned, nullptr);
  14. // Simple test
  15. {
  16. constexpr U32 kTaskCount = 64;
  17. ThreadJobManager manager(getCpuCoresCount(), true, 16);
  18. Atomic<U32> atomic(0);
  19. for(U32 i = 0; i < kTaskCount; ++i)
  20. {
  21. manager.dispatchTask([&atomic]([[maybe_unused]] U32 tid) {
  22. HighRezTimer::sleep(1.0_sec);
  23. atomic.fetchAdd(1);
  24. });
  25. }
  26. manager.waitForAllTasksToFinish();
  27. ANKI_TEST_EXPECT_EQ(atomic.load(), kTaskCount);
  28. }
  29. DefaultMemoryPool::freeSingleton();
  30. }
  31. ANKI_TEST(Util, ThreadJobManagerBench)
  32. {
  33. DefaultMemoryPool::allocateSingleton(allocAligned, nullptr);
  34. const U64 time = HighRezTimer::getCurrentTimeUs();
  35. {
  36. constexpr U32 kTaskCount = 20 * 1024 * 1024;
  37. ThreadJobManager manager(getCpuCoresCount(), true, 256);
  38. Atomic<U32> atomic(0);
  39. for(U32 i = 0; i < kTaskCount; ++i)
  40. {
  41. manager.dispatchTask([&atomic]([[maybe_unused]] U32 tid) {
  42. atomic.fetchAdd(1);
  43. });
  44. }
  45. manager.waitForAllTasksToFinish();
  46. ANKI_TEST_EXPECT_EQ(atomic.load(), kTaskCount);
  47. }
  48. const U64 timeDiff = HighRezTimer::getCurrentTimeUs() - time;
  49. ANKI_TEST_LOGI("Time spent %lu us / %lu ms", timeDiff, timeDiff / 1000);
  50. DefaultMemoryPool::freeSingleton();
  51. }