task.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2016 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015, 2016 CNRS
  5. * Copyright (C) 2011 Télécom-SudParis
  6. * Copyright (C) 2011, 2014, 2016 INRIA
  7. *
  8. * StarPU is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation; either version 2.1 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * StarPU is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  18. */
  19. #include <starpu.h>
  20. #include <starpu_profiling.h>
  21. #include <core/workers.h>
  22. #include <core/sched_ctx.h>
  23. #include <core/jobs.h>
  24. #include <core/task.h>
  25. #include <core/task_bundle.h>
  26. #include <core/dependencies/data_concurrency.h>
  27. #include <common/config.h>
  28. #include <common/utils.h>
  29. #include <common/fxt.h>
  30. #include <profiling/profiling.h>
  31. #include <profiling/bound.h>
  32. #include <math.h>
  33. #include <string.h>
  34. #include <core/debug.h>
  35. #include <core/sched_ctx.h>
  36. #include <time.h>
  37. #include <signal.h>
  38. #include <core/simgrid.h>
  39. #ifdef STARPU_HAVE_WINDOWS
  40. #include <windows.h>
  41. #endif
  42. /* XXX this should be reinitialized when StarPU is shutdown (or we should make
  43. * sure that no task remains !) */
  44. /* TODO we could make this hierarchical to avoid contention ? */
  45. //static starpu_pthread_cond_t submitted_cond = STARPU_PTHREAD_COND_INITIALIZER;
  46. /* This key stores the task currently handled by the thread, note that we
  47. * cannot use the worker structure to store that information because it is
  48. * possible that we have a task with a NULL codelet, which means its callback
  49. * could be executed by a user thread as well. */
  50. static starpu_pthread_key_t current_task_key;
  51. static int limit_min_submitted_tasks;
  52. static int limit_max_submitted_tasks;
  53. static int watchdog_crash;
  54. /* Called once at starpu_init */
  55. void _starpu_task_init(void)
  56. {
  57. STARPU_PTHREAD_KEY_CREATE(&current_task_key, NULL);
  58. limit_min_submitted_tasks = starpu_get_env_number("STARPU_LIMIT_MIN_SUBMITTED_TASKS");
  59. limit_max_submitted_tasks = starpu_get_env_number("STARPU_LIMIT_MAX_SUBMITTED_TASKS");
  60. watchdog_crash = starpu_get_env_number("STARPU_WATCHDOG_CRASH");
  61. }
  62. void _starpu_task_deinit(void)
  63. {
  64. STARPU_PTHREAD_KEY_DELETE(current_task_key);
  65. }
  66. void starpu_task_init(struct starpu_task *task)
  67. {
  68. /* TODO: memcpy from a template instead? benchmark it */
  69. STARPU_ASSERT(task);
  70. /* As most of the fields must be initialised at NULL, let's put 0
  71. * everywhere */
  72. memset(task, 0, sizeof(struct starpu_task));
  73. task->sequential_consistency = 1;
  74. /* Now we can initialise fields which recquire custom value */
  75. #if STARPU_DEFAULT_PRIO != 0
  76. task->priority = STARPU_DEFAULT_PRIO;
  77. #endif
  78. task->detach = 1;
  79. #if STARPU_TASK_INVALID != 0
  80. task->status = STARPU_TASK_INVALID;
  81. #endif
  82. task->predicted = NAN;
  83. task->predicted_transfer = NAN;
  84. task->magic = 42;
  85. task->sched_ctx = STARPU_NMAX_SCHED_CTXS;
  86. task->flops = 0.0;
  87. }
  88. /* Free all the ressources allocated for a task, without deallocating the task
  89. * structure itself (this is required for statically allocated tasks).
  90. * All values previously set by the user, like codelet and handles, remain
  91. * unchanged */
  92. void starpu_task_clean(struct starpu_task *task)
  93. {
  94. STARPU_ASSERT(task);
  95. /* If a buffer was allocated to store the profiling info, we free it. */
  96. if (task->profiling_info)
  97. {
  98. free(task->profiling_info);
  99. task->profiling_info = NULL;
  100. }
  101. /* If case the task is (still) part of a bundle */
  102. starpu_task_bundle_t bundle = task->bundle;
  103. if (bundle)
  104. starpu_task_bundle_remove(bundle, task);
  105. if (task->dyn_handles)
  106. {
  107. free(task->dyn_handles);
  108. task->dyn_handles = NULL;
  109. free(task->dyn_interfaces);
  110. task->dyn_interfaces = NULL;
  111. }
  112. if (task->dyn_modes)
  113. {
  114. free(task->dyn_modes);
  115. task->dyn_modes = NULL;
  116. }
  117. struct _starpu_job *j = (struct _starpu_job *)task->starpu_private;
  118. if (j)
  119. {
  120. _starpu_job_destroy(j);
  121. task->starpu_private = NULL;
  122. }
  123. }
  124. struct starpu_task * STARPU_ATTRIBUTE_MALLOC starpu_task_create(void)
  125. {
  126. struct starpu_task *task;
  127. _STARPU_MALLOC(task, sizeof(struct starpu_task));
  128. starpu_task_init(task);
  129. /* Dynamically allocated tasks are destroyed by default */
  130. task->destroy = 1;
  131. return task;
  132. }
  133. /* Free the ressource allocated during starpu_task_create. This function can be
  134. * called automatically after the execution of a task by setting the "destroy"
  135. * flag of the starpu_task structure (default behaviour). Calling this function
  136. * on a statically allocated task results in an undefined behaviour. */
  137. void _starpu_task_destroy(struct starpu_task *task)
  138. {
  139. /* If starpu_task_destroy is called in a callback, we just set the destroy
  140. flag. The task will be destroyed after the callback returns */
  141. if (task == starpu_task_get_current()
  142. && _starpu_get_local_worker_status() == STATUS_CALLBACK)
  143. {
  144. task->destroy = 1;
  145. }
  146. else
  147. {
  148. starpu_task_clean(task);
  149. /* TODO handle the case of task with detach = 1 and destroy = 1 */
  150. /* TODO handle the case of non terminated tasks -> return -EINVAL */
  151. /* Does user want StarPU release cl_arg ? */
  152. if (task->cl_arg_free)
  153. free(task->cl_arg);
  154. /* Does user want StarPU release callback_arg ? */
  155. if (task->callback_arg_free)
  156. free(task->callback_arg);
  157. /* Does user want StarPU release prologue_callback_arg ? */
  158. if (task->prologue_callback_arg_free)
  159. free(task->prologue_callback_arg);
  160. /* Does user want StarPU release prologue_pop_arg ? */
  161. if (task->prologue_callback_pop_arg_free)
  162. free(task->prologue_callback_pop_arg);
  163. free(task);
  164. }
  165. }
  166. void starpu_task_destroy(struct starpu_task *task)
  167. {
  168. STARPU_ASSERT(task);
  169. STARPU_ASSERT_MSG(!task->destroy || !task->detach, "starpu_task_destroy must not be called for task with destroy = 1 and detach = 1");
  170. _starpu_task_destroy(task);
  171. }
  172. int starpu_task_finished(struct starpu_task *task)
  173. {
  174. STARPU_ASSERT(task);
  175. STARPU_ASSERT_MSG(!task->detach, "starpu_task_finished can only be called on tasks with detach = 0");
  176. return _starpu_job_finished(_starpu_get_job_associated_to_task(task));
  177. }
  178. int starpu_task_wait(struct starpu_task *task)
  179. {
  180. _STARPU_LOG_IN();
  181. STARPU_ASSERT(task);
  182. STARPU_ASSERT_MSG(!task->detach, "starpu_task_wait can only be called on tasks with detach = 0");
  183. if (task->detach || task->synchronous)
  184. {
  185. _STARPU_DEBUG("Task is detached or synchronous. Waiting returns immediately\n");
  186. _STARPU_LOG_OUT_TAG("einval");
  187. return -EINVAL;
  188. }
  189. STARPU_ASSERT_MSG(_starpu_worker_may_perform_blocking_calls(), "starpu_task_wait must not be called from a task or callback");
  190. struct _starpu_job *j = (struct _starpu_job *)task->starpu_private;
  191. _STARPU_TRACE_TASK_WAIT_START(j);
  192. _starpu_wait_job(j);
  193. /* as this is a synchronous task, the liberation of the job
  194. structure was deferred */
  195. if (task->destroy)
  196. _starpu_task_destroy(task);
  197. _STARPU_TRACE_TASK_WAIT_END();
  198. _STARPU_LOG_OUT();
  199. return 0;
  200. }
  201. int starpu_task_wait_array(struct starpu_task **tasks, unsigned nb_tasks)
  202. {
  203. unsigned i;
  204. for (i = 0; i < nb_tasks; i++)
  205. {
  206. int ret = starpu_task_wait(tasks[i]);
  207. if (ret)
  208. return ret;
  209. }
  210. return 0;
  211. }
  212. #ifdef STARPU_OPENMP
  213. int _starpu_task_test_termination(struct starpu_task *task)
  214. {
  215. STARPU_ASSERT(task);
  216. STARPU_ASSERT_MSG(!task->detach, "starpu_task_wait can only be called on tasks with detach = 0");
  217. if (task->detach || task->synchronous)
  218. {
  219. _STARPU_DEBUG("Task is detached or synchronous\n");
  220. _STARPU_LOG_OUT_TAG("einval");
  221. return -EINVAL;
  222. }
  223. struct _starpu_job *j = (struct _starpu_job *)task->starpu_private;
  224. int ret = _starpu_test_job_termination(j);
  225. if (ret)
  226. {
  227. if (task->destroy)
  228. _starpu_task_destroy(task);
  229. }
  230. return ret;
  231. }
  232. #endif
  233. /* NB in case we have a regenerable task, it is possible that the job was
  234. * already counted. */
  235. int _starpu_submit_job(struct _starpu_job *j)
  236. {
  237. struct starpu_task *task = j->task;
  238. int ret;
  239. #ifdef STARPU_OPENMP
  240. const unsigned continuation = j->continuation;
  241. #else
  242. const unsigned continuation = 0;
  243. #endif
  244. _STARPU_LOG_IN();
  245. /* notify bound computation of a new task */
  246. _starpu_bound_record(j);
  247. _starpu_increment_nsubmitted_tasks_of_sched_ctx(j->task->sched_ctx);
  248. _starpu_sched_task_submit(task);
  249. #ifdef STARPU_USE_SC_HYPERVISOR
  250. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(j->task->sched_ctx);
  251. if(sched_ctx != NULL && j->task->sched_ctx != _starpu_get_initial_sched_ctx()->id && j->task->sched_ctx != STARPU_NMAX_SCHED_CTXS
  252. && sched_ctx->perf_counters != NULL)
  253. {
  254. struct starpu_perfmodel_arch arch;
  255. _STARPU_MALLOC(arch.devices, sizeof(struct starpu_perfmodel_device));
  256. arch.ndevices = 1;
  257. arch.devices[0].type = STARPU_CPU_WORKER;
  258. arch.devices[0].devid = 0;
  259. arch.devices[0].ncores = 1;
  260. _starpu_compute_buffers_footprint(j->task->cl->model, &arch, 0, j);
  261. free(arch.devices);
  262. size_t data_size = 0;
  263. if (j->task->cl)
  264. {
  265. unsigned i, nbuffers = STARPU_TASK_GET_NBUFFERS(j->task);
  266. for(i = 0; i < nbuffers; i++)
  267. {
  268. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(task, i);
  269. if (handle != NULL)
  270. data_size += _starpu_data_get_size(handle);
  271. }
  272. }
  273. _STARPU_TRACE_HYPERVISOR_BEGIN();
  274. sched_ctx->perf_counters->notify_submitted_job(j->task, j->footprint, data_size);
  275. _STARPU_TRACE_HYPERVISOR_END();
  276. }
  277. #endif//STARPU_USE_SC_HYPERVISOR
  278. /* We retain handle reference count */
  279. if (task->cl && !continuation)
  280. {
  281. unsigned i;
  282. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(task);
  283. for (i=0; i<nbuffers; i++)
  284. {
  285. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(task, i);
  286. _starpu_spin_lock(&handle->header_lock);
  287. handle->busy_count++;
  288. _starpu_spin_unlock(&handle->header_lock);
  289. }
  290. }
  291. STARPU_PTHREAD_MUTEX_LOCK(&j->sync_mutex);
  292. _starpu_handle_job_submission(j);
  293. #ifdef STARPU_OPENMP
  294. if (continuation)
  295. {
  296. j->discontinuous = 1;
  297. j->continuation = 0;
  298. }
  299. #endif
  300. #ifdef STARPU_OPENMP
  301. if (continuation)
  302. {
  303. ret = _starpu_reenforce_task_deps_and_schedule(j);
  304. }
  305. else
  306. #endif
  307. {
  308. ret = _starpu_enforce_deps_and_schedule(j);
  309. }
  310. _STARPU_LOG_OUT();
  311. return ret;
  312. }
  313. /* Note: this is racy, so valgrind would complain. But since we'll always put
  314. * the same values, this is not a problem. */
  315. void _starpu_codelet_check_deprecated_fields(struct starpu_codelet *cl)
  316. {
  317. if (!cl)
  318. return;
  319. int is_where_unset = cl->where == 0;
  320. unsigned i, some_impl;
  321. /* Check deprecated and unset fields (where, <device>_func,
  322. * <device>_funcs) */
  323. /* CPU */
  324. if (cl->cpu_func && cl->cpu_func != STARPU_MULTIPLE_CPU_IMPLEMENTATIONS && cl->cpu_funcs[0])
  325. {
  326. _STARPU_DISP("[warning] [struct starpu_codelet] both cpu_func and cpu_funcs are set. Ignoring cpu_func.\n");
  327. cl->cpu_func = STARPU_MULTIPLE_CPU_IMPLEMENTATIONS;
  328. }
  329. if (cl->cpu_func && cl->cpu_func != STARPU_MULTIPLE_CPU_IMPLEMENTATIONS)
  330. {
  331. cl->cpu_funcs[0] = cl->cpu_func;
  332. cl->cpu_func = STARPU_MULTIPLE_CPU_IMPLEMENTATIONS;
  333. }
  334. some_impl = 0;
  335. for (i = 0; i < STARPU_MAXIMPLEMENTATIONS; i++)
  336. if (cl->cpu_funcs[i])
  337. {
  338. some_impl = 1;
  339. break;
  340. }
  341. if (some_impl && cl->cpu_func == 0)
  342. {
  343. cl->cpu_func = STARPU_MULTIPLE_CPU_IMPLEMENTATIONS;
  344. }
  345. if (some_impl && is_where_unset)
  346. {
  347. cl->where |= STARPU_CPU;
  348. }
  349. /* CUDA */
  350. if (cl->cuda_func && cl->cuda_func != STARPU_MULTIPLE_CUDA_IMPLEMENTATIONS && cl->cuda_funcs[0])
  351. {
  352. _STARPU_DISP("[warning] [struct starpu_codelet] both cuda_func and cuda_funcs are set. Ignoring cuda_func.\n");
  353. cl->cuda_func = STARPU_MULTIPLE_CUDA_IMPLEMENTATIONS;
  354. }
  355. if (cl->cuda_func && cl->cuda_func != STARPU_MULTIPLE_CUDA_IMPLEMENTATIONS)
  356. {
  357. cl->cuda_funcs[0] = cl->cuda_func;
  358. cl->cuda_func = STARPU_MULTIPLE_CUDA_IMPLEMENTATIONS;
  359. }
  360. some_impl = 0;
  361. for (i = 0; i < STARPU_MAXIMPLEMENTATIONS; i++)
  362. if (cl->cuda_funcs[i])
  363. {
  364. some_impl = 1;
  365. break;
  366. }
  367. if (some_impl && cl->cuda_func == 0)
  368. {
  369. cl->cuda_func = STARPU_MULTIPLE_CUDA_IMPLEMENTATIONS;
  370. }
  371. if (some_impl && is_where_unset)
  372. {
  373. cl->where |= STARPU_CUDA;
  374. }
  375. /* OpenCL */
  376. if (cl->opencl_func && cl->opencl_func != STARPU_MULTIPLE_OPENCL_IMPLEMENTATIONS && cl->opencl_funcs[0])
  377. {
  378. _STARPU_DISP("[warning] [struct starpu_codelet] both opencl_func and opencl_funcs are set. Ignoring opencl_func.\n");
  379. cl->opencl_func = STARPU_MULTIPLE_OPENCL_IMPLEMENTATIONS;
  380. }
  381. if (cl->opencl_func && cl->opencl_func != STARPU_MULTIPLE_OPENCL_IMPLEMENTATIONS)
  382. {
  383. cl->opencl_funcs[0] = cl->opencl_func;
  384. cl->opencl_func = STARPU_MULTIPLE_OPENCL_IMPLEMENTATIONS;
  385. }
  386. some_impl = 0;
  387. for (i = 0; i < STARPU_MAXIMPLEMENTATIONS; i++)
  388. if (cl->opencl_funcs[i])
  389. {
  390. some_impl = 1;
  391. break;
  392. }
  393. if (some_impl && cl->opencl_func == 0)
  394. {
  395. cl->opencl_func = STARPU_MULTIPLE_OPENCL_IMPLEMENTATIONS;
  396. }
  397. if (some_impl && is_where_unset)
  398. {
  399. cl->where |= STARPU_OPENCL;
  400. }
  401. some_impl = 0;
  402. for (i = 0; i < STARPU_MAXIMPLEMENTATIONS; i++)
  403. if (cl->mic_funcs[i])
  404. {
  405. some_impl = 1;
  406. break;
  407. }
  408. if (some_impl && is_where_unset)
  409. {
  410. cl->where |= STARPU_MIC;
  411. }
  412. some_impl = 0;
  413. for (i = 0; i < STARPU_MAXIMPLEMENTATIONS; i++)
  414. if (cl->scc_funcs[i])
  415. {
  416. some_impl = 1;
  417. break;
  418. }
  419. if (some_impl && is_where_unset)
  420. {
  421. cl->where |= STARPU_SCC;
  422. }
  423. some_impl = 0;
  424. for (i = 0; i < STARPU_MAXIMPLEMENTATIONS; i++)
  425. if (cl->cpu_funcs_name[i])
  426. {
  427. some_impl = 1;
  428. break;
  429. }
  430. if (some_impl && is_where_unset)
  431. {
  432. cl->where |= STARPU_MIC|STARPU_SCC;
  433. }
  434. }
  435. void _starpu_task_check_deprecated_fields(struct starpu_task *task STARPU_ATTRIBUTE_UNUSED)
  436. {
  437. /* None any more */
  438. }
  439. static int _starpu_task_submit_head(struct starpu_task *task)
  440. {
  441. unsigned is_sync = task->synchronous;
  442. struct _starpu_job *j = _starpu_get_job_associated_to_task(task);
  443. if (j->internal)
  444. {
  445. // Internal tasks are submitted to initial context
  446. task->sched_ctx = _starpu_get_initial_sched_ctx()->id;
  447. }
  448. else if (task->sched_ctx == STARPU_NMAX_SCHED_CTXS)
  449. {
  450. // If the task has not specified a context, we set the current context
  451. task->sched_ctx = _starpu_sched_ctx_get_current_context();
  452. }
  453. if (is_sync)
  454. {
  455. /* Perhaps it is not possible to submit a synchronous
  456. * (blocking) task */
  457. STARPU_ASSERT_MSG(_starpu_worker_may_perform_blocking_calls(), "submitting a synchronous task must not be done from a task or a callback");
  458. task->detach = 0;
  459. }
  460. _starpu_task_check_deprecated_fields(task);
  461. _starpu_codelet_check_deprecated_fields(task->cl);
  462. if (task->cl)
  463. {
  464. unsigned i;
  465. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(task);
  466. _STARPU_TRACE_UPDATE_TASK_CNT(0);
  467. /* Check buffers */
  468. if (task->dyn_handles == NULL)
  469. STARPU_ASSERT_MSG(STARPU_TASK_GET_NBUFFERS(task) <= STARPU_NMAXBUFS, "Codelet %p has too many buffers (%d vs max %d). Either use --enable-maxbuffers configure option to increase the max, or use dyn_handles instead of handles.", task->cl, STARPU_TASK_GET_NBUFFERS(task), STARPU_NMAXBUFS);
  470. if (task->dyn_handles)
  471. {
  472. _STARPU_MALLOC(task->dyn_interfaces, nbuffers * sizeof(void *));
  473. }
  474. for (i = 0; i < nbuffers; i++)
  475. {
  476. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(task, i);
  477. /* Make sure handles are not partitioned */
  478. STARPU_ASSERT_MSG(handle->nchildren == 0, "only unpartitioned data (or the pieces of a partitioned data) can be used in a task");
  479. /* Provide the home interface for now if any,
  480. * for can_execute hooks */
  481. if (handle->home_node != -1)
  482. _STARPU_TASK_SET_INTERFACE(task, starpu_data_get_interface_on_node(handle, handle->home_node), i);
  483. }
  484. /* Check the type of worker(s) required by the task exist */
  485. if (!_starpu_worker_exists(task))
  486. {
  487. _STARPU_LOG_OUT_TAG("ENODEV");
  488. return -ENODEV;
  489. }
  490. /* In case we require that a task should be explicitely
  491. * executed on a specific worker, we make sure that the worker
  492. * is able to execute this task. */
  493. if (task->execute_on_a_specific_worker && !starpu_combined_worker_can_execute_task(task->workerid, task, 0))
  494. {
  495. _STARPU_LOG_OUT_TAG("ENODEV");
  496. return -ENODEV;
  497. }
  498. if (task->cl->model)
  499. _starpu_init_and_load_perfmodel(task->cl->model);
  500. if (task->cl->energy_model)
  501. _starpu_init_and_load_perfmodel(task->cl->energy_model);
  502. }
  503. return 0;
  504. }
  505. /* application should submit new tasks to StarPU through this function */
  506. int starpu_task_submit(struct starpu_task *task)
  507. {
  508. _STARPU_LOG_IN();
  509. STARPU_ASSERT(task);
  510. STARPU_ASSERT_MSG(task->magic == 42, "Tasks must be created with starpu_task_create, or initialized with starpu_task_init.");
  511. int ret;
  512. unsigned is_sync = task->synchronous;
  513. starpu_task_bundle_t bundle = task->bundle;
  514. /* internally, StarPU manipulates a struct _starpu_job * which is a wrapper around a
  515. * task structure, it is possible that this job structure was already
  516. * allocated. */
  517. struct _starpu_job *j = _starpu_get_job_associated_to_task(task);
  518. const unsigned continuation =
  519. #ifdef STARPU_OPENMP
  520. j->continuation
  521. #else
  522. 0
  523. #endif
  524. ;
  525. _STARPU_TRACE_TASK_SUBMIT_START();
  526. if (!j->internal)
  527. {
  528. int nsubmitted_tasks = starpu_task_nsubmitted();
  529. if (limit_max_submitted_tasks >= 0 && limit_max_submitted_tasks < nsubmitted_tasks
  530. && limit_min_submitted_tasks >= 0 && limit_min_submitted_tasks < nsubmitted_tasks)
  531. {
  532. starpu_do_schedule();
  533. starpu_task_wait_for_n_submitted(limit_min_submitted_tasks);
  534. }
  535. }
  536. ret = _starpu_task_submit_head(task);
  537. if (ret)
  538. {
  539. _STARPU_TRACE_TASK_SUBMIT_END();
  540. return ret;
  541. }
  542. if (!j->internal && !continuation)
  543. _STARPU_TRACE_TASK_SUBMIT(j);
  544. /* If this is a continuation, we don't modify the implicit data dependencies detected earlier. */
  545. if (task->cl && !continuation)
  546. _starpu_detect_implicit_data_deps(task);
  547. if (bundle)
  548. {
  549. /* We need to make sure that models for other tasks of the
  550. * bundle are also loaded, so the scheduler can estimate the
  551. * duration of the whole bundle */
  552. STARPU_PTHREAD_MUTEX_LOCK(&bundle->mutex);
  553. struct _starpu_task_bundle_entry *entry;
  554. entry = bundle->list;
  555. while (entry)
  556. {
  557. if (entry->task->cl->model)
  558. _starpu_init_and_load_perfmodel(entry->task->cl->model);
  559. if (entry->task->cl->energy_model)
  560. _starpu_init_and_load_perfmodel(entry->task->cl->energy_model);
  561. entry = entry->next;
  562. }
  563. STARPU_PTHREAD_MUTEX_UNLOCK(&bundle->mutex);
  564. }
  565. /* If profiling is activated, we allocate a structure to store the
  566. * appropriate info. */
  567. struct starpu_profiling_task_info *info;
  568. int profiling = starpu_profiling_status_get();
  569. info = _starpu_allocate_profiling_info_if_needed(task);
  570. task->profiling_info = info;
  571. /* The task is considered as block until we are sure there remains not
  572. * dependency. */
  573. task->status = STARPU_TASK_BLOCKED;
  574. if (profiling)
  575. _starpu_clock_gettime(&info->submit_time);
  576. ret = _starpu_submit_job(j);
  577. #ifdef STARPU_SIMGRID
  578. if (_starpu_simgrid_task_submit_cost())
  579. MSG_process_sleep(0.000001);
  580. #endif
  581. if (is_sync)
  582. {
  583. _starpu_sched_do_schedule(task->sched_ctx);
  584. _starpu_wait_job(j);
  585. if (task->destroy)
  586. _starpu_task_destroy(task);
  587. }
  588. _STARPU_TRACE_TASK_SUBMIT_END();
  589. _STARPU_LOG_OUT();
  590. return ret;
  591. }
  592. int _starpu_task_submit_internally(struct starpu_task *task)
  593. {
  594. struct _starpu_job *j = _starpu_get_job_associated_to_task(task);
  595. j->internal = 1;
  596. return starpu_task_submit(task);
  597. }
  598. /* application should submit new tasks to StarPU through this function */
  599. int starpu_task_submit_to_ctx(struct starpu_task *task, unsigned sched_ctx_id)
  600. {
  601. task->sched_ctx = sched_ctx_id;
  602. return starpu_task_submit(task);
  603. }
  604. /* The StarPU core can submit tasks directly to the scheduler or a worker,
  605. * skipping dependencies completely (when it knows what it is doing). */
  606. int _starpu_task_submit_nodeps(struct starpu_task *task)
  607. {
  608. int ret;
  609. ret = _starpu_task_submit_head(task);
  610. STARPU_ASSERT(ret == 0);
  611. struct _starpu_job *j = _starpu_get_job_associated_to_task(task);
  612. _starpu_increment_nsubmitted_tasks_of_sched_ctx(j->task->sched_ctx);
  613. _starpu_sched_task_submit(task);
  614. STARPU_PTHREAD_MUTEX_LOCK(&j->sync_mutex);
  615. _starpu_handle_job_submission(j);
  616. _starpu_increment_nready_tasks_of_sched_ctx(j->task->sched_ctx, j->task->flops, j->task);
  617. if (task->cl)
  618. /* This would be done by data dependencies checking */
  619. _starpu_job_set_ordered_buffers(j);
  620. task->status = STARPU_TASK_READY;
  621. STARPU_PTHREAD_MUTEX_UNLOCK(&j->sync_mutex);
  622. return _starpu_push_task(j);
  623. }
  624. /*
  625. * worker->sched_mutex must be locked when calling this function.
  626. */
  627. int _starpu_task_submit_conversion_task(struct starpu_task *task,
  628. unsigned int workerid)
  629. {
  630. int ret;
  631. STARPU_ASSERT(task->cl);
  632. STARPU_ASSERT(task->execute_on_a_specific_worker);
  633. ret = _starpu_task_submit_head(task);
  634. STARPU_ASSERT(ret == 0);
  635. /* We retain handle reference count that would have been acquired by data dependencies. */
  636. unsigned i;
  637. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(task);
  638. for (i=0; i<nbuffers; i++)
  639. {
  640. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(task, i);
  641. _starpu_spin_lock(&handle->header_lock);
  642. handle->busy_count++;
  643. _starpu_spin_unlock(&handle->header_lock);
  644. }
  645. struct _starpu_job *j = _starpu_get_job_associated_to_task(task);
  646. _starpu_increment_nsubmitted_tasks_of_sched_ctx(j->task->sched_ctx);
  647. _starpu_sched_task_submit(task);
  648. STARPU_PTHREAD_MUTEX_LOCK(&j->sync_mutex);
  649. _starpu_handle_job_submission(j);
  650. _starpu_increment_nready_tasks_of_sched_ctx(j->task->sched_ctx, j->task->flops, j->task);
  651. _starpu_job_set_ordered_buffers(j);
  652. task->status = STARPU_TASK_READY;
  653. _starpu_profiling_set_task_push_start_time(task);
  654. unsigned node = starpu_worker_get_memory_node(workerid);
  655. if (starpu_get_prefetch_flag())
  656. starpu_prefetch_task_input_on_node(task, node);
  657. struct _starpu_worker *worker;
  658. worker = _starpu_get_worker_struct(workerid);
  659. starpu_task_list_push_back(&worker->local_tasks, task);
  660. _starpu_profiling_set_task_push_end_time(task);
  661. STARPU_PTHREAD_MUTEX_UNLOCK(&j->sync_mutex);
  662. return 0;
  663. }
  664. void starpu_codelet_init(struct starpu_codelet *cl)
  665. {
  666. memset(cl, 0, sizeof(struct starpu_codelet));
  667. }
  668. void starpu_codelet_display_stats(struct starpu_codelet *cl)
  669. {
  670. unsigned worker;
  671. unsigned nworkers = starpu_worker_get_count();
  672. if (cl->name)
  673. fprintf(stderr, "Statistics for codelet %s\n", cl->name);
  674. else if (cl->model && cl->model->symbol)
  675. fprintf(stderr, "Statistics for codelet %s\n", cl->model->symbol);
  676. unsigned long total = 0;
  677. for (worker = 0; worker < nworkers; worker++)
  678. total += cl->per_worker_stats[worker];
  679. for (worker = 0; worker < nworkers; worker++)
  680. {
  681. char name[32];
  682. starpu_worker_get_name(worker, name, 32);
  683. fprintf(stderr, "\t%s -> %lu / %lu (%2.2f %%)\n", name, cl->per_worker_stats[worker], total, (100.0f*cl->per_worker_stats[worker])/total);
  684. }
  685. }
  686. /*
  687. * We wait for all the tasks that have already been submitted. Note that a
  688. * regenerable is not considered finished until it was explicitely set as
  689. * non-regenerale anymore (eg. from a callback).
  690. */
  691. int _starpu_task_wait_for_all_and_return_nb_waited_tasks(void)
  692. {
  693. unsigned nsched_ctxs = _starpu_get_nsched_ctxs();
  694. unsigned sched_ctx_id = nsched_ctxs == 1 ? 0 : starpu_sched_ctx_get_context();
  695. /* if there is no indication about which context to wait,
  696. we wait for all tasks submitted to starpu */
  697. if (sched_ctx_id == STARPU_NMAX_SCHED_CTXS)
  698. {
  699. _STARPU_DEBUG("Waiting for all tasks\n");
  700. STARPU_ASSERT_MSG(_starpu_worker_may_perform_blocking_calls(), "starpu_task_wait_for_all must not be called from a task or callback");
  701. STARPU_AYU_BARRIER();
  702. struct _starpu_machine_config *config = (struct _starpu_machine_config *)_starpu_get_machine_config();
  703. if(config->topology.nsched_ctxs == 1)
  704. {
  705. _starpu_sched_do_schedule(0);
  706. return _starpu_task_wait_for_all_in_ctx_and_return_nb_waited_tasks(0);
  707. }
  708. else
  709. {
  710. int s;
  711. for(s = 0; s < STARPU_NMAX_SCHED_CTXS; s++)
  712. {
  713. if(config->sched_ctxs[s].id != STARPU_NMAX_SCHED_CTXS)
  714. {
  715. _starpu_sched_do_schedule(config->sched_ctxs[s].id);
  716. }
  717. }
  718. for(s = 0; s < STARPU_NMAX_SCHED_CTXS; s++)
  719. {
  720. if(config->sched_ctxs[s].id != STARPU_NMAX_SCHED_CTXS)
  721. {
  722. starpu_task_wait_for_all_in_ctx(config->sched_ctxs[s].id);
  723. }
  724. }
  725. return 0;
  726. }
  727. }
  728. else
  729. {
  730. _starpu_sched_do_schedule(sched_ctx_id);
  731. _STARPU_DEBUG("Waiting for tasks submitted to context %u\n", sched_ctx_id);
  732. return _starpu_task_wait_for_all_in_ctx_and_return_nb_waited_tasks(sched_ctx_id);
  733. }
  734. }
  735. int starpu_task_wait_for_all(void)
  736. {
  737. _starpu_task_wait_for_all_and_return_nb_waited_tasks();
  738. return 0;
  739. }
  740. int _starpu_task_wait_for_all_in_ctx_and_return_nb_waited_tasks(unsigned sched_ctx)
  741. {
  742. _STARPU_TRACE_TASK_WAIT_FOR_ALL_START();
  743. int ret = _starpu_wait_for_all_tasks_of_sched_ctx(sched_ctx);
  744. _STARPU_TRACE_TASK_WAIT_FOR_ALL_END();
  745. /* TODO: improve Temanejo into knowing about contexts ... */
  746. STARPU_AYU_BARRIER();
  747. return ret;
  748. }
  749. int starpu_task_wait_for_all_in_ctx(unsigned sched_ctx)
  750. {
  751. _starpu_task_wait_for_all_in_ctx_and_return_nb_waited_tasks(sched_ctx);
  752. return 0;
  753. }
  754. /*
  755. * We wait until there's a certain number of the tasks that have already been
  756. * submitted left. Note that a regenerable is not considered finished until it
  757. * was explicitely set as non-regenerale anymore (eg. from a callback).
  758. */
  759. int starpu_task_wait_for_n_submitted(unsigned n)
  760. {
  761. unsigned nsched_ctxs = _starpu_get_nsched_ctxs();
  762. unsigned sched_ctx_id = nsched_ctxs == 1 ? 0 : starpu_sched_ctx_get_context();
  763. /* if there is no indication about which context to wait,
  764. we wait for all tasks submitted to starpu */
  765. if (sched_ctx_id == STARPU_NMAX_SCHED_CTXS)
  766. {
  767. _STARPU_DEBUG("Waiting for all tasks\n");
  768. STARPU_ASSERT_MSG(_starpu_worker_may_perform_blocking_calls(), "starpu_task_wait_for_n_submitted must not be called from a task or callback");
  769. struct _starpu_machine_config *config = (struct _starpu_machine_config *)_starpu_get_machine_config();
  770. if(config->topology.nsched_ctxs == 1)
  771. _starpu_wait_for_n_submitted_tasks_of_sched_ctx(0, n);
  772. else
  773. {
  774. int s;
  775. for(s = 0; s < STARPU_NMAX_SCHED_CTXS; s++)
  776. {
  777. if(config->sched_ctxs[s].id != STARPU_NMAX_SCHED_CTXS)
  778. {
  779. _starpu_wait_for_n_submitted_tasks_of_sched_ctx(config->sched_ctxs[s].id, n);
  780. }
  781. }
  782. }
  783. return 0;
  784. }
  785. else
  786. {
  787. _STARPU_DEBUG("Waiting for tasks submitted to context %u\n", sched_ctx_id);
  788. _starpu_wait_for_n_submitted_tasks_of_sched_ctx(sched_ctx_id, n);
  789. }
  790. return 0;
  791. }
  792. int starpu_task_wait_for_n_submitted_in_ctx(unsigned sched_ctx, unsigned n)
  793. {
  794. _starpu_wait_for_n_submitted_tasks_of_sched_ctx(sched_ctx, n);
  795. return 0;
  796. }
  797. /*
  798. * We wait until there is no ready task any more (i.e. StarPU will not be able
  799. * to progress any more).
  800. */
  801. int starpu_task_wait_for_no_ready(void)
  802. {
  803. STARPU_ASSERT_MSG(_starpu_worker_may_perform_blocking_calls(), "starpu_task_wait_for_no_ready must not be called from a task or callback");
  804. struct _starpu_machine_config *config = (struct _starpu_machine_config *)_starpu_get_machine_config();
  805. if(config->topology.nsched_ctxs == 1)
  806. _starpu_wait_for_no_ready_of_sched_ctx(0);
  807. else
  808. {
  809. int s;
  810. for(s = 0; s < STARPU_NMAX_SCHED_CTXS; s++)
  811. {
  812. if(config->sched_ctxs[s].id != STARPU_NMAX_SCHED_CTXS)
  813. {
  814. _starpu_wait_for_no_ready_of_sched_ctx(config->sched_ctxs[s].id);
  815. }
  816. }
  817. }
  818. return 0;
  819. }
  820. void starpu_do_schedule(void)
  821. {
  822. struct _starpu_machine_config *config = (struct _starpu_machine_config *)_starpu_get_machine_config();
  823. if(config->topology.nsched_ctxs == 1)
  824. _starpu_sched_do_schedule(0);
  825. else
  826. {
  827. int s;
  828. for(s = 0; s < STARPU_NMAX_SCHED_CTXS; s++)
  829. {
  830. if(config->sched_ctxs[s].id != STARPU_NMAX_SCHED_CTXS)
  831. {
  832. _starpu_sched_do_schedule(config->sched_ctxs[s].id);
  833. }
  834. }
  835. }
  836. }
  837. void
  838. starpu_drivers_request_termination(void)
  839. {
  840. struct _starpu_machine_config *config = _starpu_get_machine_config();
  841. STARPU_PTHREAD_MUTEX_LOCK(&config->submitted_mutex);
  842. int nsubmitted = starpu_task_nsubmitted();
  843. config->submitting = 0;
  844. if (nsubmitted == 0)
  845. {
  846. ANNOTATE_HAPPENS_AFTER(&config->running);
  847. config->running = 0;
  848. ANNOTATE_HAPPENS_BEFORE(&config->running);
  849. STARPU_WMB();
  850. int s;
  851. for(s = 0; s < STARPU_NMAX_SCHED_CTXS; s++)
  852. {
  853. if(config->sched_ctxs[s].id != STARPU_NMAX_SCHED_CTXS)
  854. {
  855. _starpu_check_nsubmitted_tasks_of_sched_ctx(config->sched_ctxs[s].id);
  856. }
  857. }
  858. }
  859. STARPU_PTHREAD_MUTEX_UNLOCK(&config->submitted_mutex);
  860. }
  861. int starpu_task_nsubmitted(void)
  862. {
  863. int nsubmitted = 0;
  864. struct _starpu_machine_config *config = (struct _starpu_machine_config *)_starpu_get_machine_config();
  865. if(config->topology.nsched_ctxs == 1)
  866. nsubmitted = _starpu_get_nsubmitted_tasks_of_sched_ctx(0);
  867. else
  868. {
  869. int s;
  870. for(s = 0; s < STARPU_NMAX_SCHED_CTXS; s++)
  871. {
  872. if(config->sched_ctxs[s].id != STARPU_NMAX_SCHED_CTXS)
  873. {
  874. nsubmitted += _starpu_get_nsubmitted_tasks_of_sched_ctx(config->sched_ctxs[s].id);
  875. }
  876. }
  877. }
  878. return nsubmitted;
  879. }
  880. int starpu_task_nready(void)
  881. {
  882. int nready = 0;
  883. struct _starpu_machine_config *config = (struct _starpu_machine_config *)_starpu_get_machine_config();
  884. if(config->topology.nsched_ctxs == 1)
  885. nready = starpu_sched_ctx_get_nready_tasks(0);
  886. else
  887. {
  888. int s;
  889. for(s = 0; s < STARPU_NMAX_SCHED_CTXS; s++)
  890. {
  891. if(config->sched_ctxs[s].id != STARPU_NMAX_SCHED_CTXS)
  892. {
  893. nready += starpu_sched_ctx_get_nready_tasks(config->sched_ctxs[s].id);
  894. }
  895. }
  896. }
  897. return nready;
  898. }
  899. /* Return the task currently executed by the worker, or NULL if this is called
  900. * either from a thread that is not a task or simply because there is no task
  901. * being executed at the moment. */
  902. struct starpu_task *starpu_task_get_current(void)
  903. {
  904. return (struct starpu_task *) STARPU_PTHREAD_GETSPECIFIC(current_task_key);
  905. }
  906. void _starpu_set_current_task(struct starpu_task *task)
  907. {
  908. STARPU_PTHREAD_SETSPECIFIC(current_task_key, task);
  909. }
  910. #ifdef STARPU_OPENMP
  911. /* Prepare the fields of the currentl task for accepting a new set of
  912. * dependencies in anticipation of becoming a continuation.
  913. *
  914. * When the task becomes 'continued', it will only be queued again when the new
  915. * set of dependencies is fulfilled. */
  916. void _starpu_task_prepare_for_continuation(void)
  917. {
  918. _starpu_job_prepare_for_continuation(_starpu_get_job_associated_to_task(starpu_task_get_current()));
  919. }
  920. void _starpu_task_prepare_for_continuation_ext(unsigned continuation_resubmit,
  921. void (*continuation_callback_on_sleep)(void *arg), void *continuation_callback_on_sleep_arg)
  922. {
  923. _starpu_job_prepare_for_continuation_ext(_starpu_get_job_associated_to_task(starpu_task_get_current()),
  924. continuation_resubmit, continuation_callback_on_sleep, continuation_callback_on_sleep_arg);
  925. }
  926. void _starpu_task_set_omp_cleanup_callback(struct starpu_task *task, void (*omp_cleanup_callback)(void *arg), void *omp_cleanup_callback_arg)
  927. {
  928. _starpu_job_set_omp_cleanup_callback(_starpu_get_job_associated_to_task(task),
  929. omp_cleanup_callback, omp_cleanup_callback_arg);
  930. }
  931. #endif
  932. /*
  933. * Returns 0 if tasks does not use any multiformat handle, 1 otherwise.
  934. */
  935. int
  936. _starpu_task_uses_multiformat_handles(struct starpu_task *task)
  937. {
  938. unsigned i;
  939. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(task);
  940. for (i = 0; i < nbuffers; i++)
  941. {
  942. if (_starpu_data_is_multiformat_handle(STARPU_TASK_GET_HANDLE(task, i)))
  943. return 1;
  944. }
  945. return 0;
  946. }
  947. /*
  948. * Checks whether the given handle needs to be converted in order to be used on
  949. * the node given as the second argument.
  950. */
  951. int
  952. _starpu_handle_needs_conversion_task(starpu_data_handle_t handle,
  953. unsigned int node)
  954. {
  955. return _starpu_handle_needs_conversion_task_for_arch(handle, starpu_node_get_kind(node));
  956. }
  957. int
  958. _starpu_handle_needs_conversion_task_for_arch(starpu_data_handle_t handle,
  959. enum starpu_node_kind node_kind)
  960. {
  961. /*
  962. * Here, we assume that CUDA devices and OpenCL devices use the
  963. * same data structure. A conversion is only needed when moving
  964. * data from a CPU to a GPU, or the other way around.
  965. */
  966. switch (node_kind)
  967. {
  968. case STARPU_CPU_RAM:
  969. switch(starpu_node_get_kind(handle->mf_node))
  970. {
  971. case STARPU_CPU_RAM:
  972. return 0;
  973. case STARPU_CUDA_RAM: /* Fall through */
  974. case STARPU_OPENCL_RAM:
  975. case STARPU_MIC_RAM:
  976. case STARPU_SCC_RAM:
  977. return 1;
  978. default:
  979. STARPU_ABORT();
  980. }
  981. break;
  982. case STARPU_CUDA_RAM: /* Fall through */
  983. case STARPU_OPENCL_RAM:
  984. case STARPU_MIC_RAM:
  985. case STARPU_SCC_RAM:
  986. switch(starpu_node_get_kind(handle->mf_node))
  987. {
  988. case STARPU_CPU_RAM:
  989. return 1;
  990. case STARPU_CUDA_RAM:
  991. case STARPU_OPENCL_RAM:
  992. case STARPU_MIC_RAM:
  993. case STARPU_SCC_RAM:
  994. return 0;
  995. default:
  996. STARPU_ABORT();
  997. }
  998. break;
  999. default:
  1000. STARPU_ABORT();
  1001. }
  1002. /* that instruction should never be reached */
  1003. return -EINVAL;
  1004. }
  1005. void starpu_task_set_implementation(struct starpu_task *task, unsigned impl)
  1006. {
  1007. _starpu_get_job_associated_to_task(task)->nimpl = impl;
  1008. }
  1009. unsigned starpu_task_get_implementation(struct starpu_task *task)
  1010. {
  1011. return _starpu_get_job_associated_to_task(task)->nimpl;
  1012. }
  1013. static starpu_pthread_t watchdog_thread;
  1014. /* Check from times to times that StarPU does finish some tasks */
  1015. static void *watchdog_func(void *arg)
  1016. {
  1017. char *timeout_env = arg;
  1018. float timeout;
  1019. #ifdef _MSC_VER
  1020. timeout = ((float) _atoi64(timeout_env)) / 1000000;
  1021. #else
  1022. timeout = ((float) atoll(timeout_env)) / 1000000;
  1023. #endif
  1024. struct _starpu_machine_config *config = (struct _starpu_machine_config *)_starpu_get_machine_config();
  1025. starpu_pthread_setname("watchdog");
  1026. STARPU_PTHREAD_MUTEX_LOCK(&config->submitted_mutex);
  1027. while (_starpu_machine_is_running())
  1028. {
  1029. int last_nsubmitted = starpu_task_nsubmitted();
  1030. config->watchdog_ok = 0;
  1031. STARPU_PTHREAD_MUTEX_UNLOCK(&config->submitted_mutex);
  1032. /* If we do a sleep(timeout), we might have to wait too long at the end of the computation. */
  1033. /* To avoid that, we do several sleep() of 1s (and check after each if starpu is still running) */
  1034. float t;
  1035. for (t = timeout ; t > 1.; t--)
  1036. {
  1037. starpu_sleep(1.);
  1038. if (!_starpu_machine_is_running())
  1039. {
  1040. /* Application finished, don't bother finishing the sleep */
  1041. STARPU_PTHREAD_MUTEX_UNLOCK(&config->submitted_mutex);
  1042. return NULL;
  1043. }
  1044. }
  1045. /* and one final sleep (of less than 1 s) with the rest (if needed) */
  1046. if (t > 0.)
  1047. starpu_sleep(t);
  1048. STARPU_PTHREAD_MUTEX_LOCK(&config->submitted_mutex);
  1049. if (!config->watchdog_ok && last_nsubmitted
  1050. && last_nsubmitted == starpu_task_nsubmitted())
  1051. {
  1052. fprintf(stderr,"The StarPU watchdog detected that no task finished for %fs (can be configure through STARPU_WATCHDOG_TIMEOUT)\n", timeout);
  1053. if (watchdog_crash)
  1054. {
  1055. fprintf(stderr,"Crashing the process\n");
  1056. raise(SIGABRT);
  1057. }
  1058. else
  1059. fprintf(stderr,"Set the STARPU_WATCHDOG_CRASH environment variable if you want to abort the process in such a case\n");
  1060. }
  1061. /* Only shout again after another period */
  1062. config->watchdog_ok = 1;
  1063. }
  1064. STARPU_PTHREAD_MUTEX_UNLOCK(&config->submitted_mutex);
  1065. return NULL;
  1066. }
  1067. void _starpu_watchdog_init(void)
  1068. {
  1069. struct _starpu_machine_config *config = (struct _starpu_machine_config *)_starpu_get_machine_config();
  1070. char *timeout_env = starpu_getenv("STARPU_WATCHDOG_TIMEOUT");
  1071. STARPU_PTHREAD_MUTEX_INIT(&config->submitted_mutex, NULL);
  1072. if (!timeout_env)
  1073. return;
  1074. STARPU_PTHREAD_CREATE(&watchdog_thread, NULL, watchdog_func, timeout_env);
  1075. }
  1076. void _starpu_watchdog_shutdown(void)
  1077. {
  1078. char *timeout_env = starpu_getenv("STARPU_WATCHDOG_TIMEOUT");
  1079. if (!timeout_env)
  1080. return;
  1081. starpu_pthread_join(watchdog_thread, NULL);
  1082. }