task.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010, 2011 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012 Centre National de la Recherche Scientifique
  5. * Copyright (C) 2011 Télécom-SudParis
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <starpu.h>
  19. #include <starpu_profiling.h>
  20. #include <starpu_task_bundle.h>
  21. #include <core/workers.h>
  22. #include <core/jobs.h>
  23. #include <core/task.h>
  24. #include <common/config.h>
  25. #include <common/utils.h>
  26. #include <profiling/profiling.h>
  27. #include <profiling/bound.h>
  28. /* XXX this should be reinitialized when StarPU is shutdown (or we should make
  29. * sure that no task remains !) */
  30. /* TODO we could make this hierarchical to avoid contention ? */
  31. static pthread_cond_t submitted_cond = PTHREAD_COND_INITIALIZER;
  32. static pthread_mutex_t submitted_mutex = PTHREAD_MUTEX_INITIALIZER;
  33. static long int nsubmitted = 0, nready = 0;
  34. static void _starpu_increment_nsubmitted_tasks(void);
  35. /* This key stores the task currently handled by the thread, note that we
  36. * cannot use the worker structure to store that information because it is
  37. * possible that we have a task with a NULL codelet, which means its callback
  38. * could be executed by a user thread as well. */
  39. static pthread_key_t current_task_key;
  40. void starpu_task_init(struct starpu_task *task)
  41. {
  42. STARPU_ASSERT(task);
  43. task->cl = NULL;
  44. task->cl_arg = NULL;
  45. task->cl_arg_size = 0;
  46. task->callback_func = NULL;
  47. task->callback_arg = NULL;
  48. task->priority = STARPU_DEFAULT_PRIO;
  49. task->use_tag = 0;
  50. task->synchronous = 0;
  51. task->execute_on_a_specific_worker = 0;
  52. task->bundle = NULL;
  53. task->detach = 1;
  54. /* by default, we do not let StarPU free the task structure since
  55. * starpu_task_init is likely to be used only for statically allocated
  56. * tasks */
  57. task->destroy = 0;
  58. task->regenerate = 0;
  59. task->status = STARPU_TASK_INVALID;
  60. task->profiling_info = NULL;
  61. task->predicted = -1.0;
  62. task->predicted_transfer = -1.0;
  63. task->starpu_private = NULL;
  64. }
  65. /* Free all the ressources allocated for a task, without deallocating the task
  66. * structure itself (this is required for statically allocated tasks). */
  67. void starpu_task_deinit(struct starpu_task *task)
  68. {
  69. STARPU_ASSERT(task);
  70. /* If a buffer was allocated to store the profiling info, we free it. */
  71. if (task->profiling_info)
  72. {
  73. free(task->profiling_info);
  74. task->profiling_info = NULL;
  75. }
  76. /* If case the task is (still) part of a bundle */
  77. struct starpu_task_bundle *bundle = task->bundle;
  78. if (bundle)
  79. {
  80. _STARPU_PTHREAD_MUTEX_LOCK(&bundle->mutex);
  81. int ret = starpu_task_bundle_remove(bundle, task);
  82. /* Perhaps the bundle was destroyed when removing the last
  83. * entry */
  84. if (ret != 1)
  85. _STARPU_PTHREAD_MUTEX_UNLOCK(&bundle->mutex);
  86. }
  87. struct _starpu_job *j = (struct _starpu_job *)task->starpu_private;
  88. if (j)
  89. _starpu_job_destroy(j);
  90. }
  91. struct starpu_task * __attribute__((malloc)) starpu_task_create(void)
  92. {
  93. struct starpu_task *task;
  94. task = (struct starpu_task *) calloc(1, sizeof(struct starpu_task));
  95. STARPU_ASSERT(task);
  96. starpu_task_init(task);
  97. /* Dynamically allocated tasks are destroyed by default */
  98. task->destroy = 1;
  99. return task;
  100. }
  101. /* Free the ressource allocated during starpu_task_create. This function can be
  102. * called automatically after the execution of a task by setting the "destroy"
  103. * flag of the starpu_task structure (default behaviour). Calling this function
  104. * on a statically allocated task results in an undefined behaviour. */
  105. void starpu_task_destroy(struct starpu_task *task)
  106. {
  107. STARPU_ASSERT(task);
  108. /* If starpu_task_destroy is called in a callback, we just set the destroy
  109. flag. The task will be destroyed after the callback returns */
  110. if (task == starpu_get_current_task()
  111. && _starpu_get_local_worker_status() == STATUS_CALLBACK)
  112. {
  113. task->destroy = 1;
  114. }
  115. else
  116. {
  117. starpu_task_deinit(task);
  118. /* TODO handle the case of task with detach = 1 and destroy = 1 */
  119. /* TODO handle the case of non terminated tasks -> return -EINVAL */
  120. free(task);
  121. }
  122. }
  123. int starpu_task_wait(struct starpu_task *task)
  124. {
  125. _STARPU_LOG_IN();
  126. STARPU_ASSERT(task);
  127. if (task->detach || task->synchronous)
  128. {
  129. _STARPU_DEBUG("Task is detached or asynchronous. Waiting returns immediately\n");
  130. _STARPU_LOG_OUT_TAG("einval");
  131. return -EINVAL;
  132. }
  133. if (STARPU_UNLIKELY(!_starpu_worker_may_perform_blocking_calls()))
  134. {
  135. _STARPU_LOG_OUT_TAG("edeadlk");
  136. return -EDEADLK;
  137. }
  138. struct _starpu_job *j = (struct _starpu_job *)task->starpu_private;
  139. _starpu_wait_job(j);
  140. /* as this is a synchronous task, the liberation of the job
  141. structure was deferred */
  142. if (task->destroy)
  143. free(task);
  144. _STARPU_LOG_OUT();
  145. return 0;
  146. }
  147. struct _starpu_job *_starpu_get_job_associated_to_task(struct starpu_task *task)
  148. {
  149. STARPU_ASSERT(task);
  150. if (!task->starpu_private)
  151. {
  152. struct _starpu_job *j = _starpu_job_create(task);
  153. task->starpu_private = j;
  154. }
  155. return (struct _starpu_job *)task->starpu_private;
  156. }
  157. /* NB in case we have a regenerable task, it is possible that the job was
  158. * already counted. */
  159. int _starpu_submit_job(struct _starpu_job *j)
  160. {
  161. _STARPU_LOG_IN();
  162. /* notify bound computation of a new task */
  163. _starpu_bound_record(j);
  164. j->terminated = 0;
  165. _starpu_increment_nsubmitted_tasks();
  166. _STARPU_PTHREAD_MUTEX_LOCK(&j->sync_mutex);
  167. j->submitted = 1;
  168. int ret = _starpu_enforce_deps_and_schedule(j, 1);
  169. _STARPU_PTHREAD_MUTEX_UNLOCK(&j->sync_mutex);
  170. _STARPU_LOG_OUT();
  171. return ret;
  172. }
  173. void _starpu_codelet_check_deprecated_fields(struct starpu_codelet *cl)
  174. {
  175. if (!cl)
  176. return;
  177. /* Check deprecated and unset fields (where, <device>_func,
  178. * <device>_funcs) */
  179. /* CPU */
  180. if (cl->cpu_func && cl->cpu_func != STARPU_MULTIPLE_CPU_IMPLEMENTATIONS && cl->cpu_funcs[0])
  181. {
  182. fprintf(stderr, "[warning] [struct starpu_codelet] both cpu_func and cpu_funcs are set. Ignoring cpu_func.\n");
  183. cl->cpu_func = STARPU_MULTIPLE_CPU_IMPLEMENTATIONS;
  184. }
  185. if (cl->cpu_func && cl->cpu_func != STARPU_MULTIPLE_CPU_IMPLEMENTATIONS)
  186. {
  187. cl->cpu_funcs[0] = cl->cpu_func;
  188. cl->cpu_func = STARPU_MULTIPLE_CPU_IMPLEMENTATIONS;
  189. }
  190. if (cl->cpu_funcs[0] && cl->cpu_func == 0)
  191. {
  192. cl->cpu_func = STARPU_MULTIPLE_CPU_IMPLEMENTATIONS;
  193. }
  194. #if 0
  195. if (cl->cpu_funcs[0])
  196. {
  197. cl->where |= STARPU_CPU;
  198. }
  199. #endif
  200. /* CUDA */
  201. if (cl->cuda_func && cl->cuda_func != STARPU_MULTIPLE_CUDA_IMPLEMENTATIONS && cl->cuda_funcs[0])
  202. {
  203. fprintf(stderr, "[warning] [struct starpu_codelet] both cuda_func and cuda_funcs are set. Ignoring cuda_func.\n");
  204. cl->cuda_func = STARPU_MULTIPLE_CUDA_IMPLEMENTATIONS;
  205. }
  206. if (cl->cuda_func && cl->cuda_func != STARPU_MULTIPLE_CUDA_IMPLEMENTATIONS)
  207. {
  208. cl->cuda_funcs[0] = cl->cuda_func;
  209. cl->cuda_func = STARPU_MULTIPLE_CUDA_IMPLEMENTATIONS;
  210. }
  211. if (cl->cuda_funcs[0] && cl->cuda_func == 0)
  212. {
  213. cl->cuda_func = STARPU_MULTIPLE_CUDA_IMPLEMENTATIONS;
  214. }
  215. #if 0
  216. if (cl->cuda_funcs[0])
  217. {
  218. cl->where |= STARPU_CUDA;
  219. }
  220. #endif
  221. /* OpenCL */
  222. if (cl->opencl_func && cl->opencl_func != STARPU_MULTIPLE_OPENCL_IMPLEMENTATIONS && cl->opencl_funcs[0])
  223. {
  224. fprintf(stderr, "[warning] [struct starpu_codelet] both opencl_func and opencl_funcs are set. Ignoring opencl_func.\n");
  225. cl->opencl_func = STARPU_MULTIPLE_OPENCL_IMPLEMENTATIONS;
  226. }
  227. if (cl->opencl_func && cl->opencl_func != STARPU_MULTIPLE_OPENCL_IMPLEMENTATIONS)
  228. {
  229. cl->opencl_funcs[0] = cl->opencl_func;
  230. cl->opencl_func = STARPU_MULTIPLE_OPENCL_IMPLEMENTATIONS;
  231. }
  232. if (cl->opencl_funcs[0] && cl->opencl_func == 0)
  233. {
  234. cl->opencl_func = STARPU_MULTIPLE_OPENCL_IMPLEMENTATIONS;
  235. }
  236. #if 0
  237. if (cl->opencl_funcs[0])
  238. {
  239. cl->where |= STARPU_OPENCL;
  240. }
  241. #endif
  242. /* Gordon */
  243. if (cl->gordon_func && cl->gordon_func != STARPU_MULTIPLE_GORDON_IMPLEMENTATIONS)
  244. {
  245. cl->gordon_funcs[0] = cl->gordon_func;
  246. cl->gordon_func = STARPU_MULTIPLE_GORDON_IMPLEMENTATIONS;
  247. }
  248. if (cl->gordon_funcs[0] && cl->gordon_func == 0)
  249. {
  250. cl->gordon_func = STARPU_MULTIPLE_GORDON_IMPLEMENTATIONS;
  251. }
  252. #if 0
  253. if (cl->gordon_funcs[0])
  254. {
  255. cl->where = STARPU_GORDON;
  256. }
  257. #endif
  258. }
  259. void _starpu_task_check_deprecated_fields(struct starpu_task *task)
  260. {
  261. if (task->cl)
  262. {
  263. unsigned i;
  264. for(i=0; i<task->cl->nbuffers ; i++)
  265. {
  266. if (task->buffers[i].handle && task->handles[i])
  267. {
  268. fprintf(stderr, "[warning][struct starpu_task] task->buffers[%d] and task->handles[%d] both set. Ignoring task->buffers[%d] ?\n", i, i, i);
  269. STARPU_ASSERT(task->buffers[i].mode == task->cl->modes[i]);
  270. STARPU_ABORT();
  271. }
  272. if (task->buffers[i].handle)
  273. {
  274. task->handles[i] = task->buffers[i].handle;
  275. task->cl->modes[i] = task->buffers[i].mode;
  276. }
  277. task->buffers[i].handle = NULL;
  278. task->buffers[i].mode = 0;
  279. }
  280. }
  281. }
  282. /* application should submit new tasks to StarPU through this function */
  283. int starpu_task_submit(struct starpu_task *task)
  284. {
  285. STARPU_ASSERT(task);
  286. int ret;
  287. unsigned is_sync = task->synchronous;
  288. _STARPU_LOG_IN();
  289. if (is_sync)
  290. {
  291. /* Perhaps it is not possible to submit a synchronous
  292. * (blocking) task */
  293. if (STARPU_UNLIKELY(!_starpu_worker_may_perform_blocking_calls()))
  294. {
  295. _STARPU_LOG_OUT_TAG("EDEADLK");
  296. return -EDEADLK;
  297. }
  298. task->detach = 0;
  299. }
  300. _starpu_task_check_deprecated_fields(task);
  301. _starpu_codelet_check_deprecated_fields(task->cl);
  302. if (task->cl)
  303. {
  304. unsigned i;
  305. /* Check the type of worker(s) required by the task exist */
  306. if (!_starpu_worker_exists(task))
  307. {
  308. _STARPU_LOG_OUT_TAG("ENODEV");
  309. return -ENODEV;
  310. }
  311. /* Check buffers */
  312. STARPU_ASSERT(task->cl->nbuffers <= STARPU_NMAXBUFS);
  313. for (i = 0; i < task->cl->nbuffers; i++)
  314. {
  315. /* Make sure handles are not partitioned */
  316. STARPU_ASSERT(task->handles[i]->nchildren == 0);
  317. }
  318. /* In case we require that a task should be explicitely
  319. * executed on a specific worker, we make sure that the worker
  320. * is able to execute this task. */
  321. if (task->execute_on_a_specific_worker && !starpu_combined_worker_can_execute_task(task->workerid, task, 0))
  322. {
  323. _STARPU_LOG_OUT_TAG("ENODEV");
  324. return -ENODEV;
  325. }
  326. _starpu_detect_implicit_data_deps(task);
  327. if (task->cl->model)
  328. _starpu_load_perfmodel(task->cl->model);
  329. if (task->cl->power_model)
  330. _starpu_load_perfmodel(task->cl->power_model);
  331. }
  332. /* If profiling is activated, we allocate a structure to store the
  333. * appropriate info. */
  334. struct starpu_task_profiling_info *info;
  335. int profiling = starpu_profiling_status_get();
  336. info = _starpu_allocate_profiling_info_if_needed(task);
  337. task->profiling_info = info;
  338. /* The task is considered as block until we are sure there remains not
  339. * dependency. */
  340. task->status = STARPU_TASK_BLOCKED;
  341. if (profiling)
  342. _starpu_clock_gettime(&info->submit_time);
  343. /* internally, StarPU manipulates a struct _starpu_job * which is a wrapper around a
  344. * task structure, it is possible that this job structure was already
  345. * allocated, for instance to enforce task depenencies. */
  346. struct _starpu_job *j = _starpu_get_job_associated_to_task(task);
  347. ret = _starpu_submit_job(j);
  348. if (is_sync)
  349. _starpu_wait_job(j);
  350. _STARPU_LOG_OUT();
  351. return ret;
  352. }
  353. /* The StarPU core can submit tasks directly to the scheduler or a worker,
  354. * skipping dependencies completely (when it knows what it is doing). */
  355. int _starpu_task_submit_nodeps(struct starpu_task *task)
  356. {
  357. int ret;
  358. _starpu_task_check_deprecated_fields(task);
  359. _starpu_codelet_check_deprecated_fields(task->cl);
  360. if (task->cl)
  361. {
  362. if (task->cl->model)
  363. _starpu_load_perfmodel(task->cl->model);
  364. if (task->cl->power_model)
  365. _starpu_load_perfmodel(task->cl->power_model);
  366. }
  367. struct _starpu_job *j = _starpu_get_job_associated_to_task(task);
  368. _starpu_increment_nsubmitted_tasks();
  369. _STARPU_PTHREAD_MUTEX_LOCK(&j->sync_mutex);
  370. j->submitted = 1;
  371. if (task->cl)
  372. {
  373. /* This would be done by data dependencies checking */
  374. unsigned i;
  375. for (i=0 ; i<task->cl->nbuffers ; i++)
  376. {
  377. j->ordered_buffers[i].handle = j->task->handles[i];
  378. j->ordered_buffers[i].mode = j->task->cl->modes[i];
  379. }
  380. }
  381. ret = _starpu_push_task(j, 1);
  382. _STARPU_PTHREAD_MUTEX_UNLOCK(&j->sync_mutex);
  383. return ret;
  384. }
  385. /*
  386. * worker->sched_mutex must be locked when calling this function.
  387. */
  388. int _starpu_task_submit_conversion_task(struct starpu_task *task,
  389. unsigned int workerid)
  390. {
  391. STARPU_ASSERT(task->cl);
  392. STARPU_ASSERT(task->execute_on_a_specific_worker);
  393. _starpu_task_check_deprecated_fields(task);
  394. _starpu_codelet_check_deprecated_fields(task->cl);
  395. /* We should factorize that */
  396. if (task->cl->model)
  397. _starpu_load_perfmodel(task->cl->model);
  398. if (task->cl->power_model)
  399. _starpu_load_perfmodel(task->cl->power_model);
  400. struct _starpu_job *j = _starpu_get_job_associated_to_task(task);
  401. _starpu_increment_nsubmitted_tasks();
  402. _STARPU_PTHREAD_MUTEX_LOCK(&j->sync_mutex);
  403. j->submitted = 1;
  404. _starpu_increment_nready_tasks();
  405. unsigned i;
  406. for (i=0 ; i<task->cl->nbuffers ; i++)
  407. {
  408. j->ordered_buffers[i].handle = j->task->handles[i];
  409. j->ordered_buffers[i].mode = j->task->cl->modes[i];
  410. }
  411. _STARPU_LOG_IN();
  412. task->status = STARPU_TASK_READY;
  413. _starpu_profiling_set_task_push_start_time(task);
  414. unsigned node = starpu_worker_get_memory_node(workerid);
  415. if (starpu_get_prefetch_flag())
  416. starpu_prefetch_task_input_on_node(task, node);
  417. struct _starpu_worker *worker;
  418. worker = _starpu_get_worker_struct(workerid);
  419. starpu_task_list_push_front(&worker->local_tasks, task);
  420. _starpu_profiling_set_task_push_end_time(task);
  421. _STARPU_LOG_OUT();
  422. _STARPU_PTHREAD_MUTEX_UNLOCK(&j->sync_mutex);
  423. return 0;
  424. }
  425. void starpu_display_codelet_stats(struct starpu_codelet *cl)
  426. {
  427. unsigned worker;
  428. unsigned nworkers = starpu_worker_get_count();
  429. if (cl->name)
  430. fprintf(stderr, "Statistics for codelet %s\n", cl->name);
  431. else if (cl->model && cl->model->symbol)
  432. fprintf(stderr, "Statistics for codelet %s\n", cl->model->symbol);
  433. unsigned long total = 0;
  434. for (worker = 0; worker < nworkers; worker++)
  435. total += cl->per_worker_stats[worker];
  436. for (worker = 0; worker < nworkers; worker++)
  437. {
  438. char name[32];
  439. starpu_worker_get_name(worker, name, 32);
  440. fprintf(stderr, "\t%s -> %lu / %lu (%2.2f %%)\n", name, cl->per_worker_stats[worker], total, (100.0f*cl->per_worker_stats[worker])/total);
  441. }
  442. }
  443. /*
  444. * We wait for all the tasks that have already been submitted. Note that a
  445. * regenerable is not considered finished until it was explicitely set as
  446. * non-regenerale anymore (eg. from a callback).
  447. */
  448. int starpu_task_wait_for_all(void)
  449. {
  450. if (STARPU_UNLIKELY(!_starpu_worker_may_perform_blocking_calls()))
  451. return -EDEADLK;
  452. _STARPU_PTHREAD_MUTEX_LOCK(&submitted_mutex);
  453. _STARPU_TRACE_TASK_WAIT_FOR_ALL;
  454. while (nsubmitted > 0)
  455. _STARPU_PTHREAD_COND_WAIT(&submitted_cond, &submitted_mutex);
  456. _STARPU_PTHREAD_MUTEX_UNLOCK(&submitted_mutex);
  457. return 0;
  458. }
  459. /*
  460. * We wait until there is no ready task any more (i.e. StarPU will not be able
  461. * to progress any more).
  462. */
  463. int starpu_task_wait_for_no_ready(void)
  464. {
  465. if (STARPU_UNLIKELY(!_starpu_worker_may_perform_blocking_calls()))
  466. return -EDEADLK;
  467. _STARPU_PTHREAD_MUTEX_LOCK(&submitted_mutex);
  468. _STARPU_TRACE_TASK_WAIT_FOR_ALL;
  469. while (nready > 0)
  470. _STARPU_PTHREAD_COND_WAIT(&submitted_cond, &submitted_mutex);
  471. _STARPU_PTHREAD_MUTEX_UNLOCK(&submitted_mutex);
  472. return 0;
  473. }
  474. void _starpu_decrement_nsubmitted_tasks(void)
  475. {
  476. _STARPU_PTHREAD_MUTEX_LOCK(&submitted_mutex);
  477. if (--nsubmitted == 0)
  478. _STARPU_PTHREAD_COND_BROADCAST(&submitted_cond);
  479. _STARPU_TRACE_UPDATE_TASK_CNT(nsubmitted);
  480. _STARPU_PTHREAD_MUTEX_UNLOCK(&submitted_mutex);
  481. }
  482. static void _starpu_increment_nsubmitted_tasks(void)
  483. {
  484. _STARPU_PTHREAD_MUTEX_LOCK(&submitted_mutex);
  485. nsubmitted++;
  486. _STARPU_TRACE_UPDATE_TASK_CNT(nsubmitted);
  487. _STARPU_PTHREAD_MUTEX_UNLOCK(&submitted_mutex);
  488. }
  489. void _starpu_increment_nready_tasks(void)
  490. {
  491. _STARPU_PTHREAD_MUTEX_LOCK(&submitted_mutex);
  492. nready++;
  493. _STARPU_PTHREAD_MUTEX_UNLOCK(&submitted_mutex);
  494. }
  495. void _starpu_decrement_nready_tasks(void)
  496. {
  497. _STARPU_PTHREAD_MUTEX_LOCK(&submitted_mutex);
  498. if (--nready == 0)
  499. _STARPU_PTHREAD_COND_BROADCAST(&submitted_cond);
  500. _STARPU_PTHREAD_MUTEX_UNLOCK(&submitted_mutex);
  501. }
  502. void _starpu_initialize_current_task_key(void)
  503. {
  504. pthread_key_create(&current_task_key, NULL);
  505. }
  506. /* Return the task currently executed by the worker, or NULL if this is called
  507. * either from a thread that is not a task or simply because there is no task
  508. * being executed at the moment. */
  509. struct starpu_task *starpu_get_current_task(void)
  510. {
  511. return (struct starpu_task *) pthread_getspecific(current_task_key);
  512. }
  513. void _starpu_set_current_task(struct starpu_task *task)
  514. {
  515. pthread_setspecific(current_task_key, task);
  516. }
  517. /*
  518. * Returns 0 if tasks does not use any multiformat handle, 1 otherwise.
  519. */
  520. int
  521. _starpu_task_uses_multiformat_handles(struct starpu_task *task)
  522. {
  523. unsigned i;
  524. for (i = 0; i < task->cl->nbuffers; i++)
  525. {
  526. unsigned int id;
  527. id = starpu_get_handle_interface_id(task->handles[i]);
  528. if (id == STARPU_MULTIFORMAT_INTERFACE_ID)
  529. return 1;
  530. }
  531. return 0;
  532. }
  533. /*
  534. * Checks whether the given handle needs to be converted in order to be used on
  535. * the node given as the second argument.
  536. */
  537. int
  538. _starpu_handle_needs_conversion_task(starpu_data_handle_t handle,
  539. unsigned int node)
  540. {
  541. enum _starpu_node_kind node_kind;
  542. node_kind = _starpu_get_node_kind(node);
  543. /*
  544. * Here, we assume that CUDA devices and OpenCL devices use the
  545. * same data structure. A conversion is only needed when moving
  546. * data from a CPU to a GPU, or the other way around.
  547. */
  548. switch (node_kind)
  549. {
  550. case STARPU_CPU_RAM:
  551. switch(_starpu_get_node_kind(handle->mf_node))
  552. {
  553. case STARPU_CPU_RAM:
  554. return 0;
  555. case STARPU_CUDA_RAM: /* Fall through */
  556. case STARPU_OPENCL_RAM:
  557. return 1;
  558. case STARPU_SPU_LS: /* Not supported */
  559. default:
  560. STARPU_ASSERT(0);
  561. }
  562. break;
  563. case STARPU_CUDA_RAM: /* Fall through */
  564. case STARPU_OPENCL_RAM:
  565. switch(_starpu_get_node_kind(handle->mf_node))
  566. {
  567. case STARPU_CPU_RAM:
  568. return 1;
  569. case STARPU_CUDA_RAM:
  570. case STARPU_OPENCL_RAM:
  571. return 0;
  572. case STARPU_SPU_LS: /* Not supported */
  573. default:
  574. STARPU_ASSERT(0);
  575. }
  576. break;
  577. case STARPU_SPU_LS: /* Not supported */
  578. default:
  579. STARPU_ASSERT(0);
  580. }
  581. }
  582. starpu_cpu_func_t _starpu_task_get_cpu_nth_implementation(struct starpu_codelet *cl, unsigned nimpl)
  583. {
  584. STARPU_ASSERT(cl->cpu_func == STARPU_MULTIPLE_CPU_IMPLEMENTATIONS);
  585. return cl->cpu_funcs[nimpl];
  586. }
  587. starpu_cuda_func_t _starpu_task_get_cuda_nth_implementation(struct starpu_codelet *cl, unsigned nimpl)
  588. {
  589. STARPU_ASSERT(cl->cuda_func == STARPU_MULTIPLE_CUDA_IMPLEMENTATIONS);
  590. return cl->cuda_funcs[nimpl];
  591. }
  592. starpu_opencl_func_t _starpu_task_get_opencl_nth_implementation(struct starpu_codelet *cl, unsigned nimpl)
  593. {
  594. STARPU_ASSERT(cl->opencl_func == STARPU_MULTIPLE_OPENCL_IMPLEMENTATIONS);
  595. return cl->opencl_funcs[nimpl];
  596. }
  597. starpu_gordon_func_t _starpu_task_get_gordon_nth_implementation(struct starpu_codelet *cl, unsigned nimpl)
  598. {
  599. STARPU_ASSERT(cl->gordon_func == STARPU_MULTIPLE_GORDON_IMPLEMENTATIONS);
  600. return cl->gordon_funcs[nimpl];
  601. }