task.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2013 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012, 2013 Centre National de la Recherche Scientifique
  5. * Copyright (C) 2011 Télécom-SudParis
  6. * Copyright (C) 2011 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 <common/config.h>
  27. #include <common/utils.h>
  28. #include <profiling/profiling.h>
  29. #include <profiling/bound.h>
  30. #include <math.h>
  31. #include <string.h>
  32. #include <core/debug.h>
  33. /* XXX this should be reinitialized when StarPU is shutdown (or we should make
  34. * sure that no task remains !) */
  35. /* TODO we could make this hierarchical to avoid contention ? */
  36. static _starpu_pthread_cond_t submitted_cond = _STARPU_PTHREAD_COND_INITIALIZER;
  37. static _starpu_pthread_mutex_t submitted_mutex = _STARPU_PTHREAD_MUTEX_INITIALIZER;
  38. static long int nsubmitted = 0, nready = 0;
  39. static void _starpu_increment_nsubmitted_tasks(void);
  40. /* This key stores the task currently handled by the thread, note that we
  41. * cannot use the worker structure to store that information because it is
  42. * possible that we have a task with a NULL codelet, which means its callback
  43. * could be executed by a user thread as well. */
  44. static _starpu_pthread_key_t current_task_key;
  45. void starpu_task_init(struct starpu_task *task)
  46. {
  47. /* TODO: memcpy from a template instead? benchmark it */
  48. STARPU_ASSERT(task);
  49. /* As most of the fields must be initialised at NULL, let's put 0
  50. * everywhere */
  51. memset(task, 0, sizeof(struct starpu_task));
  52. /* Now we can initialise fields which recquire custom value */
  53. #if STARPU_DEFAULT_PRIO != 0
  54. task->priority = STARPU_DEFAULT_PRIO;
  55. #endif
  56. task->detach = 1;
  57. #if STARPU_TASK_INVALID != 0
  58. task->status = STARPU_TASK_INVALID;
  59. #endif
  60. task->predicted = NAN;
  61. task->predicted_transfer = NAN;
  62. task->magic = 42;
  63. task->sched_ctx = _starpu_get_initial_sched_ctx()->id;
  64. task->flops = 0.0;
  65. }
  66. /* Free all the ressources allocated for a task, without deallocating the task
  67. * structure itself (this is required for statically allocated tasks).
  68. * All values previously set by the user, like codelet and handles, remain
  69. * unchanged */
  70. void starpu_task_clean(struct starpu_task *task)
  71. {
  72. STARPU_ASSERT(task);
  73. /* If a buffer was allocated to store the profiling info, we free it. */
  74. if (task->profiling_info)
  75. {
  76. free(task->profiling_info);
  77. task->profiling_info = NULL;
  78. }
  79. /* If case the task is (still) part of a bundle */
  80. starpu_task_bundle_t bundle = task->bundle;
  81. if (bundle)
  82. starpu_task_bundle_remove(bundle, task);
  83. struct _starpu_job *j = (struct _starpu_job *)task->starpu_private;
  84. if (j)
  85. {
  86. _starpu_job_destroy(j);
  87. task->starpu_private = NULL;
  88. }
  89. }
  90. struct starpu_task * __attribute__((malloc)) starpu_task_create(void)
  91. {
  92. struct starpu_task *task;
  93. task = (struct starpu_task *) malloc(sizeof(struct starpu_task));
  94. STARPU_ASSERT(task);
  95. starpu_task_init(task);
  96. /* Dynamically allocated tasks are destroyed by default */
  97. task->destroy = 1;
  98. return task;
  99. }
  100. /* Free the ressource allocated during starpu_task_create. This function can be
  101. * called automatically after the execution of a task by setting the "destroy"
  102. * flag of the starpu_task structure (default behaviour). Calling this function
  103. * on a statically allocated task results in an undefined behaviour. */
  104. void _starpu_task_destroy(struct starpu_task *task)
  105. {
  106. /* If starpu_task_destroy is called in a callback, we just set the destroy
  107. flag. The task will be destroyed after the callback returns */
  108. if (task == starpu_task_get_current()
  109. && _starpu_get_local_worker_status() == STATUS_CALLBACK)
  110. {
  111. task->destroy = 1;
  112. }
  113. else
  114. {
  115. starpu_task_clean(task);
  116. /* TODO handle the case of task with detach = 1 and destroy = 1 */
  117. /* TODO handle the case of non terminated tasks -> return -EINVAL */
  118. free(task);
  119. }
  120. }
  121. void starpu_task_destroy(struct starpu_task *task)
  122. {
  123. STARPU_ASSERT(task);
  124. STARPU_ASSERT_MSG(!task->destroy || !task->detach, "starpu_task_destroy must not be called for task with destroy = 1 and detach = 1");
  125. _starpu_task_destroy(task);
  126. }
  127. int starpu_task_wait(struct starpu_task *task)
  128. {
  129. _STARPU_LOG_IN();
  130. STARPU_ASSERT(task);
  131. STARPU_ASSERT_MSG(!task->detach, "starpu_task_wait can only be called on tasks with detach = 0");
  132. if (task->detach || task->synchronous)
  133. {
  134. _STARPU_DEBUG("Task is detached or asynchronous. Waiting returns immediately\n");
  135. _STARPU_LOG_OUT_TAG("einval");
  136. return -EINVAL;
  137. }
  138. if (STARPU_UNLIKELY(!_starpu_worker_may_perform_blocking_calls()))
  139. {
  140. _STARPU_LOG_OUT_TAG("edeadlk");
  141. return -EDEADLK;
  142. }
  143. struct _starpu_job *j = (struct _starpu_job *)task->starpu_private;
  144. _starpu_wait_job(j);
  145. /* as this is a synchronous task, the liberation of the job
  146. structure was deferred */
  147. if (task->destroy)
  148. _starpu_task_destroy(task);
  149. _STARPU_LOG_OUT();
  150. return 0;
  151. }
  152. struct _starpu_job *_starpu_get_job_associated_to_task(struct starpu_task *task)
  153. {
  154. STARPU_ASSERT(task);
  155. if (!task->starpu_private)
  156. {
  157. struct _starpu_job *j = _starpu_job_create(task);
  158. task->starpu_private = j;
  159. }
  160. return (struct _starpu_job *)task->starpu_private;
  161. }
  162. /* NB in case we have a regenerable task, it is possible that the job was
  163. * already counted. */
  164. int _starpu_submit_job(struct _starpu_job *j)
  165. {
  166. struct starpu_task *task = j->task;
  167. _STARPU_LOG_IN();
  168. /* notify bound computation of a new task */
  169. _starpu_bound_record(j);
  170. _starpu_increment_nsubmitted_tasks();
  171. _starpu_increment_nsubmitted_tasks_of_sched_ctx(j->task->sched_ctx);
  172. #ifdef STARPU_USE_SCHED_CTX_HYPERVISOR
  173. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(j->task->sched_ctx);
  174. if(sched_ctx != NULL && j->task->sched_ctx != 0 && j->task->sched_ctx != STARPU_NMAX_SCHED_CTXS
  175. && sched_ctx->perf_counters != NULL)
  176. {
  177. _starpu_compute_buffers_footprint(j->task->cl->model, STARPU_CPU_DEFAULT, 0, j);
  178. sched_ctx->perf_counters->notify_submitted_job(j->task, j->footprint);
  179. }
  180. #endif
  181. /* We retain handle reference count */
  182. if (task->cl)
  183. {
  184. unsigned i;
  185. for (i=0; i<task->cl->nbuffers; i++)
  186. {
  187. starpu_data_handle_t handle = task->handles[i];
  188. _starpu_spin_lock(&handle->header_lock);
  189. handle->busy_count++;
  190. _starpu_spin_unlock(&handle->header_lock);
  191. }
  192. }
  193. _STARPU_PTHREAD_MUTEX_LOCK(&j->sync_mutex);
  194. /* Need to atomically set submitted to 1 and check dependencies, since
  195. * this is concucrent with _starpu_notify_cg */
  196. j->terminated = 0;
  197. if (!j->submitted)
  198. j->submitted = 1;
  199. else
  200. j->submitted = 2;
  201. int ret = _starpu_enforce_deps_and_schedule(j);
  202. _STARPU_LOG_OUT();
  203. return ret;
  204. }
  205. /* Note: this is racy, so valgrind would complain. But since we'll always put
  206. * the same values, this is not a problem. */
  207. void _starpu_codelet_check_deprecated_fields(struct starpu_codelet *cl)
  208. {
  209. if (!cl)
  210. return;
  211. int is_where_unset = cl->where == 0;
  212. /* Check deprecated and unset fields (where, <device>_func,
  213. * <device>_funcs) */
  214. /* CPU */
  215. if (cl->cpu_func && cl->cpu_func != STARPU_MULTIPLE_CPU_IMPLEMENTATIONS && cl->cpu_funcs[0])
  216. {
  217. _STARPU_DISP("[warning] [struct starpu_codelet] both cpu_func and cpu_funcs are set. Ignoring cpu_func.\n");
  218. cl->cpu_func = STARPU_MULTIPLE_CPU_IMPLEMENTATIONS;
  219. }
  220. if (cl->cpu_func && cl->cpu_func != STARPU_MULTIPLE_CPU_IMPLEMENTATIONS)
  221. {
  222. cl->cpu_funcs[0] = cl->cpu_func;
  223. cl->cpu_func = STARPU_MULTIPLE_CPU_IMPLEMENTATIONS;
  224. }
  225. if (cl->cpu_funcs[0] && cl->cpu_func == 0)
  226. {
  227. cl->cpu_func = STARPU_MULTIPLE_CPU_IMPLEMENTATIONS;
  228. }
  229. if (cl->cpu_funcs[0] && is_where_unset)
  230. {
  231. cl->where |= STARPU_CPU;
  232. }
  233. /* CUDA */
  234. if (cl->cuda_func && cl->cuda_func != STARPU_MULTIPLE_CUDA_IMPLEMENTATIONS && cl->cuda_funcs[0])
  235. {
  236. _STARPU_DISP("[warning] [struct starpu_codelet] both cuda_func and cuda_funcs are set. Ignoring cuda_func.\n");
  237. cl->cuda_func = STARPU_MULTIPLE_CUDA_IMPLEMENTATIONS;
  238. }
  239. if (cl->cuda_func && cl->cuda_func != STARPU_MULTIPLE_CUDA_IMPLEMENTATIONS)
  240. {
  241. cl->cuda_funcs[0] = cl->cuda_func;
  242. cl->cuda_func = STARPU_MULTIPLE_CUDA_IMPLEMENTATIONS;
  243. }
  244. if (cl->cuda_funcs[0] && cl->cuda_func == 0)
  245. {
  246. cl->cuda_func = STARPU_MULTIPLE_CUDA_IMPLEMENTATIONS;
  247. }
  248. if (cl->cuda_funcs[0] && is_where_unset)
  249. {
  250. cl->where |= STARPU_CUDA;
  251. }
  252. /* OpenCL */
  253. if (cl->opencl_func && cl->opencl_func != STARPU_MULTIPLE_OPENCL_IMPLEMENTATIONS && cl->opencl_funcs[0])
  254. {
  255. _STARPU_DISP("[warning] [struct starpu_codelet] both opencl_func and opencl_funcs are set. Ignoring opencl_func.\n");
  256. cl->opencl_func = STARPU_MULTIPLE_OPENCL_IMPLEMENTATIONS;
  257. }
  258. if (cl->opencl_func && cl->opencl_func != STARPU_MULTIPLE_OPENCL_IMPLEMENTATIONS)
  259. {
  260. cl->opencl_funcs[0] = cl->opencl_func;
  261. cl->opencl_func = STARPU_MULTIPLE_OPENCL_IMPLEMENTATIONS;
  262. }
  263. if (cl->opencl_funcs[0] && cl->opencl_func == 0)
  264. {
  265. cl->opencl_func = STARPU_MULTIPLE_OPENCL_IMPLEMENTATIONS;
  266. }
  267. if (cl->opencl_funcs[0] && is_where_unset)
  268. {
  269. cl->where |= STARPU_OPENCL;
  270. }
  271. }
  272. void _starpu_task_check_deprecated_fields(struct starpu_task *task)
  273. {
  274. if (task->cl)
  275. {
  276. unsigned i;
  277. for(i=0; i<task->cl->nbuffers ; i++)
  278. {
  279. if (task->buffers[i].handle && task->handles[i])
  280. {
  281. _STARPU_DISP("[warning][struct starpu_task] task->buffers[%u] and task->handles[%u] both set. Ignoring task->buffers[%u] ?\n", i, i, i);
  282. STARPU_ASSERT(task->buffers[i].mode == task->cl->modes[i]);
  283. STARPU_ABORT();
  284. }
  285. if (task->buffers[i].handle)
  286. {
  287. task->handles[i] = task->buffers[i].handle;
  288. task->cl->modes[i] = task->buffers[i].mode;
  289. }
  290. }
  291. }
  292. }
  293. /* application should submit new tasks to StarPU through this function */
  294. int starpu_task_submit(struct starpu_task *task)
  295. {
  296. _STARPU_LOG_IN();
  297. STARPU_ASSERT(task);
  298. STARPU_ASSERT_MSG(task->magic == 42, "Tasks must be created with starpu_task_create, or initialized with starpu_task_init.");
  299. int ret;
  300. unsigned is_sync = task->synchronous;
  301. starpu_task_bundle_t bundle = task->bundle;
  302. unsigned nsched_ctxs = _starpu_get_nsched_ctxs();
  303. unsigned set_sched_ctx = STARPU_NMAX_SCHED_CTXS;
  304. /* internally, StarPU manipulates a struct _starpu_job * which is a wrapper around a
  305. * task structure, it is possible that this job structure was already
  306. * allocated. */
  307. struct _starpu_job *j = _starpu_get_job_associated_to_task(task);
  308. if (task->sched_ctx == 0 && nsched_ctxs != 1 && !j->exclude_from_dag)
  309. {
  310. set_sched_ctx = starpu_task_get_context();
  311. if (set_sched_ctx != STARPU_NMAX_SCHED_CTXS)
  312. task->sched_ctx = set_sched_ctx;
  313. }
  314. if (is_sync)
  315. {
  316. /* Perhaps it is not possible to submit a synchronous
  317. * (blocking) task */
  318. if (STARPU_UNLIKELY(!_starpu_worker_may_perform_blocking_calls()))
  319. {
  320. _STARPU_LOG_OUT_TAG("EDEADLK");
  321. return -EDEADLK;
  322. }
  323. task->detach = 0;
  324. }
  325. _starpu_task_check_deprecated_fields(task);
  326. _starpu_codelet_check_deprecated_fields(task->cl);
  327. if (task->cl)
  328. {
  329. unsigned i;
  330. /* Check buffers */
  331. STARPU_ASSERT_MSG(task->cl->nbuffers <= STARPU_NMAXBUFS, "Codelet %p has too many buffers (%d vs max %d)", task->cl, task->cl->nbuffers, STARPU_NMAXBUFS);
  332. for (i = 0; i < task->cl->nbuffers; i++)
  333. {
  334. starpu_data_handle_t handle = task->handles[i];
  335. /* Make sure handles are not partitioned */
  336. STARPU_ASSERT_MSG(handle->nchildren == 0, "only unpartitioned data can be used in a task");
  337. /* Provide the home interface for now if any,
  338. * for can_execute hooks */
  339. if (handle->home_node != -1)
  340. task->interfaces[i] = starpu_data_get_interface_on_node(task->handles[i], handle->home_node);
  341. }
  342. /* Check the type of worker(s) required by the task exist */
  343. if (!_starpu_worker_exists(task))
  344. {
  345. _STARPU_LOG_OUT_TAG("ENODEV");
  346. return -ENODEV;
  347. }
  348. /* In case we require that a task should be explicitely
  349. * executed on a specific worker, we make sure that the worker
  350. * is able to execute this task. */
  351. if (task->execute_on_a_specific_worker && !starpu_combined_worker_can_execute_task(task->workerid, task, 0))
  352. {
  353. _STARPU_LOG_OUT_TAG("ENODEV");
  354. return -ENODEV;
  355. }
  356. _starpu_detect_implicit_data_deps(task);
  357. if (task->cl->model && task->cl->model->symbol)
  358. _starpu_load_perfmodel(task->cl->model);
  359. if (task->cl->power_model && task->cl->power_model->symbol)
  360. _starpu_load_perfmodel(task->cl->power_model);
  361. }
  362. if (bundle)
  363. {
  364. /* We need to make sure that models for other tasks of the
  365. * bundle are also loaded, so the scheduler can estimate the
  366. * duration of the whole bundle */
  367. _STARPU_PTHREAD_MUTEX_LOCK(&bundle->mutex);
  368. struct _starpu_task_bundle_entry *entry;
  369. entry = bundle->list;
  370. while (entry)
  371. {
  372. if (entry->task->cl->model && entry->task->cl->model->symbol)
  373. _starpu_load_perfmodel(entry->task->cl->model);
  374. if (entry->task->cl->power_model && entry->task->cl->power_model->symbol)
  375. _starpu_load_perfmodel(entry->task->cl->power_model);
  376. entry = entry->next;
  377. }
  378. _STARPU_PTHREAD_MUTEX_UNLOCK(&bundle->mutex);
  379. }
  380. /* If profiling is activated, we allocate a structure to store the
  381. * appropriate info. */
  382. struct starpu_task_profiling_info *info;
  383. int profiling = starpu_profiling_status_get();
  384. info = _starpu_allocate_profiling_info_if_needed(task);
  385. task->profiling_info = info;
  386. /* The task is considered as block until we are sure there remains not
  387. * dependency. */
  388. task->status = STARPU_TASK_BLOCKED;
  389. if (profiling)
  390. _starpu_clock_gettime(&info->submit_time);
  391. ret = _starpu_submit_job(j);
  392. if (is_sync)
  393. {
  394. _starpu_wait_job(j);
  395. if (task->destroy)
  396. _starpu_task_destroy(task);
  397. }
  398. _STARPU_LOG_OUT();
  399. return ret;
  400. }
  401. int _starpu_task_submit_internally(struct starpu_task *task)
  402. {
  403. _starpu_exclude_task_from_dag(task);
  404. return starpu_task_submit(task);
  405. }
  406. /* application should submit new tasks to StarPU through this function */
  407. int starpu_task_submit_to_ctx(struct starpu_task *task, unsigned sched_ctx_id)
  408. {
  409. task->sched_ctx = sched_ctx_id;
  410. return starpu_task_submit(task);
  411. }
  412. /* The StarPU core can submit tasks directly to the scheduler or a worker,
  413. * skipping dependencies completely (when it knows what it is doing). */
  414. int _starpu_task_submit_nodeps(struct starpu_task *task)
  415. {
  416. _starpu_task_check_deprecated_fields(task);
  417. _starpu_codelet_check_deprecated_fields(task->cl);
  418. if (task->cl)
  419. {
  420. if (task->cl->model)
  421. _starpu_load_perfmodel(task->cl->model);
  422. if (task->cl->power_model)
  423. _starpu_load_perfmodel(task->cl->power_model);
  424. }
  425. struct _starpu_job *j = _starpu_get_job_associated_to_task(task);
  426. _starpu_increment_nsubmitted_tasks();
  427. _starpu_increment_nsubmitted_tasks_of_sched_ctx(j->task->sched_ctx);
  428. _STARPU_PTHREAD_MUTEX_LOCK(&j->sync_mutex);
  429. j->submitted = 1;
  430. if (task->cl)
  431. {
  432. /* This would be done by data dependencies checking */
  433. unsigned i;
  434. for (i=0 ; i<task->cl->nbuffers ; i++)
  435. {
  436. j->ordered_buffers[i].handle = j->task->handles[i];
  437. j->ordered_buffers[i].mode = j->task->cl->modes[i];
  438. }
  439. }
  440. _STARPU_PTHREAD_MUTEX_UNLOCK(&j->sync_mutex);
  441. return _starpu_push_task(j);
  442. }
  443. /*
  444. * worker->sched_mutex must be locked when calling this function.
  445. */
  446. int _starpu_task_submit_conversion_task(struct starpu_task *task,
  447. unsigned int workerid)
  448. {
  449. STARPU_ASSERT(task->cl);
  450. STARPU_ASSERT(task->execute_on_a_specific_worker);
  451. _starpu_task_check_deprecated_fields(task);
  452. _starpu_codelet_check_deprecated_fields(task->cl);
  453. /* We should factorize that */
  454. if (task->cl->model)
  455. _starpu_load_perfmodel(task->cl->model);
  456. if (task->cl->power_model)
  457. _starpu_load_perfmodel(task->cl->power_model);
  458. /* We retain handle reference count */
  459. unsigned i;
  460. for (i=0; i<task->cl->nbuffers; i++)
  461. {
  462. starpu_data_handle_t handle = task->handles[i];
  463. _starpu_spin_lock(&handle->header_lock);
  464. handle->busy_count++;
  465. _starpu_spin_unlock(&handle->header_lock);
  466. }
  467. struct _starpu_job *j = _starpu_get_job_associated_to_task(task);
  468. _starpu_increment_nsubmitted_tasks();
  469. _starpu_increment_nsubmitted_tasks_of_sched_ctx(j->task->sched_ctx);
  470. _STARPU_PTHREAD_MUTEX_LOCK(&j->sync_mutex);
  471. j->submitted = 1;
  472. _starpu_increment_nready_tasks();
  473. for (i=0 ; i<task->cl->nbuffers ; i++)
  474. {
  475. j->ordered_buffers[i].handle = j->task->handles[i];
  476. j->ordered_buffers[i].mode = j->task->cl->modes[i];
  477. }
  478. _STARPU_LOG_IN();
  479. task->status = STARPU_TASK_READY;
  480. _starpu_profiling_set_task_push_start_time(task);
  481. unsigned node = starpu_worker_get_memory_node(workerid);
  482. if (starpu_get_prefetch_flag())
  483. starpu_prefetch_task_input_on_node(task, node);
  484. struct _starpu_worker *worker;
  485. worker = _starpu_get_worker_struct(workerid);
  486. starpu_task_list_push_front(&worker->local_tasks, task);
  487. _starpu_profiling_set_task_push_end_time(task);
  488. _STARPU_LOG_OUT();
  489. _STARPU_PTHREAD_MUTEX_UNLOCK(&j->sync_mutex);
  490. return 0;
  491. }
  492. void starpu_codelet_init(struct starpu_codelet *cl)
  493. {
  494. memset(cl, 0, sizeof(struct starpu_codelet));
  495. }
  496. void starpu_display_codelet_stats(struct starpu_codelet *cl)
  497. {
  498. unsigned worker;
  499. unsigned nworkers = starpu_worker_get_count();
  500. if (cl->name)
  501. fprintf(stderr, "Statistics for codelet %s\n", cl->name);
  502. else if (cl->model && cl->model->symbol)
  503. fprintf(stderr, "Statistics for codelet %s\n", cl->model->symbol);
  504. unsigned long total = 0;
  505. for (worker = 0; worker < nworkers; worker++)
  506. total += cl->per_worker_stats[worker];
  507. for (worker = 0; worker < nworkers; worker++)
  508. {
  509. char name[32];
  510. starpu_worker_get_name(worker, name, 32);
  511. fprintf(stderr, "\t%s -> %lu / %lu (%2.2f %%)\n", name, cl->per_worker_stats[worker], total, (100.0f*cl->per_worker_stats[worker])/total);
  512. }
  513. }
  514. /*
  515. * We wait for all the tasks that have already been submitted. Note that a
  516. * regenerable is not considered finished until it was explicitely set as
  517. * non-regenerale anymore (eg. from a callback).
  518. */
  519. int starpu_task_wait_for_all(void)
  520. {
  521. unsigned nsched_ctxs = _starpu_get_nsched_ctxs();
  522. unsigned sched_ctx_id = nsched_ctxs == 1 ? 0 : starpu_task_get_context();
  523. /* if there is no indication about which context to wait,
  524. we wait for all tasks submitted to starpu */
  525. if (sched_ctx_id == STARPU_NMAX_SCHED_CTXS)
  526. {
  527. _STARPU_DEBUG("Waiting for all tasks\n");
  528. if (STARPU_UNLIKELY(!_starpu_worker_may_perform_blocking_calls()))
  529. return -EDEADLK;
  530. _STARPU_PTHREAD_MUTEX_LOCK(&submitted_mutex);
  531. _STARPU_TRACE_TASK_WAIT_FOR_ALL;
  532. while (nsubmitted > 0)
  533. _STARPU_PTHREAD_COND_WAIT(&submitted_cond, &submitted_mutex);
  534. _STARPU_PTHREAD_MUTEX_UNLOCK(&submitted_mutex);
  535. #ifdef HAVE_AYUDAME_H
  536. if (AYU_event) AYU_event(AYU_BARRIER, 0, NULL);
  537. #endif
  538. }
  539. else
  540. {
  541. _STARPU_DEBUG("Waiting for tasks submitted to context %u\n", sched_ctx_id);
  542. _starpu_wait_for_all_tasks_of_sched_ctx(sched_ctx_id);
  543. #ifdef HAVE_AYUDAME_H
  544. /* TODO: improve Temanejo into knowing about contexts ... */
  545. if (AYU_event) AYU_event(AYU_BARRIER, 0, NULL);
  546. #endif
  547. }
  548. return 0;
  549. }
  550. int starpu_task_wait_for_all_in_ctx(unsigned sched_ctx)
  551. {
  552. _starpu_wait_for_all_tasks_of_sched_ctx(sched_ctx);
  553. #ifdef HAVE_AYUDAME_H
  554. if (AYU_event) AYU_event(AYU_BARRIER, 0, NULL);
  555. #endif
  556. return 0;
  557. }
  558. /*
  559. * We wait until there is no ready task any more (i.e. StarPU will not be able
  560. * to progress any more).
  561. */
  562. int starpu_task_wait_for_no_ready(void)
  563. {
  564. if (STARPU_UNLIKELY(!_starpu_worker_may_perform_blocking_calls()))
  565. return -EDEADLK;
  566. _STARPU_PTHREAD_MUTEX_LOCK(&submitted_mutex);
  567. _STARPU_TRACE_TASK_WAIT_FOR_ALL;
  568. while (nready > 0)
  569. _STARPU_PTHREAD_COND_WAIT(&submitted_cond, &submitted_mutex);
  570. _STARPU_PTHREAD_MUTEX_UNLOCK(&submitted_mutex);
  571. return 0;
  572. }
  573. void _starpu_decrement_nsubmitted_tasks(void)
  574. {
  575. struct _starpu_machine_config *config = _starpu_get_machine_config();
  576. _STARPU_PTHREAD_MUTEX_LOCK(&submitted_mutex);
  577. if (--nsubmitted == 0)
  578. {
  579. if (!config->submitting)
  580. config->running = 0;
  581. _STARPU_PTHREAD_COND_BROADCAST(&submitted_cond);
  582. }
  583. _STARPU_TRACE_UPDATE_TASK_CNT(nsubmitted);
  584. _STARPU_PTHREAD_MUTEX_UNLOCK(&submitted_mutex);
  585. }
  586. void
  587. starpu_drivers_request_termination(void)
  588. {
  589. struct _starpu_machine_config *config = _starpu_get_machine_config();
  590. _STARPU_PTHREAD_MUTEX_LOCK(&submitted_mutex);
  591. config->submitting = 0;
  592. if (nsubmitted == 0)
  593. {
  594. config->running = 0;
  595. _STARPU_PTHREAD_COND_BROADCAST(&submitted_cond);
  596. }
  597. _STARPU_PTHREAD_MUTEX_UNLOCK(&submitted_mutex);
  598. }
  599. static void _starpu_increment_nsubmitted_tasks(void)
  600. {
  601. _STARPU_PTHREAD_MUTEX_LOCK(&submitted_mutex);
  602. nsubmitted++;
  603. _STARPU_TRACE_UPDATE_TASK_CNT(nsubmitted);
  604. _STARPU_PTHREAD_MUTEX_UNLOCK(&submitted_mutex);
  605. }
  606. void _starpu_increment_nready_tasks(void)
  607. {
  608. _STARPU_PTHREAD_MUTEX_LOCK(&submitted_mutex);
  609. nready++;
  610. _STARPU_PTHREAD_MUTEX_UNLOCK(&submitted_mutex);
  611. }
  612. void _starpu_decrement_nready_tasks(void)
  613. {
  614. _STARPU_PTHREAD_MUTEX_LOCK(&submitted_mutex);
  615. if (--nready == 0)
  616. _STARPU_PTHREAD_COND_BROADCAST(&submitted_cond);
  617. _STARPU_PTHREAD_MUTEX_UNLOCK(&submitted_mutex);
  618. }
  619. void _starpu_initialize_current_task_key(void)
  620. {
  621. _STARPU_PTHREAD_KEY_CREATE(&current_task_key, NULL);
  622. }
  623. /* Return the task currently executed by the worker, or NULL if this is called
  624. * either from a thread that is not a task or simply because there is no task
  625. * being executed at the moment. */
  626. struct starpu_task *starpu_task_get_current(void)
  627. {
  628. return (struct starpu_task *) _STARPU_PTHREAD_GETSPECIFIC(current_task_key);
  629. }
  630. void _starpu_set_current_task(struct starpu_task *task)
  631. {
  632. _STARPU_PTHREAD_SETSPECIFIC(current_task_key, task);
  633. }
  634. /*
  635. * Returns 0 if tasks does not use any multiformat handle, 1 otherwise.
  636. */
  637. int
  638. _starpu_task_uses_multiformat_handles(struct starpu_task *task)
  639. {
  640. unsigned i;
  641. for (i = 0; i < task->cl->nbuffers; i++)
  642. {
  643. if (_starpu_data_is_multiformat_handle(task->handles[i]))
  644. return 1;
  645. }
  646. return 0;
  647. }
  648. /*
  649. * Checks whether the given handle needs to be converted in order to be used on
  650. * the node given as the second argument.
  651. */
  652. int
  653. _starpu_handle_needs_conversion_task(starpu_data_handle_t handle,
  654. unsigned int node)
  655. {
  656. return _starpu_handle_needs_conversion_task_for_arch(handle, starpu_node_get_kind(node));
  657. }
  658. int
  659. _starpu_handle_needs_conversion_task_for_arch(starpu_data_handle_t handle,
  660. enum starpu_node_kind node_kind)
  661. {
  662. /*
  663. * Here, we assume that CUDA devices and OpenCL devices use the
  664. * same data structure. A conversion is only needed when moving
  665. * data from a CPU to a GPU, or the other way around.
  666. */
  667. switch (node_kind)
  668. {
  669. case STARPU_CPU_RAM:
  670. switch(starpu_node_get_kind(handle->mf_node))
  671. {
  672. case STARPU_CPU_RAM:
  673. return 0;
  674. case STARPU_CUDA_RAM: /* Fall through */
  675. case STARPU_OPENCL_RAM:
  676. return 1;
  677. default:
  678. STARPU_ABORT();
  679. }
  680. break;
  681. case STARPU_CUDA_RAM: /* Fall through */
  682. case STARPU_OPENCL_RAM:
  683. switch(starpu_node_get_kind(handle->mf_node))
  684. {
  685. case STARPU_CPU_RAM:
  686. return 1;
  687. case STARPU_CUDA_RAM:
  688. case STARPU_OPENCL_RAM:
  689. return 0;
  690. default:
  691. STARPU_ABORT();
  692. }
  693. break;
  694. default:
  695. STARPU_ABORT();
  696. }
  697. /* that instruction should never be reached */
  698. return -EINVAL;
  699. }
  700. starpu_cpu_func_t _starpu_task_get_cpu_nth_implementation(struct starpu_codelet *cl, unsigned nimpl)
  701. {
  702. return cl->cpu_funcs[nimpl];
  703. }
  704. starpu_cuda_func_t _starpu_task_get_cuda_nth_implementation(struct starpu_codelet *cl, unsigned nimpl)
  705. {
  706. return cl->cuda_funcs[nimpl];
  707. }
  708. starpu_opencl_func_t _starpu_task_get_opencl_nth_implementation(struct starpu_codelet *cl, unsigned nimpl)
  709. {
  710. return cl->opencl_funcs[nimpl];
  711. }