openmp_runtime_support.c 76 KB

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