| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
- // All rights reserved.
- // Code licensed under the BSD License.
- // http://www.anki3d.org/LICENSE
- #include <Tests/Framework/Framework.h>
- #include <AnKi/Util/ThreadHive.h>
- #include <AnKi/Util/HighRezTimer.h>
- #include <AnKi/Util/System.h>
- namespace anki {
- class ThreadHiveTestContext
- {
- public:
- ThreadHiveTestContext()
- {
- }
- ~ThreadHiveTestContext()
- {
- }
- union
- {
- Atomic<I32> m_countAtomic;
- I32 m_count;
- };
- };
- static void decNumber(void* arg, U32, ThreadHive& hive, ThreadHiveSemaphore* sem)
- {
- ThreadHiveTestContext* ctx = static_cast<ThreadHiveTestContext*>(arg);
- ctx->m_countAtomic.fetchSub(2);
- }
- static void incNumber(void* arg, U32, ThreadHive& hive, ThreadHiveSemaphore* sem)
- {
- ThreadHiveTestContext* ctx = static_cast<ThreadHiveTestContext*>(arg);
- ctx->m_countAtomic.fetchAdd(4);
- hive.submitTask(decNumber, arg);
- }
- static void taskToWaitOn(void* arg, U32, ThreadHive& hive, ThreadHiveSemaphore* sem)
- {
- ThreadHiveTestContext* ctx = static_cast<ThreadHiveTestContext*>(arg);
- HighRezTimer::sleep(1.0);
- ctx->m_count = 10;
- HighRezTimer::sleep(0.1);
- }
- static void taskToWait(void* arg, U32 threadId, ThreadHive& hive, ThreadHiveSemaphore* sem)
- {
- ThreadHiveTestContext* ctx = static_cast<ThreadHiveTestContext*>(arg);
- U prev = ctx->m_countAtomic.fetchAdd(1);
- ANKI_TEST_EXPECT_GEQ(prev, 10);
- }
- ANKI_TEST(Util, ThreadHive)
- {
- const U32 threadCount = 32;
- HeapAllocator<U8> alloc(allocAligned, nullptr);
- ThreadHive hive(threadCount, alloc);
- // Simple test
- if(1)
- {
- ThreadHiveTestContext ctx;
- ctx.m_countAtomic.setNonAtomically(0);
- const U INITIAL_TASK_COUNT = 100;
- for(U i = 0; i < INITIAL_TASK_COUNT; ++i)
- {
- hive.submitTask(incNumber, &ctx);
- }
- hive.waitAllTasks();
- ANKI_TEST_EXPECT_EQ(ctx.m_countAtomic.getNonAtomically(), INITIAL_TASK_COUNT * 2);
- }
- // Depedency tests
- if(1)
- {
- ThreadHiveTestContext ctx;
- ctx.m_count = 0;
- ThreadHiveTask task;
- task.m_callback = taskToWaitOn;
- task.m_argument = &ctx;
- task.m_signalSemaphore = hive.newSemaphore(1);
- hive.submitTasks(&task, 1);
- const U DEP_TASKS = 10;
- ThreadHiveTask dtasks[DEP_TASKS];
- ThreadHiveSemaphore* sem = hive.newSemaphore(DEP_TASKS);
- for(U i = 0; i < DEP_TASKS; ++i)
- {
- dtasks[i].m_callback = taskToWait;
- dtasks[i].m_argument = &ctx;
- dtasks[i].m_waitSemaphore = task.m_signalSemaphore;
- dtasks[i].m_signalSemaphore = sem;
- }
- hive.submitTasks(&dtasks[0], DEP_TASKS);
- // Again
- ThreadHiveTask dtasks2[DEP_TASKS];
- for(U i = 0; i < DEP_TASKS; ++i)
- {
- dtasks2[i].m_callback = taskToWait;
- dtasks2[i].m_argument = &ctx;
- dtasks2[i].m_waitSemaphore = sem;
- }
- hive.submitTasks(&dtasks2[0], DEP_TASKS);
- hive.waitAllTasks();
- ANKI_TEST_EXPECT_EQ(ctx.m_countAtomic.getNonAtomically(), DEP_TASKS * 2 + 10);
- }
- // Fuzzy test
- if(1)
- {
- ThreadHiveTestContext ctx;
- ctx.m_count = 0;
- I number = 0;
- ThreadHiveSemaphore* sem = nullptr;
- const U SUBMISSION_COUNT = 100;
- const U TASK_COUNT = 1000;
- for(U i = 0; i < SUBMISSION_COUNT; ++i)
- {
- for(U j = 0; j < TASK_COUNT; ++j)
- {
- Bool cb = rand() % 2;
- number = (cb) ? number + 2 : number - 2;
- ThreadHiveTask task;
- task.m_callback = (cb) ? incNumber : decNumber;
- task.m_argument = &ctx;
- task.m_signalSemaphore = hive.newSemaphore(1);
- if((rand() % 3) == 0 && j > 0 && sem)
- {
- task.m_waitSemaphore = sem;
- }
- hive.submitTasks(&task, 1);
- if((rand() % 7) == 0)
- {
- sem = task.m_signalSemaphore;
- }
- }
- sem = nullptr;
- hive.waitAllTasks();
- }
- ANKI_TEST_EXPECT_EQ(ctx.m_countAtomic.getNonAtomically(), number);
- }
- }
- class FibTask
- {
- public:
- Atomic<U64>* m_sum;
- StackAllocator<U8> m_alloc;
- U64 m_n;
- FibTask(Atomic<U64>* sum, StackAllocator<U8>& alloc, U64 n)
- : m_sum(sum)
- , m_alloc(alloc)
- , m_n(n)
- {
- }
- void doWork(ThreadHive& hive)
- {
- if(m_n > 1)
- {
- FibTask* a = m_alloc.newInstance<FibTask>(m_sum, m_alloc, m_n - 1);
- FibTask* b = m_alloc.newInstance<FibTask>(m_sum, m_alloc, m_n - 2);
- Array<ThreadHiveTask, 2> tasks;
- tasks[0].m_callback = tasks[1].m_callback = FibTask::callback;
- tasks[0].m_argument = a;
- tasks[1].m_argument = b;
- hive.submitTasks(&tasks[0], tasks.getSize());
- }
- else
- {
- m_sum->fetchAdd(m_n);
- }
- }
- static void callback(void* arg, U32, ThreadHive& hive, ThreadHiveSemaphore* sem)
- {
- static_cast<FibTask*>(arg)->doWork(hive);
- }
- };
- static U64 fib(U64 n)
- {
- if(n > 1)
- {
- return fib(n - 1) + fib(n - 2);
- }
- else
- {
- return n;
- }
- }
- ANKI_TEST(Util, ThreadHiveBench)
- {
- static const U FIB_N = 32;
- const U32 threadCount = getCpuCoresCount();
- HeapAllocator<U8> alloc(allocAligned, nullptr);
- ThreadHive hive(threadCount, alloc, true);
- StackAllocator<U8> salloc(allocAligned, nullptr, 1024);
- Atomic<U64> sum = {0};
- FibTask task(&sum, salloc, FIB_N);
- auto timeA = HighRezTimer::getCurrentTime();
- hive.submitTask(FibTask::callback, &task);
- hive.waitAllTasks();
- auto timeB = HighRezTimer::getCurrentTime();
- const U64 serialFib = fib(FIB_N);
- auto timeC = HighRezTimer::getCurrentTime();
- ANKI_TEST_LOGI("Total time %fms. Ground truth %fms", (timeB - timeA) * 1000.0, (timeC - timeB) * 1000.0);
- ANKI_TEST_EXPECT_EQ(sum.getNonAtomically(), serialFib);
- }
- } // end namespace anki
|