task.c 25 KB

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