workers.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2012 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012 Centre National de la Recherche Scientifique
  5. * Copyright (C) 2010, 2011 Institut National de Recherche en Informatique et Automatique
  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 <stdlib.h>
  20. #include <stdio.h>
  21. #include <common/config.h>
  22. #include <common/utils.h>
  23. #include <core/workers.h>
  24. #include <core/debug.h>
  25. #include <core/task.h>
  26. #include <profiling/profiling.h>
  27. #include <starpu_task_list.h>
  28. #ifdef __MINGW32__
  29. #include <windows.h>
  30. #endif
  31. /* acquire/release semantic for concurrent initialization/de-initialization */
  32. static pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER;
  33. static pthread_cond_t init_cond = PTHREAD_COND_INITIALIZER;
  34. static int init_count = 0;
  35. static enum { UNINITIALIZED, CHANGING, INITIALIZED } initialized = UNINITIALIZED;
  36. static pthread_key_t worker_key;
  37. static struct _starpu_machine_config config;
  38. int _starpu_is_initialized(void)
  39. {
  40. return initialized == INITIALIZED;
  41. }
  42. struct _starpu_machine_config *_starpu_get_machine_config(void)
  43. {
  44. return &config;
  45. }
  46. /* Makes sure that at least one of the workers of type <arch> can execute
  47. * <task>*/
  48. static uint32_t _starpu_worker_exists_and_can_execute(struct starpu_task *task,
  49. enum starpu_archtype arch)
  50. {
  51. int i;
  52. int nworkers = starpu_worker_get_count_by_type(arch);
  53. int workers[nworkers];
  54. STARPU_ASSERT(nworkers != -EINVAL);
  55. (void) starpu_worker_get_ids_by_type(arch, workers, nworkers);
  56. for (i = 0; i < nworkers; i++)
  57. if (task->cl->can_execute(workers[i], task, 0))
  58. return 1;
  59. return 0;
  60. }
  61. /* in case a task is submitted, we may check whether there exists a worker
  62. that may execute the task or not */
  63. uint32_t _starpu_worker_exists(struct starpu_task *task)
  64. {
  65. if (!(task->cl->where & config.worker_mask))
  66. return 0;
  67. if (!task->cl->can_execute)
  68. return 1;
  69. #ifdef STARPU_USE_CPU
  70. if ((task->cl->where & STARPU_CPU) &&
  71. _starpu_worker_exists_and_can_execute(task, STARPU_CPU_WORKER))
  72. return 1;
  73. #endif
  74. #ifdef STARPU_USE_CUDA
  75. if ((task->cl->where & STARPU_CUDA) &&
  76. _starpu_worker_exists_and_can_execute(task, STARPU_CUDA_WORKER))
  77. return 1;
  78. #endif
  79. #ifdef STARPU_USE_OPENCL
  80. if ((task->cl->where & STARPU_OPENCL) &&
  81. _starpu_worker_exists_and_can_execute(task, STARPU_OPENCL_WORKER))
  82. return 1;
  83. #endif
  84. return 0;
  85. }
  86. uint32_t _starpu_can_submit_cuda_task(void)
  87. {
  88. return (STARPU_CUDA & config.worker_mask);
  89. }
  90. uint32_t _starpu_can_submit_cpu_task(void)
  91. {
  92. return (STARPU_CPU & config.worker_mask);
  93. }
  94. uint32_t _starpu_can_submit_opencl_task(void)
  95. {
  96. return (STARPU_OPENCL & config.worker_mask);
  97. }
  98. static int _starpu_can_use_nth_implementation(enum starpu_archtype arch, struct starpu_codelet *cl, unsigned nimpl)
  99. {
  100. switch(arch)
  101. {
  102. case STARPU_CPU_WORKER:
  103. {
  104. starpu_cpu_func_t func = _starpu_task_get_cpu_nth_implementation(cl, nimpl);
  105. return func != NULL;
  106. }
  107. case STARPU_CUDA_WORKER:
  108. {
  109. starpu_cuda_func_t func = _starpu_task_get_cuda_nth_implementation(cl, nimpl);
  110. return func != NULL;
  111. }
  112. case STARPU_OPENCL_WORKER:
  113. {
  114. starpu_opencl_func_t func = _starpu_task_get_opencl_nth_implementation(cl, nimpl);
  115. return func != NULL;
  116. }
  117. case STARPU_GORDON_WORKER:
  118. {
  119. starpu_gordon_func_t func = _starpu_task_get_gordon_nth_implementation(cl, nimpl);
  120. return func != 0;
  121. }
  122. default:
  123. STARPU_ASSERT_MSG(0, "Unknown arch type");
  124. }
  125. return 0;
  126. }
  127. int starpu_worker_can_execute_task(unsigned workerid, struct starpu_task *task, unsigned nimpl)
  128. {
  129. /* TODO: check that the task operand sizes will fit on that device */
  130. return (task->cl->where & config.workers[workerid].worker_mask) &&
  131. _starpu_can_use_nth_implementation(config.workers[workerid].arch, task->cl, nimpl) &&
  132. (!task->cl->can_execute || task->cl->can_execute(workerid, task, nimpl));
  133. }
  134. int starpu_combined_worker_can_execute_task(unsigned workerid, struct starpu_task *task, unsigned nimpl)
  135. {
  136. /* TODO: check that the task operand sizes will fit on that device */
  137. /* TODO: call application-provided function for various cases like
  138. * double support, shared memory size limit, etc. */
  139. struct starpu_codelet *cl = task->cl;
  140. unsigned nworkers = config.topology.nworkers;
  141. /* Is this a parallel worker ? */
  142. if (workerid < nworkers)
  143. {
  144. return !!((task->cl->where & config.workers[workerid].worker_mask) &&
  145. _starpu_can_use_nth_implementation(config.workers[workerid].arch, task->cl, nimpl));
  146. }
  147. else
  148. {
  149. if ((cl->type == STARPU_SPMD)
  150. #ifdef STARPU_HAVE_HWLOC
  151. || (cl->type == STARPU_FORKJOIN)
  152. #endif
  153. )
  154. {
  155. /* TODO we should add other types of constraints */
  156. /* Is the worker larger than requested ? */
  157. int worker_size = (int)config.combined_workers[workerid - nworkers].worker_size;
  158. return !!((worker_size <= task->cl->max_parallelism) &&
  159. _starpu_can_use_nth_implementation(config.workers[workerid].arch, task->cl, nimpl));
  160. }
  161. else
  162. {
  163. /* We have a sequential task but a parallel worker */
  164. return 0;
  165. }
  166. }
  167. }
  168. /*
  169. * Runtime initialization methods
  170. */
  171. #ifdef STARPU_USE_GORDON
  172. static unsigned gordon_inited = 0;
  173. static struct _starpu_worker_set gordon_worker_set;
  174. #endif
  175. static void _starpu_init_worker_queue(struct _starpu_worker *workerarg)
  176. {
  177. pthread_cond_t *cond = workerarg->sched_cond;
  178. pthread_mutex_t *mutex = workerarg->sched_mutex;
  179. unsigned memory_node = workerarg->memory_node;
  180. _starpu_memory_node_register_condition(cond, mutex, memory_node);
  181. }
  182. static void _starpu_launch_drivers(struct _starpu_machine_config *config)
  183. {
  184. config->running = 1;
  185. pthread_key_create(&worker_key, NULL);
  186. unsigned nworkers = config->topology.nworkers;
  187. /* Launch workers asynchronously (except for SPUs) */
  188. unsigned worker;
  189. for (worker = 0; worker < nworkers; worker++)
  190. {
  191. struct _starpu_worker *workerarg = &config->workers[worker];
  192. workerarg->config = config;
  193. _STARPU_PTHREAD_MUTEX_INIT(&workerarg->mutex, NULL);
  194. _STARPU_PTHREAD_COND_INIT(&workerarg->ready_cond, NULL);
  195. workerarg->worker_size = 1;
  196. workerarg->combined_workerid = workerarg->workerid;
  197. workerarg->current_rank = 0;
  198. /* if some codelet's termination cannot be handled directly :
  199. * for instance in the Gordon driver, Gordon tasks' callbacks
  200. * may be executed by another thread than that of the Gordon
  201. * driver so that we cannot call the push_codelet_output method
  202. * directly */
  203. workerarg->terminated_jobs = _starpu_job_list_new();
  204. starpu_task_list_init(&workerarg->local_tasks);
  205. workerarg->status = STATUS_INITIALIZING;
  206. _STARPU_DEBUG("initialising worker %u\n", worker);
  207. _starpu_init_worker_queue(workerarg);
  208. switch (workerarg->arch)
  209. {
  210. #ifdef STARPU_USE_CPU
  211. case STARPU_CPU_WORKER:
  212. workerarg->set = NULL;
  213. workerarg->worker_is_initialized = 0;
  214. pthread_create(&workerarg->worker_thread,
  215. NULL, _starpu_cpu_worker, workerarg);
  216. break;
  217. #endif
  218. #ifdef STARPU_USE_CUDA
  219. case STARPU_CUDA_WORKER:
  220. workerarg->set = NULL;
  221. workerarg->worker_is_initialized = 0;
  222. pthread_create(&workerarg->worker_thread,
  223. NULL, _starpu_cuda_worker, workerarg);
  224. break;
  225. #endif
  226. #ifdef STARPU_USE_OPENCL
  227. case STARPU_OPENCL_WORKER:
  228. workerarg->set = NULL;
  229. workerarg->worker_is_initialized = 0;
  230. pthread_create(&workerarg->worker_thread,
  231. NULL, _starpu_opencl_worker, workerarg);
  232. break;
  233. #endif
  234. #ifdef STARPU_USE_GORDON
  235. case STARPU_GORDON_WORKER:
  236. /* we will only launch gordon once, but it will handle
  237. * the different SPU workers */
  238. if (!gordon_inited)
  239. {
  240. gordon_worker_set.nworkers = config->ngordon_spus;
  241. gordon_worker_set.workers = &config->workers[worker];
  242. gordon_worker_set.set_is_initialized = 0;
  243. pthread_create(&gordon_worker_set.worker_thread, NULL,
  244. _starpu_gordon_worker, &gordon_worker_set);
  245. _STARPU_PTHREAD_MUTEX_LOCK(&gordon_worker_set.mutex);
  246. while (!gordon_worker_set.set_is_initialized)
  247. _STARPU_PTHREAD_COND_WAIT(&gordon_worker_set.ready_cond,
  248. &gordon_worker_set.mutex);
  249. _STARPU_PTHREAD_MUTEX_UNLOCK(&gordon_worker_set.mutex);
  250. gordon_inited = 1;
  251. }
  252. workerarg->set = &gordon_worker_set;
  253. gordon_worker_set.joined = 0;
  254. workerarg->worker_is_running = 1;
  255. break;
  256. #endif
  257. default:
  258. STARPU_ABORT();
  259. }
  260. }
  261. for (worker = 0; worker < nworkers; worker++)
  262. {
  263. struct _starpu_worker *workerarg = &config->workers[worker];
  264. switch (workerarg->arch)
  265. {
  266. case STARPU_CPU_WORKER:
  267. case STARPU_CUDA_WORKER:
  268. case STARPU_OPENCL_WORKER:
  269. _STARPU_PTHREAD_MUTEX_LOCK(&workerarg->mutex);
  270. while (!workerarg->worker_is_initialized)
  271. _STARPU_PTHREAD_COND_WAIT(&workerarg->ready_cond, &workerarg->mutex);
  272. _STARPU_PTHREAD_MUTEX_UNLOCK(&workerarg->mutex);
  273. break;
  274. #ifdef STARPU_USE_GORDON
  275. case STARPU_GORDON_WORKER:
  276. /* the initialization of Gordon worker is
  277. * synchronous for now */
  278. break;
  279. #endif
  280. default:
  281. STARPU_ABORT();
  282. }
  283. }
  284. }
  285. void _starpu_set_local_worker_key(struct _starpu_worker *worker)
  286. {
  287. pthread_setspecific(worker_key, worker);
  288. }
  289. struct _starpu_worker *_starpu_get_local_worker_key(void)
  290. {
  291. return (struct _starpu_worker *) pthread_getspecific(worker_key);
  292. }
  293. /* Initialize the starpu_conf with default values */
  294. int starpu_conf_init(struct starpu_conf *conf)
  295. {
  296. if (!conf)
  297. return -EINVAL;
  298. conf->sched_policy_name = getenv("STARPU_SCHED");
  299. conf->sched_policy = NULL;
  300. /* Note that starpu_get_env_number returns -1 in case the variable is
  301. * not defined */
  302. conf->ncpus = starpu_get_env_number("STARPU_NCPUS");
  303. conf->ncuda = starpu_get_env_number("STARPU_NCUDA");
  304. conf->nopencl = starpu_get_env_number("STARPU_NOPENCL");
  305. conf->nspus = starpu_get_env_number("STARPU_NGORDON");
  306. conf->calibrate = starpu_get_env_number("STARPU_CALIBRATE");
  307. if (conf->calibrate == -1)
  308. conf->calibrate = 0;
  309. conf->use_explicit_workers_bindid = 0; /* TODO */
  310. conf->use_explicit_workers_cuda_gpuid = 0; /* TODO */
  311. conf->use_explicit_workers_opencl_gpuid = 0; /* TODO */
  312. conf->single_combined_worker = starpu_get_env_number("STARPU_SINGLE_COMBINED_WORKER");
  313. if (conf->single_combined_worker == -1)
  314. conf->single_combined_worker = 0;
  315. conf->disable_asynchronous_copy = starpu_get_env_number("STARPU_DISABLE_ASYNCHRONOUS_COPY");
  316. return 0;
  317. }
  318. static void _starpu_conf_set_value_against_environment(char *name, int *value)
  319. {
  320. int number;
  321. number = starpu_get_env_number(name);
  322. if (number != -1)
  323. {
  324. *value = number;
  325. }
  326. }
  327. static void _starpu_conf_check_environment(struct starpu_conf *conf)
  328. {
  329. char *sched = getenv("STARPU_SCHED");
  330. if (sched)
  331. {
  332. conf->sched_policy_name = sched;
  333. }
  334. _starpu_conf_set_value_against_environment("STARPU_NCPUS", &conf->ncpus);
  335. _starpu_conf_set_value_against_environment("STARPU_NCUDA", &conf->ncuda);
  336. _starpu_conf_set_value_against_environment("STARPU_NOPENCL", &conf->nopencl);
  337. _starpu_conf_set_value_against_environment("STARPU_NGORDON", &conf->nspus);
  338. _starpu_conf_set_value_against_environment("STARPU_CALIBRATE", &conf->calibrate);
  339. _starpu_conf_set_value_against_environment("STARPU_SINGLE_COMBINED_WORKER", &conf->single_combined_worker);
  340. _starpu_conf_set_value_against_environment("STARPU_DISABLE_ASYNCHRONOUS_COPY", &conf->disable_asynchronous_copy);
  341. }
  342. int starpu_init(struct starpu_conf *user_conf)
  343. {
  344. int ret;
  345. #ifdef __GNUC__
  346. #ifndef __OPTIMIZE__
  347. if (!getenv("STARPU_SILENT")) fprintf(stderr,"Warning: StarPU was configured with --enable-debug (-O0), and is thus not optimized\n");
  348. #endif
  349. #endif
  350. #if 0
  351. #ifndef STARPU_NO_ASSERT
  352. if (!getenv("STARPU_SILENT")) fprintf(stderr,"Warning: StarPU was configured without --enable-fast\n");
  353. #endif
  354. #endif
  355. #ifdef STARPU_MEMORY_STATUS
  356. if (!getenv("STARPU_SILENT")) fprintf(stderr,"Warning: StarPU was configured with --enable-memory-status, which slows down a bit\n");
  357. #endif
  358. #ifdef STARPU_VERBOSE
  359. if (!getenv("STARPU_SILENT")) fprintf(stderr,"Warning: StarPU was configured with --enable-verbose, which slows down a bit\n");
  360. #endif
  361. #ifdef STARPU_USE_FXT
  362. if (!getenv("STARPU_SILENT")) fprintf(stderr,"Warning: StarPU was configured with --with-fxt, which slows down a bit\n");
  363. #endif
  364. #ifdef STARPU_PERF_DEBUG
  365. if (!getenv("STARPU_SILENT")) fprintf(stderr,"Warning: StarPU was configured with --enable-perf-debug, which slows down a bit\n");
  366. #endif
  367. #ifdef STARPU_MODEL_DEBUG
  368. if (!getenv("STARPU_SILENT")) fprintf(stderr,"Warning: StarPU was configured with --enable-model-debug, which slows down a bit\n");
  369. #endif
  370. #ifdef STARPU_DATA_STATS
  371. if (!getenv("STARPU_SILENT")) fprintf(stderr,"Warning: StarPU was configured with --enable-stats, which slows down a bit\n");
  372. #endif
  373. _STARPU_PTHREAD_MUTEX_LOCK(&init_mutex);
  374. while (initialized == CHANGING)
  375. /* Wait for the other one changing it */
  376. _STARPU_PTHREAD_COND_WAIT(&init_cond, &init_mutex);
  377. init_count++;
  378. if (initialized == INITIALIZED)
  379. {
  380. /* He initialized it, don't do it again, and let the others get the mutex */
  381. _STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
  382. return 0;
  383. }
  384. /* initialized == UNINITIALIZED */
  385. initialized = CHANGING;
  386. _STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
  387. #ifdef __MINGW32__
  388. WSADATA wsadata;
  389. WSAStartup(MAKEWORD(1,0), &wsadata);
  390. #endif
  391. srand(2008);
  392. #ifdef STARPU_USE_FXT
  393. _starpu_start_fxt_profiling();
  394. #endif
  395. _starpu_open_debug_logfile();
  396. _starpu_data_interface_init();
  397. _starpu_timing_init();
  398. _starpu_profiling_init();
  399. _starpu_load_bus_performance_files();
  400. /* store the pointer to the user explicit configuration during the
  401. * initialization */
  402. if (user_conf == NULL)
  403. {
  404. struct starpu_conf *conf = malloc(sizeof(struct starpu_conf));
  405. starpu_conf_init(conf);
  406. config.conf = conf;
  407. config.default_conf = 1;
  408. }
  409. else
  410. {
  411. config.conf = user_conf;
  412. config.default_conf = 0;
  413. }
  414. _starpu_conf_check_environment(config.conf);
  415. ret = _starpu_build_topology(&config);
  416. if (ret)
  417. {
  418. _STARPU_PTHREAD_MUTEX_LOCK(&init_mutex);
  419. init_count--;
  420. initialized = UNINITIALIZED;
  421. /* Let somebody else try to do it */
  422. _STARPU_PTHREAD_COND_SIGNAL(&init_cond);
  423. _STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
  424. return ret;
  425. }
  426. /* We need to store the current task handled by the different
  427. * threads */
  428. _starpu_initialize_current_task_key();
  429. /* initialize the scheduling policy */
  430. _starpu_init_sched_policy(&config);
  431. _starpu_initialize_registered_performance_models();
  432. /* Launch "basic" workers (ie. non-combined workers) */
  433. _starpu_launch_drivers(&config);
  434. _STARPU_PTHREAD_MUTEX_LOCK(&init_mutex);
  435. initialized = INITIALIZED;
  436. /* Tell everybody that we initialized */
  437. _STARPU_PTHREAD_COND_BROADCAST(&init_cond);
  438. _STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
  439. _STARPU_DEBUG("Initialisation finished\n");
  440. return 0;
  441. }
  442. /*
  443. * Handle runtime termination
  444. */
  445. static void _starpu_terminate_workers(struct _starpu_machine_config *config)
  446. {
  447. int status STARPU_ATTRIBUTE_UNUSED;
  448. unsigned workerid;
  449. for (workerid = 0; workerid < config->topology.nworkers; workerid++)
  450. {
  451. starpu_wake_all_blocked_workers();
  452. _STARPU_DEBUG("wait for worker %u\n", workerid);
  453. struct _starpu_worker_set *set = config->workers[workerid].set;
  454. struct _starpu_worker *worker = &config->workers[workerid];
  455. /* in case StarPU termination code is called from a callback,
  456. * we have to check if pthread_self() is the worker itself */
  457. if (set)
  458. {
  459. if (!set->joined)
  460. {
  461. if (!pthread_equal(pthread_self(), set->worker_thread))
  462. {
  463. status = pthread_join(set->worker_thread, NULL);
  464. #ifdef STARPU_VERBOSE
  465. if (status)
  466. {
  467. _STARPU_DEBUG("pthread_join -> %d\n", status);
  468. }
  469. #endif
  470. }
  471. set->joined = 1;
  472. }
  473. }
  474. else
  475. {
  476. if (!pthread_equal(pthread_self(), worker->worker_thread))
  477. {
  478. status = pthread_join(worker->worker_thread, NULL);
  479. #ifdef STARPU_VERBOSE
  480. if (status)
  481. {
  482. _STARPU_DEBUG("pthread_join -> %d\n", status);
  483. }
  484. #endif
  485. }
  486. }
  487. STARPU_ASSERT(starpu_task_list_empty(&worker->local_tasks));
  488. _starpu_job_list_delete(worker->terminated_jobs);
  489. }
  490. }
  491. unsigned _starpu_machine_is_running(void)
  492. {
  493. /* running is just protected by a memory barrier */
  494. STARPU_SYNCHRONIZE();
  495. return config.running;
  496. }
  497. unsigned _starpu_worker_can_block(unsigned memnode STARPU_ATTRIBUTE_UNUSED)
  498. {
  499. #ifdef STARPU_NON_BLOCKING_DRIVERS
  500. return 0;
  501. #else
  502. unsigned can_block = 1;
  503. if (!_starpu_check_that_no_data_request_exists(memnode))
  504. can_block = 0;
  505. if (!_starpu_machine_is_running())
  506. can_block = 0;
  507. if (!_starpu_execute_registered_progression_hooks())
  508. can_block = 0;
  509. return can_block;
  510. #endif
  511. }
  512. static void _starpu_kill_all_workers(struct _starpu_machine_config *config)
  513. {
  514. /* set the flag which will tell workers to stop */
  515. config->running = 0;
  516. /* running is just protected by a memory barrier */
  517. STARPU_SYNCHRONIZE();
  518. starpu_wake_all_blocked_workers();
  519. }
  520. void starpu_shutdown(void)
  521. {
  522. const char *stats;
  523. _STARPU_PTHREAD_MUTEX_LOCK(&init_mutex);
  524. init_count--;
  525. if (init_count)
  526. {
  527. _STARPU_DEBUG("Still somebody needing StarPU, don't deinitialize\n");
  528. _STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
  529. return;
  530. }
  531. /* We're last */
  532. initialized = CHANGING;
  533. _STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
  534. starpu_task_wait_for_no_ready();
  535. _starpu_display_msi_stats();
  536. _starpu_display_alloc_cache_stats();
  537. /* tell all workers to shutdown */
  538. _starpu_kill_all_workers(&config);
  539. #ifdef STARPU_MEMORY_STATUS
  540. if ((stats = getenv("STARPU_MEMORY_STATS")) && atoi(stats))
  541. _starpu_display_data_stats();
  542. #endif
  543. #ifdef STARPU_DATA_STATS
  544. _starpu_display_comm_amounts();
  545. #endif
  546. if ((stats = getenv("STARPU_BUS_STATS")) && atoi(stats))
  547. starpu_bus_profiling_helper_display_summary();
  548. if ((stats = getenv("STARPU_WORKER_STATS")) && atoi(stats))
  549. starpu_worker_profiling_helper_display_summary();
  550. _starpu_deinitialize_registered_performance_models();
  551. /* wait for their termination */
  552. _starpu_terminate_workers(&config);
  553. _starpu_deinit_sched_policy(&config);
  554. _starpu_destroy_topology(&config);
  555. #ifdef STARPU_USE_FXT
  556. _starpu_stop_fxt_profiling();
  557. #endif
  558. _starpu_data_interface_shutdown();
  559. /* Drop all remaining tags */
  560. _starpu_tag_clear();
  561. _starpu_close_debug_logfile();
  562. _STARPU_PTHREAD_MUTEX_LOCK(&init_mutex);
  563. initialized = UNINITIALIZED;
  564. /* Let someone else that wants to initialize it again do it */
  565. _STARPU_PTHREAD_COND_SIGNAL(&init_cond);
  566. _STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
  567. /* Clear memory if it was allocated by StarPU */
  568. if (config.default_conf)
  569. free(config.conf);
  570. _STARPU_DEBUG("Shutdown finished\n");
  571. }
  572. unsigned starpu_worker_get_count(void)
  573. {
  574. return config.topology.nworkers;
  575. }
  576. int starpu_worker_get_count_by_type(enum starpu_archtype type)
  577. {
  578. switch (type)
  579. {
  580. case STARPU_CPU_WORKER:
  581. return config.topology.ncpus;
  582. case STARPU_CUDA_WORKER:
  583. return config.topology.ncudagpus;
  584. case STARPU_OPENCL_WORKER:
  585. return config.topology.nopenclgpus;
  586. case STARPU_GORDON_WORKER:
  587. return config.topology.ngordon_spus;
  588. default:
  589. return -EINVAL;
  590. }
  591. }
  592. unsigned starpu_combined_worker_get_count(void)
  593. {
  594. return config.topology.ncombinedworkers;
  595. }
  596. unsigned starpu_cpu_worker_get_count(void)
  597. {
  598. return config.topology.ncpus;
  599. }
  600. unsigned starpu_cuda_worker_get_count(void)
  601. {
  602. return config.topology.ncudagpus;
  603. }
  604. unsigned starpu_opencl_worker_get_count(void)
  605. {
  606. return config.topology.nopenclgpus;
  607. }
  608. unsigned starpu_spu_worker_get_count(void)
  609. {
  610. return config.topology.ngordon_spus;
  611. }
  612. int starpu_asynchronous_copy_disabled()
  613. {
  614. return config.conf->disable_asynchronous_copy;
  615. }
  616. /* When analyzing performance, it is useful to see what is the processing unit
  617. * that actually performed the task. This function returns the id of the
  618. * processing unit actually executing it, therefore it makes no sense to use it
  619. * within the callbacks of SPU functions for instance. If called by some thread
  620. * that is not controlled by StarPU, starpu_worker_get_id returns -1. */
  621. int starpu_worker_get_id(void)
  622. {
  623. struct _starpu_worker * worker;
  624. worker = _starpu_get_local_worker_key();
  625. if (worker)
  626. {
  627. return worker->workerid;
  628. }
  629. else
  630. {
  631. /* there is no worker associated to that thread, perhaps it is
  632. * a thread from the application or this is some SPU worker */
  633. return -1;
  634. }
  635. }
  636. int starpu_combined_worker_get_id(void)
  637. {
  638. struct _starpu_worker *worker;
  639. worker = _starpu_get_local_worker_key();
  640. if (worker)
  641. {
  642. return worker->combined_workerid;
  643. }
  644. else
  645. {
  646. /* there is no worker associated to that thread, perhaps it is
  647. * a thread from the application or this is some SPU worker */
  648. return -1;
  649. }
  650. }
  651. int starpu_combined_worker_get_size(void)
  652. {
  653. struct _starpu_worker *worker;
  654. worker = _starpu_get_local_worker_key();
  655. if (worker)
  656. {
  657. return worker->worker_size;
  658. }
  659. else
  660. {
  661. /* there is no worker associated to that thread, perhaps it is
  662. * a thread from the application or this is some SPU worker */
  663. return -1;
  664. }
  665. }
  666. int starpu_combined_worker_get_rank(void)
  667. {
  668. struct _starpu_worker *worker;
  669. worker = _starpu_get_local_worker_key();
  670. if (worker)
  671. {
  672. return worker->current_rank;
  673. }
  674. else
  675. {
  676. /* there is no worker associated to that thread, perhaps it is
  677. * a thread from the application or this is some SPU worker */
  678. return -1;
  679. }
  680. }
  681. int starpu_worker_get_devid(int id)
  682. {
  683. return config.workers[id].devid;
  684. }
  685. struct _starpu_worker *_starpu_get_worker_struct(unsigned id)
  686. {
  687. return &config.workers[id];
  688. }
  689. struct _starpu_combined_worker *_starpu_get_combined_worker_struct(unsigned id)
  690. {
  691. unsigned basic_worker_count = starpu_worker_get_count();
  692. STARPU_ASSERT(id >= basic_worker_count);
  693. return &config.combined_workers[id - basic_worker_count];
  694. }
  695. enum starpu_archtype starpu_worker_get_type(int id)
  696. {
  697. return config.workers[id].arch;
  698. }
  699. int starpu_worker_get_ids_by_type(enum starpu_archtype type, int *workerids, int maxsize)
  700. {
  701. unsigned nworkers = starpu_worker_get_count();
  702. int cnt = 0;
  703. unsigned id;
  704. for (id = 0; id < nworkers; id++)
  705. {
  706. if (starpu_worker_get_type(id) == type)
  707. {
  708. /* Perhaps the array is too small ? */
  709. if (cnt >= maxsize)
  710. return -ERANGE;
  711. workerids[cnt++] = id;
  712. }
  713. }
  714. return cnt;
  715. }
  716. void starpu_worker_get_name(int id, char *dst, size_t maxlen)
  717. {
  718. char *name = config.workers[id].name;
  719. snprintf(dst, maxlen, "%s", name);
  720. }
  721. /* Retrieve the status which indicates what the worker is currently doing. */
  722. enum _starpu_worker_status _starpu_worker_get_status(int workerid)
  723. {
  724. return config.workers[workerid].status;
  725. }
  726. /* Change the status of the worker which indicates what the worker is currently
  727. * doing (eg. executing a callback). */
  728. void _starpu_worker_set_status(int workerid, enum _starpu_worker_status status)
  729. {
  730. config.workers[workerid].status = status;
  731. }
  732. void starpu_worker_set_sched_condition(int workerid, pthread_cond_t *sched_cond, pthread_mutex_t *sched_mutex)
  733. {
  734. config.workers[workerid].sched_cond = sched_cond;
  735. config.workers[workerid].sched_mutex = sched_mutex;
  736. }