workers.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2009 (see AUTHORS file)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <common/config.h>
  19. #include <core/workers.h>
  20. #include <core/debug.h>
  21. static pthread_key_t worker_key;
  22. static struct machine_config_s config;
  23. struct machine_config_s *_starpu_get_machine_config(void)
  24. {
  25. return &config;
  26. }
  27. /* in case a task is submitted, we may check whether there exists a worker
  28. that may execute the task or not */
  29. inline uint32_t _starpu_worker_exists(uint32_t task_mask)
  30. {
  31. return (task_mask & config.worker_mask);
  32. }
  33. inline uint32_t may_submit_cuda_task(void)
  34. {
  35. return (STARPU_CUDA & config.worker_mask);
  36. }
  37. inline uint32_t may_submit_cpu_task(void)
  38. {
  39. return (STARPU_CPU & config.worker_mask);
  40. }
  41. inline uint32_t _starpu_worker_may_execute_task(unsigned workerid, uint32_t where)
  42. {
  43. return (where & config.workers[workerid].worker_mask);
  44. }
  45. /*
  46. * Runtime initialization methods
  47. */
  48. #ifdef USE_GORDON
  49. static unsigned gordon_inited = 0;
  50. static struct worker_set_s gordon_worker_set;
  51. #endif
  52. static void _starpu_init_worker_queue(struct worker_s *workerarg)
  53. {
  54. struct jobq_s *jobq = workerarg->jobq;
  55. /* warning : in case there are multiple workers on the same
  56. queue, we overwrite this value so that it is meaningless */
  57. jobq->arch = workerarg->perf_arch;
  58. jobq->who |= workerarg->worker_mask;
  59. switch (workerarg->arch) {
  60. case STARPU_CPU_WORKER:
  61. jobq->alpha = STARPU_CPU_ALPHA;
  62. break;
  63. case STARPU_CUDA_WORKER:
  64. jobq->alpha = STARPU_CUDA_ALPHA;
  65. break;
  66. case STARPU_GORDON_WORKER:
  67. jobq->alpha = STARPU_GORDON_ALPHA;
  68. break;
  69. default:
  70. STARPU_ABORT();
  71. }
  72. memory_node_attach_queue(jobq, workerarg->memory_node);
  73. }
  74. static void _starpu_init_workers(struct machine_config_s *config)
  75. {
  76. config->running = 1;
  77. pthread_key_create(&worker_key, NULL);
  78. /* Launch workers asynchronously (except for SPUs) */
  79. unsigned worker;
  80. for (worker = 0; worker < config->nworkers; worker++)
  81. {
  82. struct worker_s *workerarg = &config->workers[worker];
  83. workerarg->config = config;
  84. pthread_mutex_init(&workerarg->mutex, NULL);
  85. pthread_cond_init(&workerarg->ready_cond, NULL);
  86. workerarg->workerid = (int)worker;
  87. /* if some codelet's termination cannot be handled directly :
  88. * for instance in the Gordon driver, Gordon tasks' callbacks
  89. * may be executed by another thread than that of the Gordon
  90. * driver so that we cannot call the push_codelet_output method
  91. * directly */
  92. workerarg->terminated_jobs = job_list_new();
  93. workerarg->local_jobs = job_list_new();
  94. pthread_mutex_init(&workerarg->local_jobs_mutex, NULL);
  95. workerarg->status = STATUS_INITIALIZING;
  96. _starpu_init_worker_queue(workerarg);
  97. switch (workerarg->arch) {
  98. #ifdef USE_CPUS
  99. case STARPU_CPU_WORKER:
  100. workerarg->set = NULL;
  101. workerarg->worker_is_initialized = 0;
  102. pthread_create(&workerarg->worker_thread,
  103. NULL, _starpu_cpu_worker, workerarg);
  104. break;
  105. #endif
  106. #ifdef USE_CUDA
  107. case STARPU_CUDA_WORKER:
  108. workerarg->set = NULL;
  109. workerarg->worker_is_initialized = 0;
  110. pthread_create(&workerarg->worker_thread,
  111. NULL, _starpu_cuda_worker, workerarg);
  112. break;
  113. #endif
  114. #ifdef USE_GORDON
  115. case STARPU_GORDON_WORKER:
  116. /* we will only launch gordon once, but it will handle
  117. * the different SPU workers */
  118. if (!gordon_inited)
  119. {
  120. gordon_worker_set.nworkers = config->ngordon_spus;
  121. gordon_worker_set.workers = &config->workers[worker];
  122. gordon_worker_set.set_is_initialized = 0;
  123. pthread_create(&gordon_worker_set.worker_thread, NULL,
  124. gordon_worker, &gordon_worker_set);
  125. pthread_mutex_lock(&gordon_worker_set.mutex);
  126. if (!gordon_worker_set.set_is_initialized)
  127. pthread_cond_wait(&gordon_worker_set.ready_cond,
  128. &gordon_worker_set.mutex);
  129. pthread_mutex_unlock(&gordon_worker_set.mutex);
  130. gordon_inited = 1;
  131. }
  132. workerarg->set = &gordon_worker_set;
  133. gordon_worker_set.joined = 0;
  134. workerarg->worker_is_running = 1;
  135. break;
  136. #endif
  137. default:
  138. STARPU_ABORT();
  139. }
  140. }
  141. for (worker = 0; worker < config->nworkers; worker++)
  142. {
  143. struct worker_s *workerarg = &config->workers[worker];
  144. switch (workerarg->arch) {
  145. case STARPU_CPU_WORKER:
  146. case STARPU_CUDA_WORKER:
  147. pthread_mutex_lock(&workerarg->mutex);
  148. if (!workerarg->worker_is_initialized)
  149. pthread_cond_wait(&workerarg->ready_cond, &workerarg->mutex);
  150. pthread_mutex_unlock(&workerarg->mutex);
  151. break;
  152. #ifdef USE_GORDON
  153. case STARPU_GORDON_WORKER:
  154. /* the initialization of Gordon worker is
  155. * synchronous for now */
  156. break;
  157. #endif
  158. default:
  159. STARPU_ABORT();
  160. }
  161. }
  162. }
  163. void _starpu_set_local_worker_key(struct worker_s *worker)
  164. {
  165. pthread_setspecific(worker_key, worker);
  166. }
  167. struct worker_s *_starpu_get_local_worker_key(void)
  168. {
  169. return pthread_getspecific(worker_key);
  170. }
  171. int starpu_init(struct starpu_conf *user_conf)
  172. {
  173. int ret;
  174. srand(2008);
  175. #ifdef USE_FXT
  176. start_fxt_profiling();
  177. #endif
  178. _starpu_open_debug_logfile();
  179. timing_init();
  180. load_bus_performance_files();
  181. /* store the pointer to the user explicit configuration during the
  182. * initialization */
  183. config.user_conf = user_conf;
  184. ret = starpu_build_topology(&config);
  185. if (ret)
  186. return ret;
  187. /* initialize the scheduler */
  188. /* initialize the queue containing the jobs */
  189. init_sched_policy(&config);
  190. _starpu_init_workers(&config);
  191. return 0;
  192. }
  193. /*
  194. * Handle runtime termination
  195. */
  196. static void _starpu_terminate_workers(struct machine_config_s *config)
  197. {
  198. int status;
  199. unsigned workerid;
  200. for (workerid = 0; workerid < config->nworkers; workerid++)
  201. {
  202. starpu_wake_all_blocked_workers();
  203. #ifdef VERBOSE
  204. fprintf(stderr, "wait for worker %d\n", workerid);
  205. #endif
  206. struct worker_set_s *set = config->workers[workerid].set;
  207. struct worker_s *worker = &config->workers[workerid];
  208. /* in case StarPU termination code is called from a callback,
  209. * we have to check if pthread_self() is the worker itself */
  210. if (set){
  211. if (!set->joined) {
  212. if (pthread_self() != set->worker_thread)
  213. {
  214. status = pthread_join(set->worker_thread, NULL);
  215. #ifdef VERBOSE
  216. if (status)
  217. fprintf(stderr, "pthread_join -> %d\n", status);
  218. #endif
  219. }
  220. set->joined = 1;
  221. }
  222. }
  223. else {
  224. if (pthread_self() != worker->worker_thread)
  225. {
  226. status = pthread_join(worker->worker_thread, NULL);
  227. #ifdef VERBOSE
  228. if (status)
  229. fprintf(stderr, "pthread_join -> %d\n", status);
  230. #endif
  231. }
  232. }
  233. job_list_delete(worker->local_jobs);
  234. job_list_delete(worker->terminated_jobs);
  235. }
  236. }
  237. unsigned _starpu_machine_is_running(void)
  238. {
  239. return config.running;
  240. }
  241. unsigned _starpu_worker_can_block(unsigned memnode)
  242. {
  243. unsigned can_block = 1;
  244. if (!check_that_no_data_request_exists(memnode))
  245. can_block = 0;
  246. if (!_starpu_machine_is_running())
  247. can_block = 0;
  248. if (!_starpu_execute_registered_progression_hooks())
  249. can_block = 0;
  250. return can_block;
  251. }
  252. typedef enum {
  253. BROADCAST,
  254. LOCK,
  255. UNLOCK
  256. } queue_op;
  257. static void _starpu_operate_on_all_queues_attached_to_node(unsigned nodeid, queue_op op)
  258. {
  259. unsigned q_id;
  260. struct jobq_s *q;
  261. mem_node_descr * const descr = get_memory_node_description();
  262. pthread_rwlock_rdlock(&descr->attached_queues_rwlock);
  263. unsigned nqueues = descr->queues_count[nodeid];
  264. for (q_id = 0; q_id < nqueues; q_id++)
  265. {
  266. q = descr->attached_queues_per_node[nodeid][q_id];
  267. switch (op) {
  268. case BROADCAST:
  269. pthread_cond_broadcast(&q->activity_cond);
  270. break;
  271. case LOCK:
  272. pthread_mutex_lock(&q->activity_mutex);
  273. break;
  274. case UNLOCK:
  275. pthread_mutex_unlock(&q->activity_mutex);
  276. break;
  277. }
  278. }
  279. pthread_rwlock_unlock(&descr->attached_queues_rwlock);
  280. }
  281. inline void _starpu_lock_all_queues_attached_to_node(unsigned node)
  282. {
  283. _starpu_operate_on_all_queues_attached_to_node(node, LOCK);
  284. }
  285. inline void _starpu_unlock_all_queues_attached_to_node(unsigned node)
  286. {
  287. _starpu_operate_on_all_queues_attached_to_node(node, UNLOCK);
  288. }
  289. inline void _starpu_broadcast_all_queues_attached_to_node(unsigned node)
  290. {
  291. _starpu_operate_on_all_queues_attached_to_node(node, BROADCAST);
  292. }
  293. static void _starpu_operate_on_all_queues(queue_op op)
  294. {
  295. unsigned q_id;
  296. struct jobq_s *q;
  297. mem_node_descr * const descr = get_memory_node_description();
  298. pthread_rwlock_rdlock(&descr->attached_queues_rwlock);
  299. unsigned nqueues = descr->total_queues_count;
  300. for (q_id = 0; q_id < nqueues; q_id++)
  301. {
  302. q = descr->attached_queues_all[q_id];
  303. switch (op) {
  304. case BROADCAST:
  305. pthread_cond_broadcast(&q->activity_cond);
  306. break;
  307. case LOCK:
  308. pthread_mutex_lock(&q->activity_mutex);
  309. break;
  310. case UNLOCK:
  311. pthread_mutex_unlock(&q->activity_mutex);
  312. break;
  313. }
  314. }
  315. pthread_rwlock_unlock(&descr->attached_queues_rwlock);
  316. }
  317. static void _starpu_kill_all_workers(struct machine_config_s *config)
  318. {
  319. /* lock all workers and the scheduler (in the proper order) to make
  320. sure everyone will notice the termination */
  321. /* WARNING: here we make the asumption that a queue is not attached to
  322. * different memory nodes ! */
  323. struct sched_policy_s *sched = get_sched_policy();
  324. _starpu_operate_on_all_queues(LOCK);
  325. pthread_mutex_lock(&sched->sched_activity_mutex);
  326. /* set the flag which will tell workers to stop */
  327. config->running = 0;
  328. _starpu_operate_on_all_queues(BROADCAST);
  329. pthread_cond_broadcast(&sched->sched_activity_cond);
  330. pthread_mutex_unlock(&sched->sched_activity_mutex);
  331. _starpu_operate_on_all_queues(UNLOCK);
  332. }
  333. void starpu_shutdown(void)
  334. {
  335. display_msi_stats();
  336. display_alloc_cache_stats();
  337. /* tell all workers to shutdown */
  338. _starpu_kill_all_workers(&config);
  339. #ifdef DATA_STATS
  340. display_comm_ammounts();
  341. #endif
  342. if (starpu_get_env_number("CALIBRATE") != -1)
  343. dump_registered_models();
  344. /* wait for their termination */
  345. _starpu_terminate_workers(&config);
  346. deinit_sched_policy(&config);
  347. starpu_destroy_topology(&config);
  348. #ifdef USE_FXT
  349. stop_fxt_profiling();
  350. #endif
  351. _starpu_close_debug_logfile();
  352. }
  353. unsigned starpu_get_worker_count(void)
  354. {
  355. return config.nworkers;
  356. }
  357. unsigned starpu_get_cpu_worker_count(void)
  358. {
  359. return config.ncpus;
  360. }
  361. unsigned starpu_get_cuda_worker_count(void)
  362. {
  363. return config.ncudagpus;
  364. }
  365. unsigned starpu_get_spu_worker_count(void)
  366. {
  367. return config.ngordon_spus;
  368. }
  369. /* When analyzing performance, it is useful to see what is the processing unit
  370. * that actually performed the task. This function returns the id of the
  371. * processing unit actually executing it, therefore it makes no sense to use it
  372. * within the callbacks of SPU functions for instance. If called by some thread
  373. * that is not controlled by StarPU, starpu_get_worker_id returns -1. */
  374. int starpu_get_worker_id(void)
  375. {
  376. struct worker_s * worker;
  377. worker = _starpu_get_local_worker_key();
  378. if (worker)
  379. {
  380. return worker->workerid;
  381. }
  382. else {
  383. /* there is no worker associated to that thread, perhaps it is
  384. * a thread from the application or this is some SPU worker */
  385. return -1;
  386. }
  387. }
  388. struct worker_s *_starpu_get_worker_struct(unsigned id)
  389. {
  390. return &config.workers[id];
  391. }
  392. enum starpu_archtype starpu_get_worker_type(int id)
  393. {
  394. return config.workers[id].arch;
  395. }
  396. void starpu_get_worker_name(int id, char *dst, size_t maxlen)
  397. {
  398. char *name = config.workers[id].name;
  399. snprintf(dst, maxlen, "%s", name);
  400. }