perfmodel.c 6.9 KB

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