openmp_runtime_support.c 78 KB

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