openmp_runtime_support.c 64 KB

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