workers.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  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. /*
  183. * Returns 0 if the given driver is one of the drivers that must be launched by
  184. * the application itself, and not by StarPU, 1 otherwise.
  185. */
  186. static unsigned _starpu_may_launch_driver(struct starpu_conf *conf,
  187. struct starpu_driver *d)
  188. {
  189. if (conf->n_not_launched_drivers == 0 ||
  190. conf->not_launched_drivers == NULL)
  191. return 1;
  192. /* Is <d> in conf->not_launched_drivers ? */
  193. unsigned i;
  194. for (i = 0; i < conf->n_not_launched_drivers; i++)
  195. {
  196. if (d->type != conf->not_launched_drivers[i].type)
  197. continue;
  198. switch (d->type)
  199. {
  200. case STARPU_CUDA_WORKER:
  201. if (d->id.cuda_id == conf->not_launched_drivers[i].id.cuda_id)
  202. return 0;
  203. break;
  204. #ifdef STARPU_USE_OPENCL
  205. case STARPU_OPENCL_WORKER:
  206. if (d->id.opencl_id == conf->not_launched_drivers[i].id.opencl_id)
  207. return 0;
  208. break;
  209. #endif
  210. default:
  211. STARPU_ABORT();
  212. }
  213. }
  214. return 1;
  215. }
  216. static void _starpu_launch_drivers(struct _starpu_machine_config *config)
  217. {
  218. config->running = 1;
  219. config->submitting = 1;
  220. pthread_key_create(&worker_key, NULL);
  221. unsigned nworkers = config->topology.nworkers;
  222. /* Launch workers asynchronously (except for SPUs) */
  223. unsigned cuda = 0;
  224. unsigned worker;
  225. for (worker = 0; worker < nworkers; worker++)
  226. {
  227. struct _starpu_worker *workerarg = &config->workers[worker];
  228. workerarg->config = config;
  229. _STARPU_PTHREAD_MUTEX_INIT(&workerarg->mutex, NULL);
  230. _STARPU_PTHREAD_COND_INIT(&workerarg->ready_cond, NULL);
  231. workerarg->worker_size = 1;
  232. workerarg->combined_workerid = workerarg->workerid;
  233. workerarg->current_rank = 0;
  234. /* if some codelet's termination cannot be handled directly :
  235. * for instance in the Gordon driver, Gordon tasks' callbacks
  236. * may be executed by another thread than that of the Gordon
  237. * driver so that we cannot call the push_codelet_output method
  238. * directly */
  239. workerarg->terminated_jobs = _starpu_job_list_new();
  240. starpu_task_list_init(&workerarg->local_tasks);
  241. workerarg->status = STATUS_INITIALIZING;
  242. _STARPU_DEBUG("initialising worker %u\n", worker);
  243. _starpu_init_worker_queue(workerarg);
  244. struct starpu_driver driver;
  245. driver.type = workerarg->arch;
  246. switch (workerarg->arch)
  247. {
  248. #ifdef STARPU_USE_CPU
  249. case STARPU_CPU_WORKER:
  250. workerarg->set = NULL;
  251. workerarg->worker_is_initialized = 0;
  252. pthread_create(&workerarg->worker_thread,
  253. NULL, _starpu_cpu_worker, workerarg);
  254. break;
  255. #endif
  256. #ifdef STARPU_USE_CUDA
  257. case STARPU_CUDA_WORKER:
  258. workerarg->set = NULL;
  259. workerarg->worker_is_initialized = 0;
  260. driver.id.cuda_id = cuda;
  261. if (_starpu_may_launch_driver(config->conf, &driver))
  262. {
  263. pthread_create(&workerarg->worker_thread,
  264. NULL, _starpu_cuda_worker, workerarg);
  265. }
  266. cuda++;
  267. break;
  268. #endif
  269. #ifdef STARPU_USE_OPENCL
  270. case STARPU_OPENCL_WORKER:
  271. starpu_opencl_get_device(workerarg->devid, &driver.id.opencl_id);
  272. if (!_starpu_may_launch_driver(config->conf, &driver))
  273. break;
  274. workerarg->set = NULL;
  275. workerarg->worker_is_initialized = 0;
  276. pthread_create(&workerarg->worker_thread,
  277. NULL, _starpu_opencl_worker, workerarg);
  278. break;
  279. #endif
  280. #ifdef STARPU_USE_GORDON
  281. case STARPU_GORDON_WORKER:
  282. /* we will only launch gordon once, but it will handle
  283. * the different SPU workers */
  284. if (!gordon_inited)
  285. {
  286. gordon_worker_set.nworkers = config->ngordon_spus;
  287. gordon_worker_set.workers = &config->workers[worker];
  288. gordon_worker_set.set_is_initialized = 0;
  289. pthread_create(&gordon_worker_set.worker_thread, NULL,
  290. _starpu_gordon_worker, &gordon_worker_set);
  291. _STARPU_PTHREAD_MUTEX_LOCK(&gordon_worker_set.mutex);
  292. while (!gordon_worker_set.set_is_initialized)
  293. _STARPU_PTHREAD_COND_WAIT(&gordon_worker_set.ready_cond,
  294. &gordon_worker_set.mutex);
  295. _STARPU_PTHREAD_MUTEX_UNLOCK(&gordon_worker_set.mutex);
  296. gordon_inited = 1;
  297. }
  298. workerarg->set = &gordon_worker_set;
  299. gordon_worker_set.joined = 0;
  300. workerarg->worker_is_running = 1;
  301. break;
  302. #endif
  303. default:
  304. STARPU_ABORT();
  305. }
  306. }
  307. cuda = 0;
  308. for (worker = 0; worker < nworkers; worker++)
  309. {
  310. struct _starpu_worker *workerarg = &config->workers[worker];
  311. struct starpu_driver driver;
  312. driver.type = workerarg->arch;
  313. switch (workerarg->arch)
  314. {
  315. case STARPU_CPU_WORKER:
  316. _STARPU_PTHREAD_MUTEX_LOCK(&workerarg->mutex);
  317. while (!workerarg->worker_is_initialized)
  318. _STARPU_PTHREAD_COND_WAIT(&workerarg->ready_cond, &workerarg->mutex);
  319. _STARPU_PTHREAD_MUTEX_UNLOCK(&workerarg->mutex);
  320. break;
  321. case STARPU_CUDA_WORKER:
  322. driver.id.cuda_id = cuda;
  323. if (!_starpu_may_launch_driver(config->conf, &driver))
  324. {
  325. cuda++;
  326. break;
  327. }
  328. _STARPU_PTHREAD_MUTEX_LOCK(&workerarg->mutex);
  329. while (!workerarg->worker_is_initialized)
  330. _STARPU_PTHREAD_COND_WAIT(&workerarg->ready_cond, &workerarg->mutex);
  331. _STARPU_PTHREAD_MUTEX_UNLOCK(&workerarg->mutex);
  332. cuda++;
  333. break;
  334. #ifdef STARPU_USE_OPENCL
  335. case STARPU_OPENCL_WORKER:
  336. starpu_opencl_get_device(workerarg->devid, &driver.id.opencl_id);
  337. if (!_starpu_may_launch_driver(config->conf, &driver))
  338. break;
  339. _STARPU_PTHREAD_MUTEX_LOCK(&workerarg->mutex);
  340. while (!workerarg->worker_is_initialized)
  341. _STARPU_PTHREAD_COND_WAIT(&workerarg->ready_cond, &workerarg->mutex);
  342. _STARPU_PTHREAD_MUTEX_UNLOCK(&workerarg->mutex);
  343. break;
  344. #endif
  345. #ifdef STARPU_USE_GORDON
  346. case STARPU_GORDON_WORKER:
  347. /* the initialization of Gordon worker is
  348. * synchronous for now */
  349. break;
  350. #endif
  351. default:
  352. STARPU_ABORT();
  353. }
  354. }
  355. }
  356. void _starpu_set_local_worker_key(struct _starpu_worker *worker)
  357. {
  358. pthread_setspecific(worker_key, worker);
  359. }
  360. struct _starpu_worker *_starpu_get_local_worker_key(void)
  361. {
  362. return (struct _starpu_worker *) pthread_getspecific(worker_key);
  363. }
  364. /* Initialize the starpu_conf with default values */
  365. int starpu_conf_init(struct starpu_conf *conf)
  366. {
  367. if (!conf)
  368. return -EINVAL;
  369. memset(conf, 0, sizeof(*conf));
  370. conf->magic = 42;
  371. conf->sched_policy_name = getenv("STARPU_SCHED");
  372. conf->sched_policy = NULL;
  373. /* Note that starpu_get_env_number returns -1 in case the variable is
  374. * not defined */
  375. conf->ncpus = starpu_get_env_number("STARPU_NCPUS");
  376. conf->ncuda = starpu_get_env_number("STARPU_NCUDA");
  377. conf->nopencl = starpu_get_env_number("STARPU_NOPENCL");
  378. conf->nspus = starpu_get_env_number("STARPU_NGORDON");
  379. conf->calibrate = starpu_get_env_number("STARPU_CALIBRATE");
  380. if (conf->calibrate == -1)
  381. conf->calibrate = 0;
  382. conf->use_explicit_workers_bindid = 0; /* TODO */
  383. conf->use_explicit_workers_cuda_gpuid = 0; /* TODO */
  384. conf->use_explicit_workers_opencl_gpuid = 0; /* TODO */
  385. conf->single_combined_worker = starpu_get_env_number("STARPU_SINGLE_COMBINED_WORKER");
  386. if (conf->single_combined_worker == -1)
  387. conf->single_combined_worker = 0;
  388. conf->disable_asynchronous_copy = starpu_get_env_number("STARPU_DISABLE_ASYNCHRONOUS_COPY");
  389. if (conf->disable_asynchronous_copy == -1)
  390. conf->disable_asynchronous_copy = 0;
  391. return 0;
  392. }
  393. static void _starpu_conf_set_value_against_environment(char *name, int *value)
  394. {
  395. int number;
  396. number = starpu_get_env_number(name);
  397. if (number != -1)
  398. {
  399. *value = number;
  400. }
  401. }
  402. static void _starpu_conf_check_environment(struct starpu_conf *conf)
  403. {
  404. char *sched = getenv("STARPU_SCHED");
  405. if (sched)
  406. {
  407. conf->sched_policy_name = sched;
  408. }
  409. _starpu_conf_set_value_against_environment("STARPU_NCPUS", &conf->ncpus);
  410. _starpu_conf_set_value_against_environment("STARPU_NCUDA", &conf->ncuda);
  411. _starpu_conf_set_value_against_environment("STARPU_NOPENCL", &conf->nopencl);
  412. _starpu_conf_set_value_against_environment("STARPU_NGORDON", &conf->nspus);
  413. _starpu_conf_set_value_against_environment("STARPU_CALIBRATE", &conf->calibrate);
  414. _starpu_conf_set_value_against_environment("STARPU_SINGLE_COMBINED_WORKER", &conf->single_combined_worker);
  415. _starpu_conf_set_value_against_environment("STARPU_DISABLE_ASYNCHRONOUS_COPY", &conf->disable_asynchronous_copy);
  416. }
  417. int starpu_init(struct starpu_conf *user_conf)
  418. {
  419. int ret;
  420. #ifdef __GNUC__
  421. #ifndef __OPTIMIZE__
  422. if (!getenv("STARPU_SILENT")) fprintf(stderr,"Warning: StarPU was configured with --enable-debug (-O0), and is thus not optimized\n");
  423. #endif
  424. #endif
  425. #if 0
  426. #ifndef STARPU_NO_ASSERT
  427. if (!getenv("STARPU_SILENT")) fprintf(stderr,"Warning: StarPU was configured without --enable-fast\n");
  428. #endif
  429. #endif
  430. #ifdef STARPU_MEMORY_STATUS
  431. if (!getenv("STARPU_SILENT")) fprintf(stderr,"Warning: StarPU was configured with --enable-memory-status, which slows down a bit\n");
  432. #endif
  433. #ifdef STARPU_VERBOSE
  434. if (!getenv("STARPU_SILENT")) fprintf(stderr,"Warning: StarPU was configured with --enable-verbose, which slows down a bit\n");
  435. #endif
  436. #ifdef STARPU_USE_FXT
  437. if (!getenv("STARPU_SILENT")) fprintf(stderr,"Warning: StarPU was configured with --with-fxt, which slows down a bit\n");
  438. #endif
  439. #ifdef STARPU_PERF_DEBUG
  440. if (!getenv("STARPU_SILENT")) fprintf(stderr,"Warning: StarPU was configured with --enable-perf-debug, which slows down a bit\n");
  441. #endif
  442. #ifdef STARPU_MODEL_DEBUG
  443. if (!getenv("STARPU_SILENT")) fprintf(stderr,"Warning: StarPU was configured with --enable-model-debug, which slows down a bit\n");
  444. #endif
  445. #ifdef STARPU_DATA_STATS
  446. if (!getenv("STARPU_SILENT")) fprintf(stderr,"Warning: StarPU was configured with --enable-stats, which slows down a bit\n");
  447. #endif
  448. _STARPU_PTHREAD_MUTEX_LOCK(&init_mutex);
  449. while (initialized == CHANGING)
  450. /* Wait for the other one changing it */
  451. _STARPU_PTHREAD_COND_WAIT(&init_cond, &init_mutex);
  452. init_count++;
  453. if (initialized == INITIALIZED)
  454. {
  455. /* He initialized it, don't do it again, and let the others get the mutex */
  456. _STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
  457. return 0;
  458. }
  459. /* initialized == UNINITIALIZED */
  460. initialized = CHANGING;
  461. _STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
  462. #ifdef __MINGW32__
  463. WSADATA wsadata;
  464. WSAStartup(MAKEWORD(1,0), &wsadata);
  465. #endif
  466. srand(2008);
  467. /* store the pointer to the user explicit configuration during the
  468. * initialization */
  469. if (user_conf == NULL)
  470. {
  471. struct starpu_conf *conf = malloc(sizeof(struct starpu_conf));
  472. starpu_conf_init(conf);
  473. config.conf = conf;
  474. config.default_conf = 1;
  475. }
  476. else
  477. {
  478. if (user_conf->magic != 42) {
  479. fprintf(stderr, "starpu_conf structure needs to be initialized with starpu_conf_init\n");
  480. return -EINVAL;
  481. }
  482. config.conf = user_conf;
  483. config.default_conf = 0;
  484. }
  485. _starpu_conf_check_environment(config.conf);
  486. #ifdef STARPU_USE_FXT
  487. _starpu_start_fxt_profiling();
  488. #endif
  489. _starpu_open_debug_logfile();
  490. _starpu_data_interface_init();
  491. _starpu_timing_init();
  492. _starpu_profiling_init();
  493. _starpu_load_bus_performance_files();
  494. ret = _starpu_build_topology(&config);
  495. if (ret)
  496. {
  497. _STARPU_PTHREAD_MUTEX_LOCK(&init_mutex);
  498. init_count--;
  499. initialized = UNINITIALIZED;
  500. /* Let somebody else try to do it */
  501. _STARPU_PTHREAD_COND_SIGNAL(&init_cond);
  502. _STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
  503. return ret;
  504. }
  505. /* We need to store the current task handled by the different
  506. * threads */
  507. _starpu_initialize_current_task_key();
  508. /* initialize the scheduling policy */
  509. _starpu_init_sched_policy(&config);
  510. _starpu_initialize_registered_performance_models();
  511. /* Launch "basic" workers (ie. non-combined workers) */
  512. _starpu_launch_drivers(&config);
  513. _STARPU_PTHREAD_MUTEX_LOCK(&init_mutex);
  514. initialized = INITIALIZED;
  515. /* Tell everybody that we initialized */
  516. _STARPU_PTHREAD_COND_BROADCAST(&init_cond);
  517. _STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
  518. _STARPU_DEBUG("Initialisation finished\n");
  519. return 0;
  520. }
  521. /*
  522. * Handle runtime termination
  523. */
  524. static void _starpu_terminate_workers(struct _starpu_machine_config *config)
  525. {
  526. int status STARPU_ATTRIBUTE_UNUSED;
  527. unsigned workerid;
  528. for (workerid = 0; workerid < config->topology.nworkers; workerid++)
  529. {
  530. starpu_wake_all_blocked_workers();
  531. _STARPU_DEBUG("wait for worker %u\n", workerid);
  532. struct _starpu_worker_set *set = config->workers[workerid].set;
  533. struct _starpu_worker *worker = &config->workers[workerid];
  534. /* in case StarPU termination code is called from a callback,
  535. * we have to check if pthread_self() is the worker itself */
  536. if (set)
  537. {
  538. if (!set->joined)
  539. {
  540. if (!pthread_equal(pthread_self(), set->worker_thread))
  541. {
  542. status = pthread_join(set->worker_thread, NULL);
  543. #ifdef STARPU_VERBOSE
  544. if (status)
  545. {
  546. _STARPU_DEBUG("pthread_join -> %d\n", status);
  547. }
  548. #endif
  549. }
  550. set->joined = 1;
  551. }
  552. }
  553. else
  554. {
  555. if (!pthread_equal(pthread_self(), worker->worker_thread))
  556. {
  557. status = pthread_join(worker->worker_thread, NULL);
  558. #ifdef STARPU_VERBOSE
  559. if (status)
  560. {
  561. _STARPU_DEBUG("pthread_join -> %d\n", status);
  562. }
  563. #endif
  564. }
  565. }
  566. STARPU_ASSERT(starpu_task_list_empty(&worker->local_tasks));
  567. _starpu_job_list_delete(worker->terminated_jobs);
  568. }
  569. }
  570. unsigned _starpu_machine_is_running(void)
  571. {
  572. /* running is just protected by a memory barrier */
  573. STARPU_SYNCHRONIZE();
  574. return config.running;
  575. }
  576. unsigned _starpu_worker_can_block(unsigned memnode STARPU_ATTRIBUTE_UNUSED)
  577. {
  578. #ifdef STARPU_NON_BLOCKING_DRIVERS
  579. return 0;
  580. #else
  581. unsigned can_block = 1;
  582. if (!_starpu_check_that_no_data_request_exists(memnode))
  583. can_block = 0;
  584. if (!_starpu_machine_is_running())
  585. can_block = 0;
  586. if (!_starpu_execute_registered_progression_hooks())
  587. can_block = 0;
  588. return can_block;
  589. #endif
  590. }
  591. static void _starpu_kill_all_workers(struct _starpu_machine_config *config)
  592. {
  593. /* set the flag which will tell workers to stop */
  594. config->running = 0;
  595. /* running is just protected by a memory barrier */
  596. STARPU_SYNCHRONIZE();
  597. starpu_wake_all_blocked_workers();
  598. }
  599. void starpu_shutdown(void)
  600. {
  601. const char *stats;
  602. _STARPU_PTHREAD_MUTEX_LOCK(&init_mutex);
  603. init_count--;
  604. if (init_count)
  605. {
  606. _STARPU_DEBUG("Still somebody needing StarPU, don't deinitialize\n");
  607. _STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
  608. return;
  609. }
  610. /* We're last */
  611. initialized = CHANGING;
  612. _STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
  613. starpu_task_wait_for_no_ready();
  614. _starpu_display_msi_stats();
  615. _starpu_display_alloc_cache_stats();
  616. /* tell all workers to shutdown */
  617. _starpu_kill_all_workers(&config);
  618. #ifdef STARPU_MEMORY_STATUS
  619. if ((stats = getenv("STARPU_MEMORY_STATS")) && atoi(stats))
  620. _starpu_display_data_stats();
  621. #endif
  622. #ifdef STARPU_DATA_STATS
  623. _starpu_display_comm_amounts();
  624. #endif
  625. if ((stats = getenv("STARPU_BUS_STATS")) && atoi(stats))
  626. starpu_bus_profiling_helper_display_summary();
  627. if ((stats = getenv("STARPU_WORKER_STATS")) && atoi(stats))
  628. starpu_worker_profiling_helper_display_summary();
  629. _starpu_deinitialize_registered_performance_models();
  630. /* wait for their termination */
  631. _starpu_terminate_workers(&config);
  632. _starpu_deinit_sched_policy(&config);
  633. _starpu_destroy_topology(&config);
  634. #ifdef STARPU_USE_FXT
  635. _starpu_stop_fxt_profiling();
  636. #endif
  637. _starpu_data_interface_shutdown();
  638. /* Drop all remaining tags */
  639. _starpu_tag_clear();
  640. _starpu_close_debug_logfile();
  641. _STARPU_PTHREAD_MUTEX_LOCK(&init_mutex);
  642. initialized = UNINITIALIZED;
  643. /* Let someone else that wants to initialize it again do it */
  644. _STARPU_PTHREAD_COND_SIGNAL(&init_cond);
  645. _STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
  646. /* Clear memory if it was allocated by StarPU */
  647. if (config.default_conf)
  648. free(config.conf);
  649. _STARPU_DEBUG("Shutdown finished\n");
  650. }
  651. unsigned starpu_worker_get_count(void)
  652. {
  653. return config.topology.nworkers;
  654. }
  655. int starpu_worker_get_count_by_type(enum starpu_archtype type)
  656. {
  657. switch (type)
  658. {
  659. case STARPU_CPU_WORKER:
  660. return config.topology.ncpus;
  661. case STARPU_CUDA_WORKER:
  662. return config.topology.ncudagpus;
  663. case STARPU_OPENCL_WORKER:
  664. return config.topology.nopenclgpus;
  665. case STARPU_GORDON_WORKER:
  666. return config.topology.ngordon_spus;
  667. default:
  668. return -EINVAL;
  669. }
  670. }
  671. unsigned starpu_combined_worker_get_count(void)
  672. {
  673. return config.topology.ncombinedworkers;
  674. }
  675. unsigned starpu_cpu_worker_get_count(void)
  676. {
  677. return config.topology.ncpus;
  678. }
  679. unsigned starpu_cuda_worker_get_count(void)
  680. {
  681. return config.topology.ncudagpus;
  682. }
  683. unsigned starpu_opencl_worker_get_count(void)
  684. {
  685. return config.topology.nopenclgpus;
  686. }
  687. unsigned starpu_spu_worker_get_count(void)
  688. {
  689. return config.topology.ngordon_spus;
  690. }
  691. int starpu_asynchronous_copy_disabled()
  692. {
  693. return config.conf->disable_asynchronous_copy;
  694. }
  695. /* When analyzing performance, it is useful to see what is the processing unit
  696. * that actually performed the task. This function returns the id of the
  697. * processing unit actually executing it, therefore it makes no sense to use it
  698. * within the callbacks of SPU functions for instance. If called by some thread
  699. * that is not controlled by StarPU, starpu_worker_get_id returns -1. */
  700. int starpu_worker_get_id(void)
  701. {
  702. struct _starpu_worker * worker;
  703. worker = _starpu_get_local_worker_key();
  704. if (worker)
  705. {
  706. return worker->workerid;
  707. }
  708. else
  709. {
  710. /* there is no worker associated to that thread, perhaps it is
  711. * a thread from the application or this is some SPU worker */
  712. return -1;
  713. }
  714. }
  715. int starpu_combined_worker_get_id(void)
  716. {
  717. struct _starpu_worker *worker;
  718. worker = _starpu_get_local_worker_key();
  719. if (worker)
  720. {
  721. return worker->combined_workerid;
  722. }
  723. else
  724. {
  725. /* there is no worker associated to that thread, perhaps it is
  726. * a thread from the application or this is some SPU worker */
  727. return -1;
  728. }
  729. }
  730. int starpu_combined_worker_get_size(void)
  731. {
  732. struct _starpu_worker *worker;
  733. worker = _starpu_get_local_worker_key();
  734. if (worker)
  735. {
  736. return worker->worker_size;
  737. }
  738. else
  739. {
  740. /* there is no worker associated to that thread, perhaps it is
  741. * a thread from the application or this is some SPU worker */
  742. return -1;
  743. }
  744. }
  745. int starpu_combined_worker_get_rank(void)
  746. {
  747. struct _starpu_worker *worker;
  748. worker = _starpu_get_local_worker_key();
  749. if (worker)
  750. {
  751. return worker->current_rank;
  752. }
  753. else
  754. {
  755. /* there is no worker associated to that thread, perhaps it is
  756. * a thread from the application or this is some SPU worker */
  757. return -1;
  758. }
  759. }
  760. int starpu_worker_get_devid(int id)
  761. {
  762. return config.workers[id].devid;
  763. }
  764. struct _starpu_worker *_starpu_get_worker_struct(unsigned id)
  765. {
  766. return &config.workers[id];
  767. }
  768. struct _starpu_combined_worker *_starpu_get_combined_worker_struct(unsigned id)
  769. {
  770. unsigned basic_worker_count = starpu_worker_get_count();
  771. STARPU_ASSERT(id >= basic_worker_count);
  772. return &config.combined_workers[id - basic_worker_count];
  773. }
  774. enum starpu_archtype starpu_worker_get_type(int id)
  775. {
  776. return config.workers[id].arch;
  777. }
  778. int starpu_worker_get_ids_by_type(enum starpu_archtype type, int *workerids, int maxsize)
  779. {
  780. unsigned nworkers = starpu_worker_get_count();
  781. int cnt = 0;
  782. unsigned id;
  783. for (id = 0; id < nworkers; id++)
  784. {
  785. if (starpu_worker_get_type(id) == type)
  786. {
  787. /* Perhaps the array is too small ? */
  788. if (cnt >= maxsize)
  789. return -ERANGE;
  790. workerids[cnt++] = id;
  791. }
  792. }
  793. return cnt;
  794. }
  795. void starpu_worker_get_name(int id, char *dst, size_t maxlen)
  796. {
  797. char *name = config.workers[id].name;
  798. snprintf(dst, maxlen, "%s", name);
  799. }
  800. /* Retrieve the status which indicates what the worker is currently doing. */
  801. enum _starpu_worker_status _starpu_worker_get_status(int workerid)
  802. {
  803. return config.workers[workerid].status;
  804. }
  805. /* Change the status of the worker which indicates what the worker is currently
  806. * doing (eg. executing a callback). */
  807. void _starpu_worker_set_status(int workerid, enum _starpu_worker_status status)
  808. {
  809. config.workers[workerid].status = status;
  810. }
  811. void starpu_worker_set_sched_condition(int workerid, pthread_cond_t *sched_cond, pthread_mutex_t *sched_mutex)
  812. {
  813. config.workers[workerid].sched_cond = sched_cond;
  814. config.workers[workerid].sched_mutex = sched_mutex;
  815. }
  816. #ifdef STARPU_USE_CUDA
  817. extern int _starpu_run_cuda(struct starpu_driver *);
  818. #endif
  819. #ifdef STARPU_USE_OPENCL
  820. extern int _starpu_run_opencl(struct starpu_driver *);
  821. #endif
  822. int
  823. starpu_driver_run(struct starpu_driver *d)
  824. {
  825. if (!d)
  826. return -EINVAL;
  827. switch (d->type)
  828. {
  829. #ifdef STARPU_USE_CUDA
  830. case STARPU_CUDA_WORKER:
  831. return _starpu_run_cuda(d);
  832. #endif
  833. #ifdef STARPU_USE_OPENCL
  834. case STARPU_OPENCL_WORKER:
  835. return _starpu_run_opencl(d);
  836. #endif
  837. case STARPU_CPU_WORKER: /* Not supported yet */
  838. case STARPU_GORDON_WORKER: /* Not supported yet */
  839. default:
  840. return -EINVAL;
  841. }
  842. }
  843. #ifdef STARPU_USE_CUDA
  844. extern int _starpu_cuda_driver_init(struct starpu_driver *);
  845. extern int _starpu_cuda_driver_run_once(struct starpu_driver *);
  846. extern int _starpu_cuda_driver_deinit(struct starpu_driver *);
  847. #endif
  848. int
  849. starpu_driver_init(struct starpu_driver *d)
  850. {
  851. STARPU_ASSERT(d);
  852. switch (d->type)
  853. {
  854. #ifdef STARPU_USE_CUDA
  855. case STARPU_CUDA_WORKER:
  856. return _starpu_cuda_driver_init(d);
  857. #endif
  858. case STARPU_CPU_WORKER: /* Not supported yet */
  859. case STARPU_OPENCL_WORKER: /* Not supported yet */
  860. case STARPU_GORDON_WORKER: /* Not supported yet */
  861. default:
  862. return -EINVAL;
  863. }
  864. }
  865. int
  866. starpu_driver_run_once(struct starpu_driver *d)
  867. {
  868. STARPU_ASSERT(d);
  869. switch (d->type)
  870. {
  871. #ifdef STARPU_USE_CUDA
  872. case STARPU_CUDA_WORKER:
  873. return _starpu_cuda_driver_run_once(d);
  874. #endif
  875. case STARPU_CPU_WORKER: /* Not supported yet */
  876. case STARPU_OPENCL_WORKER: /* Not supported yet */
  877. case STARPU_GORDON_WORKER: /* Not supported yet */
  878. default:
  879. return -EINVAL;
  880. }
  881. }
  882. int
  883. starpu_driver_deinit(struct starpu_driver *d)
  884. {
  885. STARPU_ASSERT(d);
  886. switch (d->type)
  887. {
  888. #ifdef STARPU_USE_CUDA
  889. case STARPU_CUDA_WORKER:
  890. return _starpu_cuda_driver_deinit(d);
  891. #endif
  892. case STARPU_CPU_WORKER: /* Not supported yet */
  893. case STARPU_OPENCL_WORKER: /* Not supported yet */
  894. case STARPU_GORDON_WORKER: /* Not supported yet */
  895. default:
  896. return -EINVAL;
  897. }
  898. }