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->predicted_start = NAN;
  85. task->magic = 42;
  86. task->sched_ctx = STARPU_NMAX_SCHED_CTXS;
  87. task->flops = 0.0;
  88. }
  89. /* Free all the ressources allocated for a task, without deallocating the task
  90. * structure itself (this is required for statically allocated tasks).
  91. * All values previously set by the user, like codelet and handles, remain
  92. * unchanged */
  93. void starpu_task_clean(struct starpu_task *task)
  94. {
  95. STARPU_ASSERT(task);
  96. /* If a buffer was allocated to store the profiling info, we free it. */
  97. if (task->profiling_info)
  98. {
  99. free(task->profiling_info);
  100. task->profiling_info = NULL;
  101. }
  102. /* If case the task is (still) part of a bundle */
  103. starpu_task_bundle_t bundle = task->bundle;
  104. if (bundle)
  105. starpu_task_bundle_remove(bundle, task);
  106. if (task->dyn_handles)
  107. {
  108. free(task->dyn_handles);
  109. task->dyn_handles = NULL;
  110. free(task->dyn_interfaces);
  111. task->dyn_interfaces = NULL;
  112. }
  113. if (task->dyn_modes)
  114. {
  115. free(task->dyn_modes);
  116. task->dyn_modes = NULL;
  117. }
  118. struct _starpu_job *j = (struct _starpu_job *)task->starpu_private;
  119. if (j)
  120. {
  121. _starpu_job_destroy(j);
  122. task->starpu_private = NULL;
  123. }
  124. }
  125. struct starpu_task * STARPU_ATTRIBUTE_MALLOC starpu_task_create(void)
  126. {
  127. struct starpu_task *task;
  128. _STARPU_MALLOC(task, sizeof(struct starpu_task));
  129. starpu_task_init(task);
  130. /* Dynamically allocated tasks are destroyed by default */
  131. task->destroy = 1;
  132. return task;
  133. }
  134. /* Free the ressource allocated during starpu_task_create. This function can be
  135. * called automatically after the execution of a task by setting the "destroy"
  136. * flag of the starpu_task structure (default behaviour). Calling this function
  137. * on a statically allocated task results in an undefined behaviour. */
  138. void _starpu_task_destroy(struct starpu_task *task)
  139. {
  140. /* If starpu_task_destroy is called in a callback, we just set the destroy
  141. flag. The task will be destroyed after the callback returns */
  142. if (task == starpu_task_get_current()
  143. && _starpu_get_local_worker_status() == STATUS_CALLBACK)
  144. {
  145. task->destroy = 1;
  146. }
  147. else
  148. {
  149. starpu_task_clean(task);
  150. /* TODO handle the case of task with detach = 1 and destroy = 1 */
  151. /* TODO handle the case of non terminated tasks -> return -EINVAL */
  152. /* Does user want StarPU release cl_arg ? */
  153. if (task->cl_arg_free)
  154. free(task->cl_arg);
  155. /* Does user want StarPU release callback_arg ? */
  156. if (task->callback_arg_free)
  157. free(task->callback_arg);
  158. /* Does user want StarPU release prologue_callback_arg ? */
  159. if (task->prologue_callback_arg_free)
  160. free(task->prologue_callback_arg);
  161. /* Does user want StarPU release prologue_pop_arg ? */
  162. if (task->prologue_callback_pop_arg_free)
  163. free(task->prologue_callback_pop_arg);
  164. free(task);
  165. }
  166. }
  167. void starpu_task_destroy(struct starpu_task *task)
  168. {
  169. STARPU_ASSERT(task);
  170. STARPU_ASSERT_MSG(!task->destroy || !task->detach, "starpu_task_destroy must not be called for task with destroy = 1 and detach = 1");
  171. _starpu_task_destroy(task);
  172. }
  173. int starpu_task_finished(struct starpu_task *task)
  174. {
  175. STARPU_ASSERT(task);
  176. STARPU_ASSERT_MSG(!task->detach, "starpu_task_finished can only be called on tasks with detach = 0");
  177. return _starpu_job_finished(_starpu_get_job_associated_to_task(task));
  178. }
  179. int starpu_task_wait(struct starpu_task *task)
  180. {
  181. _STARPU_LOG_IN();
  182. STARPU_ASSERT(task);
  183. STARPU_ASSERT_MSG(!task->detach, "starpu_task_wait can only be called on tasks with detach = 0");
  184. if (task->detach || task->synchronous)
  185. {
  186. _STARPU_DEBUG("Task is detached or synchronous. Waiting returns immediately\n");
  187. _STARPU_LOG_OUT_TAG("einval");
  188. return -EINVAL;
  189. }
  190. STARPU_ASSERT_MSG(_starpu_worker_may_perform_blocking_calls(), "starpu_task_wait must not be called from a task or callback");
  191. struct _starpu_job *j = (struct _starpu_job *)task->starpu_private;
  192. _STARPU_TRACE_TASK_WAIT_START(j);
  193. _starpu_wait_job(j);
  194. /* as this is a synchronous task, the liberation of the job
  195. structure was deferred */
  196. if (task->destroy)
  197. _starpu_task_destroy(task);
  198. _STARPU_TRACE_TASK_WAIT_END();
  199. _STARPU_LOG_OUT();
  200. return 0;
  201. }
  202. int starpu_task_wait_array(struct starpu_task **tasks, unsigned nb_tasks)
  203. {
  204. unsigned i;
  205. for (i = 0; i < nb_tasks; i++)
  206. {
  207. int ret = starpu_task_wait(tasks[i]);
  208. if (ret)
  209. return ret;
  210. }
  211. return 0;
  212. }
  213. #ifdef STARPU_OPENMP
  214. int _starpu_task_test_termination(struct starpu_task *task)
  215. {
  216. STARPU_ASSERT(task);
  217. STARPU_ASSERT_MSG(!task->detach, "starpu_task_wait can only be called on tasks with detach = 0");
  218. if (task->detach || task->synchronous)
  219. {
  220. _STARPU_DEBUG("Task is detached or synchronous\n");
  221. _STARPU_LOG_OUT_TAG("einval");
  222. return -EINVAL;
  223. }
  224. struct _starpu_job *j = (struct _starpu_job *)task->starpu_private;
  225. int ret = _starpu_test_job_termination(j);
  226. if (ret)
  227. {
  228. if (task->destroy)
  229. _starpu_task_destroy(task);
  230. }
  231. return ret;
  232. }
  233. #endif
  234. /* NB in case we have a regenerable task, it is possible that the job was
  235. * already counted. */
  236. int _starpu_submit_job(struct _starpu_job *j)
  237. {
  238. struct starpu_task *task = j->task;
  239. int ret;
  240. #ifdef STARPU_OPENMP
  241. const unsigned continuation = j->continuation;
  242. #else
  243. const unsigned continuation = 0;
  244. #endif
  245. _STARPU_LOG_IN();
  246. /* notify bound computation of a new task */
  247. _starpu_bound_record(j);
  248. _starpu_increment_nsubmitted_tasks_of_sched_ctx(j->task->sched_ctx);
  249. _starpu_sched_task_submit(task);
  250. #ifdef STARPU_USE_SC_HYPERVISOR
  251. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(j->task->sched_ctx);
  252. if(sched_ctx != NULL && j->task->sched_ctx != _starpu_get_initial_sched_ctx()->id && j->task->sched_ctx != STARPU_NMAX_SCHED_CTXS
  253. && sched_ctx->perf_counters != NULL)
  254. {
  255. struct starpu_perfmodel_arch arch;
  256. _STARPU_MALLOC(arch.devices, sizeof(struct starpu_perfmodel_device));
  257. arch.ndevices = 1;
  258. arch.devices[0].type = STARPU_CPU_WORKER;
  259. arch.devices[0].devid = 0;
  260. arch.devices[0].ncores = 1;
  261. _starpu_compute_buffers_footprint(j->task->cl->model, &arch, 0, j);
  262. free(arch.devices);
  263. size_t data_size = 0;
  264. if (j->task->cl)
  265. {
  266. unsigned i, nbuffers = STARPU_TASK_GET_NBUFFERS(j->task);
  267. for(i = 0; i < nbuffers; i++)
  268. {
  269. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(task, i);
  270. if (handle != NULL)
  271. data_size += _starpu_data_get_size(handle);
  272. }
  273. }
  274. _STARPU_TRACE_HYPERVISOR_BEGIN();
  275. sched_ctx->perf_counters->notify_submitted_job(j->task, j->footprint, data_size);
  276. _STARPU_TRACE_HYPERVISOR_END();
  277. }
  278. #endif//STARPU_USE_SC_HYPERVISOR
  279. /* We retain handle reference count */
  280. if (task->cl && !continuation)
  281. {
  282. unsigned i;
  283. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(task);
  284. for (i=0; i<nbuffers; i++)
  285. {
  286. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(task, i);
  287. _starpu_spin_lock(&handle->header_lock);
  288. handle->busy_count++;
  289. _starpu_spin_unlock(&handle->header_lock);
  290. }
  291. }
  292. STARPU_PTHREAD_MUTEX_LOCK(&j->sync_mutex);
  293. _starpu_handle_job_submission(j);
  294. #ifdef STARPU_OPENMP
  295. if (continuation)
  296. {
  297. j->discontinuous = 1;
  298. j->continuation = 0;
  299. }
  300. #endif
  301. #ifdef STARPU_OPENMP
  302. if (continuation)
  303. {
  304. ret = _starpu_reenforce_task_deps_and_schedule(j);
  305. }
  306. else
  307. #endif
  308. {
  309. ret = _starpu_enforce_deps_and_schedule(j);
  310. }
  311. _STARPU_LOG_OUT();
  312. return ret;
  313. }
  314. /* Note: this is racy, so valgrind would complain. But since we'll always put
  315. * the same values, this is not a problem. */
  316. void _starpu_codelet_check_deprecated_fields(struct starpu_codelet *cl)
  317. {
  318. if (!cl)
  319. return;
  320. int is_where_unset = cl->where == 0;
  321. unsigned i, some_impl;
  322. /* Check deprecated and unset fields (where, <device>_func,
  323. * <device>_funcs) */
  324. /* CPU */
  325. if (cl->cpu_func && cl->cpu_func != STARPU_MULTIPLE_CPU_IMPLEMENTATIONS && cl->cpu_funcs[0])
  326. {
  327. _STARPU_DISP("[warning] [struct starpu_codelet] both cpu_func and cpu_funcs are set. Ignoring cpu_func.\n");
  328. cl->cpu_func = STARPU_MULTIPLE_CPU_IMPLEMENTATIONS;
  329. }
  330. if (cl->cpu_func && cl->cpu_func != STARPU_MULTIPLE_CPU_IMPLEMENTATIONS)
  331. {
  332. cl->cpu_funcs[0] = cl->cpu_func;
  333. cl->cpu_func = STARPU_MULTIPLE_CPU_IMPLEMENTATIONS;
  334. }
  335. some_impl = 0;
  336. for (i = 0; i < STARPU_MAXIMPLEMENTATIONS; i++)
  337. if (cl->cpu_funcs[i])
  338. {
  339. some_impl = 1;
  340. break;
  341. }
  342. if (some_impl && cl->cpu_func == 0)
  343. {
  344. cl->cpu_func = STARPU_MULTIPLE_CPU_IMPLEMENTATIONS;
  345. }
  346. if (some_impl && is_where_unset)
  347. {
  348. cl->where |= STARPU_CPU;
  349. }
  350. /* CUDA */
  351. if (cl->cuda_func && cl->cuda_func != STARPU_MULTIPLE_CUDA_IMPLEMENTATIONS && cl->cuda_funcs[0])
  352. {
  353. _STARPU_DISP("[warning] [struct starpu_codelet] both cuda_func and cuda_funcs are set. Ignoring cuda_func.\n");
  354. cl->cuda_func = STARPU_MULTIPLE_CUDA_IMPLEMENTATIONS;
  355. }
  356. if (cl->cuda_func && cl->cuda_func != STARPU_MULTIPLE_CUDA_IMPLEMENTATIONS)
  357. {
  358. cl->cuda_funcs[0] = cl->cuda_func;
  359. cl->cuda_func = STARPU_MULTIPLE_CUDA_IMPLEMENTATIONS;
  360. }
  361. some_impl = 0;
  362. for (i = 0; i < STARPU_MAXIMPLEMENTATIONS; i++)
  363. if (cl->cuda_funcs[i])
  364. {
  365. some_impl = 1;
  366. break;
  367. }
  368. if (some_impl && cl->cuda_func == 0)
  369. {
  370. cl->cuda_func = STARPU_MULTIPLE_CUDA_IMPLEMENTATIONS;
  371. }
  372. if (some_impl && is_where_unset)
  373. {
  374. cl->where |= STARPU_CUDA;
  375. }
  376. /* OpenCL */
  377. if (cl->opencl_func && cl->opencl_func != STARPU_MULTIPLE_OPENCL_IMPLEMENTATIONS && cl->opencl_funcs[0])
  378. {
  379. _STARPU_DISP("[warning] [struct starpu_codelet] both opencl_func and opencl_funcs are set. Ignoring opencl_func.\n");
  380. cl->opencl_func = STARPU_MULTIPLE_OPENCL_IMPLEMENTATIONS;
  381. }
  382. if (cl->opencl_func && cl->opencl_func != STARPU_MULTIPLE_OPENCL_IMPLEMENTATIONS)
  383. {
  384. cl->opencl_funcs[0] = cl->opencl_func;
  385. cl->opencl_func = STARPU_MULTIPLE_OPENCL_IMPLEMENTATIONS;
  386. }
  387. some_impl = 0;
  388. for (i = 0; i < STARPU_MAXIMPLEMENTATIONS; i++)
  389. if (cl->opencl_funcs[i])
  390. {
  391. some_impl = 1;
  392. break;
  393. }
  394. if (some_impl && cl->opencl_func == 0)
  395. {
  396. cl->opencl_func = STARPU_MULTIPLE_OPENCL_IMPLEMENTATIONS;
  397. }
  398. if (some_impl && is_where_unset)
  399. {
  400. cl->where |= STARPU_OPENCL;
  401. }
  402. some_impl = 0;
  403. for (i = 0; i < STARPU_MAXIMPLEMENTATIONS; i++)
  404. if (cl->mic_funcs[i])
  405. {
  406. some_impl = 1;
  407. break;
  408. }
  409. if (some_impl && is_where_unset)
  410. {
  411. cl->where |= STARPU_MIC;
  412. }
  413. some_impl = 0;
  414. for (i = 0; i < STARPU_MAXIMPLEMENTATIONS; i++)
  415. if (cl->scc_funcs[i])
  416. {
  417. some_impl = 1;
  418. break;
  419. }
  420. if (some_impl && is_where_unset)
  421. {
  422. cl->where |= STARPU_SCC;
  423. }
  424. some_impl = 0;
  425. for (i = 0; i < STARPU_MAXIMPLEMENTATIONS; i++)
  426. if (cl->cpu_funcs_name[i])
  427. {
  428. some_impl = 1;
  429. break;
  430. }
  431. if (some_impl && is_where_unset)
  432. {
  433. cl->where |= STARPU_MIC|STARPU_SCC;
  434. }
  435. }
  436. void _starpu_task_check_deprecated_fields(struct starpu_task *task STARPU_ATTRIBUTE_UNUSED)
  437. {
  438. /* None any more */
  439. }
  440. static int _starpu_task_submit_head(struct starpu_task *task)
  441. {
  442. unsigned is_sync = task->synchronous;
  443. struct _starpu_job *j = _starpu_get_job_associated_to_task(task);
  444. if (j->internal)
  445. {
  446. // Internal tasks are submitted to initial context
  447. task->sched_ctx = _starpu_get_initial_sched_ctx()->id;
  448. }
  449. else if (task->sched_ctx == STARPU_NMAX_SCHED_CTXS)
  450. {
  451. // If the task has not specified a context, we set the current context
  452. task->sched_ctx = _starpu_sched_ctx_get_current_context();
  453. }
  454. if (is_sync)
  455. {
  456. /* Perhaps it is not possible to submit a synchronous
  457. * (blocking) task */
  458. STARPU_ASSERT_MSG(_starpu_worker_may_perform_blocking_calls(), "submitting a synchronous task must not be done from a task or a callback");
  459. task->detach = 0;
  460. }
  461. _starpu_task_check_deprecated_fields(task);
  462. _starpu_codelet_check_deprecated_fields(task->cl);
  463. if (task->cl)
  464. {
  465. unsigned i;
  466. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(task);
  467. _STARPU_TRACE_UPDATE_TASK_CNT(0);
  468. /* Check buffers */
  469. if (task->dyn_handles == NULL)
  470. 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);
  471. if (task->dyn_handles)
  472. {
  473. _STARPU_MALLOC(task->dyn_interfaces, nbuffers * sizeof(void *));
  474. }
  475. for (i = 0; i < nbuffers; i++)
  476. {
  477. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(task, i);
  478. /* Make sure handles are not partitioned */
  479. STARPU_ASSERT_MSG(handle->nchildren == 0, "only unpartitioned data (or the pieces of a partitioned data) can be used in a task");
  480. /* Provide the home interface for now if any,
  481. * for can_execute hooks */
  482. if (handle->home_node != -1)
  483. _STARPU_TASK_SET_INTERFACE(task, starpu_data_get_interface_on_node(handle, handle->home_node), i);
  484. }
  485. /* Check the type of worker(s) required by the task exist */
  486. if (!_starpu_worker_exists(task))
  487. {
  488. _STARPU_LOG_OUT_TAG("ENODEV");
  489. return -ENODEV;
  490. }
  491. /* In case we require that a task should be explicitely
  492. * executed on a specific worker, we make sure that the worker
  493. * is able to execute this task. */
  494. if (task->execute_on_a_specific_worker && !starpu_combined_worker_can_execute_task(task->workerid, task, 0))
  495. {
  496. _STARPU_LOG_OUT_TAG("ENODEV");
  497. return -ENODEV;
  498. }
  499. if (task->cl->model)
  500. _starpu_init_and_load_perfmodel(task->cl->model);
  501. if (task->cl->energy_model)
  502. _starpu_init_and_load_perfmodel(task->cl->energy_model);
  503. }
  504. return 0;
  505. }
  506. /* application should submit new tasks to StarPU through this function */
  507. int starpu_task_submit(struct starpu_task *task)
  508. {
  509. _STARPU_LOG_IN();
  510. STARPU_ASSERT(task);
  511. STARPU_ASSERT_MSG(task->magic == 42, "Tasks must be created with starpu_task_create, or initialized with starpu_task_init.");
  512. int ret;
  513. unsigned is_sync = task->synchronous;
  514. starpu_task_bundle_t bundle = task->bundle;
  515. /* internally, StarPU manipulates a struct _starpu_job * which is a wrapper around a
  516. * task structure, it is possible that this job structure was already
  517. * allocated. */
  518. struct _starpu_job *j = _starpu_get_job_associated_to_task(task);
  519. const unsigned continuation =
  520. #ifdef STARPU_OPENMP
  521. j->continuation
  522. #else
  523. 0
  524. #endif
  525. ;
  526. _STARPU_TRACE_TASK_SUBMIT_START();
  527. if (!j->internal)
  528. {
  529. int nsubmitted_tasks = starpu_task_nsubmitted();
  530. if (limit_max_submitted_tasks >= 0 && limit_max_submitted_tasks < nsubmitted_tasks
  531. && limit_min_submitted_tasks >= 0 && limit_min_submitted_tasks < nsubmitted_tasks)
  532. {
  533. starpu_do_schedule();
  534. starpu_task_wait_for_n_submitted(limit_min_submitted_tasks);
  535. }
  536. }
  537. ret = _starpu_task_submit_head(task);
  538. if (ret)
  539. {
  540. _STARPU_TRACE_TASK_SUBMIT_END();
  541. return ret;
  542. }
  543. if (!j->internal && !continuation)
  544. _STARPU_TRACE_TASK_SUBMIT(j);
  545. /* If this is a continuation, we don't modify the implicit data dependencies detected earlier. */
  546. if (task->cl && !continuation)
  547. _starpu_detect_implicit_data_deps(task);
  548. if (bundle)
  549. {
  550. /* We need to make sure that models for other tasks of the
  551. * bundle are also loaded, so the scheduler can estimate the
  552. * duration of the whole bundle */
  553. STARPU_PTHREAD_MUTEX_LOCK(&bundle->mutex);
  554. struct _starpu_task_bundle_entry *entry;
  555. entry = bundle->list;
  556. while (entry)
  557. {
  558. if (entry->task->cl->model)
  559. _starpu_init_and_load_perfmodel(entry->task->cl->model);
  560. if (entry->task->cl->energy_model)
  561. _starpu_init_and_load_perfmodel(entry->task->cl->energy_model);
  562. entry = entry->next;
  563. }
  564. STARPU_PTHREAD_MUTEX_UNLOCK(&bundle->mutex);
  565. }
  566. /* If profiling is activated, we allocate a structure to store the
  567. * appropriate info. */
  568. struct starpu_profiling_task_info *info;
  569. int profiling = starpu_profiling_status_get();
  570. info = _starpu_allocate_profiling_info_if_needed(task);
  571. task->profiling_info = info;
  572. /* The task is considered as block until we are sure there remains not
  573. * dependency. */
  574. task->status = STARPU_TASK_BLOCKED;
  575. if (profiling)
  576. _starpu_clock_gettime(&info->submit_time);
  577. ret = _starpu_submit_job(j);
  578. #ifdef STARPU_SIMGRID
  579. if (_starpu_simgrid_task_submit_cost())
  580. MSG_process_sleep(0.000001);
  581. #endif
  582. if (is_sync)
  583. {
  584. _starpu_sched_do_schedule(task->sched_ctx);
  585. _starpu_wait_job(j);
  586. if (task->destroy)
  587. _starpu_task_destroy(task);
  588. }
  589. _STARPU_TRACE_TASK_SUBMIT_END();
  590. _STARPU_LOG_OUT();
  591. return ret;
  592. }
  593. int _starpu_task_submit_internally(struct starpu_task *task)
  594. {
  595. struct _starpu_job *j = _starpu_get_job_associated_to_task(task);
  596. j->internal = 1;
  597. return starpu_task_submit(task);
  598. }
  599. /* application should submit new tasks to StarPU through this function */
  600. int starpu_task_submit_to_ctx(struct starpu_task *task, unsigned sched_ctx_id)
  601. {
  602. task->sched_ctx = sched_ctx_id;
  603. return starpu_task_submit(task);
  604. }
  605. /* The StarPU core can submit tasks directly to the scheduler or a worker,
  606. * skipping dependencies completely (when it knows what it is doing). */
  607. int _starpu_task_submit_nodeps(struct starpu_task *task)
  608. {
  609. int 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. }