driver_cpu.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2012 Université de Bordeaux 1
  4. * Copyright (C) 2010 Mehdi Juhoor <mjuhoor@gmail.com>
  5. * Copyright (C) 2010, 2011 Centre National de la Recherche Scientifique
  6. * Copyright (C) 2011 Télécom-SudParis
  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 <math.h>
  20. #include <starpu.h>
  21. #include <starpu_profiling.h>
  22. #include <drivers/driver_common/driver_common.h>
  23. #include <common/utils.h>
  24. #include <core/debug.h>
  25. #include "driver_cpu.h"
  26. #include <core/sched_policy.h>
  27. /* Actually launch the job on a cpu worker.
  28. * Handle binding CPUs on cores.
  29. * In the case of a combined worker WORKER_TASK != J->TASK */
  30. static int execute_job_on_cpu(struct _starpu_job *j, struct starpu_task *worker_task, struct _starpu_worker *cpu_args, int rank, enum starpu_perf_archtype perf_arch)
  31. {
  32. int ret;
  33. int is_parallel_task = (j->task_size > 1);
  34. int profiling = starpu_profiling_status_get();
  35. struct timespec codelet_start, codelet_end;
  36. struct starpu_task *task = j->task;
  37. struct starpu_codelet *cl = task->cl;
  38. STARPU_ASSERT(cl);
  39. if (rank == 0)
  40. {
  41. ret = _starpu_fetch_task_input(j, 0);
  42. if (ret != 0)
  43. {
  44. /* there was not enough memory so the codelet cannot be executed right now ... */
  45. /* push the codelet back and try another one ... */
  46. return -EAGAIN;
  47. }
  48. }
  49. if (is_parallel_task)
  50. {
  51. _STARPU_PTHREAD_BARRIER_WAIT(&j->before_work_barrier);
  52. /* In the case of a combined worker, the scheduler needs to know
  53. * when each actual worker begins the execution */
  54. _starpu_sched_pre_exec_hook(worker_task);
  55. }
  56. /* Give profiling variable */
  57. _starpu_driver_start_job(cpu_args, j, &codelet_start, rank, profiling);
  58. /* In case this is a Fork-join parallel task, the worker does not
  59. * execute the kernel at all. */
  60. if ((rank == 0) || (cl->type != STARPU_FORKJOIN))
  61. {
  62. _starpu_cl_func_t func = _starpu_task_get_cpu_nth_implementation(cl, j->nimpl);
  63. if (is_parallel_task && cl->type == STARPU_FORKJOIN)
  64. /* bind to parallel worker */
  65. _starpu_bind_thread_on_cpus(cpu_args->config, _starpu_get_combined_worker_struct(j->combined_workerid));
  66. STARPU_ASSERT(func);
  67. func(task->interfaces, task->cl_arg);
  68. if (is_parallel_task && cl->type == STARPU_FORKJOIN)
  69. /* rebind to single CPU */
  70. _starpu_bind_thread_on_cpu(cpu_args->config, cpu_args->bindid);
  71. }
  72. _starpu_driver_end_job(cpu_args, j, perf_arch, &codelet_end, rank, profiling);
  73. if (is_parallel_task)
  74. _STARPU_PTHREAD_BARRIER_WAIT(&j->after_work_barrier);
  75. if (rank == 0)
  76. {
  77. _starpu_driver_update_job_feedback(j, cpu_args,
  78. perf_arch, &codelet_start, &codelet_end, profiling);
  79. _starpu_push_task_output(j, 0);
  80. }
  81. return 0;
  82. }
  83. static struct _starpu_worker*
  84. _starpu_get_worker_from_driver(struct starpu_driver *d)
  85. {
  86. int workers[d->id.cpu_id + 1];
  87. int nworkers;
  88. nworkers = starpu_worker_get_ids_by_type(STARPU_CPU_WORKER, workers, d->id.cpu_id+1);
  89. if (nworkers >= 0 && (unsigned) nworkers < d->id.cpu_id)
  90. return NULL; // No device was found.
  91. return _starpu_get_worker_struct(workers[d->id.cpu_id]);
  92. }
  93. int _starpu_cpu_driver_init(struct starpu_driver *d)
  94. {
  95. struct _starpu_worker *cpu_worker;
  96. cpu_worker = _starpu_get_worker_from_driver(d);
  97. STARPU_ASSERT(cpu_worker);
  98. int devid = cpu_worker->devid;
  99. _starpu_worker_init(cpu_worker, _STARPU_FUT_CPU_KEY);
  100. snprintf(cpu_worker->name, sizeof(cpu_worker->name), "CPU %d", devid);
  101. snprintf(cpu_worker->short_name, sizeof(cpu_worker->short_name), "CPU %d", devid);
  102. cpu_worker->status = STATUS_UNKNOWN;
  103. _STARPU_TRACE_WORKER_INIT_END
  104. /* tell the main thread that we are ready */
  105. _STARPU_PTHREAD_MUTEX_LOCK(&cpu_worker->mutex);
  106. cpu_worker->worker_is_initialized = 1;
  107. _STARPU_PTHREAD_COND_SIGNAL(&cpu_worker->ready_cond);
  108. _STARPU_PTHREAD_MUTEX_UNLOCK(&cpu_worker->mutex);
  109. return 0;
  110. }
  111. int _starpu_cpu_driver_run_once(struct starpu_driver *d)
  112. {
  113. struct _starpu_worker *cpu_worker;
  114. cpu_worker = _starpu_get_worker_from_driver(d);
  115. STARPU_ASSERT(cpu_worker);
  116. unsigned memnode = cpu_worker->memory_node;
  117. int workerid = cpu_worker->workerid;
  118. _STARPU_TRACE_START_PROGRESS(memnode);
  119. _starpu_datawizard_progress(memnode, 1);
  120. _STARPU_TRACE_END_PROGRESS(memnode);
  121. struct _starpu_job *j;
  122. struct starpu_task *task;
  123. int res;
  124. task = _starpu_get_worker_task(cpu_worker, workerid, memnode);
  125. if (!task)
  126. return 0;
  127. j = _starpu_get_job_associated_to_task(task);
  128. /* can a cpu perform that task ? */
  129. if (!_STARPU_CPU_MAY_PERFORM(j))
  130. {
  131. /* put it and the end of the queue ... XXX */
  132. _starpu_push_task(j);
  133. return 0;
  134. }
  135. int rank = 0;
  136. int is_parallel_task = (j->task_size > 1);
  137. enum starpu_perf_archtype perf_arch;
  138. /* Get the rank in case it is a parallel task */
  139. if (is_parallel_task)
  140. {
  141. _STARPU_PTHREAD_MUTEX_LOCK(&j->sync_mutex);
  142. rank = j->active_task_alias_count++;
  143. _STARPU_PTHREAD_MUTEX_UNLOCK(&j->sync_mutex);
  144. struct _starpu_combined_worker *combined_worker;
  145. combined_worker = _starpu_get_combined_worker_struct(j->combined_workerid);
  146. cpu_worker->combined_workerid = j->combined_workerid;
  147. cpu_worker->worker_size = combined_worker->worker_size;
  148. cpu_worker->current_rank = rank;
  149. perf_arch = combined_worker->perf_arch;
  150. }
  151. else
  152. {
  153. cpu_worker->combined_workerid = cpu_worker->workerid;
  154. cpu_worker->worker_size = 1;
  155. cpu_worker->current_rank = 0;
  156. perf_arch = cpu_worker->perf_arch;
  157. }
  158. _starpu_set_current_task(j->task);
  159. cpu_worker->current_task = j->task;
  160. res = execute_job_on_cpu(j, task, cpu_worker, rank, perf_arch);
  161. _starpu_set_current_task(NULL);
  162. cpu_worker->current_task = NULL;
  163. if (res)
  164. {
  165. switch (res)
  166. {
  167. case -EAGAIN:
  168. _starpu_push_task(j);
  169. return 0;
  170. default:
  171. STARPU_ABORT();
  172. }
  173. }
  174. /* In the case of combined workers, we need to inform the
  175. * scheduler each worker's execution is over.
  176. * Then we free the workers' task alias */
  177. if (is_parallel_task)
  178. {
  179. _starpu_sched_post_exec_hook(task);
  180. free(task);
  181. }
  182. if (rank == 0)
  183. _starpu_handle_job_termination(j);
  184. return 0;
  185. }
  186. int _starpu_cpu_driver_deinit(struct starpu_driver *d)
  187. {
  188. _STARPU_TRACE_WORKER_DEINIT_START
  189. struct _starpu_worker *cpu_worker;
  190. cpu_worker = _starpu_get_worker_from_driver(d);
  191. STARPU_ASSERT(cpu_worker);
  192. unsigned memnode = cpu_worker->memory_node;
  193. _starpu_handle_all_pending_node_data_requests(memnode);
  194. /* In case there remains some memory that was automatically
  195. * allocated by StarPU, we release it now. Note that data
  196. * coherency is not maintained anymore at that point ! */
  197. _starpu_free_all_automatically_allocated_buffers(memnode);
  198. _STARPU_TRACE_WORKER_DEINIT_END(_STARPU_FUT_CPU_KEY);
  199. return 0;
  200. }
  201. void *
  202. _starpu_cpu_worker(void *arg)
  203. {
  204. struct _starpu_worker *args = arg;
  205. struct starpu_driver d =
  206. {
  207. .type = STARPU_CPU_WORKER,
  208. .id.cpu_id = args->devid
  209. };
  210. _starpu_cpu_driver_init(&d);
  211. while (_starpu_machine_is_running())
  212. _starpu_cpu_driver_run_once(&d);
  213. _starpu_cpu_driver_deinit(&d);
  214. return NULL;
  215. }
  216. int _starpu_run_cpu(struct starpu_driver *d)
  217. {
  218. STARPU_ASSERT(d && d->type == STARPU_CPU_WORKER);
  219. struct _starpu_worker *worker = _starpu_get_worker_from_driver(d);
  220. STARPU_ASSERT(worker);
  221. worker->set = NULL;
  222. worker->worker_is_initialized = 0;
  223. _starpu_cpu_worker(worker);
  224. return 0;
  225. }