openmp_runtime_support.c 78 KB

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