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