perfmodel.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2016 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 CNRS
  5. * Copyright (C) 2011 Télécom-SudParis
  6. * Copyright (C) 2016, 2017 Inria
  7. * Copyright (C) 2016 Uppsala University
  8. *
  9. * StarPU is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation; either version 2.1 of the License, or (at
  12. * your option) any later version.
  13. *
  14. * StarPU is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  17. *
  18. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  19. */
  20. #include <starpu.h>
  21. #include <starpu_profiling.h>
  22. #include <common/config.h>
  23. #include <common/utils.h>
  24. #ifdef HAVE_UNISTD_H
  25. #include <unistd.h>
  26. #endif
  27. #include <sys/stat.h>
  28. #include <core/perfmodel/perfmodel.h>
  29. #include <core/jobs.h>
  30. #include <core/workers.h>
  31. #include <datawizard/datawizard.h>
  32. #include <core/task.h>
  33. #ifdef STARPU_HAVE_WINDOWS
  34. #include <windows.h>
  35. #endif
  36. /* This flag indicates whether performance models should be calibrated or not.
  37. * 0: models need not be calibrated
  38. * 1: models must be calibrated
  39. * 2: models must be calibrated, existing models are overwritten.
  40. */
  41. static unsigned calibrate_flag = 0;
  42. void _starpu_set_calibrate_flag(unsigned val)
  43. {
  44. calibrate_flag = val;
  45. }
  46. unsigned _starpu_get_calibrate_flag(void)
  47. {
  48. return calibrate_flag;
  49. }
  50. struct starpu_perfmodel_arch* starpu_worker_get_perf_archtype(int workerid, unsigned sched_ctx_id)
  51. {
  52. STARPU_ASSERT(workerid>=0);
  53. if(sched_ctx_id != STARPU_NMAX_SCHED_CTXS)
  54. {
  55. unsigned child_sched_ctx = starpu_sched_ctx_worker_is_master_for_child_ctx(workerid, sched_ctx_id);
  56. if(child_sched_ctx != STARPU_NMAX_SCHED_CTXS)
  57. return _starpu_sched_ctx_get_perf_archtype(child_sched_ctx);
  58. struct _starpu_sched_ctx *stream_ctx = _starpu_worker_get_ctx_stream(workerid);
  59. if(stream_ctx != NULL)
  60. return _starpu_sched_ctx_get_perf_archtype(stream_ctx->id);
  61. }
  62. struct _starpu_machine_config *config = _starpu_get_machine_config();
  63. /* This workerid may either be a basic worker or a combined worker */
  64. unsigned nworkers = config->topology.nworkers;
  65. if (workerid < (int)config->topology.nworkers)
  66. return &config->workers[workerid].perf_arch;
  67. /* We have a combined worker */
  68. unsigned ncombinedworkers = config->topology.ncombinedworkers;
  69. STARPU_ASSERT(workerid < (int)(ncombinedworkers + nworkers));
  70. return &config->combined_workers[workerid - nworkers].perf_arch;
  71. }
  72. /*
  73. * PER ARCH model
  74. */
  75. static double per_arch_task_expected_perf(struct starpu_perfmodel *model, struct starpu_perfmodel_arch * arch, struct starpu_task *task, unsigned nimpl)
  76. {
  77. int comb;
  78. double (*per_arch_cost_function)(struct starpu_task *task, struct starpu_perfmodel_arch* arch, unsigned nimpl);
  79. if (model->arch_cost_function)
  80. return model->arch_cost_function(task, arch, nimpl);
  81. comb = starpu_perfmodel_arch_comb_get(arch->ndevices, arch->devices);
  82. STARPU_ASSERT_MSG(comb != -1, "Didn't find the proper arch combination\n");
  83. STARPU_ASSERT_MSG(model->state->per_arch[comb] != NULL, "STARPU_PER_ARCH needs per-arch cost_function to be defined");
  84. per_arch_cost_function = model->state->per_arch[comb][nimpl].cost_function;
  85. STARPU_ASSERT_MSG(per_arch_cost_function, "STARPU_PER_ARCH needs per-arch cost_function to be defined");
  86. return per_arch_cost_function(task, arch, nimpl);
  87. }
  88. /*
  89. * Common model
  90. */
  91. double starpu_worker_get_relative_speedup(struct starpu_perfmodel_arch* perf_arch)
  92. {
  93. double speedup = 0;
  94. int dev;
  95. for(dev = 0; dev < perf_arch->ndevices; dev++)
  96. {
  97. double coef = 0.0;
  98. if (perf_arch->devices[dev].type == STARPU_CPU_WORKER)
  99. coef = _STARPU_CPU_ALPHA;
  100. else if (perf_arch->devices[dev].type == STARPU_CUDA_WORKER)
  101. coef = _STARPU_CUDA_ALPHA;
  102. else if (perf_arch->devices[dev].type == STARPU_OPENCL_WORKER)
  103. coef = _STARPU_OPENCL_ALPHA;
  104. else if (perf_arch->devices[dev].type == STARPU_MIC_WORKER)
  105. coef = _STARPU_MIC_ALPHA;
  106. else if (perf_arch->devices[dev].type == STARPU_SCC_WORKER)
  107. coef = _STARPU_SCC_ALPHA;
  108. else if (perf_arch->devices[dev].type == STARPU_MPI_MS_WORKER)
  109. coef = _STARPU_MPI_MS_ALPHA;
  110. speedup += coef * (perf_arch->devices[dev].ncores);
  111. }
  112. return speedup;
  113. }
  114. static double common_task_expected_perf(struct starpu_perfmodel *model, struct starpu_perfmodel_arch* arch, struct starpu_task *task, unsigned nimpl)
  115. {
  116. double exp;
  117. double alpha;
  118. STARPU_ASSERT_MSG(model->cost_function, "STARPU_COMMON requires common cost_function to be defined");
  119. exp = model->cost_function(task, nimpl);
  120. alpha = starpu_worker_get_relative_speedup(arch);
  121. STARPU_ASSERT(!_STARPU_IS_ZERO(alpha));
  122. return (exp/alpha);
  123. }
  124. void _starpu_init_and_load_perfmodel(struct starpu_perfmodel *model)
  125. {
  126. if (!model || model->is_loaded)
  127. return;
  128. starpu_perfmodel_init(model);
  129. if (model->is_loaded)
  130. return;
  131. switch (model->type)
  132. {
  133. case STARPU_PER_ARCH:
  134. case STARPU_COMMON:
  135. /* Nothing more to do than init */
  136. break;
  137. case STARPU_HISTORY_BASED:
  138. case STARPU_NL_REGRESSION_BASED:
  139. _starpu_load_history_based_model(model, 1);
  140. break;
  141. case STARPU_REGRESSION_BASED:
  142. case STARPU_MULTIPLE_REGRESSION_BASED:
  143. _starpu_load_history_based_model(model, 0);
  144. break;
  145. default:
  146. STARPU_ABORT();
  147. }
  148. model->is_loaded = 1;
  149. }
  150. static double starpu_model_expected_perf(struct starpu_task *task, struct starpu_perfmodel *model, struct starpu_perfmodel_arch* arch, unsigned nimpl)
  151. {
  152. double exp_perf = 0.0;
  153. if (model)
  154. {
  155. _starpu_init_and_load_perfmodel(model);
  156. struct _starpu_job *j = _starpu_get_job_associated_to_task(task);
  157. switch (model->type)
  158. {
  159. case STARPU_PER_ARCH:
  160. exp_perf = per_arch_task_expected_perf(model, arch, task, nimpl);
  161. STARPU_ASSERT_MSG(isnan(exp_perf)||exp_perf>=0,"exp_perf=%lf\n",exp_perf);
  162. break;
  163. case STARPU_COMMON:
  164. exp_perf = common_task_expected_perf(model, arch, task, nimpl);
  165. STARPU_ASSERT_MSG(isnan(exp_perf)||exp_perf>=0,"exp_perf=%lf\n",exp_perf);
  166. break;
  167. case STARPU_HISTORY_BASED:
  168. exp_perf = _starpu_history_based_job_expected_perf(model, arch, j, nimpl);
  169. STARPU_ASSERT_MSG(isnan(exp_perf)||exp_perf>=0,"exp_perf=%lf\n",exp_perf);
  170. break;
  171. case STARPU_REGRESSION_BASED:
  172. exp_perf = _starpu_regression_based_job_expected_perf(model, arch, j, nimpl);
  173. STARPU_ASSERT_MSG(isnan(exp_perf)||exp_perf>=0,"exp_perf=%lf\n",exp_perf);
  174. break;
  175. case STARPU_NL_REGRESSION_BASED:
  176. exp_perf = _starpu_non_linear_regression_based_job_expected_perf(model, arch, j,nimpl);
  177. STARPU_ASSERT_MSG(isnan(exp_perf)||exp_perf>=0,"exp_perf=%lf\n",exp_perf);
  178. break;
  179. case STARPU_MULTIPLE_REGRESSION_BASED:
  180. exp_perf = _starpu_multiple_regression_based_job_expected_perf(model, arch, j, nimpl);
  181. STARPU_ASSERT_MSG(isnan(exp_perf)||exp_perf>=0,"exp_perf=%lf\n",exp_perf);
  182. break;
  183. default:
  184. STARPU_ABORT();
  185. }
  186. }
  187. /* no model was found */
  188. return exp_perf;
  189. }
  190. double starpu_task_expected_length(struct starpu_task *task, struct starpu_perfmodel_arch* arch, unsigned nimpl)
  191. {
  192. if (!task->cl)
  193. /* Tasks without codelet don't actually take time */
  194. return 0.0;
  195. return starpu_model_expected_perf(task, task->cl->model, arch, nimpl);
  196. }
  197. double starpu_task_expected_energy(struct starpu_task *task, struct starpu_perfmodel_arch* arch, unsigned nimpl)
  198. {
  199. if (!task->cl)
  200. /* Tasks without codelet don't actually take time */
  201. return 0.0;
  202. return starpu_model_expected_perf(task, task->cl->energy_model, arch, nimpl);
  203. }
  204. double starpu_task_expected_conversion_time(struct starpu_task *task,
  205. struct starpu_perfmodel_arch* arch,
  206. unsigned nimpl)
  207. {
  208. unsigned i;
  209. double sum = 0.0;
  210. enum starpu_node_kind node_kind;
  211. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(task);
  212. #ifdef STARPU_DEVEL
  213. #warning TODO: conversion time with combined arch perfmodel
  214. #endif
  215. STARPU_ASSERT_MSG(arch->ndevices == 1, "TODO");
  216. for (i = 0; i < nbuffers; i++)
  217. {
  218. starpu_data_handle_t handle;
  219. struct starpu_task *conversion_task;
  220. handle = STARPU_TASK_GET_HANDLE(task, i);
  221. if (!_starpu_data_is_multiformat_handle(handle))
  222. continue;
  223. switch(arch->devices[0].type)
  224. {
  225. case STARPU_CPU_WORKER:
  226. node_kind = STARPU_CPU_RAM;
  227. break;
  228. case STARPU_CUDA_WORKER:
  229. node_kind = STARPU_CUDA_RAM;
  230. break;
  231. case STARPU_OPENCL_WORKER:
  232. node_kind = STARPU_OPENCL_RAM;
  233. break;
  234. case STARPU_MIC_WORKER:
  235. node_kind = STARPU_MIC_RAM;
  236. break;
  237. case STARPU_SCC_WORKER:
  238. node_kind = STARPU_SCC_RAM;
  239. break;
  240. case STARPU_MPI_MS_WORKER:
  241. node_kind = STARPU_MPI_MS_RAM;
  242. break;
  243. default:
  244. STARPU_ABORT();
  245. break;
  246. }
  247. if (!_starpu_handle_needs_conversion_task_for_arch(handle, node_kind))
  248. continue;
  249. conversion_task = _starpu_create_conversion_task_for_arch(handle, node_kind);
  250. sum += starpu_task_expected_length(conversion_task, arch, nimpl);
  251. _starpu_spin_lock(&handle->header_lock);
  252. handle->refcnt--;
  253. handle->busy_count--;
  254. if (!_starpu_data_check_not_busy(handle))
  255. _starpu_spin_unlock(&handle->header_lock);
  256. starpu_task_clean(conversion_task);
  257. free(conversion_task);
  258. }
  259. return sum;
  260. }
  261. /* Predict the transfer time (in µs) to move a handle to a memory node */
  262. double starpu_data_expected_transfer_time(starpu_data_handle_t handle, unsigned memory_node, enum starpu_data_access_mode mode)
  263. {
  264. /* If we don't need to read the content of the handle */
  265. if (!(mode & STARPU_R))
  266. return 0.0;
  267. if (_starpu_is_data_present_or_requested(handle, memory_node))
  268. return 0.0;
  269. size_t size = _starpu_data_get_size(handle);
  270. /* XXX in case we have an abstract piece of data (eg. with the
  271. * void interface, this does not introduce any overhead, and we
  272. * don't even want to consider the latency that is not
  273. * relevant). */
  274. if (size == 0)
  275. return 0.0;
  276. int src_node = _starpu_select_src_node(handle, memory_node);
  277. if (src_node < 0)
  278. /* Will just create it in place. Ideally we should take the
  279. * time to create it into account */
  280. return 0.0;
  281. return starpu_transfer_predict(src_node, memory_node, size);
  282. }
  283. /* Data transfer performance modeling */
  284. double starpu_task_expected_data_transfer_time(unsigned memory_node, struct starpu_task *task)
  285. {
  286. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(task);
  287. unsigned buffer;
  288. double penalty = 0.0;
  289. for (buffer = 0; buffer < nbuffers; buffer++)
  290. {
  291. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(task, buffer);
  292. enum starpu_data_access_mode mode = STARPU_TASK_GET_MODE(task, buffer);
  293. penalty += starpu_data_expected_transfer_time(handle, memory_node, mode);
  294. }
  295. return penalty;
  296. }
  297. /* Return the expected duration of the entire task bundle in µs */
  298. double starpu_task_bundle_expected_length(starpu_task_bundle_t bundle, struct starpu_perfmodel_arch* arch, unsigned nimpl)
  299. {
  300. double expected_length = 0.0;
  301. /* We expect the length of the bundle the be the sum of the different tasks length. */
  302. STARPU_PTHREAD_MUTEX_LOCK(&bundle->mutex);
  303. struct _starpu_task_bundle_entry *entry;
  304. entry = bundle->list;
  305. while (entry)
  306. {
  307. if(!entry->task->scheduled)
  308. {
  309. double task_length = starpu_task_expected_length(entry->task, arch, nimpl);
  310. /* In case the task is not calibrated, we consider the task
  311. * ends immediately. */
  312. if (task_length > 0.0)
  313. expected_length += task_length;
  314. }
  315. entry = entry->next;
  316. }
  317. STARPU_PTHREAD_MUTEX_UNLOCK(&bundle->mutex);
  318. return expected_length;
  319. }
  320. /* Return the expected energy consumption of the entire task bundle in J */
  321. double starpu_task_bundle_expected_energy(starpu_task_bundle_t bundle, struct starpu_perfmodel_arch* arch, unsigned nimpl)
  322. {
  323. double expected_energy = 0.0;
  324. /* We expect total consumption of the bundle the be the sum of the different tasks consumption. */
  325. STARPU_PTHREAD_MUTEX_LOCK(&bundle->mutex);
  326. struct _starpu_task_bundle_entry *entry;
  327. entry = bundle->list;
  328. while (entry)
  329. {
  330. double task_energy = starpu_task_expected_energy(entry->task, arch, nimpl);
  331. /* In case the task is not calibrated, we consider the task
  332. * ends immediately. */
  333. if (task_energy > 0.0)
  334. expected_energy += task_energy;
  335. entry = entry->next;
  336. }
  337. STARPU_PTHREAD_MUTEX_UNLOCK(&bundle->mutex);
  338. return expected_energy;
  339. }
  340. /* Return the time (in µs) expected to transfer all data used within the bundle */
  341. double starpu_task_bundle_expected_data_transfer_time(starpu_task_bundle_t bundle, unsigned memory_node)
  342. {
  343. STARPU_PTHREAD_MUTEX_LOCK(&bundle->mutex);
  344. struct _starpu_handle_list *handles = NULL;
  345. /* We list all the handle that are accessed within the bundle. */
  346. /* For each task in the bundle */
  347. struct _starpu_task_bundle_entry *entry = bundle->list;
  348. while (entry)
  349. {
  350. struct starpu_task *task = entry->task;
  351. if (task->cl)
  352. {
  353. unsigned b;
  354. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(task);
  355. for (b = 0; b < nbuffers; b++)
  356. {
  357. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(task, b);
  358. enum starpu_data_access_mode mode = STARPU_TASK_GET_MODE(task, b);
  359. if (!(mode & STARPU_R))
  360. continue;
  361. /* Insert the handle in the sorted list in case
  362. * it's not already in that list. */
  363. _insertion_handle_sorted(&handles, handle, mode);
  364. }
  365. }
  366. entry = entry->next;
  367. }
  368. STARPU_PTHREAD_MUTEX_UNLOCK(&bundle->mutex);
  369. /* Compute the sum of data transfer time, and destroy the list */
  370. double total_exp = 0.0;
  371. while (handles)
  372. {
  373. struct _starpu_handle_list *current = handles;
  374. handles = handles->next;
  375. double exp;
  376. exp = starpu_data_expected_transfer_time(current->handle, memory_node, current->mode);
  377. total_exp += exp;
  378. free(current);
  379. }
  380. return total_exp;
  381. }
  382. static int directory_existence_was_tested = 0;
  383. static char *_perf_model_dir = NULL;
  384. static char *_perf_model_dir_codelet = NULL;
  385. static char *_perf_model_dir_bus = NULL;
  386. static char *_perf_model_dir_debug = NULL;
  387. #define _PERF_MODEL_DIR_MAXLEN 256
  388. void _starpu_set_perf_model_dirs()
  389. {
  390. _STARPU_MALLOC(_perf_model_dir, _PERF_MODEL_DIR_MAXLEN);
  391. _STARPU_MALLOC(_perf_model_dir_codelet, _PERF_MODEL_DIR_MAXLEN);
  392. _STARPU_MALLOC(_perf_model_dir_bus, _PERF_MODEL_DIR_MAXLEN);
  393. _STARPU_MALLOC(_perf_model_dir_debug, _PERF_MODEL_DIR_MAXLEN);
  394. #ifdef STARPU_PERF_MODEL_DIR
  395. /* use the directory specified at configure time */
  396. snprintf(_perf_model_dir, _PERF_MODEL_DIR_MAXLEN, "%s", (char *)STARPU_PERF_MODEL_DIR);
  397. #else
  398. snprintf(_perf_model_dir, _PERF_MODEL_DIR_MAXLEN, "%s/.starpu/sampling/", _starpu_get_home_path());
  399. #endif
  400. char *path = starpu_getenv("STARPU_PERF_MODEL_DIR");
  401. if (path)
  402. {
  403. snprintf(_perf_model_dir, _PERF_MODEL_DIR_MAXLEN, "%s/", path);
  404. }
  405. snprintf(_perf_model_dir_codelet, _PERF_MODEL_DIR_MAXLEN, "%s/codelets/%d/", _perf_model_dir, _STARPU_PERFMODEL_VERSION);
  406. snprintf(_perf_model_dir_bus, _PERF_MODEL_DIR_MAXLEN, "%s/bus/", _perf_model_dir);
  407. snprintf(_perf_model_dir_debug, _PERF_MODEL_DIR_MAXLEN, "%s/debug/", _perf_model_dir);
  408. }
  409. char *_starpu_get_perf_model_dir_codelet()
  410. {
  411. _starpu_create_sampling_directory_if_needed();
  412. return _perf_model_dir_codelet;
  413. }
  414. char *_starpu_get_perf_model_dir_bus()
  415. {
  416. _starpu_create_sampling_directory_if_needed();
  417. return _perf_model_dir_bus;
  418. }
  419. char *_starpu_get_perf_model_dir_debug()
  420. {
  421. _starpu_create_sampling_directory_if_needed();
  422. return _perf_model_dir_debug;
  423. }
  424. void _starpu_create_sampling_directory_if_needed(void)
  425. {
  426. if (!directory_existence_was_tested)
  427. {
  428. _starpu_set_perf_model_dirs();
  429. /* The performance of the codelets are stored in
  430. * $STARPU_PERF_MODEL_DIR/codelets/ while those of the bus are stored in
  431. * $STARPU_PERF_MODEL_DIR/bus/ so that we don't have name collisions */
  432. /* Testing if a directory exists and creating it otherwise
  433. may not be safe: it is possible that the permission are
  434. changed in between. Instead, we create it and check if
  435. it already existed before */
  436. _starpu_mkpath_and_check(_perf_model_dir, S_IRWXU);
  437. /* Per-task performance models */
  438. _starpu_mkpath_and_check(_perf_model_dir_codelet, S_IRWXU);
  439. /* Performance of the memory subsystem */
  440. _starpu_mkpath_and_check(_perf_model_dir_bus, S_IRWXU);
  441. /* Performance debug measurements */
  442. _starpu_mkpath(_perf_model_dir_debug, S_IRWXU);
  443. directory_existence_was_tested = 1;
  444. }
  445. }
  446. void starpu_perfmodel_free_sampling_directories(void)
  447. {
  448. free(_perf_model_dir);
  449. _perf_model_dir = NULL;
  450. free(_perf_model_dir_codelet);
  451. _perf_model_dir_codelet = NULL;
  452. free(_perf_model_dir_bus);
  453. _perf_model_dir_bus = NULL;
  454. free(_perf_model_dir_debug);
  455. _perf_model_dir_debug = NULL;
  456. directory_existence_was_tested = 0;
  457. }