perfmodel.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2013 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012, 2013 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. #include <unistd.h>
  23. #include <sys/stat.h>
  24. #include <core/perfmodel/perfmodel.h>
  25. #include <core/jobs.h>
  26. #include <core/workers.h>
  27. #include <datawizard/datawizard.h>
  28. #ifdef STARPU_HAVE_WINDOWS
  29. #include <windows.h>
  30. #endif
  31. /* This flag indicates whether performance models should be calibrated or not.
  32. * 0: models need not be calibrated
  33. * 1: models must be calibrated
  34. * 2: models must be calibrated, existing models are overwritten.
  35. */
  36. static unsigned calibrate_flag = 0;
  37. void _starpu_set_calibrate_flag(unsigned val)
  38. {
  39. calibrate_flag = val;
  40. }
  41. unsigned _starpu_get_calibrate_flag(void)
  42. {
  43. return calibrate_flag;
  44. }
  45. enum starpu_perfmodel_archtype starpu_worker_get_perf_archtype(int workerid)
  46. {
  47. struct _starpu_machine_config *config = _starpu_get_machine_config();
  48. /* This workerid may either be a basic worker or a combined worker */
  49. unsigned nworkers = config->topology.nworkers;
  50. if (workerid < (int)config->topology.nworkers)
  51. return config->workers[workerid].perf_arch;
  52. /* We have a combined worker */
  53. unsigned ncombinedworkers = config->topology.ncombinedworkers;
  54. STARPU_ASSERT(workerid < (int)(ncombinedworkers + nworkers));
  55. return config->combined_workers[workerid - nworkers].perf_arch;
  56. }
  57. /*
  58. * PER ARCH model
  59. */
  60. static double per_arch_task_expected_perf(struct starpu_perfmodel *model, enum starpu_perfmodel_archtype arch, struct starpu_task *task, unsigned nimpl)
  61. {
  62. double exp = NAN;
  63. double (*per_arch_cost_function)(struct starpu_task *task, enum starpu_perfmodel_archtype arch, unsigned nimpl);
  64. double (*per_arch_cost_model)(struct starpu_data_descr *);
  65. per_arch_cost_function = model->per_arch[arch][nimpl].cost_function;
  66. per_arch_cost_model = model->per_arch[arch][nimpl].cost_model;
  67. if (per_arch_cost_function)
  68. exp = per_arch_cost_function(task, arch, nimpl);
  69. else if (per_arch_cost_model)
  70. exp = per_arch_cost_model(task->buffers);
  71. return exp;
  72. }
  73. /*
  74. * Common model
  75. */
  76. double starpu_worker_get_relative_speedup(enum starpu_perfmodel_archtype perf_archtype)
  77. {
  78. if (perf_archtype < STARPU_CUDA_DEFAULT)
  79. {
  80. return _STARPU_CPU_ALPHA * (perf_archtype + 1);
  81. }
  82. else if (perf_archtype < STARPU_OPENCL_DEFAULT)
  83. {
  84. return _STARPU_CUDA_ALPHA;
  85. }
  86. else if (perf_archtype < STARPU_NARCH_VARIATIONS)
  87. {
  88. return _STARPU_OPENCL_ALPHA;
  89. }
  90. STARPU_ABORT();
  91. /* Never reached ! */
  92. return NAN;
  93. }
  94. static double common_task_expected_perf(struct starpu_perfmodel *model, enum starpu_perfmodel_archtype arch, struct starpu_task *task, unsigned nimpl)
  95. {
  96. double exp;
  97. double alpha;
  98. if (model->cost_function)
  99. {
  100. exp = model->cost_function(task, nimpl);
  101. alpha = starpu_worker_get_relative_speedup(arch);
  102. STARPU_ASSERT(!_STARPU_IS_ZERO(alpha));
  103. return (exp/alpha);
  104. }
  105. else if (model->cost_model)
  106. {
  107. exp = model->cost_model(task->buffers);
  108. alpha = starpu_worker_get_relative_speedup(arch);
  109. STARPU_ASSERT(!_STARPU_IS_ZERO(alpha));
  110. return (exp/alpha);
  111. }
  112. return NAN;
  113. }
  114. void _starpu_load_perfmodel(struct starpu_perfmodel *model)
  115. {
  116. if (!model || model->is_loaded)
  117. return;
  118. int load_model = _starpu_register_model(model);
  119. if (!load_model)
  120. return;
  121. switch (model->type)
  122. {
  123. case STARPU_PER_ARCH:
  124. _starpu_load_per_arch_based_model(model);
  125. break;
  126. case STARPU_COMMON:
  127. _starpu_load_common_based_model(model);
  128. break;
  129. case STARPU_HISTORY_BASED:
  130. case STARPU_NL_REGRESSION_BASED:
  131. _starpu_load_history_based_model(model, 1);
  132. break;
  133. case STARPU_REGRESSION_BASED:
  134. _starpu_load_history_based_model(model, 0);
  135. break;
  136. default:
  137. STARPU_ABORT();
  138. }
  139. model->is_loaded = 1;
  140. }
  141. static double starpu_model_expected_perf(struct starpu_task *task, struct starpu_perfmodel *model, enum starpu_perfmodel_archtype arch, unsigned nimpl)
  142. {
  143. if (model)
  144. {
  145. struct _starpu_job *j = _starpu_get_job_associated_to_task(task);
  146. switch (model->type)
  147. {
  148. case STARPU_PER_ARCH:
  149. return per_arch_task_expected_perf(model, arch, task, nimpl);
  150. case STARPU_COMMON:
  151. return common_task_expected_perf(model, arch, task, nimpl);
  152. case STARPU_HISTORY_BASED:
  153. return _starpu_history_based_job_expected_perf(model, arch, j, nimpl);
  154. case STARPU_REGRESSION_BASED:
  155. return _starpu_regression_based_job_expected_perf(model, arch, j, nimpl);
  156. case STARPU_NL_REGRESSION_BASED:
  157. return _starpu_non_linear_regression_based_job_expected_perf(model, arch, j,nimpl);
  158. default:
  159. STARPU_ABORT();
  160. }
  161. }
  162. /* no model was found */
  163. return 0.0;
  164. }
  165. double starpu_task_expected_length(struct starpu_task *task, enum starpu_perfmodel_archtype arch, unsigned nimpl)
  166. {
  167. return starpu_model_expected_perf(task, task->cl->model, arch, nimpl);
  168. }
  169. double starpu_task_expected_power(struct starpu_task *task, enum starpu_perfmodel_archtype arch, unsigned nimpl)
  170. {
  171. return starpu_model_expected_perf(task, task->cl->power_model, arch, nimpl);
  172. }
  173. double starpu_task_expected_conversion_time(struct starpu_task *task,
  174. enum starpu_perfmodel_archtype arch,
  175. unsigned nimpl)
  176. {
  177. unsigned i;
  178. double sum = 0.0;
  179. enum starpu_node_kind node_kind;
  180. for (i = 0; i < task->cl->nbuffers; i++)
  181. {
  182. starpu_data_handle_t handle;
  183. struct starpu_task *conversion_task;
  184. handle = STARPU_TASK_GET_HANDLE(task, i);
  185. if (!_starpu_data_is_multiformat_handle(handle))
  186. continue;
  187. if (arch < STARPU_CUDA_DEFAULT)
  188. node_kind = STARPU_CPU_RAM;
  189. else if (arch < STARPU_OPENCL_DEFAULT)
  190. node_kind = STARPU_CUDA_RAM;
  191. else
  192. node_kind = STARPU_OPENCL_RAM;
  193. if (!_starpu_handle_needs_conversion_task_for_arch(handle, node_kind))
  194. continue;
  195. conversion_task = _starpu_create_conversion_task_for_arch(handle, node_kind);
  196. sum += starpu_task_expected_length(conversion_task, arch, nimpl);
  197. _starpu_spin_lock(&handle->header_lock);
  198. handle->refcnt--;
  199. handle->busy_count--;
  200. _starpu_spin_unlock(&handle->header_lock);
  201. starpu_task_clean(conversion_task);
  202. free(conversion_task);
  203. }
  204. return sum;
  205. }
  206. /* Predict the transfer time (in µs) to move a handle to a memory node */
  207. double starpu_data_expected_transfer_time(starpu_data_handle_t handle, unsigned memory_node, enum starpu_data_access_mode mode)
  208. {
  209. /* If we don't need to read the content of the handle */
  210. if (!(mode & STARPU_R))
  211. return 0.0;
  212. if (_starpu_is_data_present_or_requested(handle, memory_node))
  213. return 0.0;
  214. size_t size = _starpu_data_get_size(handle);
  215. /* XXX in case we have an abstract piece of data (eg. with the
  216. * void interface, this does not introduce any overhead, and we
  217. * don't even want to consider the latency that is not
  218. * relevant). */
  219. if (size == 0)
  220. return 0.0;
  221. unsigned src_node = _starpu_select_src_node(handle, memory_node);
  222. return _starpu_predict_transfer_time(src_node, memory_node, size);
  223. }
  224. /* Data transfer performance modeling */
  225. double starpu_task_expected_data_transfer_time(unsigned memory_node, struct starpu_task *task)
  226. {
  227. unsigned nbuffers = task->cl->nbuffers;
  228. unsigned buffer;
  229. double penalty = 0.0;
  230. for (buffer = 0; buffer < nbuffers; buffer++)
  231. {
  232. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(task, buffer);
  233. enum starpu_data_access_mode mode = STARPU_CODELET_GET_MODE(task->cl, buffer);
  234. penalty += starpu_data_expected_transfer_time(handle, memory_node, mode);
  235. }
  236. return penalty;
  237. }
  238. /* Return the expected duration of the entire task bundle in µs */
  239. double starpu_task_bundle_expected_length(starpu_task_bundle_t bundle, enum starpu_perfmodel_archtype arch, unsigned nimpl)
  240. {
  241. double expected_length = 0.0;
  242. /* We expect the length of the bundle the be the sum of the different tasks length. */
  243. STARPU_PTHREAD_MUTEX_LOCK(&bundle->mutex);
  244. struct _starpu_task_bundle_entry *entry;
  245. entry = bundle->list;
  246. while (entry)
  247. {
  248. if(!entry->task->scheduled)
  249. {
  250. double task_length = starpu_task_expected_length(entry->task, arch, nimpl);
  251. /* In case the task is not calibrated, we consider the task
  252. * ends immediately. */
  253. if (task_length > 0.0)
  254. expected_length += task_length;
  255. }
  256. entry = entry->next;
  257. }
  258. STARPU_PTHREAD_MUTEX_UNLOCK(&bundle->mutex);
  259. return expected_length;
  260. }
  261. /* Return the expected power consumption of the entire task bundle in J */
  262. double starpu_task_bundle_expected_power(starpu_task_bundle_t bundle, enum starpu_perfmodel_archtype arch, unsigned nimpl)
  263. {
  264. double expected_power = 0.0;
  265. /* We expect total consumption of the bundle the be the sum of the different tasks consumption. */
  266. STARPU_PTHREAD_MUTEX_LOCK(&bundle->mutex);
  267. struct _starpu_task_bundle_entry *entry;
  268. entry = bundle->list;
  269. while (entry)
  270. {
  271. double task_power = starpu_task_expected_power(entry->task, arch, nimpl);
  272. /* In case the task is not calibrated, we consider the task
  273. * ends immediately. */
  274. if (task_power > 0.0)
  275. expected_power += task_power;
  276. entry = entry->next;
  277. }
  278. STARPU_PTHREAD_MUTEX_UNLOCK(&bundle->mutex);
  279. return expected_power;
  280. }
  281. /* Return the time (in µs) expected to transfer all data used within the bundle */
  282. double starpu_task_bundle_expected_data_transfer_time(starpu_task_bundle_t bundle, unsigned memory_node)
  283. {
  284. STARPU_PTHREAD_MUTEX_LOCK(&bundle->mutex);
  285. struct _starpu_handle_list *handles = NULL;
  286. /* We list all the handle that are accessed within the bundle. */
  287. /* For each task in the bundle */
  288. struct _starpu_task_bundle_entry *entry = bundle->list;
  289. while (entry)
  290. {
  291. struct starpu_task *task = entry->task;
  292. if (task->cl)
  293. {
  294. unsigned b;
  295. for (b = 0; b < task->cl->nbuffers; b++)
  296. {
  297. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(task, b);
  298. enum starpu_data_access_mode mode = STARPU_CODELET_GET_MODE(task->cl, b);
  299. if (!(mode & STARPU_R))
  300. continue;
  301. /* Insert the handle in the sorted list in case
  302. * it's not already in that list. */
  303. _insertion_handle_sorted(&handles, handle, mode);
  304. }
  305. }
  306. entry = entry->next;
  307. }
  308. STARPU_PTHREAD_MUTEX_UNLOCK(&bundle->mutex);
  309. /* Compute the sum of data transfer time, and destroy the list */
  310. double total_exp = 0.0;
  311. while (handles)
  312. {
  313. struct _starpu_handle_list *current = handles;
  314. handles = handles->next;
  315. double exp;
  316. exp = starpu_data_expected_transfer_time(current->handle, memory_node, current->mode);
  317. total_exp += exp;
  318. free(current);
  319. }
  320. return total_exp;
  321. }
  322. static int directory_existence_was_tested = 0;
  323. void _starpu_get_perf_model_dir(char *path, size_t maxlen)
  324. {
  325. #ifdef STARPU_PERF_MODEL_DIR
  326. /* use the directory specified at configure time */
  327. snprintf(path, maxlen, "%s", STARPU_PERF_MODEL_DIR);
  328. #else
  329. snprintf(path, maxlen, "%s/.starpu/sampling/", _starpu_get_home_path());
  330. #endif
  331. }
  332. void _starpu_get_perf_model_dir_codelets(char *path, size_t maxlen)
  333. {
  334. _starpu_get_perf_model_dir(path, maxlen);
  335. strncat(path, "/codelets/", maxlen);
  336. }
  337. void _starpu_get_perf_model_dir_bus(char *path, size_t maxlen)
  338. {
  339. _starpu_get_perf_model_dir(path, maxlen);
  340. strncat(path, "/bus/", maxlen);
  341. }
  342. void _starpu_get_perf_model_dir_debug(char *path, size_t maxlen)
  343. {
  344. _starpu_get_perf_model_dir(path, maxlen);
  345. strncat(path, "/debug/", maxlen);
  346. }
  347. void _starpu_create_sampling_directory_if_needed(void)
  348. {
  349. if (!directory_existence_was_tested)
  350. {
  351. char perf_model_dir[STARPU_NMAXWORKERS];
  352. _starpu_get_perf_model_dir(perf_model_dir, STARPU_NMAXWORKERS);
  353. /* The performance of the codelets are stored in
  354. * $STARPU_PERF_MODEL_DIR/codelets/ while those of the bus are stored in
  355. * $STARPU_PERF_MODEL_DIR/bus/ so that we don't have name collisions */
  356. /* Testing if a directory exists and creating it otherwise
  357. may not be safe: it is possible that the permission are
  358. changed in between. Instead, we create it and check if
  359. it already existed before */
  360. _starpu_mkpath_and_check(perf_model_dir, S_IRWXU);
  361. /* Per-task performance models */
  362. char perf_model_dir_codelets[STARPU_NMAXWORKERS];
  363. _starpu_get_perf_model_dir_codelets(perf_model_dir_codelets, STARPU_NMAXWORKERS);
  364. _starpu_mkpath_and_check(perf_model_dir_codelets, S_IRWXU);
  365. /* Performance of the memory subsystem */
  366. char perf_model_dir_bus[STARPU_NMAXWORKERS];
  367. _starpu_get_perf_model_dir_bus(perf_model_dir_bus, STARPU_NMAXWORKERS);
  368. _starpu_mkpath_and_check(perf_model_dir_bus, S_IRWXU);
  369. /* Performance debug measurements */
  370. char perf_model_dir_debug[STARPU_NMAXWORKERS];
  371. _starpu_get_perf_model_dir_debug(perf_model_dir_debug, STARPU_NMAXWORKERS);
  372. _starpu_mkpath_and_check(perf_model_dir_debug, S_IRWXU);
  373. directory_existence_was_tested = 1;
  374. }
  375. }