Jobs.cpp 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzCore/Jobs/Job.h>
  9. #include <AzCore/Jobs/JobCompletion.h>
  10. #include <AzCore/Jobs/JobCompletionSpin.h>
  11. #include <AzCore/Jobs/JobFunction.h>
  12. #include <AzCore/Jobs/JobManager.h>
  13. #include <AzCore/Jobs/task_group.h>
  14. #include <AzCore/Jobs/Algorithms.h>
  15. #include <AzCore/std/delegate/delegate.h>
  16. #include <AzCore/std/bind/bind.h>
  17. #include <AzCore/Math/Vector3.h>
  18. #include <AzCore/Math/Transform.h>
  19. #include <AzCore/Math/Random.h>
  20. #include <AzCore/std/containers/array.h>
  21. #include <AzCore/std/containers/fixed_list.h>
  22. #include <AzCore/std/containers/unordered_set.h>
  23. #include <AzCore/std/parallel/containers/concurrent_vector.h>
  24. #include <AzCore/Memory/SystemAllocator.h>
  25. #include <AzCore/Memory/PoolAllocator.h>
  26. #include <AzCore/std/time.h>
  27. #include <AzCore/std/parallel/thread.h>
  28. #include <AzCore/UnitTest/TestTypes.h>
  29. #include <random>
  30. #if AZ_TRAIT_SUPPORTS_MICROSOFT_PPL
  31. // Enable this to test against Microsoft PPL, keep in mind you MUST have Exceptions enabled to use PPL
  32. //# define AZ_COMPARE_TO_PPL
  33. #endif //
  34. //#define AZ_JOBS_PRINT_CALL_ORDER
  35. #if defined(AZ_COMPARE_TO_PPL)
  36. # include <ppl.h>
  37. #endif // AZ_COMPARE_TO_PPL
  38. using namespace AZ;
  39. using namespace AZ::Debug;
  40. namespace UnitTest
  41. {
  42. static const int g_fibonacciFast = 10;
  43. static const int g_fibonacciFastResult = 55;
  44. #ifdef _DEBUG
  45. static const int g_fibonacciSlow = 15;
  46. static const int g_fibonacciSlowResult = 610;
  47. #else
  48. static const int g_fibonacciSlow = 20;
  49. static const int g_fibonacciSlowResult = 6765;
  50. #endif
  51. static AZStd::sys_time_t s_totalJobsTime = 0;
  52. class DefaultJobManagerSetupFixture
  53. : public LeakDetectionFixture
  54. {
  55. protected:
  56. JobManager* m_jobManager = nullptr;
  57. JobContext* m_jobContext = nullptr;
  58. unsigned int m_numWorkerThreads;
  59. public:
  60. DefaultJobManagerSetupFixture(unsigned int numWorkerThreads = 0)
  61. : m_numWorkerThreads(numWorkerThreads)
  62. {
  63. }
  64. void SetUp() override
  65. {
  66. LeakDetectionFixture::SetUp();
  67. JobManagerDesc desc;
  68. JobManagerThreadDesc threadDesc;
  69. #if AZ_TRAIT_SET_JOB_PROCESSOR_ID
  70. threadDesc.m_cpuId = 0; // Don't set processors IDs on windows
  71. #endif // AZ_TRAIT_SET_JOB_PROCESSOR_ID
  72. if (m_numWorkerThreads == 0)
  73. {
  74. m_numWorkerThreads = AZStd::thread::hardware_concurrency();
  75. }
  76. m_numWorkerThreads = desc.GetWorkerThreadCount(m_numWorkerThreads);
  77. for (unsigned int i = 0; i < m_numWorkerThreads; ++i)
  78. {
  79. desc.m_workerThreads.push_back(threadDesc);
  80. #if AZ_TRAIT_SET_JOB_PROCESSOR_ID
  81. threadDesc.m_cpuId++;
  82. #endif // AZ_TRAIT_SET_JOB_PROCESSOR_ID
  83. }
  84. m_jobManager = aznew JobManager(desc);
  85. m_jobContext = aznew JobContext(*m_jobManager);
  86. JobContext::SetGlobalContext(m_jobContext);
  87. }
  88. void TearDown() override
  89. {
  90. JobContext::SetGlobalContext(nullptr);
  91. delete m_jobContext;
  92. delete m_jobManager;
  93. LeakDetectionFixture::TearDown();
  94. }
  95. };
  96. // BasicJobExample-Begin
  97. void Vector3Sum(const Vector3* array, unsigned int size, Vector3* result)
  98. {
  99. Vector3 sum = Vector3::CreateZero();
  100. for (unsigned int i = 0; i < size; ++i)
  101. {
  102. sum += array[i];
  103. }
  104. *result = sum;
  105. }
  106. class Vector3SumDelegate
  107. {
  108. public:
  109. Vector3SumDelegate(const Vector3* array, unsigned int size)
  110. : m_array(array)
  111. , m_size(size)
  112. {
  113. }
  114. void Process()
  115. {
  116. m_result = Vector3::CreateZero();
  117. for (unsigned int i = 0; i < m_size; ++i)
  118. {
  119. m_result += m_array[i];
  120. }
  121. }
  122. const Vector3& GetResult() const { return m_result; }
  123. private:
  124. const Vector3* m_array;
  125. unsigned int m_size;
  126. Vector3 m_result;
  127. };
  128. struct Vector3SumFunctor
  129. {
  130. Vector3SumFunctor(const Vector3* array, unsigned int size, Vector3* result)
  131. : m_array(array)
  132. , m_size(size)
  133. , m_result(result)
  134. {
  135. }
  136. void operator()()
  137. {
  138. Vector3 sum = Vector3::CreateZero();
  139. for (unsigned int i = 0; i < m_size; ++i)
  140. {
  141. sum += m_array[i];
  142. }
  143. * m_result = sum;
  144. }
  145. private:
  146. const Vector3* m_array;
  147. unsigned int m_size;
  148. Vector3* m_result;
  149. };
  150. class Vector3SumJob
  151. : public Job
  152. {
  153. public:
  154. AZ_CLASS_ALLOCATOR(Vector3SumJob, ThreadPoolAllocator);
  155. Vector3SumJob(const Vector3* array, unsigned int size, Vector3* result, JobContext* context = nullptr)
  156. : Job(true, context)
  157. , m_array(array)
  158. , m_size(size)
  159. , m_result(result)
  160. {
  161. }
  162. void Process() override
  163. {
  164. Vector3 sum = Vector3::CreateZero();
  165. for (unsigned int i = 0; i < m_size; ++i)
  166. {
  167. sum += m_array[i];
  168. }
  169. *m_result = sum;
  170. }
  171. private:
  172. const Vector3* m_array;
  173. unsigned int m_size;
  174. Vector3* m_result;
  175. };
  176. class JobBasicTest
  177. : public DefaultJobManagerSetupFixture
  178. {
  179. public:
  180. void run()
  181. {
  182. AZStd::fixed_vector<Vector3, 100> vecArray(100, Vector3::CreateOne());
  183. AZStd::sys_time_t tStart = AZStd::GetTimeNowMicroSecond();
  184. JobCompletionSpin doneJob(m_jobContext);
  185. //test user jobs
  186. {
  187. Vector3 result;
  188. Job* job = aznew Vector3SumJob(&vecArray[0], (unsigned int)vecArray.size(), &result, m_jobContext);
  189. doneJob.Reset(true);
  190. job->SetDependent(&doneJob);
  191. job->Start();
  192. doneJob.StartAndWaitForCompletion();
  193. AZ_TEST_ASSERT(result.IsClose(Vector3(100.0f, 100.0f, 100.0f)));
  194. }
  195. //test function jobs
  196. {
  197. Vector3 result;
  198. Job* job = CreateJobFunction(AZStd::bind(Vector3Sum, &vecArray[0], (unsigned int)vecArray.size(), &result), true, m_jobContext);
  199. doneJob.Reset(true);
  200. job->SetDependent(&doneJob);
  201. job->Start();
  202. doneJob.StartAndWaitForCompletion();
  203. AZ_TEST_ASSERT(result.IsClose(Vector3(100.0f, 100.0f, 100.0f)));
  204. }
  205. //test delegate jobs
  206. {
  207. Vector3SumDelegate sumDelegate(&vecArray[0], (unsigned int)vecArray.size());
  208. Job* job = CreateJobFunction(AZStd::make_delegate(&sumDelegate, &Vector3SumDelegate::Process), true, m_jobContext);
  209. doneJob.Reset(true);
  210. job->SetDependent(&doneJob);
  211. job->Start();
  212. doneJob.StartAndWaitForCompletion();
  213. AZ_TEST_ASSERT(sumDelegate.GetResult().IsClose(Vector3(100.0f, 100.0f, 100.0f)));
  214. }
  215. //test generic jobs
  216. {
  217. Vector3 result;
  218. Vector3SumFunctor sumFunctor(&vecArray[0], (unsigned int)vecArray.size(), &result);
  219. Job* job = CreateJobFunction(sumFunctor, true, m_jobContext);
  220. doneJob.Reset(true);
  221. job->SetDependent(&doneJob);
  222. job->Start();
  223. doneJob.StartAndWaitForCompletion();
  224. AZ_TEST_ASSERT(result.IsClose(Vector3(100.0f, 100.0f, 100.0f)));
  225. }
  226. s_totalJobsTime += AZStd::GetTimeNowMicroSecond() - tStart;
  227. }
  228. };
  229. #if AZ_TRAIT_DISABLE_FAILED_JOB_BASIC_TESTS
  230. TEST_F(JobBasicTest, DISABLED_Test)
  231. #else
  232. TEST_F(JobBasicTest, Test)
  233. #endif // AZ_TRAIT_DISABLE_FAILED_JOB_BASIC_TESTS
  234. {
  235. run();
  236. }
  237. // BasicJobExample-End
  238. // FibonacciJobExample-Begin
  239. class FibonacciJobJoin
  240. : public Job
  241. {
  242. public:
  243. AZ_CLASS_ALLOCATOR(FibonacciJobJoin, ThreadPoolAllocator);
  244. FibonacciJobJoin(int* result, JobContext* context = nullptr)
  245. : Job(true, context)
  246. , m_result(result)
  247. {
  248. }
  249. void Process() override
  250. {
  251. *m_result = m_value1 + m_value2;
  252. }
  253. int m_value1;
  254. int m_value2;
  255. int* m_result;
  256. };
  257. class FibonacciJobFork
  258. : public Job
  259. {
  260. public:
  261. AZ_CLASS_ALLOCATOR(FibonacciJobFork, ThreadPoolAllocator);
  262. FibonacciJobFork(int n, int* result, JobContext* context = nullptr)
  263. : Job(true, context)
  264. , m_n(n)
  265. , m_result(result)
  266. {
  267. }
  268. void Process() override
  269. {
  270. AZStd::sys_time_t tStart = AZStd::GetTimeNowMicroSecond();
  271. //this is a spectacularly inefficient way to compute a Fibonacci number, just an example to test the jobs
  272. if (m_n < 2)
  273. {
  274. *m_result = m_n;
  275. }
  276. else
  277. {
  278. FibonacciJobJoin* jobJoin = aznew FibonacciJobJoin(m_result, GetContext());
  279. Job* job1 = aznew FibonacciJobFork(m_n - 1, &jobJoin->m_value1, GetContext());
  280. Job* job2 = aznew FibonacciJobFork(m_n - 2, &jobJoin->m_value2, GetContext());
  281. job1->SetDependent(jobJoin);
  282. job2->SetDependent(jobJoin);
  283. job1->Start();
  284. job2->Start();
  285. SetContinuation(jobJoin);
  286. jobJoin->Start();
  287. }
  288. s_totalJobsTime += AZStd::GetTimeNowMicroSecond() - tStart;
  289. }
  290. private:
  291. int m_n;
  292. int* m_result;
  293. };
  294. class JobFibonacciTest
  295. : public DefaultJobManagerSetupFixture
  296. {
  297. public:
  298. //AZ_CLASS_ALLOCATOR(JobFibonacciTest, ThreadPoolAllocator);
  299. void run()
  300. {
  301. AZStd::sys_time_t tStart = AZStd::GetTimeNowMicroSecond();
  302. int result = 0;
  303. Job* job = aznew FibonacciJobFork(g_fibonacciSlow, &result, m_jobContext);
  304. JobCompletionSpin doneJob(m_jobContext);
  305. job->SetDependent(&doneJob);
  306. job->Start();
  307. doneJob.StartAndWaitForCompletion();
  308. AZ_TEST_ASSERT(result == g_fibonacciSlowResult);
  309. s_totalJobsTime += AZStd::GetTimeNowMicroSecond() - tStart;
  310. }
  311. };
  312. TEST_F(JobFibonacciTest, Test)
  313. {
  314. run();
  315. }
  316. // FibonacciJobExample-End
  317. // FibonacciJob2Example-Begin
  318. class FibonacciJob2
  319. : public Job
  320. {
  321. public:
  322. AZ_CLASS_ALLOCATOR(FibonacciJob2, ThreadPoolAllocator);
  323. FibonacciJob2(int n, int* result, JobContext* context = nullptr)
  324. : Job(true, context)
  325. , m_n(n)
  326. , m_result(result)
  327. {
  328. }
  329. void Process() override
  330. {
  331. AZStd::sys_time_t tStart = AZStd::GetTimeNowMicroSecond();
  332. //this is a spectacularly inefficient way to compute a Fibonacci number, just an example to test the jobs
  333. if (m_n < 2)
  334. {
  335. *m_result = m_n;
  336. }
  337. else
  338. {
  339. int result1 = 0;
  340. int result2 = 0;
  341. Job* job1 = aznew FibonacciJob2(m_n - 1, &result1, m_context);
  342. Job* job2 = aznew FibonacciJob2(m_n - 2, &result2, m_context);
  343. StartAsChild(job1);
  344. StartAsChild(job2);
  345. WaitForChildren();
  346. *m_result = result1 + result2;
  347. }
  348. s_totalJobsTime += AZStd::GetTimeNowMicroSecond() - tStart;
  349. }
  350. private:
  351. int m_n;
  352. int* m_result;
  353. };
  354. class JobFibonacci2Test
  355. : public DefaultJobManagerSetupFixture
  356. {
  357. public:
  358. void run()
  359. {
  360. AZStd::sys_time_t tStart = AZStd::GetTimeNowMicroSecond();
  361. int result = 0;
  362. Job* job = aznew FibonacciJob2(g_fibonacciFast, &result, m_jobContext); //can't go too high here, stack depth can get crazy
  363. JobCompletion doneJob(m_jobContext);
  364. job->SetDependent(&doneJob);
  365. job->Start();
  366. doneJob.StartAndWaitForCompletion();
  367. AZ_TEST_ASSERT(result == g_fibonacciFastResult);
  368. s_totalJobsTime += AZStd::GetTimeNowMicroSecond() - tStart;
  369. }
  370. };
  371. TEST_F(JobFibonacci2Test, Test)
  372. {
  373. run();
  374. }
  375. // FibonacciJob2Example-End
  376. // MergeSortJobExample-Begin
  377. class MergeSortJobJoin
  378. : public Job
  379. {
  380. public:
  381. AZ_CLASS_ALLOCATOR(MergeSortJobJoin, ThreadPoolAllocator);
  382. MergeSortJobJoin(int* array, int* tempArray, int size1, int size2, JobContext* context = nullptr)
  383. : Job(true, context)
  384. , m_array(array)
  385. , m_tempArray(tempArray)
  386. , m_size1(size1)
  387. , m_size2(size2)
  388. {
  389. }
  390. void Process() override
  391. {
  392. //merge
  393. int pos1 = 0;
  394. int pos2 = 0;
  395. int* array1 = &m_array[0];
  396. int* array2 = &m_array[m_size1];
  397. int* tempPtr = m_tempArray;
  398. while ((pos1 < m_size1) && (pos2 < m_size2))
  399. {
  400. if (array1[pos1] < array2[pos2])
  401. {
  402. *tempPtr = array1[pos1++];
  403. }
  404. else
  405. {
  406. *tempPtr = array2[pos2++];
  407. }
  408. ++tempPtr;
  409. }
  410. while (pos1 < m_size1)
  411. {
  412. *tempPtr = array1[pos1++];
  413. ++tempPtr;
  414. }
  415. while (pos2 < m_size2)
  416. {
  417. *tempPtr = array2[pos2++];
  418. ++tempPtr;
  419. }
  420. //copy back to main array, this isn't the most efficient sort, just an example
  421. memcpy(m_array, m_tempArray, (m_size1 + m_size2) * sizeof(int));
  422. }
  423. private:
  424. int* m_array;
  425. int* m_tempArray;
  426. int m_size1;
  427. int m_size2;
  428. };
  429. class MergeSortJobFork
  430. : public Job
  431. {
  432. public:
  433. AZ_CLASS_ALLOCATOR(MergeSortJobFork, ThreadPoolAllocator);
  434. MergeSortJobFork(int* array, int* tempArray, int size, JobContext* context = nullptr)
  435. : Job(true, context)
  436. , m_array(array)
  437. , m_tempArray(tempArray)
  438. , m_size(size)
  439. {
  440. }
  441. void Process() override
  442. {
  443. unsigned int size1 = m_size / 2;
  444. unsigned int size2 = m_size - size1;
  445. int* array1 = &m_array[0];
  446. int* array2 = &m_array[size1];
  447. int* tempArray1 = &m_tempArray[0];
  448. int* tempArray2 = &m_tempArray[size1];
  449. MergeSortJobJoin* jobJoin = aznew MergeSortJobJoin(m_array, m_tempArray, size1, size2, GetContext());
  450. if (size1 > 1)
  451. {
  452. Job* job = aznew MergeSortJobFork(array1, tempArray1, size1, GetContext());
  453. job->SetDependent(jobJoin);
  454. job->Start();
  455. }
  456. if (size2 > 1)
  457. {
  458. Job* job = aznew MergeSortJobFork(array2, tempArray2, size2, GetContext());
  459. job->SetDependent(jobJoin);
  460. job->Start();
  461. }
  462. SetContinuation(jobJoin);
  463. jobJoin->Start();
  464. }
  465. private:
  466. int* m_array;
  467. int* m_tempArray;
  468. int m_size;
  469. };
  470. class JobMergeSortTest
  471. : public DefaultJobManagerSetupFixture
  472. {
  473. public:
  474. void run()
  475. {
  476. #ifdef _DEBUG
  477. const int arraySize = 2000;
  478. #else
  479. const int arraySize = 100000;
  480. #endif
  481. SimpleLcgRandom random;
  482. int* array = reinterpret_cast<int*>(azmalloc(sizeof(int) * arraySize, 4));
  483. int* tempArray = reinterpret_cast<int*>(azmalloc(sizeof(int) * arraySize, 4));
  484. for (int i = 0; i < arraySize; ++i)
  485. {
  486. array[i] = random.GetRandom();
  487. }
  488. AZStd::sys_time_t tStart = AZStd::GetTimeNowMicroSecond();
  489. Job* job = aznew MergeSortJobFork(array, tempArray, arraySize, m_jobContext);
  490. JobCompletion doneJob(m_jobContext);
  491. job->SetDependent(&doneJob);
  492. job->Start();
  493. doneJob.StartAndWaitForCompletion();
  494. s_totalJobsTime += AZStd::GetTimeNowMicroSecond() - tStart;
  495. for (int i = 0; i < arraySize - 1; ++i)
  496. {
  497. AZ_TEST_ASSERT(array[i] <= array[i + 1]);
  498. }
  499. azfree(array);
  500. azfree(tempArray);
  501. }
  502. };
  503. TEST_F(JobMergeSortTest, Test)
  504. {
  505. run();
  506. }
  507. // MergeSortJobExample-End
  508. // QuickSortJobExample-Begin
  509. class QuickSortJob
  510. : public Job
  511. {
  512. public:
  513. AZ_CLASS_ALLOCATOR(QuickSortJob, ThreadPoolAllocator);
  514. QuickSortJob(int* array, int left, int right, JobContext* context = nullptr)
  515. : Job(true, context)
  516. , m_array(array)
  517. , m_left(left)
  518. , m_right(right)
  519. {
  520. }
  521. void Process() override
  522. {
  523. if (m_right <= m_left)
  524. {
  525. return;
  526. }
  527. //partition
  528. int i = m_left - 1;
  529. {
  530. int j = m_right;
  531. int v = m_array[m_right];
  532. for (;; )
  533. {
  534. while (m_array[++i] < v)
  535. {
  536. }
  537. while (v < m_array[--j])
  538. {
  539. if (j == m_left)
  540. {
  541. break;
  542. }
  543. }
  544. if (i >= j)
  545. {
  546. break;
  547. }
  548. AZStd::swap(m_array[i], m_array[j]);
  549. }
  550. AZStd::swap(m_array[i], m_array[m_right]);
  551. }
  552. Job* job1 = aznew QuickSortJob(m_array, m_left, i - 1, GetContext());
  553. Job* job2 = aznew QuickSortJob(m_array, i + 1, m_right, GetContext());
  554. SetContinuation(job1);
  555. SetContinuation(job2);
  556. job1->Start();
  557. job2->Start();
  558. }
  559. private:
  560. int* m_array;
  561. int m_left;
  562. int m_right;
  563. };
  564. class JobQuickSortTest
  565. : public DefaultJobManagerSetupFixture
  566. {
  567. public:
  568. void run()
  569. {
  570. #ifdef _DEBUG
  571. const int arraySize = 2000;
  572. #else
  573. const int arraySize = 100000;
  574. #endif
  575. SimpleLcgRandom random;
  576. int* array = reinterpret_cast<int*>(azmalloc(sizeof(int) * arraySize, 4));
  577. for (int i = 0; i < arraySize; ++i)
  578. {
  579. array[i] = random.GetRandom();
  580. }
  581. AZStd::sys_time_t tStart = AZStd::GetTimeNowMicroSecond();
  582. Job* job = aznew QuickSortJob(array, 0, arraySize - 1, m_jobContext);
  583. JobCompletion doneJob(m_jobContext);
  584. job->SetDependent(&doneJob);
  585. job->Start();
  586. doneJob.StartAndWaitForCompletion();
  587. s_totalJobsTime += AZStd::GetTimeNowMicroSecond() - tStart;
  588. for (int i = 0; i < arraySize - 1; ++i)
  589. {
  590. AZ_TEST_ASSERT(array[i] <= array[i + 1]);
  591. }
  592. azfree(array);
  593. }
  594. };
  595. TEST_F(JobQuickSortTest, Test)
  596. {
  597. run();
  598. }
  599. // QuickSortJobExample-End
  600. class JobCancellationTest
  601. : public DefaultJobManagerSetupFixture
  602. {
  603. public:
  604. void SetUp() override
  605. {
  606. DefaultJobManagerSetupFixture::SetUp();
  607. m_cancelGroup1 = aznew JobCancelGroup();
  608. m_cancelGroup2 = aznew JobCancelGroup(m_cancelGroup1);
  609. m_cancelGroup3 = aznew JobCancelGroup(m_cancelGroup2);
  610. m_context1 = aznew JobContext(m_jobContext->GetJobManager(), *m_cancelGroup1);
  611. m_context2 = aznew JobContext(m_jobContext->GetJobManager(), *m_cancelGroup2);
  612. m_context3 = aznew JobContext(m_jobContext->GetJobManager(), *m_cancelGroup3);
  613. m_value = 0;
  614. }
  615. void TearDown() override
  616. {
  617. delete m_context3;
  618. delete m_context2;
  619. delete m_context1;
  620. delete m_cancelGroup3;
  621. delete m_cancelGroup2;
  622. delete m_cancelGroup1;
  623. DefaultJobManagerSetupFixture::TearDown();
  624. }
  625. void run()
  626. {
  627. AZStd::sys_time_t tStart = AZStd::GetTimeNowMicroSecond();
  628. JobCompletion completion(m_jobContext);
  629. {
  630. m_value = 0;
  631. completion.Reset(true);
  632. StartJobs(&completion);
  633. completion.StartAndWaitForCompletion();
  634. AZ_TEST_ASSERT(m_value == 111);
  635. }
  636. {
  637. m_value = 0;
  638. completion.Reset(true);
  639. m_cancelGroup3->Cancel(); //cancel before starting jobs, so test is deterministic
  640. StartJobs(&completion);
  641. completion.StartAndWaitForCompletion();
  642. m_cancelGroup3->Reset();
  643. AZ_TEST_ASSERT(m_value == 110);
  644. }
  645. {
  646. m_value = 0;
  647. completion.Reset(true);
  648. m_cancelGroup2->Cancel(); //cancel before starting jobs, so test is deterministic
  649. StartJobs(&completion);
  650. completion.StartAndWaitForCompletion();
  651. m_cancelGroup2->Reset();
  652. AZ_TEST_ASSERT(m_value == 100);
  653. }
  654. {
  655. m_value = 0;
  656. completion.Reset(true);
  657. m_cancelGroup1->Cancel(); //cancel before starting jobs, so test is deterministic
  658. StartJobs(&completion);
  659. completion.StartAndWaitForCompletion();
  660. m_cancelGroup1->Reset();
  661. AZ_TEST_ASSERT(m_value == 0);
  662. }
  663. s_totalJobsTime += AZStd::GetTimeNowMicroSecond() - tStart;
  664. }
  665. void StartJobs(Job* dependent)
  666. {
  667. {
  668. Job* job = CreateJobFunction(AZStd::bind(&JobCancellationTest::Add, this, 100), true, m_context1);
  669. job->SetDependent(dependent);
  670. job->Start();
  671. }
  672. {
  673. Job* job = CreateJobFunction(AZStd::bind(&JobCancellationTest::Add, this, 10), true, m_context2);
  674. job->SetDependent(dependent);
  675. job->Start();
  676. }
  677. {
  678. Job* job = CreateJobFunction(AZStd::bind(&JobCancellationTest::Add, this, 1), true, m_context3);
  679. job->SetDependent(dependent);
  680. job->Start();
  681. }
  682. }
  683. void Add(int x)
  684. {
  685. m_value += x;
  686. }
  687. private:
  688. JobCancelGroup* m_cancelGroup1;
  689. JobCancelGroup* m_cancelGroup2;
  690. JobCancelGroup* m_cancelGroup3;
  691. JobContext* m_context1;
  692. JobContext* m_context2;
  693. JobContext* m_context3;
  694. AZStd::atomic<int> m_value;
  695. };
  696. TEST_F(JobCancellationTest, Test)
  697. {
  698. run();
  699. }
  700. class JobAssistTest
  701. : public DefaultJobManagerSetupFixture
  702. {
  703. public:
  704. void run()
  705. {
  706. AZStd::sys_time_t tStart = AZStd::GetTimeNowMicroSecond();
  707. int result = 0;
  708. Job* job = aznew FibonacciJobFork(g_fibonacciSlow, &result, m_jobContext);
  709. job->StartAndAssistUntilComplete();
  710. AZ_TEST_ASSERT(result == g_fibonacciSlowResult);
  711. s_totalJobsTime += AZStd::GetTimeNowMicroSecond() - tStart;
  712. }
  713. };
  714. TEST_F(JobAssistTest, Test)
  715. {
  716. run();
  717. }
  718. // TaskGroupJobExample-Begin
  719. class JobTaskGroupTest
  720. : public DefaultJobManagerSetupFixture
  721. {
  722. public:
  723. void run()
  724. {
  725. AZStd::sys_time_t tStart = AZStd::GetTimeNowMicroSecond();
  726. int result;
  727. CalcFibonacci(g_fibonacciFast, &result);
  728. AZ_TEST_ASSERT(result == g_fibonacciFastResult);
  729. structured_task_group group(m_jobContext);
  730. group.run(&JobTaskGroupTest::TestFunc);
  731. group.wait();
  732. s_totalJobsTime += AZStd::GetTimeNowMicroSecond() - tStart;
  733. }
  734. static void TestFunc()
  735. {
  736. }
  737. void CalcFibonacci(int n, int* result)
  738. {
  739. //this is a spectacularly inefficient way to compute a Fibonacci number, just an example to test the jobs
  740. if (n < 2)
  741. {
  742. *result = n;
  743. }
  744. else
  745. {
  746. int result1, result2;
  747. structured_task_group group(m_jobContext);
  748. group.run(AZStd::bind(&JobTaskGroupTest::CalcFibonacci, this, n - 1, &result1));
  749. group.run(AZStd::bind(&JobTaskGroupTest::CalcFibonacci, this, n - 2, &result2));
  750. group.wait();
  751. *result = result1 + result2;
  752. }
  753. }
  754. };
  755. TEST_F(JobTaskGroupTest, Test)
  756. {
  757. run();
  758. }
  759. // TaskGroupJobExample-End
  760. class JobGlobalContextTest
  761. : public DefaultJobManagerSetupFixture
  762. {
  763. public:
  764. void run()
  765. {
  766. AZStd::sys_time_t tStart = AZStd::GetTimeNowMicroSecond();
  767. int result;
  768. CalcFibonacci(g_fibonacciFast, &result);
  769. AZ_TEST_ASSERT(result == g_fibonacciFastResult);
  770. s_totalJobsTime += AZStd::GetTimeNowMicroSecond() - tStart;
  771. }
  772. void CalcFibonacci(int n, int* result)
  773. {
  774. //this is a spectacularly inefficient way to compute a Fibonacci number, just an example to test the jobs
  775. if (n < 2)
  776. {
  777. *result = n;
  778. }
  779. else
  780. {
  781. int result1, result2;
  782. structured_task_group group;
  783. group.run(AZStd::bind(&JobGlobalContextTest::CalcFibonacci, this, n - 1, &result1));
  784. group.run(AZStd::bind(&JobGlobalContextTest::CalcFibonacci, this, n - 2, &result2));
  785. group.wait();
  786. *result = result1 + result2;
  787. }
  788. }
  789. };
  790. TEST_F(JobGlobalContextTest, Test)
  791. {
  792. run();
  793. }
  794. class JobParallelInvokeTest
  795. : public DefaultJobManagerSetupFixture
  796. {
  797. public:
  798. void run()
  799. {
  800. AZStd::sys_time_t tStart = AZStd::GetTimeNowMicroSecond();
  801. int result1, result2, result3;
  802. parallel_invoke(
  803. AZStd::bind(&JobParallelInvokeTest::FibTask, this, g_fibonacciSlow, &result1),
  804. AZStd::bind(&JobParallelInvokeTest::FibTask, this, g_fibonacciFast, &result2), m_jobContext);
  805. AZ_TEST_ASSERT(result1 == g_fibonacciSlowResult);
  806. AZ_TEST_ASSERT(result2 == g_fibonacciFastResult);
  807. parallel_invoke(
  808. AZStd::bind(&JobParallelInvokeTest::FibTask, this, g_fibonacciFast, &result1),
  809. AZStd::bind(&JobParallelInvokeTest::FibTask, this, g_fibonacciSlow, &result2),
  810. AZStd::bind(&JobParallelInvokeTest::FibTask, this, g_fibonacciSlow, &result3), m_jobContext);
  811. AZ_TEST_ASSERT(result1 == g_fibonacciFastResult);
  812. AZ_TEST_ASSERT(result2 == g_fibonacciSlowResult);
  813. AZ_TEST_ASSERT(result3 == g_fibonacciSlowResult);
  814. s_totalJobsTime += AZStd::GetTimeNowMicroSecond() - tStart;
  815. }
  816. void FibTask(int n, int* result)
  817. {
  818. *result = CalcFibonacci(n);
  819. }
  820. int CalcFibonacci(int n)
  821. {
  822. if (n < 2)
  823. {
  824. return n;
  825. }
  826. return CalcFibonacci(n - 1) + CalcFibonacci(n - 2);
  827. }
  828. };
  829. TEST_F(JobParallelInvokeTest, Test)
  830. {
  831. run();
  832. }
  833. class JobParallelForTest
  834. : public DefaultJobManagerSetupFixture
  835. {
  836. public:
  837. void TearDown() override
  838. {
  839. m_results.set_capacity(0);
  840. DefaultJobManagerSetupFixture::TearDown();
  841. }
  842. void run()
  843. {
  844. const int maxFibonacci = 30;
  845. AZStd::sys_time_t tStart = AZStd::GetTimeNowMicroSecond();
  846. //just a few iterations
  847. {
  848. m_results.resize(maxFibonacci);
  849. parallel_for(0, maxFibonacci, AZStd::bind(&JobParallelForTest::FibTask, this, AZStd::placeholders::_1, maxFibonacci));
  850. AZ_TEST_ASSERT(m_results[0] == 0);
  851. AZ_TEST_ASSERT(m_results[1] == 1);
  852. for (int i = 2; i < maxFibonacci; ++i)
  853. {
  854. AZ_TEST_ASSERT(m_results[i] == (m_results[i - 1] + m_results[i - 2]));
  855. }
  856. }
  857. //lots and lots of iterations
  858. {
  859. const int numSets = 500;
  860. const int numIterations = numSets * maxFibonacci;
  861. m_results.resize(numIterations);
  862. parallel_for(0, numIterations, AZStd::bind(&JobParallelForTest::FibTask, this, AZStd::placeholders::_1, maxFibonacci));
  863. for (int setIndex = 0; setIndex < numSets; ++setIndex)
  864. {
  865. int offset = setIndex * maxFibonacci;
  866. AZ_TEST_ASSERT(m_results[offset + 0] == 0);
  867. AZ_TEST_ASSERT(m_results[offset + 1] == 1);
  868. for (int i = 2; i < maxFibonacci; ++i)
  869. {
  870. AZ_TEST_ASSERT(m_results[offset + i] == (m_results[offset + i - 1] + m_results[offset + i - 2]));
  871. }
  872. }
  873. }
  874. //step size
  875. {
  876. const int numIterations = 100;
  877. const int step = 3;
  878. m_results.resize(numIterations * step);
  879. parallel_for(0, numIterations * step, step, AZStd::bind(&JobParallelForTest::Double, this, AZStd::placeholders::_1));
  880. for (int i = 0; i < numIterations * step; i += step)
  881. {
  882. AZ_TEST_ASSERT(m_results[i] == i * 2);
  883. }
  884. }
  885. //start only
  886. {
  887. const int numIterations = 100;
  888. m_results.resize(numIterations);
  889. JobCompletion doneJob;
  890. parallel_for_start(0, numIterations, AZStd::bind(&JobParallelForTest::Double, this, AZStd::placeholders::_1), &doneJob);
  891. doneJob.StartAndWaitForCompletion();
  892. for (int i = 0; i < numIterations; ++i)
  893. {
  894. AZ_TEST_ASSERT(m_results[i] == i * 2);
  895. }
  896. }
  897. s_totalJobsTime += AZStd::GetTimeNowMicroSecond() - tStart;
  898. }
  899. void FibTask(int i, int maxFibonacci)
  900. {
  901. m_results[i] = CalcFibonacci(i % maxFibonacci);
  902. }
  903. int CalcFibonacci(int n)
  904. {
  905. if (n < 2)
  906. {
  907. return n;
  908. }
  909. return CalcFibonacci(n - 1) + CalcFibonacci(n - 2);
  910. }
  911. void Double(int i)
  912. {
  913. m_results[i] = i * 2;
  914. }
  915. private:
  916. AZStd::vector<int> m_results;
  917. };
  918. TEST_F(JobParallelForTest, Test)
  919. {
  920. run();
  921. }
  922. class JobParallelForEachTest
  923. : public DefaultJobManagerSetupFixture
  924. {
  925. public:
  926. void run()
  927. {
  928. AZStd::sys_time_t tStart = AZStd::GetTimeNowMicroSecond();
  929. //random access iterator
  930. {
  931. const int numValues = 1000;
  932. AZStd::fixed_vector<int, 1000> values;
  933. for (int i = 0; i < numValues; ++i)
  934. {
  935. values.push_back(i);
  936. }
  937. parallel_for_each(values.begin(), values.end(), AZStd::bind(&Double, AZStd::placeholders::_1));
  938. for (int i = 0; i < numValues; ++i)
  939. {
  940. AZ_TEST_ASSERT(values[i] == 2 * i);
  941. }
  942. }
  943. //forward iterator
  944. {
  945. const int numValues = 1000;
  946. AZStd::fixed_list<int, numValues> values;
  947. for (int i = 0; i < numValues; ++i)
  948. {
  949. values.push_back(i);
  950. }
  951. parallel_for_each(values.begin(), values.end(), AZStd::bind(&Double, AZStd::placeholders::_1));
  952. int i = 0;
  953. for (AZStd::fixed_list<int, numValues>::const_iterator iter = values.begin(); iter != values.end(); ++iter)
  954. {
  955. AZ_TEST_ASSERT(*iter == 2 * i);
  956. ++i;
  957. }
  958. }
  959. //start only
  960. {
  961. const int numValues = 1000;
  962. AZStd::fixed_vector<int, numValues> values;
  963. for (int i = 0; i < numValues; ++i)
  964. {
  965. values.push_back(i);
  966. }
  967. JobCompletion doneJob;
  968. parallel_for_each_start(values.begin(), values.end(), &JobParallelForEachTest::Double, &doneJob);
  969. doneJob.StartAndWaitForCompletion();
  970. for (int i = 0; i < numValues; ++i)
  971. {
  972. AZ_TEST_ASSERT(values[i] == 2 * i);
  973. }
  974. }
  975. s_totalJobsTime += AZStd::GetTimeNowMicroSecond() - tStart;
  976. }
  977. static void Double(int& i)
  978. {
  979. i *= 2;
  980. }
  981. private:
  982. AZStd::vector<int> m_results;
  983. };
  984. TEST_F(JobParallelForEachTest, Test)
  985. {
  986. run();
  987. }
  988. class PERF_JobParallelForOverheadTest
  989. : public DefaultJobManagerSetupFixture
  990. {
  991. public:
  992. static const size_t numElementsScale = 1;
  993. #ifdef _DEBUG
  994. static const size_t m_numElements = 10000 / numElementsScale;
  995. #else
  996. static const size_t m_numElements = 100000 / numElementsScale;
  997. #endif
  998. PERF_JobParallelForOverheadTest()
  999. : DefaultJobManagerSetupFixture()
  1000. {}
  1001. void TearDown() override
  1002. {
  1003. m_vectors.set_capacity(0);
  1004. m_vectors1.set_capacity(0);
  1005. m_results.set_capacity(0);
  1006. m_transforms.set_capacity(0);
  1007. m_callOrder.clear();
  1008. DefaultJobManagerSetupFixture::TearDown();
  1009. }
  1010. void run()
  1011. {
  1012. m_vectors.reserve(m_numElements);
  1013. m_vectors1.reserve(m_numElements);
  1014. //m_callOrder.resize(m_numElements);
  1015. m_results.reserve(m_numElements);
  1016. m_transforms.reserve(m_numElements);
  1017. for (size_t i = 0; i < m_numElements; ++i)
  1018. {
  1019. m_vectors.push_back(Vector3::CreateOne());
  1020. m_vectors1.push_back(Vector3::CreateOne());
  1021. m_results.push_back(Vector3::CreateZero());
  1022. m_transforms.push_back(Transform::CreateRotationX(static_cast<float>(i) / 3.0f));
  1023. }
  1024. AZStd::sys_time_t tStart = 0;
  1025. AZStd::sys_time_t nonParallelMS = 0, parallelForMS = 0, parallelForPPLMS = 0;
  1026. AZStd::sys_time_t nonParallelProcessMS = 0, parallelForProcessMS = 0, parallelForProcessPPLMS = 0;
  1027. static const int numOfArrayIterations = /*5*/ 1;
  1028. // non parallel test
  1029. m_processElementsTime = 0;
  1030. tStart = AZStd::GetTimeNowMicroSecond();
  1031. for (int i = 0; i < numOfArrayIterations; ++i)
  1032. {
  1033. for (size_t j = 0; j < m_numElements; ++j)
  1034. {
  1035. ProcessElement(j);
  1036. }
  1037. }
  1038. nonParallelMS = AZStd::GetTimeNowMicroSecond() - tStart;
  1039. nonParallelProcessMS = m_processElementsTime;
  1040. // parallel_for test
  1041. {
  1042. #ifdef AZ_JOBS_PRINT_CALL_ORDER
  1043. m_callOrder.clear();
  1044. #endif //#ifdef AZ_JOBS_PRINT_CALL_ORDER
  1045. m_processElementsTime = 0;
  1046. tStart = AZStd::GetTimeNowMicroSecond();
  1047. for (int i = 0; i < numOfArrayIterations; ++i)
  1048. {
  1049. parallel_for(static_cast<size_t>(0), m_numElements, AZStd::bind(&PERF_JobParallelForOverheadTest::ProcessElement, this, AZStd::placeholders::_1) /*,static_partitioner()*/);
  1050. }
  1051. parallelForMS = AZStd::GetTimeNowMicroSecond() - tStart;
  1052. parallelForProcessMS = m_processElementsTime;
  1053. }
  1054. #if defined(AZ_COMPARE_TO_PPL)
  1055. // compare to MS Concurrency::parallel_for
  1056. {
  1057. # ifdef AZ_JOBS_PRINT_CALL_ORDER
  1058. m_callOrder.clear();
  1059. # endif // #ifdef AZ_JOBS_PRINT_CALL_ORDER
  1060. m_processElementsTime = 0;
  1061. tStart = AZStd::GetTimeNowMicroSecond();
  1062. //Concurrency::auto_partitioner part;
  1063. for (int i = 0; i < numOfArrayIterations; ++i)
  1064. {
  1065. Concurrency::parallel_for(static_cast<size_t>(0), m_numElements, AZStd::bind(&PERF_JobParallelForOverheadTest::ProcessElement, this, AZStd::placeholders::_1) /*,part*/);
  1066. }
  1067. parallelForPPLMS = AZStd::GetTimeNowMicroSecond() - tStart;
  1068. parallelForProcessPPLMS = m_processElementsTime;
  1069. }
  1070. #endif // AZ_COMPARE_TO_PPL
  1071. AZ_Printf("UnitTest", "\n\nJob overhead test. Serial %lld (%lld) Parallel %lld (%lld) PPL %lld (%lld) Total: %lld\n\n", nonParallelMS, nonParallelProcessMS, parallelForMS, parallelForProcessMS, parallelForPPLMS, parallelForProcessPPLMS, s_totalJobsTime);
  1072. #ifdef AZ_JOBS_PRINT_CALL_ORDER
  1073. // Find all unique threads
  1074. typedef AZStd::unordered_set<AZStd::native_thread_id_type> ThreadSetType;
  1075. ThreadSetType threads;
  1076. for (unsigned int i = 0; i < m_callOrder.size(); ++i)
  1077. {
  1078. threads.insert(m_callOrder[i].second);
  1079. }
  1080. // print order by thread
  1081. unsigned int totalProcessedElements = 0;
  1082. printf("Elements processed by %d threads:\n", threads.size());
  1083. for (ThreadSetType::iterator it = threads.begin(); it != threads.end(); ++it)
  1084. {
  1085. unsigned int elementsProcessed = 0;
  1086. AZStd::native_thread_id_type threadId = *it;
  1087. printf("Thread %d!\n", threadId);
  1088. for (unsigned int i = 0; i < m_callOrder.size(); ++i)
  1089. {
  1090. if (m_callOrder[i].second == threadId)
  1091. {
  1092. if (elementsProcessed % 10 == 0)
  1093. {
  1094. printf("%d\n", m_callOrder[i].first);
  1095. }
  1096. else
  1097. {
  1098. printf("%d,", m_callOrder[i].first);
  1099. }
  1100. elementsProcessed++;
  1101. }
  1102. }
  1103. totalProcessedElements += elementsProcessed;
  1104. printf("\nTotal Elements for thread %d are %d\n\n\n\n\n", threadId, elementsProcessed);
  1105. }
  1106. printf("\nTotal Elements %d\n", totalProcessedElements);
  1107. m_jobManager->PrintStats();
  1108. #endif
  1109. }
  1110. void ProcessElement(size_t index)
  1111. {
  1112. int numIterations = 50;
  1113. if (index > m_numElements * 7 / 8 || index < m_numElements / 8) // simulate asymmetrical load
  1114. {
  1115. numIterations = 100;
  1116. }
  1117. //int numIterations = m_random.GetRandom() % 100;
  1118. #ifdef AZ_JOBS_PRINT_CALL_ORDER
  1119. m_callOrder.push_back(AZStd::make_pair(index, AZStd::this_thread::get_id().m_id));
  1120. #endif
  1121. for (int i = 0; i < numIterations; ++i)
  1122. {
  1123. Transform tm = m_transforms[index].GetInverse();
  1124. Transform tm1 = tm.GetInverse();
  1125. tm = tm1 * tm;
  1126. Vector3 v = m_vectors[index] * m_vectors1[index].GetLength();
  1127. m_results[index] = tm.TransformVector(v);
  1128. }
  1129. }
  1130. private:
  1131. AZStd::vector<Vector3> m_vectors;
  1132. AZStd::vector<Vector3> m_vectors1;
  1133. AZStd::concurrent_vector<AZStd::pair<size_t, AZStd::native_thread_id_type> > m_callOrder;
  1134. AZStd::vector<Transform> m_transforms;
  1135. AZStd::vector<Vector3> m_results;
  1136. AZStd::sys_time_t m_processElementsTime;
  1137. SimpleLcgRandom m_random;
  1138. };
  1139. #ifdef ENABLE_PERFORMANCE_TEST
  1140. TEST_F(PERF_JobParallelForOverheadTest, Test)
  1141. {
  1142. run();
  1143. }
  1144. #endif
  1145. class JobFunctionTestWithoutCurrentJobArg
  1146. : public DefaultJobManagerSetupFixture
  1147. {
  1148. public:
  1149. void run()
  1150. {
  1151. constexpr size_t JobCount = 32;
  1152. size_t jobData[JobCount] = { 0 };
  1153. AZ::JobCompletion completion;
  1154. for (size_t i = 0; i < JobCount; ++i)
  1155. {
  1156. AZ::Job* job = AZ::CreateJobFunction([i, &jobData]() // NOT passing the current job as an argument
  1157. {
  1158. jobData[i] = i + 1;
  1159. },
  1160. true
  1161. );
  1162. job->SetDependent(&completion);
  1163. job->Start();
  1164. }
  1165. completion.StartAndWaitForCompletion();
  1166. for (size_t i = 0; i < JobCount; ++i)
  1167. {
  1168. EXPECT_EQ(jobData[i], i + 1);
  1169. }
  1170. }
  1171. };
  1172. TEST_F(JobFunctionTestWithoutCurrentJobArg, Test)
  1173. {
  1174. run();
  1175. }
  1176. class JobFunctionTestWithCurrentJobArg
  1177. : public DefaultJobManagerSetupFixture
  1178. {
  1179. public:
  1180. void run()
  1181. {
  1182. constexpr size_t JobCount = 32;
  1183. size_t jobData[JobCount] = { 0 };
  1184. AZ::JobCompletion completion;
  1185. // Push a parent job that pushes the work as child jobs (requires the current job, so this is a real world test of "functor with current job as param")
  1186. AZ::Job* parentJob = AZ::CreateJobFunction([this, &jobData](AZ::Job& thisJob)
  1187. {
  1188. EXPECT_EQ(m_jobManager->GetCurrentJob(), &thisJob);
  1189. for (size_t i = 0; i < JobCount; ++i)
  1190. {
  1191. AZ::Job* childJob = AZ::CreateJobFunction([this, i, &jobData](AZ::Job& thisJob)
  1192. {
  1193. EXPECT_EQ(m_jobManager->GetCurrentJob(), &thisJob);
  1194. jobData[i] = i + 1;
  1195. },
  1196. true
  1197. );
  1198. thisJob.StartAsChild(childJob);
  1199. }
  1200. thisJob.WaitForChildren(); // Note: this is required before the parent job returns
  1201. },
  1202. true
  1203. );
  1204. parentJob->SetDependent(&completion);
  1205. parentJob->Start();
  1206. completion.StartAndWaitForCompletion();
  1207. for (size_t i = 0; i < JobCount; ++i)
  1208. {
  1209. EXPECT_EQ(jobData[i], i + 1);
  1210. }
  1211. }
  1212. };
  1213. TEST_F(JobFunctionTestWithCurrentJobArg, Test)
  1214. {
  1215. run();
  1216. }
  1217. class JobCompletionCompleteNotScheduled
  1218. : public DefaultJobManagerSetupFixture
  1219. {
  1220. public:
  1221. JobCompletionCompleteNotScheduled()
  1222. : DefaultJobManagerSetupFixture(1) // Only 1 worker to serialize job execution
  1223. {
  1224. }
  1225. void run()
  1226. {
  1227. AZStd::atomic_int32_t testSequence{0};
  1228. JobCompletion jobCompletion;
  1229. Job* firstJob = CreateJobFunction([&testSequence]
  1230. {
  1231. ++testSequence; // 0 => 1
  1232. },
  1233. true
  1234. );
  1235. firstJob->SetDependent(&jobCompletion);
  1236. firstJob->Start();
  1237. AZStd::binary_semaphore testThreadSemaphore;
  1238. AZStd::binary_semaphore jobSemaphore;
  1239. JobCompletion secondJobCompletion;
  1240. Job* secondJob = CreateJobFunction([&testSequence, &testThreadSemaphore, &jobSemaphore]
  1241. {
  1242. testThreadSemaphore.release();
  1243. jobSemaphore.acquire();
  1244. ++testSequence; // 1 => 2
  1245. },
  1246. true
  1247. );
  1248. secondJob->SetDependent(&secondJobCompletion);
  1249. secondJob->Start();
  1250. // guarantee the parkedJob has started, with only 1 job thread this means firstJob must be complete
  1251. testThreadSemaphore.acquire();
  1252. // If waiting on completion is unscheduled and executes immediately, then the value of sequence will be 1
  1253. jobCompletion.StartAndWaitForCompletion();
  1254. EXPECT_EQ(testSequence, 1);
  1255. // Allow the second job to finish
  1256. jobSemaphore.release();
  1257. // Safety sync before exiting this scope
  1258. secondJobCompletion.StartAndWaitForCompletion();
  1259. EXPECT_EQ(testSequence, 2);
  1260. }
  1261. };
  1262. TEST_F(JobCompletionCompleteNotScheduled, Test)
  1263. {
  1264. run();
  1265. }
  1266. class TestJobWithPriority : public Job
  1267. {
  1268. public:
  1269. static AZStd::atomic<AZ::s32> s_numIncompleteJobs;
  1270. AZ_CLASS_ALLOCATOR(TestJobWithPriority, ThreadPoolAllocator);
  1271. TestJobWithPriority(AZ::s8 priority, const char* name, JobContext* context, AZStd::binary_semaphore& binarySemaphore, AZStd::vector<AZStd::string>& namesOfProcessedJobs)
  1272. : Job(true, context, false, priority)
  1273. , m_name(name)
  1274. , m_binarySemaphore(binarySemaphore)
  1275. , m_namesOfProcessedJobs(namesOfProcessedJobs)
  1276. {
  1277. ++s_numIncompleteJobs;
  1278. }
  1279. void Process() override
  1280. {
  1281. // Ensure the job does not complete until it is able to acquire the semaphore,
  1282. // then add its name to the vector of processed jobs.
  1283. m_binarySemaphore.acquire();
  1284. m_namesOfProcessedJobs.push_back(m_name);
  1285. m_binarySemaphore.release();
  1286. --s_numIncompleteJobs;
  1287. }
  1288. private:
  1289. const AZStd::string m_name;
  1290. AZStd::binary_semaphore& m_binarySemaphore;
  1291. AZStd::vector<AZStd::string>& m_namesOfProcessedJobs;
  1292. };
  1293. AZStd::atomic<AZ::s32> TestJobWithPriority::s_numIncompleteJobs = 0;
  1294. class JobPriorityTestFixture : public DefaultJobManagerSetupFixture
  1295. {
  1296. public:
  1297. JobPriorityTestFixture() : DefaultJobManagerSetupFixture(1) // Only 1 worker to serialize job execution
  1298. {
  1299. }
  1300. void RunTest()
  1301. {
  1302. AZStd::vector<AZStd::string> namesOfProcessedJobs;
  1303. // The queue is empty, and calling 'Start' on a job inserts it into the job queue, but it won't necesarily begin processing immediately.
  1304. // So we need to set the priority of the first job queued to the highest available, ensuring it will get processed first even if the lone
  1305. // worker thread active in this test does not inspect the queue until after all the jobs below have been 'started' (this is guaranteed by
  1306. // the fact that jobs with equal priority values are processed in FIFO order).
  1307. //
  1308. // The binary semaphore ensures that this first job does not complete until we've finished queuing all the other jobs.
  1309. //
  1310. // All jobs that we start in this test have the 'isAutoDelete' param set to true, so we don't have to store them or clean up.
  1311. AZStd::binary_semaphore binarySemaphore;
  1312. (aznew TestJobWithPriority(127, "FirstJobQueued", m_jobContext, binarySemaphore, namesOfProcessedJobs))->Start();
  1313. // Queue a number of other jobs before releasing the semaphore that will allow "FirstJobQueued" to complete.
  1314. // These additional jobs should be processed in order of their priority, or where the priority is equal in the order they were queued.
  1315. (aznew TestJobWithPriority(-128, "LowestPriority", m_jobContext, binarySemaphore, namesOfProcessedJobs))->Start();
  1316. (aznew TestJobWithPriority(-1, "LowPriority1", m_jobContext, binarySemaphore, namesOfProcessedJobs))->Start();
  1317. (aznew TestJobWithPriority(-1, "LowPriority2", m_jobContext, binarySemaphore, namesOfProcessedJobs))->Start();
  1318. (aznew TestJobWithPriority(-1, "LowPriority3", m_jobContext, binarySemaphore, namesOfProcessedJobs))->Start();
  1319. (aznew TestJobWithPriority(0, "DefaultPriority1", m_jobContext, binarySemaphore, namesOfProcessedJobs))->Start();
  1320. (aznew TestJobWithPriority(0, "DefaultPriority2", m_jobContext, binarySemaphore, namesOfProcessedJobs))->Start();
  1321. (aznew TestJobWithPriority(0, "DefaultPriority3", m_jobContext, binarySemaphore, namesOfProcessedJobs))->Start();
  1322. (aznew TestJobWithPriority(1, "HighPriority1", m_jobContext, binarySemaphore, namesOfProcessedJobs))->Start();
  1323. (aznew TestJobWithPriority(1, "HighPriority2", m_jobContext, binarySemaphore, namesOfProcessedJobs))->Start();
  1324. (aznew TestJobWithPriority(1, "HighPriority3", m_jobContext, binarySemaphore, namesOfProcessedJobs))->Start();
  1325. (aznew TestJobWithPriority(127, "HighestPriority", m_jobContext, binarySemaphore, namesOfProcessedJobs))->Start();
  1326. // Release the binary semaphore so the first queued job will complete. The rest of the queued jobs should now complete in order of their priority.
  1327. binarySemaphore.release();
  1328. // Wait until all the jobs have completed. Ideally we would start one last job (with the lowest priority so it is guaranteed to be processed last
  1329. // even if the jobs started above have yet to complete) and wait until it has completed, but this results in this main thread picking up jobs and
  1330. // thorwing all the careful scheduling on the one worker thread active in this test out the window (please see Job::StartAndAssistUntilComplete).
  1331. while (TestJobWithPriority::s_numIncompleteJobs > 0) {}
  1332. // Verify that the jobs were completed in the expected order.
  1333. EXPECT_EQ(namesOfProcessedJobs[0], "FirstJobQueued");
  1334. EXPECT_EQ(namesOfProcessedJobs[1], "HighestPriority");
  1335. EXPECT_EQ(namesOfProcessedJobs[2], "HighPriority1");
  1336. EXPECT_EQ(namesOfProcessedJobs[3], "HighPriority2");
  1337. EXPECT_EQ(namesOfProcessedJobs[4], "HighPriority3");
  1338. EXPECT_EQ(namesOfProcessedJobs[5], "DefaultPriority1");
  1339. EXPECT_EQ(namesOfProcessedJobs[6], "DefaultPriority2");
  1340. EXPECT_EQ(namesOfProcessedJobs[7], "DefaultPriority3");
  1341. EXPECT_EQ(namesOfProcessedJobs[8], "LowPriority1");
  1342. EXPECT_EQ(namesOfProcessedJobs[9], "LowPriority2");
  1343. EXPECT_EQ(namesOfProcessedJobs[10], "LowPriority3");
  1344. EXPECT_EQ(namesOfProcessedJobs[11], "LowestPriority");
  1345. }
  1346. };
  1347. TEST_F(JobPriorityTestFixture, Test)
  1348. {
  1349. RunTest();
  1350. }
  1351. } // UnitTest
  1352. #if defined(HAVE_BENCHMARK)
  1353. namespace Benchmark
  1354. {
  1355. double CalculatePi(AZ::u32 depth)
  1356. {
  1357. double pi = 0.0;
  1358. for (AZ::u32 i = 0; i < depth; ++i)
  1359. {
  1360. const double numerator = static_cast<double>(((i % 2) * 2) - 1);
  1361. const double denominator = static_cast<double>((2 * i) - 1);
  1362. pi += numerator / denominator;
  1363. }
  1364. return (pi - 1.0) * 4;
  1365. }
  1366. class TestJobCalculatePi : public Job
  1367. {
  1368. public:
  1369. static AZStd::atomic<AZ::s32> s_numIncompleteJobs;
  1370. AZ_CLASS_ALLOCATOR(TestJobCalculatePi, ThreadPoolAllocator);
  1371. TestJobCalculatePi(AZ::u32 depth, AZ::s8 priority, JobContext* context)
  1372. : Job(true, context, false, priority)
  1373. , m_depth(depth)
  1374. {
  1375. ++s_numIncompleteJobs;
  1376. }
  1377. void Process() override
  1378. {
  1379. benchmark::DoNotOptimize(CalculatePi(m_depth));
  1380. --s_numIncompleteJobs;
  1381. }
  1382. private:
  1383. const AZ::u32 m_depth;
  1384. };
  1385. AZStd::atomic<AZ::s32> TestJobCalculatePi::s_numIncompleteJobs = 0;
  1386. class JobBenchmarkFixture : public ::benchmark::Fixture
  1387. {
  1388. public:
  1389. static const AZ::s32 LIGHT_WEIGHT_JOB_CALCULATE_PI_DEPTH = 1;
  1390. static const AZ::s32 MEDIUM_WEIGHT_JOB_CALCULATE_PI_DEPTH = 1024;
  1391. static const AZ::s32 HEAVY_WEIGHT_JOB_CALCULATE_PI_DEPTH = 1048576;
  1392. static const AZ::u32 SMALL_NUMBER_OF_JOBS = 10;
  1393. static const AZ::u32 MEDIUM_NUMBER_OF_JOBS = 1024;
  1394. static const AZ::u32 LARGE_NUMBER_OF_JOBS = 16384;
  1395. void internalSetUp()
  1396. {
  1397. JobManagerDesc desc;
  1398. JobManagerThreadDesc threadDesc;
  1399. #if AZ_TRAIT_SET_JOB_PROCESSOR_ID
  1400. threadDesc.m_cpuId = 0; // Don't set processors IDs on windows
  1401. #endif // AZ_TRAIT_SET_JOB_PROCESSOR_ID
  1402. const AZ::u32 numWorkerThreads = desc.GetWorkerThreadCount(AZStd::thread::hardware_concurrency());
  1403. for (AZ::u32 i = 0; i < numWorkerThreads; ++i)
  1404. {
  1405. desc.m_workerThreads.push_back(threadDesc);
  1406. #if AZ_TRAIT_SET_JOB_PROCESSOR_ID
  1407. threadDesc.m_cpuId++;
  1408. #endif // AZ_TRAIT_SET_JOB_PROCESSOR_ID
  1409. }
  1410. m_jobManager = aznew JobManager(desc);
  1411. m_jobContext = aznew JobContext(*m_jobManager);
  1412. JobContext::SetGlobalContext(m_jobContext);
  1413. // Generate some random priorities
  1414. m_randomPriorities.resize(LARGE_NUMBER_OF_JOBS);
  1415. std::mt19937_64 randomPriorityGenerator(1); // Always use the same seed
  1416. std::uniform_int_distribution<> randomPriorityDistribution(std::numeric_limits<AZ::s8>::min(),
  1417. std::numeric_limits<AZ::s8>::max());
  1418. std::generate(m_randomPriorities.begin(), m_randomPriorities.end(), [&randomPriorityDistribution, &randomPriorityGenerator]()
  1419. {
  1420. return static_cast<AZ::s8>(randomPriorityDistribution(randomPriorityGenerator));
  1421. });
  1422. // Generate some random depths
  1423. m_randomDepths.resize(LARGE_NUMBER_OF_JOBS);
  1424. std::mt19937_64 randomDepthGenerator(1); // Always use the same seed
  1425. std::uniform_int_distribution<> randomDepthDistribution(LIGHT_WEIGHT_JOB_CALCULATE_PI_DEPTH,
  1426. HEAVY_WEIGHT_JOB_CALCULATE_PI_DEPTH);
  1427. std::generate(m_randomDepths.begin(), m_randomDepths.end(), [&randomDepthDistribution, &randomDepthGenerator]()
  1428. {
  1429. return randomDepthDistribution(randomDepthGenerator);
  1430. });
  1431. }
  1432. void SetUp(::benchmark::State&) override
  1433. {
  1434. internalSetUp();
  1435. }
  1436. void SetUp(const ::benchmark::State&) override
  1437. {
  1438. internalSetUp();
  1439. }
  1440. void internalTearDown()
  1441. {
  1442. JobContext::SetGlobalContext(nullptr);
  1443. // We must clear these vectors before destroying the allocators.
  1444. m_randomPriorities = {};
  1445. m_randomDepths = {};
  1446. delete m_jobContext;
  1447. delete m_jobManager;
  1448. }
  1449. void TearDown(::benchmark::State&) override
  1450. {
  1451. internalTearDown();
  1452. }
  1453. void TearDown(const ::benchmark::State&) override
  1454. {
  1455. internalTearDown();
  1456. }
  1457. protected:
  1458. inline void RunCalculatePiJob(AZ::s32 depth, AZ::s8 priority)
  1459. {
  1460. (aznew TestJobCalculatePi(depth, priority, m_jobContext))->Start();
  1461. }
  1462. inline void RunMultipleCalculatePiJobsWithDefaultPriority(AZ::u32 numberOfJobs, AZ::s32 depth)
  1463. {
  1464. for (AZ::u32 i = 0; i < numberOfJobs; ++i)
  1465. {
  1466. RunCalculatePiJob(depth, 0);
  1467. }
  1468. // Wait until all the jobs have completed.
  1469. while (TestJobCalculatePi::s_numIncompleteJobs > 0) {}
  1470. }
  1471. inline void RunMultipleCalculatePiJobsWithRandomPriority(AZ::u32 numberOfJobs, AZ::s32 depth)
  1472. {
  1473. for (AZ::u32 i = 0; i < numberOfJobs; ++i)
  1474. {
  1475. RunCalculatePiJob(depth, m_randomPriorities[i]);
  1476. }
  1477. // Wait until all the jobs have completed.
  1478. while (TestJobCalculatePi::s_numIncompleteJobs > 0) {}
  1479. }
  1480. inline void RunMultipleCalculatePiJobsWithRandomDepthAndDefaultPriority(AZ::u32 numberOfJobs)
  1481. {
  1482. for (AZ::u32 i = 0; i < numberOfJobs; ++i)
  1483. {
  1484. RunCalculatePiJob(m_randomDepths[i], 0);
  1485. }
  1486. // Wait until all the jobs have completed.
  1487. while (TestJobCalculatePi::s_numIncompleteJobs > 0) {}
  1488. }
  1489. inline void RunMultipleCalculatePiJobsWithRandomDepthAndRandomPriority(AZ::u32 numberOfJobs)
  1490. {
  1491. for (AZ::u32 i = 0; i < numberOfJobs; ++i)
  1492. {
  1493. RunCalculatePiJob(m_randomDepths[i],m_randomPriorities[i]);
  1494. }
  1495. // Wait until all the jobs have completed.
  1496. while (TestJobCalculatePi::s_numIncompleteJobs > 0) {}
  1497. }
  1498. protected:
  1499. JobManager* m_jobManager = nullptr;
  1500. JobContext* m_jobContext = nullptr;
  1501. AZStd::vector<AZ::u32> m_randomDepths;
  1502. AZStd::vector<AZ::s8> m_randomPriorities;
  1503. };
  1504. BENCHMARK_F(JobBenchmarkFixture, RunSmallNumberOfLightWeightJobsWithDefaultPriority)(benchmark::State& state)
  1505. {
  1506. for ([[maybe_unused]] auto _ : state)
  1507. {
  1508. RunMultipleCalculatePiJobsWithDefaultPriority(SMALL_NUMBER_OF_JOBS, LIGHT_WEIGHT_JOB_CALCULATE_PI_DEPTH);
  1509. }
  1510. }
  1511. BENCHMARK_F(JobBenchmarkFixture, RunMediumNumberOfLightWeightJobsWithDefaultPriority)(benchmark::State& state)
  1512. {
  1513. for ([[maybe_unused]] auto _ : state)
  1514. {
  1515. RunMultipleCalculatePiJobsWithDefaultPriority(MEDIUM_NUMBER_OF_JOBS, LIGHT_WEIGHT_JOB_CALCULATE_PI_DEPTH);
  1516. }
  1517. }
  1518. BENCHMARK_F(JobBenchmarkFixture, RunLargeNumberOfLightWeightJobsWithDefaultPriority)(benchmark::State& state)
  1519. {
  1520. for ([[maybe_unused]] auto _ : state)
  1521. {
  1522. RunMultipleCalculatePiJobsWithDefaultPriority(LARGE_NUMBER_OF_JOBS, LIGHT_WEIGHT_JOB_CALCULATE_PI_DEPTH);
  1523. }
  1524. }
  1525. BENCHMARK_F(JobBenchmarkFixture, RunSmallNumberOfMediumWeightJobsWithDefaultPriority)(benchmark::State& state)
  1526. {
  1527. for ([[maybe_unused]] auto _ : state)
  1528. {
  1529. RunMultipleCalculatePiJobsWithDefaultPriority(SMALL_NUMBER_OF_JOBS, MEDIUM_WEIGHT_JOB_CALCULATE_PI_DEPTH);
  1530. }
  1531. }
  1532. BENCHMARK_F(JobBenchmarkFixture, RunMediumNumberOfMediumWeightJobsWithDefaultPriority)(benchmark::State& state)
  1533. {
  1534. for ([[maybe_unused]] auto _ : state)
  1535. {
  1536. RunMultipleCalculatePiJobsWithDefaultPriority(MEDIUM_NUMBER_OF_JOBS, MEDIUM_WEIGHT_JOB_CALCULATE_PI_DEPTH);
  1537. }
  1538. }
  1539. BENCHMARK_F(JobBenchmarkFixture, RunLargeNumberOfMediumWeightJobsWithDefaultPriority)(benchmark::State& state)
  1540. {
  1541. for ([[maybe_unused]] auto _ : state)
  1542. {
  1543. RunMultipleCalculatePiJobsWithDefaultPriority(LARGE_NUMBER_OF_JOBS, MEDIUM_WEIGHT_JOB_CALCULATE_PI_DEPTH);
  1544. }
  1545. }
  1546. BENCHMARK_F(JobBenchmarkFixture, RunSmallNumberOfHeavyWeightJobsWithDefaultPriority)(benchmark::State& state)
  1547. {
  1548. for ([[maybe_unused]] auto _ : state)
  1549. {
  1550. RunMultipleCalculatePiJobsWithDefaultPriority(SMALL_NUMBER_OF_JOBS, HEAVY_WEIGHT_JOB_CALCULATE_PI_DEPTH);
  1551. }
  1552. }
  1553. BENCHMARK_F(JobBenchmarkFixture, RunMediumNumberOfHeavyWeightJobsWithDefaultPriority)(benchmark::State& state)
  1554. {
  1555. for ([[maybe_unused]] auto _ : state)
  1556. {
  1557. RunMultipleCalculatePiJobsWithDefaultPriority(MEDIUM_NUMBER_OF_JOBS, HEAVY_WEIGHT_JOB_CALCULATE_PI_DEPTH);
  1558. }
  1559. }
  1560. BENCHMARK_F(JobBenchmarkFixture, RunLargeNumberOfHeavyWeightJobsWithDefaultPriority)(benchmark::State& state)
  1561. {
  1562. for ([[maybe_unused]] auto _ : state)
  1563. {
  1564. RunMultipleCalculatePiJobsWithDefaultPriority(LARGE_NUMBER_OF_JOBS, HEAVY_WEIGHT_JOB_CALCULATE_PI_DEPTH);
  1565. }
  1566. }
  1567. BENCHMARK_F(JobBenchmarkFixture, RunSmallNumberOfRandomWeightJobsWithDefaultPriority)(benchmark::State& state)
  1568. {
  1569. for ([[maybe_unused]] auto _ : state)
  1570. {
  1571. RunMultipleCalculatePiJobsWithRandomDepthAndDefaultPriority(SMALL_NUMBER_OF_JOBS);
  1572. }
  1573. }
  1574. BENCHMARK_F(JobBenchmarkFixture, RunMediumNumberOfRandomWeightJobsWithDefaultPriority)(benchmark::State& state)
  1575. {
  1576. for ([[maybe_unused]] auto _ : state)
  1577. {
  1578. RunMultipleCalculatePiJobsWithRandomDepthAndDefaultPriority(MEDIUM_NUMBER_OF_JOBS);
  1579. }
  1580. }
  1581. BENCHMARK_F(JobBenchmarkFixture, RunLargeNumberOfRandomWeightJobsWithDefaultPriority)(benchmark::State& state)
  1582. {
  1583. for ([[maybe_unused]] auto _ : state)
  1584. {
  1585. RunMultipleCalculatePiJobsWithRandomDepthAndDefaultPriority(LARGE_NUMBER_OF_JOBS);
  1586. }
  1587. }
  1588. BENCHMARK_F(JobBenchmarkFixture, RunSmallNumberOfLightWeightJobsWithRandomPriorities)(benchmark::State& state)
  1589. {
  1590. for ([[maybe_unused]] auto _ : state)
  1591. {
  1592. RunMultipleCalculatePiJobsWithRandomPriority(SMALL_NUMBER_OF_JOBS, LIGHT_WEIGHT_JOB_CALCULATE_PI_DEPTH);
  1593. }
  1594. }
  1595. BENCHMARK_F(JobBenchmarkFixture, RunMediumNumberOfLightWeightJobsWithRandomPriorities)(benchmark::State& state)
  1596. {
  1597. for ([[maybe_unused]] auto _ : state)
  1598. {
  1599. RunMultipleCalculatePiJobsWithRandomPriority(MEDIUM_NUMBER_OF_JOBS, LIGHT_WEIGHT_JOB_CALCULATE_PI_DEPTH);
  1600. }
  1601. }
  1602. BENCHMARK_F(JobBenchmarkFixture, RunLargeNumberOfLightWeightJobsWithRandomPriorities)(benchmark::State& state)
  1603. {
  1604. for ([[maybe_unused]] auto _ : state)
  1605. {
  1606. RunMultipleCalculatePiJobsWithRandomPriority(LARGE_NUMBER_OF_JOBS, LIGHT_WEIGHT_JOB_CALCULATE_PI_DEPTH);
  1607. }
  1608. }
  1609. BENCHMARK_F(JobBenchmarkFixture, RunSmallNumberOfMediumWeightJobsWithRandomPriorities)(benchmark::State& state)
  1610. {
  1611. for ([[maybe_unused]] auto _ : state)
  1612. {
  1613. RunMultipleCalculatePiJobsWithRandomPriority(SMALL_NUMBER_OF_JOBS, MEDIUM_WEIGHT_JOB_CALCULATE_PI_DEPTH);
  1614. }
  1615. }
  1616. BENCHMARK_F(JobBenchmarkFixture, RunMediumNumberOfMediumWeightJobsWithRandomPriorities)(benchmark::State& state)
  1617. {
  1618. for ([[maybe_unused]] auto _ : state)
  1619. {
  1620. RunMultipleCalculatePiJobsWithRandomPriority(MEDIUM_NUMBER_OF_JOBS, MEDIUM_WEIGHT_JOB_CALCULATE_PI_DEPTH);
  1621. }
  1622. }
  1623. BENCHMARK_F(JobBenchmarkFixture, RunLargeNumberOfMediumWeightJobsWithRandomPriorities)(benchmark::State& state)
  1624. {
  1625. for ([[maybe_unused]] auto _ : state)
  1626. {
  1627. RunMultipleCalculatePiJobsWithRandomPriority(LARGE_NUMBER_OF_JOBS, MEDIUM_WEIGHT_JOB_CALCULATE_PI_DEPTH);
  1628. }
  1629. }
  1630. BENCHMARK_F(JobBenchmarkFixture, RunSmallNumberOfHeavyWeightJobsWithRandomPriorities)(benchmark::State& state)
  1631. {
  1632. for ([[maybe_unused]] auto _ : state)
  1633. {
  1634. RunMultipleCalculatePiJobsWithRandomPriority(SMALL_NUMBER_OF_JOBS, HEAVY_WEIGHT_JOB_CALCULATE_PI_DEPTH);
  1635. }
  1636. }
  1637. BENCHMARK_F(JobBenchmarkFixture, RunMediumNumberOfHeavyWeightJobsWithRandomPriorities)(benchmark::State& state)
  1638. {
  1639. for ([[maybe_unused]] auto _ : state)
  1640. {
  1641. RunMultipleCalculatePiJobsWithRandomPriority(MEDIUM_NUMBER_OF_JOBS, HEAVY_WEIGHT_JOB_CALCULATE_PI_DEPTH);
  1642. }
  1643. }
  1644. BENCHMARK_F(JobBenchmarkFixture, RunLargeNumberOfHeavyWeightJobsWithRandomPriorities)(benchmark::State& state)
  1645. {
  1646. for ([[maybe_unused]] auto _ : state)
  1647. {
  1648. RunMultipleCalculatePiJobsWithRandomPriority(LARGE_NUMBER_OF_JOBS, HEAVY_WEIGHT_JOB_CALCULATE_PI_DEPTH);
  1649. }
  1650. }
  1651. BENCHMARK_F(JobBenchmarkFixture, RunSmallNumberOfRandomWeightJobsWithRandomPriorities)(benchmark::State& state)
  1652. {
  1653. for ([[maybe_unused]] auto _ : state)
  1654. {
  1655. RunMultipleCalculatePiJobsWithRandomDepthAndRandomPriority(SMALL_NUMBER_OF_JOBS);
  1656. }
  1657. }
  1658. BENCHMARK_F(JobBenchmarkFixture, RunMediumNumberOfRandomWeightJobsWithRandomPriorities)(benchmark::State& state)
  1659. {
  1660. for ([[maybe_unused]] auto _ : state)
  1661. {
  1662. RunMultipleCalculatePiJobsWithRandomDepthAndRandomPriority(MEDIUM_NUMBER_OF_JOBS);
  1663. }
  1664. }
  1665. BENCHMARK_F(JobBenchmarkFixture, RunLargeNumberOfRandomWeightJobsWithRandomPriorities)(benchmark::State& state)
  1666. {
  1667. for ([[maybe_unused]] auto _ : state)
  1668. {
  1669. RunMultipleCalculatePiJobsWithRandomDepthAndRandomPriority(LARGE_NUMBER_OF_JOBS);
  1670. }
  1671. }
  1672. } // Benchmark
  1673. #endif // HAVE_BENCHMARK