openmp_runtime_support.c 72 KB

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