perfmodel.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010, 2011 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011 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_perf_archtype starpu_worker_get_perf_archtype(int workerid)
  46. {
  47. struct starpu_machine_config_s *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_perf_archtype arch, struct starpu_task *task, unsigned nimpl)
  61. {
  62. double exp = -1.0;
  63. double (*per_arch_cost_model)(struct starpu_buffer_descr *);
  64. per_arch_cost_model = model->per_arch[arch][nimpl].cost_model;
  65. if (per_arch_cost_model)
  66. exp = per_arch_cost_model(task->buffers);
  67. return exp;
  68. }
  69. /*
  70. * Common model
  71. */
  72. double starpu_worker_get_relative_speedup(enum starpu_perf_archtype perf_archtype)
  73. {
  74. if (perf_archtype < STARPU_CUDA_DEFAULT)
  75. {
  76. return STARPU_CPU_ALPHA * (perf_archtype + 1);
  77. }
  78. else if (perf_archtype < STARPU_OPENCL_DEFAULT)
  79. {
  80. return STARPU_CUDA_ALPHA;
  81. }
  82. else if (perf_archtype < STARPU_GORDON_DEFAULT)
  83. {
  84. return STARPU_OPENCL_ALPHA;
  85. }
  86. else if (perf_archtype < STARPU_NARCH_VARIATIONS) {
  87. /* Gordon value */
  88. return STARPU_GORDON_ALPHA;
  89. }
  90. STARPU_ABORT();
  91. /* Never reached ! */
  92. return -1.0;
  93. }
  94. static double common_task_expected_perf(struct starpu_perfmodel *model, enum starpu_perf_archtype arch, struct starpu_task *task)
  95. {
  96. double exp;
  97. double alpha;
  98. if (model->cost_model) {
  99. exp = model->cost_model(task->buffers);
  100. alpha = starpu_worker_get_relative_speedup(arch);
  101. STARPU_ASSERT(alpha != 0.0f);
  102. return (exp/alpha);
  103. }
  104. return -1.0;
  105. }
  106. void _starpu_load_perfmodel(struct starpu_perfmodel *model)
  107. {
  108. if (!model || model->is_loaded)
  109. return;
  110. int load_model = _starpu_register_model(model);
  111. if (!load_model)
  112. return;
  113. switch (model->type) {
  114. case STARPU_PER_ARCH:
  115. case STARPU_COMMON:
  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, enum starpu_perf_archtype arch, unsigned nimpl)
  130. {
  131. if (model) {
  132. starpu_job_t j = _starpu_get_job_associated_to_task(task);
  133. switch (model->type) {
  134. case STARPU_PER_ARCH:
  135. return per_arch_task_expected_perf(model, arch, task, nimpl);
  136. case STARPU_COMMON:
  137. return common_task_expected_perf(model, arch, task);
  138. case STARPU_HISTORY_BASED:
  139. return _starpu_history_based_job_expected_perf(model, arch, j, nimpl);
  140. case STARPU_REGRESSION_BASED:
  141. return _starpu_regression_based_job_expected_perf(model, arch, j, nimpl);
  142. case STARPU_NL_REGRESSION_BASED:
  143. return _starpu_non_linear_regression_based_job_expected_perf(model, arch, j,nimpl);
  144. default:
  145. STARPU_ABORT();
  146. };
  147. }
  148. /* no model was found */
  149. return 0.0;
  150. }
  151. double starpu_task_expected_length(struct starpu_task *task, enum starpu_perf_archtype arch, unsigned nimpl)
  152. {
  153. return starpu_model_expected_perf(task, task->cl->model, arch, nimpl);
  154. }
  155. double starpu_task_expected_power(struct starpu_task *task, enum starpu_perf_archtype arch, unsigned nimpl)
  156. {
  157. return starpu_model_expected_perf(task, task->cl->power_model, arch, nimpl);
  158. }
  159. double starpu_task_expected_conversion_time(struct starpu_task *task, enum starpu_perf_archtype arch, unsigned nimpl)
  160. {
  161. return starpu_model_expected_perf(task, task->cl->conversion_model, arch, nimpl);
  162. }
  163. /* Predict the transfer time (in µs) to move a handle to a memory node */
  164. double starpu_data_expected_transfer_time(starpu_data_handle_t handle, unsigned memory_node, enum starpu_access_mode mode)
  165. {
  166. /* If we don't need to read the content of the handle */
  167. if (!(mode & STARPU_R))
  168. return 0.0;
  169. if (_starpu_is_data_present_or_requested(handle, memory_node))
  170. return 0.0;
  171. size_t size = _starpu_data_get_size(handle);
  172. /* XXX in case we have an abstract piece of data (eg. with the
  173. * void interface, this does not introduce any overhead, and we
  174. * don't even want to consider the latency that is not
  175. * relevant). */
  176. if (size == 0)
  177. return 0.0;
  178. uint32_t src_node = _starpu_select_src_node(handle, memory_node);
  179. return _starpu_predict_transfer_time(src_node, memory_node, size);
  180. }
  181. /* Data transfer performance modeling */
  182. double starpu_task_expected_data_transfer_time(uint32_t memory_node, struct starpu_task *task)
  183. {
  184. unsigned nbuffers = task->cl->nbuffers;
  185. unsigned buffer;
  186. double penalty = 0.0;
  187. for (buffer = 0; buffer < nbuffers; buffer++)
  188. {
  189. starpu_data_handle_t handle = task->buffers[buffer].handle;
  190. enum starpu_access_mode mode = task->buffers[buffer].mode;
  191. penalty += starpu_data_expected_transfer_time(handle, memory_node, mode);
  192. }
  193. return penalty;
  194. }
  195. static int directory_existence_was_tested = 0;
  196. void _starpu_get_perf_model_dir(char *path, size_t maxlen)
  197. {
  198. #ifdef STARPU_PERF_MODEL_DIR
  199. /* use the directory specified at configure time */
  200. snprintf(path, maxlen, "%s", STARPU_PERF_MODEL_DIR);
  201. #else
  202. /* by default, we use $HOME/.starpu/sampling */
  203. const char *home_path = getenv("HOME");
  204. if (!home_path)
  205. home_path = getenv("USERPROFILE");
  206. if (!home_path) {
  207. _STARPU_ERROR("couldn't find a home place to put starpu data\n");
  208. }
  209. snprintf(path, maxlen, "%s/.starpu/sampling/", home_path);
  210. #endif
  211. }
  212. void _starpu_get_perf_model_dir_codelets(char *path, size_t maxlen)
  213. {
  214. _starpu_get_perf_model_dir(path, maxlen);
  215. strncat(path, "/codelets/", maxlen);
  216. }
  217. void _starpu_get_perf_model_dir_bus(char *path, size_t maxlen)
  218. {
  219. _starpu_get_perf_model_dir(path, maxlen);
  220. strncat(path, "/bus/", maxlen);
  221. }
  222. void _starpu_get_perf_model_dir_debug(char *path, size_t maxlen)
  223. {
  224. _starpu_get_perf_model_dir(path, maxlen);
  225. strncat(path, "/debug/", maxlen);
  226. }
  227. void _starpu_create_sampling_directory_if_needed(void)
  228. {
  229. if (!directory_existence_was_tested)
  230. {
  231. char perf_model_dir[256];
  232. _starpu_get_perf_model_dir(perf_model_dir, 256);
  233. /* The performance of the codelets are stored in
  234. * $STARPU_PERF_MODEL_DIR/codelets/ while those of the bus are stored in
  235. * $STARPU_PERF_MODEL_DIR/bus/ so that we don't have name collisions */
  236. /* Testing if a directory exists and creating it otherwise
  237. may not be safe: it is possible that the permission are
  238. changed in between. Instead, we create it and check if
  239. it already existed before */
  240. int ret;
  241. ret = _starpu_mkpath(perf_model_dir, S_IRWXU);
  242. if (ret == -1)
  243. {
  244. STARPU_ASSERT(errno == EEXIST);
  245. /* make sure that it is actually a directory */
  246. struct stat sb;
  247. stat(perf_model_dir, &sb);
  248. STARPU_ASSERT(S_ISDIR(sb.st_mode));
  249. }
  250. /* Per-task performance models */
  251. char perf_model_dir_codelets[256];
  252. _starpu_get_perf_model_dir_codelets(perf_model_dir_codelets, 256);
  253. ret = _starpu_mkpath(perf_model_dir_codelets, S_IRWXU);
  254. if (ret == -1)
  255. {
  256. STARPU_ASSERT(errno == EEXIST);
  257. /* make sure that it is actually a directory */
  258. struct stat sb;
  259. stat(perf_model_dir_codelets, &sb);
  260. STARPU_ASSERT(S_ISDIR(sb.st_mode));
  261. }
  262. /* Performance of the memory subsystem */
  263. char perf_model_dir_bus[256];
  264. _starpu_get_perf_model_dir_bus(perf_model_dir_bus, 256);
  265. ret = _starpu_mkpath(perf_model_dir_bus, S_IRWXU);
  266. if (ret == -1)
  267. {
  268. STARPU_ASSERT(errno == EEXIST);
  269. /* make sure that it is actually a directory */
  270. struct stat sb;
  271. stat(perf_model_dir_bus, &sb);
  272. STARPU_ASSERT(S_ISDIR(sb.st_mode));
  273. }
  274. /* Performance debug measurements */
  275. char perf_model_dir_debug[256];
  276. _starpu_get_perf_model_dir_debug(perf_model_dir_debug, 256);
  277. ret = _starpu_mkpath(perf_model_dir_debug, S_IRWXU);
  278. if (ret == -1)
  279. {
  280. STARPU_ASSERT(errno == EEXIST);
  281. /* make sure that it is actually a directory */
  282. struct stat sb;
  283. stat(perf_model_dir_debug, &sb);
  284. STARPU_ASSERT(S_ISDIR(sb.st_mode));
  285. }
  286. directory_existence_was_tested = 1;
  287. }
  288. }