perfmodel.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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. unsigned int node, cpu_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;
  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. if (arch < STARPU_CUDA_DEFAULT)
  220. node = cpu_node;
  221. #ifdef STARPU_USE_CUDA
  222. else if (arch >= STARPU_CUDA_DEFAULT && arch < STARPU_OPENCL_DEFAULT)
  223. node = cuda_node;
  224. #endif
  225. #ifdef STARPU_USE_OPENCL
  226. else if (arch >= STARPU_OPENCL_DEFAULT && arch < STARPU_GORDON_DEFAULT)
  227. node = opencl_node;
  228. #endif
  229. else
  230. STARPU_ASSERT(0);
  231. if (!_starpu_handle_needs_conversion_task(handle, node))
  232. continue;
  233. conversion_task = _starpu_create_conversion_task(handle, node);
  234. sum += starpu_task_expected_length(conversion_task, arch, nimpl);
  235. handle->refcnt--;
  236. handle->busy_count--;
  237. starpu_task_deinit(conversion_task);
  238. free(conversion_task);
  239. }
  240. return sum;
  241. }
  242. /* Predict the transfer time (in µs) to move a handle to a memory node */
  243. double starpu_data_expected_transfer_time(starpu_data_handle_t handle, unsigned memory_node, enum starpu_access_mode mode)
  244. {
  245. /* If we don't need to read the content of the handle */
  246. if (!(mode & STARPU_R))
  247. return 0.0;
  248. if (_starpu_is_data_present_or_requested(handle, memory_node))
  249. return 0.0;
  250. size_t size = _starpu_data_get_size(handle);
  251. /* XXX in case we have an abstract piece of data (eg. with the
  252. * void interface, this does not introduce any overhead, and we
  253. * don't even want to consider the latency that is not
  254. * relevant). */
  255. if (size == 0)
  256. return 0.0;
  257. uint32_t src_node = _starpu_select_src_node(handle, memory_node);
  258. return _starpu_predict_transfer_time(src_node, memory_node, size);
  259. }
  260. /* Data transfer performance modeling */
  261. double starpu_task_expected_data_transfer_time(uint32_t memory_node, struct starpu_task *task)
  262. {
  263. unsigned nbuffers = task->cl->nbuffers;
  264. unsigned buffer;
  265. double penalty = 0.0;
  266. for (buffer = 0; buffer < nbuffers; buffer++)
  267. {
  268. starpu_data_handle_t handle = task->handles[buffer];
  269. enum starpu_access_mode mode = task->cl->modes[buffer];
  270. penalty += starpu_data_expected_transfer_time(handle, memory_node, mode);
  271. }
  272. return penalty;
  273. }
  274. /* Return the expected duration of the entire task bundle in µs */
  275. double _starpu_task_bundle_expected_length(starpu_task_bundle_t bundle, enum starpu_perf_archtype arch, unsigned nimpl)
  276. {
  277. double expected_length = 0.0;
  278. /* We expect the length of the bundle the be the sum of the different tasks length. */
  279. _STARPU_PTHREAD_MUTEX_LOCK(&bundle->mutex);
  280. struct _starpu_task_bundle_entry *entry;
  281. entry = bundle->list;
  282. while (entry)
  283. {
  284. double task_length = starpu_task_expected_length(entry->task, arch, nimpl);
  285. /* In case the task is not calibrated, we consider the task
  286. * ends immediately. */
  287. if (task_length > 0.0)
  288. expected_length += task_length;
  289. entry = entry->next;
  290. }
  291. _STARPU_PTHREAD_MUTEX_UNLOCK(&bundle->mutex);
  292. return expected_length;
  293. }
  294. /* Return the expected power consumption of the entire task bundle in J */
  295. double _starpu_task_bundle_expected_power(starpu_task_bundle_t bundle, enum starpu_perf_archtype arch, unsigned nimpl)
  296. {
  297. double expected_power = 0.0;
  298. /* We expect total consumption of the bundle the be the sum of the different tasks consumption. */
  299. _STARPU_PTHREAD_MUTEX_LOCK(&bundle->mutex);
  300. struct _starpu_task_bundle_entry *entry;
  301. entry = bundle->list;
  302. while (entry)
  303. {
  304. double task_power = starpu_task_expected_power(entry->task, arch, nimpl);
  305. /* In case the task is not calibrated, we consider the task
  306. * ends immediately. */
  307. if (task_power > 0.0)
  308. expected_power += task_power;
  309. entry = entry->next;
  310. }
  311. _STARPU_PTHREAD_MUTEX_UNLOCK(&bundle->mutex);
  312. return expected_power;
  313. }
  314. /* Return the time (in µs) expected to transfer all data used within the bundle */
  315. double _starpu_task_bundle_expected_data_transfer_time(starpu_task_bundle_t bundle, unsigned memory_node)
  316. {
  317. _STARPU_PTHREAD_MUTEX_LOCK(&bundle->mutex);
  318. struct _starpu_handle_list *handles = NULL;
  319. /* We list all the handle that are accessed within the bundle. */
  320. /* For each task in the bundle */
  321. struct _starpu_task_bundle_entry *entry = bundle->list;
  322. while (entry)
  323. {
  324. struct starpu_task *task = entry->task;
  325. if (task->cl)
  326. {
  327. unsigned b;
  328. for (b = 0; b < task->cl->nbuffers; b++)
  329. {
  330. starpu_data_handle_t handle = task->handles[b];
  331. enum starpu_access_mode mode = task->cl->modes[b];
  332. if (!(mode & STARPU_R))
  333. continue;
  334. /* Insert the handle in the sorted list in case
  335. * it's not already in that list. */
  336. _insertion_handle_sorted(&handles, handle, mode);
  337. }
  338. }
  339. entry = entry->next;
  340. }
  341. _STARPU_PTHREAD_MUTEX_UNLOCK(&bundle->mutex);
  342. /* Compute the sum of data transfer time, and destroy the list */
  343. double total_exp = 0.0;
  344. while (handles)
  345. {
  346. struct _starpu_handle_list *current = handles;
  347. handles = handles->next;
  348. double exp;
  349. exp = starpu_data_expected_transfer_time(current->handle, memory_node, current->mode);
  350. total_exp += exp;
  351. free(current);
  352. }
  353. return total_exp;
  354. }
  355. static int directory_existence_was_tested = 0;
  356. void _starpu_get_perf_model_dir(char *path, size_t maxlen)
  357. {
  358. #ifdef STARPU_PERF_MODEL_DIR
  359. /* use the directory specified at configure time */
  360. snprintf(path, maxlen, "%s", STARPU_PERF_MODEL_DIR);
  361. #else
  362. /* by default, we use $HOME/.starpu/sampling */
  363. const char *home_path = getenv("STARPU_HOME");
  364. if (!home_path)
  365. home_path = getenv("HOME");
  366. if (!home_path)
  367. home_path = getenv("USERPROFILE");
  368. if (!home_path)
  369. _STARPU_ERROR("couldn't find a home place to put starpu data\n");
  370. snprintf(path, maxlen, "%s/.starpu/sampling/", home_path);
  371. #endif
  372. }
  373. void _starpu_get_perf_model_dir_codelets(char *path, size_t maxlen)
  374. {
  375. _starpu_get_perf_model_dir(path, maxlen);
  376. strncat(path, "/codelets/", maxlen);
  377. }
  378. void _starpu_get_perf_model_dir_bus(char *path, size_t maxlen)
  379. {
  380. _starpu_get_perf_model_dir(path, maxlen);
  381. strncat(path, "/bus/", maxlen);
  382. }
  383. void _starpu_get_perf_model_dir_debug(char *path, size_t maxlen)
  384. {
  385. _starpu_get_perf_model_dir(path, maxlen);
  386. strncat(path, "/debug/", maxlen);
  387. }
  388. void _starpu_create_sampling_directory_if_needed(void)
  389. {
  390. if (!directory_existence_was_tested)
  391. {
  392. char perf_model_dir[256];
  393. _starpu_get_perf_model_dir(perf_model_dir, 256);
  394. /* The performance of the codelets are stored in
  395. * $STARPU_PERF_MODEL_DIR/codelets/ while those of the bus are stored in
  396. * $STARPU_PERF_MODEL_DIR/bus/ so that we don't have name collisions */
  397. /* Testing if a directory exists and creating it otherwise
  398. may not be safe: it is possible that the permission are
  399. changed in between. Instead, we create it and check if
  400. it already existed before */
  401. int ret;
  402. ret = _starpu_mkpath(perf_model_dir, S_IRWXU);
  403. if (ret == -1)
  404. {
  405. if (errno != EEXIST) {
  406. fprintf(stderr,"Error making starpu directory %s:\n", perf_model_dir);
  407. perror("mkdir");
  408. STARPU_ASSERT(0);
  409. }
  410. /* make sure that it is actually a directory */
  411. struct stat sb;
  412. stat(perf_model_dir, &sb);
  413. if (!S_ISDIR(sb.st_mode)) {
  414. fprintf(stderr,"Error: %s is not a directory:\n", perf_model_dir);
  415. STARPU_ASSERT(0);
  416. }
  417. }
  418. /* Per-task performance models */
  419. char perf_model_dir_codelets[256];
  420. _starpu_get_perf_model_dir_codelets(perf_model_dir_codelets, 256);
  421. ret = _starpu_mkpath(perf_model_dir_codelets, S_IRWXU);
  422. if (ret == -1)
  423. {
  424. if (errno != EEXIST) {
  425. fprintf(stderr,"Error making starpu directory %s:\n", perf_model_dir);
  426. perror("mkdir");
  427. STARPU_ASSERT(0);
  428. }
  429. /* make sure that it is actually a directory */
  430. struct stat sb;
  431. stat(perf_model_dir_codelets, &sb);
  432. if (!S_ISDIR(sb.st_mode)) {
  433. fprintf(stderr,"Error: %s is not a directory:\n", perf_model_dir);
  434. STARPU_ASSERT(0);
  435. }
  436. }
  437. /* Performance of the memory subsystem */
  438. char perf_model_dir_bus[256];
  439. _starpu_get_perf_model_dir_bus(perf_model_dir_bus, 256);
  440. ret = _starpu_mkpath(perf_model_dir_bus, S_IRWXU);
  441. if (ret == -1)
  442. {
  443. if (errno != EEXIST) {
  444. fprintf(stderr,"Error making starpu directory %s:\n", perf_model_dir);
  445. perror("mkdir");
  446. STARPU_ASSERT(0);
  447. }
  448. /* make sure that it is actually a directory */
  449. struct stat sb;
  450. stat(perf_model_dir_bus, &sb);
  451. if (!S_ISDIR(sb.st_mode)) {
  452. fprintf(stderr,"Error: %s is not a directory:\n", perf_model_dir);
  453. STARPU_ASSERT(0);
  454. }
  455. }
  456. /* Performance debug measurements */
  457. char perf_model_dir_debug[256];
  458. _starpu_get_perf_model_dir_debug(perf_model_dir_debug, 256);
  459. ret = _starpu_mkpath(perf_model_dir_debug, S_IRWXU);
  460. if (ret == -1)
  461. {
  462. if (errno != EEXIST) {
  463. fprintf(stderr,"Error making starpu directory %s:\n", perf_model_dir);
  464. perror("mkdir");
  465. STARPU_ASSERT(0);
  466. }
  467. /* make sure that it is actually a directory */
  468. struct stat sb;
  469. stat(perf_model_dir_debug, &sb);
  470. if (!S_ISDIR(sb.st_mode)) {
  471. fprintf(stderr,"Error: %s is not a directory:\n", perf_model_dir);
  472. STARPU_ASSERT(0);
  473. }
  474. }
  475. directory_existence_was_tested = 1;
  476. }
  477. }