openmp_runtime_support.c 75 KB

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