perfmodel.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2013 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012 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 *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 = NAN;
  63. double (*per_arch_cost_function)(struct starpu_task *task, enum starpu_perf_archtype arch, unsigned nimpl);
  64. double (*per_arch_cost_model)(struct starpu_buffer_descr *);
  65. per_arch_cost_function = model->per_arch[arch][nimpl].cost_function;
  66. per_arch_cost_model = model->per_arch[arch][nimpl].cost_model;
  67. if (per_arch_cost_function)
  68. exp = per_arch_cost_function(task, arch, nimpl);
  69. else if (per_arch_cost_model)
  70. exp = per_arch_cost_model(task->buffers);
  71. return exp;
  72. }
  73. /*
  74. * Common model
  75. */
  76. double starpu_worker_get_relative_speedup(enum starpu_perf_archtype perf_archtype)
  77. {
  78. if (perf_archtype < STARPU_CUDA_DEFAULT)
  79. {
  80. return _STARPU_CPU_ALPHA * (perf_archtype + 1);
  81. }
  82. else if (perf_archtype < STARPU_OPENCL_DEFAULT)
  83. {
  84. return _STARPU_CUDA_ALPHA;
  85. }
  86. else if (perf_archtype < STARPU_GORDON_DEFAULT)
  87. {
  88. return _STARPU_OPENCL_ALPHA;
  89. }
  90. else if (perf_archtype < STARPU_NARCH_VARIATIONS)
  91. {
  92. /* Gordon value */
  93. return _STARPU_GORDON_ALPHA;
  94. }
  95. STARPU_ABORT();
  96. /* Never reached ! */
  97. return NAN;
  98. }
  99. static double common_task_expected_perf(struct starpu_perfmodel *model, enum starpu_perf_archtype arch, struct starpu_task *task, unsigned nimpl)
  100. {
  101. double exp;
  102. double alpha;
  103. if (model->cost_function)
  104. {
  105. exp = model->cost_function(task, nimpl);
  106. alpha = starpu_worker_get_relative_speedup(arch);
  107. STARPU_ASSERT(!_STARPU_IS_ZERO(alpha));
  108. return (exp/alpha);
  109. }
  110. else if (model->cost_model)
  111. {
  112. exp = model->cost_model(task->buffers);
  113. alpha = starpu_worker_get_relative_speedup(arch);
  114. STARPU_ASSERT(!_STARPU_IS_ZERO(alpha));
  115. return (exp/alpha);
  116. }
  117. return NAN;
  118. }
  119. void _starpu_load_perfmodel(struct starpu_perfmodel *model)
  120. {
  121. if (!model || model->is_loaded)
  122. return;
  123. int load_model = _starpu_register_model(model);
  124. if (!load_model)
  125. return;
  126. switch (model->type)
  127. {
  128. case STARPU_PER_ARCH:
  129. _starpu_load_per_arch_based_model(model);
  130. break;
  131. case STARPU_COMMON:
  132. _starpu_load_common_based_model(model);
  133. break;
  134. case STARPU_HISTORY_BASED:
  135. case STARPU_NL_REGRESSION_BASED:
  136. _starpu_load_history_based_model(model, 1);
  137. break;
  138. case STARPU_REGRESSION_BASED:
  139. _starpu_load_history_based_model(model, 0);
  140. break;
  141. default:
  142. STARPU_ABORT();
  143. }
  144. model->is_loaded = 1;
  145. }
  146. static double starpu_model_expected_perf(struct starpu_task *task, struct starpu_perfmodel *model, enum starpu_perf_archtype arch, unsigned nimpl)
  147. {
  148. if (model)
  149. {
  150. struct _starpu_job *j = _starpu_get_job_associated_to_task(task);
  151. switch (model->type)
  152. {
  153. case STARPU_PER_ARCH:
  154. return per_arch_task_expected_perf(model, arch, task, nimpl);
  155. case STARPU_COMMON:
  156. return common_task_expected_perf(model, arch, task, nimpl);
  157. case STARPU_HISTORY_BASED:
  158. return _starpu_history_based_job_expected_perf(model, arch, j, nimpl);
  159. case STARPU_REGRESSION_BASED:
  160. return _starpu_regression_based_job_expected_perf(model, arch, j, nimpl);
  161. case STARPU_NL_REGRESSION_BASED:
  162. return _starpu_non_linear_regression_based_job_expected_perf(model, arch, j,nimpl);
  163. default:
  164. STARPU_ABORT();
  165. }
  166. }
  167. /* no model was found */
  168. return 0.0;
  169. }
  170. double starpu_task_expected_length(struct starpu_task *task, enum starpu_perf_archtype arch, unsigned nimpl)
  171. {
  172. return starpu_model_expected_perf(task, task->cl->model, arch, nimpl);
  173. }
  174. double starpu_task_expected_power(struct starpu_task *task, enum starpu_perf_archtype arch, unsigned nimpl)
  175. {
  176. return starpu_model_expected_perf(task, task->cl->power_model, arch, nimpl);
  177. }
  178. double starpu_task_expected_conversion_time(struct starpu_task *task,
  179. enum starpu_perf_archtype arch,
  180. unsigned nimpl)
  181. {
  182. unsigned i;
  183. double sum = 0.0;
  184. int node;
  185. /* We need to get one node per archtype. This is kinda ugly,
  186. * but it does the job.
  187. * XXX : Should we return 0 if there are no devices ?
  188. * (err != 1 && err != -ERANGE)
  189. */
  190. #ifdef STARPU_USE_CPU
  191. int cpu_worker, cpu_node;
  192. cpu_worker = starpu_worker_get_by_type(STARPU_CPU_WORKER, 0);
  193. if (cpu_worker == -1)
  194. return 0.0;
  195. cpu_node = starpu_worker_get_memory_node(cpu_worker);
  196. #endif
  197. #ifdef STARPU_USE_CUDA
  198. int cuda_worker, cuda_node;
  199. cuda_worker = starpu_worker_get_by_type(STARPU_CUDA_WORKER, 0);
  200. if (cuda_worker == -1)
  201. return 0.0;
  202. cuda_node = starpu_worker_get_memory_node(cuda_worker);
  203. #endif
  204. #ifdef STARPU_USE_OPENCL
  205. int opencl_worker, opencl_node;
  206. opencl_worker = starpu_worker_get_by_type(STARPU_OPENCL_WORKER, 0);
  207. if (opencl_worker == -1)
  208. return 0.0;
  209. opencl_node = starpu_worker_get_memory_node(opencl_worker);
  210. #endif
  211. for (i = 0; i < task->cl->nbuffers; i++)
  212. {
  213. starpu_data_handle_t handle;
  214. struct starpu_task *conversion_task;
  215. handle = task->handles[i];
  216. if (!_starpu_data_is_multiformat_handle(handle))
  217. continue;
  218. node = -EINVAL;
  219. #ifdef STARPU_USE_CPU
  220. if (arch < STARPU_CUDA_DEFAULT)
  221. node = cpu_node;
  222. #endif
  223. #ifdef STARPU_USE_CUDA
  224. if (arch >= STARPU_CUDA_DEFAULT && arch < STARPU_OPENCL_DEFAULT)
  225. node = cuda_node;
  226. #endif
  227. #ifdef STARPU_USE_OPENCL
  228. if (arch >= STARPU_OPENCL_DEFAULT && arch < STARPU_GORDON_DEFAULT)
  229. node = opencl_node;
  230. #endif
  231. if (node == -EINVAL)
  232. STARPU_ABORT();
  233. if (!_starpu_handle_needs_conversion_task(handle, node))
  234. continue;
  235. conversion_task = _starpu_create_conversion_task(handle, node);
  236. sum += starpu_task_expected_length(conversion_task, arch, nimpl);
  237. _starpu_spin_lock(&handle->header_lock);
  238. handle->refcnt--;
  239. handle->busy_count--;
  240. _starpu_spin_unlock(&handle->header_lock);
  241. starpu_task_clean(conversion_task);
  242. free(conversion_task);
  243. }
  244. return sum;
  245. }
  246. /* Predict the transfer time (in µs) to move a handle to a memory node */
  247. double starpu_data_expected_transfer_time(starpu_data_handle_t handle, unsigned memory_node, enum starpu_access_mode mode)
  248. {
  249. /* If we don't need to read the content of the handle */
  250. if (!(mode & STARPU_R))
  251. return 0.0;
  252. if (_starpu_is_data_present_or_requested(handle, memory_node))
  253. return 0.0;
  254. size_t size = _starpu_data_get_size(handle);
  255. /* XXX in case we have an abstract piece of data (eg. with the
  256. * void interface, this does not introduce any overhead, and we
  257. * don't even want to consider the latency that is not
  258. * relevant). */
  259. if (size == 0)
  260. return 0.0;
  261. uint32_t src_node = _starpu_select_src_node(handle, memory_node);
  262. return _starpu_predict_transfer_time(src_node, memory_node, size);
  263. }
  264. /* Data transfer performance modeling */
  265. double starpu_task_expected_data_transfer_time(uint32_t memory_node, struct starpu_task *task)
  266. {
  267. unsigned nbuffers = task->cl->nbuffers;
  268. unsigned buffer;
  269. double penalty = 0.0;
  270. for (buffer = 0; buffer < nbuffers; buffer++)
  271. {
  272. starpu_data_handle_t handle = task->handles[buffer];
  273. enum starpu_access_mode mode = task->cl->modes[buffer];
  274. penalty += starpu_data_expected_transfer_time(handle, memory_node, mode);
  275. }
  276. return penalty;
  277. }
  278. /* Return the expected duration of the entire task bundle in µs */
  279. double starpu_task_bundle_expected_length(starpu_task_bundle_t bundle, enum starpu_perf_archtype arch, unsigned nimpl)
  280. {
  281. double expected_length = 0.0;
  282. /* We expect the length of the bundle the be the sum of the different tasks length. */
  283. _STARPU_PTHREAD_MUTEX_LOCK(&bundle->mutex);
  284. struct _starpu_task_bundle_entry *entry;
  285. entry = bundle->list;
  286. while (entry)
  287. {
  288. if(!entry->task->scheduled)
  289. {
  290. double task_length = starpu_task_expected_length(entry->task, arch, nimpl);
  291. /* In case the task is not calibrated, we consider the task
  292. * ends immediately. */
  293. if (task_length > 0.0)
  294. expected_length += task_length;
  295. }
  296. entry = entry->next;
  297. }
  298. _STARPU_PTHREAD_MUTEX_UNLOCK(&bundle->mutex);
  299. return expected_length;
  300. }
  301. /* Return the expected power consumption of the entire task bundle in J */
  302. double starpu_task_bundle_expected_power(starpu_task_bundle_t bundle, enum starpu_perf_archtype arch, unsigned nimpl)
  303. {
  304. double expected_power = 0.0;
  305. /* We expect total consumption of the bundle the be the sum of the different tasks consumption. */
  306. _STARPU_PTHREAD_MUTEX_LOCK(&bundle->mutex);
  307. struct _starpu_task_bundle_entry *entry;
  308. entry = bundle->list;
  309. while (entry)
  310. {
  311. double task_power = starpu_task_expected_power(entry->task, arch, nimpl);
  312. /* In case the task is not calibrated, we consider the task
  313. * ends immediately. */
  314. if (task_power > 0.0)
  315. expected_power += task_power;
  316. entry = entry->next;
  317. }
  318. _STARPU_PTHREAD_MUTEX_UNLOCK(&bundle->mutex);
  319. return expected_power;
  320. }
  321. /* Return the time (in µs) expected to transfer all data used within the bundle */
  322. double starpu_task_bundle_expected_data_transfer_time(starpu_task_bundle_t bundle, unsigned memory_node)
  323. {
  324. _STARPU_PTHREAD_MUTEX_LOCK(&bundle->mutex);
  325. struct _starpu_handle_list *handles = NULL;
  326. /* We list all the handle that are accessed within the bundle. */
  327. /* For each task in the bundle */
  328. struct _starpu_task_bundle_entry *entry = bundle->list;
  329. while (entry)
  330. {
  331. struct starpu_task *task = entry->task;
  332. if (task->cl)
  333. {
  334. unsigned b;
  335. for (b = 0; b < task->cl->nbuffers; b++)
  336. {
  337. starpu_data_handle_t handle = task->handles[b];
  338. enum starpu_access_mode mode = task->cl->modes[b];
  339. if (!(mode & STARPU_R))
  340. continue;
  341. /* Insert the handle in the sorted list in case
  342. * it's not already in that list. */
  343. _insertion_handle_sorted(&handles, handle, mode);
  344. }
  345. }
  346. entry = entry->next;
  347. }
  348. _STARPU_PTHREAD_MUTEX_UNLOCK(&bundle->mutex);
  349. /* Compute the sum of data transfer time, and destroy the list */
  350. double total_exp = 0.0;
  351. while (handles)
  352. {
  353. struct _starpu_handle_list *current = handles;
  354. handles = handles->next;
  355. double exp;
  356. exp = starpu_data_expected_transfer_time(current->handle, memory_node, current->mode);
  357. total_exp += exp;
  358. free(current);
  359. }
  360. return total_exp;
  361. }
  362. static int directory_existence_was_tested = 0;
  363. void _starpu_get_perf_model_dir(char *path, size_t maxlen)
  364. {
  365. #ifdef STARPU_PERF_MODEL_DIR
  366. /* use the directory specified at configure time */
  367. snprintf(path, maxlen, "%s", STARPU_PERF_MODEL_DIR);
  368. #else
  369. snprintf(path, maxlen, "%s/.starpu/sampling/", _starpu_get_home_path());
  370. #endif
  371. }
  372. void _starpu_get_perf_model_dir_codelets(char *path, size_t maxlen)
  373. {
  374. _starpu_get_perf_model_dir(path, maxlen);
  375. strncat(path, "/codelets/", maxlen);
  376. }
  377. void _starpu_get_perf_model_dir_bus(char *path, size_t maxlen)
  378. {
  379. _starpu_get_perf_model_dir(path, maxlen);
  380. strncat(path, "/bus/", maxlen);
  381. }
  382. void _starpu_get_perf_model_dir_debug(char *path, size_t maxlen)
  383. {
  384. _starpu_get_perf_model_dir(path, maxlen);
  385. strncat(path, "/debug/", maxlen);
  386. }
  387. void _starpu_create_sampling_directory_if_needed(void)
  388. {
  389. if (!directory_existence_was_tested)
  390. {
  391. char perf_model_dir[256];
  392. _starpu_get_perf_model_dir(perf_model_dir, 256);
  393. /* The performance of the codelets are stored in
  394. * $STARPU_PERF_MODEL_DIR/codelets/ while those of the bus are stored in
  395. * $STARPU_PERF_MODEL_DIR/bus/ so that we don't have name collisions */
  396. /* Testing if a directory exists and creating it otherwise
  397. may not be safe: it is possible that the permission are
  398. changed in between. Instead, we create it and check if
  399. it already existed before */
  400. _starpu_mkpath_and_check(perf_model_dir, S_IRWXU);
  401. /* Per-task performance models */
  402. char perf_model_dir_codelets[256];
  403. _starpu_get_perf_model_dir_codelets(perf_model_dir_codelets, 256);
  404. _starpu_mkpath_and_check(perf_model_dir_codelets, S_IRWXU);
  405. /* Performance of the memory subsystem */
  406. char perf_model_dir_bus[256];
  407. _starpu_get_perf_model_dir_bus(perf_model_dir_bus, 256);
  408. _starpu_mkpath_and_check(perf_model_dir_bus, S_IRWXU);
  409. /* Performance debug measurements */
  410. char perf_model_dir_debug[256];
  411. _starpu_get_perf_model_dir_debug(perf_model_dir_debug, 256);
  412. _starpu_mkpath_and_check(perf_model_dir_debug, S_IRWXU);
  413. directory_existence_was_tested = 1;
  414. }
  415. }