openmp_runtime_support.c 75 KB

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