workers.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010, 2011 Université de Bordeaux 1
  4. * Copyright (C) 2010 Centre National de la Recherche Scientifique
  5. * Copyright (C) 2010 Institut National de Recherche en Informatique et Automatique
  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 <stdlib.h>
  19. #include <stdio.h>
  20. #include <common/config.h>
  21. #include <common/utils.h>
  22. #include <core/workers.h>
  23. #include <core/debug.h>
  24. #include <core/task.h>
  25. #include <profiling/profiling.h>
  26. #include <starpu_task_list.h>
  27. #ifdef __MINGW32__
  28. #include <windows.h>
  29. #endif
  30. /* acquire/release semantic for concurrent initialization/de-initialization */
  31. static pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER;
  32. static pthread_cond_t init_cond = PTHREAD_COND_INITIALIZER;
  33. static int init_count;
  34. static enum { UNINITIALIZED, CHANGING, INITIALIZED } initialized = UNINITIALIZED;
  35. static pthread_key_t worker_key;
  36. static struct starpu_machine_config_s config;
  37. static struct starpu_sched_ctx sched_ctx;
  38. struct starpu_machine_config_s *_starpu_get_machine_config(void)
  39. {
  40. return &config;
  41. }
  42. /* in case a task is submitted, we may check whether there exists a worker
  43. that may execute the task or not */
  44. inline uint32_t _starpu_worker_exists(uint32_t task_mask)
  45. {
  46. return (task_mask & config.worker_mask);
  47. }
  48. inline uint32_t _starpu_may_submit_cuda_task(void)
  49. {
  50. return (STARPU_CUDA & config.worker_mask);
  51. }
  52. inline uint32_t _starpu_may_submit_cpu_task(void)
  53. {
  54. return (STARPU_CPU & config.worker_mask);
  55. }
  56. inline uint32_t _starpu_may_submit_opencl_task(void)
  57. {
  58. return (STARPU_OPENCL & config.worker_mask);
  59. }
  60. int starpu_worker_may_execute_task(unsigned workerid, struct starpu_task *task)
  61. {
  62. /* TODO: check that the task operand sizes will fit on that device */
  63. /* TODO: call application-provided function for various cases like
  64. * double support, shared memory size limit, etc. */
  65. return !!(task->cl->where & config.workers[workerid].worker_mask);
  66. }
  67. int starpu_combined_worker_may_execute_task(unsigned workerid, struct starpu_task *task)
  68. {
  69. /* TODO: check that the task operand sizes will fit on that device */
  70. /* TODO: call application-provided function for various cases like
  71. * double support, shared memory size limit, etc. */
  72. struct starpu_codelet_t *cl = task->cl;
  73. unsigned nworkers = config.topology.nworkers;
  74. /* Is this a parallel worker ? */
  75. if (workerid < nworkers)
  76. {
  77. return !!(task->cl->where & config.workers[workerid].worker_mask);
  78. }
  79. else {
  80. if ((cl->type == STARPU_SPMD) || (cl->type == STARPU_FORKJOIN))
  81. {
  82. /* TODO we should add other types of constraints */
  83. /* Is the worker larger than requested ? */
  84. int worker_size = (int)config.combined_workers[workerid - nworkers].worker_size;
  85. return !!(worker_size <= task->cl->max_parallelism);
  86. }
  87. else
  88. {
  89. /* We have a sequential task but a parallel worker */
  90. return 0;
  91. }
  92. }
  93. }
  94. /*
  95. * Runtime initialization methods
  96. */
  97. #ifdef STARPU_USE_GORDON
  98. static unsigned gordon_inited = 0;
  99. static struct starpu_worker_set_s gordon_worker_set;
  100. #endif
  101. static void _starpu_init_worker_queue(struct starpu_worker_s *workerarg)
  102. {
  103. pthread_cond_t *cond = workerarg->sched_cond;
  104. pthread_mutex_t *mutex = workerarg->sched_mutex;
  105. unsigned memory_node = workerarg->memory_node;
  106. _starpu_memory_node_register_condition(cond, mutex, memory_node);
  107. }
  108. static void _starpu_launch_drivers(struct starpu_machine_config_s *config)
  109. {
  110. config->running = 1;
  111. pthread_key_create(&worker_key, NULL);
  112. unsigned nworkers = config->topology.nworkers;
  113. /* Launch workers asynchronously (except for SPUs) */
  114. unsigned worker;
  115. for (worker = 0; worker < nworkers; worker++)
  116. {
  117. struct starpu_worker_s *workerarg = &config->workers[worker];
  118. workerarg->config = config;
  119. PTHREAD_MUTEX_INIT(&workerarg->changing_ctx_mutex, NULL);
  120. PTHREAD_COND_INIT(&workerarg->changing_ctx_cond, NULL);
  121. PTHREAD_MUTEX_INIT(&workerarg->mutex, NULL);
  122. PTHREAD_COND_INIT(&workerarg->ready_cond, NULL);
  123. workerarg->worker_size = 1;
  124. workerarg->combined_workerid = workerarg->workerid;
  125. workerarg->current_rank = 1;
  126. /* if some codelet's termination cannot be handled directly :
  127. * for instance in the Gordon driver, Gordon tasks' callbacks
  128. * may be executed by another thread than that of the Gordon
  129. * driver so that we cannot call the push_codelet_output method
  130. * directly */
  131. workerarg->terminated_jobs = starpu_job_list_new();
  132. starpu_task_list_init(&workerarg->local_tasks);
  133. workerarg->status = STATUS_INITIALIZING;
  134. _STARPU_DEBUG("initialising worker %d\n", worker);
  135. _starpu_init_worker_queue(workerarg);
  136. switch (workerarg->arch) {
  137. #ifdef STARPU_USE_CPU
  138. case STARPU_CPU_WORKER:
  139. workerarg->set = NULL;
  140. workerarg->worker_is_initialized = 0;
  141. pthread_create(&workerarg->worker_thread,
  142. NULL, _starpu_cpu_worker, workerarg);
  143. break;
  144. #endif
  145. #ifdef STARPU_USE_CUDA
  146. case STARPU_CUDA_WORKER:
  147. workerarg->set = NULL;
  148. workerarg->worker_is_initialized = 0;
  149. pthread_create(&workerarg->worker_thread,
  150. NULL, _starpu_cuda_worker, workerarg);
  151. break;
  152. #endif
  153. #ifdef STARPU_USE_OPENCL
  154. case STARPU_OPENCL_WORKER:
  155. workerarg->set = NULL;
  156. workerarg->worker_is_initialized = 0;
  157. pthread_create(&workerarg->worker_thread,
  158. NULL, _starpu_opencl_worker, workerarg);
  159. break;
  160. #endif
  161. #ifdef STARPU_USE_GORDON
  162. case STARPU_GORDON_WORKER:
  163. /* we will only launch gordon once, but it will handle
  164. * the different SPU workers */
  165. if (!gordon_inited)
  166. {
  167. gordon_worker_set.nworkers = config->ngordon_spus;
  168. gordon_worker_set.workers = &config->workers[worker];
  169. gordon_worker_set.set_is_initialized = 0;
  170. pthread_create(&gordon_worker_set.worker_thread, NULL,
  171. _starpu_gordon_worker, &gordon_worker_set);
  172. PTHREAD_MUTEX_LOCK(&gordon_worker_set.mutex);
  173. while (!gordon_worker_set.set_is_initialized)
  174. PTHREAD_COND_WAIT(&gordon_worker_set.ready_cond,
  175. &gordon_worker_set.mutex);
  176. PTHREAD_MUTEX_UNLOCK(&gordon_worker_set.mutex);
  177. gordon_inited = 1;
  178. }
  179. workerarg->set = &gordon_worker_set;
  180. gordon_worker_set.joined = 0;
  181. workerarg->worker_is_running = 1;
  182. break;
  183. #endif
  184. default:
  185. STARPU_ABORT();
  186. }
  187. }
  188. for (worker = 0; worker < nworkers; worker++)
  189. {
  190. struct starpu_worker_s *workerarg = &config->workers[worker];
  191. switch (workerarg->arch) {
  192. case STARPU_CPU_WORKER:
  193. case STARPU_CUDA_WORKER:
  194. case STARPU_OPENCL_WORKER:
  195. PTHREAD_MUTEX_LOCK(&workerarg->mutex);
  196. while (!workerarg->worker_is_initialized)
  197. PTHREAD_COND_WAIT(&workerarg->ready_cond, &workerarg->mutex);
  198. PTHREAD_MUTEX_UNLOCK(&workerarg->mutex);
  199. break;
  200. #ifdef STARPU_USE_GORDON
  201. case STARPU_GORDON_WORKER:
  202. /* the initialization of Gordon worker is
  203. * synchronous for now */
  204. break;
  205. #endif
  206. default:
  207. STARPU_ABORT();
  208. }
  209. }
  210. }
  211. void _starpu_set_local_worker_key(struct starpu_worker_s *worker)
  212. {
  213. pthread_setspecific(worker_key, worker);
  214. }
  215. struct starpu_worker_s *_starpu_get_local_worker_key(void)
  216. {
  217. return pthread_getspecific(worker_key);
  218. }
  219. /* Initialize the starpu_conf with default values */
  220. int starpu_conf_init(struct starpu_conf *conf)
  221. {
  222. if (!conf)
  223. return -EINVAL;
  224. conf->sched_policy_name = getenv("STARPU_SCHED");
  225. conf->sched_policy = NULL;
  226. /* Note that starpu_get_env_number returns -1 in case the variable is
  227. * not defined */
  228. conf->ncpus = starpu_get_env_number("STARPU_NCPUS");
  229. conf->ncuda = starpu_get_env_number("STARPU_NCUDA");
  230. conf->nopencl = starpu_get_env_number("STARPU_NOPENCL");
  231. conf->nspus = starpu_get_env_number("STARPU_NGORDON");
  232. conf->calibrate = starpu_get_env_number("STARPU_CALIBRATE");
  233. conf->use_explicit_workers_bindid = 0; /* TODO */
  234. conf->use_explicit_workers_cuda_gpuid = 0; /* TODO */
  235. conf->use_explicit_workers_opencl_gpuid = 0; /* TODO */
  236. return 0;
  237. };
  238. int starpu_init(struct starpu_conf *user_conf)
  239. {
  240. int ret;
  241. PTHREAD_MUTEX_LOCK(&init_mutex);
  242. while (initialized == CHANGING)
  243. /* Wait for the other one changing it */
  244. PTHREAD_COND_WAIT(&init_cond, &init_mutex);
  245. init_count++;
  246. if (initialized == INITIALIZED) {
  247. /* He initialized it, don't do it again, and let the others get the mutex */
  248. PTHREAD_MUTEX_UNLOCK(&init_mutex);
  249. return 0;
  250. }
  251. /* initialized == UNINITIALIZED */
  252. initialized = CHANGING;
  253. PTHREAD_MUTEX_UNLOCK(&init_mutex);
  254. #ifdef __MINGW32__
  255. WSADATA wsadata;
  256. WSAStartup(MAKEWORD(1,0), &wsadata);
  257. #endif
  258. srand(2008);
  259. #ifdef STARPU_USE_FXT
  260. _starpu_start_fxt_profiling();
  261. #endif
  262. _starpu_open_debug_logfile();
  263. _starpu_timing_init();
  264. _starpu_profiling_init();
  265. _starpu_load_bus_performance_files();
  266. /* store the pointer to the user explicit configuration during the
  267. * initialization */
  268. config.user_conf = user_conf;
  269. ret = _starpu_build_topology(&config);
  270. if (ret) {
  271. PTHREAD_MUTEX_LOCK(&init_mutex);
  272. init_count--;
  273. initialized = UNINITIALIZED;
  274. /* Let somebody else try to do it */
  275. PTHREAD_COND_SIGNAL(&init_cond);
  276. PTHREAD_MUTEX_UNLOCK(&init_mutex);
  277. return ret;
  278. }
  279. /* We need to store the current task handled by the different
  280. * threads */
  281. _starpu_initialize_current_task_key();
  282. /* initialize the scheduling policy */
  283. if(user_conf == NULL)
  284. _starpu_create_sched_ctx(&sched_ctx, NULL, NULL, -1, 1);
  285. else
  286. _starpu_create_sched_ctx(&sched_ctx, user_conf->sched_policy_name, NULL, -1, 1);
  287. //_starpu_init_sched_policy(&config, &sched_ctx);
  288. _starpu_initialize_registered_performance_models();
  289. /* Launch "basic" workers (ie. non-combined workers) */
  290. _starpu_launch_drivers(&config);
  291. PTHREAD_MUTEX_LOCK(&init_mutex);
  292. initialized = INITIALIZED;
  293. /* Tell everybody that we initialized */
  294. PTHREAD_COND_BROADCAST(&init_cond);
  295. PTHREAD_MUTEX_UNLOCK(&init_mutex);
  296. return 0;
  297. }
  298. /*
  299. * Handle runtime termination
  300. */
  301. static void _starpu_terminate_workers(struct starpu_machine_config_s *config)
  302. {
  303. int status __attribute__((unused));
  304. unsigned workerid;
  305. for (workerid = 0; workerid < config->topology.nworkers; workerid++)
  306. {
  307. starpu_wake_all_blocked_workers();
  308. _STARPU_DEBUG("wait for worker %u\n", workerid);
  309. struct starpu_worker_set_s *set = config->workers[workerid].set;
  310. struct starpu_worker_s *worker = &config->workers[workerid];
  311. /* in case StarPU termination code is called from a callback,
  312. * we have to check if pthread_self() is the worker itself */
  313. if (set){
  314. if (!set->joined) {
  315. if (!pthread_equal(pthread_self(), set->worker_thread))
  316. {
  317. status = pthread_join(set->worker_thread, NULL);
  318. #ifdef STARPU_VERBOSE
  319. if (status) {
  320. _STARPU_DEBUG("pthread_join -> %d\n", status);
  321. }
  322. #endif
  323. }
  324. set->joined = 1;
  325. }
  326. }
  327. else {
  328. if (!pthread_equal(pthread_self(), worker->worker_thread))
  329. {
  330. status = pthread_join(worker->worker_thread, NULL);
  331. #ifdef STARPU_VERBOSE
  332. if (status) {
  333. _STARPU_DEBUG("pthread_join -> %d\n", status);
  334. }
  335. #endif
  336. }
  337. }
  338. worker->status = STATUS_JOINED;
  339. STARPU_ASSERT(starpu_task_list_empty(&worker->local_tasks));
  340. starpu_job_list_delete(worker->terminated_jobs);
  341. }
  342. }
  343. unsigned _starpu_machine_is_running(void)
  344. {
  345. return config.running;
  346. }
  347. unsigned _starpu_worker_can_block(unsigned memnode __attribute__((unused)))
  348. {
  349. #ifdef STARPU_NON_BLOCKING_DRIVERS
  350. return 0;
  351. #else
  352. unsigned can_block = 1;
  353. if (!_starpu_check_that_no_data_request_exists(memnode))
  354. can_block = 0;
  355. if (!_starpu_machine_is_running())
  356. can_block = 0;
  357. if (!_starpu_execute_registered_progression_hooks())
  358. can_block = 0;
  359. return can_block;
  360. #endif
  361. }
  362. static void _starpu_kill_all_workers(struct starpu_machine_config_s *config)
  363. {
  364. /* set the flag which will tell workers to stop */
  365. config->running = 0;
  366. starpu_wake_all_blocked_workers();
  367. }
  368. void starpu_shutdown(void)
  369. {
  370. const char *stats;
  371. PTHREAD_MUTEX_LOCK(&init_mutex);
  372. init_count--;
  373. if (init_count)
  374. /* Still somebody needing StarPU, don't deinitialize */
  375. return;
  376. /* We're last */
  377. initialized = CHANGING;
  378. PTHREAD_MUTEX_UNLOCK(&init_mutex);
  379. _starpu_display_msi_stats();
  380. _starpu_display_alloc_cache_stats();
  381. /* tell all workers to shutdown */
  382. _starpu_kill_all_workers(&config);
  383. #ifdef STARPU_DATA_STATS
  384. _starpu_display_comm_amounts();
  385. #endif
  386. if ((stats = getenv("STARPU_BUS_STATS")) && atoi(stats))
  387. starpu_bus_profiling_helper_display_summary();
  388. if ((stats = getenv("STARPU_WORKER_STATS")) && atoi(stats))
  389. starpu_worker_profiling_helper_display_summary();
  390. _starpu_deinitialize_registered_performance_models();
  391. /* wait for their termination */
  392. _starpu_terminate_workers(&config);
  393. _starpu_delete_all_sched_ctxs();
  394. _starpu_destroy_topology(&config);
  395. #ifdef STARPU_USE_FXT
  396. _starpu_stop_fxt_profiling();
  397. #endif
  398. _starpu_close_debug_logfile();
  399. PTHREAD_MUTEX_LOCK(&init_mutex);
  400. initialized = UNINITIALIZED;
  401. /* Let someone else that wants to initialize it again do it */
  402. PTHREAD_COND_SIGNAL(&init_cond);
  403. PTHREAD_MUTEX_UNLOCK(&init_mutex);
  404. }
  405. unsigned starpu_worker_get_count(void)
  406. {
  407. return config.topology.nworkers;
  408. }
  409. unsigned starpu_combined_worker_get_count(void)
  410. {
  411. return config.topology.ncombinedworkers;
  412. }
  413. unsigned starpu_cpu_worker_get_count(void)
  414. {
  415. return config.topology.ncpus;
  416. }
  417. unsigned starpu_cuda_worker_get_count(void)
  418. {
  419. return config.topology.ncudagpus;
  420. }
  421. unsigned starpu_opencl_worker_get_count(void)
  422. {
  423. return config.topology.nopenclgpus;
  424. }
  425. unsigned starpu_spu_worker_get_count(void)
  426. {
  427. return config.topology.ngordon_spus;
  428. }
  429. /* When analyzing performance, it is useful to see what is the processing unit
  430. * that actually performed the task. This function returns the id of the
  431. * processing unit actually executing it, therefore it makes no sense to use it
  432. * within the callbacks of SPU functions for instance. If called by some thread
  433. * that is not controlled by StarPU, starpu_worker_get_id returns -1. */
  434. int starpu_worker_get_id(void)
  435. {
  436. struct starpu_worker_s * worker;
  437. worker = _starpu_get_local_worker_key();
  438. if (worker)
  439. {
  440. return worker->workerid;
  441. }
  442. else {
  443. /* there is no worker associated to that thread, perhaps it is
  444. * a thread from the application or this is some SPU worker */
  445. return -1;
  446. }
  447. }
  448. int starpu_combined_worker_get_id(void)
  449. {
  450. struct starpu_worker_s *worker;
  451. worker = _starpu_get_local_worker_key();
  452. if (worker)
  453. {
  454. return worker->combined_workerid;
  455. }
  456. else {
  457. /* there is no worker associated to that thread, perhaps it is
  458. * a thread from the application or this is some SPU worker */
  459. return -1;
  460. }
  461. }
  462. int starpu_combined_worker_get_size(void)
  463. {
  464. struct starpu_worker_s *worker;
  465. worker = _starpu_get_local_worker_key();
  466. if (worker)
  467. {
  468. return worker->worker_size;
  469. }
  470. else {
  471. /* there is no worker associated to that thread, perhaps it is
  472. * a thread from the application or this is some SPU worker */
  473. return -1;
  474. }
  475. }
  476. int starpu_combined_worker_get_rank(void)
  477. {
  478. struct starpu_worker_s *worker;
  479. worker = _starpu_get_local_worker_key();
  480. if (worker)
  481. {
  482. return worker->current_rank;
  483. }
  484. else {
  485. /* there is no worker associated to that thread, perhaps it is
  486. * a thread from the application or this is some SPU worker */
  487. return -1;
  488. }
  489. }
  490. int starpu_worker_get_devid(int id)
  491. {
  492. return config.workers[id].devid;
  493. }
  494. struct starpu_worker_s *_starpu_get_worker_struct(unsigned id)
  495. {
  496. return &config.workers[id];
  497. }
  498. struct starpu_combined_worker_s *_starpu_get_combined_worker_struct(unsigned id)
  499. {
  500. unsigned basic_worker_count = starpu_worker_get_count();
  501. STARPU_ASSERT(id >= basic_worker_count);
  502. return &config.combined_workers[id - basic_worker_count];
  503. }
  504. enum starpu_archtype starpu_worker_get_type(int id)
  505. {
  506. return config.workers[id].arch;
  507. }
  508. void starpu_worker_get_name(int id, char *dst, size_t maxlen)
  509. {
  510. char *name = config.workers[id].name;
  511. snprintf(dst, maxlen, "%s", name);
  512. }
  513. /* Retrieve the status which indicates what the worker is currently doing. */
  514. starpu_worker_status _starpu_worker_get_status(int workerid)
  515. {
  516. return config.workers[workerid].status;
  517. }
  518. /* Change the status of the worker which indicates what the worker is currently
  519. * doing (eg. executing a callback). */
  520. void _starpu_worker_set_status(int workerid, starpu_worker_status status)
  521. {
  522. config.workers[workerid].status = status;
  523. }
  524. void starpu_worker_set_sched_condition(int workerid, pthread_cond_t *sched_cond, pthread_mutex_t *sched_mutex)
  525. {
  526. config.workers[workerid].sched_cond = sched_cond;
  527. config.workers[workerid].sched_mutex = sched_mutex;
  528. }
  529. struct starpu_sched_ctx* _starpu_get_initial_sched_ctx(void){
  530. return &sched_ctx;
  531. }