perfmodel.c 15 KB

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