perfmodel.c 13 KB

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