driver_cpu.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2014 Université de Bordeaux 1
  4. * Copyright (C) 2010 Mehdi Juhoor <mjuhoor@gmail.com>
  5. * Copyright (C) 2010-2014 Centre National de la Recherche Scientifique
  6. * Copyright (C) 2011 Télécom-SudParis
  7. * Copyright (C) 2014 Inria
  8. *
  9. * StarPU is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation; either version 2.1 of the License, or (at
  12. * your option) any later version.
  13. *
  14. * StarPU is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  17. *
  18. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  19. */
  20. #include <common/config.h>
  21. #include <math.h>
  22. #include <starpu.h>
  23. #include <starpu_profiling.h>
  24. #include <drivers/driver_common/driver_common.h>
  25. #include <common/utils.h>
  26. #include <core/debug.h>
  27. #include <core/workers.h>
  28. #include "driver_cpu.h"
  29. #include <core/sched_policy.h>
  30. #include <datawizard/memory_manager.h>
  31. #include <datawizard/malloc.h>
  32. #include <core/simgrid.h>
  33. #ifdef STARPU_HAVE_HWLOC
  34. #include <hwloc.h>
  35. #ifndef HWLOC_API_VERSION
  36. #define HWLOC_OBJ_PU HWLOC_OBJ_PROC
  37. #endif
  38. #endif
  39. #ifdef STARPU_HAVE_WINDOWS
  40. #include <windows.h>
  41. #endif
  42. /* Actually launch the job on a cpu worker.
  43. * Handle binding CPUs on cores.
  44. * In the case of a combined worker WORKER_TASK != J->TASK */
  45. static int execute_job_on_cpu(struct _starpu_job *j, struct starpu_task *worker_task, struct _starpu_worker *cpu_args, int rank, struct starpu_perfmodel_arch* perf_arch)
  46. {
  47. int ret;
  48. int is_parallel_task = (j->task_size > 1);
  49. int profiling = starpu_profiling_status_get();
  50. struct timespec codelet_start, codelet_end;
  51. struct starpu_task *task = j->task;
  52. struct starpu_codelet *cl = task->cl;
  53. #ifdef STARPU_OPENMP
  54. /* At this point, j->continuation as been cleared as the task is being
  55. * woken up, thus we use j->discontinuous instead for the check */
  56. const unsigned continuation_wake_up = j->discontinuous;
  57. #else
  58. const unsigned continuation_wake_up = 0;
  59. #endif
  60. STARPU_ASSERT(cl);
  61. if (rank == 0 && !continuation_wake_up)
  62. {
  63. ret = _starpu_fetch_task_input(j);
  64. if (ret != 0)
  65. {
  66. /* there was not enough memory so the codelet cannot be executed right now ... */
  67. /* push the codelet back and try another one ... */
  68. return -EAGAIN;
  69. }
  70. }
  71. if (is_parallel_task)
  72. {
  73. STARPU_PTHREAD_BARRIER_WAIT(&j->before_work_barrier);
  74. /* In the case of a combined worker, the scheduler needs to know
  75. * when each actual worker begins the execution */
  76. _starpu_sched_pre_exec_hook(worker_task);
  77. }
  78. /* Give profiling variable */
  79. _starpu_driver_start_job(cpu_args, j, perf_arch, &codelet_start, rank, profiling);
  80. /* In case this is a Fork-join parallel task, the worker does not
  81. * execute the kernel at all. */
  82. if ((rank == 0) || (cl->type != STARPU_FORKJOIN))
  83. {
  84. _starpu_cl_func_t func = _starpu_task_get_cpu_nth_implementation(cl, j->nimpl);
  85. if (is_parallel_task && cl->type == STARPU_FORKJOIN)
  86. /* bind to parallel worker */
  87. _starpu_bind_thread_on_cpus(cpu_args->config, _starpu_get_combined_worker_struct(j->combined_workerid));
  88. STARPU_ASSERT_MSG(func, "when STARPU_CPU is defined in 'where', cpu_func or cpu_funcs has to be defined");
  89. if (starpu_get_env_number("STARPU_DISABLE_KERNELS") <= 0)
  90. {
  91. _STARPU_TRACE_START_EXECUTING();
  92. #ifdef STARPU_SIMGRID
  93. _starpu_simgrid_execute_job(j, perf_arch, NAN);
  94. #else
  95. func(_STARPU_TASK_GET_INTERFACES(task), task->cl_arg);
  96. #endif
  97. _STARPU_TRACE_END_EXECUTING();
  98. }
  99. if (is_parallel_task && cl->type == STARPU_FORKJOIN)
  100. /* rebind to single CPU */
  101. _starpu_bind_thread_on_cpu(cpu_args->config, cpu_args->bindid);
  102. }
  103. _starpu_driver_end_job(cpu_args, j, perf_arch, &codelet_end, rank, profiling);
  104. if (is_parallel_task)
  105. STARPU_PTHREAD_BARRIER_WAIT(&j->after_work_barrier);
  106. if (rank == 0)
  107. {
  108. _starpu_driver_update_job_feedback(j, cpu_args,
  109. perf_arch, &codelet_start, &codelet_end, profiling);
  110. #ifdef STARPU_OPENMP
  111. if (!j->continuation)
  112. #endif
  113. {
  114. _starpu_push_task_output(j);
  115. }
  116. }
  117. return 0;
  118. }
  119. static size_t _starpu_cpu_get_global_mem_size(int nodeid STARPU_ATTRIBUTE_UNUSED, struct _starpu_machine_config *config)
  120. {
  121. size_t global_mem;
  122. starpu_ssize_t limit;
  123. limit = starpu_get_env_number("STARPU_LIMIT_CPU_MEM");
  124. #ifdef STARPU_DEVEL
  125. # warning TODO: take into account NUMA node and check STARPU_LIMIT_CPU_numanode_MEM
  126. #endif
  127. #if defined(STARPU_HAVE_HWLOC)
  128. struct _starpu_machine_topology *topology = &config->topology;
  129. #if 0
  130. /* Do not limit ourself to a single NUMA node yet, as we don't have real NUMA support for now */
  131. int depth_node = hwloc_get_type_depth(topology->hwtopology, HWLOC_OBJ_NODE);
  132. if (depth_node == HWLOC_TYPE_DEPTH_UNKNOWN)
  133. global_mem = hwloc_get_root_obj(topology->hwtopology)->memory.total_memory;
  134. else
  135. global_mem = hwloc_get_obj_by_depth(topology->hwtopology, depth_node, nodeid)->memory.local_memory;
  136. #else
  137. global_mem = hwloc_get_root_obj(topology->hwtopology)->memory.total_memory;
  138. #endif
  139. #else /* STARPU_HAVE_HWLOC */
  140. #ifdef STARPU_DEVEL
  141. # warning use sysinfo when available to get global size
  142. #endif
  143. global_mem = 0;
  144. #endif
  145. if (limit < 0)
  146. // No limit is defined, we return the global memory size
  147. return global_mem;
  148. else if ((size_t)limit * 1024*1024 > global_mem)
  149. // The requested limit is higher than what is available, we return the global memory size
  150. return global_mem;
  151. else
  152. // We limit the memory
  153. return limit*1024*1024;
  154. }
  155. int _starpu_cpu_driver_init(struct _starpu_worker *cpu_worker)
  156. {
  157. int devid = cpu_worker->devid;
  158. _starpu_worker_start(cpu_worker, _STARPU_FUT_CPU_KEY);
  159. /* FIXME: when we have NUMA support, properly turn node number into NUMA node number */
  160. _starpu_memory_manager_set_global_memory_size(cpu_worker->memory_node, _starpu_cpu_get_global_mem_size(cpu_worker->memory_node, cpu_worker->config));
  161. snprintf(cpu_worker->name, sizeof(cpu_worker->name), "CPU %d", devid);
  162. snprintf(cpu_worker->short_name, sizeof(cpu_worker->short_name), "CPU %d", devid);
  163. _STARPU_TRACE_WORKER_INIT_END(cpu_worker->workerid);
  164. /* tell the main thread that we are ready */
  165. STARPU_PTHREAD_MUTEX_LOCK(&cpu_worker->mutex);
  166. cpu_worker->status = STATUS_UNKNOWN;
  167. cpu_worker->worker_is_initialized = 1;
  168. STARPU_PTHREAD_COND_SIGNAL(&cpu_worker->ready_cond);
  169. STARPU_PTHREAD_MUTEX_UNLOCK(&cpu_worker->mutex);
  170. return 0;
  171. }
  172. int _starpu_cpu_driver_run_once(struct _starpu_worker *cpu_worker)
  173. {
  174. unsigned memnode = cpu_worker->memory_node;
  175. int workerid = cpu_worker->workerid;
  176. _STARPU_TRACE_START_PROGRESS(memnode);
  177. _starpu_datawizard_progress(memnode, 1);
  178. _STARPU_TRACE_END_PROGRESS(memnode);
  179. struct _starpu_job *j;
  180. struct starpu_task *task;
  181. int res;
  182. task = _starpu_get_worker_task(cpu_worker, workerid, memnode);
  183. if (!task)
  184. return 0;
  185. j = _starpu_get_job_associated_to_task(task);
  186. /* can a cpu perform that task ? */
  187. if (!_STARPU_CPU_MAY_PERFORM(j))
  188. {
  189. /* put it and the end of the queue ... XXX */
  190. _starpu_push_task_to_workers(task);
  191. return 0;
  192. }
  193. int rank = 0;
  194. int is_parallel_task = (j->task_size > 1);
  195. struct starpu_perfmodel_arch* perf_arch;
  196. /* Get the rank in case it is a parallel task */
  197. if (is_parallel_task)
  198. {
  199. STARPU_PTHREAD_MUTEX_LOCK(&j->sync_mutex);
  200. rank = j->active_task_alias_count++;
  201. STARPU_PTHREAD_MUTEX_UNLOCK(&j->sync_mutex);
  202. struct _starpu_combined_worker *combined_worker;
  203. combined_worker = _starpu_get_combined_worker_struct(j->combined_workerid);
  204. cpu_worker->combined_workerid = j->combined_workerid;
  205. cpu_worker->worker_size = combined_worker->worker_size;
  206. cpu_worker->current_rank = rank;
  207. perf_arch = &combined_worker->perf_arch;
  208. }
  209. else
  210. {
  211. cpu_worker->combined_workerid = cpu_worker->workerid;
  212. cpu_worker->worker_size = 1;
  213. cpu_worker->current_rank = 0;
  214. perf_arch = &cpu_worker->perf_arch;
  215. }
  216. _starpu_set_current_task(j->task);
  217. cpu_worker->current_task = j->task;
  218. res = execute_job_on_cpu(j, task, cpu_worker, rank, perf_arch);
  219. _starpu_set_current_task(NULL);
  220. cpu_worker->current_task = NULL;
  221. if (res)
  222. {
  223. switch (res)
  224. {
  225. case -EAGAIN:
  226. _starpu_push_task_to_workers(task);
  227. return 0;
  228. default:
  229. STARPU_ABORT();
  230. }
  231. }
  232. /* In the case of combined workers, we need to inform the
  233. * scheduler each worker's execution is over.
  234. * Then we free the workers' task alias */
  235. if (is_parallel_task)
  236. {
  237. _starpu_sched_post_exec_hook(task);
  238. free(task);
  239. }
  240. if (rank == 0)
  241. _starpu_handle_job_termination(j);
  242. return 0;
  243. }
  244. int _starpu_cpu_driver_deinit(struct _starpu_worker *cpu_worker)
  245. {
  246. _STARPU_TRACE_WORKER_DEINIT_START;
  247. unsigned memnode = cpu_worker->memory_node;
  248. _starpu_handle_all_pending_node_data_requests(memnode);
  249. /* In case there remains some memory that was automatically
  250. * allocated by StarPU, we release it now. Note that data
  251. * coherency is not maintained anymore at that point ! */
  252. _starpu_free_all_automatically_allocated_buffers(memnode);
  253. _STARPU_TRACE_WORKER_DEINIT_END(_STARPU_FUT_CPU_KEY);
  254. return 0;
  255. }
  256. void *
  257. _starpu_cpu_worker(void *arg)
  258. {
  259. struct _starpu_worker *args = arg;
  260. _starpu_cpu_driver_init(args);
  261. while (_starpu_machine_is_running())
  262. {
  263. _starpu_may_pause();
  264. _starpu_cpu_driver_run_once(args);
  265. }
  266. _starpu_cpu_driver_deinit(args);
  267. return NULL;
  268. }
  269. int _starpu_run_cpu(struct _starpu_worker *worker)
  270. {
  271. worker->set = NULL;
  272. worker->worker_is_initialized = 0;
  273. _starpu_cpu_worker(worker);
  274. return 0;
  275. }