perfmodel.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * StarPU
  3. * Copyright (C) Université Bordeaux 1, CNRS 2008-2009 (see AUTHORS file)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <starpu.h>
  17. #include <starpu_profiling.h>
  18. #include <common/config.h>
  19. #include <common/utils.h>
  20. #include <unistd.h>
  21. #include <sys/stat.h>
  22. #include <core/perfmodel/perfmodel.h>
  23. #include <core/jobs.h>
  24. #include <core/workers.h>
  25. #include <datawizard/datawizard.h>
  26. /* This flag indicates whether performance models should be calibrated or not.
  27. * 0: models need not be calibrated
  28. * 1: models must be calibrated
  29. * 2: models must be calibrated, existing models are overwritten.
  30. */
  31. static unsigned calibrate_flag = 0;
  32. void _starpu_set_calibrate_flag(unsigned val)
  33. {
  34. calibrate_flag = val;
  35. }
  36. unsigned _starpu_get_calibrate_flag(void)
  37. {
  38. return calibrate_flag;
  39. }
  40. /*
  41. * PER ARCH model
  42. */
  43. static double per_arch_task_expected_length(struct starpu_perfmodel_t *model, enum starpu_perf_archtype arch, struct starpu_task *task)
  44. {
  45. double exp = -1.0;
  46. double (*per_arch_cost_model)(struct starpu_buffer_descr_t *);
  47. if (!model->is_loaded)
  48. {
  49. model->benchmarking = _starpu_get_calibrate_flag();
  50. _starpu_register_model(model);
  51. model->is_loaded = 1;
  52. }
  53. per_arch_cost_model = model->per_arch[arch].cost_model;
  54. if (per_arch_cost_model)
  55. exp = per_arch_cost_model(task->buffers);
  56. return exp;
  57. }
  58. /*
  59. * Common model
  60. */
  61. static double common_task_expected_length(struct starpu_perfmodel_t *model, int workerid, struct starpu_task *task)
  62. {
  63. double exp;
  64. if (model->cost_model) {
  65. float alpha;
  66. exp = model->cost_model(task->buffers);
  67. enum starpu_archtype arch = starpu_worker_get_type(workerid);
  68. switch (arch) {
  69. case STARPU_CPU_WORKER:
  70. alpha = STARPU_CPU_ALPHA;
  71. break;
  72. case STARPU_CUDA_WORKER:
  73. alpha = STARPU_CUDA_ALPHA;
  74. break;
  75. case STARPU_OPENCL_WORKER:
  76. alpha = STARPU_OPENCL_ALPHA;
  77. break;
  78. default:
  79. /* perhaps there are various worker types on that queue */
  80. alpha = 1.0; // this value is not significant ...
  81. break;
  82. }
  83. STARPU_ASSERT(alpha != 0.0f);
  84. return (exp/alpha);
  85. }
  86. return -1.0;
  87. }
  88. double _starpu_job_expected_length(int workerid, struct starpu_job_s *j, enum starpu_perf_archtype arch)
  89. {
  90. struct starpu_task *task = j->task;
  91. struct starpu_perfmodel_t *model = task->cl->model;
  92. if (model) {
  93. switch (model->type) {
  94. case STARPU_PER_ARCH:
  95. return per_arch_task_expected_length(model, arch, task);
  96. case STARPU_COMMON:
  97. return common_task_expected_length(model, workerid, task);
  98. case STARPU_HISTORY_BASED:
  99. return _starpu_history_based_job_expected_length(model, arch, j);
  100. case STARPU_REGRESSION_BASED:
  101. return _starpu_regression_based_job_expected_length(model, arch, j);
  102. default:
  103. STARPU_ABORT();
  104. };
  105. }
  106. /* no model was found */
  107. return 0.0;
  108. }
  109. /* Data transfer performance modeling */
  110. double _starpu_data_expected_penalty(uint32_t memory_node, struct starpu_task *task)
  111. {
  112. unsigned nbuffers = task->cl->nbuffers;
  113. unsigned buffer;
  114. double penalty = 0.0;
  115. for (buffer = 0; buffer < nbuffers; buffer++)
  116. {
  117. starpu_data_handle handle = task->buffers[buffer].handle;
  118. starpu_access_mode mode = task->buffers[buffer].mode;
  119. if ((mode == STARPU_W) || (mode == STARPU_SCRATCH))
  120. continue;
  121. if (!_starpu_is_data_present_or_requested(handle, memory_node))
  122. {
  123. size_t size = _starpu_data_get_size(handle);
  124. uint32_t src_node = _starpu_select_src_node(handle);
  125. penalty += _starpu_predict_transfer_time(src_node, memory_node, size);
  126. }
  127. }
  128. return penalty;
  129. }
  130. static int directory_existence_was_tested = 0;
  131. void _starpu_get_perf_model_dir(char *path, size_t maxlen)
  132. {
  133. #ifdef STARPU_PERF_MODEL_DIR
  134. /* use the directory specified at configure time */
  135. snprintf(path, maxlen, "%s", STARPU_PERF_MODEL_DIR);
  136. #else
  137. /* by default, we use $HOME/.starpu/sampling */
  138. const char *home_path = getenv("HOME");
  139. if (!home_path)
  140. home_path = getenv("USERPROFILE");
  141. if (!home_path) {
  142. fprintf(stderr,"couldn't find a home place to put starpu data\n");
  143. STARPU_ABORT();
  144. }
  145. snprintf(path, maxlen, "%s/.starpu/sampling/", home_path);
  146. #endif
  147. }
  148. void _starpu_get_perf_model_dir_codelets(char *path, size_t maxlen)
  149. {
  150. _starpu_get_perf_model_dir(path, maxlen);
  151. strncat(path, "codelets/", maxlen);
  152. }
  153. void _starpu_get_perf_model_dir_bus(char *path, size_t maxlen)
  154. {
  155. _starpu_get_perf_model_dir(path, maxlen);
  156. strncat(path, "bus/", maxlen);
  157. }
  158. void _starpu_get_perf_model_dir_debug(char *path, size_t maxlen)
  159. {
  160. _starpu_get_perf_model_dir(path, maxlen);
  161. strncat(path, "debug/", maxlen);
  162. }
  163. void _starpu_create_sampling_directory_if_needed(void)
  164. {
  165. if (!directory_existence_was_tested)
  166. {
  167. char perf_model_dir[256];
  168. _starpu_get_perf_model_dir(perf_model_dir, 256);
  169. /* The performance of the codelets are stored in
  170. * $STARPU_PERF_MODEL_DIR/codelets/ while those of the bus are stored in
  171. * $STARPU_PERF_MODEL_DIR/bus/ so that we don't have name collisions */
  172. /* Testing if a directory exists and creating it otherwise
  173. may not be safe: it is possible that the permission are
  174. changed in between. Instead, we create it and check if
  175. it already existed before */
  176. int ret;
  177. ret = _starpu_mkpath(perf_model_dir, S_IRWXU);
  178. if (ret == -1)
  179. {
  180. STARPU_ASSERT(errno == EEXIST);
  181. /* make sure that it is actually a directory */
  182. struct stat sb;
  183. stat(perf_model_dir, &sb);
  184. STARPU_ASSERT(S_ISDIR(sb.st_mode));
  185. }
  186. /* Per-task performance models */
  187. char perf_model_dir_codelets[256];
  188. _starpu_get_perf_model_dir_codelets(perf_model_dir_codelets, 256);
  189. ret = _starpu_mkpath(perf_model_dir_codelets, S_IRWXU);
  190. if (ret == -1)
  191. {
  192. STARPU_ASSERT(errno == EEXIST);
  193. /* make sure that it is actually a directory */
  194. struct stat sb;
  195. stat(perf_model_dir_codelets, &sb);
  196. STARPU_ASSERT(S_ISDIR(sb.st_mode));
  197. }
  198. /* Performance of the memory subsystem */
  199. char perf_model_dir_bus[256];
  200. _starpu_get_perf_model_dir_bus(perf_model_dir_bus, 256);
  201. ret = _starpu_mkpath(perf_model_dir_bus, S_IRWXU);
  202. if (ret == -1)
  203. {
  204. STARPU_ASSERT(errno == EEXIST);
  205. /* make sure that it is actually a directory */
  206. struct stat sb;
  207. stat(perf_model_dir_bus, &sb);
  208. STARPU_ASSERT(S_ISDIR(sb.st_mode));
  209. }
  210. /* Performance debug measurements */
  211. char perf_model_dir_debug[256];
  212. _starpu_get_perf_model_dir_debug(perf_model_dir_debug, 256);
  213. ret = _starpu_mkpath(perf_model_dir_debug, S_IRWXU);
  214. if (ret == -1)
  215. {
  216. STARPU_ASSERT(errno == EEXIST);
  217. /* make sure that it is actually a directory */
  218. struct stat sb;
  219. stat(perf_model_dir_debug, &sb);
  220. STARPU_ASSERT(S_ISDIR(sb.st_mode));
  221. }
  222. directory_existence_was_tested = 1;
  223. }
  224. }