perfmodel.c 14 KB

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