openmp_runtime_support.c 77 KB

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