ThreadJobManager.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright (C) 2009-2023, 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. Tracer::allocateSingleton();
  15. // Simple test
  16. {
  17. constexpr U32 kTaskCount = 64;
  18. ThreadJobManager manager(getCpuCoresCount(), true, 16);
  19. Atomic<U32> atomic(0);
  20. for(U32 i = 0; i < kTaskCount; ++i)
  21. {
  22. manager.dispatchTask([&atomic]([[maybe_unused]] U32 tid) {
  23. HighRezTimer::sleep(1.0_sec);
  24. atomic.fetchAdd(1);
  25. });
  26. }
  27. manager.waitForAllTasksToFinish();
  28. ANKI_TEST_EXPECT_EQ(atomic.load(), kTaskCount);
  29. }
  30. DefaultMemoryPool::freeSingleton();
  31. Tracer::freeSingleton();
  32. }
  33. ANKI_TEST(Util, ThreadJobManagerBench)
  34. {
  35. DefaultMemoryPool::allocateSingleton(allocAligned, nullptr);
  36. Tracer::allocateSingleton();
  37. const U64 time = HighRezTimer::getCurrentTimeUs();
  38. {
  39. constexpr U32 kTaskCount = 20 * 1024 * 1024;
  40. ThreadJobManager manager(getCpuCoresCount(), true, 256);
  41. Atomic<U32> atomic(0);
  42. for(U32 i = 0; i < kTaskCount; ++i)
  43. {
  44. manager.dispatchTask([&atomic]([[maybe_unused]] U32 tid) {
  45. atomic.fetchAdd(1);
  46. });
  47. }
  48. manager.waitForAllTasksToFinish();
  49. ANKI_TEST_EXPECT_EQ(atomic.load(), kTaskCount);
  50. }
  51. const U64 timeDiff = HighRezTimer::getCurrentTimeUs() - time;
  52. ANKI_TEST_LOGI("Time spent %lu us / %lu ms", timeDiff, timeDiff / 1000);
  53. DefaultMemoryPool::freeSingleton();
  54. Tracer::freeSingleton();
  55. }