openmp_runtime_support.c 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2014 Inria
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <starpu.h>
  17. #ifdef STARPU_OPENMP
  18. /*
  19. * locally disable -Wdeprecated-declarations to avoid
  20. * lots of deprecated warnings for ucontext related functions
  21. */
  22. #pragma GCC diagnostic push
  23. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  24. #include <util/openmp_runtime_support.h>
  25. #include <core/task.h>
  26. #include <core/workers.h>
  27. #include <common/list.h>
  28. #include <common/starpu_spinlock.h>
  29. #include <common/uthash.h>
  30. #include <datawizard/interfaces/data_interface.h>
  31. #include <stdlib.h>
  32. #include <ctype.h>
  33. #include <strings.h>
  34. #define _STARPU_INITIAL_THREAD_STACKSIZE 2097152
  35. static struct starpu_omp_global _global_state;
  36. static starpu_pthread_key_t omp_thread_key;
  37. static starpu_pthread_key_t omp_task_key;
  38. struct starpu_omp_global *_starpu_omp_global_state = NULL;
  39. double _starpu_omp_clock_ref = 0.0; /* clock reference for starpu_omp_get_wtick */
  40. static struct starpu_omp_critical *create_omp_critical_struct(void);
  41. static void destroy_omp_critical_struct(struct starpu_omp_critical *critical);
  42. static struct starpu_omp_device *create_omp_device_struct(void);
  43. static void destroy_omp_device_struct(struct starpu_omp_device *device);
  44. static struct starpu_omp_region *create_omp_region_struct(struct starpu_omp_region *parent_region, struct starpu_omp_device *owner_device);
  45. static void destroy_omp_region_struct(struct starpu_omp_region *region);
  46. static struct starpu_omp_thread *create_omp_thread_struct(struct starpu_omp_region *owner_region);
  47. static void destroy_omp_thread_struct(struct starpu_omp_thread *thread);
  48. static struct starpu_omp_task *create_omp_task_struct(struct starpu_omp_task *parent_task,
  49. struct starpu_omp_thread *owner_thread, struct starpu_omp_region *owner_region, int is_implicit);
  50. static void destroy_omp_task_struct(struct starpu_omp_task *task);
  51. static void _wake_up_locked_task(struct starpu_omp_task *task);
  52. static void wake_up_barrier(struct starpu_omp_region *parallel_region);
  53. static void starpu_omp_task_preempt(void);
  54. struct starpu_omp_thread *_starpu_omp_get_thread(void)
  55. {
  56. struct starpu_omp_thread *thread = STARPU_PTHREAD_GETSPECIFIC(omp_thread_key);
  57. return thread;
  58. }
  59. struct starpu_omp_task *_starpu_omp_get_task(void)
  60. {
  61. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  62. return task;
  63. }
  64. static void condition_init(struct starpu_omp_condition *condition)
  65. {
  66. condition->contention_list_head = NULL;
  67. }
  68. static void condition_exit(struct starpu_omp_condition *condition)
  69. {
  70. STARPU_ASSERT(condition->contention_list_head == NULL);
  71. condition->contention_list_head = NULL;
  72. }
  73. static void condition__sleep_callback(void *_lock)
  74. {
  75. struct _starpu_spinlock *lock = _lock;
  76. _starpu_spin_unlock(lock);
  77. }
  78. static void condition_wait(struct starpu_omp_condition *condition, struct _starpu_spinlock *lock)
  79. {
  80. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  81. struct starpu_omp_task_link link;
  82. _starpu_spin_lock(&task->lock);
  83. task->wait_on |= starpu_omp_task_wait_on_condition;
  84. _starpu_spin_unlock(&task->lock);
  85. link.task = task;
  86. link.next = condition->contention_list_head;
  87. condition->contention_list_head = &link;
  88. _starpu_task_prepare_for_continuation_ext(0, condition__sleep_callback, lock);
  89. starpu_omp_task_preempt();
  90. /* re-acquire the lock released by the callback */
  91. _starpu_spin_lock(lock);
  92. }
  93. #if 0
  94. /* unused for now */
  95. static void condition_signal(struct starpu_omp_condition *condition)
  96. {
  97. if (condition->contention_list_head != NULL)
  98. {
  99. struct starpu_omp_task *next_task = condition->contention_list_head->task;
  100. condition->contention_list_head = condition->contention_list_head->next;
  101. _starpu_spin_lock(&next_task->lock);
  102. STARPU_ASSERT(next_task->wait_on & starpu_omp_task_wait_on_condition);
  103. next_task->wait_on &= ~starpu_omp_task_wait_on_condition;
  104. _wake_up_locked_task(next_task);
  105. _starpu_spin_unlock(&next_task->lock);
  106. }
  107. }
  108. #endif
  109. static void condition_broadcast(struct starpu_omp_condition *condition)
  110. {
  111. while (condition->contention_list_head != NULL)
  112. {
  113. struct starpu_omp_task *next_task = condition->contention_list_head->task;
  114. condition->contention_list_head = condition->contention_list_head->next;
  115. _starpu_spin_lock(&next_task->lock);
  116. STARPU_ASSERT(next_task->wait_on & starpu_omp_task_wait_on_condition);
  117. next_task->wait_on &= ~starpu_omp_task_wait_on_condition;
  118. _wake_up_locked_task(next_task);
  119. _starpu_spin_unlock(&next_task->lock);
  120. }
  121. }
  122. static void register_thread_worker(struct starpu_omp_thread *thread)
  123. {
  124. STARPU_ASSERT(thread->worker != NULL);
  125. _starpu_spin_lock(&_global_state.hash_workers_lock);
  126. struct _starpu_worker *check = thread->worker;
  127. struct starpu_omp_thread *tmp = NULL;
  128. HASH_FIND_PTR(_global_state.hash_workers, &check, tmp);
  129. STARPU_ASSERT(tmp == NULL);
  130. HASH_ADD_PTR(_global_state.hash_workers, worker, thread);
  131. _starpu_spin_unlock(&_global_state.hash_workers_lock);
  132. }
  133. static struct starpu_omp_thread *get_worker_thread(struct _starpu_worker *starpu_worker)
  134. {
  135. struct starpu_omp_thread *thread = NULL;
  136. _starpu_spin_lock(&_global_state.hash_workers_lock);
  137. HASH_FIND_PTR(_global_state.hash_workers, &starpu_worker, thread);
  138. _starpu_spin_unlock(&_global_state.hash_workers_lock);
  139. return thread;
  140. }
  141. static struct starpu_omp_thread *get_local_thread(void)
  142. {
  143. struct starpu_omp_thread *thread = STARPU_PTHREAD_GETSPECIFIC(omp_thread_key);
  144. if (thread == NULL)
  145. {
  146. struct _starpu_worker *starpu_worker = _starpu_get_local_worker_key();
  147. STARPU_ASSERT(starpu_worker != NULL);
  148. _starpu_spin_lock(&_global_state.hash_workers_lock);
  149. HASH_FIND_PTR(_global_state.hash_workers, &starpu_worker, thread);
  150. STARPU_ASSERT(thread != NULL);
  151. _starpu_spin_unlock(&_global_state.hash_workers_lock);
  152. STARPU_PTHREAD_SETSPECIFIC(omp_thread_key, thread);
  153. }
  154. return thread;
  155. }
  156. static struct starpu_omp_critical *create_omp_critical_struct(void)
  157. {
  158. struct starpu_omp_critical *critical = malloc(sizeof(*critical));
  159. memset(critical, 0, sizeof(*critical));
  160. _starpu_spin_init(&critical->lock);
  161. return critical;
  162. }
  163. static void destroy_omp_critical_struct(struct starpu_omp_critical *critical)
  164. {
  165. STARPU_ASSERT(critical->state == 0);
  166. STARPU_ASSERT(critical->contention_list_head == NULL);
  167. _starpu_spin_destroy(&critical->lock);
  168. critical->name = NULL;
  169. free(critical);
  170. }
  171. static struct starpu_omp_device *create_omp_device_struct(void)
  172. {
  173. struct starpu_omp_device *device = malloc(sizeof(*device));
  174. if (device == NULL)
  175. _STARPU_ERROR("memory allocation failed");
  176. memset(device, 0, sizeof(*device));
  177. return device;
  178. }
  179. static void destroy_omp_device_struct(struct starpu_omp_device *device)
  180. {
  181. memset(device, 0, sizeof(*device));
  182. free(device);
  183. }
  184. static struct starpu_omp_region *create_omp_region_struct(struct starpu_omp_region *parent_region, struct starpu_omp_device *owner_device)
  185. {
  186. struct starpu_omp_region *region = malloc(sizeof(*region));
  187. if (region == NULL)
  188. _STARPU_ERROR("memory allocation failed");
  189. memset(region, 0, sizeof(*region));
  190. region->parent_region = parent_region;
  191. region->owner_device = owner_device;
  192. region->thread_list = starpu_omp_thread_list_new();
  193. region->implicit_task_list = starpu_omp_task_list_new();
  194. _starpu_spin_init(&region->lock);
  195. _starpu_spin_init(&region->registered_handles_lock);
  196. region->level = (parent_region != NULL)?parent_region->level+1:0;
  197. return region;
  198. }
  199. static void destroy_omp_region_struct(struct starpu_omp_region *region)
  200. {
  201. STARPU_ASSERT(region->nb_threads == 0);
  202. STARPU_ASSERT(starpu_omp_thread_list_empty(region->thread_list));
  203. STARPU_ASSERT(starpu_omp_task_list_empty(region->implicit_task_list));
  204. STARPU_ASSERT(region->continuation_starpu_task == NULL);
  205. starpu_omp_thread_list_delete(region->thread_list);
  206. starpu_omp_task_list_delete(region->implicit_task_list);
  207. _starpu_spin_destroy(&region->registered_handles_lock);
  208. _starpu_spin_destroy(&region->lock);
  209. memset(region, 0, sizeof(*region));
  210. free(region);
  211. }
  212. static void omp_initial_thread_func(void)
  213. {
  214. struct starpu_omp_thread *initial_thread = _global_state.initial_thread;
  215. struct starpu_omp_task *initial_task = _global_state.initial_task;
  216. while (1)
  217. {
  218. struct starpu_task *continuation_starpu_task = initial_task->nested_region->continuation_starpu_task;
  219. starpu_driver_run_once(&initial_thread->starpu_driver);
  220. /*
  221. * if we are leaving the first nested region we give control back to initial task
  222. * otherwise, we should continue to execute work
  223. */
  224. if (_starpu_task_test_termination(continuation_starpu_task))
  225. {
  226. initial_task->nested_region->continuation_starpu_task = NULL;
  227. STARPU_PTHREAD_SETSPECIFIC(omp_task_key, initial_task);
  228. swapcontext(&initial_thread->ctx, &initial_task->ctx);
  229. }
  230. }
  231. }
  232. static struct starpu_omp_thread *create_omp_thread_struct(struct starpu_omp_region *owner_region)
  233. {
  234. struct starpu_omp_thread *thread = starpu_omp_thread_new();
  235. if (thread == NULL)
  236. _STARPU_ERROR("memory allocation failed");
  237. memset(thread, 0, sizeof(*thread));
  238. thread->owner_region = owner_region;
  239. return thread;
  240. }
  241. static void destroy_omp_thread_struct(struct starpu_omp_thread *thread)
  242. {
  243. STARPU_ASSERT(thread->current_task == NULL);
  244. memset(thread, 0, sizeof(*thread));
  245. starpu_omp_thread_delete(thread);
  246. }
  247. static void starpu_omp_explicit_task_entry(struct starpu_omp_task *task)
  248. {
  249. STARPU_ASSERT(!task->is_implicit);
  250. task->f(task->starpu_buffers, task->starpu_cl_arg);
  251. _starpu_omp_unregister_task_handles(task);
  252. task->state = starpu_omp_task_state_terminated;
  253. struct starpu_omp_thread *thread = STARPU_PTHREAD_GETSPECIFIC(omp_thread_key);
  254. /*
  255. * the task reached the terminated state, definitively give hand back to the worker code.
  256. *
  257. * about to run on the worker stack...
  258. */
  259. setcontext(&thread->ctx);
  260. STARPU_ASSERT(0); /* unreachable code */
  261. }
  262. static void starpu_omp_implicit_task_entry(struct starpu_omp_task *task)
  263. {
  264. struct starpu_omp_thread *thread = STARPU_PTHREAD_GETSPECIFIC(omp_thread_key);
  265. STARPU_ASSERT(task->is_implicit);
  266. task->f(task->starpu_buffers, task->starpu_cl_arg);
  267. starpu_omp_barrier();
  268. if (thread == task->owner_region->master_thread)
  269. {
  270. _starpu_omp_unregister_region_handles(task->owner_region);
  271. }
  272. task->state = starpu_omp_task_state_terminated;
  273. /*
  274. * the task reached the terminated state, definitively give hand back to the worker code.
  275. *
  276. * about to run on the worker stack...
  277. */
  278. setcontext(&thread->ctx);
  279. STARPU_ASSERT(0); /* unreachable code */
  280. }
  281. /*
  282. * stop executing a task that is about to block
  283. * and give hand back to the thread
  284. */
  285. static void starpu_omp_task_preempt(void)
  286. {
  287. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  288. struct starpu_omp_thread *thread = STARPU_PTHREAD_GETSPECIFIC(omp_thread_key);
  289. task->state = starpu_omp_task_state_preempted;
  290. /*
  291. * the task reached a blocked state, give hand back to the worker code.
  292. *
  293. * about to run on the worker stack...
  294. */
  295. swapcontext(&task->ctx, &thread->ctx);
  296. /* now running on the task stack again */
  297. }
  298. /*
  299. * wrap a task function to allow the task to be preempted
  300. */
  301. static void starpu_omp_implicit_task_exec(void *buffers[], void *cl_arg)
  302. {
  303. struct starpu_omp_task *task = starpu_task_get_current()->omp_task;
  304. STARPU_ASSERT(task->is_implicit);
  305. STARPU_PTHREAD_SETSPECIFIC(omp_task_key, task);
  306. struct starpu_omp_thread *thread = get_local_thread();
  307. if (task->state != starpu_omp_task_state_preempted)
  308. {
  309. task->starpu_buffers = buffers;
  310. task->starpu_cl_arg = cl_arg;
  311. STARPU_ASSERT (task->stack == NULL);
  312. /* TODO: use ICV stack size info instead */
  313. task->stack = malloc(task->stacksize);
  314. if (task->stack == NULL)
  315. _STARPU_ERROR("memory allocation failed");
  316. getcontext(&task->ctx);
  317. /*
  318. * we do not use uc_link, starpu_omp_task_entry will handle
  319. * the end of the task
  320. */
  321. task->ctx.uc_link = NULL;
  322. task->ctx.uc_stack.ss_sp = task->stack;
  323. task->ctx.uc_stack.ss_size = task->stacksize;
  324. makecontext(&task->ctx, (void (*) ()) starpu_omp_implicit_task_entry, 1, task);
  325. }
  326. task->state = starpu_omp_task_state_clear;
  327. /*
  328. * start the task execution, or restore a previously preempted task.
  329. * about to run on the task stack...
  330. * */
  331. swapcontext(&thread->ctx, &task->ctx);
  332. /* now running on the worker stack again */
  333. STARPU_ASSERT(task->state == starpu_omp_task_state_preempted
  334. || task->state == starpu_omp_task_state_terminated);
  335. STARPU_PTHREAD_SETSPECIFIC(omp_task_key, NULL);
  336. /* TODO: analyse the cause of the return and take appropriate steps */
  337. if (task->state == starpu_omp_task_state_terminated)
  338. {
  339. task->starpu_task->omp_task = NULL;
  340. task->starpu_task = NULL;
  341. free(task->stack);
  342. task->stack = NULL;
  343. memset(&task->ctx, 0, sizeof(task->ctx));
  344. }
  345. else if (task->state != starpu_omp_task_state_preempted)
  346. _STARPU_ERROR("invalid omp task state");
  347. }
  348. /*
  349. * wrap a task function to allow the task to be preempted
  350. */
  351. static void starpu_omp_explicit_task_exec(void *buffers[], void *cl_arg)
  352. {
  353. struct starpu_omp_task *task = starpu_task_get_current()->omp_task;
  354. STARPU_ASSERT(!task->is_implicit);
  355. STARPU_PTHREAD_SETSPECIFIC(omp_task_key, task);
  356. struct starpu_omp_thread *thread = get_local_thread();
  357. if (task->state != starpu_omp_task_state_preempted)
  358. {
  359. if (!task->is_untied)
  360. {
  361. struct _starpu_worker *starpu_worker = _starpu_get_local_worker_key();
  362. task->starpu_task->workerid = starpu_worker->workerid;
  363. task->starpu_task->execute_on_a_specific_worker = 1;
  364. }
  365. task->starpu_buffers = buffers;
  366. task->starpu_cl_arg = cl_arg;
  367. STARPU_ASSERT (task->stack == NULL);
  368. /* TODO: use ICV stack size info instead */
  369. task->stack = malloc(task->stacksize);
  370. if (task->stack == NULL)
  371. _STARPU_ERROR("memory allocation failed");
  372. getcontext(&task->ctx);
  373. /*
  374. * we do not use uc_link, starpu_omp_task_entry will handle
  375. * the end of the task
  376. */
  377. task->ctx.uc_link = NULL;
  378. task->ctx.uc_stack.ss_sp = task->stack;
  379. task->ctx.uc_stack.ss_size = task->stacksize;
  380. makecontext(&task->ctx, (void (*) ()) starpu_omp_explicit_task_entry, 1, task);
  381. }
  382. task->state = starpu_omp_task_state_clear;
  383. /*
  384. * start the task execution, or restore a previously preempted task.
  385. * about to run on the task stack...
  386. * */
  387. swapcontext(&thread->ctx, &task->ctx);
  388. /* now running on the worker stack again */
  389. STARPU_ASSERT(task->state == starpu_omp_task_state_preempted
  390. || task->state == starpu_omp_task_state_terminated);
  391. STARPU_PTHREAD_SETSPECIFIC(omp_task_key, NULL);
  392. /* TODO: analyse the cause of the return and take appropriate steps */
  393. if (task->state == starpu_omp_task_state_terminated)
  394. {
  395. struct starpu_omp_task *parent_task = task->parent_task;
  396. struct starpu_omp_region *parallel_region = task->owner_region;
  397. free(task->stack);
  398. task->stack = NULL;
  399. memset(&task->ctx, 0, sizeof(task->ctx));
  400. _starpu_spin_lock(&parent_task->lock);
  401. if (STARPU_ATOMIC_ADD(&parent_task->child_task_count, -1) == 0)
  402. {
  403. if (parent_task->state == starpu_omp_task_state_zombie)
  404. {
  405. STARPU_ASSERT(!parent_task->is_implicit);
  406. destroy_omp_task_struct(parent_task);
  407. }
  408. else if (parent_task->wait_on & starpu_omp_task_wait_on_task_childs)
  409. {
  410. parent_task->wait_on &= ~starpu_omp_task_wait_on_task_childs;
  411. _wake_up_locked_task(parent_task);
  412. }
  413. }
  414. _starpu_spin_unlock(&parent_task->lock);
  415. _starpu_spin_lock(&parallel_region->lock);
  416. if (STARPU_ATOMIC_ADD(&parallel_region->bound_explicit_task_count, -1) == 0)
  417. {
  418. struct starpu_omp_task *waiting_task = parallel_region->waiting_task;
  419. _starpu_spin_unlock(&parallel_region->lock);
  420. if (waiting_task)
  421. {
  422. _starpu_spin_lock(&waiting_task->lock);
  423. _starpu_spin_lock(&parallel_region->lock);
  424. parallel_region->waiting_task = NULL;
  425. STARPU_ASSERT(waiting_task->wait_on & starpu_omp_task_wait_on_region_tasks);
  426. waiting_task->wait_on &= ~starpu_omp_task_wait_on_region_tasks;
  427. _wake_up_locked_task(waiting_task);
  428. _starpu_spin_unlock(&parallel_region->lock);
  429. _starpu_spin_unlock(&waiting_task->lock);
  430. }
  431. }
  432. else
  433. {
  434. _starpu_spin_unlock(&parallel_region->lock);
  435. }
  436. if (task->task_group)
  437. {
  438. struct starpu_omp_task *leader_task = task->task_group->leader_task;
  439. STARPU_ASSERT(leader_task != task);
  440. _starpu_spin_lock(&leader_task->lock);
  441. if (STARPU_ATOMIC_ADD(&task->task_group->descendent_task_count, -1) == 0)
  442. {
  443. if (leader_task->wait_on & starpu_omp_task_wait_on_group)
  444. {
  445. leader_task->wait_on &= ~starpu_omp_task_wait_on_group;
  446. _wake_up_locked_task(leader_task);
  447. }
  448. }
  449. _starpu_spin_unlock(&leader_task->lock);
  450. }
  451. }
  452. else if (task->state != starpu_omp_task_state_preempted)
  453. _STARPU_ERROR("invalid omp task state");
  454. }
  455. static struct starpu_omp_task *create_omp_task_struct(struct starpu_omp_task *parent_task,
  456. struct starpu_omp_thread *owner_thread, struct starpu_omp_region *owner_region, int is_implicit)
  457. {
  458. struct starpu_omp_task *task = starpu_omp_task_new();
  459. if (task == NULL)
  460. _STARPU_ERROR("memory allocation failed");
  461. memset(task, 0, sizeof(*task));
  462. task->parent_task = parent_task;
  463. task->owner_thread = owner_thread;
  464. task->owner_region = owner_region;
  465. task->is_implicit = is_implicit;
  466. _starpu_spin_init(&task->lock);
  467. /* TODO: initialize task->data_env_icvs with proper values */
  468. memset(&task->data_env_icvs, 0, sizeof(task->data_env_icvs));
  469. if (is_implicit)
  470. {
  471. /* TODO: initialize task->implicit_task_icvs with proper values */
  472. memset(&task->implicit_task_icvs, 0, sizeof(task->implicit_task_icvs));
  473. }
  474. if (owner_region->level > 0)
  475. {
  476. task->stacksize = owner_region->owner_device->icvs.stacksize_var;
  477. }
  478. return task;
  479. }
  480. static void destroy_omp_task_struct(struct starpu_omp_task *task)
  481. {
  482. STARPU_ASSERT(task->state == starpu_omp_task_state_terminated || (task->state == starpu_omp_task_state_zombie && task->child_task_count == 0));
  483. STARPU_ASSERT(task->nested_region == NULL);
  484. STARPU_ASSERT(task->starpu_task == NULL);
  485. STARPU_ASSERT(task->stack == NULL);
  486. _starpu_spin_destroy(&task->lock);
  487. memset(task, 0, sizeof(*task));
  488. starpu_omp_task_delete(task);
  489. }
  490. /*
  491. * setup the main application thread to handle the possible preemption of the initial task
  492. */
  493. static void omp_initial_thread_setup(void)
  494. {
  495. struct starpu_omp_thread *initial_thread = _global_state.initial_thread;
  496. struct starpu_omp_task *initial_task = _global_state.initial_task;
  497. /* .current_task */
  498. initial_thread->current_task = initial_task;
  499. /* .owner_region already set in create_omp_thread_struct */
  500. /* .initial_thread_stack */
  501. initial_thread->initial_thread_stack = malloc(_STARPU_INITIAL_THREAD_STACKSIZE);
  502. if (initial_thread->initial_thread_stack == NULL)
  503. _STARPU_ERROR("memory allocation failed");
  504. /* .ctx */
  505. getcontext(&initial_thread->ctx);
  506. /*
  507. * we do not use uc_link, the initial thread always should give hand back to the initial task
  508. */
  509. initial_thread->ctx.uc_link = NULL;
  510. initial_thread->ctx.uc_stack.ss_sp = initial_thread->initial_thread_stack;
  511. initial_thread->ctx.uc_stack.ss_size = _STARPU_INITIAL_THREAD_STACKSIZE;
  512. makecontext(&initial_thread->ctx, omp_initial_thread_func, 0);
  513. /* .starpu_driver */
  514. /*
  515. * we configure starpu to not launch CPU worker 0
  516. * because we will use the main thread to play the role of worker 0
  517. */
  518. struct starpu_conf conf;
  519. int ret = starpu_conf_init(&conf);
  520. STARPU_CHECK_RETURN_VALUE(ret, "starpu_conf_init");
  521. initial_thread->starpu_driver.type = STARPU_CPU_WORKER;
  522. initial_thread->starpu_driver.id.cpu_id = 0;
  523. conf.not_launched_drivers = &initial_thread->starpu_driver;
  524. conf.n_not_launched_drivers = 1;
  525. /* we are now ready to start StarPU */
  526. ret = starpu_init(&conf);
  527. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  528. ret = starpu_driver_init(&initial_thread->starpu_driver);
  529. STARPU_CHECK_RETURN_VALUE(ret, "starpu_driver_init");
  530. STARPU_PTHREAD_SETSPECIFIC(omp_task_key, initial_task);
  531. initial_thread->worker = _starpu_get_worker_struct(0);
  532. STARPU_ASSERT(initial_thread->worker);
  533. STARPU_PTHREAD_SETSPECIFIC(omp_thread_key, initial_thread);
  534. register_thread_worker(initial_thread);
  535. }
  536. static void omp_initial_thread_exit()
  537. {
  538. struct starpu_omp_thread *initial_thread = _global_state.initial_thread;
  539. int ret = starpu_driver_deinit(&initial_thread->starpu_driver);
  540. STARPU_CHECK_RETURN_VALUE(ret, "starpu_driver_deinit");
  541. memset(&initial_thread->starpu_driver, 0, sizeof (initial_thread->starpu_driver));
  542. /* the driver for the main thread is now de-inited, we can shutdown Starpu */
  543. starpu_shutdown();
  544. free(initial_thread->initial_thread_stack);
  545. initial_thread->initial_thread_stack = NULL;
  546. memset(&initial_thread->ctx, 0, sizeof (initial_thread->ctx));
  547. initial_thread->current_task = NULL;
  548. }
  549. static void omp_initial_region_setup(void)
  550. {
  551. omp_initial_thread_setup();
  552. const int max_active_levels = _starpu_omp_initial_icv_values->max_active_levels_var;
  553. const int max_threads = (int)starpu_cpu_worker_get_count();
  554. /* implementation specific initial ICV values override */
  555. if (_starpu_omp_initial_icv_values->nthreads_var[0] == 0)
  556. {
  557. _starpu_omp_initial_icv_values->nthreads_var[0] = max_threads;
  558. _starpu_omp_initial_icv_values->nthreads_var[1] = 0;
  559. }
  560. else
  561. {
  562. int i;
  563. for (i = 0; i < max_active_levels; i++)
  564. {
  565. if (_starpu_omp_initial_icv_values->nthreads_var[i] == 0)
  566. break;
  567. if (_starpu_omp_initial_icv_values->nthreads_var[i] > max_threads)
  568. {
  569. _starpu_omp_initial_icv_values->nthreads_var[i] = max_threads;
  570. }
  571. }
  572. }
  573. _starpu_omp_initial_icv_values->dyn_var = 0;
  574. _starpu_omp_initial_icv_values->nest_var = 0;
  575. _global_state.initial_device->icvs.max_active_levels_var = max_active_levels;
  576. _global_state.initial_device->icvs.def_sched_var = _starpu_omp_initial_icv_values->def_sched_var;
  577. _global_state.initial_device->icvs.def_sched_chunk_var = _starpu_omp_initial_icv_values->def_sched_chunk_var;
  578. _global_state.initial_device->icvs.stacksize_var = _starpu_omp_initial_icv_values->stacksize_var;
  579. _global_state.initial_device->icvs.wait_policy_var = _starpu_omp_initial_icv_values->wait_policy_var;
  580. _global_state.initial_region->master_thread = _global_state.initial_thread;
  581. _global_state.initial_region->nb_threads++;
  582. _global_state.initial_region->icvs.dyn_var = _starpu_omp_initial_icv_values->dyn_var;
  583. _global_state.initial_region->icvs.nest_var = _starpu_omp_initial_icv_values->nest_var;
  584. if (_starpu_omp_initial_icv_values->nthreads_var[1] != 0)
  585. {
  586. _global_state.initial_region->icvs.nthreads_var = malloc((1+max_active_levels-_global_state.initial_region->level) * sizeof(*_global_state.initial_region->icvs.nthreads_var));
  587. int i,j;
  588. for (i = _global_state.initial_region->level, j = 0; i < max_active_levels; i++, j++)
  589. {
  590. _global_state.initial_region->icvs.nthreads_var[j] = _starpu_omp_initial_icv_values->nthreads_var[j];
  591. }
  592. _global_state.initial_region->icvs.nthreads_var[j] = 0;
  593. }
  594. else
  595. {
  596. _global_state.initial_region->icvs.nthreads_var = malloc(2 * sizeof(*_global_state.initial_region->icvs.nthreads_var));
  597. _global_state.initial_region->icvs.nthreads_var[0] = _starpu_omp_initial_icv_values->nthreads_var[0];
  598. _global_state.initial_region->icvs.nthreads_var[1] = 0;
  599. }
  600. if (_starpu_omp_initial_icv_values->bind_var[1] != starpu_omp_proc_bind_undefined)
  601. {
  602. _global_state.initial_region->icvs.bind_var = malloc((1+max_active_levels-_global_state.initial_region->level) * sizeof(*_global_state.initial_region->icvs.bind_var));
  603. int i,j;
  604. for (i = _global_state.initial_region->level, j = 0; i < max_active_levels; i++, j++)
  605. {
  606. _global_state.initial_region->icvs.bind_var[j] = _starpu_omp_initial_icv_values->bind_var[j];
  607. }
  608. _global_state.initial_region->icvs.bind_var[j] = starpu_omp_proc_bind_undefined;
  609. }
  610. else
  611. {
  612. _global_state.initial_region->icvs.bind_var = malloc(2 * sizeof(*_global_state.initial_region->icvs.bind_var));
  613. _global_state.initial_region->icvs.bind_var[0] = _starpu_omp_initial_icv_values->bind_var[0];
  614. _global_state.initial_region->icvs.bind_var[1] = starpu_omp_proc_bind_undefined;
  615. }
  616. _global_state.initial_region->icvs.thread_limit_var = _starpu_omp_initial_icv_values->thread_limit_var;
  617. _global_state.initial_region->icvs.active_levels_var = 0;
  618. _global_state.initial_region->icvs.levels_var = 0;
  619. _global_state.initial_region->icvs.run_sched_var = _starpu_omp_initial_icv_values->run_sched_var;
  620. _global_state.initial_region->icvs.run_sched_chunk_var = _starpu_omp_initial_icv_values->run_sched_chunk_var;
  621. _global_state.initial_region->icvs.default_device_var = _starpu_omp_initial_icv_values->default_device_var;
  622. starpu_omp_task_list_push_back(_global_state.initial_region->implicit_task_list,
  623. _global_state.initial_task);
  624. }
  625. static void omp_initial_region_exit(void)
  626. {
  627. omp_initial_thread_exit();
  628. _global_state.initial_task->state = starpu_omp_task_state_terminated;
  629. starpu_omp_task_list_pop_front(_global_state.initial_region->implicit_task_list);
  630. _global_state.initial_region->master_thread = NULL;
  631. free(_global_state.initial_region->icvs.nthreads_var);
  632. free(_global_state.initial_region->icvs.bind_var);
  633. _global_state.initial_region->nb_threads--;
  634. }
  635. /*
  636. * Entry point to be called by the OpenMP runtime constructor
  637. */
  638. int starpu_omp_init(void)
  639. {
  640. STARPU_PTHREAD_KEY_CREATE(&omp_thread_key, NULL);
  641. STARPU_PTHREAD_KEY_CREATE(&omp_task_key, NULL);
  642. _global_state.initial_device = create_omp_device_struct();
  643. _global_state.initial_region = create_omp_region_struct(NULL, _global_state.initial_device);
  644. _global_state.initial_thread = create_omp_thread_struct(_global_state.initial_region);
  645. _global_state.initial_task = create_omp_task_struct(NULL,
  646. _global_state.initial_thread, _global_state.initial_region, 1);
  647. _global_state.default_critical = create_omp_critical_struct();
  648. _global_state.named_criticals = NULL;
  649. _starpu_spin_init(&_global_state.named_criticals_lock);
  650. _global_state.hash_workers = NULL;
  651. _starpu_spin_init(&_global_state.hash_workers_lock);
  652. _starpu_omp_environment_init();
  653. _global_state.icvs.cancel_var = _starpu_omp_initial_icv_values->cancel_var;
  654. omp_initial_region_setup();
  655. /* init clock reference for starpu_omp_get_wtick */
  656. _starpu_omp_clock_ref = starpu_timing_now();
  657. _starpu_omp_global_state = &_global_state;
  658. return 0;
  659. }
  660. void starpu_omp_shutdown(void)
  661. {
  662. omp_initial_region_exit();
  663. /* TODO: free ICV variables */
  664. /* TODO: free task/thread/region/device structures */
  665. destroy_omp_task_struct(_global_state.initial_task);
  666. _global_state.initial_task = NULL;
  667. _global_state.initial_thread = NULL;
  668. destroy_omp_region_struct(_global_state.initial_region);
  669. _global_state.initial_region = NULL;
  670. destroy_omp_device_struct(_global_state.initial_device);
  671. _global_state.initial_device = NULL;
  672. destroy_omp_critical_struct(_global_state.default_critical);
  673. _global_state.default_critical = NULL;
  674. _starpu_spin_lock(&_global_state.named_criticals_lock);
  675. {
  676. struct starpu_omp_critical *critical, *tmp;
  677. HASH_ITER(hh, _global_state.named_criticals, critical, tmp)
  678. {
  679. STARPU_ASSERT(critical != NULL);
  680. HASH_DEL(_global_state.named_criticals, critical);
  681. destroy_omp_critical_struct(critical);
  682. }
  683. }
  684. STARPU_ASSERT(_global_state.named_criticals == NULL);
  685. _starpu_spin_unlock(&_global_state.named_criticals_lock);
  686. _starpu_spin_destroy(&_global_state.named_criticals_lock);
  687. _starpu_spin_lock(&_global_state.hash_workers_lock);
  688. {
  689. struct starpu_omp_thread *thread, *tmp;
  690. HASH_ITER(hh, _global_state.hash_workers, thread, tmp)
  691. {
  692. STARPU_ASSERT(thread != NULL);
  693. HASH_DEL(_global_state.hash_workers, thread);
  694. destroy_omp_thread_struct(thread);
  695. }
  696. }
  697. STARPU_ASSERT(_global_state.hash_workers == NULL);
  698. _starpu_spin_unlock(&_global_state.hash_workers_lock);
  699. _starpu_spin_destroy(&_global_state.hash_workers_lock);
  700. _starpu_omp_environment_exit();
  701. STARPU_PTHREAD_KEY_DELETE(omp_task_key);
  702. STARPU_PTHREAD_KEY_DELETE(omp_thread_key);
  703. }
  704. void starpu_omp_parallel_region(const struct starpu_omp_parallel_region_attr *attr)
  705. {
  706. struct starpu_omp_thread *master_thread = STARPU_PTHREAD_GETSPECIFIC(omp_thread_key);
  707. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  708. struct starpu_omp_region *generating_region = task->owner_region;
  709. const int max_active_levels = generating_region->owner_device->icvs.max_active_levels_var;
  710. struct starpu_omp_region *new_region =
  711. create_omp_region_struct(generating_region, _global_state.initial_device);
  712. int ret;
  713. int nb_threads = 1;
  714. /* TODO: for now, nested parallel sections are not supported, thus we
  715. * open an active parallel section only if the generating region is the
  716. * initial region */
  717. if (attr->if_clause != 0)
  718. {
  719. const int max_threads = (int)starpu_cpu_worker_get_count();
  720. if (attr->num_threads > 0)
  721. {
  722. nb_threads = attr->num_threads;
  723. }
  724. else
  725. {
  726. nb_threads = generating_region->icvs.nthreads_var[0];
  727. }
  728. if (nb_threads > max_threads)
  729. {
  730. nb_threads = max_threads;
  731. }
  732. if (nb_threads > 1 && generating_region->icvs.active_levels_var+1 > max_active_levels)
  733. {
  734. nb_threads = 1;
  735. }
  736. }
  737. STARPU_ASSERT(nb_threads > 0);
  738. new_region->icvs.dyn_var = generating_region->icvs.dyn_var;
  739. new_region->icvs.nest_var = generating_region->icvs.nest_var;
  740. /* the nthreads_var and bind_var arrays do not hold more than
  741. * max_active_levels entries at most, even if some in-between levels
  742. * are inactive */
  743. if (new_region->level < max_active_levels)
  744. {
  745. if (generating_region->icvs.nthreads_var[1] != 0)
  746. {
  747. new_region->icvs.nthreads_var = malloc((1+max_active_levels-new_region->level) * sizeof(*new_region->icvs.nthreads_var));
  748. int i,j;
  749. for (i = new_region->level, j = 0; i < max_active_levels; i++, j++)
  750. {
  751. new_region->icvs.nthreads_var[j] = generating_region->icvs.nthreads_var[j+1];
  752. }
  753. new_region->icvs.nthreads_var[j] = 0;
  754. }
  755. else
  756. {
  757. new_region->icvs.nthreads_var = malloc(2 * sizeof(*new_region->icvs.nthreads_var));
  758. new_region->icvs.nthreads_var[0] = generating_region->icvs.nthreads_var[0];
  759. new_region->icvs.nthreads_var[1] = 0;
  760. }
  761. if (generating_region->icvs.bind_var[1] != starpu_omp_proc_bind_undefined)
  762. {
  763. new_region->icvs.bind_var = malloc((1+max_active_levels-new_region->level) * sizeof(*new_region->icvs.bind_var));
  764. int i,j;
  765. for (i = new_region->level, j = 0; i < max_active_levels; i++, j++)
  766. {
  767. new_region->icvs.bind_var[j] = generating_region->icvs.bind_var[j+1];
  768. }
  769. new_region->icvs.bind_var[j] = starpu_omp_proc_bind_undefined;
  770. }
  771. else
  772. {
  773. new_region->icvs.bind_var = malloc(2 * sizeof(*new_region->icvs.bind_var));
  774. new_region->icvs.bind_var[0] = generating_region->icvs.bind_var[0];
  775. new_region->icvs.bind_var[1] = starpu_omp_proc_bind_undefined;
  776. }
  777. }
  778. else
  779. {
  780. new_region->icvs.nthreads_var = malloc(sizeof(*new_region->icvs.nthreads_var));
  781. new_region->icvs.nthreads_var[0] = generating_region->icvs.nthreads_var[0];
  782. new_region->icvs.bind_var = malloc(sizeof(*new_region->icvs.bind_var));
  783. new_region->icvs.bind_var[0] = generating_region->icvs.bind_var[0];
  784. }
  785. new_region->icvs.thread_limit_var = generating_region->icvs.thread_limit_var;
  786. new_region->icvs.active_levels_var = (nb_threads > 1)?generating_region->icvs.active_levels_var+1:generating_region->icvs.active_levels_var;
  787. new_region->icvs.levels_var = generating_region->icvs.levels_var+1;
  788. new_region->icvs.run_sched_var = generating_region->icvs.run_sched_var;
  789. new_region->icvs.run_sched_chunk_var = generating_region->icvs.run_sched_chunk_var;
  790. new_region->icvs.default_device_var = generating_region->icvs.default_device_var;
  791. int i;
  792. for (i = 0; i < nb_threads; i++)
  793. {
  794. struct starpu_omp_thread *new_thread;
  795. if (i == 0)
  796. {
  797. new_thread = master_thread;
  798. new_region->master_thread = master_thread;
  799. }
  800. else
  801. {
  802. /* TODO: specify actual starpu worker */
  803. /* TODO: use a less arbitrary thread/worker mapping scheme */
  804. if (generating_region->level == 0)
  805. {
  806. struct _starpu_worker *worker = _starpu_get_worker_struct(i);
  807. new_thread = get_worker_thread(worker);
  808. if (new_thread == NULL)
  809. {
  810. new_thread = create_omp_thread_struct(new_region);
  811. new_thread->worker = _starpu_get_worker_struct(i);
  812. register_thread_worker(new_thread);
  813. }
  814. }
  815. else
  816. {
  817. new_thread = master_thread;
  818. }
  819. starpu_omp_thread_list_push_back(new_region->thread_list, new_thread);
  820. }
  821. struct starpu_omp_task *new_task = create_omp_task_struct(task, new_thread, new_region, 1);
  822. new_task->rank = new_region->nb_threads;
  823. new_region->nb_threads++;
  824. starpu_omp_task_list_push_back(new_region->implicit_task_list, new_task);
  825. }
  826. STARPU_ASSERT(new_region->nb_threads == nb_threads);
  827. /*
  828. * if task == initial_task, create a starpu task as a continuation to all the implicit
  829. * tasks of the new region, else prepare the task for preemption,
  830. * to become itself a continuation to the implicit tasks of the new region
  831. */
  832. if (task == _global_state.initial_task)
  833. {
  834. new_region->continuation_starpu_task = starpu_task_create();
  835. /* in that case, the continuation starpu task is only used for synchronisation */
  836. new_region->continuation_starpu_task->cl = NULL;
  837. new_region->continuation_starpu_task->workerid = master_thread->worker->workerid;
  838. new_region->continuation_starpu_task->execute_on_a_specific_worker = 1;
  839. /* this sync task will be tested for completion in omp_initial_thread_func() */
  840. new_region->continuation_starpu_task->detach = 0;
  841. }
  842. else
  843. {
  844. /* through the preemption, the parent starpu task becomes the continuation task */
  845. _starpu_task_prepare_for_continuation();
  846. new_region->continuation_starpu_task = task->starpu_task;
  847. }
  848. task->nested_region = new_region;
  849. /*
  850. * create the starpu tasks for the implicit omp tasks,
  851. * create explicit dependencies between these starpu tasks and the continuation starpu task
  852. */
  853. struct starpu_omp_task * implicit_task;
  854. for (implicit_task = starpu_omp_task_list_begin(new_region->implicit_task_list);
  855. implicit_task != starpu_omp_task_list_end(new_region->implicit_task_list);
  856. implicit_task = starpu_omp_task_list_next(implicit_task))
  857. {
  858. implicit_task->cl = attr->cl;
  859. /*
  860. * save pointer to the regions user function from the parallel region codelet
  861. *
  862. * TODO: add support for multiple/heterogeneous implementations
  863. */
  864. implicit_task->f = implicit_task->cl.cpu_funcs[0];
  865. /*
  866. * plug the task wrapper into the parallel region codelet instead, to support task preemption
  867. */
  868. implicit_task->cl.cpu_funcs[0] = starpu_omp_implicit_task_exec;
  869. implicit_task->starpu_task = starpu_task_create();
  870. implicit_task->starpu_task->cl = &implicit_task->cl;
  871. {
  872. unsigned i;
  873. for (i = 0; i < implicit_task->cl.nbuffers; i++)
  874. {
  875. implicit_task->starpu_task->handles[i] = attr->handles[i];
  876. }
  877. }
  878. implicit_task->starpu_task->cl_arg = attr->cl_arg;
  879. implicit_task->starpu_task->cl_arg_size = attr->cl_arg_size;
  880. implicit_task->starpu_task->cl_arg_free = attr->cl_arg_free;
  881. implicit_task->starpu_task->omp_task = implicit_task;
  882. implicit_task->starpu_task->workerid = implicit_task->owner_thread->worker->workerid;
  883. implicit_task->starpu_task->execute_on_a_specific_worker = 1;
  884. starpu_task_declare_deps_array(new_region->continuation_starpu_task, 1, &implicit_task->starpu_task);
  885. }
  886. attr = NULL;
  887. /*
  888. * submit all the region implicit starpu tasks
  889. */
  890. for (implicit_task = starpu_omp_task_list_begin(new_region->implicit_task_list);
  891. implicit_task != starpu_omp_task_list_end(new_region->implicit_task_list);
  892. implicit_task = starpu_omp_task_list_next(implicit_task))
  893. {
  894. ret = starpu_task_submit(implicit_task->starpu_task);
  895. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  896. }
  897. /*
  898. * submit the region continuation starpu task if task == initial_task
  899. */
  900. if (task == _global_state.initial_task)
  901. {
  902. ret = _starpu_task_submit_internally(new_region->continuation_starpu_task);
  903. STARPU_CHECK_RETURN_VALUE(ret, "_starpu_task_submit_internally");
  904. }
  905. /*
  906. * preempt for completion of the region
  907. */
  908. starpu_omp_task_preempt();
  909. if (task == _global_state.initial_task)
  910. {
  911. STARPU_ASSERT(new_region->continuation_starpu_task == NULL);
  912. }
  913. else
  914. {
  915. STARPU_ASSERT(new_region->continuation_starpu_task != NULL);
  916. new_region->continuation_starpu_task = NULL;
  917. }
  918. /*
  919. * TODO: free region resources
  920. */
  921. for (i = 0; i < nb_threads; i++)
  922. {
  923. if (i == 0)
  924. {
  925. new_region->master_thread = NULL;
  926. }
  927. else
  928. {
  929. starpu_omp_thread_list_pop_front(new_region->thread_list);
  930. /* TODO: cleanup unused threads */
  931. }
  932. new_region->nb_threads--;
  933. struct starpu_omp_task *implicit_task = starpu_omp_task_list_pop_front(new_region->implicit_task_list);
  934. destroy_omp_task_struct(implicit_task);
  935. }
  936. STARPU_ASSERT(new_region->nb_threads == 0);
  937. task->nested_region = NULL;
  938. free(new_region->icvs.bind_var);
  939. free(new_region->icvs.nthreads_var);
  940. destroy_omp_region_struct(new_region);
  941. }
  942. static void _wake_up_locked_task(struct starpu_omp_task *task)
  943. {
  944. if (task->wait_on == 0)
  945. {
  946. int ret = starpu_task_submit(task->starpu_task);
  947. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  948. }
  949. }
  950. static void wake_up_barrier(struct starpu_omp_region *parallel_region)
  951. {
  952. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  953. struct starpu_omp_task *implicit_task;
  954. for (implicit_task = starpu_omp_task_list_begin(parallel_region->implicit_task_list);
  955. implicit_task != starpu_omp_task_list_end(parallel_region->implicit_task_list);
  956. implicit_task = starpu_omp_task_list_next(implicit_task))
  957. {
  958. if (implicit_task == task)
  959. continue;
  960. _starpu_spin_lock(&implicit_task->lock);
  961. STARPU_ASSERT(implicit_task->wait_on & starpu_omp_task_wait_on_barrier);
  962. implicit_task->wait_on &= ~starpu_omp_task_wait_on_barrier;
  963. _wake_up_locked_task(implicit_task);
  964. _starpu_spin_unlock(&implicit_task->lock);
  965. }
  966. }
  967. static void barrier__sleep_callback(void *_task)
  968. {
  969. struct starpu_omp_task *task = _task;
  970. _starpu_spin_unlock(&task->lock);
  971. }
  972. static void region_tasks__sleep_callback(void *_task)
  973. {
  974. struct starpu_omp_task *task = _task;
  975. struct starpu_omp_region *parallel_region = task->owner_region;
  976. _starpu_spin_unlock(&task->lock);
  977. _starpu_spin_unlock(&parallel_region->lock);
  978. }
  979. void starpu_omp_barrier(void)
  980. {
  981. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  982. /* Assume barriers are performed in by the implicit tasks of a parallel_region */
  983. STARPU_ASSERT(task->is_implicit);
  984. struct starpu_omp_region *parallel_region = task->owner_region;
  985. _starpu_spin_lock(&task->lock);
  986. int inc_barrier_count = STARPU_ATOMIC_ADD(&parallel_region->barrier_count, 1);
  987. if (inc_barrier_count == parallel_region->nb_threads)
  988. {
  989. /* last task reaching the barrier */
  990. _starpu_spin_lock(&parallel_region->lock);
  991. parallel_region->barrier_count = 0;
  992. if (parallel_region->bound_explicit_task_count > 0)
  993. {
  994. task->wait_on |= starpu_omp_task_wait_on_region_tasks;
  995. parallel_region->waiting_task = task;
  996. _starpu_task_prepare_for_continuation_ext(0, region_tasks__sleep_callback, task);
  997. starpu_omp_task_preempt();
  998. }
  999. else
  1000. {
  1001. _starpu_spin_unlock(&task->lock);
  1002. _starpu_spin_unlock(&parallel_region->lock);
  1003. }
  1004. wake_up_barrier(parallel_region);
  1005. }
  1006. else
  1007. {
  1008. /* not the last task reaching the barrier
  1009. * . prepare for conditional continuation
  1010. * . sleep
  1011. */
  1012. task->wait_on |= starpu_omp_task_wait_on_barrier;
  1013. _starpu_task_prepare_for_continuation_ext(0, barrier__sleep_callback, task);
  1014. starpu_omp_task_preempt();
  1015. STARPU_ASSERT(task->child_task_count == 0);
  1016. }
  1017. }
  1018. void starpu_omp_master(void (*f)(void *arg), void *arg)
  1019. {
  1020. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  1021. struct starpu_omp_thread *thread = STARPU_PTHREAD_GETSPECIFIC(omp_thread_key);
  1022. /* Assume master is performed in by the implicit tasks of a region */
  1023. STARPU_ASSERT(task->is_implicit);
  1024. struct starpu_omp_region *region = task->owner_region;
  1025. if (thread == region->master_thread)
  1026. {
  1027. f(arg);
  1028. }
  1029. }
  1030. /* variant of omp_master for inlined code
  1031. * return !0 for the task that should perform the master section
  1032. * return 0 for the tasks that should not perform the master section */
  1033. int starpu_omp_master_inline(void)
  1034. {
  1035. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  1036. struct starpu_omp_thread *thread = STARPU_PTHREAD_GETSPECIFIC(omp_thread_key);
  1037. /* Assume master is performed in by the implicit tasks of a region */
  1038. STARPU_ASSERT(task->is_implicit);
  1039. struct starpu_omp_region *region = task->owner_region;
  1040. return thread == region->master_thread;
  1041. }
  1042. void starpu_omp_single(void (*f)(void *arg), void *arg, int nowait)
  1043. {
  1044. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  1045. /* Assume singles are performed in by the implicit tasks of a region */
  1046. STARPU_ASSERT(task->is_implicit);
  1047. struct starpu_omp_region *region = task->owner_region;
  1048. int first = STARPU_BOOL_COMPARE_AND_SWAP(&region->single_id, task->single_id, task->single_id+1);
  1049. task->single_id++;
  1050. if (first)
  1051. {
  1052. f(arg);
  1053. }
  1054. if (!nowait)
  1055. {
  1056. starpu_omp_barrier();
  1057. }
  1058. }
  1059. /* variant of omp_single for inlined code
  1060. * return !0 for the task that should perform the single section
  1061. * return 0 for the tasks that should not perform the single section
  1062. * wait/nowait should be handled directly by the calling code using starpu_omp_barrier */
  1063. int starpu_omp_single_inline(void)
  1064. {
  1065. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  1066. /* Assume singles are performed in by the implicit tasks of a region */
  1067. STARPU_ASSERT(task->is_implicit);
  1068. struct starpu_omp_region *region = task->owner_region;
  1069. int first = STARPU_BOOL_COMPARE_AND_SWAP(&region->single_id, task->single_id, task->single_id+1);
  1070. task->single_id++;
  1071. return first;
  1072. }
  1073. void starpu_omp_single_copyprivate(void (*f)(void *arg, void *data, unsigned long long data_size), void *arg, void *data, unsigned long long data_size)
  1074. {
  1075. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  1076. /* Assume singles are performed in by the implicit tasks of a region */
  1077. STARPU_ASSERT(task->is_implicit);
  1078. struct starpu_omp_region *region = task->owner_region;
  1079. int first = STARPU_BOOL_COMPARE_AND_SWAP(&region->single_id, task->single_id, task->single_id+1);
  1080. task->single_id++;
  1081. if (first)
  1082. {
  1083. region->copy_private_data = data;
  1084. f(arg, data, data_size);
  1085. }
  1086. starpu_omp_barrier();
  1087. if (!first)
  1088. {
  1089. memcpy(data, region->copy_private_data, data_size);
  1090. }
  1091. starpu_omp_barrier();
  1092. }
  1093. void *starpu_omp_single_copyprivate_inline_begin(void *data)
  1094. {
  1095. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  1096. /* Assume singles are performed in by the implicit tasks of a region */
  1097. STARPU_ASSERT(task->is_implicit);
  1098. struct starpu_omp_region *region = task->owner_region;
  1099. int first = STARPU_BOOL_COMPARE_AND_SWAP(&region->single_id, task->single_id, task->single_id+1);
  1100. task->single_id++;
  1101. if (first)
  1102. {
  1103. task->single_first = 1;
  1104. region->copy_private_data = data;
  1105. }
  1106. else
  1107. {
  1108. starpu_omp_barrier();
  1109. }
  1110. return first?NULL:region->copy_private_data;
  1111. }
  1112. void starpu_omp_single_copyprivate_inline_end(void)
  1113. {
  1114. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  1115. /* Assume singles are performed in by the implicit tasks of a region */
  1116. STARPU_ASSERT(task->is_implicit);
  1117. if (task->single_first)
  1118. {
  1119. task->single_first = 0;
  1120. starpu_omp_barrier();
  1121. }
  1122. starpu_omp_barrier();
  1123. }
  1124. static void critical__sleep_callback(void *_critical)
  1125. {
  1126. struct starpu_omp_critical *critical = _critical;
  1127. _starpu_spin_unlock(&critical->lock);
  1128. }
  1129. void starpu_omp_critical(void (*f)(void *arg), void *arg, const char *name)
  1130. {
  1131. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  1132. struct starpu_omp_critical *critical = NULL;
  1133. struct starpu_omp_task_link link;
  1134. if (name)
  1135. {
  1136. _starpu_spin_lock(&_global_state.named_criticals_lock);
  1137. HASH_FIND_STR(_global_state.named_criticals, name, critical);
  1138. if (critical == NULL)
  1139. {
  1140. critical = create_omp_critical_struct();
  1141. critical->name = name;
  1142. HASH_ADD_STR(_global_state.named_criticals, name, critical);
  1143. }
  1144. _starpu_spin_unlock(&_global_state.named_criticals_lock);
  1145. }
  1146. else
  1147. {
  1148. critical = _global_state.default_critical;
  1149. }
  1150. _starpu_spin_lock(&critical->lock);
  1151. while (critical->state != 0)
  1152. {
  1153. _starpu_spin_lock(&task->lock);
  1154. task->wait_on |= starpu_omp_task_wait_on_critical;
  1155. _starpu_spin_unlock(&task->lock);
  1156. link.task = task;
  1157. link.next = critical->contention_list_head;
  1158. critical->contention_list_head = &link;
  1159. _starpu_task_prepare_for_continuation_ext(0, critical__sleep_callback, critical);
  1160. starpu_omp_task_preempt();
  1161. /* re-acquire the spin lock */
  1162. _starpu_spin_lock(&critical->lock);
  1163. }
  1164. critical->state = 1;
  1165. _starpu_spin_unlock(&critical->lock);
  1166. f(arg);
  1167. _starpu_spin_lock(&critical->lock);
  1168. STARPU_ASSERT(critical->state == 1);
  1169. critical->state = 0;
  1170. if (critical->contention_list_head != NULL)
  1171. {
  1172. struct starpu_omp_task *next_task = critical->contention_list_head->task;
  1173. critical->contention_list_head = critical->contention_list_head->next;
  1174. _starpu_spin_lock(&next_task->lock);
  1175. STARPU_ASSERT(next_task->wait_on & starpu_omp_task_wait_on_critical);
  1176. next_task->wait_on &= ~starpu_omp_task_wait_on_critical;
  1177. _wake_up_locked_task(next_task);
  1178. _starpu_spin_unlock(&next_task->lock);
  1179. }
  1180. _starpu_spin_unlock(&critical->lock);
  1181. }
  1182. void starpu_omp_critical_inline_begin(const char *name)
  1183. {
  1184. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  1185. struct starpu_omp_critical *critical = NULL;
  1186. struct starpu_omp_task_link link;
  1187. if (name)
  1188. {
  1189. _starpu_spin_lock(&_global_state.named_criticals_lock);
  1190. HASH_FIND_STR(_global_state.named_criticals, name, critical);
  1191. if (critical == NULL)
  1192. {
  1193. critical = create_omp_critical_struct();
  1194. critical->name = name;
  1195. HASH_ADD_STR(_global_state.named_criticals, name, critical);
  1196. }
  1197. _starpu_spin_unlock(&_global_state.named_criticals_lock);
  1198. }
  1199. else
  1200. {
  1201. critical = _global_state.default_critical;
  1202. }
  1203. _starpu_spin_lock(&critical->lock);
  1204. while (critical->state != 0)
  1205. {
  1206. _starpu_spin_lock(&task->lock);
  1207. task->wait_on |= starpu_omp_task_wait_on_critical;
  1208. _starpu_spin_unlock(&task->lock);
  1209. link.task = task;
  1210. link.next = critical->contention_list_head;
  1211. critical->contention_list_head = &link;
  1212. _starpu_task_prepare_for_continuation_ext(0, critical__sleep_callback, critical);
  1213. starpu_omp_task_preempt();
  1214. /* re-acquire the spin lock */
  1215. _starpu_spin_lock(&critical->lock);
  1216. }
  1217. critical->state = 1;
  1218. _starpu_spin_unlock(&critical->lock);
  1219. }
  1220. void starpu_omp_critical_inline_end(const char *name)
  1221. {
  1222. struct starpu_omp_critical *critical = NULL;
  1223. if (name)
  1224. {
  1225. _starpu_spin_lock(&_global_state.named_criticals_lock);
  1226. HASH_FIND_STR(_global_state.named_criticals, name, critical);
  1227. STARPU_ASSERT(critical != NULL);
  1228. _starpu_spin_unlock(&_global_state.named_criticals_lock);
  1229. }
  1230. else
  1231. {
  1232. critical = _global_state.default_critical;
  1233. }
  1234. _starpu_spin_lock(&critical->lock);
  1235. STARPU_ASSERT(critical->state == 1);
  1236. critical->state = 0;
  1237. if (critical->contention_list_head != NULL)
  1238. {
  1239. struct starpu_omp_task *next_task = critical->contention_list_head->task;
  1240. critical->contention_list_head = critical->contention_list_head->next;
  1241. _starpu_spin_lock(&next_task->lock);
  1242. STARPU_ASSERT(next_task->wait_on & starpu_omp_task_wait_on_critical);
  1243. next_task->wait_on &= ~starpu_omp_task_wait_on_critical;
  1244. _wake_up_locked_task(next_task);
  1245. _starpu_spin_unlock(&next_task->lock);
  1246. }
  1247. _starpu_spin_unlock(&critical->lock);
  1248. }
  1249. static void explicit_task__destroy_callback(void *_task)
  1250. {
  1251. struct starpu_omp_task *task = _task;
  1252. STARPU_ASSERT(!task->is_implicit);
  1253. task->starpu_task->omp_task = NULL;
  1254. task->starpu_task = NULL;
  1255. _starpu_spin_lock(&task->lock);
  1256. if (task->child_task_count != 0)
  1257. {
  1258. task->state = starpu_omp_task_state_zombie;
  1259. _starpu_spin_unlock(&task->lock);
  1260. }
  1261. else
  1262. {
  1263. destroy_omp_task_struct(task);
  1264. }
  1265. }
  1266. void starpu_omp_task_region(const struct starpu_omp_task_region_attr *attr)
  1267. {
  1268. struct starpu_omp_task *generating_task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  1269. struct starpu_omp_region *parallel_region = generating_task->owner_region;
  1270. int is_undeferred = 0;
  1271. int is_final = 0;
  1272. int is_included = 0;
  1273. int is_merged = 0;
  1274. int is_untied = 0;
  1275. int ret;
  1276. if (!attr->if_clause)
  1277. {
  1278. is_undeferred = 1;
  1279. }
  1280. if (generating_task->is_final)
  1281. {
  1282. is_final = 1;
  1283. is_included = 1;
  1284. }
  1285. else if (attr->final_clause)
  1286. {
  1287. is_final = 1;
  1288. }
  1289. if (is_included)
  1290. {
  1291. is_undeferred = 1;
  1292. }
  1293. if ((is_undeferred || is_included) & attr->mergeable_clause)
  1294. {
  1295. is_merged = 1;
  1296. }
  1297. if (is_merged)
  1298. {
  1299. /* note: no need to backup/restore ICVs for merged tasks, merged tasks use the data environment of the caller */
  1300. unsigned i;
  1301. for (i = 0; i < attr->cl.nbuffers; i++)
  1302. {
  1303. ret = starpu_data_acquire(attr->handles[i], attr->cl.modes[i]);
  1304. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire");
  1305. }
  1306. void (*f)(void **starpu_buffers, void *starpu_cl_arg) = attr->cl.cpu_funcs[0];
  1307. f((void**)attr->handles, attr->cl_arg);
  1308. for (i = 0; i < attr->cl.nbuffers; i++)
  1309. {
  1310. starpu_data_release(attr->handles[i]);
  1311. }
  1312. if (attr->cl_arg_free)
  1313. {
  1314. free(attr->cl_arg);
  1315. }
  1316. }
  1317. else if (is_included)
  1318. {
  1319. /* TODO: backup current ICVs and setup new ICVs for the included task */
  1320. unsigned i;
  1321. for (i = 0; i < attr->cl.nbuffers; i++)
  1322. {
  1323. ret = starpu_data_acquire(attr->handles[i], attr->cl.modes[i]);
  1324. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire");
  1325. }
  1326. void (*f)(void **starpu_buffers, void *starpu_cl_arg) = attr->cl.cpu_funcs[0];
  1327. f((void**)attr->handles, attr->cl_arg);
  1328. for (i = 0; i < attr->cl.nbuffers; i++)
  1329. {
  1330. starpu_data_release(attr->handles[i]);
  1331. }
  1332. if (attr->cl_arg_free)
  1333. {
  1334. free(attr->cl_arg);
  1335. }
  1336. /* TODO: restore backuped ICVs */
  1337. }
  1338. else
  1339. {
  1340. struct starpu_omp_task *generated_task =
  1341. create_omp_task_struct(generating_task, NULL, parallel_region, 0);
  1342. generated_task->cl = attr->cl;
  1343. if (attr->untied_clause)
  1344. {
  1345. is_untied = 1;
  1346. }
  1347. generated_task->is_undeferred = is_undeferred;
  1348. generated_task->is_final = is_final;
  1349. generated_task->is_untied = is_untied;
  1350. generated_task->task_group = generating_task->task_group;
  1351. generated_task->rank = -1;
  1352. /*
  1353. * save pointer to the regions user function from the task region codelet
  1354. *
  1355. * TODO: add support for multiple/heterogeneous implementations
  1356. */
  1357. generated_task->f = generated_task->cl.cpu_funcs[0];
  1358. /*
  1359. * plug the task wrapper into the task region codelet instead, to support task preemption
  1360. */
  1361. generated_task->cl.cpu_funcs[0] = starpu_omp_explicit_task_exec;
  1362. generated_task->starpu_task = starpu_task_create();
  1363. generated_task->starpu_task->cl = &generated_task->cl;
  1364. generated_task->starpu_task->cl_arg = attr->cl_arg;
  1365. generated_task->starpu_task->cl_arg_size = attr->cl_arg_size;
  1366. generated_task->starpu_task->cl_arg_free = attr->cl_arg_free;
  1367. {
  1368. unsigned i;
  1369. for (i = 0; i < generated_task->cl.nbuffers; i++)
  1370. {
  1371. generated_task->starpu_task->handles[i] = attr->handles[i];
  1372. }
  1373. }
  1374. generated_task->starpu_task->omp_task = generated_task;
  1375. _starpu_task_set_omp_cleanup_callback(generated_task->starpu_task, explicit_task__destroy_callback, generated_task);
  1376. /* if the task is tied, execute_on_a_specific_worker will be changed to 1
  1377. * upon the first preemption of the generated task, once we know
  1378. * which worker thread has been selected */
  1379. generated_task->starpu_task->execute_on_a_specific_worker = 0;
  1380. (void)STARPU_ATOMIC_ADD(&generating_task->child_task_count, 1);
  1381. (void)STARPU_ATOMIC_ADD(&parallel_region->bound_explicit_task_count, 1);
  1382. if (generated_task->task_group)
  1383. {
  1384. (void)STARPU_ATOMIC_ADD(&generated_task->task_group->descendent_task_count, 1);
  1385. }
  1386. /* do not use the attribute struct afterward as it may become out of scope */
  1387. attr = NULL;
  1388. if (is_undeferred)
  1389. {
  1390. _starpu_task_prepare_for_continuation();
  1391. starpu_task_declare_deps_array(generating_task->starpu_task, 1,
  1392. &generated_task->starpu_task);
  1393. }
  1394. ret = starpu_task_submit(generated_task->starpu_task);
  1395. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  1396. if (is_undeferred)
  1397. {
  1398. starpu_omp_task_preempt();
  1399. }
  1400. }
  1401. }
  1402. static void task_childs__sleep_callback(void *_task)
  1403. {
  1404. struct starpu_omp_task *task = _task;
  1405. _starpu_spin_unlock(&task->lock);
  1406. }
  1407. void starpu_omp_taskwait(void)
  1408. {
  1409. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  1410. _starpu_spin_lock(&task->lock);
  1411. if (task->child_task_count > 0)
  1412. {
  1413. task->wait_on |= starpu_omp_task_wait_on_task_childs;
  1414. _starpu_task_prepare_for_continuation_ext(0, task_childs__sleep_callback, task);
  1415. starpu_omp_task_preempt();
  1416. STARPU_ASSERT(task->child_task_count == 0);
  1417. }
  1418. else
  1419. {
  1420. _starpu_spin_unlock(&task->lock);
  1421. }
  1422. }
  1423. static void group__sleep_callback(void *_task)
  1424. {
  1425. struct starpu_omp_task *task = _task;
  1426. _starpu_spin_unlock(&task->lock);
  1427. }
  1428. void starpu_omp_taskgroup(void (*f)(void *arg), void *arg)
  1429. {
  1430. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  1431. struct starpu_omp_task_group *p_previous_task_group;
  1432. struct starpu_omp_task_group task_group;
  1433. p_previous_task_group = task->task_group;
  1434. task_group.descendent_task_count = 0;
  1435. task_group.leader_task = task;
  1436. task->task_group = &task_group;
  1437. f(arg);
  1438. _starpu_spin_lock(&task->lock);
  1439. if (task_group.descendent_task_count > 0)
  1440. {
  1441. task->wait_on |= starpu_omp_task_wait_on_group;
  1442. _starpu_task_prepare_for_continuation_ext(0, group__sleep_callback, task);
  1443. starpu_omp_task_preempt();
  1444. STARPU_ASSERT(task_group.descendent_task_count == 0);
  1445. }
  1446. else
  1447. {
  1448. _starpu_spin_unlock(&task->lock);
  1449. }
  1450. task->task_group = p_previous_task_group;
  1451. }
  1452. static inline void _starpu_omp_for_loop(struct starpu_omp_region *parallel_region, struct starpu_omp_task *task,
  1453. struct starpu_omp_loop *loop, int first_call,
  1454. unsigned long long nb_iterations, unsigned long long chunk, int schedule, int ordered, unsigned long long *_first_i, unsigned long long *_nb_i)
  1455. {
  1456. *_nb_i = 0;
  1457. if (schedule == starpu_omp_sched_undefined)
  1458. {
  1459. schedule = parallel_region->owner_device->icvs.def_sched_var;
  1460. chunk = parallel_region->owner_device->icvs.def_sched_chunk_var;
  1461. }
  1462. else if (schedule == starpu_omp_sched_runtime)
  1463. {
  1464. schedule = parallel_region->icvs.run_sched_var;
  1465. chunk = parallel_region->icvs.run_sched_chunk_var;
  1466. }
  1467. STARPU_ASSERT( schedule == starpu_omp_sched_static
  1468. || schedule == starpu_omp_sched_dynamic
  1469. || schedule == starpu_omp_sched_guided
  1470. || schedule == starpu_omp_sched_auto);
  1471. if (schedule == starpu_omp_sched_auto)
  1472. {
  1473. schedule = starpu_omp_sched_static;
  1474. chunk = 0;
  1475. }
  1476. if (schedule == starpu_omp_sched_static)
  1477. {
  1478. if (chunk > 0)
  1479. {
  1480. if (first_call)
  1481. {
  1482. *_first_i = task->rank * chunk;
  1483. }
  1484. else
  1485. {
  1486. *_first_i += parallel_region->nb_threads * chunk;
  1487. }
  1488. if (*_first_i < nb_iterations)
  1489. {
  1490. if (*_first_i + chunk > nb_iterations)
  1491. {
  1492. *_nb_i = nb_iterations - *_first_i;
  1493. }
  1494. else
  1495. {
  1496. *_nb_i = chunk;
  1497. }
  1498. }
  1499. }
  1500. else
  1501. {
  1502. if (first_call)
  1503. {
  1504. *_nb_i = nb_iterations / parallel_region->nb_threads;
  1505. *_first_i = (unsigned)task->rank * (*_nb_i);
  1506. unsigned long long remainder = nb_iterations % parallel_region->nb_threads;
  1507. if (remainder > 0)
  1508. {
  1509. if ((unsigned)task->rank < remainder)
  1510. {
  1511. (*_nb_i)++;
  1512. *_first_i += (unsigned)task->rank;
  1513. }
  1514. else
  1515. {
  1516. *_first_i += remainder;
  1517. }
  1518. }
  1519. }
  1520. }
  1521. }
  1522. else if (schedule == starpu_omp_sched_dynamic)
  1523. {
  1524. if (chunk == 0)
  1525. {
  1526. chunk = 1;
  1527. }
  1528. if (first_call)
  1529. {
  1530. *_first_i = 0;
  1531. }
  1532. _starpu_spin_lock(&parallel_region->lock);
  1533. if (loop->next_iteration < nb_iterations)
  1534. {
  1535. *_first_i = loop->next_iteration;
  1536. if (*_first_i + chunk > nb_iterations)
  1537. {
  1538. *_nb_i = nb_iterations - *_first_i;
  1539. }
  1540. else
  1541. {
  1542. *_nb_i = chunk;
  1543. }
  1544. loop->next_iteration += *_nb_i;
  1545. }
  1546. _starpu_spin_unlock(&parallel_region->lock);
  1547. }
  1548. else if (schedule == starpu_omp_sched_guided)
  1549. {
  1550. if (chunk == 0)
  1551. {
  1552. chunk = 1;
  1553. }
  1554. if (first_call)
  1555. {
  1556. *_first_i = 0;
  1557. }
  1558. _starpu_spin_lock(&parallel_region->lock);
  1559. if (loop->next_iteration < nb_iterations)
  1560. {
  1561. *_first_i = loop->next_iteration;
  1562. *_nb_i = (nb_iterations - *_first_i)/parallel_region->nb_threads;
  1563. if (*_nb_i < chunk)
  1564. {
  1565. if (*_first_i+chunk > nb_iterations)
  1566. {
  1567. *_nb_i = nb_iterations - *_first_i;
  1568. }
  1569. else
  1570. {
  1571. *_nb_i = chunk;
  1572. }
  1573. }
  1574. loop->next_iteration += *_nb_i;
  1575. }
  1576. _starpu_spin_unlock(&parallel_region->lock);
  1577. }
  1578. if (ordered)
  1579. {
  1580. task->ordered_first_i = *_first_i;
  1581. task->ordered_nb_i = *_nb_i;
  1582. }
  1583. }
  1584. static inline struct starpu_omp_loop *_starpu_omp_for_get_loop(struct starpu_omp_region *parallel_region, struct starpu_omp_task *task)
  1585. {
  1586. struct starpu_omp_loop *loop;
  1587. loop = parallel_region->loop_list;
  1588. while (loop && loop->id != task->loop_id)
  1589. {
  1590. loop = loop->next_loop;
  1591. }
  1592. return loop;
  1593. }
  1594. static inline struct starpu_omp_loop *_starpu_omp_for_loop_begin(struct starpu_omp_region *parallel_region, struct starpu_omp_task *task,
  1595. int ordered)
  1596. {
  1597. struct starpu_omp_loop *loop;
  1598. _starpu_spin_lock(&parallel_region->lock);
  1599. loop = _starpu_omp_for_get_loop(parallel_region, task);
  1600. if (!loop)
  1601. {
  1602. loop = malloc(sizeof(*loop));
  1603. if (loop == NULL)
  1604. _STARPU_ERROR("memory allocation failed\n");
  1605. loop->id = task->loop_id;
  1606. loop->next_iteration = 0;
  1607. loop->nb_completed_threads = 0;
  1608. loop->next_loop = parallel_region->loop_list;
  1609. parallel_region->loop_list = loop;
  1610. if (ordered)
  1611. {
  1612. loop->ordered_iteration = 0;
  1613. _starpu_spin_init(&loop->ordered_lock);
  1614. condition_init(&loop->ordered_cond);
  1615. }
  1616. }
  1617. _starpu_spin_unlock(&parallel_region->lock);
  1618. return loop;
  1619. }
  1620. static inline void _starpu_omp_for_loop_end(struct starpu_omp_region *parallel_region, struct starpu_omp_task *task,
  1621. struct starpu_omp_loop *loop, int ordered)
  1622. {
  1623. _starpu_spin_lock(&parallel_region->lock);
  1624. loop->nb_completed_threads++;
  1625. if (loop->nb_completed_threads == parallel_region->nb_threads)
  1626. {
  1627. struct starpu_omp_loop **p_loop;
  1628. if (ordered)
  1629. {
  1630. loop->ordered_iteration = 0;
  1631. condition_exit(&loop->ordered_cond);
  1632. _starpu_spin_destroy(&loop->ordered_lock);
  1633. }
  1634. STARPU_ASSERT(loop->next_loop == NULL);
  1635. p_loop = &(parallel_region->loop_list);
  1636. while (*p_loop != loop)
  1637. {
  1638. p_loop = &((*p_loop)->next_loop);
  1639. }
  1640. *p_loop = NULL;
  1641. free(loop);
  1642. }
  1643. _starpu_spin_unlock(&parallel_region->lock);
  1644. task->loop_id++;
  1645. }
  1646. int starpu_omp_for_inline_first(unsigned long long nb_iterations, unsigned long long chunk, int schedule, int ordered, unsigned long long *_first_i, unsigned long long *_nb_i)
  1647. {
  1648. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  1649. struct starpu_omp_region *parallel_region = task->owner_region;
  1650. struct starpu_omp_loop *loop = _starpu_omp_for_loop_begin(parallel_region, task, ordered);
  1651. _starpu_omp_for_loop(parallel_region, task, loop, 1, nb_iterations, chunk, schedule, ordered, _first_i, _nb_i);
  1652. if (*_nb_i == 0)
  1653. {
  1654. _starpu_omp_for_loop_end(parallel_region, task, loop, ordered);
  1655. }
  1656. return (*_nb_i != 0);
  1657. }
  1658. int starpu_omp_for_inline_next(unsigned long long nb_iterations, unsigned long long chunk, int schedule, int ordered, unsigned long long *_first_i, unsigned long long *_nb_i)
  1659. {
  1660. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  1661. struct starpu_omp_region *parallel_region = task->owner_region;
  1662. struct starpu_omp_loop *loop = _starpu_omp_for_loop_begin(parallel_region, task, ordered);
  1663. _starpu_omp_for_loop(parallel_region, task, loop, 0, nb_iterations, chunk, schedule, ordered, _first_i, _nb_i);
  1664. if (*_nb_i == 0)
  1665. {
  1666. _starpu_omp_for_loop_end(parallel_region, task, loop, ordered);
  1667. }
  1668. return (*_nb_i != 0);
  1669. }
  1670. int starpu_omp_for_inline_first_alt(unsigned long long nb_iterations, unsigned long long chunk, int schedule, int ordered, unsigned long long *_begin_i, unsigned long long *_end_i)
  1671. {
  1672. unsigned long long nb_i;
  1673. int end = starpu_omp_for_inline_first(nb_iterations, chunk, schedule, ordered, _begin_i, &nb_i);
  1674. *_end_i = *_begin_i + nb_i;
  1675. return end;
  1676. }
  1677. int starpu_omp_for_inline_next_alt(unsigned long long nb_iterations, unsigned long long chunk, int schedule, int ordered, unsigned long long *_begin_i, unsigned long long *_end_i)
  1678. {
  1679. unsigned long long nb_i;
  1680. int end = starpu_omp_for_inline_next(nb_iterations, chunk, schedule, ordered, _begin_i, &nb_i);
  1681. *_end_i = *_begin_i + nb_i;
  1682. return end;
  1683. }
  1684. void starpu_omp_for(void (*f)(unsigned long long _first_i, unsigned long long _nb_i, void *arg), void *arg, unsigned long long nb_iterations, unsigned long long chunk, int schedule, int ordered, int nowait)
  1685. {
  1686. unsigned long long _first_i = 0;
  1687. unsigned long long _nb_i = 0;
  1688. if (starpu_omp_for_inline_first(nb_iterations, chunk, schedule, ordered, &_first_i, &_nb_i))
  1689. {
  1690. do
  1691. {
  1692. f(_first_i, _nb_i, arg);
  1693. }
  1694. while (starpu_omp_for_inline_next(nb_iterations, chunk, schedule, ordered, &_first_i, &_nb_i));
  1695. }
  1696. if (!nowait)
  1697. {
  1698. starpu_omp_barrier();
  1699. }
  1700. }
  1701. void starpu_omp_for_alt(void (*f)(unsigned long long _begin_i, unsigned long long _end_i, void *arg), void *arg, unsigned long long nb_iterations, unsigned long long chunk, int schedule, int ordered, int nowait)
  1702. {
  1703. unsigned long long _begin_i = 0;
  1704. unsigned long long _end_i = 0;
  1705. if (starpu_omp_for_inline_first_alt(nb_iterations, chunk, schedule, ordered, &_begin_i, &_end_i))
  1706. {
  1707. do
  1708. {
  1709. f(_begin_i, _end_i, arg);
  1710. }
  1711. while (starpu_omp_for_inline_next_alt(nb_iterations, chunk, schedule, ordered, &_begin_i, &_end_i));
  1712. }
  1713. if (!nowait)
  1714. {
  1715. starpu_omp_barrier();
  1716. }
  1717. }
  1718. void starpu_omp_ordered(void (*f)(void *arg), void *arg)
  1719. {
  1720. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  1721. struct starpu_omp_region *parallel_region = task->owner_region;
  1722. struct starpu_omp_loop *loop = _starpu_omp_for_get_loop(parallel_region, task);
  1723. unsigned long long i;
  1724. STARPU_ASSERT(task->ordered_nb_i > 0);
  1725. i = task->ordered_first_i;
  1726. task->ordered_first_i++;
  1727. task->ordered_nb_i--;
  1728. _starpu_spin_lock(&loop->ordered_lock);
  1729. while (i != loop->ordered_iteration)
  1730. {
  1731. STARPU_ASSERT(i > loop->ordered_iteration);
  1732. condition_wait(&loop->ordered_cond, &loop->ordered_lock);
  1733. }
  1734. f(arg);
  1735. loop->ordered_iteration++;
  1736. condition_broadcast(&loop->ordered_cond);
  1737. _starpu_spin_unlock(&loop->ordered_lock);
  1738. }
  1739. void starpu_omp_ordered_inline_begin(void)
  1740. {
  1741. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  1742. struct starpu_omp_region *parallel_region = task->owner_region;
  1743. struct starpu_omp_loop *loop = _starpu_omp_for_get_loop(parallel_region, task);
  1744. unsigned long long i;
  1745. STARPU_ASSERT(task->ordered_nb_i > 0);
  1746. i = task->ordered_first_i;
  1747. task->ordered_first_i++;
  1748. task->ordered_nb_i--;
  1749. _starpu_spin_lock(&loop->ordered_lock);
  1750. while (i != loop->ordered_iteration)
  1751. {
  1752. STARPU_ASSERT(i > loop->ordered_iteration);
  1753. condition_wait(&loop->ordered_cond, &loop->ordered_lock);
  1754. }
  1755. }
  1756. void starpu_omp_ordered_inline_end(void)
  1757. {
  1758. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  1759. struct starpu_omp_region *parallel_region = task->owner_region;
  1760. struct starpu_omp_loop *loop = _starpu_omp_for_get_loop(parallel_region, task);
  1761. loop->ordered_iteration++;
  1762. condition_broadcast(&loop->ordered_cond);
  1763. _starpu_spin_unlock(&loop->ordered_lock);
  1764. }
  1765. static inline struct starpu_omp_sections *_starpu_omp_get_sections(struct starpu_omp_region *parallel_region, struct starpu_omp_task *task)
  1766. {
  1767. struct starpu_omp_sections *sections;
  1768. sections = parallel_region->sections_list;
  1769. while (sections && sections->id != task->sections_id)
  1770. {
  1771. sections = sections->next_sections;
  1772. }
  1773. return sections;
  1774. }
  1775. static inline struct starpu_omp_sections *_starpu_omp_sections_begin(struct starpu_omp_region *parallel_region, struct starpu_omp_task *task)
  1776. {
  1777. struct starpu_omp_sections *sections;
  1778. _starpu_spin_lock(&parallel_region->lock);
  1779. sections = _starpu_omp_get_sections(parallel_region, task);
  1780. if (!sections)
  1781. {
  1782. sections = malloc(sizeof(*sections));
  1783. if (sections == NULL)
  1784. _STARPU_ERROR("memory allocation failed\n");
  1785. sections->id = task->sections_id;
  1786. sections->next_section_num = 0;
  1787. sections->nb_completed_threads = 0;
  1788. sections->next_sections = parallel_region->sections_list;
  1789. parallel_region->sections_list = sections;
  1790. }
  1791. _starpu_spin_unlock(&parallel_region->lock);
  1792. return sections;
  1793. }
  1794. static inline void _starpu_omp_sections_end(struct starpu_omp_region *parallel_region, struct starpu_omp_task *task,
  1795. struct starpu_omp_sections *sections)
  1796. {
  1797. _starpu_spin_lock(&parallel_region->lock);
  1798. sections->nb_completed_threads++;
  1799. if (sections->nb_completed_threads == parallel_region->nb_threads)
  1800. {
  1801. struct starpu_omp_sections **p_sections;
  1802. STARPU_ASSERT(sections->next_sections == NULL);
  1803. p_sections = &(parallel_region->sections_list);
  1804. while (*p_sections != sections)
  1805. {
  1806. p_sections = &((*p_sections)->next_sections);
  1807. }
  1808. *p_sections = NULL;
  1809. free(sections);
  1810. }
  1811. _starpu_spin_unlock(&parallel_region->lock);
  1812. task->sections_id++;
  1813. }
  1814. void starpu_omp_sections(unsigned long long nb_sections, void (**section_f)(void *arg), void **section_arg, int nowait)
  1815. {
  1816. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  1817. struct starpu_omp_region *parallel_region = task->owner_region;
  1818. struct starpu_omp_sections *sections = _starpu_omp_sections_begin(parallel_region, task);
  1819. for (;;)
  1820. {
  1821. void (*f)(void *arg) = NULL;
  1822. void *arg = NULL;
  1823. _starpu_spin_lock(&parallel_region->lock);
  1824. if (sections->next_section_num < nb_sections)
  1825. {
  1826. f = section_f[sections->next_section_num];
  1827. arg = section_arg[sections->next_section_num];
  1828. sections->next_section_num ++;
  1829. }
  1830. _starpu_spin_unlock(&parallel_region->lock);
  1831. if (f == NULL)
  1832. break;
  1833. f(arg);
  1834. }
  1835. _starpu_omp_sections_end(parallel_region, task, sections);
  1836. if (!nowait)
  1837. {
  1838. starpu_omp_barrier();
  1839. }
  1840. }
  1841. void starpu_omp_sections_combined(unsigned long long nb_sections, void (*section_f)(unsigned long long section_num, void *arg), void *section_arg, int nowait)
  1842. {
  1843. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  1844. struct starpu_omp_region *parallel_region = task->owner_region;
  1845. struct starpu_omp_sections *sections = _starpu_omp_sections_begin(parallel_region, task);
  1846. for (;;)
  1847. {
  1848. unsigned long long section_num;
  1849. void *arg = NULL;
  1850. _starpu_spin_lock(&parallel_region->lock);
  1851. if (sections->next_section_num < nb_sections)
  1852. {
  1853. section_num = sections->next_section_num;
  1854. arg = section_arg;
  1855. sections->next_section_num ++;
  1856. }
  1857. else
  1858. {
  1859. _starpu_spin_unlock(&parallel_region->lock);
  1860. break;
  1861. }
  1862. _starpu_spin_unlock(&parallel_region->lock);
  1863. section_f(section_num, arg);
  1864. }
  1865. _starpu_omp_sections_end(parallel_region, task, sections);
  1866. if (!nowait)
  1867. {
  1868. starpu_omp_barrier();
  1869. }
  1870. }
  1871. static void _starpu_omp_lock_init(void **_internal)
  1872. {
  1873. struct _starpu_omp_lock_internal * _lock;
  1874. _lock = malloc(sizeof(*_lock));
  1875. STARPU_ASSERT(_lock != NULL);
  1876. memset(_lock, 0, sizeof(*_lock));
  1877. _starpu_spin_init(&_lock->lock);
  1878. condition_init(&_lock->cond);
  1879. *_internal = _lock;
  1880. }
  1881. static void _starpu_omp_lock_destroy(void **_internal)
  1882. {
  1883. struct _starpu_omp_lock_internal * const _lock = *_internal;
  1884. STARPU_ASSERT(_lock->state == 0);
  1885. condition_exit(&_lock->cond);
  1886. _starpu_spin_destroy(&_lock->lock);
  1887. memset(_lock, 0, sizeof(*_lock));
  1888. free(_lock);
  1889. *_internal = NULL;
  1890. }
  1891. static void _starpu_omp_lock_set(void **_internal)
  1892. {
  1893. struct _starpu_omp_lock_internal * const _lock = *_internal;
  1894. _starpu_spin_lock(&_lock->lock);
  1895. while (_lock->state != 0)
  1896. {
  1897. condition_wait(&_lock->cond, &_lock->lock);
  1898. }
  1899. _lock->state = 1;
  1900. _starpu_spin_unlock(&_lock->lock);
  1901. }
  1902. static void _starpu_omp_lock_unset(void **_internal)
  1903. {
  1904. struct _starpu_omp_lock_internal * const _lock = *_internal;
  1905. _starpu_spin_lock(&_lock->lock);
  1906. STARPU_ASSERT(_lock->state == 1);
  1907. _lock->state = 0;
  1908. condition_broadcast(&_lock->cond);
  1909. _starpu_spin_unlock(&_lock->lock);
  1910. }
  1911. static int _starpu_omp_lock_test(void **_internal)
  1912. {
  1913. struct _starpu_omp_lock_internal * const _lock = *_internal;
  1914. int ret = 0;
  1915. _starpu_spin_lock(&_lock->lock);
  1916. if (_lock->state == 0)
  1917. {
  1918. _lock->state = 1;
  1919. ret = 1;
  1920. }
  1921. _starpu_spin_unlock(&_lock->lock);
  1922. return ret;
  1923. }
  1924. static void _starpu_omp_nest_lock_init(void **_internal)
  1925. {
  1926. struct _starpu_omp_nest_lock_internal * _nest_lock;
  1927. _nest_lock = malloc(sizeof(*_nest_lock));
  1928. STARPU_ASSERT(_nest_lock != NULL);
  1929. memset(_nest_lock, 0, sizeof(*_nest_lock));
  1930. _starpu_spin_init(&_nest_lock->lock);
  1931. condition_init(&_nest_lock->cond);
  1932. *_internal = _nest_lock;
  1933. }
  1934. static void _starpu_omp_nest_lock_destroy(void **_internal)
  1935. {
  1936. struct _starpu_omp_nest_lock_internal * const _nest_lock = *_internal;
  1937. STARPU_ASSERT(_nest_lock->state == 0);
  1938. STARPU_ASSERT(_nest_lock->nesting == 0);
  1939. STARPU_ASSERT(_nest_lock->owner_task == NULL);
  1940. condition_exit(&_nest_lock->cond);
  1941. _starpu_spin_destroy(&_nest_lock->lock);
  1942. memset(_nest_lock, 0, sizeof(*_nest_lock));
  1943. free(_nest_lock);
  1944. *_internal = NULL;
  1945. }
  1946. static void _starpu_omp_nest_lock_set(void **_internal)
  1947. {
  1948. struct _starpu_omp_nest_lock_internal * const _nest_lock = *_internal;
  1949. struct starpu_omp_task * const task = _starpu_omp_get_task();
  1950. _starpu_spin_lock(&_nest_lock->lock);
  1951. if (_nest_lock->owner_task == task)
  1952. {
  1953. STARPU_ASSERT(_nest_lock->state == 1);
  1954. STARPU_ASSERT(_nest_lock->nesting > 0);
  1955. _nest_lock->nesting++;
  1956. }
  1957. else
  1958. {
  1959. while (_nest_lock->state != 0)
  1960. {
  1961. condition_wait(&_nest_lock->cond, &_nest_lock->lock);
  1962. }
  1963. STARPU_ASSERT(_nest_lock->nesting == 0);
  1964. STARPU_ASSERT(_nest_lock->owner_task == NULL);
  1965. _nest_lock->state = 1;
  1966. _nest_lock->owner_task = task;
  1967. _nest_lock->nesting = 1;
  1968. }
  1969. _starpu_spin_unlock(&_nest_lock->lock);
  1970. }
  1971. static void _starpu_omp_nest_lock_unset(void **_internal)
  1972. {
  1973. struct _starpu_omp_nest_lock_internal * const _nest_lock = *_internal;
  1974. struct starpu_omp_task * const task = _starpu_omp_get_task();
  1975. _starpu_spin_lock(&_nest_lock->lock);
  1976. STARPU_ASSERT(_nest_lock->owner_task == task);
  1977. STARPU_ASSERT(_nest_lock->state == 1);
  1978. STARPU_ASSERT(_nest_lock->nesting > 0);
  1979. _nest_lock->nesting--;
  1980. if (_nest_lock->nesting == 0)
  1981. {
  1982. _nest_lock->state = 0;
  1983. _nest_lock->owner_task = NULL;
  1984. condition_broadcast(&_nest_lock->cond);
  1985. }
  1986. _starpu_spin_unlock(&_nest_lock->lock);
  1987. }
  1988. static int _starpu_omp_nest_lock_test(void **_internal)
  1989. {
  1990. struct _starpu_omp_nest_lock_internal * const _nest_lock = *_internal;
  1991. struct starpu_omp_task * const task = _starpu_omp_get_task();
  1992. int ret = 0;
  1993. _starpu_spin_lock(&_nest_lock->lock);
  1994. if (_nest_lock->state == 0)
  1995. {
  1996. STARPU_ASSERT(_nest_lock->nesting == 0);
  1997. STARPU_ASSERT(_nest_lock->owner_task == NULL);
  1998. _nest_lock->state = 1;
  1999. _nest_lock->owner_task = task;
  2000. _nest_lock->nesting = 1;
  2001. ret = 1;
  2002. }
  2003. else if (_nest_lock->owner_task == task)
  2004. {
  2005. STARPU_ASSERT(_nest_lock->state == 1);
  2006. STARPU_ASSERT(_nest_lock->nesting > 0);
  2007. _nest_lock->nesting++;
  2008. ret = 1;
  2009. }
  2010. _starpu_spin_unlock(&_nest_lock->lock);
  2011. return ret;
  2012. }
  2013. void starpu_omp_init_lock (starpu_omp_lock_t *lock)
  2014. {
  2015. _starpu_omp_lock_init(&lock->internal);
  2016. }
  2017. void starpu_omp_destroy_lock (starpu_omp_lock_t *lock)
  2018. {
  2019. _starpu_omp_lock_destroy(&lock->internal);
  2020. }
  2021. void starpu_omp_set_lock (starpu_omp_lock_t *lock)
  2022. {
  2023. _starpu_omp_lock_set(&lock->internal);
  2024. }
  2025. void starpu_omp_unset_lock (starpu_omp_lock_t *lock)
  2026. {
  2027. _starpu_omp_lock_unset(&lock->internal);
  2028. }
  2029. int starpu_omp_test_lock (starpu_omp_lock_t *lock)
  2030. {
  2031. return _starpu_omp_lock_test(&lock->internal);
  2032. }
  2033. void starpu_omp_init_nest_lock (starpu_omp_nest_lock_t *nest_lock)
  2034. {
  2035. _starpu_omp_nest_lock_init(&nest_lock->internal);
  2036. }
  2037. void starpu_omp_destroy_nest_lock (starpu_omp_nest_lock_t *nest_lock)
  2038. {
  2039. _starpu_omp_nest_lock_destroy(&nest_lock->internal);
  2040. }
  2041. void starpu_omp_set_nest_lock (starpu_omp_nest_lock_t *nest_lock)
  2042. {
  2043. _starpu_omp_nest_lock_set(&nest_lock->internal);
  2044. }
  2045. void starpu_omp_unset_nest_lock (starpu_omp_nest_lock_t *nest_lock)
  2046. {
  2047. _starpu_omp_nest_lock_unset(&nest_lock->internal);
  2048. }
  2049. int starpu_omp_test_nest_lock (starpu_omp_nest_lock_t *nest_lock)
  2050. {
  2051. return _starpu_omp_nest_lock_test(&nest_lock->internal);
  2052. }
  2053. /*
  2054. * restore deprecated diagnostics (-Wdeprecated-declarations)
  2055. */
  2056. #pragma GCC diagnostic pop
  2057. #endif /* STARPU_OPENMP */