driver_cpu.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2017 Université de Bordeaux
  4. * Copyright (C) 2010 Mehdi Juhoor <mjuhoor@gmail.com>
  5. * Copyright (C) 2010-2017 CNRS
  6. * Copyright (C) 2011 Télécom-SudParis
  7. * Copyright (C) 2014, 2017 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 <core/drivers.h>
  29. #include <drivers/cpu/driver_cpu.h>
  30. #include <core/sched_policy.h>
  31. #include <datawizard/memory_manager.h>
  32. #include <datawizard/malloc.h>
  33. #include <core/simgrid.h>
  34. #include <core/task.h>
  35. #ifdef STARPU_HAVE_HWLOC
  36. #include <hwloc.h>
  37. #ifndef HWLOC_API_VERSION
  38. #define HWLOC_OBJ_PU HWLOC_OBJ_PROC
  39. #endif
  40. #endif
  41. #ifdef STARPU_HAVE_WINDOWS
  42. #include <windows.h>
  43. #endif
  44. /* Actually launch the job on a cpu worker.
  45. * Handle binding CPUs on cores.
  46. * In the case of a combined worker WORKER_TASK != J->TASK */
  47. 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)
  48. {
  49. int is_parallel_task = (j->task_size > 1);
  50. int profiling = starpu_profiling_status_get();
  51. /* start/end timestamp are only conditionnally measured in
  52. * _starpu_driver_start_job/_end_job, thus make sure that they are
  53. * always initialized */
  54. struct timespec codelet_start = {0,0};
  55. struct timespec codelet_end = {0,0};
  56. struct starpu_task *task = j->task;
  57. struct starpu_codelet *cl = task->cl;
  58. STARPU_ASSERT(cl);
  59. if (is_parallel_task)
  60. {
  61. STARPU_PTHREAD_BARRIER_WAIT(&j->before_work_barrier);
  62. /* In the case of a combined worker, the scheduler needs to know
  63. * when each actual worker begins the execution */
  64. _starpu_sched_pre_exec_hook(worker_task);
  65. }
  66. /* Give profiling variable */
  67. _starpu_driver_start_job(cpu_args, j, perf_arch, &codelet_start, rank, profiling);
  68. /* In case this is a Fork-join parallel task, the worker does not
  69. * execute the kernel at all. */
  70. if ((rank == 0) || (cl->type != STARPU_FORKJOIN))
  71. {
  72. _starpu_cl_func_t func = _starpu_task_get_cpu_nth_implementation(cl, j->nimpl);
  73. if (is_parallel_task && cl->type == STARPU_FORKJOIN)
  74. /* bind to parallel worker */
  75. _starpu_bind_thread_on_cpus(cpu_args->config, _starpu_get_combined_worker_struct(j->combined_workerid));
  76. STARPU_ASSERT_MSG(func, "when STARPU_CPU is defined in 'where', cpu_func or cpu_funcs has to be defined");
  77. if (_starpu_get_disable_kernels() <= 0)
  78. {
  79. _STARPU_TRACE_START_EXECUTING();
  80. #ifdef STARPU_SIMGRID
  81. if (cl->flags & STARPU_CODELET_SIMGRID_EXECUTE)
  82. func(_STARPU_TASK_GET_INTERFACES(task), task->cl_arg);
  83. else if (cl->flags & STARPU_CODELET_SIMGRID_EXECUTE_AND_INJECT)
  84. {
  85. _SIMGRID_TIMER_BEGIN(1);
  86. func(_STARPU_TASK_GET_INTERFACES(task), task->cl_arg);
  87. _SIMGRID_TIMER_END;
  88. }
  89. else
  90. _starpu_simgrid_submit_job(cpu_args->workerid, j, perf_arch, NAN, NULL);
  91. #else
  92. func(_STARPU_TASK_GET_INTERFACES(task), task->cl_arg);
  93. #endif
  94. _STARPU_TRACE_END_EXECUTING();
  95. }
  96. if (is_parallel_task && cl->type == STARPU_FORKJOIN)
  97. /* rebind to single CPU */
  98. _starpu_bind_thread_on_cpu(cpu_args->config, cpu_args->bindid, cpu_args->workerid);
  99. }
  100. _starpu_driver_end_job(cpu_args, j, perf_arch, &codelet_end, rank, profiling);
  101. if (is_parallel_task)
  102. {
  103. STARPU_PTHREAD_BARRIER_WAIT(&j->after_work_barrier);
  104. #ifdef STARPU_SIMGRID
  105. if (rank == 0)
  106. {
  107. /* Wait for other threads to exit barrier_wait so we
  108. * can safely drop the job structure */
  109. MSG_process_sleep(0.0000001);
  110. j->after_work_busy_barrier = 0;
  111. }
  112. #else
  113. ANNOTATE_HAPPENS_BEFORE(&j->after_work_busy_barrier);
  114. (void) STARPU_ATOMIC_ADD(&j->after_work_busy_barrier, -1);
  115. if (rank == 0)
  116. {
  117. /* Wait with a busy barrier for other workers to have
  118. * finished with the blocking barrier before we can
  119. * safely drop the job structure */
  120. while (j->after_work_busy_barrier > 0)
  121. {
  122. STARPU_UYIELD();
  123. STARPU_SYNCHRONIZE();
  124. }
  125. ANNOTATE_HAPPENS_AFTER(&j->after_work_busy_barrier);
  126. }
  127. #endif
  128. }
  129. if (rank == 0)
  130. {
  131. _starpu_driver_update_job_feedback(j, cpu_args,
  132. perf_arch, &codelet_start, &codelet_end, profiling);
  133. #ifdef STARPU_OPENMP
  134. if (!j->continuation)
  135. #endif
  136. {
  137. _starpu_push_task_output(j);
  138. }
  139. }
  140. return 0;
  141. }
  142. static size_t _starpu_cpu_get_global_mem_size(int nodeid STARPU_ATTRIBUTE_UNUSED, struct _starpu_machine_config *config STARPU_ATTRIBUTE_UNUSED)
  143. {
  144. size_t global_mem;
  145. starpu_ssize_t limit = -1;
  146. char name[32];
  147. #if defined(STARPU_HAVE_HWLOC)
  148. struct _starpu_machine_topology *topology = &config->topology;
  149. int nnumas = starpu_get_nb_numa_nodes();
  150. if (nnumas > 1)
  151. {
  152. int depth_node = hwloc_get_type_depth(topology->hwtopology, HWLOC_OBJ_NODE);
  153. if (depth_node == HWLOC_TYPE_DEPTH_UNKNOWN)
  154. global_mem = hwloc_get_root_obj(topology->hwtopology)->memory.total_memory;
  155. else {
  156. hwloc_obj_t obj = hwloc_get_obj_by_depth(topology->hwtopology, depth_node, nodeid);
  157. global_mem = obj->memory.local_memory;
  158. sprintf(name, "STARPU_LIMIT_CPU_NUMA_%d_MEM", obj->os_index);
  159. limit = starpu_get_env_number(name);
  160. }
  161. }
  162. else
  163. {
  164. /* Do not limit ourself to a single NUMA node */
  165. global_mem = hwloc_get_root_obj(topology->hwtopology)->memory.total_memory;
  166. }
  167. #else /* STARPU_HAVE_HWLOC */
  168. #ifdef STARPU_DEVEL
  169. # warning TODO: use sysinfo when available to get global size
  170. #endif
  171. global_mem = 0;
  172. #endif
  173. if (limit == -1)
  174. limit = starpu_get_env_number("STARPU_LIMIT_CPU_MEM");
  175. if (limit < 0)
  176. // No limit is defined, we return the global memory size
  177. return global_mem;
  178. else if (global_mem && (size_t)limit * 1024*1024 > global_mem)
  179. // The requested limit is higher than what is available, we return the global memory size
  180. return global_mem;
  181. else
  182. // We limit the memory
  183. return limit*1024*1024;
  184. }
  185. int _starpu_cpu_driver_init(struct _starpu_worker *cpu_worker)
  186. {
  187. int devid = cpu_worker->devid;
  188. _starpu_driver_start(cpu_worker, _STARPU_FUT_CPU_KEY, 1);
  189. _starpu_memory_manager_set_global_memory_size(cpu_worker->memory_node, _starpu_cpu_get_global_mem_size(cpu_worker->numa_memory_node, cpu_worker->config));
  190. snprintf(cpu_worker->name, sizeof(cpu_worker->name), "CPU %d", devid);
  191. snprintf(cpu_worker->short_name, sizeof(cpu_worker->short_name), "CPU %d", devid);
  192. starpu_pthread_setname(cpu_worker->short_name);
  193. _STARPU_TRACE_WORKER_INIT_END(cpu_worker->workerid);
  194. STARPU_PTHREAD_MUTEX_LOCK_SCHED(&cpu_worker->sched_mutex);
  195. cpu_worker->status = STATUS_UNKNOWN;
  196. STARPU_PTHREAD_MUTEX_UNLOCK_SCHED(&cpu_worker->sched_mutex);
  197. /* tell the main thread that we are ready */
  198. STARPU_PTHREAD_MUTEX_LOCK(&cpu_worker->mutex);
  199. cpu_worker->worker_is_initialized = 1;
  200. STARPU_PTHREAD_COND_SIGNAL(&cpu_worker->ready_cond);
  201. STARPU_PTHREAD_MUTEX_UNLOCK(&cpu_worker->mutex);
  202. return 0;
  203. }
  204. static int _starpu_cpu_driver_execute_task(struct _starpu_worker *cpu_worker, struct starpu_task *task, struct _starpu_job *j)
  205. {
  206. int res;
  207. int rank;
  208. int is_parallel_task = (j->task_size > 1);
  209. struct starpu_perfmodel_arch* perf_arch;
  210. rank = cpu_worker->current_rank;
  211. /* Get the rank in case it is a parallel task */
  212. if (is_parallel_task)
  213. {
  214. if(j->combined_workerid != -1)
  215. {
  216. struct _starpu_combined_worker *combined_worker;
  217. combined_worker = _starpu_get_combined_worker_struct(j->combined_workerid);
  218. cpu_worker->combined_workerid = j->combined_workerid;
  219. cpu_worker->worker_size = combined_worker->worker_size;
  220. perf_arch = &combined_worker->perf_arch;
  221. }
  222. else
  223. {
  224. struct _starpu_sched_ctx *sched_ctx = _starpu_sched_ctx_get_sched_ctx_for_worker_and_job(cpu_worker, j);
  225. STARPU_ASSERT_MSG(sched_ctx != NULL, "there should be a worker %d in the ctx of this job \n", cpu_worker->workerid);
  226. perf_arch = &sched_ctx->perf_arch;
  227. }
  228. }
  229. else
  230. {
  231. cpu_worker->combined_workerid = cpu_worker->workerid;
  232. cpu_worker->worker_size = 1;
  233. struct _starpu_sched_ctx *sched_ctx = _starpu_sched_ctx_get_sched_ctx_for_worker_and_job(cpu_worker, j);
  234. if (sched_ctx && !sched_ctx->sched_policy && !sched_ctx->awake_workers && sched_ctx->main_master == cpu_worker->workerid)
  235. perf_arch = &sched_ctx->perf_arch;
  236. else
  237. perf_arch = &cpu_worker->perf_arch;
  238. }
  239. _starpu_set_current_task(j->task);
  240. cpu_worker->current_task = j->task;
  241. res = execute_job_on_cpu(j, task, cpu_worker, rank, perf_arch);
  242. _starpu_set_current_task(NULL);
  243. cpu_worker->current_task = NULL;
  244. if (res)
  245. {
  246. switch (res)
  247. {
  248. case -EAGAIN:
  249. _starpu_push_task_to_workers(task);
  250. return 0;
  251. default:
  252. STARPU_ABORT();
  253. }
  254. }
  255. /* In the case of combined workers, we need to inform the
  256. * scheduler each worker's execution is over.
  257. * Then we free the workers' task alias */
  258. if (is_parallel_task)
  259. {
  260. _starpu_sched_post_exec_hook(task);
  261. free(task);
  262. }
  263. if (rank == 0)
  264. _starpu_handle_job_termination(j);
  265. return 0;
  266. }
  267. int _starpu_cpu_driver_run_once(struct _starpu_worker *cpu_worker)
  268. {
  269. unsigned memnode = cpu_worker->memory_node;
  270. int workerid = cpu_worker->workerid;
  271. int res;
  272. struct _starpu_job *j;
  273. struct starpu_task *task = NULL, *pending_task;
  274. int rank = 0;
  275. #ifdef STARPU_SIMGRID
  276. starpu_pthread_wait_reset(&cpu_worker->wait);
  277. #endif
  278. /* Test if async transfers are completed */
  279. pending_task = cpu_worker->task_transferring;
  280. if (pending_task != NULL && cpu_worker->nb_buffers_transferred == cpu_worker->nb_buffers_totransfer)
  281. {
  282. int ret;
  283. _STARPU_TRACE_END_PROGRESS(memnode);
  284. j = _starpu_get_job_associated_to_task(pending_task);
  285. _starpu_fetch_task_input_tail(pending_task, j, cpu_worker);
  286. /* Reset it */
  287. cpu_worker->task_transferring = NULL;
  288. ret = _starpu_cpu_driver_execute_task(cpu_worker, pending_task, j);
  289. _STARPU_TRACE_START_PROGRESS(memnode);
  290. return ret;
  291. }
  292. res = __starpu_datawizard_progress(1, 1);
  293. if (!pending_task)
  294. task = _starpu_get_worker_task(cpu_worker, workerid, memnode);
  295. #ifdef STARPU_SIMGRID
  296. if (!res && !task)
  297. /* No progress, wait */
  298. starpu_pthread_wait_wait(&cpu_worker->wait);
  299. #endif
  300. if (!task)
  301. /* No task or task still pending transfers */
  302. return 0;
  303. j = _starpu_get_job_associated_to_task(task);
  304. /* NOTE: j->task is != task for parallel tasks, which share the same
  305. * job. */
  306. /* can a cpu perform that task ? */
  307. if (!_STARPU_CPU_MAY_PERFORM(j))
  308. {
  309. /* put it and the end of the queue ... XXX */
  310. _starpu_push_task_to_workers(task);
  311. return 0;
  312. }
  313. _STARPU_TRACE_END_PROGRESS(memnode);
  314. /* Get the rank in case it is a parallel task */
  315. if (j->task_size > 1)
  316. {
  317. STARPU_PTHREAD_MUTEX_LOCK(&j->sync_mutex);
  318. rank = j->active_task_alias_count++;
  319. STARPU_PTHREAD_MUTEX_UNLOCK(&j->sync_mutex);
  320. }
  321. else
  322. {
  323. rank = 0;
  324. }
  325. cpu_worker->current_rank = rank;
  326. #ifdef STARPU_OPENMP
  327. /* At this point, j->continuation as been cleared as the task is being
  328. * woken up, thus we use j->discontinuous instead for the check */
  329. const unsigned continuation_wake_up = j->discontinuous;
  330. #else
  331. const unsigned continuation_wake_up = 0;
  332. #endif
  333. if (rank == 0 && !continuation_wake_up)
  334. {
  335. res = _starpu_fetch_task_input(task, j, 1);
  336. STARPU_ASSERT(res == 0);
  337. }
  338. else
  339. {
  340. int ret = _starpu_cpu_driver_execute_task(cpu_worker, task, j);
  341. _STARPU_TRACE_END_PROGRESS(memnode);
  342. return ret;
  343. }
  344. _STARPU_TRACE_END_PROGRESS(memnode);
  345. return 0;
  346. }
  347. int _starpu_cpu_driver_deinit(struct _starpu_worker *cpu_worker)
  348. {
  349. _STARPU_TRACE_WORKER_DEINIT_START;
  350. unsigned memnode = cpu_worker->memory_node;
  351. _starpu_handle_all_pending_node_data_requests(memnode);
  352. /* In case there remains some memory that was automatically
  353. * allocated by StarPU, we release it now. Note that data
  354. * coherency is not maintained anymore at that point ! */
  355. _starpu_free_all_automatically_allocated_buffers(memnode);
  356. cpu_worker->worker_is_initialized = 0;
  357. _STARPU_TRACE_WORKER_DEINIT_END(_STARPU_FUT_CPU_KEY);
  358. return 0;
  359. }
  360. void *_starpu_cpu_worker(void *arg)
  361. {
  362. struct _starpu_worker *worker = arg;
  363. _starpu_cpu_driver_init(worker);
  364. _STARPU_TRACE_START_PROGRESS(worker->memory_node);
  365. while (_starpu_machine_is_running())
  366. {
  367. _starpu_may_pause();
  368. _starpu_cpu_driver_run_once(worker);
  369. }
  370. _STARPU_TRACE_END_PROGRESS(worker->memory_node);
  371. _starpu_cpu_driver_deinit(worker);
  372. return NULL;
  373. }
  374. int _starpu_cpu_driver_run(struct _starpu_worker *worker)
  375. {
  376. worker->set = NULL;
  377. worker->worker_is_initialized = 0;
  378. _starpu_cpu_worker(worker);
  379. return 0;
  380. }
  381. struct _starpu_driver_ops _starpu_driver_cpu_ops =
  382. {
  383. .init = _starpu_cpu_driver_init,
  384. .run = _starpu_cpu_driver_run,
  385. .run_once = _starpu_cpu_driver_run_once,
  386. .deinit = _starpu_cpu_driver_deinit
  387. };