workers.c 31 KB

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