openmp_runtime_support.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741
  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 <stdlib.h>
  31. #include <ctype.h>
  32. #include <strings.h>
  33. #define _STARPU_STACKSIZE 2097152
  34. static struct starpu_omp_global _global_state;
  35. static starpu_pthread_key_t omp_thread_key;
  36. static starpu_pthread_key_t omp_task_key;
  37. struct starpu_omp_global *_starpu_omp_global_state = NULL;
  38. double _starpu_omp_clock_ref = 0.0; /* clock reference for starpu_omp_get_wtick */
  39. static struct starpu_omp_critical *create_omp_critical_struct(void);
  40. static void destroy_omp_critical_struct(struct starpu_omp_critical *critical);
  41. static struct starpu_omp_device *create_omp_device_struct(void);
  42. static void destroy_omp_device_struct(struct starpu_omp_device *device);
  43. static struct starpu_omp_region *create_omp_region_struct(struct starpu_omp_region *parent_region, struct starpu_omp_device *owner_device);
  44. static void destroy_omp_region_struct(struct starpu_omp_region *region);
  45. static struct starpu_omp_thread *create_omp_thread_struct(struct starpu_omp_region *owner_region);
  46. static void destroy_omp_thread_struct(struct starpu_omp_thread *thread);
  47. static struct starpu_omp_task *create_omp_task_struct(struct starpu_omp_task *parent_task,
  48. struct starpu_omp_thread *owner_thread, struct starpu_omp_region *owner_region, int is_implicit);
  49. static void destroy_omp_task_struct(struct starpu_omp_task *task);
  50. static void _wake_up_locked_task(struct starpu_omp_task *task);
  51. static void wake_up_barrier(struct starpu_omp_region *parallel_region);
  52. static void starpu_omp_task_preempt(void);
  53. struct starpu_omp_thread *_starpu_omp_get_thread(void)
  54. {
  55. struct starpu_omp_thread *thread = STARPU_PTHREAD_GETSPECIFIC(omp_thread_key);
  56. return thread;
  57. }
  58. struct starpu_omp_task *_starpu_omp_get_task(void)
  59. {
  60. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  61. return task;
  62. }
  63. static void condition_init(struct starpu_omp_condition *condition)
  64. {
  65. condition->contention_list_head = NULL;
  66. }
  67. static void condition_exit(struct starpu_omp_condition *condition)
  68. {
  69. STARPU_ASSERT(condition->contention_list_head == NULL);
  70. condition->contention_list_head = NULL;
  71. }
  72. static void condition__sleep_callback(void *_lock)
  73. {
  74. struct _starpu_spinlock *lock = _lock;
  75. _starpu_spin_unlock(lock);
  76. }
  77. static void condition_wait(struct starpu_omp_condition *condition, struct _starpu_spinlock *lock)
  78. {
  79. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  80. struct starpu_omp_task_link link;
  81. _starpu_spin_lock(&task->lock);
  82. task->wait_on |= starpu_omp_task_wait_on_condition;
  83. _starpu_spin_unlock(&task->lock);
  84. link.task = task;
  85. link.next = condition->contention_list_head;
  86. condition->contention_list_head = &link;
  87. _starpu_task_prepare_for_continuation_ext(0, condition__sleep_callback, lock);
  88. starpu_omp_task_preempt();
  89. /* re-acquire the lock released by the callback */
  90. _starpu_spin_lock(lock);
  91. }
  92. #if 0
  93. /* unused for now */
  94. static void condition_signal(struct starpu_omp_condition *condition)
  95. {
  96. if (condition->contention_list_head != NULL)
  97. {
  98. struct starpu_omp_task *next_task = condition->contention_list_head->task;
  99. condition->contention_list_head = condition->contention_list_head->next;
  100. _starpu_spin_lock(&next_task->lock);
  101. STARPU_ASSERT(next_task->wait_on & starpu_omp_task_wait_on_condition);
  102. next_task->wait_on &= ~starpu_omp_task_wait_on_condition;
  103. _wake_up_locked_task(next_task);
  104. _starpu_spin_unlock(&next_task->lock);
  105. }
  106. }
  107. #endif
  108. static void condition_broadcast(struct starpu_omp_condition *condition)
  109. {
  110. while (condition->contention_list_head != NULL)
  111. {
  112. struct starpu_omp_task *next_task = condition->contention_list_head->task;
  113. condition->contention_list_head = condition->contention_list_head->next;
  114. _starpu_spin_lock(&next_task->lock);
  115. STARPU_ASSERT(next_task->wait_on & starpu_omp_task_wait_on_condition);
  116. next_task->wait_on &= ~starpu_omp_task_wait_on_condition;
  117. _wake_up_locked_task(next_task);
  118. _starpu_spin_unlock(&next_task->lock);
  119. }
  120. }
  121. static void register_thread_worker(struct starpu_omp_thread *thread)
  122. {
  123. STARPU_ASSERT(thread->worker != NULL);
  124. _starpu_spin_lock(&_global_state.hash_workers_lock);
  125. struct _starpu_worker *check = thread->worker;
  126. struct starpu_omp_thread *tmp = NULL;
  127. HASH_FIND_PTR(_global_state.hash_workers, &check, tmp);
  128. STARPU_ASSERT(tmp == NULL);
  129. HASH_ADD_PTR(_global_state.hash_workers, worker, thread);
  130. _starpu_spin_unlock(&_global_state.hash_workers_lock);
  131. }
  132. static struct starpu_omp_thread *get_worker_thread(struct _starpu_worker *starpu_worker)
  133. {
  134. struct starpu_omp_thread *thread = NULL;
  135. _starpu_spin_lock(&_global_state.hash_workers_lock);
  136. HASH_FIND_PTR(_global_state.hash_workers, &starpu_worker, thread);
  137. _starpu_spin_unlock(&_global_state.hash_workers_lock);
  138. return thread;
  139. }
  140. static struct starpu_omp_thread *get_local_thread(void)
  141. {
  142. struct starpu_omp_thread *thread = STARPU_PTHREAD_GETSPECIFIC(omp_thread_key);
  143. if (thread == NULL)
  144. {
  145. struct _starpu_worker *starpu_worker = _starpu_get_local_worker_key();
  146. STARPU_ASSERT(starpu_worker != NULL);
  147. _starpu_spin_lock(&_global_state.hash_workers_lock);
  148. HASH_FIND_PTR(_global_state.hash_workers, &starpu_worker, thread);
  149. STARPU_ASSERT(thread != NULL);
  150. _starpu_spin_unlock(&_global_state.hash_workers_lock);
  151. STARPU_PTHREAD_SETSPECIFIC(omp_thread_key, thread);
  152. }
  153. return thread;
  154. }
  155. static struct starpu_omp_critical *create_omp_critical_struct(void)
  156. {
  157. struct starpu_omp_critical *critical = malloc(sizeof(*critical));
  158. memset(critical, 0, sizeof(*critical));
  159. _starpu_spin_init(&critical->lock);
  160. return critical;
  161. }
  162. static void destroy_omp_critical_struct(struct starpu_omp_critical *critical)
  163. {
  164. STARPU_ASSERT(critical->state == 0);
  165. STARPU_ASSERT(critical->contention_list_head == NULL);
  166. _starpu_spin_destroy(&critical->lock);
  167. critical->name = NULL;
  168. free(critical);
  169. }
  170. static struct starpu_omp_device *create_omp_device_struct(void)
  171. {
  172. struct starpu_omp_device *device = malloc(sizeof(*device));
  173. if (device == NULL)
  174. _STARPU_ERROR("memory allocation failed");
  175. memset(device, 0, sizeof(*device));
  176. /* TODO: initialize device->icvs with proper values */
  177. memset(&device->icvs, 0, sizeof(device->icvs));
  178. return device;
  179. }
  180. static void destroy_omp_device_struct(struct starpu_omp_device *device)
  181. {
  182. memset(device, 0, sizeof(*device));
  183. free(device);
  184. }
  185. static struct starpu_omp_region *create_omp_region_struct(struct starpu_omp_region *parent_region, struct starpu_omp_device *owner_device)
  186. {
  187. struct starpu_omp_region *region = malloc(sizeof(*region));
  188. if (region == NULL)
  189. _STARPU_ERROR("memory allocation failed");
  190. memset(region, 0, sizeof(*region));
  191. region->parent_region = parent_region;
  192. region->owner_device = owner_device;
  193. region->thread_list = starpu_omp_thread_list_new();
  194. region->implicit_task_list = starpu_omp_task_list_new();
  195. _starpu_spin_init(&region->lock);
  196. region->level = (parent_region != NULL)?parent_region->level+1:0;
  197. return region;
  198. }
  199. static void destroy_omp_region_struct(struct starpu_omp_region *region)
  200. {
  201. STARPU_ASSERT(region->nb_threads == 0);
  202. STARPU_ASSERT(starpu_omp_thread_list_empty(region->thread_list));
  203. STARPU_ASSERT(starpu_omp_task_list_empty(region->implicit_task_list));
  204. STARPU_ASSERT(region->continuation_starpu_task == NULL);
  205. starpu_omp_thread_list_delete(region->thread_list);
  206. starpu_omp_task_list_delete(region->implicit_task_list);
  207. _starpu_spin_destroy(&region->lock);
  208. memset(region, 0, sizeof(*region));
  209. free(region);
  210. }
  211. static void omp_initial_thread_func(void)
  212. {
  213. struct starpu_omp_thread *initial_thread = _global_state.initial_thread;
  214. struct starpu_omp_task *initial_task = _global_state.initial_task;
  215. while (1)
  216. {
  217. struct starpu_task *continuation_starpu_task = initial_task->nested_region->continuation_starpu_task;
  218. starpu_driver_run_once(&initial_thread->starpu_driver);
  219. /*
  220. * if we are leaving the first nested region we give control back to initial task
  221. * otherwise, we should continue to execute work
  222. */
  223. if (_starpu_task_test_termination(continuation_starpu_task))
  224. {
  225. initial_task->nested_region->continuation_starpu_task = NULL;
  226. STARPU_PTHREAD_SETSPECIFIC(omp_task_key, initial_task);
  227. swapcontext(&initial_thread->ctx, &initial_task->ctx);
  228. }
  229. }
  230. }
  231. static struct starpu_omp_thread *create_omp_thread_struct(struct starpu_omp_region *owner_region)
  232. {
  233. struct starpu_omp_thread *thread = starpu_omp_thread_new();
  234. if (thread == NULL)
  235. _STARPU_ERROR("memory allocation failed");
  236. memset(thread, 0, sizeof(*thread));
  237. thread->owner_region = owner_region;
  238. return thread;
  239. }
  240. static void destroy_omp_thread_struct(struct starpu_omp_thread *thread)
  241. {
  242. STARPU_ASSERT(thread->current_task == NULL);
  243. memset(thread, 0, sizeof(*thread));
  244. starpu_omp_thread_delete(thread);
  245. }
  246. static void starpu_omp_explicit_task_entry(struct starpu_omp_task *task)
  247. {
  248. STARPU_ASSERT(!task->is_implicit);
  249. task->f(task->starpu_buffers, task->starpu_cl_arg);
  250. task->state = starpu_omp_task_state_terminated;
  251. struct starpu_omp_thread *thread = STARPU_PTHREAD_GETSPECIFIC(omp_thread_key);
  252. /*
  253. * the task reached the terminated state, definitively give hand back to the worker code.
  254. *
  255. * about to run on the worker stack...
  256. */
  257. setcontext(&thread->ctx);
  258. STARPU_ASSERT(0); /* unreachable code */
  259. }
  260. static void starpu_omp_implicit_task_entry(struct starpu_omp_task *task)
  261. {
  262. STARPU_ASSERT(task->is_implicit);
  263. task->f(task->starpu_buffers, task->starpu_cl_arg);
  264. starpu_omp_barrier();
  265. task->state = starpu_omp_task_state_terminated;
  266. struct starpu_omp_thread *thread = STARPU_PTHREAD_GETSPECIFIC(omp_thread_key);
  267. /*
  268. * the task reached the terminated state, definitively give hand back to the worker code.
  269. *
  270. * about to run on the worker stack...
  271. */
  272. setcontext(&thread->ctx);
  273. STARPU_ASSERT(0); /* unreachable code */
  274. }
  275. /*
  276. * stop executing a task that is about to block
  277. * and give hand back to the thread
  278. */
  279. static void starpu_omp_task_preempt(void)
  280. {
  281. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  282. struct starpu_omp_thread *thread = STARPU_PTHREAD_GETSPECIFIC(omp_thread_key);
  283. task->state = starpu_omp_task_state_preempted;
  284. /*
  285. * the task reached a blocked state, give hand back to the worker code.
  286. *
  287. * about to run on the worker stack...
  288. */
  289. swapcontext(&task->ctx, &thread->ctx);
  290. /* now running on the task stack again */
  291. }
  292. /*
  293. * wrap a task function to allow the task to be preempted
  294. */
  295. static void starpu_omp_implicit_task_exec(void *buffers[], void *cl_arg)
  296. {
  297. struct starpu_omp_task *task = starpu_task_get_current()->omp_task;
  298. STARPU_ASSERT(task->is_implicit);
  299. STARPU_PTHREAD_SETSPECIFIC(omp_task_key, task);
  300. struct starpu_omp_thread *thread = get_local_thread();
  301. if (task->state != starpu_omp_task_state_preempted)
  302. {
  303. task->starpu_buffers = buffers;
  304. task->starpu_cl_arg = cl_arg;
  305. STARPU_ASSERT (task->stack == NULL);
  306. /* TODO: use ICV stack size info instead */
  307. task->stack = malloc(_STARPU_STACKSIZE);
  308. if (task->stack == NULL)
  309. _STARPU_ERROR("memory allocation failed");
  310. getcontext(&task->ctx);
  311. /*
  312. * we do not use uc_link, starpu_omp_task_entry will handle
  313. * the end of the task
  314. */
  315. task->ctx.uc_link = NULL;
  316. task->ctx.uc_stack.ss_sp = task->stack;
  317. task->ctx.uc_stack.ss_size = _STARPU_STACKSIZE;
  318. makecontext(&task->ctx, (void (*) ()) starpu_omp_implicit_task_entry, 1, task);
  319. }
  320. task->state = starpu_omp_task_state_clear;
  321. /*
  322. * start the task execution, or restore a previously preempted task.
  323. * about to run on the task stack...
  324. * */
  325. swapcontext(&thread->ctx, &task->ctx);
  326. /* now running on the worker stack again */
  327. STARPU_ASSERT(task->state == starpu_omp_task_state_preempted
  328. || task->state == starpu_omp_task_state_terminated);
  329. STARPU_PTHREAD_SETSPECIFIC(omp_task_key, NULL);
  330. /* TODO: analyse the cause of the return and take appropriate steps */
  331. if (task->state == starpu_omp_task_state_terminated)
  332. {
  333. task->starpu_task->omp_task = NULL;
  334. task->starpu_task = NULL;
  335. free(task->stack);
  336. task->stack = NULL;
  337. memset(&task->ctx, 0, sizeof(task->ctx));
  338. }
  339. else if (task->state != starpu_omp_task_state_preempted)
  340. _STARPU_ERROR("invalid omp task state");
  341. }
  342. /*
  343. * wrap a task function to allow the task to be preempted
  344. */
  345. static void starpu_omp_explicit_task_exec(void *buffers[], void *cl_arg)
  346. {
  347. struct starpu_omp_task *task = starpu_task_get_current()->omp_task;
  348. STARPU_ASSERT(!task->is_implicit);
  349. STARPU_PTHREAD_SETSPECIFIC(omp_task_key, task);
  350. struct starpu_omp_thread *thread = get_local_thread();
  351. if (task->state != starpu_omp_task_state_preempted)
  352. {
  353. if (!task->is_untied)
  354. {
  355. struct _starpu_worker *starpu_worker = _starpu_get_local_worker_key();
  356. task->starpu_task->workerid = starpu_worker->workerid;
  357. task->starpu_task->execute_on_a_specific_worker = 1;
  358. }
  359. task->starpu_buffers = buffers;
  360. task->starpu_cl_arg = cl_arg;
  361. STARPU_ASSERT (task->stack == NULL);
  362. /* TODO: use ICV stack size info instead */
  363. task->stack = malloc(_STARPU_STACKSIZE);
  364. if (task->stack == NULL)
  365. _STARPU_ERROR("memory allocation failed");
  366. getcontext(&task->ctx);
  367. /*
  368. * we do not use uc_link, starpu_omp_task_entry will handle
  369. * the end of the task
  370. */
  371. task->ctx.uc_link = NULL;
  372. task->ctx.uc_stack.ss_sp = task->stack;
  373. task->ctx.uc_stack.ss_size = _STARPU_STACKSIZE;
  374. makecontext(&task->ctx, (void (*) ()) starpu_omp_explicit_task_entry, 1, task);
  375. }
  376. task->state = starpu_omp_task_state_clear;
  377. /*
  378. * start the task execution, or restore a previously preempted task.
  379. * about to run on the task stack...
  380. * */
  381. swapcontext(&thread->ctx, &task->ctx);
  382. /* now running on the worker stack again */
  383. STARPU_ASSERT(task->state == starpu_omp_task_state_preempted
  384. || task->state == starpu_omp_task_state_terminated);
  385. STARPU_PTHREAD_SETSPECIFIC(omp_task_key, NULL);
  386. /* TODO: analyse the cause of the return and take appropriate steps */
  387. if (task->state == starpu_omp_task_state_terminated)
  388. {
  389. struct starpu_omp_task *parent_task = task->parent_task;
  390. struct starpu_omp_region *parallel_region = task->owner_region;
  391. free(task->stack);
  392. task->stack = NULL;
  393. memset(&task->ctx, 0, sizeof(task->ctx));
  394. _starpu_spin_lock(&parent_task->lock);
  395. if (STARPU_ATOMIC_ADD(&parent_task->child_task_count, -1) == 0)
  396. {
  397. if (parent_task->state == starpu_omp_task_state_zombie)
  398. {
  399. STARPU_ASSERT(!parent_task->is_implicit);
  400. destroy_omp_task_struct(parent_task);
  401. }
  402. else if (parent_task->wait_on & starpu_omp_task_wait_on_task_childs)
  403. {
  404. parent_task->wait_on &= ~starpu_omp_task_wait_on_task_childs;
  405. _wake_up_locked_task(parent_task);
  406. }
  407. }
  408. _starpu_spin_unlock(&parent_task->lock);
  409. _starpu_spin_lock(&parallel_region->lock);
  410. if (STARPU_ATOMIC_ADD(&parallel_region->bound_explicit_task_count, -1) == 0)
  411. {
  412. struct starpu_omp_task *waiting_task = parallel_region->waiting_task;
  413. _starpu_spin_unlock(&parallel_region->lock);
  414. if (waiting_task)
  415. {
  416. _starpu_spin_lock(&waiting_task->lock);
  417. _starpu_spin_lock(&parallel_region->lock);
  418. parallel_region->waiting_task = NULL;
  419. STARPU_ASSERT(waiting_task->wait_on & starpu_omp_task_wait_on_region_tasks);
  420. waiting_task->wait_on &= ~starpu_omp_task_wait_on_region_tasks;
  421. _wake_up_locked_task(waiting_task);
  422. _starpu_spin_unlock(&parallel_region->lock);
  423. _starpu_spin_unlock(&waiting_task->lock);
  424. }
  425. }
  426. else
  427. {
  428. _starpu_spin_unlock(&parallel_region->lock);
  429. }
  430. if (task->task_group)
  431. {
  432. struct starpu_omp_task *leader_task = task->task_group->leader_task;
  433. STARPU_ASSERT(leader_task != task);
  434. _starpu_spin_lock(&leader_task->lock);
  435. if (STARPU_ATOMIC_ADD(&task->task_group->descendent_task_count, -1) == 0)
  436. {
  437. if (leader_task->wait_on & starpu_omp_task_wait_on_group)
  438. {
  439. leader_task->wait_on &= ~starpu_omp_task_wait_on_group;
  440. _wake_up_locked_task(leader_task);
  441. }
  442. }
  443. _starpu_spin_unlock(&leader_task->lock);
  444. }
  445. }
  446. else if (task->state != starpu_omp_task_state_preempted)
  447. _STARPU_ERROR("invalid omp task state");
  448. }
  449. static struct starpu_omp_task *create_omp_task_struct(struct starpu_omp_task *parent_task,
  450. struct starpu_omp_thread *owner_thread, struct starpu_omp_region *owner_region, int is_implicit)
  451. {
  452. struct starpu_omp_task *task = starpu_omp_task_new();
  453. if (task == NULL)
  454. _STARPU_ERROR("memory allocation failed");
  455. memset(task, 0, sizeof(*task));
  456. task->parent_task = parent_task;
  457. task->owner_thread = owner_thread;
  458. task->owner_region = owner_region;
  459. task->is_implicit = is_implicit;
  460. _starpu_spin_init(&task->lock);
  461. /* TODO: initialize task->data_env_icvs with proper values */
  462. memset(&task->data_env_icvs, 0, sizeof(task->data_env_icvs));
  463. if (is_implicit)
  464. {
  465. /* TODO: initialize task->implicit_task_icvs with proper values */
  466. memset(&task->implicit_task_icvs, 0, sizeof(task->implicit_task_icvs));
  467. }
  468. return task;
  469. }
  470. static void destroy_omp_task_struct(struct starpu_omp_task *task)
  471. {
  472. STARPU_ASSERT(task->state == starpu_omp_task_state_terminated);
  473. STARPU_ASSERT(task->nested_region == NULL);
  474. STARPU_ASSERT(task->starpu_task == NULL);
  475. STARPU_ASSERT(task->stack == NULL);
  476. _starpu_spin_destroy(&task->lock);
  477. memset(task, 0, sizeof(*task));
  478. starpu_omp_task_delete(task);
  479. }
  480. /*
  481. * setup the main application thread to handle the possible preemption of the initial task
  482. */
  483. static void omp_initial_thread_setup(void)
  484. {
  485. struct starpu_omp_thread *initial_thread = _global_state.initial_thread;
  486. struct starpu_omp_task *initial_task = _global_state.initial_task;
  487. /* .current_task */
  488. initial_thread->current_task = initial_task;
  489. /* .owner_region already set in create_omp_thread_struct */
  490. /* .initial_thread_stack */
  491. initial_thread->initial_thread_stack = malloc(_STARPU_STACKSIZE);
  492. if (initial_thread->initial_thread_stack == NULL)
  493. _STARPU_ERROR("memory allocation failed");
  494. /* .ctx */
  495. getcontext(&initial_thread->ctx);
  496. /*
  497. * we do not use uc_link, the initial thread always should give hand back to the initial task
  498. */
  499. initial_thread->ctx.uc_link = NULL;
  500. initial_thread->ctx.uc_stack.ss_sp = initial_thread->initial_thread_stack;
  501. initial_thread->ctx.uc_stack.ss_size = _STARPU_STACKSIZE;
  502. makecontext(&initial_thread->ctx, omp_initial_thread_func, 0);
  503. /* .starpu_driver */
  504. /*
  505. * we configure starpu to not launch CPU worker 0
  506. * because we will use the main thread to play the role of worker 0
  507. */
  508. struct starpu_conf conf;
  509. int ret = starpu_conf_init(&conf);
  510. STARPU_CHECK_RETURN_VALUE(ret, "starpu_conf_init");
  511. initial_thread->starpu_driver.type = STARPU_CPU_WORKER;
  512. initial_thread->starpu_driver.id.cpu_id = 0;
  513. conf.not_launched_drivers = &initial_thread->starpu_driver;
  514. conf.n_not_launched_drivers = 1;
  515. /* we are now ready to start StarPU */
  516. ret = starpu_init(&conf);
  517. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  518. ret = starpu_driver_init(&initial_thread->starpu_driver);
  519. STARPU_CHECK_RETURN_VALUE(ret, "starpu_driver_init");
  520. STARPU_PTHREAD_SETSPECIFIC(omp_task_key, initial_task);
  521. initial_thread->worker = _starpu_get_worker_struct(0);
  522. STARPU_ASSERT(initial_thread->worker);
  523. STARPU_PTHREAD_SETSPECIFIC(omp_thread_key, initial_thread);
  524. register_thread_worker(initial_thread);
  525. }
  526. static void omp_initial_thread_exit()
  527. {
  528. struct starpu_omp_thread *initial_thread = _global_state.initial_thread;
  529. int ret = starpu_driver_deinit(&initial_thread->starpu_driver);
  530. STARPU_CHECK_RETURN_VALUE(ret, "starpu_driver_deinit");
  531. memset(&initial_thread->starpu_driver, 0, sizeof (initial_thread->starpu_driver));
  532. /* the driver for the main thread is now de-inited, we can shutdown Starpu */
  533. starpu_shutdown();
  534. free(initial_thread->initial_thread_stack);
  535. initial_thread->initial_thread_stack = NULL;
  536. memset(&initial_thread->ctx, 0, sizeof (initial_thread->ctx));
  537. initial_thread->current_task = NULL;
  538. }
  539. static void omp_initial_region_setup(void)
  540. {
  541. _global_state.initial_region->master_thread = _global_state.initial_thread;
  542. _global_state.initial_region->nb_threads++;
  543. starpu_omp_task_list_push_back(_global_state.initial_region->implicit_task_list,
  544. _global_state.initial_task);
  545. omp_initial_thread_setup();
  546. }
  547. static void omp_initial_region_exit(void)
  548. {
  549. omp_initial_thread_exit();
  550. _global_state.initial_task->state = starpu_omp_task_state_terminated;
  551. starpu_omp_task_list_pop_front(_global_state.initial_region->implicit_task_list);
  552. _global_state.initial_region->master_thread = NULL;
  553. _global_state.initial_region->nb_threads--;
  554. }
  555. /*
  556. * Entry point to be called by the OpenMP runtime constructor
  557. */
  558. int starpu_omp_init(void)
  559. {
  560. STARPU_PTHREAD_KEY_CREATE(&omp_thread_key, NULL);
  561. STARPU_PTHREAD_KEY_CREATE(&omp_task_key, NULL);
  562. _starpu_omp_environment_init();
  563. _global_state.icvs.cancel_var = _starpu_omp_initial_icv_values->cancel_var;
  564. _global_state.initial_device = create_omp_device_struct();
  565. _global_state.initial_region = create_omp_region_struct(NULL, _global_state.initial_device);
  566. _global_state.initial_thread = create_omp_thread_struct(_global_state.initial_region);
  567. _global_state.initial_task = create_omp_task_struct(NULL,
  568. _global_state.initial_thread, _global_state.initial_region, 1);
  569. _global_state.default_critical = create_omp_critical_struct();
  570. _global_state.named_criticals = NULL;
  571. _starpu_spin_init(&_global_state.named_criticals_lock);
  572. _global_state.hash_workers = NULL;
  573. _starpu_spin_init(&_global_state.hash_workers_lock);
  574. _starpu_omp_global_state = &_global_state;
  575. omp_initial_region_setup();
  576. /* init clock reference for starpu_omp_get_wtick */
  577. _starpu_omp_clock_ref = starpu_timing_now();
  578. return 0;
  579. }
  580. void starpu_omp_shutdown(void)
  581. {
  582. omp_initial_region_exit();
  583. /* TODO: free ICV variables */
  584. /* TODO: free task/thread/region/device structures */
  585. destroy_omp_task_struct(_global_state.initial_task);
  586. _global_state.initial_task = NULL;
  587. _global_state.initial_thread = NULL;
  588. destroy_omp_region_struct(_global_state.initial_region);
  589. _global_state.initial_region = NULL;
  590. destroy_omp_device_struct(_global_state.initial_device);
  591. _global_state.initial_device = NULL;
  592. destroy_omp_critical_struct(_global_state.default_critical);
  593. _global_state.default_critical = NULL;
  594. _starpu_spin_lock(&_global_state.named_criticals_lock);
  595. {
  596. struct starpu_omp_critical *critical, *tmp;
  597. HASH_ITER(hh, _global_state.named_criticals, critical, tmp)
  598. {
  599. STARPU_ASSERT(critical != NULL);
  600. HASH_DEL(_global_state.named_criticals, critical);
  601. destroy_omp_critical_struct(critical);
  602. }
  603. }
  604. STARPU_ASSERT(_global_state.named_criticals == NULL);
  605. _starpu_spin_unlock(&_global_state.named_criticals_lock);
  606. _starpu_spin_destroy(&_global_state.named_criticals_lock);
  607. _starpu_spin_lock(&_global_state.hash_workers_lock);
  608. {
  609. struct starpu_omp_thread *thread, *tmp;
  610. HASH_ITER(hh, _global_state.hash_workers, thread, tmp)
  611. {
  612. STARPU_ASSERT(thread != NULL);
  613. HASH_DEL(_global_state.hash_workers, thread);
  614. destroy_omp_thread_struct(thread);
  615. }
  616. }
  617. STARPU_ASSERT(_global_state.hash_workers == NULL);
  618. _starpu_spin_unlock(&_global_state.hash_workers_lock);
  619. _starpu_spin_destroy(&_global_state.hash_workers_lock);
  620. STARPU_PTHREAD_KEY_DELETE(omp_task_key);
  621. STARPU_PTHREAD_KEY_DELETE(omp_thread_key);
  622. }
  623. void starpu_omp_parallel_region(const struct starpu_codelet * const _parallel_region_cl, starpu_data_handle_t *handles,
  624. void * const cl_arg, size_t cl_arg_size, unsigned cl_arg_free, int if_clause)
  625. {
  626. struct starpu_omp_thread *master_thread = STARPU_PTHREAD_GETSPECIFIC(omp_thread_key);
  627. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  628. struct starpu_omp_region *region = task->owner_region;
  629. int ret;
  630. /* TODO: compute the proper nb_threads and launch additional workers as needed.
  631. * for now, the level 1 parallel region spans all the threads
  632. * and level >= 2 parallel regions have only one thread */
  633. int nb_threads = (if_clause != 0 && region->level == 0)?starpu_cpu_worker_get_count():1;
  634. struct starpu_omp_region *new_region =
  635. create_omp_region_struct(region, _global_state.initial_device);
  636. int i;
  637. for (i = 0; i < nb_threads; i++)
  638. {
  639. struct starpu_omp_thread *new_thread;
  640. if (i == 0)
  641. {
  642. new_thread = master_thread;
  643. new_region->master_thread = master_thread;
  644. }
  645. else
  646. {
  647. /* TODO: specify actual starpu worker */
  648. /* TODO: use a less arbitrary thread/worker mapping scheme */
  649. if (region->level == 0)
  650. {
  651. struct _starpu_worker *worker = _starpu_get_worker_struct(i);
  652. new_thread = get_worker_thread(worker);
  653. if (new_thread == NULL)
  654. {
  655. new_thread = create_omp_thread_struct(new_region);
  656. new_thread->worker = _starpu_get_worker_struct(i);
  657. register_thread_worker(new_thread);
  658. }
  659. }
  660. else
  661. {
  662. new_thread = master_thread;
  663. }
  664. starpu_omp_thread_list_push_back(new_region->thread_list, new_thread);
  665. }
  666. struct starpu_omp_task *new_task = create_omp_task_struct(task, new_thread, new_region, 1);
  667. new_task->rank = new_region->nb_threads;
  668. new_region->nb_threads++;
  669. starpu_omp_task_list_push_back(new_region->implicit_task_list, new_task);
  670. }
  671. STARPU_ASSERT(new_region->nb_threads == nb_threads);
  672. /*
  673. * if task == initial_task, create a starpu task as a continuation to all the implicit
  674. * tasks of the new region, else prepare the task for preemption,
  675. * to become itself a continuation to the implicit tasks of the new region
  676. */
  677. if (task == _global_state.initial_task)
  678. {
  679. new_region->continuation_starpu_task = starpu_task_create();
  680. /* in that case, the continuation starpu task is only used for synchronisation */
  681. new_region->continuation_starpu_task->cl = NULL;
  682. new_region->continuation_starpu_task->workerid = master_thread->worker->workerid;
  683. new_region->continuation_starpu_task->execute_on_a_specific_worker = 1;
  684. /* this sync task will be tested for completion in omp_initial_thread_func() */
  685. new_region->continuation_starpu_task->detach = 0;
  686. }
  687. else
  688. {
  689. /* through the preemption, the parent starpu task becomes the continuation task */
  690. _starpu_task_prepare_for_continuation();
  691. new_region->continuation_starpu_task = task->starpu_task;
  692. }
  693. task->nested_region = new_region;
  694. /*
  695. * create the starpu tasks for the implicit omp tasks,
  696. * create explicit dependencies between these starpu tasks and the continuation starpu task
  697. */
  698. struct starpu_omp_task * implicit_task;
  699. for (implicit_task = starpu_omp_task_list_begin(new_region->implicit_task_list);
  700. implicit_task != starpu_omp_task_list_end(new_region->implicit_task_list);
  701. implicit_task = starpu_omp_task_list_next(implicit_task))
  702. {
  703. implicit_task->cl = *_parallel_region_cl;
  704. /*
  705. * save pointer to the regions user function from the parallel region codelet
  706. *
  707. * TODO: add support for multiple/heterogeneous implementations
  708. */
  709. implicit_task->f = implicit_task->cl.cpu_funcs[0];
  710. /*
  711. * plug the task wrapper into the parallel region codelet instead, to support task preemption
  712. */
  713. implicit_task->cl.cpu_funcs[0] = starpu_omp_implicit_task_exec;
  714. implicit_task->starpu_task = starpu_task_create();
  715. implicit_task->starpu_task->cl = &implicit_task->cl;
  716. {
  717. unsigned i;
  718. for (i = 0; i < implicit_task->cl.nbuffers; i++)
  719. {
  720. implicit_task->starpu_task->handles[i] = handles[i];
  721. }
  722. }
  723. implicit_task->starpu_task->cl_arg = cl_arg;
  724. implicit_task->starpu_task->cl_arg_size = cl_arg_size;
  725. implicit_task->starpu_task->cl_arg_free = cl_arg_free;
  726. implicit_task->starpu_task->omp_task = implicit_task;
  727. implicit_task->starpu_task->workerid = implicit_task->owner_thread->worker->workerid;
  728. implicit_task->starpu_task->execute_on_a_specific_worker = 1;
  729. starpu_task_declare_deps_array(new_region->continuation_starpu_task, 1, &implicit_task->starpu_task);
  730. }
  731. /*
  732. * submit all the region implicit starpu tasks
  733. */
  734. for (implicit_task = starpu_omp_task_list_begin(new_region->implicit_task_list);
  735. implicit_task != starpu_omp_task_list_end(new_region->implicit_task_list);
  736. implicit_task = starpu_omp_task_list_next(implicit_task))
  737. {
  738. ret = starpu_task_submit(implicit_task->starpu_task);
  739. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  740. }
  741. /*
  742. * submit the region continuation starpu task if task == initial_task
  743. */
  744. if (task == _global_state.initial_task)
  745. {
  746. ret = _starpu_task_submit_internally(new_region->continuation_starpu_task);
  747. STARPU_CHECK_RETURN_VALUE(ret, "_starpu_task_submit_internally");
  748. }
  749. /*
  750. * preempt for completion of the region
  751. */
  752. starpu_omp_task_preempt();
  753. if (task == _global_state.initial_task)
  754. {
  755. STARPU_ASSERT(new_region->continuation_starpu_task == NULL);
  756. }
  757. else
  758. {
  759. STARPU_ASSERT(new_region->continuation_starpu_task != NULL);
  760. new_region->continuation_starpu_task = NULL;
  761. }
  762. /*
  763. * TODO: free region resources
  764. */
  765. for (i = 0; i < nb_threads; i++)
  766. {
  767. if (i == 0)
  768. {
  769. new_region->master_thread = NULL;
  770. }
  771. else
  772. {
  773. starpu_omp_thread_list_pop_front(new_region->thread_list);
  774. /* TODO: cleanup unused threads */
  775. }
  776. new_region->nb_threads--;
  777. struct starpu_omp_task *implicit_task = starpu_omp_task_list_pop_front(new_region->implicit_task_list);
  778. destroy_omp_task_struct(implicit_task);
  779. }
  780. STARPU_ASSERT(new_region->nb_threads == 0);
  781. task->nested_region = NULL;
  782. destroy_omp_region_struct(new_region);
  783. }
  784. static void _wake_up_locked_task(struct starpu_omp_task *task)
  785. {
  786. if (task->wait_on == 0)
  787. {
  788. int ret = starpu_task_submit(task->starpu_task);
  789. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  790. }
  791. }
  792. static void wake_up_barrier(struct starpu_omp_region *parallel_region)
  793. {
  794. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  795. struct starpu_omp_task *implicit_task;
  796. for (implicit_task = starpu_omp_task_list_begin(parallel_region->implicit_task_list);
  797. implicit_task != starpu_omp_task_list_end(parallel_region->implicit_task_list);
  798. implicit_task = starpu_omp_task_list_next(implicit_task))
  799. {
  800. if (implicit_task == task)
  801. continue;
  802. _starpu_spin_lock(&implicit_task->lock);
  803. STARPU_ASSERT(implicit_task->wait_on & starpu_omp_task_wait_on_barrier);
  804. implicit_task->wait_on &= ~starpu_omp_task_wait_on_barrier;
  805. _wake_up_locked_task(implicit_task);
  806. _starpu_spin_unlock(&implicit_task->lock);
  807. }
  808. }
  809. static void barrier__sleep_callback(void *_task)
  810. {
  811. struct starpu_omp_task *task = _task;
  812. _starpu_spin_unlock(&task->lock);
  813. }
  814. static void region_tasks__sleep_callback(void *_task)
  815. {
  816. struct starpu_omp_task *task = _task;
  817. struct starpu_omp_region *parallel_region = task->owner_region;
  818. _starpu_spin_unlock(&task->lock);
  819. _starpu_spin_unlock(&parallel_region->lock);
  820. }
  821. void starpu_omp_barrier(void)
  822. {
  823. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  824. /* Assume barriers are performed in by the implicit tasks of a parallel_region */
  825. STARPU_ASSERT(task->is_implicit);
  826. struct starpu_omp_region *parallel_region = task->owner_region;
  827. _starpu_spin_lock(&task->lock);
  828. int inc_barrier_count = STARPU_ATOMIC_ADD(&parallel_region->barrier_count, 1);
  829. if (inc_barrier_count == parallel_region->nb_threads)
  830. {
  831. /* last task reaching the barrier */
  832. _starpu_spin_lock(&parallel_region->lock);
  833. parallel_region->barrier_count = 0;
  834. if (parallel_region->bound_explicit_task_count > 0)
  835. {
  836. task->wait_on |= starpu_omp_task_wait_on_region_tasks;
  837. parallel_region->waiting_task = task;
  838. _starpu_task_prepare_for_continuation_ext(0, region_tasks__sleep_callback, task);
  839. starpu_omp_task_preempt();
  840. }
  841. else
  842. {
  843. _starpu_spin_unlock(&task->lock);
  844. _starpu_spin_unlock(&parallel_region->lock);
  845. }
  846. wake_up_barrier(parallel_region);
  847. }
  848. else
  849. {
  850. /* not the last task reaching the barrier
  851. * . prepare for conditional continuation
  852. * . sleep
  853. */
  854. task->wait_on |= starpu_omp_task_wait_on_barrier;
  855. _starpu_task_prepare_for_continuation_ext(0, barrier__sleep_callback, task);
  856. starpu_omp_task_preempt();
  857. STARPU_ASSERT(task->child_task_count == 0);
  858. }
  859. }
  860. void starpu_omp_master(void (*f)(void *arg), void *arg)
  861. {
  862. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  863. struct starpu_omp_thread *thread = STARPU_PTHREAD_GETSPECIFIC(omp_thread_key);
  864. /* Assume master is performed in by the implicit tasks of a region */
  865. STARPU_ASSERT(task->is_implicit);
  866. struct starpu_omp_region *region = task->owner_region;
  867. if (thread == region->master_thread)
  868. {
  869. f(arg);
  870. }
  871. }
  872. /* variant of omp_master for inlined code
  873. * return !0 for the task that should perform the master section
  874. * return 0 for the tasks that should not perform the master section */
  875. int starpu_omp_master_inline(void)
  876. {
  877. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  878. struct starpu_omp_thread *thread = STARPU_PTHREAD_GETSPECIFIC(omp_thread_key);
  879. /* Assume master is performed in by the implicit tasks of a region */
  880. STARPU_ASSERT(task->is_implicit);
  881. struct starpu_omp_region *region = task->owner_region;
  882. return thread == region->master_thread;
  883. }
  884. void starpu_omp_single(void (*f)(void *arg), void *arg, int nowait)
  885. {
  886. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  887. /* Assume singles are performed in by the implicit tasks of a region */
  888. STARPU_ASSERT(task->is_implicit);
  889. struct starpu_omp_region *region = task->owner_region;
  890. int first = STARPU_BOOL_COMPARE_AND_SWAP(&region->single_id, task->single_id, task->single_id+1);
  891. task->single_id++;
  892. if (first)
  893. {
  894. f(arg);
  895. }
  896. if (!nowait)
  897. {
  898. starpu_omp_barrier();
  899. }
  900. }
  901. /* variant of omp_single for inlined code
  902. * return !0 for the task that should perform the single section
  903. * return 0 for the tasks that should not perform the single section
  904. * wait/nowait should be handled directly by the calling code using starpu_omp_barrier */
  905. int starpu_omp_single_inline(void)
  906. {
  907. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  908. /* Assume singles are performed in by the implicit tasks of a region */
  909. STARPU_ASSERT(task->is_implicit);
  910. struct starpu_omp_region *region = task->owner_region;
  911. int first = STARPU_BOOL_COMPARE_AND_SWAP(&region->single_id, task->single_id, task->single_id+1);
  912. task->single_id++;
  913. return first;
  914. }
  915. static void critical__sleep_callback(void *_critical)
  916. {
  917. struct starpu_omp_critical *critical = _critical;
  918. _starpu_spin_unlock(&critical->lock);
  919. }
  920. void starpu_omp_critical(void (*f)(void *arg), void *arg, const char *name)
  921. {
  922. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  923. struct starpu_omp_critical *critical = NULL;
  924. struct starpu_omp_task_link link;
  925. if (name)
  926. {
  927. _starpu_spin_lock(&_global_state.named_criticals_lock);
  928. HASH_FIND_STR(_global_state.named_criticals, name, critical);
  929. if (critical == NULL)
  930. {
  931. critical = create_omp_critical_struct();
  932. critical->name = name;
  933. HASH_ADD_STR(_global_state.named_criticals, name, critical);
  934. }
  935. _starpu_spin_unlock(&_global_state.named_criticals_lock);
  936. }
  937. else
  938. {
  939. critical = _global_state.default_critical;
  940. }
  941. _starpu_spin_lock(&critical->lock);
  942. while (critical->state != 0)
  943. {
  944. _starpu_spin_lock(&task->lock);
  945. task->wait_on |= starpu_omp_task_wait_on_critical;
  946. _starpu_spin_unlock(&task->lock);
  947. link.task = task;
  948. link.next = critical->contention_list_head;
  949. critical->contention_list_head = &link;
  950. _starpu_task_prepare_for_continuation_ext(0, critical__sleep_callback, critical);
  951. starpu_omp_task_preempt();
  952. /* re-acquire the spin lock */
  953. _starpu_spin_lock(&critical->lock);
  954. }
  955. critical->state = 1;
  956. _starpu_spin_unlock(&critical->lock);
  957. f(arg);
  958. _starpu_spin_lock(&critical->lock);
  959. STARPU_ASSERT(critical->state == 1);
  960. critical->state = 0;
  961. if (critical->contention_list_head != NULL)
  962. {
  963. struct starpu_omp_task *next_task = critical->contention_list_head->task;
  964. critical->contention_list_head = critical->contention_list_head->next;
  965. _starpu_spin_lock(&next_task->lock);
  966. STARPU_ASSERT(next_task->wait_on & starpu_omp_task_wait_on_critical);
  967. next_task->wait_on &= ~starpu_omp_task_wait_on_critical;
  968. _wake_up_locked_task(next_task);
  969. _starpu_spin_unlock(&next_task->lock);
  970. }
  971. _starpu_spin_unlock(&critical->lock);
  972. }
  973. void starpu_omp_critical_inline_begin(const char *name)
  974. {
  975. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  976. struct starpu_omp_critical *critical = NULL;
  977. struct starpu_omp_task_link link;
  978. if (name)
  979. {
  980. _starpu_spin_lock(&_global_state.named_criticals_lock);
  981. HASH_FIND_STR(_global_state.named_criticals, name, critical);
  982. if (critical == NULL)
  983. {
  984. critical = create_omp_critical_struct();
  985. critical->name = name;
  986. HASH_ADD_STR(_global_state.named_criticals, name, critical);
  987. }
  988. _starpu_spin_unlock(&_global_state.named_criticals_lock);
  989. }
  990. else
  991. {
  992. critical = _global_state.default_critical;
  993. }
  994. _starpu_spin_lock(&critical->lock);
  995. while (critical->state != 0)
  996. {
  997. _starpu_spin_lock(&task->lock);
  998. task->wait_on |= starpu_omp_task_wait_on_critical;
  999. _starpu_spin_unlock(&task->lock);
  1000. link.task = task;
  1001. link.next = critical->contention_list_head;
  1002. critical->contention_list_head = &link;
  1003. _starpu_task_prepare_for_continuation_ext(0, critical__sleep_callback, critical);
  1004. starpu_omp_task_preempt();
  1005. /* re-acquire the spin lock */
  1006. _starpu_spin_lock(&critical->lock);
  1007. }
  1008. critical->state = 1;
  1009. _starpu_spin_unlock(&critical->lock);
  1010. }
  1011. void starpu_omp_critical_inline_end(const char *name)
  1012. {
  1013. struct starpu_omp_critical *critical = NULL;
  1014. if (name)
  1015. {
  1016. _starpu_spin_lock(&_global_state.named_criticals_lock);
  1017. HASH_FIND_STR(_global_state.named_criticals, name, critical);
  1018. STARPU_ASSERT(critical != NULL);
  1019. _starpu_spin_unlock(&_global_state.named_criticals_lock);
  1020. }
  1021. else
  1022. {
  1023. critical = _global_state.default_critical;
  1024. }
  1025. _starpu_spin_lock(&critical->lock);
  1026. STARPU_ASSERT(critical->state == 1);
  1027. critical->state = 0;
  1028. if (critical->contention_list_head != NULL)
  1029. {
  1030. struct starpu_omp_task *next_task = critical->contention_list_head->task;
  1031. critical->contention_list_head = critical->contention_list_head->next;
  1032. _starpu_spin_lock(&next_task->lock);
  1033. STARPU_ASSERT(next_task->wait_on & starpu_omp_task_wait_on_critical);
  1034. next_task->wait_on &= ~starpu_omp_task_wait_on_critical;
  1035. _wake_up_locked_task(next_task);
  1036. _starpu_spin_unlock(&next_task->lock);
  1037. }
  1038. _starpu_spin_unlock(&critical->lock);
  1039. }
  1040. static void explicit_task__destroy_callback(void *_task)
  1041. {
  1042. struct starpu_omp_task *task = _task;
  1043. STARPU_ASSERT(!task->is_implicit);
  1044. task->starpu_task->omp_task = NULL;
  1045. task->starpu_task = NULL;
  1046. _starpu_spin_lock(&task->lock);
  1047. if (task->child_task_count != 0)
  1048. {
  1049. task->state = starpu_omp_task_state_zombie;
  1050. _starpu_spin_unlock(&task->lock);
  1051. }
  1052. else
  1053. {
  1054. destroy_omp_task_struct(task);
  1055. }
  1056. }
  1057. void starpu_omp_task_region(const struct starpu_codelet * const _task_region_cl, starpu_data_handle_t *handles,
  1058. void * const cl_arg, size_t cl_arg_size, unsigned cl_arg_free,
  1059. int if_clause, int final_clause, int untied_clause, int mergeable_clause)
  1060. {
  1061. struct starpu_omp_task *generating_task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  1062. struct starpu_omp_region *parallel_region = generating_task->owner_region;
  1063. int is_undeferred = 0;
  1064. int is_final = 0;
  1065. int is_included = 0;
  1066. int is_merged = 0;
  1067. int is_untied = 0;
  1068. int ret;
  1069. if (!if_clause)
  1070. {
  1071. is_undeferred = 1;
  1072. }
  1073. if (generating_task->is_final)
  1074. {
  1075. is_final = 1;
  1076. is_included = 1;
  1077. }
  1078. else if (final_clause)
  1079. {
  1080. is_final = 1;
  1081. }
  1082. if (is_included)
  1083. {
  1084. is_undeferred = 1;
  1085. }
  1086. if ((is_undeferred || is_included) & mergeable_clause)
  1087. {
  1088. is_merged = 1;
  1089. }
  1090. if (is_merged)
  1091. {
  1092. /* note: no need to backup/restore ICVs for merged tasks, merged tasks use the data environment of the caller */
  1093. unsigned i;
  1094. for (i = 0; i < _task_region_cl->nbuffers; i++)
  1095. {
  1096. ret = starpu_data_acquire(handles[i], _task_region_cl->modes[i]);
  1097. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire");
  1098. }
  1099. void (*f)(void **starpu_buffers, void *starpu_cl_arg) = _task_region_cl->cpu_funcs[0];
  1100. f((void**)handles, cl_arg);
  1101. for (i = 0; i < _task_region_cl->nbuffers; i++)
  1102. {
  1103. starpu_data_release(handles[i]);
  1104. }
  1105. if (cl_arg_free)
  1106. {
  1107. free(cl_arg);
  1108. }
  1109. }
  1110. else if (is_included)
  1111. {
  1112. /* TODO: backup current ICVs and setup new ICVs for the included task */
  1113. unsigned i;
  1114. for (i = 0; i < _task_region_cl->nbuffers; i++)
  1115. {
  1116. ret = starpu_data_acquire(handles[i], _task_region_cl->modes[i]);
  1117. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire");
  1118. }
  1119. void (*f)(void **starpu_buffers, void *starpu_cl_arg) = _task_region_cl->cpu_funcs[0];
  1120. f((void**)handles, cl_arg);
  1121. for (i = 0; i < _task_region_cl->nbuffers; i++)
  1122. {
  1123. starpu_data_release(handles[i]);
  1124. }
  1125. if (cl_arg_free)
  1126. {
  1127. free(cl_arg);
  1128. }
  1129. /* TODO: restore backuped ICVs */
  1130. }
  1131. else
  1132. {
  1133. struct starpu_omp_task *generated_task =
  1134. create_omp_task_struct(generating_task, NULL, parallel_region, 0);
  1135. generated_task->cl = *_task_region_cl;
  1136. if (untied_clause)
  1137. {
  1138. is_untied = 1;
  1139. }
  1140. generated_task->is_undeferred = is_undeferred;
  1141. generated_task->is_final = is_final;
  1142. generated_task->is_untied = is_untied;
  1143. generated_task->task_group = generating_task->task_group;
  1144. generated_task->rank = -1;
  1145. /*
  1146. * save pointer to the regions user function from the task region codelet
  1147. *
  1148. * TODO: add support for multiple/heterogeneous implementations
  1149. */
  1150. generated_task->f = generated_task->cl.cpu_funcs[0];
  1151. /*
  1152. * plug the task wrapper into the task region codelet instead, to support task preemption
  1153. */
  1154. generated_task->cl.cpu_funcs[0] = starpu_omp_explicit_task_exec;
  1155. generated_task->starpu_task = starpu_task_create();
  1156. generated_task->starpu_task->cl = &generated_task->cl;
  1157. generated_task->starpu_task->cl_arg = cl_arg;
  1158. generated_task->starpu_task->cl_arg_size = cl_arg_size;
  1159. generated_task->starpu_task->cl_arg_free = cl_arg_free;
  1160. {
  1161. unsigned i;
  1162. for (i = 0; i < generated_task->cl.nbuffers; i++)
  1163. {
  1164. generated_task->starpu_task->handles[i] = handles[i];
  1165. }
  1166. }
  1167. generated_task->starpu_task->omp_task = generated_task;
  1168. _starpu_task_set_omp_cleanup_callback(generated_task->starpu_task, explicit_task__destroy_callback, generated_task);
  1169. /* if the task is tied, execute_on_a_specific_worker will be changed to 1
  1170. * upon the first preemption of the generated task, once we know
  1171. * which worker thread has been selected */
  1172. generated_task->starpu_task->execute_on_a_specific_worker = 0;
  1173. (void)STARPU_ATOMIC_ADD(&generating_task->child_task_count, 1);
  1174. (void)STARPU_ATOMIC_ADD(&parallel_region->bound_explicit_task_count, 1);
  1175. if (generated_task->task_group)
  1176. {
  1177. (void)STARPU_ATOMIC_ADD(&generated_task->task_group->descendent_task_count, 1);
  1178. }
  1179. if (is_undeferred)
  1180. {
  1181. _starpu_task_prepare_for_continuation();
  1182. starpu_task_declare_deps_array(generating_task->starpu_task, 1,
  1183. &generated_task->starpu_task);
  1184. }
  1185. ret = starpu_task_submit(generated_task->starpu_task);
  1186. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  1187. if (is_undeferred)
  1188. {
  1189. starpu_omp_task_preempt();
  1190. }
  1191. }
  1192. }
  1193. static void task_childs__sleep_callback(void *_task)
  1194. {
  1195. struct starpu_omp_task *task = _task;
  1196. _starpu_spin_unlock(&task->lock);
  1197. }
  1198. void starpu_omp_taskwait(void)
  1199. {
  1200. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  1201. _starpu_spin_lock(&task->lock);
  1202. if (task->child_task_count > 0)
  1203. {
  1204. task->wait_on |= starpu_omp_task_wait_on_task_childs;
  1205. _starpu_task_prepare_for_continuation_ext(0, task_childs__sleep_callback, task);
  1206. starpu_omp_task_preempt();
  1207. STARPU_ASSERT(task->child_task_count == 0);
  1208. }
  1209. else
  1210. {
  1211. _starpu_spin_unlock(&task->lock);
  1212. }
  1213. }
  1214. static void group__sleep_callback(void *_task)
  1215. {
  1216. struct starpu_omp_task *task = _task;
  1217. _starpu_spin_unlock(&task->lock);
  1218. }
  1219. void starpu_omp_taskgroup(void (*f)(void *arg), void *arg)
  1220. {
  1221. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  1222. struct starpu_omp_task_group *p_previous_task_group;
  1223. struct starpu_omp_task_group task_group;
  1224. p_previous_task_group = task->task_group;
  1225. task_group.descendent_task_count = 0;
  1226. task_group.leader_task = task;
  1227. task->task_group = &task_group;
  1228. f(arg);
  1229. _starpu_spin_lock(&task->lock);
  1230. if (task_group.descendent_task_count > 0)
  1231. {
  1232. task->wait_on |= starpu_omp_task_wait_on_group;
  1233. _starpu_task_prepare_for_continuation_ext(0, group__sleep_callback, task);
  1234. starpu_omp_task_preempt();
  1235. STARPU_ASSERT(task_group.descendent_task_count == 0);
  1236. }
  1237. else
  1238. {
  1239. _starpu_spin_unlock(&task->lock);
  1240. }
  1241. task->task_group = p_previous_task_group;
  1242. }
  1243. static inline void _starpu_omp_for_loop(struct starpu_omp_region *parallel_region, struct starpu_omp_task *task,
  1244. struct starpu_omp_loop *loop, int first_call,
  1245. unsigned long long nb_iterations, unsigned long long chunk, int schedule, unsigned long long *_first_i, unsigned long long *_nb_i)
  1246. {
  1247. *_nb_i = 0;
  1248. if (schedule == starpu_omp_schedule_static || schedule == starpu_omp_schedule_auto)
  1249. {
  1250. if (chunk > 0)
  1251. {
  1252. if (first_call)
  1253. {
  1254. *_first_i = task->rank * chunk;
  1255. }
  1256. else
  1257. {
  1258. *_first_i += parallel_region->nb_threads * chunk;
  1259. }
  1260. if (*_first_i < nb_iterations)
  1261. {
  1262. if (*_first_i + chunk > nb_iterations)
  1263. {
  1264. *_nb_i = nb_iterations - *_first_i;
  1265. }
  1266. else
  1267. {
  1268. *_nb_i = chunk;
  1269. }
  1270. }
  1271. }
  1272. else
  1273. {
  1274. if (first_call)
  1275. {
  1276. *_nb_i = nb_iterations / parallel_region->nb_threads;
  1277. *_first_i = (unsigned)task->rank * (*_nb_i);
  1278. unsigned long long remainder = nb_iterations % parallel_region->nb_threads;
  1279. if (remainder > 0)
  1280. {
  1281. if ((unsigned)task->rank < remainder)
  1282. {
  1283. (*_nb_i)++;
  1284. *_first_i += (unsigned)task->rank;
  1285. }
  1286. else
  1287. {
  1288. *_first_i += remainder;
  1289. }
  1290. }
  1291. }
  1292. }
  1293. }
  1294. else if (schedule == starpu_omp_schedule_dynamic)
  1295. {
  1296. if (chunk == 0)
  1297. {
  1298. chunk = 1;
  1299. }
  1300. if (first_call)
  1301. {
  1302. *_first_i = 0;
  1303. }
  1304. _starpu_spin_lock(&parallel_region->lock);
  1305. if (loop->next_iteration < nb_iterations)
  1306. {
  1307. *_first_i = loop->next_iteration;
  1308. if (*_first_i + chunk > nb_iterations)
  1309. {
  1310. *_nb_i = nb_iterations - *_first_i;
  1311. }
  1312. else
  1313. {
  1314. *_nb_i = chunk;
  1315. }
  1316. loop->next_iteration += *_nb_i;
  1317. }
  1318. _starpu_spin_unlock(&parallel_region->lock);
  1319. }
  1320. else if (schedule == starpu_omp_schedule_guided)
  1321. {
  1322. if (chunk == 0)
  1323. {
  1324. chunk = 1;
  1325. }
  1326. if (first_call)
  1327. {
  1328. *_first_i = 0;
  1329. }
  1330. _starpu_spin_lock(&parallel_region->lock);
  1331. if (loop->next_iteration < nb_iterations)
  1332. {
  1333. *_first_i = loop->next_iteration;
  1334. *_nb_i = (nb_iterations - *_first_i)/parallel_region->nb_threads;
  1335. if (*_nb_i < chunk)
  1336. {
  1337. if (*_first_i+chunk > nb_iterations)
  1338. {
  1339. *_nb_i = nb_iterations - *_first_i;
  1340. }
  1341. else
  1342. {
  1343. *_nb_i = chunk;
  1344. }
  1345. }
  1346. loop->next_iteration += *_nb_i;
  1347. }
  1348. _starpu_spin_unlock(&parallel_region->lock);
  1349. }
  1350. }
  1351. static inline struct starpu_omp_loop *_starpu_omp_for_get_loop(struct starpu_omp_region *parallel_region, struct starpu_omp_task *task)
  1352. {
  1353. struct starpu_omp_loop *loop;
  1354. loop = parallel_region->loop_list;
  1355. while (loop && loop->id != task->loop_id)
  1356. {
  1357. loop = loop->next_loop;
  1358. }
  1359. return loop;
  1360. }
  1361. static inline struct starpu_omp_loop *_starpu_omp_for_loop_begin(struct starpu_omp_region *parallel_region, struct starpu_omp_task *task,
  1362. int ordered)
  1363. {
  1364. struct starpu_omp_loop *loop;
  1365. _starpu_spin_lock(&parallel_region->lock);
  1366. loop = _starpu_omp_for_get_loop(parallel_region, task);
  1367. if (!loop)
  1368. {
  1369. loop = malloc(sizeof(*loop));
  1370. if (loop == NULL)
  1371. _STARPU_ERROR("memory allocation failed\n");
  1372. loop->id = task->loop_id;
  1373. loop->next_iteration = 0;
  1374. loop->nb_completed_threads = 0;
  1375. loop->next_loop = parallel_region->loop_list;
  1376. parallel_region->loop_list = loop;
  1377. if (ordered)
  1378. {
  1379. loop->ordered_iteration = 0;
  1380. _starpu_spin_init(&loop->ordered_lock);
  1381. condition_init(&loop->ordered_cond);
  1382. }
  1383. }
  1384. _starpu_spin_unlock(&parallel_region->lock);
  1385. return loop;
  1386. }
  1387. static inline void _starpu_omp_for_loop_end(struct starpu_omp_region *parallel_region, struct starpu_omp_task *task,
  1388. struct starpu_omp_loop *loop, int ordered)
  1389. {
  1390. _starpu_spin_lock(&parallel_region->lock);
  1391. loop->nb_completed_threads++;
  1392. if (loop->nb_completed_threads == parallel_region->nb_threads)
  1393. {
  1394. struct starpu_omp_loop **p_loop;
  1395. if (ordered)
  1396. {
  1397. loop->ordered_iteration = 0;
  1398. condition_exit(&loop->ordered_cond);
  1399. _starpu_spin_destroy(&loop->ordered_lock);
  1400. }
  1401. STARPU_ASSERT(loop->next_loop == NULL);
  1402. p_loop = &(parallel_region->loop_list);
  1403. while (*p_loop != loop)
  1404. {
  1405. p_loop = &((*p_loop)->next_loop);
  1406. }
  1407. *p_loop = NULL;
  1408. free(loop);
  1409. }
  1410. _starpu_spin_unlock(&parallel_region->lock);
  1411. task->loop_id++;
  1412. }
  1413. 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)
  1414. {
  1415. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  1416. struct starpu_omp_region *parallel_region = task->owner_region;
  1417. struct starpu_omp_loop *loop = _starpu_omp_for_loop_begin(parallel_region, task, ordered);
  1418. _starpu_omp_for_loop(parallel_region, task, loop, 1, nb_iterations, chunk, schedule, _first_i, _nb_i);
  1419. if (*_nb_i == 0)
  1420. {
  1421. _starpu_omp_for_loop_end(parallel_region, task, loop, ordered);
  1422. }
  1423. return (*_nb_i != 0);
  1424. }
  1425. 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)
  1426. {
  1427. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  1428. struct starpu_omp_region *parallel_region = task->owner_region;
  1429. struct starpu_omp_loop *loop = _starpu_omp_for_loop_begin(parallel_region, task, ordered);
  1430. _starpu_omp_for_loop(parallel_region, task, loop, 0, nb_iterations, chunk, schedule, _first_i, _nb_i);
  1431. if (*_nb_i == 0)
  1432. {
  1433. _starpu_omp_for_loop_end(parallel_region, task, loop, ordered);
  1434. }
  1435. return (*_nb_i != 0);
  1436. }
  1437. 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)
  1438. {
  1439. unsigned long long nb_i;
  1440. int end = starpu_omp_for_inline_first(nb_iterations, chunk, schedule, ordered, _begin_i, &nb_i);
  1441. *_end_i = *_begin_i + nb_i;
  1442. return end;
  1443. }
  1444. 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)
  1445. {
  1446. unsigned long long nb_i;
  1447. int end = starpu_omp_for_inline_next(nb_iterations, chunk, schedule, ordered, _begin_i, &nb_i);
  1448. *_end_i = *_begin_i + nb_i;
  1449. return end;
  1450. }
  1451. 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)
  1452. {
  1453. unsigned long long _first_i = 0;
  1454. unsigned long long _nb_i = 0;
  1455. if (starpu_omp_for_inline_first(nb_iterations, chunk, schedule, ordered, &_first_i, &_nb_i))
  1456. {
  1457. do
  1458. {
  1459. f(_first_i, _nb_i, arg);
  1460. }
  1461. while (starpu_omp_for_inline_next(nb_iterations, chunk, schedule, ordered, &_first_i, &_nb_i));
  1462. }
  1463. if (!nowait)
  1464. {
  1465. starpu_omp_barrier();
  1466. }
  1467. }
  1468. 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)
  1469. {
  1470. unsigned long long _begin_i = 0;
  1471. unsigned long long _end_i = 0;
  1472. if (starpu_omp_for_inline_first_alt(nb_iterations, chunk, schedule, ordered, &_begin_i, &_end_i))
  1473. {
  1474. do
  1475. {
  1476. f(_begin_i, _end_i, arg);
  1477. }
  1478. while (starpu_omp_for_inline_next_alt(nb_iterations, chunk, schedule, ordered, &_begin_i, &_end_i));
  1479. }
  1480. if (!nowait)
  1481. {
  1482. starpu_omp_barrier();
  1483. }
  1484. }
  1485. void starpu_omp_ordered(void (*f)(unsigned long long _i, void *arg), void *arg, unsigned long long i)
  1486. {
  1487. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  1488. struct starpu_omp_region *parallel_region = task->owner_region;
  1489. struct starpu_omp_loop *loop = _starpu_omp_for_get_loop(parallel_region, task);
  1490. _starpu_spin_lock(&loop->ordered_lock);
  1491. while (i != loop->ordered_iteration)
  1492. {
  1493. STARPU_ASSERT(i > loop->ordered_iteration);
  1494. condition_wait(&loop->ordered_cond, &loop->ordered_lock);
  1495. }
  1496. f(i, arg);
  1497. loop->ordered_iteration++;
  1498. condition_broadcast(&loop->ordered_cond);
  1499. _starpu_spin_unlock(&loop->ordered_lock);
  1500. }
  1501. void starpu_omp_ordered_inline_begin(unsigned long long i)
  1502. {
  1503. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  1504. struct starpu_omp_region *parallel_region = task->owner_region;
  1505. struct starpu_omp_loop *loop = _starpu_omp_for_get_loop(parallel_region, task);
  1506. _starpu_spin_lock(&loop->ordered_lock);
  1507. while (i != loop->ordered_iteration)
  1508. {
  1509. STARPU_ASSERT(i > loop->ordered_iteration);
  1510. condition_wait(&loop->ordered_cond, &loop->ordered_lock);
  1511. }
  1512. }
  1513. void starpu_omp_ordered_inline_end(void)
  1514. {
  1515. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  1516. struct starpu_omp_region *parallel_region = task->owner_region;
  1517. struct starpu_omp_loop *loop = _starpu_omp_for_get_loop(parallel_region, task);
  1518. loop->ordered_iteration++;
  1519. condition_broadcast(&loop->ordered_cond);
  1520. _starpu_spin_unlock(&loop->ordered_lock);
  1521. }
  1522. static inline struct starpu_omp_sections *_starpu_omp_get_sections(struct starpu_omp_region *parallel_region, struct starpu_omp_task *task)
  1523. {
  1524. struct starpu_omp_sections *sections;
  1525. sections = parallel_region->sections_list;
  1526. while (sections && sections->id != task->sections_id)
  1527. {
  1528. sections = sections->next_sections;
  1529. }
  1530. return sections;
  1531. }
  1532. static inline struct starpu_omp_sections *_starpu_omp_sections_begin(struct starpu_omp_region *parallel_region, struct starpu_omp_task *task)
  1533. {
  1534. struct starpu_omp_sections *sections;
  1535. _starpu_spin_lock(&parallel_region->lock);
  1536. sections = _starpu_omp_get_sections(parallel_region, task);
  1537. if (!sections)
  1538. {
  1539. sections = malloc(sizeof(*sections));
  1540. if (sections == NULL)
  1541. _STARPU_ERROR("memory allocation failed\n");
  1542. sections->id = task->sections_id;
  1543. sections->next_section_num = 0;
  1544. sections->nb_completed_threads = 0;
  1545. sections->next_sections = parallel_region->sections_list;
  1546. parallel_region->sections_list = sections;
  1547. }
  1548. _starpu_spin_unlock(&parallel_region->lock);
  1549. return sections;
  1550. }
  1551. static inline void _starpu_omp_sections_end(struct starpu_omp_region *parallel_region, struct starpu_omp_task *task,
  1552. struct starpu_omp_sections *sections)
  1553. {
  1554. _starpu_spin_lock(&parallel_region->lock);
  1555. sections->nb_completed_threads++;
  1556. if (sections->nb_completed_threads == parallel_region->nb_threads)
  1557. {
  1558. struct starpu_omp_sections **p_sections;
  1559. STARPU_ASSERT(sections->next_sections == NULL);
  1560. p_sections = &(parallel_region->sections_list);
  1561. while (*p_sections != sections)
  1562. {
  1563. p_sections = &((*p_sections)->next_sections);
  1564. }
  1565. *p_sections = NULL;
  1566. free(sections);
  1567. }
  1568. _starpu_spin_unlock(&parallel_region->lock);
  1569. task->sections_id++;
  1570. }
  1571. void starpu_omp_sections(int nb_sections, void (**section_f)(void *arg), void **section_arg, int nowait)
  1572. {
  1573. struct starpu_omp_task *task = STARPU_PTHREAD_GETSPECIFIC(omp_task_key);
  1574. struct starpu_omp_region *parallel_region = task->owner_region;
  1575. struct starpu_omp_sections *sections = _starpu_omp_sections_begin(parallel_region, task);
  1576. for (;;)
  1577. {
  1578. void (*f)(void *arg) = NULL;
  1579. void *arg = NULL;
  1580. _starpu_spin_lock(&parallel_region->lock);
  1581. if (sections->next_section_num < nb_sections)
  1582. {
  1583. f = section_f[sections->next_section_num];
  1584. arg = section_arg[sections->next_section_num];
  1585. sections->next_section_num ++;
  1586. }
  1587. _starpu_spin_unlock(&parallel_region->lock);
  1588. if (f == NULL)
  1589. break;
  1590. f(arg);
  1591. }
  1592. _starpu_omp_sections_end(parallel_region, task, sections);
  1593. if (!nowait)
  1594. {
  1595. starpu_omp_barrier();
  1596. }
  1597. }
  1598. /*
  1599. * restore deprecated diagnostics (-Wdeprecated-declarations)
  1600. */
  1601. #pragma GCC diagnostic pop
  1602. #endif /* STARPU_OPENMP */