workers.c 28 KB

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