perfmodel.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010, 2011 Université de Bordeaux 1
  4. * Copyright (C) 2010 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_t *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_t *);
  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_t *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. extern pthread_rwlock_t registered_models_rwlock;
  107. void _starpu_load_perfmodel(struct starpu_perfmodel_t *model)
  108. {
  109. if (!model || model->is_loaded)
  110. return;
  111. /* If the model has already been loaded, there is nothing to do */
  112. PTHREAD_RWLOCK_RDLOCK(&registered_models_rwlock);
  113. if (model->is_loaded) {
  114. PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  115. return;
  116. }
  117. PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  118. /* We have to make sure the model has not been loaded since the
  119. * last time we took the lock */
  120. PTHREAD_RWLOCK_WRLOCK(&registered_models_rwlock);
  121. if (model->is_loaded) {
  122. PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  123. return;
  124. }
  125. _starpu_register_model(model);
  126. PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  127. switch (model->type) {
  128. case STARPU_PER_ARCH:
  129. case STARPU_COMMON:
  130. break;
  131. case STARPU_HISTORY_BASED:
  132. case STARPU_NL_REGRESSION_BASED:
  133. _starpu_load_history_based_model(model, 1);
  134. break;
  135. case STARPU_REGRESSION_BASED:
  136. _starpu_load_history_based_model(model, 0);
  137. break;
  138. default:
  139. STARPU_ABORT();
  140. }
  141. model->is_loaded = 1;
  142. }
  143. static double starpu_model_expected_perf(struct starpu_task *task, struct starpu_perfmodel_t *model, enum starpu_perf_archtype arch, unsigned nimpl)
  144. {
  145. if (model) {
  146. starpu_job_t j = _starpu_get_job_associated_to_task(task);
  147. switch (model->type) {
  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);
  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_perf_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_perf_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, enum starpu_perf_archtype arch, unsigned nimpl)
  174. {
  175. return starpu_model_expected_perf(task, task->cl->conversion_model, arch, nimpl);
  176. }
  177. /* Predict the transfer time (in µs) to move a handle to a memory node */
  178. double starpu_data_expected_transfer_time(starpu_data_handle handle, unsigned memory_node, starpu_access_mode mode)
  179. {
  180. /* If we don't need to read the content of the handle */
  181. if (!(mode & STARPU_R))
  182. return 0.0;
  183. if (_starpu_is_data_present_or_requested(handle, memory_node))
  184. return 0.0;
  185. size_t size = _starpu_data_get_size(handle);
  186. /* XXX in case we have an abstract piece of data (eg. with the
  187. * void interface, this does not introduce any overhead, and we
  188. * don't even want to consider the latency that is not
  189. * relevant). */
  190. if (size == 0)
  191. return 0.0;
  192. uint32_t src_node = _starpu_select_src_node(handle, memory_node);
  193. return _starpu_predict_transfer_time(src_node, memory_node, size);
  194. }
  195. /* Data transfer performance modeling */
  196. double starpu_task_expected_data_transfer_time(uint32_t memory_node, struct starpu_task *task)
  197. {
  198. unsigned nbuffers = task->cl->nbuffers;
  199. unsigned buffer;
  200. double penalty = 0.0;
  201. for (buffer = 0; buffer < nbuffers; buffer++)
  202. {
  203. starpu_data_handle handle = task->buffers[buffer].handle;
  204. starpu_access_mode mode = task->buffers[buffer].mode;
  205. penalty += starpu_data_expected_transfer_time(handle, memory_node, mode);
  206. }
  207. return penalty;
  208. }
  209. static int directory_existence_was_tested = 0;
  210. void _starpu_get_perf_model_dir(char *path, size_t maxlen)
  211. {
  212. #ifdef STARPU_PERF_MODEL_DIR
  213. /* use the directory specified at configure time */
  214. snprintf(path, maxlen, "%s", STARPU_PERF_MODEL_DIR);
  215. #else
  216. /* by default, we use $HOME/.starpu/sampling */
  217. const char *home_path = getenv("HOME");
  218. if (!home_path)
  219. home_path = getenv("USERPROFILE");
  220. if (!home_path) {
  221. _STARPU_ERROR("couldn't find a home place to put starpu data\n");
  222. }
  223. snprintf(path, maxlen, "%s/.starpu/sampling/", home_path);
  224. #endif
  225. }
  226. void _starpu_get_perf_model_dir_codelets(char *path, size_t maxlen)
  227. {
  228. _starpu_get_perf_model_dir(path, maxlen);
  229. strncat(path, "/codelets/", maxlen);
  230. }
  231. void _starpu_get_perf_model_dir_bus(char *path, size_t maxlen)
  232. {
  233. _starpu_get_perf_model_dir(path, maxlen);
  234. strncat(path, "/bus/", maxlen);
  235. }
  236. void _starpu_get_perf_model_dir_debug(char *path, size_t maxlen)
  237. {
  238. _starpu_get_perf_model_dir(path, maxlen);
  239. strncat(path, "/debug/", maxlen);
  240. }
  241. void _starpu_create_sampling_directory_if_needed(void)
  242. {
  243. if (!directory_existence_was_tested)
  244. {
  245. char perf_model_dir[256];
  246. _starpu_get_perf_model_dir(perf_model_dir, 256);
  247. /* The performance of the codelets are stored in
  248. * $STARPU_PERF_MODEL_DIR/codelets/ while those of the bus are stored in
  249. * $STARPU_PERF_MODEL_DIR/bus/ so that we don't have name collisions */
  250. /* Testing if a directory exists and creating it otherwise
  251. may not be safe: it is possible that the permission are
  252. changed in between. Instead, we create it and check if
  253. it already existed before */
  254. int ret;
  255. ret = _starpu_mkpath(perf_model_dir, S_IRWXU);
  256. if (ret == -1)
  257. {
  258. STARPU_ASSERT(errno == EEXIST);
  259. /* make sure that it is actually a directory */
  260. struct stat sb;
  261. stat(perf_model_dir, &sb);
  262. STARPU_ASSERT(S_ISDIR(sb.st_mode));
  263. }
  264. /* Per-task performance models */
  265. char perf_model_dir_codelets[256];
  266. _starpu_get_perf_model_dir_codelets(perf_model_dir_codelets, 256);
  267. ret = _starpu_mkpath(perf_model_dir_codelets, S_IRWXU);
  268. if (ret == -1)
  269. {
  270. STARPU_ASSERT(errno == EEXIST);
  271. /* make sure that it is actually a directory */
  272. struct stat sb;
  273. stat(perf_model_dir_codelets, &sb);
  274. STARPU_ASSERT(S_ISDIR(sb.st_mode));
  275. }
  276. /* Performance of the memory subsystem */
  277. char perf_model_dir_bus[256];
  278. _starpu_get_perf_model_dir_bus(perf_model_dir_bus, 256);
  279. ret = _starpu_mkpath(perf_model_dir_bus, S_IRWXU);
  280. if (ret == -1)
  281. {
  282. STARPU_ASSERT(errno == EEXIST);
  283. /* make sure that it is actually a directory */
  284. struct stat sb;
  285. stat(perf_model_dir_bus, &sb);
  286. STARPU_ASSERT(S_ISDIR(sb.st_mode));
  287. }
  288. /* Performance debug measurements */
  289. char perf_model_dir_debug[256];
  290. _starpu_get_perf_model_dir_debug(perf_model_dir_debug, 256);
  291. ret = _starpu_mkpath(perf_model_dir_debug, S_IRWXU);
  292. if (ret == -1)
  293. {
  294. STARPU_ASSERT(errno == EEXIST);
  295. /* make sure that it is actually a directory */
  296. struct stat sb;
  297. stat(perf_model_dir_debug, &sb);
  298. STARPU_ASSERT(S_ISDIR(sb.st_mode));
  299. }
  300. directory_existence_was_tested = 1;
  301. }
  302. }