task.c 38 KB

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