perfmodel.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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(struct starpu_jobq_s *q, struct starpu_task *task)
  111. {
  112. uint32_t memory_node = q->memory_node;
  113. unsigned nbuffers = task->cl->nbuffers;
  114. unsigned buffer;
  115. double penalty = 0.0;
  116. for (buffer = 0; buffer < nbuffers; buffer++)
  117. {
  118. starpu_data_handle handle = task->buffers[buffer].handle;
  119. starpu_access_mode mode = task->buffers[buffer].mode;
  120. if ((mode == STARPU_W) || (mode == STARPU_SCRATCH))
  121. continue;
  122. if (!_starpu_is_data_present_or_requested(handle, memory_node))
  123. {
  124. size_t size = _starpu_data_get_size(handle);
  125. uint32_t src_node = _starpu_select_src_node(handle);
  126. penalty += _starpu_predict_transfer_time(src_node, memory_node, size);
  127. }
  128. }
  129. return penalty;
  130. }
  131. static int directory_existence_was_tested = 0;
  132. void _starpu_get_perf_model_dir(char *path, size_t maxlen)
  133. {
  134. #ifdef STARPU_PERF_MODEL_DIR
  135. /* use the directory specified at configure time */
  136. snprintf(path, maxlen, "%s", STARPU_PERF_MODEL_DIR);
  137. #else
  138. /* by default, we use $HOME/.starpu/sampling */
  139. const char *home_path = getenv("HOME");
  140. if (!home_path)
  141. home_path = getenv("USERPROFILE");
  142. if (!home_path) {
  143. fprintf(stderr,"couldn't find a home place to put starpu data\n");
  144. STARPU_ABORT();
  145. }
  146. snprintf(path, maxlen, "%s/.starpu/sampling/", home_path);
  147. #endif
  148. }
  149. void _starpu_get_perf_model_dir_codelets(char *path, size_t maxlen)
  150. {
  151. _starpu_get_perf_model_dir(path, maxlen);
  152. strncat(path, "codelets/", maxlen);
  153. }
  154. void _starpu_get_perf_model_dir_bus(char *path, size_t maxlen)
  155. {
  156. _starpu_get_perf_model_dir(path, maxlen);
  157. strncat(path, "bus/", maxlen);
  158. }
  159. void _starpu_get_perf_model_dir_debug(char *path, size_t maxlen)
  160. {
  161. _starpu_get_perf_model_dir(path, maxlen);
  162. strncat(path, "debug/", maxlen);
  163. }
  164. void _starpu_create_sampling_directory_if_needed(void)
  165. {
  166. if (!directory_existence_was_tested)
  167. {
  168. char perf_model_dir[256];
  169. _starpu_get_perf_model_dir(perf_model_dir, 256);
  170. /* The performance of the codelets are stored in
  171. * $STARPU_PERF_MODEL_DIR/codelets/ while those of the bus are stored in
  172. * $STARPU_PERF_MODEL_DIR/bus/ so that we don't have name collisions */
  173. /* Testing if a directory exists and creating it otherwise
  174. may not be safe: it is possible that the permission are
  175. changed in between. Instead, we create it and check if
  176. it already existed before */
  177. int ret;
  178. ret = _starpu_mkpath(perf_model_dir, S_IRWXU);
  179. if (ret == -1)
  180. {
  181. STARPU_ASSERT(errno == EEXIST);
  182. /* make sure that it is actually a directory */
  183. struct stat sb;
  184. stat(perf_model_dir, &sb);
  185. STARPU_ASSERT(S_ISDIR(sb.st_mode));
  186. }
  187. /* Per-task performance models */
  188. char perf_model_dir_codelets[256];
  189. _starpu_get_perf_model_dir_codelets(perf_model_dir_codelets, 256);
  190. ret = _starpu_mkpath(perf_model_dir_codelets, S_IRWXU);
  191. if (ret == -1)
  192. {
  193. STARPU_ASSERT(errno == EEXIST);
  194. /* make sure that it is actually a directory */
  195. struct stat sb;
  196. stat(perf_model_dir_codelets, &sb);
  197. STARPU_ASSERT(S_ISDIR(sb.st_mode));
  198. }
  199. /* Performance of the memory subsystem */
  200. char perf_model_dir_bus[256];
  201. _starpu_get_perf_model_dir_bus(perf_model_dir_bus, 256);
  202. ret = _starpu_mkpath(perf_model_dir_bus, S_IRWXU);
  203. if (ret == -1)
  204. {
  205. STARPU_ASSERT(errno == EEXIST);
  206. /* make sure that it is actually a directory */
  207. struct stat sb;
  208. stat(perf_model_dir_bus, &sb);
  209. STARPU_ASSERT(S_ISDIR(sb.st_mode));
  210. }
  211. /* Performance debug measurements */
  212. char perf_model_dir_debug[256];
  213. _starpu_get_perf_model_dir_debug(perf_model_dir_debug, 256);
  214. ret = _starpu_mkpath(perf_model_dir_debug, S_IRWXU);
  215. if (ret == -1)
  216. {
  217. STARPU_ASSERT(errno == EEXIST);
  218. /* make sure that it is actually a directory */
  219. struct stat sb;
  220. stat(perf_model_dir_debug, &sb);
  221. STARPU_ASSERT(S_ISDIR(sb.st_mode));
  222. }
  223. directory_existence_was_tested = 1;
  224. }
  225. }