pthreadpool.h 97 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555
  1. #ifndef PTHREADPOOL_H_
  2. #define PTHREADPOOL_H_
  3. #include <stddef.h>
  4. #include <stdint.h>
  5. typedef struct pthreadpool* pthreadpool_t;
  6. typedef void (*pthreadpool_task_1d_t)(void*, size_t);
  7. typedef void (*pthreadpool_task_1d_with_thread_t)(void*, size_t, size_t);
  8. typedef void (*pthreadpool_task_1d_tile_1d_t)(void*, size_t, size_t);
  9. typedef void (*pthreadpool_task_2d_t)(void*, size_t, size_t);
  10. typedef void (*pthreadpool_task_2d_with_thread_t)(void*, size_t, size_t, size_t);
  11. typedef void (*pthreadpool_task_2d_tile_1d_t)(void*, size_t, size_t, size_t);
  12. typedef void (*pthreadpool_task_2d_tile_2d_t)(void*, size_t, size_t, size_t, size_t);
  13. typedef void (*pthreadpool_task_3d_t)(void*, size_t, size_t, size_t);
  14. typedef void (*pthreadpool_task_3d_tile_1d_t)(void*, size_t, size_t, size_t, size_t);
  15. typedef void (*pthreadpool_task_3d_tile_1d_with_thread_t)(void*, size_t, size_t, size_t, size_t, size_t);
  16. typedef void (*pthreadpool_task_3d_tile_2d_t)(void*, size_t, size_t, size_t, size_t, size_t);
  17. typedef void (*pthreadpool_task_4d_t)(void*, size_t, size_t, size_t, size_t);
  18. typedef void (*pthreadpool_task_4d_tile_1d_t)(void*, size_t, size_t, size_t, size_t, size_t);
  19. typedef void (*pthreadpool_task_4d_tile_2d_t)(void*, size_t, size_t, size_t, size_t, size_t, size_t);
  20. typedef void (*pthreadpool_task_5d_t)(void*, size_t, size_t, size_t, size_t, size_t);
  21. typedef void (*pthreadpool_task_5d_tile_1d_t)(void*, size_t, size_t, size_t, size_t, size_t, size_t);
  22. typedef void (*pthreadpool_task_5d_tile_2d_t)(void*, size_t, size_t, size_t, size_t, size_t, size_t, size_t);
  23. typedef void (*pthreadpool_task_6d_t)(void*, size_t, size_t, size_t, size_t, size_t, size_t);
  24. typedef void (*pthreadpool_task_6d_tile_1d_t)(void*, size_t, size_t, size_t, size_t, size_t, size_t, size_t);
  25. typedef void (*pthreadpool_task_6d_tile_2d_t)(void*, size_t, size_t, size_t, size_t, size_t, size_t, size_t, size_t);
  26. typedef void (*pthreadpool_task_1d_with_id_t)(void*, uint32_t, size_t);
  27. typedef void (*pthreadpool_task_2d_tile_1d_with_id_t)(void*, uint32_t, size_t, size_t, size_t);
  28. typedef void (*pthreadpool_task_2d_tile_2d_with_id_t)(void*, uint32_t, size_t, size_t, size_t, size_t);
  29. typedef void (*pthreadpool_task_3d_tile_1d_with_id_t)(void*, uint32_t, size_t, size_t, size_t, size_t);
  30. typedef void (*pthreadpool_task_3d_tile_2d_with_id_t)(void*, uint32_t, size_t, size_t, size_t, size_t, size_t);
  31. typedef void (*pthreadpool_task_4d_tile_2d_with_id_t)(void*, uint32_t, size_t, size_t, size_t, size_t, size_t, size_t);
  32. typedef void (*pthreadpool_task_2d_tile_1d_with_id_with_thread_t)(void*, uint32_t, size_t, size_t, size_t, size_t);
  33. typedef void (*pthreadpool_task_3d_tile_1d_with_id_with_thread_t)(void*, uint32_t, size_t, size_t, size_t, size_t, size_t);
  34. /**
  35. * Disable support for denormalized numbers to the maximum extent possible for
  36. * the duration of the computation.
  37. *
  38. * Handling denormalized floating-point numbers is often implemented in
  39. * microcode, and incurs significant performance degradation. This hint
  40. * instructs the thread pool to disable support for denormalized numbers before
  41. * running the computation by manipulating architecture-specific control
  42. * registers, and restore the initial value of control registers after the
  43. * computation is complete. The thread pool temporary disables denormalized
  44. * numbers on all threads involved in the computation (i.e. the caller threads,
  45. * and potentially worker threads).
  46. *
  47. * Disabling denormalized numbers may have a small negative effect on results'
  48. * accuracy. As various architectures differ in capabilities to control
  49. * processing of denormalized numbers, using this flag may also hurt results'
  50. * reproducibility across different instruction set architectures.
  51. */
  52. #define PTHREADPOOL_FLAG_DISABLE_DENORMALS 0x00000001
  53. /**
  54. * Yield worker threads to the system scheduler after the operation is finished.
  55. *
  56. * Force workers to use kernel wait (instead of active spin-wait by default) for
  57. * new commands after this command is processed. This flag affects only the
  58. * immediate next operation on this thread pool. To make the thread pool always
  59. * use kernel wait, pass this flag to all parallelization functions.
  60. */
  61. #define PTHREADPOOL_FLAG_YIELD_WORKERS 0x00000002
  62. #ifdef __cplusplus
  63. extern "C" {
  64. #endif
  65. /**
  66. * Create a thread pool with the specified number of threads.
  67. *
  68. * @param threads_count the number of threads in the thread pool.
  69. * A value of 0 has special interpretation: it creates a thread pool with as
  70. * many threads as there are logical processors in the system.
  71. *
  72. * @returns A pointer to an opaque thread pool object if the call is
  73. * successful, or NULL pointer if the call failed.
  74. */
  75. pthreadpool_t pthreadpool_create(size_t threads_count);
  76. /**
  77. * Query the number of threads in a thread pool.
  78. *
  79. * @param threadpool the thread pool to query.
  80. *
  81. * @returns The number of threads in the thread pool.
  82. */
  83. size_t pthreadpool_get_threads_count(pthreadpool_t threadpool);
  84. /**
  85. * Process items on a 1D grid.
  86. *
  87. * The function implements a parallel version of the following snippet:
  88. *
  89. * for (size_t i = 0; i < range; i++)
  90. * function(context, i);
  91. *
  92. * When the function returns, all items have been processed and the thread pool
  93. * is ready for a new task.
  94. *
  95. * @note If multiple threads call this function with the same thread pool, the
  96. * calls are serialized.
  97. *
  98. * @param threadpool the thread pool to use for parallelisation. If threadpool
  99. * is NULL, all items are processed serially on the calling thread.
  100. * @param function the function to call for each item.
  101. * @param context the first argument passed to the specified function.
  102. * @param range the number of items on the 1D grid to process. The
  103. * specified function will be called once for each item.
  104. * @param flags a bitwise combination of zero or more optional flags
  105. * (PTHREADPOOL_FLAG_DISABLE_DENORMALS or PTHREADPOOL_FLAG_YIELD_WORKERS)
  106. */
  107. void pthreadpool_parallelize_1d(
  108. pthreadpool_t threadpool,
  109. pthreadpool_task_1d_t function,
  110. void* context,
  111. size_t range,
  112. uint32_t flags);
  113. /**
  114. * Process items on a 1D grid passing along the current thread id.
  115. *
  116. * The function implements a parallel version of the following snippet:
  117. *
  118. * for (size_t i = 0; i < range; i++)
  119. * function(context, thread_index, i);
  120. *
  121. * When the function returns, all items have been processed and the thread pool
  122. * is ready for a new task.
  123. *
  124. * @note If multiple threads call this function with the same thread pool, the
  125. * calls are serialized.
  126. *
  127. * @param threadpool the thread pool to use for parallelisation. If threadpool
  128. * is NULL, all items are processed serially on the calling thread.
  129. * @param function the function to call for each item.
  130. * @param context the first argument passed to the specified function.
  131. * @param range the number of items on the 1D grid to process. The
  132. * specified function will be called once for each item.
  133. * @param flags a bitwise combination of zero or more optional flags
  134. * (PTHREADPOOL_FLAG_DISABLE_DENORMALS or PTHREADPOOL_FLAG_YIELD_WORKERS)
  135. */
  136. void pthreadpool_parallelize_1d_with_thread(
  137. pthreadpool_t threadpool,
  138. pthreadpool_task_1d_with_thread_t function,
  139. void* context,
  140. size_t range,
  141. uint32_t flags);
  142. /**
  143. * Process items on a 1D grid using a microarchitecture-aware task function.
  144. *
  145. * The function implements a parallel version of the following snippet:
  146. *
  147. * uint32_t uarch_index = cpuinfo_initialize() ?
  148. * cpuinfo_get_current_uarch_index() : default_uarch_index;
  149. * if (uarch_index > max_uarch_index) uarch_index = default_uarch_index;
  150. * for (size_t i = 0; i < range; i++)
  151. * function(context, uarch_index, i);
  152. *
  153. * When the function returns, all items have been processed and the thread pool
  154. * is ready for a new task.
  155. *
  156. * @note If multiple threads call this function with the same thread pool, the
  157. * calls are serialized.
  158. *
  159. * @param threadpool the thread pool to use for parallelisation. If
  160. * threadpool is NULL, all items are processed serially on the calling
  161. * thread.
  162. * @param function the function to call for each item.
  163. * @param context the first argument passed to the specified
  164. * function.
  165. * @param default_uarch_index the microarchitecture index to use when
  166. * pthreadpool is configured without cpuinfo, cpuinfo initialization failed,
  167. * or index returned by cpuinfo_get_current_uarch_index() exceeds the
  168. * max_uarch_index value.
  169. * @param max_uarch_index the maximum microarchitecture index expected by
  170. * the specified function. If the index returned by
  171. * cpuinfo_get_current_uarch_index() exceeds this value, default_uarch_index
  172. * will be used instead. default_uarch_index can exceed max_uarch_index.
  173. * @param range the number of items on the 1D grid to process.
  174. * The specified function will be called once for each item.
  175. * @param flags a bitwise combination of zero or more optional
  176. * flags (PTHREADPOOL_FLAG_DISABLE_DENORMALS or
  177. * PTHREADPOOL_FLAG_YIELD_WORKERS)
  178. */
  179. void pthreadpool_parallelize_1d_with_uarch(
  180. pthreadpool_t threadpool,
  181. pthreadpool_task_1d_with_id_t function,
  182. void* context,
  183. uint32_t default_uarch_index,
  184. uint32_t max_uarch_index,
  185. size_t range,
  186. uint32_t flags);
  187. /**
  188. * Process items on a 1D grid with specified maximum tile size.
  189. *
  190. * The function implements a parallel version of the following snippet:
  191. *
  192. * for (size_t i = 0; i < range; i += tile)
  193. * function(context, i, min(range - i, tile));
  194. *
  195. * When the call returns, all items have been processed and the thread pool is
  196. * ready for a new task.
  197. *
  198. * @note If multiple threads call this function with the same thread pool,
  199. * the calls are serialized.
  200. *
  201. * @param threadpool the thread pool to use for parallelisation. If threadpool
  202. * is NULL, all items are processed serially on the calling thread.
  203. * @param function the function to call for each tile.
  204. * @param context the first argument passed to the specified function.
  205. * @param range the number of items on the 1D grid to process.
  206. * @param tile the maximum number of items on the 1D grid to process in
  207. * one function call.
  208. * @param flags a bitwise combination of zero or more optional flags
  209. * (PTHREADPOOL_FLAG_DISABLE_DENORMALS or PTHREADPOOL_FLAG_YIELD_WORKERS)
  210. */
  211. void pthreadpool_parallelize_1d_tile_1d(
  212. pthreadpool_t threadpool,
  213. pthreadpool_task_1d_tile_1d_t function,
  214. void* context,
  215. size_t range,
  216. size_t tile,
  217. uint32_t flags);
  218. /**
  219. * Process items on a 2D grid.
  220. *
  221. * The function implements a parallel version of the following snippet:
  222. *
  223. * for (size_t i = 0; i < range_i; i++)
  224. * for (size_t j = 0; j < range_j; j++)
  225. * function(context, i, j);
  226. *
  227. * When the function returns, all items have been processed and the thread pool
  228. * is ready for a new task.
  229. *
  230. * @note If multiple threads call this function with the same thread pool, the
  231. * calls are serialized.
  232. *
  233. * @param threadpool the thread pool to use for parallelisation. If threadpool
  234. * is NULL, all items are processed serially on the calling thread.
  235. * @param function the function to call for each item.
  236. * @param context the first argument passed to the specified function.
  237. * @param range_i the number of items to process along the first dimension
  238. * of the 2D grid.
  239. * @param range_j the number of items to process along the second dimension
  240. * of the 2D grid.
  241. * @param flags a bitwise combination of zero or more optional flags
  242. * (PTHREADPOOL_FLAG_DISABLE_DENORMALS or PTHREADPOOL_FLAG_YIELD_WORKERS)
  243. */
  244. void pthreadpool_parallelize_2d(
  245. pthreadpool_t threadpool,
  246. pthreadpool_task_2d_t function,
  247. void* context,
  248. size_t range_i,
  249. size_t range_j,
  250. uint32_t flags);
  251. /**
  252. * Process items on a 2D grid passing along the current thread id.
  253. *
  254. * The function implements a parallel version of the following snippet:
  255. *
  256. * for (size_t i = 0; i < range_i; i++)
  257. * for (size_t j = 0; j < range_j; j++)
  258. * function(context, thread_index, i, j);
  259. *
  260. * When the function returns, all items have been processed and the thread pool
  261. * is ready for a new task.
  262. *
  263. * @note If multiple threads call this function with the same thread pool, the
  264. * calls are serialized.
  265. *
  266. * @param threadpool the thread pool to use for parallelisation. If threadpool
  267. * is NULL, all items are processed serially on the calling thread.
  268. * @param function the function to call for each item.
  269. * @param context the first argument passed to the specified function.
  270. * @param range_i the number of items to process along the first dimension
  271. * of the 2D grid.
  272. * @param range_j the number of items to process along the second dimension
  273. * of the 2D grid.
  274. * @param flags a bitwise combination of zero or more optional flags
  275. * (PTHREADPOOL_FLAG_DISABLE_DENORMALS or PTHREADPOOL_FLAG_YIELD_WORKERS)
  276. */
  277. void pthreadpool_parallelize_2d_with_thread(
  278. pthreadpool_t threadpool,
  279. pthreadpool_task_2d_with_thread_t function,
  280. void* context,
  281. size_t range_i,
  282. size_t range_j,
  283. uint32_t flags);
  284. /**
  285. * Process items on a 2D grid with the specified maximum tile size along the
  286. * last grid dimension.
  287. *
  288. * The function implements a parallel version of the following snippet:
  289. *
  290. * for (size_t i = 0; i < range_i; i++)
  291. * for (size_t j = 0; j < range_j; j += tile_j)
  292. * function(context, i, j, min(range_j - j, tile_j));
  293. *
  294. * When the function returns, all items have been processed and the thread pool
  295. * is ready for a new task.
  296. *
  297. * @note If multiple threads call this function with the same thread pool, the
  298. * calls are serialized.
  299. *
  300. * @param threadpool the thread pool to use for parallelisation. If threadpool
  301. * is NULL, all items are processed serially on the calling thread.
  302. * @param function the function to call for each tile.
  303. * @param context the first argument passed to the specified function.
  304. * @param range_i the number of items to process along the first dimension
  305. * of the 2D grid.
  306. * @param range_j the number of items to process along the second dimension
  307. * of the 2D grid.
  308. * @param tile_j the maximum number of items along the second dimension of
  309. * the 2D grid to process in one function call.
  310. * @param flags a bitwise combination of zero or more optional flags
  311. * (PTHREADPOOL_FLAG_DISABLE_DENORMALS or PTHREADPOOL_FLAG_YIELD_WORKERS)
  312. */
  313. void pthreadpool_parallelize_2d_tile_1d(
  314. pthreadpool_t threadpool,
  315. pthreadpool_task_2d_tile_1d_t function,
  316. void* context,
  317. size_t range_i,
  318. size_t range_j,
  319. size_t tile_j,
  320. uint32_t flags);
  321. /**
  322. * Process items on a 2D grid with the specified maximum tile size along the
  323. * last grid dimension using a microarchitecture-aware task function.
  324. *
  325. * The function implements a parallel version of the following snippet:
  326. *
  327. * uint32_t uarch_index = cpuinfo_initialize() ?
  328. * cpuinfo_get_current_uarch_index() : default_uarch_index;
  329. * if (uarch_index > max_uarch_index) uarch_index = default_uarch_index;
  330. * for (size_t i = 0; i < range_i; i++)
  331. * for (size_t j = 0; j < range_j; j += tile_j)
  332. * function(context, uarch_index, i, j, min(range_j - j, tile_j));
  333. *
  334. * When the function returns, all items have been processed and the thread pool
  335. * is ready for a new task.
  336. *
  337. * @note If multiple threads call this function with the same thread pool, the
  338. * calls are serialized.
  339. *
  340. * @param threadpool the thread pool to use for parallelisation. If threadpool
  341. * is NULL, all items are processed serially on the calling thread.
  342. * @param function the function to call for each tile.
  343. * @param context the first argument passed to the specified function.
  344. * @param default_uarch_index the microarchitecture index to use when
  345. * pthreadpool is configured without cpuinfo, cpuinfo initialization failed,
  346. * or index returned by cpuinfo_get_current_uarch_index() exceeds the
  347. * max_uarch_index value.
  348. * @param max_uarch_index the maximum microarchitecture index expected by
  349. * the specified function. If the index returned by
  350. * cpuinfo_get_current_uarch_index() exceeds this value, default_uarch_index
  351. * will be used instead. default_uarch_index can exceed max_uarch_index.
  352. * @param range_i the number of items to process along the first dimension
  353. * of the 2D grid.
  354. * @param range_j the number of items to process along the second dimension
  355. * of the 2D grid.
  356. * @param tile_j the maximum number of items along the second dimension of
  357. * the 2D grid to process in one function call.
  358. * @param flags a bitwise combination of zero or more optional flags
  359. * (PTHREADPOOL_FLAG_DISABLE_DENORMALS or PTHREADPOOL_FLAG_YIELD_WORKERS)
  360. */
  361. void pthreadpool_parallelize_2d_tile_1d_with_uarch(
  362. pthreadpool_t threadpool,
  363. pthreadpool_task_2d_tile_1d_with_id_t function,
  364. void* context,
  365. uint32_t default_uarch_index,
  366. uint32_t max_uarch_index,
  367. size_t range_i,
  368. size_t range_j,
  369. size_t tile_j,
  370. uint32_t flags);
  371. /**
  372. * Process items on a 2D grid with the specified maximum tile size along the
  373. * last grid dimension using a microarchitecture-aware task function and passing
  374. * along the current thread id.
  375. *
  376. * The function implements a parallel version of the following snippet:
  377. *
  378. * uint32_t uarch_index = cpuinfo_initialize() ?
  379. * cpuinfo_get_current_uarch_index() : default_uarch_index;
  380. * if (uarch_index > max_uarch_index) uarch_index = default_uarch_index;
  381. * for (size_t i = 0; i < range_i; i++)
  382. * for (size_t j = 0; j < range_j; j += tile_j)
  383. * function(context, uarch_index, thread_index, i, j, min(range_j - j, tile_j));
  384. *
  385. * When the function returns, all items have been processed and the thread pool
  386. * is ready for a new task.
  387. *
  388. * @note If multiple threads call this function with the same thread pool, the
  389. * calls are serialized.
  390. *
  391. * @param threadpool the thread pool to use for parallelisation. If threadpool
  392. * is NULL, all items are processed serially on the calling thread.
  393. * @param function the function to call for each tile.
  394. * @param context the first argument passed to the specified function.
  395. * @param default_uarch_index the microarchitecture index to use when
  396. * pthreadpool is configured without cpuinfo, cpuinfo initialization failed,
  397. * or index returned by cpuinfo_get_current_uarch_index() exceeds the
  398. * max_uarch_index value.
  399. * @param max_uarch_index the maximum microarchitecture index expected by
  400. * the specified function. If the index returned by
  401. * cpuinfo_get_current_uarch_index() exceeds this value, default_uarch_index
  402. * will be used instead. default_uarch_index can exceed max_uarch_index.
  403. * @param range_i the number of items to process along the first dimension
  404. * of the 2D grid.
  405. * @param range_j the number of items to process along the second dimension
  406. * of the 2D grid.
  407. * @param tile_j the maximum number of items along the second dimension of
  408. * the 2D grid to process in one function call.
  409. * @param flags a bitwise combination of zero or more optional flags
  410. * (PTHREADPOOL_FLAG_DISABLE_DENORMALS or PTHREADPOOL_FLAG_YIELD_WORKERS)
  411. */
  412. void pthreadpool_parallelize_2d_tile_1d_with_uarch_with_thread(
  413. pthreadpool_t threadpool,
  414. pthreadpool_task_2d_tile_1d_with_id_with_thread_t function,
  415. void* context,
  416. uint32_t default_uarch_index,
  417. uint32_t max_uarch_index,
  418. size_t range_i,
  419. size_t range_j,
  420. size_t tile_j,
  421. uint32_t flags);
  422. /**
  423. * Process items on a 2D grid with the specified maximum tile size along each
  424. * grid dimension.
  425. *
  426. * The function implements a parallel version of the following snippet:
  427. *
  428. * for (size_t i = 0; i < range_i; i += tile_i)
  429. * for (size_t j = 0; j < range_j; j += tile_j)
  430. * function(context, i, j,
  431. * min(range_i - i, tile_i), min(range_j - j, tile_j));
  432. *
  433. * When the function returns, all items have been processed and the thread pool
  434. * is ready for a new task.
  435. *
  436. * @note If multiple threads call this function with the same thread pool, the
  437. * calls are serialized.
  438. *
  439. * @param threadpool the thread pool to use for parallelisation. If threadpool
  440. * is NULL, all items are processed serially on the calling thread.
  441. * @param function the function to call for each tile.
  442. * @param context the first argument passed to the specified function.
  443. * @param range_i the number of items to process along the first dimension
  444. * of the 2D grid.
  445. * @param range_j the number of items to process along the second dimension
  446. * of the 2D grid.
  447. * @param tile_j the maximum number of items along the first dimension of
  448. * the 2D grid to process in one function call.
  449. * @param tile_j the maximum number of items along the second dimension of
  450. * the 2D grid to process in one function call.
  451. * @param flags a bitwise combination of zero or more optional flags
  452. * (PTHREADPOOL_FLAG_DISABLE_DENORMALS or PTHREADPOOL_FLAG_YIELD_WORKERS)
  453. */
  454. void pthreadpool_parallelize_2d_tile_2d(
  455. pthreadpool_t threadpool,
  456. pthreadpool_task_2d_tile_2d_t function,
  457. void* context,
  458. size_t range_i,
  459. size_t range_j,
  460. size_t tile_i,
  461. size_t tile_j,
  462. uint32_t flags);
  463. /**
  464. * Process items on a 2D grid with the specified maximum tile size along each
  465. * grid dimension using a microarchitecture-aware task function.
  466. *
  467. * The function implements a parallel version of the following snippet:
  468. *
  469. * uint32_t uarch_index = cpuinfo_initialize() ?
  470. * cpuinfo_get_current_uarch_index() : default_uarch_index;
  471. * if (uarch_index > max_uarch_index) uarch_index = default_uarch_index;
  472. * for (size_t i = 0; i < range_i; i += tile_i)
  473. * for (size_t j = 0; j < range_j; j += tile_j)
  474. * function(context, uarch_index, i, j,
  475. * min(range_i - i, tile_i), min(range_j - j, tile_j));
  476. *
  477. * When the function returns, all items have been processed and the thread pool
  478. * is ready for a new task.
  479. *
  480. * @note If multiple threads call this function with the same thread pool, the
  481. * calls are serialized.
  482. *
  483. * @param threadpool the thread pool to use for parallelisation. If
  484. * threadpool is NULL, all items are processed serially on the calling
  485. * thread.
  486. * @param function the function to call for each tile.
  487. * @param context the first argument passed to the specified
  488. * function.
  489. * @param default_uarch_index the microarchitecture index to use when
  490. * pthreadpool is configured without cpuinfo,
  491. * cpuinfo initialization failed, or index returned
  492. * by cpuinfo_get_current_uarch_index() exceeds
  493. * the max_uarch_index value.
  494. * @param max_uarch_index the maximum microarchitecture index expected
  495. * by the specified function. If the index returned
  496. * by cpuinfo_get_current_uarch_index() exceeds this
  497. * value, default_uarch_index will be used instead.
  498. * default_uarch_index can exceed max_uarch_index.
  499. * @param range_i the number of items to process along the first
  500. * dimension of the 2D grid.
  501. * @param range_j the number of items to process along the second
  502. * dimension of the 2D grid.
  503. * @param tile_j the maximum number of items along the first
  504. * dimension of the 2D grid to process in one function call.
  505. * @param tile_j the maximum number of items along the second
  506. * dimension of the 2D grid to process in one function call.
  507. * @param flags a bitwise combination of zero or more optional
  508. * flags (PTHREADPOOL_FLAG_DISABLE_DENORMALS or
  509. * PTHREADPOOL_FLAG_YIELD_WORKERS)
  510. */
  511. void pthreadpool_parallelize_2d_tile_2d_with_uarch(
  512. pthreadpool_t threadpool,
  513. pthreadpool_task_2d_tile_2d_with_id_t function,
  514. void* context,
  515. uint32_t default_uarch_index,
  516. uint32_t max_uarch_index,
  517. size_t range_i,
  518. size_t range_j,
  519. size_t tile_i,
  520. size_t tile_j,
  521. uint32_t flags);
  522. /**
  523. * Process items on a 3D grid.
  524. *
  525. * The function implements a parallel version of the following snippet:
  526. *
  527. * for (size_t i = 0; i < range_i; i++)
  528. * for (size_t j = 0; j < range_j; j++)
  529. * for (size_t k = 0; k < range_k; k++)
  530. * function(context, i, j, k);
  531. *
  532. * When the function returns, all items have been processed and the thread pool
  533. * is ready for a new task.
  534. *
  535. * @note If multiple threads call this function with the same thread pool, the
  536. * calls are serialized.
  537. *
  538. * @param threadpool the thread pool to use for parallelisation. If threadpool
  539. * is NULL, all items are processed serially on the calling thread.
  540. * @param function the function to call for each tile.
  541. * @param context the first argument passed to the specified function.
  542. * @param range_i the number of items to process along the first dimension
  543. * of the 3D grid.
  544. * @param range_j the number of items to process along the second dimension
  545. * of the 3D grid.
  546. * @param range_k the number of items to process along the third dimension
  547. * of the 3D grid.
  548. * @param flags a bitwise combination of zero or more optional flags
  549. * (PTHREADPOOL_FLAG_DISABLE_DENORMALS or PTHREADPOOL_FLAG_YIELD_WORKERS)
  550. */
  551. void pthreadpool_parallelize_3d(
  552. pthreadpool_t threadpool,
  553. pthreadpool_task_3d_t function,
  554. void* context,
  555. size_t range_i,
  556. size_t range_j,
  557. size_t range_k,
  558. uint32_t flags);
  559. /**
  560. * Process items on a 3D grid with the specified maximum tile size along the
  561. * last grid dimension.
  562. *
  563. * The function implements a parallel version of the following snippet:
  564. *
  565. * for (size_t i = 0; i < range_i; i++)
  566. * for (size_t j = 0; j < range_j; j++)
  567. * for (size_t k = 0; k < range_k; k += tile_k)
  568. * function(context, i, j, k, min(range_k - k, tile_k));
  569. *
  570. * When the function returns, all items have been processed and the thread pool
  571. * is ready for a new task.
  572. *
  573. * @note If multiple threads call this function with the same thread pool, the
  574. * calls are serialized.
  575. *
  576. * @param threadpool the thread pool to use for parallelisation. If threadpool
  577. * is NULL, all items are processed serially on the calling thread.
  578. * @param function the function to call for each tile.
  579. * @param context the first argument passed to the specified function.
  580. * @param range_i the number of items to process along the first dimension
  581. * of the 3D grid.
  582. * @param range_j the number of items to process along the second dimension
  583. * of the 3D grid.
  584. * @param range_k the number of items to process along the third dimension
  585. * of the 3D grid.
  586. * @param tile_k the maximum number of items along the third dimension of
  587. * the 3D grid to process in one function call.
  588. * @param flags a bitwise combination of zero or more optional flags
  589. * (PTHREADPOOL_FLAG_DISABLE_DENORMALS or PTHREADPOOL_FLAG_YIELD_WORKERS)
  590. */
  591. void pthreadpool_parallelize_3d_tile_1d(
  592. pthreadpool_t threadpool,
  593. pthreadpool_task_3d_tile_1d_t function,
  594. void* context,
  595. size_t range_i,
  596. size_t range_j,
  597. size_t range_k,
  598. size_t tile_k,
  599. uint32_t flags);
  600. /**
  601. * Process items on a 3D grid with the specified maximum tile size along the
  602. * last grid dimension and passing along the current thread id.
  603. *
  604. * The function implements a parallel version of the following snippet:
  605. *
  606. * for (size_t i = 0; i < range_i; i++)
  607. * for (size_t j = 0; j < range_j; j++)
  608. * for (size_t k = 0; k < range_k; k += tile_k)
  609. * function(context, thread_index, i, j, k, min(range_k - k, tile_k));
  610. *
  611. * When the function returns, all items have been processed and the thread pool
  612. * is ready for a new task.
  613. *
  614. * @note If multiple threads call this function with the same thread pool, the
  615. * calls are serialized.
  616. *
  617. * @param threadpool the thread pool to use for parallelisation. If threadpool
  618. * is NULL, all items are processed serially on the calling thread.
  619. * @param function the function to call for each tile.
  620. * @param context the first argument passed to the specified function.
  621. * @param range_i the number of items to process along the first dimension
  622. * of the 3D grid.
  623. * @param range_j the number of items to process along the second dimension
  624. * of the 3D grid.
  625. * @param range_k the number of items to process along the third dimension
  626. * of the 3D grid.
  627. * @param tile_k the maximum number of items along the third dimension of
  628. * the 3D grid to process in one function call.
  629. * @param flags a bitwise combination of zero or more optional flags
  630. * (PTHREADPOOL_FLAG_DISABLE_DENORMALS or PTHREADPOOL_FLAG_YIELD_WORKERS)
  631. */
  632. void pthreadpool_parallelize_3d_tile_1d_with_thread(
  633. pthreadpool_t threadpool,
  634. pthreadpool_task_3d_tile_1d_with_thread_t function,
  635. void* context,
  636. size_t range_i,
  637. size_t range_j,
  638. size_t range_k,
  639. size_t tile_k,
  640. uint32_t flags);
  641. /**
  642. * Process items on a 3D grid with the specified maximum tile size along the
  643. * last grid dimension using a microarchitecture-aware task function.
  644. *
  645. * The function implements a parallel version of the following snippet:
  646. *
  647. * uint32_t uarch_index = cpuinfo_initialize() ?
  648. * cpuinfo_get_current_uarch_index() : default_uarch_index;
  649. * if (uarch_index > max_uarch_index) uarch_index = default_uarch_index;
  650. * for (size_t i = 0; i < range_i; i++)
  651. * for (size_t j = 0; j < range_j; j++)
  652. * for (size_t k = 0; k < range_k; k += tile_k)
  653. * function(context, uarch_index, i, j, k, min(range_k - k, tile_k));
  654. *
  655. * When the function returns, all items have been processed and the thread pool
  656. * is ready for a new task.
  657. *
  658. * @note If multiple threads call this function with the same thread pool, the
  659. * calls are serialized.
  660. *
  661. * @param threadpool the thread pool to use for parallelisation. If
  662. * threadpool is NULL, all items are processed serially on the calling
  663. * thread.
  664. * @param function the function to call for each tile.
  665. * @param context the first argument passed to the specified
  666. * function.
  667. * @param default_uarch_index the microarchitecture index to use when
  668. * pthreadpool is configured without cpuinfo, cpuinfo initialization failed,
  669. * or index returned by cpuinfo_get_current_uarch_index() exceeds the
  670. * max_uarch_index value.
  671. * @param max_uarch_index the maximum microarchitecture index expected by
  672. * the specified function. If the index returned by
  673. * cpuinfo_get_current_uarch_index() exceeds this value, default_uarch_index
  674. * will be used instead. default_uarch_index can exceed max_uarch_index.
  675. * @param range_i the number of items to process along the first
  676. * dimension of the 3D grid.
  677. * @param range_j the number of items to process along the second
  678. * dimension of the 3D grid.
  679. * @param range_k the number of items to process along the third
  680. * dimension of the 3D grid.
  681. * @param tile_k the maximum number of items along the third
  682. * dimension of the 3D grid to process in one function call.
  683. * @param flags a bitwise combination of zero or more optional
  684. * flags (PTHREADPOOL_FLAG_DISABLE_DENORMALS or
  685. * PTHREADPOOL_FLAG_YIELD_WORKERS)
  686. */
  687. void pthreadpool_parallelize_3d_tile_1d_with_uarch(
  688. pthreadpool_t threadpool,
  689. pthreadpool_task_3d_tile_1d_with_id_t function,
  690. void* context,
  691. uint32_t default_uarch_index,
  692. uint32_t max_uarch_index,
  693. size_t range_i,
  694. size_t range_j,
  695. size_t range_k,
  696. size_t tile_k,
  697. uint32_t flags);
  698. /**
  699. * Process items on a 3D grid with the specified maximum tile size along the
  700. * last grid dimension using a microarchitecture-aware task function and passing
  701. * along the current thread id.
  702. *
  703. * The function implements a parallel version of the following snippet:
  704. *
  705. * uint32_t uarch_index = cpuinfo_initialize() ?
  706. * cpuinfo_get_current_uarch_index() : default_uarch_index;
  707. * if (uarch_index > max_uarch_index) uarch_index = default_uarch_index;
  708. * for (size_t i = 0; i < range_i; i++)
  709. * for (size_t j = 0; j < range_j; j++)
  710. * for (size_t k = 0; k < range_k; k += tile_k)
  711. * function(context, uarch_index, thread_index, i, j, k, min(range_k - k, tile_k));
  712. *
  713. * When the function returns, all items have been processed and the thread pool
  714. * is ready for a new task.
  715. *
  716. * @note If multiple threads call this function with the same thread pool, the
  717. * calls are serialized.
  718. *
  719. * @param threadpool the thread pool to use for parallelisation. If
  720. * threadpool is NULL, all items are processed serially on the calling
  721. * thread.
  722. * @param function the function to call for each tile.
  723. * @param context the first argument passed to the specified
  724. * function.
  725. * @param default_uarch_index the microarchitecture index to use when
  726. * pthreadpool is configured without cpuinfo, cpuinfo initialization failed,
  727. * or index returned by cpuinfo_get_current_uarch_index() exceeds the
  728. * max_uarch_index value.
  729. * @param max_uarch_index the maximum microarchitecture index expected by
  730. * the specified function. If the index returned by
  731. * cpuinfo_get_current_uarch_index() exceeds this value, default_uarch_index
  732. * will be used instead. default_uarch_index can exceed max_uarch_index.
  733. * @param range_i the number of items to process along the first
  734. * dimension of the 3D grid.
  735. * @param range_j the number of items to process along the second
  736. * dimension of the 3D grid.
  737. * @param range_k the number of items to process along the third
  738. * dimension of the 3D grid.
  739. * @param tile_k the maximum number of items along the third
  740. * dimension of the 3D grid to process in one function call.
  741. * @param flags a bitwise combination of zero or more optional
  742. * flags (PTHREADPOOL_FLAG_DISABLE_DENORMALS or
  743. * PTHREADPOOL_FLAG_YIELD_WORKERS)
  744. */
  745. void pthreadpool_parallelize_3d_tile_1d_with_uarch_with_thread(
  746. pthreadpool_t threadpool,
  747. pthreadpool_task_3d_tile_1d_with_id_with_thread_t function,
  748. void* context,
  749. uint32_t default_uarch_index,
  750. uint32_t max_uarch_index,
  751. size_t range_i,
  752. size_t range_j,
  753. size_t range_k,
  754. size_t tile_k,
  755. uint32_t flags);
  756. /**
  757. * Process items on a 3D grid with the specified maximum tile size along the
  758. * last two grid dimensions.
  759. *
  760. * The function implements a parallel version of the following snippet:
  761. *
  762. * for (size_t i = 0; i < range_i; i++)
  763. * for (size_t j = 0; j < range_j; j += tile_j)
  764. * for (size_t k = 0; k < range_k; k += tile_k)
  765. * function(context, i, j, k,
  766. * min(range_j - j, tile_j), min(range_k - k, tile_k));
  767. *
  768. * When the function returns, all items have been processed and the thread pool
  769. * is ready for a new task.
  770. *
  771. * @note If multiple threads call this function with the same thread pool, the
  772. * calls are serialized.
  773. *
  774. * @param threadpool the thread pool to use for parallelisation. If threadpool
  775. * is NULL, all items are processed serially on the calling thread.
  776. * @param function the function to call for each tile.
  777. * @param context the first argument passed to the specified function.
  778. * @param range_i the number of items to process along the first dimension
  779. * of the 3D grid.
  780. * @param range_j the number of items to process along the second dimension
  781. * of the 3D grid.
  782. * @param range_k the number of items to process along the third dimension
  783. * of the 3D grid.
  784. * @param tile_j the maximum number of items along the second dimension of
  785. * the 3D grid to process in one function call.
  786. * @param tile_k the maximum number of items along the third dimension of
  787. * the 3D grid to process in one function call.
  788. * @param flags a bitwise combination of zero or more optional flags
  789. * (PTHREADPOOL_FLAG_DISABLE_DENORMALS or PTHREADPOOL_FLAG_YIELD_WORKERS)
  790. */
  791. void pthreadpool_parallelize_3d_tile_2d(
  792. pthreadpool_t threadpool,
  793. pthreadpool_task_3d_tile_2d_t function,
  794. void* context,
  795. size_t range_i,
  796. size_t range_j,
  797. size_t range_k,
  798. size_t tile_j,
  799. size_t tile_k,
  800. uint32_t flags);
  801. /**
  802. * Process items on a 3D grid with the specified maximum tile size along the
  803. * last two grid dimensions using a microarchitecture-aware task function.
  804. *
  805. * The function implements a parallel version of the following snippet:
  806. *
  807. * uint32_t uarch_index = cpuinfo_initialize() ?
  808. * cpuinfo_get_current_uarch_index() : default_uarch_index;
  809. * if (uarch_index > max_uarch_index) uarch_index = default_uarch_index;
  810. * for (size_t i = 0; i < range_i; i++)
  811. * for (size_t j = 0; j < range_j; j += tile_j)
  812. * for (size_t k = 0; k < range_k; k += tile_k)
  813. * function(context, uarch_index, i, j, k,
  814. * min(range_j - j, tile_j), min(range_k - k, tile_k));
  815. *
  816. * When the function returns, all items have been processed and the thread pool
  817. * is ready for a new task.
  818. *
  819. * @note If multiple threads call this function with the same thread pool, the
  820. * calls are serialized.
  821. *
  822. * @param threadpool the thread pool to use for parallelisation. If
  823. * threadpool is NULL, all items are processed serially on the calling
  824. * thread.
  825. * @param function the function to call for each tile.
  826. * @param context the first argument passed to the specified
  827. * function.
  828. * @param default_uarch_index the microarchitecture index to use when
  829. * pthreadpool is configured without cpuinfo, cpuinfo initialization failed,
  830. * or index returned by cpuinfo_get_current_uarch_index() exceeds the
  831. * max_uarch_index value.
  832. * @param max_uarch_index the maximum microarchitecture index expected by
  833. * the specified function. If the index returned by
  834. * cpuinfo_get_current_uarch_index() exceeds this value, default_uarch_index
  835. * will be used instead. default_uarch_index can exceed max_uarch_index.
  836. * @param range_i the number of items to process along the first
  837. * dimension of the 3D grid.
  838. * @param range_j the number of items to process along the second
  839. * dimension of the 3D grid.
  840. * @param range_k the number of items to process along the third
  841. * dimension of the 3D grid.
  842. * @param tile_j the maximum number of items along the second
  843. * dimension of the 3D grid to process in one function call.
  844. * @param tile_k the maximum number of items along the third
  845. * dimension of the 3D grid to process in one function call.
  846. * @param flags a bitwise combination of zero or more optional
  847. * flags (PTHREADPOOL_FLAG_DISABLE_DENORMALS or
  848. * PTHREADPOOL_FLAG_YIELD_WORKERS)
  849. */
  850. void pthreadpool_parallelize_3d_tile_2d_with_uarch(
  851. pthreadpool_t threadpool,
  852. pthreadpool_task_3d_tile_2d_with_id_t function,
  853. void* context,
  854. uint32_t default_uarch_index,
  855. uint32_t max_uarch_index,
  856. size_t range_i,
  857. size_t range_j,
  858. size_t range_k,
  859. size_t tile_j,
  860. size_t tile_k,
  861. uint32_t flags);
  862. /**
  863. * Process items on a 4D grid.
  864. *
  865. * The function implements a parallel version of the following snippet:
  866. *
  867. * for (size_t i = 0; i < range_i; i++)
  868. * for (size_t j = 0; j < range_j; j++)
  869. * for (size_t k = 0; k < range_k; k++)
  870. * for (size_t l = 0; l < range_l; l++)
  871. * function(context, i, j, k, l);
  872. *
  873. * When the function returns, all items have been processed and the thread pool
  874. * is ready for a new task.
  875. *
  876. * @note If multiple threads call this function with the same thread pool, the
  877. * calls are serialized.
  878. *
  879. * @param threadpool the thread pool to use for parallelisation. If threadpool
  880. * is NULL, all items are processed serially on the calling thread.
  881. * @param function the function to call for each tile.
  882. * @param context the first argument passed to the specified function.
  883. * @param range_i the number of items to process along the first dimension
  884. * of the 4D grid.
  885. * @param range_j the number of items to process along the second dimension
  886. * of the 4D grid.
  887. * @param range_k the number of items to process along the third dimension
  888. * of the 4D grid.
  889. * @param range_l the number of items to process along the fourth dimension
  890. * of the 4D grid.
  891. * @param flags a bitwise combination of zero or more optional flags
  892. * (PTHREADPOOL_FLAG_DISABLE_DENORMALS or PTHREADPOOL_FLAG_YIELD_WORKERS)
  893. */
  894. void pthreadpool_parallelize_4d(
  895. pthreadpool_t threadpool,
  896. pthreadpool_task_4d_t function,
  897. void* context,
  898. size_t range_i,
  899. size_t range_j,
  900. size_t range_k,
  901. size_t range_l,
  902. uint32_t flags);
  903. /**
  904. * Process items on a 4D grid with the specified maximum tile size along the
  905. * last grid dimension.
  906. *
  907. * The function implements a parallel version of the following snippet:
  908. *
  909. * for (size_t i = 0; i < range_i; i++)
  910. * for (size_t j = 0; j < range_j; j++)
  911. * for (size_t k = 0; k < range_k; k++)
  912. * for (size_t l = 0; l < range_l; l += tile_l)
  913. * function(context, i, j, k, l, min(range_l - l, tile_l));
  914. *
  915. * When the function returns, all items have been processed and the thread pool
  916. * is ready for a new task.
  917. *
  918. * @note If multiple threads call this function with the same thread pool, the
  919. * calls are serialized.
  920. *
  921. * @param threadpool the thread pool to use for parallelisation. If threadpool
  922. * is NULL, all items are processed serially on the calling thread.
  923. * @param function the function to call for each tile.
  924. * @param context the first argument passed to the specified function.
  925. * @param range_i the number of items to process along the first dimension
  926. * of the 4D grid.
  927. * @param range_j the number of items to process along the second dimension
  928. * of the 4D grid.
  929. * @param range_k the number of items to process along the third dimension
  930. * of the 4D grid.
  931. * @param range_l the number of items to process along the fourth dimension
  932. * of the 4D grid.
  933. * @param tile_l the maximum number of items along the fourth dimension of
  934. * the 4D grid to process in one function call.
  935. * @param flags a bitwise combination of zero or more optional flags
  936. * (PTHREADPOOL_FLAG_DISABLE_DENORMALS or PTHREADPOOL_FLAG_YIELD_WORKERS)
  937. */
  938. void pthreadpool_parallelize_4d_tile_1d(
  939. pthreadpool_t threadpool,
  940. pthreadpool_task_4d_tile_1d_t function,
  941. void* context,
  942. size_t range_i,
  943. size_t range_j,
  944. size_t range_k,
  945. size_t range_l,
  946. size_t tile_l,
  947. uint32_t flags);
  948. /**
  949. * Process items on a 4D grid with the specified maximum tile size along the
  950. * last two grid dimensions.
  951. *
  952. * The function implements a parallel version of the following snippet:
  953. *
  954. * for (size_t i = 0; i < range_i; i++)
  955. * for (size_t j = 0; j < range_j; j++)
  956. * for (size_t k = 0; k < range_k; k += tile_k)
  957. * for (size_t l = 0; l < range_l; l += tile_l)
  958. * function(context, i, j, k, l,
  959. * min(range_k - k, tile_k), min(range_l - l, tile_l));
  960. *
  961. * When the function returns, all items have been processed and the thread pool
  962. * is ready for a new task.
  963. *
  964. * @note If multiple threads call this function with the same thread pool, the
  965. * calls are serialized.
  966. *
  967. * @param threadpool the thread pool to use for parallelisation. If threadpool
  968. * is NULL, all items are processed serially on the calling thread.
  969. * @param function the function to call for each tile.
  970. * @param context the first argument passed to the specified function.
  971. * @param range_i the number of items to process along the first dimension
  972. * of the 4D grid.
  973. * @param range_j the number of items to process along the second dimension
  974. * of the 4D grid.
  975. * @param range_k the number of items to process along the third dimension
  976. * of the 4D grid.
  977. * @param range_l the number of items to process along the fourth dimension
  978. * of the 4D grid.
  979. * @param tile_k the maximum number of items along the third dimension of
  980. * the 4D grid to process in one function call.
  981. * @param tile_l the maximum number of items along the fourth dimension of
  982. * the 4D grid to process in one function call.
  983. * @param flags a bitwise combination of zero or more optional flags
  984. * (PTHREADPOOL_FLAG_DISABLE_DENORMALS or PTHREADPOOL_FLAG_YIELD_WORKERS)
  985. */
  986. void pthreadpool_parallelize_4d_tile_2d(
  987. pthreadpool_t threadpool,
  988. pthreadpool_task_4d_tile_2d_t function,
  989. void* context,
  990. size_t range_i,
  991. size_t range_j,
  992. size_t range_k,
  993. size_t range_l,
  994. size_t tile_k,
  995. size_t tile_l,
  996. uint32_t flags);
  997. /**
  998. * Process items on a 4D grid with the specified maximum tile size along the
  999. * last two grid dimensions using a microarchitecture-aware task function.
  1000. *
  1001. * The function implements a parallel version of the following snippet:
  1002. *
  1003. * uint32_t uarch_index = cpuinfo_initialize() ?
  1004. * cpuinfo_get_current_uarch_index() : default_uarch_index;
  1005. * if (uarch_index > max_uarch_index) uarch_index = default_uarch_index;
  1006. * for (size_t i = 0; i < range_i; i++)
  1007. * for (size_t j = 0; j < range_j; j++)
  1008. * for (size_t k = 0; k < range_k; k += tile_k)
  1009. * for (size_t l = 0; l < range_l; l += tile_l)
  1010. * function(context, uarch_index, i, j, k, l,
  1011. * min(range_k - k, tile_k), min(range_l - l, tile_l));
  1012. *
  1013. * When the function returns, all items have been processed and the thread pool
  1014. * is ready for a new task.
  1015. *
  1016. * @note If multiple threads call this function with the same thread pool, the
  1017. * calls are serialized.
  1018. *
  1019. * @param threadpool the thread pool to use for parallelisation. If
  1020. * threadpool is NULL, all items are processed serially on the calling
  1021. * thread.
  1022. * @param function the function to call for each tile.
  1023. * @param context the first argument passed to the specified
  1024. * function.
  1025. * @param default_uarch_index the microarchitecture index to use when
  1026. * pthreadpool is configured without cpuinfo, cpuinfo initialization failed,
  1027. * or index returned by cpuinfo_get_current_uarch_index() exceeds the
  1028. * max_uarch_index value.
  1029. * @param max_uarch_index the maximum microarchitecture index expected by
  1030. * the specified function. If the index returned by
  1031. * cpuinfo_get_current_uarch_index() exceeds this value, default_uarch_index
  1032. * will be used instead. default_uarch_index can exceed max_uarch_index.
  1033. * @param range_i the number of items to process along the first
  1034. * dimension of the 4D grid.
  1035. * @param range_j the number of items to process along the second
  1036. * dimension of the 4D grid.
  1037. * @param range_k the number of items to process along the third
  1038. * dimension of the 4D grid.
  1039. * @param range_l the number of items to process along the fourth
  1040. * dimension of the 4D grid.
  1041. * @param tile_k the maximum number of items along the third
  1042. * dimension of the 4D grid to process in one function call.
  1043. * @param tile_l the maximum number of items along the fourth
  1044. * dimension of the 4D grid to process in one function call.
  1045. * @param flags a bitwise combination of zero or more optional
  1046. * flags (PTHREADPOOL_FLAG_DISABLE_DENORMALS or
  1047. * PTHREADPOOL_FLAG_YIELD_WORKERS)
  1048. */
  1049. void pthreadpool_parallelize_4d_tile_2d_with_uarch(
  1050. pthreadpool_t threadpool,
  1051. pthreadpool_task_4d_tile_2d_with_id_t function,
  1052. void* context,
  1053. uint32_t default_uarch_index,
  1054. uint32_t max_uarch_index,
  1055. size_t range_i,
  1056. size_t range_j,
  1057. size_t range_k,
  1058. size_t range_l,
  1059. size_t tile_k,
  1060. size_t tile_l,
  1061. uint32_t flags);
  1062. /**
  1063. * Process items on a 5D grid.
  1064. *
  1065. * The function implements a parallel version of the following snippet:
  1066. *
  1067. * for (size_t i = 0; i < range_i; i++)
  1068. * for (size_t j = 0; j < range_j; j++)
  1069. * for (size_t k = 0; k < range_k; k++)
  1070. * for (size_t l = 0; l < range_l; l++)
  1071. * for (size_t m = 0; m < range_m; m++)
  1072. * function(context, i, j, k, l, m);
  1073. *
  1074. * When the function returns, all items have been processed and the thread pool
  1075. * is ready for a new task.
  1076. *
  1077. * @note If multiple threads call this function with the same thread pool, the
  1078. * calls are serialized.
  1079. *
  1080. * @param threadpool the thread pool to use for parallelisation. If threadpool
  1081. * is NULL, all items are processed serially on the calling thread.
  1082. * @param function the function to call for each tile.
  1083. * @param context the first argument passed to the specified function.
  1084. * @param range_i the number of items to process along the first dimension
  1085. * of the 5D grid.
  1086. * @param range_j the number of items to process along the second dimension
  1087. * of the 5D grid.
  1088. * @param range_k the number of items to process along the third dimension
  1089. * of the 5D grid.
  1090. * @param range_l the number of items to process along the fourth dimension
  1091. * of the 5D grid.
  1092. * @param range_m the number of items to process along the fifth dimension
  1093. * of the 5D grid.
  1094. * @param flags a bitwise combination of zero or more optional flags
  1095. * (PTHREADPOOL_FLAG_DISABLE_DENORMALS or PTHREADPOOL_FLAG_YIELD_WORKERS)
  1096. */
  1097. void pthreadpool_parallelize_5d(
  1098. pthreadpool_t threadpool,
  1099. pthreadpool_task_5d_t function,
  1100. void* context,
  1101. size_t range_i,
  1102. size_t range_j,
  1103. size_t range_k,
  1104. size_t range_l,
  1105. size_t range_m,
  1106. uint32_t flags);
  1107. /**
  1108. * Process items on a 5D grid with the specified maximum tile size along the
  1109. * last grid dimension.
  1110. *
  1111. * The function implements a parallel version of the following snippet:
  1112. *
  1113. * for (size_t i = 0; i < range_i; i++)
  1114. * for (size_t j = 0; j < range_j; j++)
  1115. * for (size_t k = 0; k < range_k; k++)
  1116. * for (size_t l = 0; l < range_l; l++)
  1117. * for (size_t m = 0; m < range_m; m += tile_m)
  1118. * function(context, i, j, k, l, m, min(range_m - m, tile_m));
  1119. *
  1120. * When the function returns, all items have been processed and the thread pool
  1121. * is ready for a new task.
  1122. *
  1123. * @note If multiple threads call this function with the same thread pool, the
  1124. * calls are serialized.
  1125. *
  1126. * @param threadpool the thread pool to use for parallelisation. If threadpool
  1127. * is NULL, all items are processed serially on the calling thread.
  1128. * @param function the function to call for each tile.
  1129. * @param context the first argument passed to the specified function.
  1130. * @param range_i the number of items to process along the first dimension
  1131. * of the 5D grid.
  1132. * @param range_j the number of items to process along the second dimension
  1133. * of the 5D grid.
  1134. * @param range_k the number of items to process along the third dimension
  1135. * of the 5D grid.
  1136. * @param range_l the number of items to process along the fourth dimension
  1137. * of the 5D grid.
  1138. * @param range_m the number of items to process along the fifth dimension
  1139. * of the 5D grid.
  1140. * @param tile_m the maximum number of items along the fifth dimension of
  1141. * the 5D grid to process in one function call.
  1142. * @param flags a bitwise combination of zero or more optional flags
  1143. * (PTHREADPOOL_FLAG_DISABLE_DENORMALS or PTHREADPOOL_FLAG_YIELD_WORKERS)
  1144. */
  1145. void pthreadpool_parallelize_5d_tile_1d(
  1146. pthreadpool_t threadpool,
  1147. pthreadpool_task_5d_tile_1d_t function,
  1148. void* context,
  1149. size_t range_i,
  1150. size_t range_j,
  1151. size_t range_k,
  1152. size_t range_l,
  1153. size_t range_m,
  1154. size_t tile_m,
  1155. uint32_t flags);
  1156. /**
  1157. * Process items on a 5D grid with the specified maximum tile size along the
  1158. * last two grid dimensions.
  1159. *
  1160. * The function implements a parallel version of the following snippet:
  1161. *
  1162. * for (size_t i = 0; i < range_i; i++)
  1163. * for (size_t j = 0; j < range_j; j++)
  1164. * for (size_t k = 0; k < range_k; k++)
  1165. * for (size_t l = 0; l < range_l; l += tile_l)
  1166. * for (size_t m = 0; m < range_m; m += tile_m)
  1167. * function(context, i, j, k, l, m,
  1168. * min(range_l - l, tile_l), min(range_m - m, tile_m));
  1169. *
  1170. * When the function returns, all items have been processed and the thread pool
  1171. * is ready for a new task.
  1172. *
  1173. * @note If multiple threads call this function with the same thread pool, the
  1174. * calls are serialized.
  1175. *
  1176. * @param threadpool the thread pool to use for parallelisation. If threadpool
  1177. * is NULL, all items are processed serially on the calling thread.
  1178. * @param function the function to call for each tile.
  1179. * @param context the first argument passed to the specified function.
  1180. * @param range_i the number of items to process along the first dimension
  1181. * of the 5D grid.
  1182. * @param range_j the number of items to process along the second dimension
  1183. * of the 5D grid.
  1184. * @param range_k the number of items to process along the third dimension
  1185. * of the 5D grid.
  1186. * @param range_l the number of items to process along the fourth dimension
  1187. * of the 5D grid.
  1188. * @param range_m the number of items to process along the fifth dimension
  1189. * of the 5D grid.
  1190. * @param tile_l the maximum number of items along the fourth dimension of
  1191. * the 5D grid to process in one function call.
  1192. * @param tile_m the maximum number of items along the fifth dimension of
  1193. * the 5D grid to process in one function call.
  1194. * @param flags a bitwise combination of zero or more optional flags
  1195. * (PTHREADPOOL_FLAG_DISABLE_DENORMALS or PTHREADPOOL_FLAG_YIELD_WORKERS)
  1196. */
  1197. void pthreadpool_parallelize_5d_tile_2d(
  1198. pthreadpool_t threadpool,
  1199. pthreadpool_task_5d_tile_2d_t function,
  1200. void* context,
  1201. size_t range_i,
  1202. size_t range_j,
  1203. size_t range_k,
  1204. size_t range_l,
  1205. size_t range_m,
  1206. size_t tile_l,
  1207. size_t tile_m,
  1208. uint32_t flags);
  1209. /**
  1210. * Process items on a 6D grid.
  1211. *
  1212. * The function implements a parallel version of the following snippet:
  1213. *
  1214. * for (size_t i = 0; i < range_i; i++)
  1215. * for (size_t j = 0; j < range_j; j++)
  1216. * for (size_t k = 0; k < range_k; k++)
  1217. * for (size_t l = 0; l < range_l; l++)
  1218. * for (size_t m = 0; m < range_m; m++)
  1219. * for (size_t n = 0; n < range_n; n++)
  1220. * function(context, i, j, k, l, m, n);
  1221. *
  1222. * When the function returns, all items have been processed and the thread pool
  1223. * is ready for a new task.
  1224. *
  1225. * @note If multiple threads call this function with the same thread pool, the
  1226. * calls are serialized.
  1227. *
  1228. * @param threadpool the thread pool to use for parallelisation. If threadpool
  1229. * is NULL, all items are processed serially on the calling thread.
  1230. * @param function the function to call for each tile.
  1231. * @param context the first argument passed to the specified function.
  1232. * @param range_i the number of items to process along the first dimension
  1233. * of the 6D grid.
  1234. * @param range_j the number of items to process along the second dimension
  1235. * of the 6D grid.
  1236. * @param range_k the number of items to process along the third dimension
  1237. * of the 6D grid.
  1238. * @param range_l the number of items to process along the fourth dimension
  1239. * of the 6D grid.
  1240. * @param range_m the number of items to process along the fifth dimension
  1241. * of the 6D grid.
  1242. * @param range_n the number of items to process along the sixth dimension
  1243. * of the 6D grid.
  1244. * @param tile_n the maximum number of items along the sixth dimension of
  1245. * the 6D grid to process in one function call.
  1246. * @param flags a bitwise combination of zero or more optional flags
  1247. * (PTHREADPOOL_FLAG_DISABLE_DENORMALS or PTHREADPOOL_FLAG_YIELD_WORKERS)
  1248. */
  1249. void pthreadpool_parallelize_6d(
  1250. pthreadpool_t threadpool,
  1251. pthreadpool_task_6d_t function,
  1252. void* context,
  1253. size_t range_i,
  1254. size_t range_j,
  1255. size_t range_k,
  1256. size_t range_l,
  1257. size_t range_m,
  1258. size_t range_n,
  1259. uint32_t flags);
  1260. /**
  1261. * Process items on a 6D grid with the specified maximum tile size along the
  1262. * last grid dimension.
  1263. *
  1264. * The function implements a parallel version of the following snippet:
  1265. *
  1266. * for (size_t i = 0; i < range_i; i++)
  1267. * for (size_t j = 0; j < range_j; j++)
  1268. * for (size_t k = 0; k < range_k; k++)
  1269. * for (size_t l = 0; l < range_l; l++)
  1270. * for (size_t m = 0; m < range_m; m++)
  1271. * for (size_t n = 0; n < range_n; n += tile_n)
  1272. * function(context, i, j, k, l, m, n, min(range_n - n, tile_n));
  1273. *
  1274. * When the function returns, all items have been processed and the thread pool
  1275. * is ready for a new task.
  1276. *
  1277. * @note If multiple threads call this function with the same thread pool, the
  1278. * calls are serialized.
  1279. *
  1280. * @param threadpool the thread pool to use for parallelisation. If threadpool
  1281. * is NULL, all items are processed serially on the calling thread.
  1282. * @param function the function to call for each tile.
  1283. * @param context the first argument passed to the specified function.
  1284. * @param range_i the number of items to process along the first dimension
  1285. * of the 6D grid.
  1286. * @param range_j the number of items to process along the second dimension
  1287. * of the 6D grid.
  1288. * @param range_k the number of items to process along the third dimension
  1289. * of the 6D grid.
  1290. * @param range_l the number of items to process along the fourth dimension
  1291. * of the 6D grid.
  1292. * @param range_m the number of items to process along the fifth dimension
  1293. * of the 6D grid.
  1294. * @param range_n the number of items to process along the sixth dimension
  1295. * of the 6D grid.
  1296. * @param tile_n the maximum number of items along the sixth dimension of
  1297. * the 6D grid to process in one function call.
  1298. * @param flags a bitwise combination of zero or more optional flags
  1299. * (PTHREADPOOL_FLAG_DISABLE_DENORMALS or PTHREADPOOL_FLAG_YIELD_WORKERS)
  1300. */
  1301. void pthreadpool_parallelize_6d_tile_1d(
  1302. pthreadpool_t threadpool,
  1303. pthreadpool_task_6d_tile_1d_t function,
  1304. void* context,
  1305. size_t range_i,
  1306. size_t range_j,
  1307. size_t range_k,
  1308. size_t range_l,
  1309. size_t range_m,
  1310. size_t range_n,
  1311. size_t tile_n,
  1312. uint32_t flags);
  1313. /**
  1314. * Process items on a 6D grid with the specified maximum tile size along the
  1315. * last two grid dimensions.
  1316. *
  1317. * The function implements a parallel version of the following snippet:
  1318. *
  1319. * for (size_t i = 0; i < range_i; i++)
  1320. * for (size_t j = 0; j < range_j; j++)
  1321. * for (size_t k = 0; k < range_k; k++)
  1322. * for (size_t l = 0; l < range_l; l++)
  1323. * for (size_t m = 0; m < range_m; m += tile_m)
  1324. * for (size_t n = 0; n < range_n; n += tile_n)
  1325. * function(context, i, j, k, l, m, n,
  1326. * min(range_m - m, tile_m), min(range_n - n, tile_n));
  1327. *
  1328. * When the function returns, all items have been processed and the thread pool
  1329. * is ready for a new task.
  1330. *
  1331. * @note If multiple threads call this function with the same thread pool, the
  1332. * calls are serialized.
  1333. *
  1334. * @param threadpool the thread pool to use for parallelisation. If threadpool
  1335. * is NULL, all items are processed serially on the calling thread.
  1336. * @param function the function to call for each tile.
  1337. * @param context the first argument passed to the specified function.
  1338. * @param range_i the number of items to process along the first dimension
  1339. * of the 6D grid.
  1340. * @param range_j the number of items to process along the second dimension
  1341. * of the 6D grid.
  1342. * @param range_k the number of items to process along the third dimension
  1343. * of the 6D grid.
  1344. * @param range_l the number of items to process along the fourth dimension
  1345. * of the 6D grid.
  1346. * @param range_m the number of items to process along the fifth dimension
  1347. * of the 6D grid.
  1348. * @param range_n the number of items to process along the sixth dimension
  1349. * of the 6D grid.
  1350. * @param tile_m the maximum number of items along the fifth dimension of
  1351. * the 6D grid to process in one function call.
  1352. * @param tile_n the maximum number of items along the sixth dimension of
  1353. * the 6D grid to process in one function call.
  1354. * @param flags a bitwise combination of zero or more optional flags
  1355. * (PTHREADPOOL_FLAG_DISABLE_DENORMALS or PTHREADPOOL_FLAG_YIELD_WORKERS)
  1356. */
  1357. void pthreadpool_parallelize_6d_tile_2d(
  1358. pthreadpool_t threadpool,
  1359. pthreadpool_task_6d_tile_2d_t function,
  1360. void* context,
  1361. size_t range_i,
  1362. size_t range_j,
  1363. size_t range_k,
  1364. size_t range_l,
  1365. size_t range_m,
  1366. size_t range_n,
  1367. size_t tile_m,
  1368. size_t tile_n,
  1369. uint32_t flags);
  1370. /**
  1371. * Terminates threads in the thread pool and releases associated resources.
  1372. *
  1373. * @warning Accessing the thread pool after a call to this function constitutes
  1374. * undefined behaviour and may cause data corruption.
  1375. *
  1376. * @param[in,out] threadpool The thread pool to destroy.
  1377. */
  1378. void pthreadpool_destroy(pthreadpool_t threadpool);
  1379. #ifndef PTHREADPOOL_NO_DEPRECATED_API
  1380. /* Legacy API for compatibility with pre-existing users (e.g. NNPACK) */
  1381. #if defined(__GNUC__)
  1382. #define PTHREADPOOL_DEPRECATED __attribute__((__deprecated__))
  1383. #else
  1384. #define PTHREADPOOL_DEPRECATED
  1385. #endif
  1386. typedef void (*pthreadpool_function_1d_t)(void*, size_t);
  1387. typedef void (*pthreadpool_function_1d_tiled_t)(void*, size_t, size_t);
  1388. typedef void (*pthreadpool_function_2d_t)(void*, size_t, size_t);
  1389. typedef void (*pthreadpool_function_2d_tiled_t)(void*, size_t, size_t, size_t, size_t);
  1390. typedef void (*pthreadpool_function_3d_tiled_t)(void*, size_t, size_t, size_t, size_t, size_t, size_t);
  1391. typedef void (*pthreadpool_function_4d_tiled_t)(void*, size_t, size_t, size_t, size_t, size_t, size_t, size_t, size_t);
  1392. void pthreadpool_compute_1d(
  1393. pthreadpool_t threadpool,
  1394. pthreadpool_function_1d_t function,
  1395. void* argument,
  1396. size_t range) PTHREADPOOL_DEPRECATED;
  1397. void pthreadpool_compute_1d_tiled(
  1398. pthreadpool_t threadpool,
  1399. pthreadpool_function_1d_tiled_t function,
  1400. void* argument,
  1401. size_t range,
  1402. size_t tile) PTHREADPOOL_DEPRECATED;
  1403. void pthreadpool_compute_2d(
  1404. pthreadpool_t threadpool,
  1405. pthreadpool_function_2d_t function,
  1406. void* argument,
  1407. size_t range_i,
  1408. size_t range_j) PTHREADPOOL_DEPRECATED;
  1409. void pthreadpool_compute_2d_tiled(
  1410. pthreadpool_t threadpool,
  1411. pthreadpool_function_2d_tiled_t function,
  1412. void* argument,
  1413. size_t range_i,
  1414. size_t range_j,
  1415. size_t tile_i,
  1416. size_t tile_j) PTHREADPOOL_DEPRECATED;
  1417. void pthreadpool_compute_3d_tiled(
  1418. pthreadpool_t threadpool,
  1419. pthreadpool_function_3d_tiled_t function,
  1420. void* argument,
  1421. size_t range_i,
  1422. size_t range_j,
  1423. size_t range_k,
  1424. size_t tile_i,
  1425. size_t tile_j,
  1426. size_t tile_k) PTHREADPOOL_DEPRECATED;
  1427. void pthreadpool_compute_4d_tiled(
  1428. pthreadpool_t threadpool,
  1429. pthreadpool_function_4d_tiled_t function,
  1430. void* argument,
  1431. size_t range_i,
  1432. size_t range_j,
  1433. size_t range_k,
  1434. size_t range_l,
  1435. size_t tile_i,
  1436. size_t tile_j,
  1437. size_t tile_k,
  1438. size_t tile_l) PTHREADPOOL_DEPRECATED;
  1439. #endif /* PTHREADPOOL_NO_DEPRECATED_API */
  1440. #ifdef __cplusplus
  1441. } /* extern "C" */
  1442. #endif
  1443. #ifdef __cplusplus
  1444. namespace libpthreadpool {
  1445. namespace detail {
  1446. namespace {
  1447. template<class T>
  1448. void call_wrapper_1d(void* arg, size_t i) {
  1449. (*static_cast<const T*>(arg))(i);
  1450. }
  1451. template<class T>
  1452. void call_wrapper_1d_tile_1d(void* arg, size_t range_i, size_t tile_i) {
  1453. (*static_cast<const T*>(arg))(range_i, tile_i);
  1454. }
  1455. template<class T>
  1456. void call_wrapper_2d(void* functor, size_t i, size_t j) {
  1457. (*static_cast<const T*>(functor))(i, j);
  1458. }
  1459. template<class T>
  1460. void call_wrapper_2d_tile_1d(void* functor,
  1461. size_t i, size_t range_j, size_t tile_j)
  1462. {
  1463. (*static_cast<const T*>(functor))(i, range_j, tile_j);
  1464. }
  1465. template<class T>
  1466. void call_wrapper_2d_tile_2d(void* functor,
  1467. size_t range_i, size_t range_j,
  1468. size_t tile_i, size_t tile_j)
  1469. {
  1470. (*static_cast<const T*>(functor))(range_i, range_j, tile_i, tile_j);
  1471. }
  1472. template<class T>
  1473. void call_wrapper_3d(void* functor, size_t i, size_t j, size_t k) {
  1474. (*static_cast<const T*>(functor))(i, j, k);
  1475. }
  1476. template<class T>
  1477. void call_wrapper_3d_tile_1d(void* functor,
  1478. size_t i, size_t j, size_t range_k,
  1479. size_t tile_k)
  1480. {
  1481. (*static_cast<const T*>(functor))(i, j, range_k, tile_k);
  1482. }
  1483. template<class T>
  1484. void call_wrapper_3d_tile_2d(void* functor,
  1485. size_t i, size_t range_j, size_t range_k,
  1486. size_t tile_j, size_t tile_k)
  1487. {
  1488. (*static_cast<const T*>(functor))(i, range_j, range_k, tile_j, tile_k);
  1489. }
  1490. template<class T>
  1491. void call_wrapper_4d(void* functor, size_t i, size_t j, size_t k, size_t l) {
  1492. (*static_cast<const T*>(functor))(i, j, k, l);
  1493. }
  1494. template<class T>
  1495. void call_wrapper_4d_tile_1d(void* functor,
  1496. size_t i, size_t j, size_t k, size_t range_l,
  1497. size_t tile_l)
  1498. {
  1499. (*static_cast<const T*>(functor))(i, j, k, range_l, tile_l);
  1500. }
  1501. template<class T>
  1502. void call_wrapper_4d_tile_2d(void* functor,
  1503. size_t i, size_t j, size_t range_k, size_t range_l,
  1504. size_t tile_k, size_t tile_l)
  1505. {
  1506. (*static_cast<const T*>(functor))(i, j, range_k, range_l, tile_k, tile_l);
  1507. }
  1508. template<class T>
  1509. void call_wrapper_5d(void* functor, size_t i, size_t j, size_t k, size_t l, size_t m) {
  1510. (*static_cast<const T*>(functor))(i, j, k, l, m);
  1511. }
  1512. template<class T>
  1513. void call_wrapper_5d_tile_1d(void* functor,
  1514. size_t i, size_t j, size_t k, size_t l, size_t range_m,
  1515. size_t tile_m)
  1516. {
  1517. (*static_cast<const T*>(functor))(i, j, k, l, range_m, tile_m);
  1518. }
  1519. template<class T>
  1520. void call_wrapper_5d_tile_2d(void* functor,
  1521. size_t i, size_t j, size_t k, size_t range_l, size_t range_m,
  1522. size_t tile_l, size_t tile_m)
  1523. {
  1524. (*static_cast<const T*>(functor))(i, j, k, range_l, range_m, tile_l, tile_m);
  1525. }
  1526. template<class T>
  1527. void call_wrapper_6d(void* functor, size_t i, size_t j, size_t k, size_t l, size_t m, size_t n) {
  1528. (*static_cast<const T*>(functor))(i, j, k, l, m, n);
  1529. }
  1530. template<class T>
  1531. void call_wrapper_6d_tile_1d(void* functor,
  1532. size_t i, size_t j, size_t k, size_t l, size_t m, size_t range_n,
  1533. size_t tile_n)
  1534. {
  1535. (*static_cast<const T*>(functor))(i, j, k, l, m, range_n, tile_n);
  1536. }
  1537. template<class T>
  1538. void call_wrapper_6d_tile_2d(void* functor,
  1539. size_t i, size_t j, size_t k, size_t l, size_t range_m, size_t range_n,
  1540. size_t tile_m, size_t tile_n)
  1541. {
  1542. (*static_cast<const T*>(functor))(i, j, k, l, range_m, range_n, tile_m, tile_n);
  1543. }
  1544. } /* namespace */
  1545. } /* namespace detail */
  1546. } /* namespace libpthreadpool */
  1547. /**
  1548. * Process items on a 1D grid.
  1549. *
  1550. * The function implements a parallel version of the following snippet:
  1551. *
  1552. * for (size_t i = 0; i < range; i++)
  1553. * functor(i);
  1554. *
  1555. * When the function returns, all items have been processed and the thread pool
  1556. * is ready for a new task.
  1557. *
  1558. * @note If multiple threads call this function with the same thread pool, the
  1559. * calls are serialized.
  1560. *
  1561. * @param threadpool the thread pool to use for parallelisation. If threadpool
  1562. * is NULL, all items are processed serially on the calling thread.
  1563. * @param functor the functor to call for each item.
  1564. * @param range the number of items on the 1D grid to process. The
  1565. * specified functor will be called once for each item.
  1566. * @param flags a bitwise combination of zero or more optional flags
  1567. * (PTHREADPOOL_FLAG_DISABLE_DENORMALS or PTHREADPOOL_FLAG_YIELD_WORKERS)
  1568. */
  1569. template<class T>
  1570. inline void pthreadpool_parallelize_1d(
  1571. pthreadpool_t threadpool,
  1572. const T& functor,
  1573. size_t range,
  1574. uint32_t flags = 0)
  1575. {
  1576. pthreadpool_parallelize_1d(
  1577. threadpool,
  1578. &libpthreadpool::detail::call_wrapper_1d<const T>,
  1579. const_cast<void*>(static_cast<const void*>(&functor)),
  1580. range,
  1581. flags);
  1582. }
  1583. /**
  1584. * Process items on a 1D grid with specified maximum tile size.
  1585. *
  1586. * The function implements a parallel version of the following snippet:
  1587. *
  1588. * for (size_t i = 0; i < range; i += tile)
  1589. * functor(i, min(range - i, tile));
  1590. *
  1591. * When the call returns, all items have been processed and the thread pool is
  1592. * ready for a new task.
  1593. *
  1594. * @note If multiple threads call this function with the same thread pool,
  1595. * the calls are serialized.
  1596. *
  1597. * @param threadpool the thread pool to use for parallelisation. If threadpool
  1598. * is NULL, all items are processed serially on the calling thread.
  1599. * @param functor the functor to call for each tile.
  1600. * @param range the number of items on the 1D grid to process.
  1601. * @param tile the maximum number of items on the 1D grid to process in
  1602. * one functor call.
  1603. * @param flags a bitwise combination of zero or more optional flags
  1604. * (PTHREADPOOL_FLAG_DISABLE_DENORMALS or PTHREADPOOL_FLAG_YIELD_WORKERS)
  1605. */
  1606. template<class T>
  1607. inline void pthreadpool_parallelize_1d_tile_1d(
  1608. pthreadpool_t threadpool,
  1609. const T& functor,
  1610. size_t range,
  1611. size_t tile,
  1612. uint32_t flags = 0)
  1613. {
  1614. pthreadpool_parallelize_1d_tile_1d(
  1615. threadpool,
  1616. &libpthreadpool::detail::call_wrapper_1d_tile_1d<const T>,
  1617. const_cast<void*>(static_cast<const void*>(&functor)),
  1618. range,
  1619. tile,
  1620. flags);
  1621. }
  1622. /**
  1623. * Process items on a 2D grid.
  1624. *
  1625. * The function implements a parallel version of the following snippet:
  1626. *
  1627. * for (size_t i = 0; i < range_i; i++)
  1628. * for (size_t j = 0; j < range_j; j++)
  1629. * functor(i, j);
  1630. *
  1631. * When the function returns, all items have been processed and the thread pool
  1632. * is ready for a new task.
  1633. *
  1634. * @note If multiple threads call this function with the same thread pool, the
  1635. * calls are serialized.
  1636. *
  1637. * @param threadpool the thread pool to use for parallelisation. If threadpool
  1638. * is NULL, all items are processed serially on the calling thread.
  1639. * @param functor the functor to call for each item.
  1640. * @param range_i the number of items to process along the first dimension
  1641. * of the 2D grid.
  1642. * @param range_j the number of items to process along the second dimension
  1643. * of the 2D grid.
  1644. * @param flags a bitwise combination of zero or more optional flags
  1645. * (PTHREADPOOL_FLAG_DISABLE_DENORMALS or PTHREADPOOL_FLAG_YIELD_WORKERS)
  1646. */
  1647. template<class T>
  1648. inline void pthreadpool_parallelize_2d(
  1649. pthreadpool_t threadpool,
  1650. const T& functor,
  1651. size_t range_i,
  1652. size_t range_j,
  1653. uint32_t flags = 0)
  1654. {
  1655. pthreadpool_parallelize_2d(
  1656. threadpool,
  1657. &libpthreadpool::detail::call_wrapper_2d<const T>,
  1658. const_cast<void*>(static_cast<const void*>(&functor)),
  1659. range_i,
  1660. range_j,
  1661. flags);
  1662. }
  1663. /**
  1664. * Process items on a 2D grid with the specified maximum tile size along the
  1665. * last grid dimension.
  1666. *
  1667. * The function implements a parallel version of the following snippet:
  1668. *
  1669. * for (size_t i = 0; i < range_i; i++)
  1670. * for (size_t j = 0; j < range_j; j += tile_j)
  1671. * functor(i, j, min(range_j - j, tile_j));
  1672. *
  1673. * When the function returns, all items have been processed and the thread pool
  1674. * is ready for a new task.
  1675. *
  1676. * @note If multiple threads call this function with the same thread pool, the
  1677. * calls are serialized.
  1678. *
  1679. * @param threadpool the thread pool to use for parallelisation. If threadpool
  1680. * is NULL, all items are processed serially on the calling thread.
  1681. * @param functor the functor to call for each tile.
  1682. * @param range_i the number of items to process along the first dimension
  1683. * of the 2D grid.
  1684. * @param range_j the number of items to process along the second dimension
  1685. * of the 2D grid.
  1686. * @param tile_j the maximum number of items along the second dimension of
  1687. * the 2D grid to process in one functor call.
  1688. * @param flags a bitwise combination of zero or more optional flags
  1689. * (PTHREADPOOL_FLAG_DISABLE_DENORMALS or PTHREADPOOL_FLAG_YIELD_WORKERS)
  1690. */
  1691. template<class T>
  1692. inline void pthreadpool_parallelize_2d_tile_1d(
  1693. pthreadpool_t threadpool,
  1694. const T& functor,
  1695. size_t range_i,
  1696. size_t range_j,
  1697. size_t tile_j,
  1698. uint32_t flags = 0)
  1699. {
  1700. pthreadpool_parallelize_2d_tile_1d(
  1701. threadpool,
  1702. &libpthreadpool::detail::call_wrapper_2d_tile_1d<const T>,
  1703. const_cast<void*>(static_cast<const void*>(&functor)),
  1704. range_i,
  1705. range_j,
  1706. tile_j,
  1707. flags);
  1708. }
  1709. /**
  1710. * Process items on a 2D grid with the specified maximum tile size along each
  1711. * grid dimension.
  1712. *
  1713. * The function implements a parallel version of the following snippet:
  1714. *
  1715. * for (size_t i = 0; i < range_i; i += tile_i)
  1716. * for (size_t j = 0; j < range_j; j += tile_j)
  1717. * functor(i, j,
  1718. * min(range_i - i, tile_i), min(range_j - j, tile_j));
  1719. *
  1720. * When the function returns, all items have been processed and the thread pool
  1721. * is ready for a new task.
  1722. *
  1723. * @note If multiple threads call this function with the same thread pool, the
  1724. * calls are serialized.
  1725. *
  1726. * @param threadpool the thread pool to use for parallelisation. If threadpool
  1727. * is NULL, all items are processed serially on the calling thread.
  1728. * @param functor the functor to call for each tile.
  1729. * @param range_i the number of items to process along the first dimension
  1730. * of the 2D grid.
  1731. * @param range_j the number of items to process along the second dimension
  1732. * of the 2D grid.
  1733. * @param tile_j the maximum number of items along the first dimension of
  1734. * the 2D grid to process in one functor call.
  1735. * @param tile_j the maximum number of items along the second dimension of
  1736. * the 2D grid to process in one functor call.
  1737. * @param flags a bitwise combination of zero or more optional flags
  1738. * (PTHREADPOOL_FLAG_DISABLE_DENORMALS or PTHREADPOOL_FLAG_YIELD_WORKERS)
  1739. */
  1740. template<class T>
  1741. inline void pthreadpool_parallelize_2d_tile_2d(
  1742. pthreadpool_t threadpool,
  1743. const T& functor,
  1744. size_t range_i,
  1745. size_t range_j,
  1746. size_t tile_i,
  1747. size_t tile_j,
  1748. uint32_t flags = 0)
  1749. {
  1750. pthreadpool_parallelize_2d_tile_2d(
  1751. threadpool,
  1752. &libpthreadpool::detail::call_wrapper_2d_tile_2d<const T>,
  1753. const_cast<void*>(static_cast<const void*>(&functor)),
  1754. range_i,
  1755. range_j,
  1756. tile_i,
  1757. tile_j,
  1758. flags);
  1759. }
  1760. /**
  1761. * Process items on a 3D grid.
  1762. *
  1763. * The function implements a parallel version of the following snippet:
  1764. *
  1765. * for (size_t i = 0; i < range_i; i++)
  1766. * for (size_t j = 0; j < range_j; j++)
  1767. * for (size_t k = 0; k < range_k; k++)
  1768. * functor(i, j, k);
  1769. *
  1770. * When the function returns, all items have been processed and the thread pool
  1771. * is ready for a new task.
  1772. *
  1773. * @note If multiple threads call this function with the same thread pool, the
  1774. * calls are serialized.
  1775. *
  1776. * @param threadpool the thread pool to use for parallelisation. If threadpool
  1777. * is NULL, all items are processed serially on the calling thread.
  1778. * @param functor the functor to call for each tile.
  1779. * @param range_i the number of items to process along the first dimension
  1780. * of the 3D grid.
  1781. * @param range_j the number of items to process along the second dimension
  1782. * of the 3D grid.
  1783. * @param range_k the number of items to process along the third dimension
  1784. * of the 3D grid.
  1785. * @param flags a bitwise combination of zero or more optional flags
  1786. * (PTHREADPOOL_FLAG_DISABLE_DENORMALS or PTHREADPOOL_FLAG_YIELD_WORKERS)
  1787. */
  1788. template<class T>
  1789. inline void pthreadpool_parallelize_3d(
  1790. pthreadpool_t threadpool,
  1791. const T& functor,
  1792. size_t range_i,
  1793. size_t range_j,
  1794. size_t range_k,
  1795. uint32_t flags = 0)
  1796. {
  1797. pthreadpool_parallelize_3d(
  1798. threadpool,
  1799. &libpthreadpool::detail::call_wrapper_3d<const T>,
  1800. const_cast<void*>(static_cast<const void*>(&functor)),
  1801. range_i,
  1802. range_j,
  1803. range_k,
  1804. flags);
  1805. }
  1806. /**
  1807. * Process items on a 3D grid with the specified maximum tile size along the
  1808. * last grid dimension.
  1809. *
  1810. * The function implements a parallel version of the following snippet:
  1811. *
  1812. * for (size_t i = 0; i < range_i; i++)
  1813. * for (size_t j = 0; j < range_j; j++)
  1814. * for (size_t k = 0; k < range_k; k += tile_k)
  1815. * functor(i, j, k, min(range_k - k, tile_k));
  1816. *
  1817. * When the function returns, all items have been processed and the thread pool
  1818. * is ready for a new task.
  1819. *
  1820. * @note If multiple threads call this function with the same thread pool, the
  1821. * calls are serialized.
  1822. *
  1823. * @param threadpool the thread pool to use for parallelisation. If threadpool
  1824. * is NULL, all items are processed serially on the calling thread.
  1825. * @param functor the functor to call for each tile.
  1826. * @param range_i the number of items to process along the first dimension
  1827. * of the 3D grid.
  1828. * @param range_j the number of items to process along the second dimension
  1829. * of the 3D grid.
  1830. * @param range_k the number of items to process along the third dimension
  1831. * of the 3D grid.
  1832. * @param tile_k the maximum number of items along the third dimension of
  1833. * the 3D grid to process in one functor call.
  1834. * @param flags a bitwise combination of zero or more optional flags
  1835. * (PTHREADPOOL_FLAG_DISABLE_DENORMALS or PTHREADPOOL_FLAG_YIELD_WORKERS)
  1836. */
  1837. template<class T>
  1838. inline void pthreadpool_parallelize_3d_tile_1d(
  1839. pthreadpool_t threadpool,
  1840. const T& functor,
  1841. size_t range_i,
  1842. size_t range_j,
  1843. size_t range_k,
  1844. size_t tile_k,
  1845. uint32_t flags = 0)
  1846. {
  1847. pthreadpool_parallelize_3d_tile_1d(
  1848. threadpool,
  1849. &libpthreadpool::detail::call_wrapper_3d_tile_1d<const T>,
  1850. const_cast<void*>(static_cast<const void*>(&functor)),
  1851. range_i,
  1852. range_j,
  1853. range_k,
  1854. tile_k,
  1855. flags);
  1856. }
  1857. /**
  1858. * Process items on a 3D grid with the specified maximum tile size along the
  1859. * last two grid dimensions.
  1860. *
  1861. * The function implements a parallel version of the following snippet:
  1862. *
  1863. * for (size_t i = 0; i < range_i; i++)
  1864. * for (size_t j = 0; j < range_j; j += tile_j)
  1865. * for (size_t k = 0; k < range_k; k += tile_k)
  1866. * functor(i, j, k,
  1867. * min(range_j - j, tile_j), min(range_k - k, tile_k));
  1868. *
  1869. * When the function returns, all items have been processed and the thread pool
  1870. * is ready for a new task.
  1871. *
  1872. * @note If multiple threads call this function with the same thread pool, the
  1873. * calls are serialized.
  1874. *
  1875. * @param threadpool the thread pool to use for parallelisation. If threadpool
  1876. * is NULL, all items are processed serially on the calling thread.
  1877. * @param functor the functor to call for each tile.
  1878. * @param range_i the number of items to process along the first dimension
  1879. * of the 3D grid.
  1880. * @param range_j the number of items to process along the second dimension
  1881. * of the 3D grid.
  1882. * @param range_k the number of items to process along the third dimension
  1883. * of the 3D grid.
  1884. * @param tile_j the maximum number of items along the second dimension of
  1885. * the 3D grid to process in one functor call.
  1886. * @param tile_k the maximum number of items along the third dimension of
  1887. * the 3D grid to process in one functor call.
  1888. * @param flags a bitwise combination of zero or more optional flags
  1889. * (PTHREADPOOL_FLAG_DISABLE_DENORMALS or PTHREADPOOL_FLAG_YIELD_WORKERS)
  1890. */
  1891. template<class T>
  1892. inline void pthreadpool_parallelize_3d_tile_2d(
  1893. pthreadpool_t threadpool,
  1894. const T& functor,
  1895. size_t range_i,
  1896. size_t range_j,
  1897. size_t range_k,
  1898. size_t tile_j,
  1899. size_t tile_k,
  1900. uint32_t flags = 0)
  1901. {
  1902. pthreadpool_parallelize_3d_tile_2d(
  1903. threadpool,
  1904. &libpthreadpool::detail::call_wrapper_3d_tile_2d<const T>,
  1905. const_cast<void*>(static_cast<const void*>(&functor)),
  1906. range_i,
  1907. range_j,
  1908. range_k,
  1909. tile_j,
  1910. tile_k,
  1911. flags);
  1912. }
  1913. /**
  1914. * Process items on a 4D grid.
  1915. *
  1916. * The function implements a parallel version of the following snippet:
  1917. *
  1918. * for (size_t i = 0; i < range_i; i++)
  1919. * for (size_t j = 0; j < range_j; j++)
  1920. * for (size_t k = 0; k < range_k; k++)
  1921. * for (size_t l = 0; l < range_l; l++)
  1922. * functor(i, j, k, l);
  1923. *
  1924. * When the function returns, all items have been processed and the thread pool
  1925. * is ready for a new task.
  1926. *
  1927. * @note If multiple threads call this function with the same thread pool, the
  1928. * calls are serialized.
  1929. *
  1930. * @param threadpool the thread pool to use for parallelisation. If threadpool
  1931. * is NULL, all items are processed serially on the calling thread.
  1932. * @param functor the functor to call for each tile.
  1933. * @param range_i the number of items to process along the first dimension
  1934. * of the 4D grid.
  1935. * @param range_j the number of items to process along the second dimension
  1936. * of the 4D grid.
  1937. * @param range_k the number of items to process along the third dimension
  1938. * of the 4D grid.
  1939. * @param range_l the number of items to process along the fourth dimension
  1940. * of the 4D grid.
  1941. * @param flags a bitwise combination of zero or more optional flags
  1942. * (PTHREADPOOL_FLAG_DISABLE_DENORMALS or PTHREADPOOL_FLAG_YIELD_WORKERS)
  1943. */
  1944. template<class T>
  1945. inline void pthreadpool_parallelize_4d(
  1946. pthreadpool_t threadpool,
  1947. const T& functor,
  1948. size_t range_i,
  1949. size_t range_j,
  1950. size_t range_k,
  1951. size_t range_l,
  1952. uint32_t flags = 0)
  1953. {
  1954. pthreadpool_parallelize_4d(
  1955. threadpool,
  1956. &libpthreadpool::detail::call_wrapper_4d<const T>,
  1957. const_cast<void*>(static_cast<const void*>(&functor)),
  1958. range_i,
  1959. range_j,
  1960. range_k,
  1961. range_l,
  1962. flags);
  1963. }
  1964. /**
  1965. * Process items on a 4D grid with the specified maximum tile size along the
  1966. * last grid dimension.
  1967. *
  1968. * The function implements a parallel version of the following snippet:
  1969. *
  1970. * for (size_t i = 0; i < range_i; i++)
  1971. * for (size_t j = 0; j < range_j; j++)
  1972. * for (size_t k = 0; k < range_k; k++)
  1973. * for (size_t l = 0; l < range_l; l += tile_l)
  1974. * functor(i, j, k, l, min(range_l - l, tile_l));
  1975. *
  1976. * When the function returns, all items have been processed and the thread pool
  1977. * is ready for a new task.
  1978. *
  1979. * @note If multiple threads call this function with the same thread pool, the
  1980. * calls are serialized.
  1981. *
  1982. * @param threadpool the thread pool to use for parallelisation. If threadpool
  1983. * is NULL, all items are processed serially on the calling thread.
  1984. * @param functor the functor to call for each tile.
  1985. * @param range_i the number of items to process along the first dimension
  1986. * of the 4D grid.
  1987. * @param range_j the number of items to process along the second dimension
  1988. * of the 4D grid.
  1989. * @param range_k the number of items to process along the third dimension
  1990. * of the 4D grid.
  1991. * @param range_l the number of items to process along the fourth dimension
  1992. * of the 4D grid.
  1993. * @param tile_l the maximum number of items along the fourth dimension of
  1994. * the 4D grid to process in one functor call.
  1995. * @param flags a bitwise combination of zero or more optional flags
  1996. * (PTHREADPOOL_FLAG_DISABLE_DENORMALS or PTHREADPOOL_FLAG_YIELD_WORKERS)
  1997. */
  1998. template<class T>
  1999. inline void pthreadpool_parallelize_4d_tile_1d(
  2000. pthreadpool_t threadpool,
  2001. const T& functor,
  2002. size_t range_i,
  2003. size_t range_j,
  2004. size_t range_k,
  2005. size_t range_l,
  2006. size_t tile_l,
  2007. uint32_t flags = 0)
  2008. {
  2009. pthreadpool_parallelize_4d_tile_1d(
  2010. threadpool,
  2011. &libpthreadpool::detail::call_wrapper_4d_tile_1d<const T>,
  2012. const_cast<void*>(static_cast<const void*>(&functor)),
  2013. range_i,
  2014. range_j,
  2015. range_k,
  2016. range_l,
  2017. tile_l,
  2018. flags);
  2019. }
  2020. /**
  2021. * Process items on a 4D grid with the specified maximum tile size along the
  2022. * last two grid dimensions.
  2023. *
  2024. * The function implements a parallel version of the following snippet:
  2025. *
  2026. * for (size_t i = 0; i < range_i; i++)
  2027. * for (size_t j = 0; j < range_j; j++)
  2028. * for (size_t k = 0; k < range_k; k += tile_k)
  2029. * for (size_t l = 0; l < range_l; l += tile_l)
  2030. * functor(i, j, k, l,
  2031. * min(range_k - k, tile_k), min(range_l - l, tile_l));
  2032. *
  2033. * When the function returns, all items have been processed and the thread pool
  2034. * is ready for a new task.
  2035. *
  2036. * @note If multiple threads call this function with the same thread pool, the
  2037. * calls are serialized.
  2038. *
  2039. * @param threadpool the thread pool to use for parallelisation. If threadpool
  2040. * is NULL, all items are processed serially on the calling thread.
  2041. * @param functor the functor to call for each tile.
  2042. * @param range_i the number of items to process along the first dimension
  2043. * of the 4D grid.
  2044. * @param range_j the number of items to process along the second dimension
  2045. * of the 4D grid.
  2046. * @param range_k the number of items to process along the third dimension
  2047. * of the 4D grid.
  2048. * @param range_l the number of items to process along the fourth dimension
  2049. * of the 4D grid.
  2050. * @param tile_k the maximum number of items along the third dimension of
  2051. * the 4D grid to process in one functor call.
  2052. * @param tile_l the maximum number of items along the fourth dimension of
  2053. * the 4D grid to process in one functor call.
  2054. * @param flags a bitwise combination of zero or more optional flags
  2055. * (PTHREADPOOL_FLAG_DISABLE_DENORMALS or PTHREADPOOL_FLAG_YIELD_WORKERS)
  2056. */
  2057. template<class T>
  2058. inline void pthreadpool_parallelize_4d_tile_2d(
  2059. pthreadpool_t threadpool,
  2060. const T& functor,
  2061. size_t range_i,
  2062. size_t range_j,
  2063. size_t range_k,
  2064. size_t range_l,
  2065. size_t tile_k,
  2066. size_t tile_l,
  2067. uint32_t flags = 0)
  2068. {
  2069. pthreadpool_parallelize_4d_tile_2d(
  2070. threadpool,
  2071. &libpthreadpool::detail::call_wrapper_4d_tile_2d<const T>,
  2072. const_cast<void*>(static_cast<const void*>(&functor)),
  2073. range_i,
  2074. range_j,
  2075. range_k,
  2076. range_l,
  2077. tile_k,
  2078. tile_l,
  2079. flags);
  2080. }
  2081. /**
  2082. * Process items on a 5D grid.
  2083. *
  2084. * The function implements a parallel version of the following snippet:
  2085. *
  2086. * for (size_t i = 0; i < range_i; i++)
  2087. * for (size_t j = 0; j < range_j; j++)
  2088. * for (size_t k = 0; k < range_k; k++)
  2089. * for (size_t l = 0; l < range_l; l++)
  2090. * for (size_t m = 0; m < range_m; m++)
  2091. * functor(i, j, k, l, m);
  2092. *
  2093. * When the function returns, all items have been processed and the thread pool
  2094. * is ready for a new task.
  2095. *
  2096. * @note If multiple threads call this function with the same thread pool, the
  2097. * calls are serialized.
  2098. *
  2099. * @param threadpool the thread pool to use for parallelisation. If threadpool
  2100. * is NULL, all items are processed serially on the calling thread.
  2101. * @param functor the functor to call for each tile.
  2102. * @param range_i the number of items to process along the first dimension
  2103. * of the 5D grid.
  2104. * @param range_j the number of items to process along the second dimension
  2105. * of the 5D grid.
  2106. * @param range_k the number of items to process along the third dimension
  2107. * of the 5D grid.
  2108. * @param range_l the number of items to process along the fourth dimension
  2109. * of the 5D grid.
  2110. * @param range_m the number of items to process along the fifth dimension
  2111. * of the 5D grid.
  2112. * @param flags a bitwise combination of zero or more optional flags
  2113. * (PTHREADPOOL_FLAG_DISABLE_DENORMALS or PTHREADPOOL_FLAG_YIELD_WORKERS)
  2114. */
  2115. template<class T>
  2116. inline void pthreadpool_parallelize_5d(
  2117. pthreadpool_t threadpool,
  2118. const T& functor,
  2119. size_t range_i,
  2120. size_t range_j,
  2121. size_t range_k,
  2122. size_t range_l,
  2123. size_t range_m,
  2124. uint32_t flags = 0)
  2125. {
  2126. pthreadpool_parallelize_5d(
  2127. threadpool,
  2128. &libpthreadpool::detail::call_wrapper_5d<const T>,
  2129. const_cast<void*>(static_cast<const void*>(&functor)),
  2130. range_i,
  2131. range_j,
  2132. range_k,
  2133. range_l,
  2134. range_m,
  2135. flags);
  2136. }
  2137. /**
  2138. * Process items on a 5D grid with the specified maximum tile size along the
  2139. * last grid dimension.
  2140. *
  2141. * The function implements a parallel version of the following snippet:
  2142. *
  2143. * for (size_t i = 0; i < range_i; i++)
  2144. * for (size_t j = 0; j < range_j; j++)
  2145. * for (size_t k = 0; k < range_k; k++)
  2146. * for (size_t l = 0; l < range_l; l++)
  2147. * for (size_t m = 0; m < range_m; m += tile_m)
  2148. * functor(i, j, k, l, m, min(range_m - m, tile_m));
  2149. *
  2150. * When the function returns, all items have been processed and the thread pool
  2151. * is ready for a new task.
  2152. *
  2153. * @note If multiple threads call this function with the same thread pool, the
  2154. * calls are serialized.
  2155. *
  2156. * @param threadpool the thread pool to use for parallelisation. If threadpool
  2157. * is NULL, all items are processed serially on the calling thread.
  2158. * @param functor the functor to call for each tile.
  2159. * @param range_i the number of items to process along the first dimension
  2160. * of the 5D grid.
  2161. * @param range_j the number of items to process along the second dimension
  2162. * of the 5D grid.
  2163. * @param range_k the number of items to process along the third dimension
  2164. * of the 5D grid.
  2165. * @param range_l the number of items to process along the fourth dimension
  2166. * of the 5D grid.
  2167. * @param range_m the number of items to process along the fifth dimension
  2168. * of the 5D grid.
  2169. * @param tile_m the maximum number of items along the fifth dimension of
  2170. * the 5D grid to process in one functor call.
  2171. * @param flags a bitwise combination of zero or more optional flags
  2172. * (PTHREADPOOL_FLAG_DISABLE_DENORMALS or PTHREADPOOL_FLAG_YIELD_WORKERS)
  2173. */
  2174. template<class T>
  2175. inline void pthreadpool_parallelize_5d_tile_1d(
  2176. pthreadpool_t threadpool,
  2177. const T& functor,
  2178. size_t range_i,
  2179. size_t range_j,
  2180. size_t range_k,
  2181. size_t range_l,
  2182. size_t range_m,
  2183. size_t tile_m,
  2184. uint32_t flags = 0)
  2185. {
  2186. pthreadpool_parallelize_5d_tile_1d(
  2187. threadpool,
  2188. &libpthreadpool::detail::call_wrapper_5d_tile_1d<const T>,
  2189. const_cast<void*>(static_cast<const void*>(&functor)),
  2190. range_i,
  2191. range_j,
  2192. range_k,
  2193. range_l,
  2194. range_m,
  2195. tile_m,
  2196. flags);
  2197. }
  2198. /**
  2199. * Process items on a 5D grid with the specified maximum tile size along the
  2200. * last two grid dimensions.
  2201. *
  2202. * The function implements a parallel version of the following snippet:
  2203. *
  2204. * for (size_t i = 0; i < range_i; i++)
  2205. * for (size_t j = 0; j < range_j; j++)
  2206. * for (size_t k = 0; k < range_k; k++)
  2207. * for (size_t l = 0; l < range_l; l += tile_l)
  2208. * for (size_t m = 0; m < range_m; m += tile_m)
  2209. * functor(i, j, k, l, m,
  2210. * min(range_l - l, tile_l), min(range_m - m, tile_m));
  2211. *
  2212. * When the function returns, all items have been processed and the thread pool
  2213. * is ready for a new task.
  2214. *
  2215. * @note If multiple threads call this function with the same thread pool, the
  2216. * calls are serialized.
  2217. *
  2218. * @param threadpool the thread pool to use for parallelisation. If threadpool
  2219. * is NULL, all items are processed serially on the calling thread.
  2220. * @param functor the functor to call for each tile.
  2221. * @param range_i the number of items to process along the first dimension
  2222. * of the 5D grid.
  2223. * @param range_j the number of items to process along the second dimension
  2224. * of the 5D grid.
  2225. * @param range_k the number of items to process along the third dimension
  2226. * of the 5D grid.
  2227. * @param range_l the number of items to process along the fourth dimension
  2228. * of the 5D grid.
  2229. * @param range_m the number of items to process along the fifth dimension
  2230. * of the 5D grid.
  2231. * @param tile_l the maximum number of items along the fourth dimension of
  2232. * the 5D grid to process in one functor call.
  2233. * @param tile_m the maximum number of items along the fifth dimension of
  2234. * the 5D grid to process in one functor call.
  2235. * @param flags a bitwise combination of zero or more optional flags
  2236. * (PTHREADPOOL_FLAG_DISABLE_DENORMALS or PTHREADPOOL_FLAG_YIELD_WORKERS)
  2237. */
  2238. template<class T>
  2239. inline void pthreadpool_parallelize_5d_tile_2d(
  2240. pthreadpool_t threadpool,
  2241. const T& functor,
  2242. size_t range_i,
  2243. size_t range_j,
  2244. size_t range_k,
  2245. size_t range_l,
  2246. size_t range_m,
  2247. size_t tile_l,
  2248. size_t tile_m,
  2249. uint32_t flags = 0)
  2250. {
  2251. pthreadpool_parallelize_5d_tile_2d(
  2252. threadpool,
  2253. &libpthreadpool::detail::call_wrapper_5d_tile_2d<const T>,
  2254. const_cast<void*>(static_cast<const void*>(&functor)),
  2255. range_i,
  2256. range_j,
  2257. range_k,
  2258. range_l,
  2259. range_m,
  2260. tile_l,
  2261. tile_m,
  2262. flags);
  2263. }
  2264. /**
  2265. * Process items on a 6D grid.
  2266. *
  2267. * The function implements a parallel version of the following snippet:
  2268. *
  2269. * for (size_t i = 0; i < range_i; i++)
  2270. * for (size_t j = 0; j < range_j; j++)
  2271. * for (size_t k = 0; k < range_k; k++)
  2272. * for (size_t l = 0; l < range_l; l++)
  2273. * for (size_t m = 0; m < range_m; m++)
  2274. * for (size_t n = 0; n < range_n; n++)
  2275. * functor(i, j, k, l, m, n);
  2276. *
  2277. * When the function returns, all items have been processed and the thread pool
  2278. * is ready for a new task.
  2279. *
  2280. * @note If multiple threads call this function with the same thread pool, the
  2281. * calls are serialized.
  2282. *
  2283. * @param threadpool the thread pool to use for parallelisation. If threadpool
  2284. * is NULL, all items are processed serially on the calling thread.
  2285. * @param functor the functor to call for each tile.
  2286. * @param range_i the number of items to process along the first dimension
  2287. * of the 6D grid.
  2288. * @param range_j the number of items to process along the second dimension
  2289. * of the 6D grid.
  2290. * @param range_k the number of items to process along the third dimension
  2291. * of the 6D grid.
  2292. * @param range_l the number of items to process along the fourth dimension
  2293. * of the 6D grid.
  2294. * @param range_m the number of items to process along the fifth dimension
  2295. * of the 6D grid.
  2296. * @param range_n the number of items to process along the sixth dimension
  2297. * of the 6D grid.
  2298. * @param tile_n the maximum number of items along the sixth dimension of
  2299. * the 6D grid to process in one functor call.
  2300. * @param flags a bitwise combination of zero or more optional flags
  2301. * (PTHREADPOOL_FLAG_DISABLE_DENORMALS or PTHREADPOOL_FLAG_YIELD_WORKERS)
  2302. */
  2303. template<class T>
  2304. inline void pthreadpool_parallelize_6d(
  2305. pthreadpool_t threadpool,
  2306. const T& functor,
  2307. size_t range_i,
  2308. size_t range_j,
  2309. size_t range_k,
  2310. size_t range_l,
  2311. size_t range_m,
  2312. size_t range_n,
  2313. uint32_t flags = 0)
  2314. {
  2315. pthreadpool_parallelize_6d(
  2316. threadpool,
  2317. &libpthreadpool::detail::call_wrapper_6d<const T>,
  2318. const_cast<void*>(static_cast<const void*>(&functor)),
  2319. range_i,
  2320. range_j,
  2321. range_k,
  2322. range_l,
  2323. range_m,
  2324. range_n,
  2325. flags);
  2326. }
  2327. /**
  2328. * Process items on a 6D grid with the specified maximum tile size along the
  2329. * last grid dimension.
  2330. *
  2331. * The function implements a parallel version of the following snippet:
  2332. *
  2333. * for (size_t i = 0; i < range_i; i++)
  2334. * for (size_t j = 0; j < range_j; j++)
  2335. * for (size_t k = 0; k < range_k; k++)
  2336. * for (size_t l = 0; l < range_l; l++)
  2337. * for (size_t m = 0; m < range_m; m++)
  2338. * for (size_t n = 0; n < range_n; n += tile_n)
  2339. * functor(i, j, k, l, m, n, min(range_n - n, tile_n));
  2340. *
  2341. * When the function returns, all items have been processed and the thread pool
  2342. * is ready for a new task.
  2343. *
  2344. * @note If multiple threads call this function with the same thread pool, the
  2345. * calls are serialized.
  2346. *
  2347. * @param threadpool the thread pool to use for parallelisation. If threadpool
  2348. * is NULL, all items are processed serially on the calling thread.
  2349. * @param functor the functor to call for each tile.
  2350. * @param range_i the number of items to process along the first dimension
  2351. * of the 6D grid.
  2352. * @param range_j the number of items to process along the second dimension
  2353. * of the 6D grid.
  2354. * @param range_k the number of items to process along the third dimension
  2355. * of the 6D grid.
  2356. * @param range_l the number of items to process along the fourth dimension
  2357. * of the 6D grid.
  2358. * @param range_m the number of items to process along the fifth dimension
  2359. * of the 6D grid.
  2360. * @param range_n the number of items to process along the sixth dimension
  2361. * of the 6D grid.
  2362. * @param tile_n the maximum number of items along the sixth dimension of
  2363. * the 6D grid to process in one functor call.
  2364. * @param flags a bitwise combination of zero or more optional flags
  2365. * (PTHREADPOOL_FLAG_DISABLE_DENORMALS or PTHREADPOOL_FLAG_YIELD_WORKERS)
  2366. */
  2367. template<class T>
  2368. inline void pthreadpool_parallelize_6d_tile_1d(
  2369. pthreadpool_t threadpool,
  2370. const T& functor,
  2371. size_t range_i,
  2372. size_t range_j,
  2373. size_t range_k,
  2374. size_t range_l,
  2375. size_t range_m,
  2376. size_t range_n,
  2377. size_t tile_n,
  2378. uint32_t flags = 0)
  2379. {
  2380. pthreadpool_parallelize_6d_tile_1d(
  2381. threadpool,
  2382. &libpthreadpool::detail::call_wrapper_6d_tile_1d<const T>,
  2383. const_cast<void*>(static_cast<const void*>(&functor)),
  2384. range_i,
  2385. range_j,
  2386. range_k,
  2387. range_l,
  2388. range_m,
  2389. range_n,
  2390. tile_n,
  2391. flags);
  2392. }
  2393. /**
  2394. * Process items on a 6D grid with the specified maximum tile size along the
  2395. * last two grid dimensions.
  2396. *
  2397. * The function implements a parallel version of the following snippet:
  2398. *
  2399. * for (size_t i = 0; i < range_i; i++)
  2400. * for (size_t j = 0; j < range_j; j++)
  2401. * for (size_t k = 0; k < range_k; k++)
  2402. * for (size_t l = 0; l < range_l; l++)
  2403. * for (size_t m = 0; m < range_m; m += tile_m)
  2404. * for (size_t n = 0; n < range_n; n += tile_n)
  2405. * functor(i, j, k, l, m, n,
  2406. * min(range_m - m, tile_m), min(range_n - n, tile_n));
  2407. *
  2408. * When the function returns, all items have been processed and the thread pool
  2409. * is ready for a new task.
  2410. *
  2411. * @note If multiple threads call this function with the same thread pool, the
  2412. * calls are serialized.
  2413. *
  2414. * @param threadpool the thread pool to use for parallelisation. If threadpool
  2415. * is NULL, all items are processed serially on the calling thread.
  2416. * @param functor the functor to call for each tile.
  2417. * @param range_i the number of items to process along the first dimension
  2418. * of the 6D grid.
  2419. * @param range_j the number of items to process along the second dimension
  2420. * of the 6D grid.
  2421. * @param range_k the number of items to process along the third dimension
  2422. * of the 6D grid.
  2423. * @param range_l the number of items to process along the fourth dimension
  2424. * of the 6D grid.
  2425. * @param range_m the number of items to process along the fifth dimension
  2426. * of the 6D grid.
  2427. * @param range_n the number of items to process along the sixth dimension
  2428. * of the 6D grid.
  2429. * @param tile_m the maximum number of items along the fifth dimension of
  2430. * the 6D grid to process in one functor call.
  2431. * @param tile_n the maximum number of items along the sixth dimension of
  2432. * the 6D grid to process in one functor call.
  2433. * @param flags a bitwise combination of zero or more optional flags
  2434. * (PTHREADPOOL_FLAG_DISABLE_DENORMALS or PTHREADPOOL_FLAG_YIELD_WORKERS)
  2435. */
  2436. template<class T>
  2437. inline void pthreadpool_parallelize_6d_tile_2d(
  2438. pthreadpool_t threadpool,
  2439. const T& functor,
  2440. size_t range_i,
  2441. size_t range_j,
  2442. size_t range_k,
  2443. size_t range_l,
  2444. size_t range_m,
  2445. size_t range_n,
  2446. size_t tile_m,
  2447. size_t tile_n,
  2448. uint32_t flags = 0)
  2449. {
  2450. pthreadpool_parallelize_6d_tile_2d(
  2451. threadpool,
  2452. &libpthreadpool::detail::call_wrapper_6d_tile_2d<const T>,
  2453. const_cast<void*>(static_cast<const void*>(&functor)),
  2454. range_i,
  2455. range_j,
  2456. range_k,
  2457. range_l,
  2458. range_m,
  2459. range_n,
  2460. tile_m,
  2461. tile_n,
  2462. flags);
  2463. }
  2464. #endif /* __cplusplus */
  2465. #endif /* PTHREADPOOL_H_ */