openmp_runtime_support.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224
  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 (generating_region->icvs.nthreads_var[0] < max_threads)
  721. {
  722. nb_threads = generating_region->icvs.nthreads_var[0];
  723. }
  724. else
  725. {
  726. nb_threads = max_threads;
  727. }
  728. if (nb_threads > 1 && generating_region->icvs.active_levels_var+1 > max_active_levels)
  729. {
  730. nb_threads = 1;
  731. }
  732. }
  733. STARPU_ASSERT(nb_threads > 0);
  734. new_region->icvs.dyn_var = generating_region->icvs.dyn_var;
  735. new_region->icvs.nest_var = generating_region->icvs.nest_var;
  736. /* the nthreads_var and bind_var arrays do not hold more than
  737. * max_active_levels entries at most, even if some in-between levels
  738. * are inactive */
  739. if (new_region->level < max_active_levels)
  740. {
  741. if (generating_region->icvs.nthreads_var[1] != 0)
  742. {
  743. new_region->icvs.nthreads_var = malloc((1+max_active_levels-new_region->level) * sizeof(*new_region->icvs.nthreads_var));
  744. int i,j;
  745. for (i = new_region->level, j = 0; i < max_active_levels; i++, j++)
  746. {
  747. new_region->icvs.nthreads_var[j] = generating_region->icvs.nthreads_var[j+1];
  748. }
  749. new_region->icvs.nthreads_var[j] = 0;
  750. }
  751. else
  752. {
  753. new_region->icvs.nthreads_var = malloc(2 * sizeof(*new_region->icvs.nthreads_var));
  754. new_region->icvs.nthreads_var[0] = generating_region->icvs.nthreads_var[0];
  755. new_region->icvs.nthreads_var[1] = 0;
  756. }
  757. if (generating_region->icvs.bind_var[1] != starpu_omp_proc_bind_undefined)
  758. {
  759. new_region->icvs.bind_var = malloc((1+max_active_levels-new_region->level) * sizeof(*new_region->icvs.bind_var));
  760. int i,j;
  761. for (i = new_region->level, j = 0; i < max_active_levels; i++, j++)
  762. {
  763. new_region->icvs.bind_var[j] = generating_region->icvs.bind_var[j+1];
  764. }
  765. new_region->icvs.bind_var[j] = starpu_omp_proc_bind_undefined;
  766. }
  767. else
  768. {
  769. new_region->icvs.bind_var = malloc(2 * sizeof(*new_region->icvs.bind_var));
  770. new_region->icvs.bind_var[0] = generating_region->icvs.bind_var[0];
  771. new_region->icvs.bind_var[1] = starpu_omp_proc_bind_undefined;
  772. }
  773. }
  774. else
  775. {
  776. new_region->icvs.nthreads_var = malloc(sizeof(*new_region->icvs.nthreads_var));
  777. new_region->icvs.nthreads_var[0] = generating_region->icvs.nthreads_var[0];
  778. new_region->icvs.bind_var = malloc(sizeof(*new_region->icvs.bind_var));
  779. new_region->icvs.bind_var[0] = generating_region->icvs.bind_var[0];
  780. }
  781. new_region->icvs.thread_limit_var = generating_region->icvs.thread_limit_var;
  782. new_region->icvs.active_levels_var = (nb_threads > 1)?generating_region->icvs.active_levels_var+1:generating_region->icvs.active_levels_var;
  783. new_region->icvs.levels_var = generating_region->icvs.levels_var+1;
  784. new_region->icvs.run_sched_var = generating_region->icvs.run_sched_var;
  785. new_region->icvs.run_sched_chunk_var = generating_region->icvs.run_sched_chunk_var;
  786. new_region->icvs.default_device_var = generating_region->icvs.default_device_var;
  787. int i;
  788. for (i = 0; i < nb_threads; i++)
  789. {
  790. struct starpu_omp_thread *new_thread;
  791. if (i == 0)
  792. {
  793. new_thread = master_thread;
  794. new_region->master_thread = master_thread;
  795. }
  796. else
  797. {
  798. /* TODO: specify actual starpu worker */
  799. /* TODO: use a less arbitrary thread/worker mapping scheme */
  800. if (generating_region->level == 0)
  801. {
  802. struct _starpu_worker *worker = _starpu_get_worker_struct(i);
  803. new_thread = get_worker_thread(worker);
  804. if (new_thread == NULL)
  805. {
  806. new_thread = create_omp_thread_struct(new_region);
  807. new_thread->worker = _starpu_get_worker_struct(i);
  808. register_thread_worker(new_thread);
  809. }
  810. }
  811. else
  812. {
  813. new_thread = master_thread;
  814. }
  815. starpu_omp_thread_list_push_back(new_region->thread_list, new_thread);
  816. }
  817. struct starpu_omp_task *new_task = create_omp_task_struct(task, new_thread, new_region, 1);
  818. new_task->rank = new_region->nb_threads;
  819. new_region->nb_threads++;
  820. starpu_omp_task_list_push_back(new_region->implicit_task_list, new_task);
  821. }
  822. STARPU_ASSERT(new_region->nb_threads == nb_threads);
  823. /*
  824. * if task == initial_task, create a starpu task as a continuation to all the implicit
  825. * tasks of the new region, else prepare the task for preemption,
  826. * to become itself a continuation to the implicit tasks of the new region
  827. */
  828. if (task == _global_state.initial_task)
  829. {
  830. new_region->continuation_starpu_task = starpu_task_create();
  831. /* in that case, the continuation starpu task is only used for synchronisation */
  832. new_region->continuation_starpu_task->cl = NULL;
  833. new_region->continuation_starpu_task->workerid = master_thread->worker->workerid;
  834. new_region->continuation_starpu_task->execute_on_a_specific_worker = 1;
  835. /* this sync task will be tested for completion in omp_initial_thread_func() */
  836. new_region->continuation_starpu_task->detach = 0;
  837. }
  838. else
  839. {
  840. /* through the preemption, the parent starpu task becomes the continuation task */
  841. _starpu_task_prepare_for_continuation();
  842. new_region->continuation_starpu_task = task->starpu_task;
  843. }
  844. task->nested_region = new_region;
  845. /*
  846. * create the starpu tasks for the implicit omp tasks,
  847. * create explicit dependencies between these starpu tasks and the continuation starpu task
  848. */
  849. struct starpu_omp_task * implicit_task;
  850. for (implicit_task = starpu_omp_task_list_begin(new_region->implicit_task_list);
  851. implicit_task != starpu_omp_task_list_end(new_region->implicit_task_list);
  852. implicit_task = starpu_omp_task_list_next(implicit_task))
  853. {
  854. implicit_task->cl = attr->cl;
  855. /*
  856. * save pointer to the regions user function from the parallel region codelet
  857. *
  858. * TODO: add support for multiple/heterogeneous implementations
  859. */
  860. implicit_task->f = implicit_task->cl.cpu_funcs[0];
  861. /*
  862. * plug the task wrapper into the parallel region codelet instead, to support task preemption
  863. */
  864. implicit_task->cl.cpu_funcs[0] = starpu_omp_implicit_task_exec;
  865. implicit_task->starpu_task = starpu_task_create();
  866. implicit_task->starpu_task->cl = &implicit_task->cl;
  867. {
  868. unsigned i;
  869. for (i = 0; i < implicit_task->cl.nbuffers; i++)
  870. {
  871. implicit_task->starpu_task->handles[i] = attr->handles[i];
  872. }
  873. }
  874. implicit_task->starpu_task->cl_arg = attr->cl_arg;
  875. implicit_task->starpu_task->cl_arg_size = attr->cl_arg_size;
  876. implicit_task->starpu_task->cl_arg_free = attr->cl_arg_free;
  877. implicit_task->starpu_task->omp_task = implicit_task;
  878. implicit_task->starpu_task->workerid = implicit_task->owner_thread->worker->workerid;
  879. implicit_task->starpu_task->execute_on_a_specific_worker = 1;
  880. starpu_task_declare_deps_array(new_region->continuation_starpu_task, 1, &implicit_task->starpu_task);
  881. }
  882. attr = NULL;
  883. /*
  884. * submit all the region implicit starpu tasks
  885. */
  886. for (implicit_task = starpu_omp_task_list_begin(new_region->implicit_task_list);
  887. implicit_task != starpu_omp_task_list_end(new_region->implicit_task_list);
  888. implicit_task = starpu_omp_task_list_next(implicit_task))
  889. {
  890. ret = starpu_task_submit(implicit_task->starpu_task);
  891. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  892. }
  893. /*
  894. * submit the region continuation starpu task if task == initial_task
  895. */
  896. if (task == _global_state.initial_task)
  897. {
  898. ret = _starpu_task_submit_internally(new_region->continuation_starpu_task);
  899. STARPU_CHECK_RETURN_VALUE(ret, "_starpu_task_submit_internally");
  900. }
  901. /*
  902. * preempt for completion of the region
  903. */
  904. starpu_omp_task_preempt();
  905. if (task == _global_state.initial_task)
  906. {
  907. STARPU_ASSERT(new_region->continuation_starpu_task == NULL);
  908. }
  909. else
  910. {
  911. STARPU_ASSERT(new_region->continuation_starpu_task != NULL);
  912. new_region->continuation_starpu_task = NULL;
  913. }
  914. /*
  915. * TODO: free region resources
  916. */
  917. for (i = 0; i < nb_threads; i++)
  918. {
  919. if (i == 0)
  920. {
  921. new_region->master_thread = NULL;
  922. }
  923. else
  924. {
  925. starpu_omp_thread_list_pop_front(new_region->thread_list);
  926. /* TODO: cleanup unused threads */
  927. }
  928. new_region->nb_threads--;
  929. struct starpu_omp_task *implicit_task = starpu_omp_task_list_pop_front(new_region->implicit_task_list);
  930. destroy_omp_task_struct(implicit_task);
  931. }
  932. STARPU_ASSERT(new_region->nb_threads == 0);
  933. task->nested_region = NULL;
  934. free(new_region->icvs.bind_var);
  935. free(new_region->icvs.nthreads_var);
  936. destroy_omp_region_struct(new_region);
  937. }
  938. static void _wake_up_locked_task(struct starpu_omp_task *task)
  939. {
  940. if (task->wait_on == 0)
  941. {
  942. int ret = starpu_task_submit(task->starpu_task);
  943. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  944. }
  945. }
  946. static void wake_up_barrier(struct starpu_omp_region *parallel_region)
  947. {
  948. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  949. struct starpu_omp_task *implicit_task;
  950. for (implicit_task = starpu_omp_task_list_begin(parallel_region->implicit_task_list);
  951. implicit_task != starpu_omp_task_list_end(parallel_region->implicit_task_list);
  952. implicit_task = starpu_omp_task_list_next(implicit_task))
  953. {
  954. if (implicit_task == task)
  955. continue;
  956. _starpu_spin_lock(&implicit_task->lock);
  957. STARPU_ASSERT(implicit_task->wait_on & starpu_omp_task_wait_on_barrier);
  958. implicit_task->wait_on &= ~starpu_omp_task_wait_on_barrier;
  959. _wake_up_locked_task(implicit_task);
  960. _starpu_spin_unlock(&implicit_task->lock);
  961. }
  962. }
  963. static void barrier__sleep_callback(void *_task)
  964. {
  965. struct starpu_omp_task *task = _task;
  966. _starpu_spin_unlock(&task->lock);
  967. }
  968. static void region_tasks__sleep_callback(void *_task)
  969. {
  970. struct starpu_omp_task *task = _task;
  971. struct starpu_omp_region *parallel_region = task->owner_region;
  972. _starpu_spin_unlock(&task->lock);
  973. _starpu_spin_unlock(&parallel_region->lock);
  974. }
  975. void starpu_omp_barrier(void)
  976. {
  977. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  978. /* Assume barriers are performed in by the implicit tasks of a parallel_region */
  979. STARPU_ASSERT(task->is_implicit);
  980. struct starpu_omp_region *parallel_region = task->owner_region;
  981. _starpu_spin_lock(&task->lock);
  982. int inc_barrier_count = STARPU_ATOMIC_ADD(&parallel_region->barrier_count, 1);
  983. if (inc_barrier_count == parallel_region->nb_threads)
  984. {
  985. /* last task reaching the barrier */
  986. _starpu_spin_lock(&parallel_region->lock);
  987. parallel_region->barrier_count = 0;
  988. if (parallel_region->bound_explicit_task_count > 0)
  989. {
  990. task->wait_on |= starpu_omp_task_wait_on_region_tasks;
  991. parallel_region->waiting_task = task;
  992. _starpu_task_prepare_for_continuation_ext(0, region_tasks__sleep_callback, task);
  993. starpu_omp_task_preempt();
  994. }
  995. else
  996. {
  997. _starpu_spin_unlock(&task->lock);
  998. _starpu_spin_unlock(&parallel_region->lock);
  999. }
  1000. wake_up_barrier(parallel_region);
  1001. }
  1002. else
  1003. {
  1004. /* not the last task reaching the barrier
  1005. * . prepare for conditional continuation
  1006. * . sleep
  1007. */
  1008. task->wait_on |= starpu_omp_task_wait_on_barrier;
  1009. _starpu_task_prepare_for_continuation_ext(0, barrier__sleep_callback, task);
  1010. starpu_omp_task_preempt();
  1011. STARPU_ASSERT(task->child_task_count == 0);
  1012. }
  1013. }
  1014. void starpu_omp_master(void (*f)(void *arg), void *arg)
  1015. {
  1016. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  1017. struct starpu_omp_thread *thread = STARPU_PTHREAD_GETSPECIFIC(omp_thread_key);
  1018. /* Assume master is performed in by the implicit tasks of a region */
  1019. STARPU_ASSERT(task->is_implicit);
  1020. struct starpu_omp_region *region = task->owner_region;
  1021. if (thread == region->master_thread)
  1022. {
  1023. f(arg);
  1024. }
  1025. }
  1026. /* variant of omp_master for inlined code
  1027. * return !0 for the task that should perform the master section
  1028. * return 0 for the tasks that should not perform the master section */
  1029. int starpu_omp_master_inline(void)
  1030. {
  1031. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  1032. struct starpu_omp_thread *thread = STARPU_PTHREAD_GETSPECIFIC(omp_thread_key);
  1033. /* Assume master is performed in by the implicit tasks of a region */
  1034. STARPU_ASSERT(task->is_implicit);
  1035. struct starpu_omp_region *region = task->owner_region;
  1036. return thread == region->master_thread;
  1037. }
  1038. void starpu_omp_single(void (*f)(void *arg), void *arg, int nowait)
  1039. {
  1040. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  1041. /* Assume singles are performed in by the implicit tasks of a region */
  1042. STARPU_ASSERT(task->is_implicit);
  1043. struct starpu_omp_region *region = task->owner_region;
  1044. int first = STARPU_BOOL_COMPARE_AND_SWAP(&region->single_id, task->single_id, task->single_id+1);
  1045. task->single_id++;
  1046. if (first)
  1047. {
  1048. f(arg);
  1049. }
  1050. if (!nowait)
  1051. {
  1052. starpu_omp_barrier();
  1053. }
  1054. }
  1055. /* variant of omp_single for inlined code
  1056. * return !0 for the task that should perform the single section
  1057. * return 0 for the tasks that should not perform the single section
  1058. * wait/nowait should be handled directly by the calling code using starpu_omp_barrier */
  1059. int starpu_omp_single_inline(void)
  1060. {
  1061. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  1062. /* Assume singles are performed in by the implicit tasks of a region */
  1063. STARPU_ASSERT(task->is_implicit);
  1064. struct starpu_omp_region *region = task->owner_region;
  1065. int first = STARPU_BOOL_COMPARE_AND_SWAP(&region->single_id, task->single_id, task->single_id+1);
  1066. task->single_id++;
  1067. return first;
  1068. }
  1069. 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)
  1070. {
  1071. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  1072. /* Assume singles are performed in by the implicit tasks of a region */
  1073. STARPU_ASSERT(task->is_implicit);
  1074. struct starpu_omp_region *region = task->owner_region;
  1075. int first = STARPU_BOOL_COMPARE_AND_SWAP(&region->single_id, task->single_id, task->single_id+1);
  1076. task->single_id++;
  1077. if (first)
  1078. {
  1079. region->copy_private_data = data;
  1080. f(arg, data, data_size);
  1081. }
  1082. starpu_omp_barrier();
  1083. if (!first)
  1084. {
  1085. memcpy(data, region->copy_private_data, data_size);
  1086. }
  1087. starpu_omp_barrier();
  1088. }
  1089. void *starpu_omp_single_copyprivate_inline_begin(void *data)
  1090. {
  1091. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  1092. /* Assume singles are performed in by the implicit tasks of a region */
  1093. STARPU_ASSERT(task->is_implicit);
  1094. struct starpu_omp_region *region = task->owner_region;
  1095. int first = STARPU_BOOL_COMPARE_AND_SWAP(&region->single_id, task->single_id, task->single_id+1);
  1096. task->single_id++;
  1097. if (first)
  1098. {
  1099. task->single_first = 1;
  1100. region->copy_private_data = data;
  1101. }
  1102. else
  1103. {
  1104. starpu_omp_barrier();
  1105. }
  1106. return first?NULL:region->copy_private_data;
  1107. }
  1108. void starpu_omp_single_copyprivate_inline_end(void)
  1109. {
  1110. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  1111. /* Assume singles are performed in by the implicit tasks of a region */
  1112. STARPU_ASSERT(task->is_implicit);
  1113. if (task->single_first)
  1114. {
  1115. task->single_first = 0;
  1116. starpu_omp_barrier();
  1117. }
  1118. starpu_omp_barrier();
  1119. }
  1120. static void critical__sleep_callback(void *_critical)
  1121. {
  1122. struct starpu_omp_critical *critical = _critical;
  1123. _starpu_spin_unlock(&critical->lock);
  1124. }
  1125. void starpu_omp_critical(void (*f)(void *arg), void *arg, const char *name)
  1126. {
  1127. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  1128. struct starpu_omp_critical *critical = NULL;
  1129. struct starpu_omp_task_link link;
  1130. if (name)
  1131. {
  1132. _starpu_spin_lock(&_global_state.named_criticals_lock);
  1133. HASH_FIND_STR(_global_state.named_criticals, name, critical);
  1134. if (critical == NULL)
  1135. {
  1136. critical = create_omp_critical_struct();
  1137. critical->name = name;
  1138. HASH_ADD_STR(_global_state.named_criticals, name, critical);
  1139. }
  1140. _starpu_spin_unlock(&_global_state.named_criticals_lock);
  1141. }
  1142. else
  1143. {
  1144. critical = _global_state.default_critical;
  1145. }
  1146. _starpu_spin_lock(&critical->lock);
  1147. while (critical->state != 0)
  1148. {
  1149. _starpu_spin_lock(&task->lock);
  1150. task->wait_on |= starpu_omp_task_wait_on_critical;
  1151. _starpu_spin_unlock(&task->lock);
  1152. link.task = task;
  1153. link.next = critical->contention_list_head;
  1154. critical->contention_list_head = &link;
  1155. _starpu_task_prepare_for_continuation_ext(0, critical__sleep_callback, critical);
  1156. starpu_omp_task_preempt();
  1157. /* re-acquire the spin lock */
  1158. _starpu_spin_lock(&critical->lock);
  1159. }
  1160. critical->state = 1;
  1161. _starpu_spin_unlock(&critical->lock);
  1162. f(arg);
  1163. _starpu_spin_lock(&critical->lock);
  1164. STARPU_ASSERT(critical->state == 1);
  1165. critical->state = 0;
  1166. if (critical->contention_list_head != NULL)
  1167. {
  1168. struct starpu_omp_task *next_task = critical->contention_list_head->task;
  1169. critical->contention_list_head = critical->contention_list_head->next;
  1170. _starpu_spin_lock(&next_task->lock);
  1171. STARPU_ASSERT(next_task->wait_on & starpu_omp_task_wait_on_critical);
  1172. next_task->wait_on &= ~starpu_omp_task_wait_on_critical;
  1173. _wake_up_locked_task(next_task);
  1174. _starpu_spin_unlock(&next_task->lock);
  1175. }
  1176. _starpu_spin_unlock(&critical->lock);
  1177. }
  1178. void starpu_omp_critical_inline_begin(const char *name)
  1179. {
  1180. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  1181. struct starpu_omp_critical *critical = NULL;
  1182. struct starpu_omp_task_link link;
  1183. if (name)
  1184. {
  1185. _starpu_spin_lock(&_global_state.named_criticals_lock);
  1186. HASH_FIND_STR(_global_state.named_criticals, name, critical);
  1187. if (critical == NULL)
  1188. {
  1189. critical = create_omp_critical_struct();
  1190. critical->name = name;
  1191. HASH_ADD_STR(_global_state.named_criticals, name, critical);
  1192. }
  1193. _starpu_spin_unlock(&_global_state.named_criticals_lock);
  1194. }
  1195. else
  1196. {
  1197. critical = _global_state.default_critical;
  1198. }
  1199. _starpu_spin_lock(&critical->lock);
  1200. while (critical->state != 0)
  1201. {
  1202. _starpu_spin_lock(&task->lock);
  1203. task->wait_on |= starpu_omp_task_wait_on_critical;
  1204. _starpu_spin_unlock(&task->lock);
  1205. link.task = task;
  1206. link.next = critical->contention_list_head;
  1207. critical->contention_list_head = &link;
  1208. _starpu_task_prepare_for_continuation_ext(0, critical__sleep_callback, critical);
  1209. starpu_omp_task_preempt();
  1210. /* re-acquire the spin lock */
  1211. _starpu_spin_lock(&critical->lock);
  1212. }
  1213. critical->state = 1;
  1214. _starpu_spin_unlock(&critical->lock);
  1215. }
  1216. void starpu_omp_critical_inline_end(const char *name)
  1217. {
  1218. struct starpu_omp_critical *critical = NULL;
  1219. if (name)
  1220. {
  1221. _starpu_spin_lock(&_global_state.named_criticals_lock);
  1222. HASH_FIND_STR(_global_state.named_criticals, name, critical);
  1223. STARPU_ASSERT(critical != NULL);
  1224. _starpu_spin_unlock(&_global_state.named_criticals_lock);
  1225. }
  1226. else
  1227. {
  1228. critical = _global_state.default_critical;
  1229. }
  1230. _starpu_spin_lock(&critical->lock);
  1231. STARPU_ASSERT(critical->state == 1);
  1232. critical->state = 0;
  1233. if (critical->contention_list_head != NULL)
  1234. {
  1235. struct starpu_omp_task *next_task = critical->contention_list_head->task;
  1236. critical->contention_list_head = critical->contention_list_head->next;
  1237. _starpu_spin_lock(&next_task->lock);
  1238. STARPU_ASSERT(next_task->wait_on & starpu_omp_task_wait_on_critical);
  1239. next_task->wait_on &= ~starpu_omp_task_wait_on_critical;
  1240. _wake_up_locked_task(next_task);
  1241. _starpu_spin_unlock(&next_task->lock);
  1242. }
  1243. _starpu_spin_unlock(&critical->lock);
  1244. }
  1245. static void explicit_task__destroy_callback(void *_task)
  1246. {
  1247. struct starpu_omp_task *task = _task;
  1248. STARPU_ASSERT(!task->is_implicit);
  1249. task->starpu_task->omp_task = NULL;
  1250. task->starpu_task = NULL;
  1251. _starpu_spin_lock(&task->lock);
  1252. if (task->child_task_count != 0)
  1253. {
  1254. task->state = starpu_omp_task_state_zombie;
  1255. _starpu_spin_unlock(&task->lock);
  1256. }
  1257. else
  1258. {
  1259. destroy_omp_task_struct(task);
  1260. }
  1261. }
  1262. void starpu_omp_task_region(const struct starpu_omp_task_region_attr *attr)
  1263. {
  1264. struct starpu_omp_task *generating_task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  1265. struct starpu_omp_region *parallel_region = generating_task->owner_region;
  1266. int is_undeferred = 0;
  1267. int is_final = 0;
  1268. int is_included = 0;
  1269. int is_merged = 0;
  1270. int is_untied = 0;
  1271. int ret;
  1272. if (!attr->if_clause)
  1273. {
  1274. is_undeferred = 1;
  1275. }
  1276. if (generating_task->is_final)
  1277. {
  1278. is_final = 1;
  1279. is_included = 1;
  1280. }
  1281. else if (attr->final_clause)
  1282. {
  1283. is_final = 1;
  1284. }
  1285. if (is_included)
  1286. {
  1287. is_undeferred = 1;
  1288. }
  1289. if ((is_undeferred || is_included) & attr->mergeable_clause)
  1290. {
  1291. is_merged = 1;
  1292. }
  1293. if (is_merged)
  1294. {
  1295. /* note: no need to backup/restore ICVs for merged tasks, merged tasks use the data environment of the caller */
  1296. unsigned i;
  1297. for (i = 0; i < attr->cl.nbuffers; i++)
  1298. {
  1299. ret = starpu_data_acquire(attr->handles[i], attr->cl.modes[i]);
  1300. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire");
  1301. }
  1302. void (*f)(void **starpu_buffers, void *starpu_cl_arg) = attr->cl.cpu_funcs[0];
  1303. f((void**)attr->handles, attr->cl_arg);
  1304. for (i = 0; i < attr->cl.nbuffers; i++)
  1305. {
  1306. starpu_data_release(attr->handles[i]);
  1307. }
  1308. if (attr->cl_arg_free)
  1309. {
  1310. free(attr->cl_arg);
  1311. }
  1312. }
  1313. else if (is_included)
  1314. {
  1315. /* TODO: backup current ICVs and setup new ICVs for the included task */
  1316. unsigned i;
  1317. for (i = 0; i < attr->cl.nbuffers; i++)
  1318. {
  1319. ret = starpu_data_acquire(attr->handles[i], attr->cl.modes[i]);
  1320. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire");
  1321. }
  1322. void (*f)(void **starpu_buffers, void *starpu_cl_arg) = attr->cl.cpu_funcs[0];
  1323. f((void**)attr->handles, attr->cl_arg);
  1324. for (i = 0; i < attr->cl.nbuffers; i++)
  1325. {
  1326. starpu_data_release(attr->handles[i]);
  1327. }
  1328. if (attr->cl_arg_free)
  1329. {
  1330. free(attr->cl_arg);
  1331. }
  1332. /* TODO: restore backuped ICVs */
  1333. }
  1334. else
  1335. {
  1336. struct starpu_omp_task *generated_task =
  1337. create_omp_task_struct(generating_task, NULL, parallel_region, 0);
  1338. generated_task->cl = attr->cl;
  1339. if (attr->untied_clause)
  1340. {
  1341. is_untied = 1;
  1342. }
  1343. generated_task->is_undeferred = is_undeferred;
  1344. generated_task->is_final = is_final;
  1345. generated_task->is_untied = is_untied;
  1346. generated_task->task_group = generating_task->task_group;
  1347. generated_task->rank = -1;
  1348. /*
  1349. * save pointer to the regions user function from the task region codelet
  1350. *
  1351. * TODO: add support for multiple/heterogeneous implementations
  1352. */
  1353. generated_task->f = generated_task->cl.cpu_funcs[0];
  1354. /*
  1355. * plug the task wrapper into the task region codelet instead, to support task preemption
  1356. */
  1357. generated_task->cl.cpu_funcs[0] = starpu_omp_explicit_task_exec;
  1358. generated_task->starpu_task = starpu_task_create();
  1359. generated_task->starpu_task->cl = &generated_task->cl;
  1360. generated_task->starpu_task->cl_arg = attr->cl_arg;
  1361. generated_task->starpu_task->cl_arg_size = attr->cl_arg_size;
  1362. generated_task->starpu_task->cl_arg_free = attr->cl_arg_free;
  1363. {
  1364. unsigned i;
  1365. for (i = 0; i < generated_task->cl.nbuffers; i++)
  1366. {
  1367. generated_task->starpu_task->handles[i] = attr->handles[i];
  1368. }
  1369. }
  1370. generated_task->starpu_task->omp_task = generated_task;
  1371. _starpu_task_set_omp_cleanup_callback(generated_task->starpu_task, explicit_task__destroy_callback, generated_task);
  1372. /* if the task is tied, execute_on_a_specific_worker will be changed to 1
  1373. * upon the first preemption of the generated task, once we know
  1374. * which worker thread has been selected */
  1375. generated_task->starpu_task->execute_on_a_specific_worker = 0;
  1376. (void)STARPU_ATOMIC_ADD(&generating_task->child_task_count, 1);
  1377. (void)STARPU_ATOMIC_ADD(&parallel_region->bound_explicit_task_count, 1);
  1378. if (generated_task->task_group)
  1379. {
  1380. (void)STARPU_ATOMIC_ADD(&generated_task->task_group->descendent_task_count, 1);
  1381. }
  1382. /* do not use the attribute struct afterward as it may become out of scope */
  1383. attr = NULL;
  1384. if (is_undeferred)
  1385. {
  1386. _starpu_task_prepare_for_continuation();
  1387. starpu_task_declare_deps_array(generating_task->starpu_task, 1,
  1388. &generated_task->starpu_task);
  1389. }
  1390. ret = starpu_task_submit(generated_task->starpu_task);
  1391. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  1392. if (is_undeferred)
  1393. {
  1394. starpu_omp_task_preempt();
  1395. }
  1396. }
  1397. }
  1398. static void task_childs__sleep_callback(void *_task)
  1399. {
  1400. struct starpu_omp_task *task = _task;
  1401. _starpu_spin_unlock(&task->lock);
  1402. }
  1403. void starpu_omp_taskwait(void)
  1404. {
  1405. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  1406. _starpu_spin_lock(&task->lock);
  1407. if (task->child_task_count > 0)
  1408. {
  1409. task->wait_on |= starpu_omp_task_wait_on_task_childs;
  1410. _starpu_task_prepare_for_continuation_ext(0, task_childs__sleep_callback, task);
  1411. starpu_omp_task_preempt();
  1412. STARPU_ASSERT(task->child_task_count == 0);
  1413. }
  1414. else
  1415. {
  1416. _starpu_spin_unlock(&task->lock);
  1417. }
  1418. }
  1419. static void group__sleep_callback(void *_task)
  1420. {
  1421. struct starpu_omp_task *task = _task;
  1422. _starpu_spin_unlock(&task->lock);
  1423. }
  1424. void starpu_omp_taskgroup(void (*f)(void *arg), void *arg)
  1425. {
  1426. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  1427. struct starpu_omp_task_group *p_previous_task_group;
  1428. struct starpu_omp_task_group task_group;
  1429. p_previous_task_group = task->task_group;
  1430. task_group.descendent_task_count = 0;
  1431. task_group.leader_task = task;
  1432. task->task_group = &task_group;
  1433. f(arg);
  1434. _starpu_spin_lock(&task->lock);
  1435. if (task_group.descendent_task_count > 0)
  1436. {
  1437. task->wait_on |= starpu_omp_task_wait_on_group;
  1438. _starpu_task_prepare_for_continuation_ext(0, group__sleep_callback, task);
  1439. starpu_omp_task_preempt();
  1440. STARPU_ASSERT(task_group.descendent_task_count == 0);
  1441. }
  1442. else
  1443. {
  1444. _starpu_spin_unlock(&task->lock);
  1445. }
  1446. task->task_group = p_previous_task_group;
  1447. }
  1448. static inline void _starpu_omp_for_loop(struct starpu_omp_region *parallel_region, struct starpu_omp_task *task,
  1449. struct starpu_omp_loop *loop, int first_call,
  1450. unsigned long long nb_iterations, unsigned long long chunk, int schedule, int ordered, unsigned long long *_first_i, unsigned long long *_nb_i)
  1451. {
  1452. *_nb_i = 0;
  1453. if (schedule == starpu_omp_sched_undefined)
  1454. {
  1455. schedule = parallel_region->owner_device->icvs.def_sched_var;
  1456. chunk = parallel_region->owner_device->icvs.def_sched_chunk_var;
  1457. }
  1458. else if (schedule == starpu_omp_sched_runtime)
  1459. {
  1460. schedule = parallel_region->icvs.run_sched_var;
  1461. chunk = parallel_region->icvs.run_sched_chunk_var;
  1462. }
  1463. STARPU_ASSERT( schedule == starpu_omp_sched_static
  1464. || schedule == starpu_omp_sched_dynamic
  1465. || schedule == starpu_omp_sched_guided
  1466. || schedule == starpu_omp_sched_auto);
  1467. if (schedule == starpu_omp_sched_auto)
  1468. {
  1469. schedule = starpu_omp_sched_static;
  1470. chunk = 0;
  1471. }
  1472. if (schedule == starpu_omp_sched_static)
  1473. {
  1474. if (chunk > 0)
  1475. {
  1476. if (first_call)
  1477. {
  1478. *_first_i = task->rank * chunk;
  1479. }
  1480. else
  1481. {
  1482. *_first_i += parallel_region->nb_threads * chunk;
  1483. }
  1484. if (*_first_i < nb_iterations)
  1485. {
  1486. if (*_first_i + chunk > nb_iterations)
  1487. {
  1488. *_nb_i = nb_iterations - *_first_i;
  1489. }
  1490. else
  1491. {
  1492. *_nb_i = chunk;
  1493. }
  1494. }
  1495. }
  1496. else
  1497. {
  1498. if (first_call)
  1499. {
  1500. *_nb_i = nb_iterations / parallel_region->nb_threads;
  1501. *_first_i = (unsigned)task->rank * (*_nb_i);
  1502. unsigned long long remainder = nb_iterations % parallel_region->nb_threads;
  1503. if (remainder > 0)
  1504. {
  1505. if ((unsigned)task->rank < remainder)
  1506. {
  1507. (*_nb_i)++;
  1508. *_first_i += (unsigned)task->rank;
  1509. }
  1510. else
  1511. {
  1512. *_first_i += remainder;
  1513. }
  1514. }
  1515. }
  1516. }
  1517. }
  1518. else if (schedule == starpu_omp_sched_dynamic)
  1519. {
  1520. if (chunk == 0)
  1521. {
  1522. chunk = 1;
  1523. }
  1524. if (first_call)
  1525. {
  1526. *_first_i = 0;
  1527. }
  1528. _starpu_spin_lock(&parallel_region->lock);
  1529. if (loop->next_iteration < nb_iterations)
  1530. {
  1531. *_first_i = loop->next_iteration;
  1532. if (*_first_i + chunk > nb_iterations)
  1533. {
  1534. *_nb_i = nb_iterations - *_first_i;
  1535. }
  1536. else
  1537. {
  1538. *_nb_i = chunk;
  1539. }
  1540. loop->next_iteration += *_nb_i;
  1541. }
  1542. _starpu_spin_unlock(&parallel_region->lock);
  1543. }
  1544. else if (schedule == starpu_omp_sched_guided)
  1545. {
  1546. if (chunk == 0)
  1547. {
  1548. chunk = 1;
  1549. }
  1550. if (first_call)
  1551. {
  1552. *_first_i = 0;
  1553. }
  1554. _starpu_spin_lock(&parallel_region->lock);
  1555. if (loop->next_iteration < nb_iterations)
  1556. {
  1557. *_first_i = loop->next_iteration;
  1558. *_nb_i = (nb_iterations - *_first_i)/parallel_region->nb_threads;
  1559. if (*_nb_i < chunk)
  1560. {
  1561. if (*_first_i+chunk > nb_iterations)
  1562. {
  1563. *_nb_i = nb_iterations - *_first_i;
  1564. }
  1565. else
  1566. {
  1567. *_nb_i = chunk;
  1568. }
  1569. }
  1570. loop->next_iteration += *_nb_i;
  1571. }
  1572. _starpu_spin_unlock(&parallel_region->lock);
  1573. }
  1574. if (ordered)
  1575. {
  1576. task->ordered_first_i = *_first_i;
  1577. task->ordered_nb_i = *_nb_i;
  1578. }
  1579. }
  1580. static inline struct starpu_omp_loop *_starpu_omp_for_get_loop(struct starpu_omp_region *parallel_region, struct starpu_omp_task *task)
  1581. {
  1582. struct starpu_omp_loop *loop;
  1583. loop = parallel_region->loop_list;
  1584. while (loop && loop->id != task->loop_id)
  1585. {
  1586. loop = loop->next_loop;
  1587. }
  1588. return loop;
  1589. }
  1590. static inline struct starpu_omp_loop *_starpu_omp_for_loop_begin(struct starpu_omp_region *parallel_region, struct starpu_omp_task *task,
  1591. int ordered)
  1592. {
  1593. struct starpu_omp_loop *loop;
  1594. _starpu_spin_lock(&parallel_region->lock);
  1595. loop = _starpu_omp_for_get_loop(parallel_region, task);
  1596. if (!loop)
  1597. {
  1598. loop = malloc(sizeof(*loop));
  1599. if (loop == NULL)
  1600. _STARPU_ERROR("memory allocation failed\n");
  1601. loop->id = task->loop_id;
  1602. loop->next_iteration = 0;
  1603. loop->nb_completed_threads = 0;
  1604. loop->next_loop = parallel_region->loop_list;
  1605. parallel_region->loop_list = loop;
  1606. if (ordered)
  1607. {
  1608. loop->ordered_iteration = 0;
  1609. _starpu_spin_init(&loop->ordered_lock);
  1610. condition_init(&loop->ordered_cond);
  1611. }
  1612. }
  1613. _starpu_spin_unlock(&parallel_region->lock);
  1614. return loop;
  1615. }
  1616. static inline void _starpu_omp_for_loop_end(struct starpu_omp_region *parallel_region, struct starpu_omp_task *task,
  1617. struct starpu_omp_loop *loop, int ordered)
  1618. {
  1619. _starpu_spin_lock(&parallel_region->lock);
  1620. loop->nb_completed_threads++;
  1621. if (loop->nb_completed_threads == parallel_region->nb_threads)
  1622. {
  1623. struct starpu_omp_loop **p_loop;
  1624. if (ordered)
  1625. {
  1626. loop->ordered_iteration = 0;
  1627. condition_exit(&loop->ordered_cond);
  1628. _starpu_spin_destroy(&loop->ordered_lock);
  1629. }
  1630. STARPU_ASSERT(loop->next_loop == NULL);
  1631. p_loop = &(parallel_region->loop_list);
  1632. while (*p_loop != loop)
  1633. {
  1634. p_loop = &((*p_loop)->next_loop);
  1635. }
  1636. *p_loop = NULL;
  1637. free(loop);
  1638. }
  1639. _starpu_spin_unlock(&parallel_region->lock);
  1640. task->loop_id++;
  1641. }
  1642. 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)
  1643. {
  1644. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  1645. struct starpu_omp_region *parallel_region = task->owner_region;
  1646. struct starpu_omp_loop *loop = _starpu_omp_for_loop_begin(parallel_region, task, ordered);
  1647. _starpu_omp_for_loop(parallel_region, task, loop, 1, nb_iterations, chunk, schedule, ordered, _first_i, _nb_i);
  1648. if (*_nb_i == 0)
  1649. {
  1650. _starpu_omp_for_loop_end(parallel_region, task, loop, ordered);
  1651. }
  1652. return (*_nb_i != 0);
  1653. }
  1654. 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)
  1655. {
  1656. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  1657. struct starpu_omp_region *parallel_region = task->owner_region;
  1658. struct starpu_omp_loop *loop = _starpu_omp_for_loop_begin(parallel_region, task, ordered);
  1659. _starpu_omp_for_loop(parallel_region, task, loop, 0, nb_iterations, chunk, schedule, ordered, _first_i, _nb_i);
  1660. if (*_nb_i == 0)
  1661. {
  1662. _starpu_omp_for_loop_end(parallel_region, task, loop, ordered);
  1663. }
  1664. return (*_nb_i != 0);
  1665. }
  1666. 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)
  1667. {
  1668. unsigned long long nb_i;
  1669. int end = starpu_omp_for_inline_first(nb_iterations, chunk, schedule, ordered, _begin_i, &nb_i);
  1670. *_end_i = *_begin_i + nb_i;
  1671. return end;
  1672. }
  1673. 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)
  1674. {
  1675. unsigned long long nb_i;
  1676. int end = starpu_omp_for_inline_next(nb_iterations, chunk, schedule, ordered, _begin_i, &nb_i);
  1677. *_end_i = *_begin_i + nb_i;
  1678. return end;
  1679. }
  1680. 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)
  1681. {
  1682. unsigned long long _first_i = 0;
  1683. unsigned long long _nb_i = 0;
  1684. if (starpu_omp_for_inline_first(nb_iterations, chunk, schedule, ordered, &_first_i, &_nb_i))
  1685. {
  1686. do
  1687. {
  1688. f(_first_i, _nb_i, arg);
  1689. }
  1690. while (starpu_omp_for_inline_next(nb_iterations, chunk, schedule, ordered, &_first_i, &_nb_i));
  1691. }
  1692. if (!nowait)
  1693. {
  1694. starpu_omp_barrier();
  1695. }
  1696. }
  1697. 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)
  1698. {
  1699. unsigned long long _begin_i = 0;
  1700. unsigned long long _end_i = 0;
  1701. if (starpu_omp_for_inline_first_alt(nb_iterations, chunk, schedule, ordered, &_begin_i, &_end_i))
  1702. {
  1703. do
  1704. {
  1705. f(_begin_i, _end_i, arg);
  1706. }
  1707. while (starpu_omp_for_inline_next_alt(nb_iterations, chunk, schedule, ordered, &_begin_i, &_end_i));
  1708. }
  1709. if (!nowait)
  1710. {
  1711. starpu_omp_barrier();
  1712. }
  1713. }
  1714. void starpu_omp_ordered(void (*f)(void *arg), void *arg)
  1715. {
  1716. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  1717. struct starpu_omp_region *parallel_region = task->owner_region;
  1718. struct starpu_omp_loop *loop = _starpu_omp_for_get_loop(parallel_region, task);
  1719. unsigned long long i;
  1720. STARPU_ASSERT(task->ordered_nb_i > 0);
  1721. i = task->ordered_first_i;
  1722. task->ordered_first_i++;
  1723. task->ordered_nb_i--;
  1724. _starpu_spin_lock(&loop->ordered_lock);
  1725. while (i != loop->ordered_iteration)
  1726. {
  1727. STARPU_ASSERT(i > loop->ordered_iteration);
  1728. condition_wait(&loop->ordered_cond, &loop->ordered_lock);
  1729. }
  1730. f(arg);
  1731. loop->ordered_iteration++;
  1732. condition_broadcast(&loop->ordered_cond);
  1733. _starpu_spin_unlock(&loop->ordered_lock);
  1734. }
  1735. void starpu_omp_ordered_inline_begin(void)
  1736. {
  1737. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  1738. struct starpu_omp_region *parallel_region = task->owner_region;
  1739. struct starpu_omp_loop *loop = _starpu_omp_for_get_loop(parallel_region, task);
  1740. unsigned long long i;
  1741. STARPU_ASSERT(task->ordered_nb_i > 0);
  1742. i = task->ordered_first_i;
  1743. task->ordered_first_i++;
  1744. task->ordered_nb_i--;
  1745. _starpu_spin_lock(&loop->ordered_lock);
  1746. while (i != loop->ordered_iteration)
  1747. {
  1748. STARPU_ASSERT(i > loop->ordered_iteration);
  1749. condition_wait(&loop->ordered_cond, &loop->ordered_lock);
  1750. }
  1751. }
  1752. void starpu_omp_ordered_inline_end(void)
  1753. {
  1754. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  1755. struct starpu_omp_region *parallel_region = task->owner_region;
  1756. struct starpu_omp_loop *loop = _starpu_omp_for_get_loop(parallel_region, task);
  1757. loop->ordered_iteration++;
  1758. condition_broadcast(&loop->ordered_cond);
  1759. _starpu_spin_unlock(&loop->ordered_lock);
  1760. }
  1761. static inline struct starpu_omp_sections *_starpu_omp_get_sections(struct starpu_omp_region *parallel_region, struct starpu_omp_task *task)
  1762. {
  1763. struct starpu_omp_sections *sections;
  1764. sections = parallel_region->sections_list;
  1765. while (sections && sections->id != task->sections_id)
  1766. {
  1767. sections = sections->next_sections;
  1768. }
  1769. return sections;
  1770. }
  1771. static inline struct starpu_omp_sections *_starpu_omp_sections_begin(struct starpu_omp_region *parallel_region, struct starpu_omp_task *task)
  1772. {
  1773. struct starpu_omp_sections *sections;
  1774. _starpu_spin_lock(&parallel_region->lock);
  1775. sections = _starpu_omp_get_sections(parallel_region, task);
  1776. if (!sections)
  1777. {
  1778. sections = malloc(sizeof(*sections));
  1779. if (sections == NULL)
  1780. _STARPU_ERROR("memory allocation failed\n");
  1781. sections->id = task->sections_id;
  1782. sections->next_section_num = 0;
  1783. sections->nb_completed_threads = 0;
  1784. sections->next_sections = parallel_region->sections_list;
  1785. parallel_region->sections_list = sections;
  1786. }
  1787. _starpu_spin_unlock(&parallel_region->lock);
  1788. return sections;
  1789. }
  1790. static inline void _starpu_omp_sections_end(struct starpu_omp_region *parallel_region, struct starpu_omp_task *task,
  1791. struct starpu_omp_sections *sections)
  1792. {
  1793. _starpu_spin_lock(&parallel_region->lock);
  1794. sections->nb_completed_threads++;
  1795. if (sections->nb_completed_threads == parallel_region->nb_threads)
  1796. {
  1797. struct starpu_omp_sections **p_sections;
  1798. STARPU_ASSERT(sections->next_sections == NULL);
  1799. p_sections = &(parallel_region->sections_list);
  1800. while (*p_sections != sections)
  1801. {
  1802. p_sections = &((*p_sections)->next_sections);
  1803. }
  1804. *p_sections = NULL;
  1805. free(sections);
  1806. }
  1807. _starpu_spin_unlock(&parallel_region->lock);
  1808. task->sections_id++;
  1809. }
  1810. void starpu_omp_sections(unsigned long long nb_sections, void (**section_f)(void *arg), void **section_arg, int nowait)
  1811. {
  1812. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  1813. struct starpu_omp_region *parallel_region = task->owner_region;
  1814. struct starpu_omp_sections *sections = _starpu_omp_sections_begin(parallel_region, task);
  1815. for (;;)
  1816. {
  1817. void (*f)(void *arg) = NULL;
  1818. void *arg = NULL;
  1819. _starpu_spin_lock(&parallel_region->lock);
  1820. if (sections->next_section_num < nb_sections)
  1821. {
  1822. f = section_f[sections->next_section_num];
  1823. arg = section_arg[sections->next_section_num];
  1824. sections->next_section_num ++;
  1825. }
  1826. _starpu_spin_unlock(&parallel_region->lock);
  1827. if (f == NULL)
  1828. break;
  1829. f(arg);
  1830. }
  1831. _starpu_omp_sections_end(parallel_region, task, sections);
  1832. if (!nowait)
  1833. {
  1834. starpu_omp_barrier();
  1835. }
  1836. }
  1837. 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)
  1838. {
  1839. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  1840. struct starpu_omp_region *parallel_region = task->owner_region;
  1841. struct starpu_omp_sections *sections = _starpu_omp_sections_begin(parallel_region, task);
  1842. for (;;)
  1843. {
  1844. unsigned long long section_num;
  1845. void *arg = NULL;
  1846. _starpu_spin_lock(&parallel_region->lock);
  1847. if (sections->next_section_num < nb_sections)
  1848. {
  1849. section_num = sections->next_section_num;
  1850. arg = section_arg;
  1851. sections->next_section_num ++;
  1852. }
  1853. else
  1854. {
  1855. _starpu_spin_unlock(&parallel_region->lock);
  1856. break;
  1857. }
  1858. _starpu_spin_unlock(&parallel_region->lock);
  1859. section_f(section_num, arg);
  1860. }
  1861. _starpu_omp_sections_end(parallel_region, task, sections);
  1862. if (!nowait)
  1863. {
  1864. starpu_omp_barrier();
  1865. }
  1866. }
  1867. static void _starpu_omp_lock_init(void **_internal)
  1868. {
  1869. struct _starpu_omp_lock_internal * _lock;
  1870. _lock = malloc(sizeof(*_lock));
  1871. STARPU_ASSERT(_lock != NULL);
  1872. memset(_lock, 0, sizeof(*_lock));
  1873. _starpu_spin_init(&_lock->lock);
  1874. condition_init(&_lock->cond);
  1875. *_internal = _lock;
  1876. }
  1877. static void _starpu_omp_lock_destroy(void **_internal)
  1878. {
  1879. struct _starpu_omp_lock_internal * const _lock = *_internal;
  1880. STARPU_ASSERT(_lock->state == 0);
  1881. condition_exit(&_lock->cond);
  1882. _starpu_spin_destroy(&_lock->lock);
  1883. memset(_lock, 0, sizeof(*_lock));
  1884. free(_lock);
  1885. *_internal = NULL;
  1886. }
  1887. static void _starpu_omp_lock_set(void **_internal)
  1888. {
  1889. struct _starpu_omp_lock_internal * const _lock = *_internal;
  1890. _starpu_spin_lock(&_lock->lock);
  1891. while (_lock->state != 0)
  1892. {
  1893. condition_wait(&_lock->cond, &_lock->lock);
  1894. }
  1895. _lock->state = 1;
  1896. _starpu_spin_unlock(&_lock->lock);
  1897. }
  1898. static void _starpu_omp_lock_unset(void **_internal)
  1899. {
  1900. struct _starpu_omp_lock_internal * const _lock = *_internal;
  1901. _starpu_spin_lock(&_lock->lock);
  1902. STARPU_ASSERT(_lock->state == 1);
  1903. _lock->state = 0;
  1904. condition_broadcast(&_lock->cond);
  1905. _starpu_spin_unlock(&_lock->lock);
  1906. }
  1907. static int _starpu_omp_lock_test(void **_internal)
  1908. {
  1909. struct _starpu_omp_lock_internal * const _lock = *_internal;
  1910. int ret = 0;
  1911. _starpu_spin_lock(&_lock->lock);
  1912. if (_lock->state == 0)
  1913. {
  1914. _lock->state = 1;
  1915. ret = 1;
  1916. }
  1917. _starpu_spin_unlock(&_lock->lock);
  1918. return ret;
  1919. }
  1920. static void _starpu_omp_nest_lock_init(void **_internal)
  1921. {
  1922. struct _starpu_omp_nest_lock_internal * _nest_lock;
  1923. _nest_lock = malloc(sizeof(*_nest_lock));
  1924. STARPU_ASSERT(_nest_lock != NULL);
  1925. memset(_nest_lock, 0, sizeof(*_nest_lock));
  1926. _starpu_spin_init(&_nest_lock->lock);
  1927. condition_init(&_nest_lock->cond);
  1928. *_internal = _nest_lock;
  1929. }
  1930. static void _starpu_omp_nest_lock_destroy(void **_internal)
  1931. {
  1932. struct _starpu_omp_nest_lock_internal * const _nest_lock = *_internal;
  1933. STARPU_ASSERT(_nest_lock->state == 0);
  1934. STARPU_ASSERT(_nest_lock->nesting == 0);
  1935. STARPU_ASSERT(_nest_lock->owner_task == NULL);
  1936. condition_exit(&_nest_lock->cond);
  1937. _starpu_spin_destroy(&_nest_lock->lock);
  1938. memset(_nest_lock, 0, sizeof(*_nest_lock));
  1939. free(_nest_lock);
  1940. *_internal = NULL;
  1941. }
  1942. static void _starpu_omp_nest_lock_set(void **_internal)
  1943. {
  1944. struct _starpu_omp_nest_lock_internal * const _nest_lock = *_internal;
  1945. struct starpu_omp_task * const task = _starpu_omp_get_task();
  1946. _starpu_spin_lock(&_nest_lock->lock);
  1947. if (_nest_lock->owner_task == task)
  1948. {
  1949. STARPU_ASSERT(_nest_lock->state == 1);
  1950. STARPU_ASSERT(_nest_lock->nesting > 0);
  1951. _nest_lock->nesting++;
  1952. }
  1953. else
  1954. {
  1955. while (_nest_lock->state != 0)
  1956. {
  1957. condition_wait(&_nest_lock->cond, &_nest_lock->lock);
  1958. }
  1959. STARPU_ASSERT(_nest_lock->nesting == 0);
  1960. STARPU_ASSERT(_nest_lock->owner_task == NULL);
  1961. _nest_lock->state = 1;
  1962. _nest_lock->owner_task = task;
  1963. _nest_lock->nesting = 1;
  1964. }
  1965. _starpu_spin_unlock(&_nest_lock->lock);
  1966. }
  1967. static void _starpu_omp_nest_lock_unset(void **_internal)
  1968. {
  1969. struct _starpu_omp_nest_lock_internal * const _nest_lock = *_internal;
  1970. struct starpu_omp_task * const task = _starpu_omp_get_task();
  1971. _starpu_spin_lock(&_nest_lock->lock);
  1972. STARPU_ASSERT(_nest_lock->owner_task == task);
  1973. STARPU_ASSERT(_nest_lock->state == 1);
  1974. STARPU_ASSERT(_nest_lock->nesting > 0);
  1975. _nest_lock->nesting--;
  1976. if (_nest_lock->nesting == 0)
  1977. {
  1978. _nest_lock->state = 0;
  1979. _nest_lock->owner_task = NULL;
  1980. condition_broadcast(&_nest_lock->cond);
  1981. }
  1982. _starpu_spin_unlock(&_nest_lock->lock);
  1983. }
  1984. static int _starpu_omp_nest_lock_test(void **_internal)
  1985. {
  1986. struct _starpu_omp_nest_lock_internal * const _nest_lock = *_internal;
  1987. struct starpu_omp_task * const task = _starpu_omp_get_task();
  1988. int ret = 0;
  1989. _starpu_spin_lock(&_nest_lock->lock);
  1990. if (_nest_lock->state == 0)
  1991. {
  1992. STARPU_ASSERT(_nest_lock->nesting == 0);
  1993. STARPU_ASSERT(_nest_lock->owner_task == NULL);
  1994. _nest_lock->state = 1;
  1995. _nest_lock->owner_task = task;
  1996. _nest_lock->nesting = 1;
  1997. ret = 1;
  1998. }
  1999. else if (_nest_lock->owner_task == task)
  2000. {
  2001. STARPU_ASSERT(_nest_lock->state == 1);
  2002. STARPU_ASSERT(_nest_lock->nesting > 0);
  2003. _nest_lock->nesting++;
  2004. ret = 1;
  2005. }
  2006. _starpu_spin_unlock(&_nest_lock->lock);
  2007. return ret;
  2008. }
  2009. void starpu_omp_init_lock (starpu_omp_lock_t *lock)
  2010. {
  2011. _starpu_omp_lock_init(&lock->internal);
  2012. }
  2013. void starpu_omp_destroy_lock (starpu_omp_lock_t *lock)
  2014. {
  2015. _starpu_omp_lock_destroy(&lock->internal);
  2016. }
  2017. void starpu_omp_set_lock (starpu_omp_lock_t *lock)
  2018. {
  2019. _starpu_omp_lock_set(&lock->internal);
  2020. }
  2021. void starpu_omp_unset_lock (starpu_omp_lock_t *lock)
  2022. {
  2023. _starpu_omp_lock_unset(&lock->internal);
  2024. }
  2025. int starpu_omp_test_lock (starpu_omp_lock_t *lock)
  2026. {
  2027. return _starpu_omp_lock_test(&lock->internal);
  2028. }
  2029. void starpu_omp_init_nest_lock (starpu_omp_nest_lock_t *nest_lock)
  2030. {
  2031. _starpu_omp_nest_lock_init(&nest_lock->internal);
  2032. }
  2033. void starpu_omp_destroy_nest_lock (starpu_omp_nest_lock_t *nest_lock)
  2034. {
  2035. _starpu_omp_nest_lock_destroy(&nest_lock->internal);
  2036. }
  2037. void starpu_omp_set_nest_lock (starpu_omp_nest_lock_t *nest_lock)
  2038. {
  2039. _starpu_omp_nest_lock_set(&nest_lock->internal);
  2040. }
  2041. void starpu_omp_unset_nest_lock (starpu_omp_nest_lock_t *nest_lock)
  2042. {
  2043. _starpu_omp_nest_lock_unset(&nest_lock->internal);
  2044. }
  2045. int starpu_omp_test_nest_lock (starpu_omp_nest_lock_t *nest_lock)
  2046. {
  2047. return _starpu_omp_nest_lock_test(&nest_lock->internal);
  2048. }
  2049. /*
  2050. * restore deprecated diagnostics (-Wdeprecated-declarations)
  2051. */
  2052. #pragma GCC diagnostic pop
  2053. #endif /* STARPU_OPENMP */