workers.c 29 KB

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