starpu_perfmodel.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. * Copyright (C) 2011 Télécom-SudParis
  5. * Copyright (C) 2013 Thibaut Lambert
  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. #ifndef __STARPU_PERFMODEL_H__
  19. #define __STARPU_PERFMODEL_H__
  20. #include <starpu.h>
  21. #include <stdio.h>
  22. #ifdef __cplusplus
  23. extern "C"
  24. {
  25. #endif
  26. /**
  27. @defgroup API_Performance_Model Performance Model
  28. @{
  29. */
  30. struct starpu_task;
  31. struct starpu_data_descr;
  32. /**
  33. todo
  34. */
  35. struct starpu_perfmodel_device
  36. {
  37. enum starpu_worker_archtype type; /**< type of the device */
  38. int devid; /**< identifier of the precise device */
  39. int ncores; /**< number of execution in parallel, minus 1 */
  40. };
  41. /**
  42. todo
  43. */
  44. struct starpu_perfmodel_arch
  45. {
  46. int ndevices; /**< number of the devices for the given arch */
  47. struct starpu_perfmodel_device *devices; /**< list of the devices for the given arch */
  48. };
  49. struct starpu_perfmodel_history_entry
  50. {
  51. double mean; /**< mean_n = 1/n sum */
  52. double deviation; /**< n dev_n = sum2 - 1/n (sum)^2 */
  53. double sum; /**< sum of samples (in µs) */
  54. double sum2; /**< sum of samples^2 */
  55. unsigned nsample; /**< number of samples */
  56. unsigned nerror;
  57. uint32_t footprint; /**< data footprint */
  58. size_t size; /**< in bytes */
  59. double flops; /**< Provided by the application */
  60. double duration;
  61. starpu_tag_t tag;
  62. double *parameters;
  63. };
  64. struct starpu_perfmodel_history_list
  65. {
  66. struct starpu_perfmodel_history_list *next;
  67. struct starpu_perfmodel_history_entry *entry;
  68. };
  69. /**
  70. todo
  71. */
  72. struct starpu_perfmodel_regression_model
  73. {
  74. double sumlny; /**< sum of ln(measured) */
  75. double sumlnx; /**< sum of ln(size) */
  76. double sumlnx2; /**< sum of ln(size)^2 */
  77. unsigned long minx; /**< minimum size */
  78. unsigned long maxx; /**< maximum size */
  79. double sumlnxlny; /**< sum of ln(size)*ln(measured) */
  80. double alpha; /**< estimated = alpha * size ^ beta */
  81. double beta; /**< estimated = alpha * size ^ beta */
  82. unsigned valid; /**< whether the linear regression model is valid (i.e. enough measures) */
  83. double a; /**< estimated = a size ^b + c */
  84. double b; /**< estimated = a size ^b + c */
  85. double c; /**< estimated = a size ^b + c */
  86. unsigned nl_valid; /**< whether the non-linear regression model is valid (i.e. enough measures) */
  87. unsigned nsample; /**< number of sample values for non-linear regression */
  88. double *coeff; /**< list of computed coefficients for multiple linear regression model */
  89. unsigned ncoeff; /**< number of coefficients for multiple linear regression model */
  90. unsigned multi_valid; /**< whether the multiple linear regression model is valid */
  91. };
  92. struct starpu_perfmodel_history_table;
  93. #define starpu_per_arch_perfmodel starpu_perfmodel_per_arch STARPU_DEPRECATED
  94. typedef double (*starpu_perfmodel_per_arch_cost_function)(struct starpu_task *task, struct starpu_perfmodel_arch* arch, unsigned nimpl);
  95. typedef size_t (*starpu_perfmodel_per_arch_size_base)(struct starpu_task *task, struct starpu_perfmodel_arch* arch, unsigned nimpl);
  96. /**
  97. information about the performance model of a given arch.
  98. */
  99. struct starpu_perfmodel_per_arch
  100. {
  101. /**
  102. Used by ::STARPU_PER_ARCH, must point to functions which take a
  103. task, the target arch and implementation number (as mere
  104. conveniency, since the array is already indexed by these), and
  105. must return a task duration estimation in micro-seconds.
  106. */
  107. starpu_perfmodel_per_arch_cost_function cost_function;
  108. /**
  109. Same as in structure starpu_perfmodel, but per-arch, in case it
  110. depends on the architecture-specific implementation.
  111. */
  112. starpu_perfmodel_per_arch_size_base size_base;
  113. /**
  114. \private
  115. The history of performance measurements.
  116. */
  117. struct starpu_perfmodel_history_table *history;
  118. /**
  119. \private
  120. Used by ::STARPU_HISTORY_BASED, ::STARPU_NL_REGRESSION_BASED and
  121. ::STARPU_MULTIPLE_REGRESSION_BASED, records all execution history
  122. measures.
  123. */
  124. struct starpu_perfmodel_history_list *list;
  125. /**
  126. \private
  127. Used by ::STARPU_REGRESSION_BASED, ::STARPU_NL_REGRESSION_BASED
  128. and ::STARPU_MULTIPLE_REGRESSION_BASED, contains the estimated
  129. factors of the regression.
  130. */
  131. struct starpu_perfmodel_regression_model regression;
  132. char debug_path[256];
  133. };
  134. /**
  135. todo
  136. */
  137. enum starpu_perfmodel_type
  138. {
  139. STARPU_PERFMODEL_INVALID=0,
  140. STARPU_PER_WORKER, /**< Application-provided per-worker cost model function */
  141. STARPU_PER_ARCH, /**< Application-provided per-arch cost model function */
  142. STARPU_COMMON, /**< Application-provided common cost model function, with per-arch factor */
  143. STARPU_HISTORY_BASED, /**< Automatic history-based cost model */
  144. STARPU_REGRESSION_BASED, /**< Automatic linear regression-based cost model (alpha * size ^ beta) */
  145. STARPU_NL_REGRESSION_BASED, /**< Automatic non-linear regression-based cost model (a * size ^ b + c) */
  146. STARPU_MULTIPLE_REGRESSION_BASED /**< Automatic multiple linear regression-based cost model. Application
  147. provides parameters, their combinations and exponents. */
  148. };
  149. struct _starpu_perfmodel_state;
  150. typedef struct _starpu_perfmodel_state* starpu_perfmodel_state_t;
  151. /**
  152. Contain all information about a performance model. At least the
  153. type and symbol fields have to be filled when defining a performance
  154. model for a codelet. For compatibility, make sure to initialize the
  155. whole structure to zero, either by using explicit memset, or by
  156. letting the compiler implicitly do it in e.g. static storage case. If
  157. not provided, other fields have to be zero.
  158. */
  159. struct starpu_perfmodel
  160. {
  161. /**
  162. type of performance model
  163. <ul>
  164. <li>
  165. ::STARPU_HISTORY_BASED, ::STARPU_REGRESSION_BASED,
  166. ::STARPU_NL_REGRESSION_BASED: No other fields needs to be
  167. provided, this is purely history-based.
  168. </li>
  169. <li>
  170. ::STARPU_MULTIPLE_REGRESSION_BASED: Need to provide fields
  171. starpu_perfmodel::nparameters (number of different parameters),
  172. starpu_perfmodel::ncombinations (number of parameters
  173. combinations-tuples) and table starpu_perfmodel::combinations
  174. which defines exponents of the equation. Function cl_perf_func
  175. also needs to define how to extract parameters from the task.
  176. </li>
  177. <li>
  178. ::STARPU_PER_ARCH: either field
  179. starpu_perfmodel::arch_cost_function has to be filled with a
  180. function that returns the cost in micro-seconds on the arch given
  181. as parameter, or field starpu_perfmodel::per_arch has to be filled
  182. with functions which return the cost in micro-seconds.
  183. </li>
  184. <li>
  185. ::STARPU_COMMON: field starpu_perfmodel::cost_function has to be
  186. filled with a function that returns the cost in micro-seconds on a
  187. CPU, timing on other archs will be determined by multiplying by an
  188. arch-specific factor.
  189. </li>
  190. </ul>
  191. */
  192. enum starpu_perfmodel_type type;
  193. /**
  194. Used by ::STARPU_COMMON. Take a task and implementation number,
  195. and must return a task duration estimation in micro-seconds.
  196. */
  197. double (*cost_function)(struct starpu_task *, unsigned nimpl);
  198. /**
  199. Used by ::STARPU_PER_ARCH. Take a task, an arch and implementation
  200. number, and must return a task duration estimation in
  201. micro-seconds on that arch.
  202. */
  203. double (*arch_cost_function)(struct starpu_task *, struct starpu_perfmodel_arch * arch, unsigned nimpl);
  204. /**
  205. Used by ::STARPU_PER_WORKER. Take a task, a worker id and implementation
  206. number, and must return a task duration estimation in
  207. micro-seconds on that worker.
  208. */
  209. double (*worker_cost_function)(struct starpu_task *, unsigned workerid, unsigned nimpl);
  210. /**
  211. Used by ::STARPU_HISTORY_BASED, ::STARPU_REGRESSION_BASED and
  212. ::STARPU_NL_REGRESSION_BASED. If not <c>NULL</c>, take a task and
  213. implementation number, and return the size to be used as index to
  214. distinguish histories and as a base for regressions.
  215. */
  216. size_t (*size_base)(struct starpu_task *, unsigned nimpl);
  217. /**
  218. Used by ::STARPU_HISTORY_BASED. If not <c>NULL</c>, take a task
  219. and return the footprint to be used as index to distinguish
  220. histories. The default is to use the starpu_task_data_footprint()
  221. function.
  222. */
  223. uint32_t (*footprint)(struct starpu_task *);
  224. /**
  225. symbol name for the performance model, which will be used as file
  226. name to store the model. It must be set otherwise the model will
  227. be ignored.
  228. */
  229. const char *symbol;
  230. /**
  231. \private
  232. Whether the performance model is already loaded from the disk.
  233. */
  234. unsigned is_loaded;
  235. /**
  236. \private
  237. */
  238. unsigned benchmarking;
  239. /**
  240. \private
  241. */
  242. unsigned is_init;
  243. void (*parameters)(struct starpu_task * task, double *parameters);
  244. /**
  245. \private
  246. Names of parameters used for multiple linear regression models (M,
  247. N, K)
  248. */
  249. const char **parameters_names;
  250. /**
  251. \private
  252. Number of parameters used for multiple linear regression models
  253. */
  254. unsigned nparameters;
  255. /**
  256. \private
  257. Table of combinations of parameters (and the exponents) used for
  258. multiple linear regression models
  259. */
  260. unsigned **combinations;
  261. /**
  262. \private
  263. Number of combination of parameters used for multiple linear
  264. regression models
  265. */
  266. unsigned ncombinations;
  267. /**
  268. \private
  269. */
  270. starpu_perfmodel_state_t state;
  271. };
  272. /**
  273. Initialize the \p model performance model structure. This is automatically
  274. called when e.g. submitting a task using a codelet using this performance model.
  275. */
  276. void starpu_perfmodel_init(struct starpu_perfmodel *model);
  277. /**
  278. Deinitialize the \p model performance model structure. You need to call this
  279. before deallocating the structure. You will probably want to call
  280. starpu_perfmodel_unload_model() before calling this function, to save the perfmodel.
  281. */
  282. int starpu_perfmodel_deinit(struct starpu_perfmodel *model);
  283. /**
  284. starpu_energy_start - start counting hardware events in an event set
  285. - \p workerid is the worker on which calibration is to be performed (in the case of GPUs, use -1 for CPUs)
  286. - \p archi is the type of architecture on which calibration will be run
  287. */
  288. int starpu_energy_start(int workerid, enum starpu_worker_archtype archi);
  289. /**
  290. starpu_energy_stop - stop counting hardware events in an event set
  291. - \p model is the energy performance model to be filled with the result
  292. - \p task is a task specimen, so the performance model folds the result according to the parameter sizes of the task.
  293. - \p nimpl is the implementation number run during calibration
  294. - \p ntasks is the number of tasks run during calibration
  295. - \p workerid is the worker on which calibration was performed (in the case of GPUs, use -1 for CPUs)
  296. - \p archi is the type of architecture on which calibration was run
  297. */
  298. int starpu_energy_stop(struct starpu_perfmodel *model, struct starpu_task *task, unsigned nimpl, unsigned ntasks, int workerid, enum starpu_worker_archtype archi);
  299. /**
  300. Load the performance model found in the file named \p filename. \p model has to be
  301. completely zero, and will be filled with the information stored in the given file.
  302. */
  303. int starpu_perfmodel_load_file(const char *filename, struct starpu_perfmodel *model);
  304. /**
  305. Load a given performance model. \p model has to be
  306. completely zero, and will be filled with the information stored in
  307. <c>$STARPU_HOME/.starpu</c>. The function is intended to be used by
  308. external tools that want to read the performance model files.
  309. */
  310. int starpu_perfmodel_load_symbol(const char *symbol, struct starpu_perfmodel *model);
  311. /**
  312. Unload \p model which has been previously loaded
  313. through the function starpu_perfmodel_load_symbol()
  314. */
  315. int starpu_perfmodel_unload_model(struct starpu_perfmodel *model);
  316. /**
  317. Save the performance model in its file.
  318. */
  319. void starpu_save_history_based_model(struct starpu_perfmodel *model);
  320. /**
  321. Fills \p path (supposed to be \p maxlen long) with the full path to the
  322. performance model file for symbol \p symbol. This path can later on be used
  323. for instance with starpu_perfmodel_load_file() .
  324. */
  325. void starpu_perfmodel_get_model_path(const char *symbol, char *path, size_t maxlen);
  326. /**
  327. Dump performance model \p model to output stream \p output, in XML format.
  328. */
  329. void starpu_perfmodel_dump_xml(FILE *output, struct starpu_perfmodel *model);
  330. /**
  331. Free internal memory used for sampling
  332. management. It should only be called by an application which is not
  333. calling starpu_shutdown() as this function already calls it. See for
  334. example <c>tools/starpu_perfmodel_display.c</c>.
  335. */
  336. void starpu_perfmodel_free_sampling(void);
  337. /**
  338. Return the architecture type of the worker \p workerid.
  339. */
  340. struct starpu_perfmodel_arch *starpu_worker_get_perf_archtype(int workerid, unsigned sched_ctx_id);
  341. int starpu_perfmodel_get_narch_combs(void);
  342. int starpu_perfmodel_arch_comb_add(int ndevices, struct starpu_perfmodel_device* devices);
  343. int starpu_perfmodel_arch_comb_get(int ndevices, struct starpu_perfmodel_device *devices);
  344. struct starpu_perfmodel_arch *starpu_perfmodel_arch_comb_fetch(int comb);
  345. struct starpu_perfmodel_per_arch *starpu_perfmodel_get_model_per_arch(struct starpu_perfmodel *model, struct starpu_perfmodel_arch *arch, unsigned impl);
  346. struct starpu_perfmodel_per_arch *starpu_perfmodel_get_model_per_devices(struct starpu_perfmodel *model, int impl, ...);
  347. int starpu_perfmodel_set_per_devices_cost_function(struct starpu_perfmodel *model, int impl, starpu_perfmodel_per_arch_cost_function func, ...);
  348. int starpu_perfmodel_set_per_devices_size_base(struct starpu_perfmodel *model, int impl, starpu_perfmodel_per_arch_size_base func, ...);
  349. /**
  350. Return the path to the debugging information for the performance model.
  351. */
  352. void starpu_perfmodel_debugfilepath(struct starpu_perfmodel *model, struct starpu_perfmodel_arch *arch, char *path, size_t maxlen, unsigned nimpl);
  353. const char* starpu_perfmodel_get_archtype_name(enum starpu_worker_archtype archtype);
  354. /**
  355. Return the architecture name for \p arch
  356. */
  357. void starpu_perfmodel_get_arch_name(struct starpu_perfmodel_arch *arch, char *archname, size_t maxlen, unsigned nimpl);
  358. /**
  359. Return the estimated time in µs of a task with the given model and the given footprint.
  360. */
  361. double starpu_perfmodel_history_based_expected_perf(struct starpu_perfmodel *model, struct starpu_perfmodel_arch* arch, uint32_t footprint);
  362. /**
  363. If starpu_init() is not used, starpu_perfmodel_initialize() should be used called calling starpu_perfmodel_* functions.
  364. */
  365. void starpu_perfmodel_initialize(void);
  366. /**
  367. Print a list of all performance models on \p output
  368. */
  369. int starpu_perfmodel_list(FILE *output);
  370. void starpu_perfmodel_print(struct starpu_perfmodel *model, struct starpu_perfmodel_arch *arch, unsigned nimpl, char *parameter, uint32_t *footprint, FILE *output);
  371. int starpu_perfmodel_print_all(struct starpu_perfmodel *model, char *arch, char *parameter, uint32_t *footprint, FILE *output);
  372. int starpu_perfmodel_print_estimations(struct starpu_perfmodel *model, uint32_t footprint, FILE *output);
  373. int starpu_perfmodel_list_combs(FILE *output, struct starpu_perfmodel *model);
  374. /**
  375. Feed the performance model \p model with one explicit
  376. measurement (in µs or J), in addition to measurements done by StarPU
  377. itself. This can be useful when the application already has an
  378. existing set of measurements done in good conditions, that StarPU
  379. could benefit from instead of doing on-line measurements. An example
  380. of use can be seen in \ref PerformanceModelExample.
  381. Note that this records only one measurement, and StarPU would ignore
  382. the first measurement (since it is usually disturbed by library loading
  383. etc.). Make sure to call this function several times to record all your
  384. measurements.
  385. You can also call starpu_perfmodel_update_history_n() to directly provide an
  386. average performed on several tasks.
  387. */
  388. void starpu_perfmodel_update_history(struct starpu_perfmodel *model, struct starpu_task *task, struct starpu_perfmodel_arch *arch, unsigned cpuid, unsigned nimpl, double measured);
  389. /**
  390. Feed the performance model \p model with an explicit average measurement (in µs or J).
  391. This is similar to starpu_perfmodel_update_history(), but records a batch of
  392. \p number measurements provided as the average of the measurements \p average_measured.
  393. */
  394. void starpu_perfmodel_update_history_n(struct starpu_perfmodel *model, struct starpu_task *task, struct starpu_perfmodel_arch *arch, unsigned cpuid, unsigned nimpl, double average_measured, unsigned number);
  395. /**
  396. Print the directory name storing performance models on \p output
  397. */
  398. void starpu_perfmodel_directory(FILE *output);
  399. /**
  400. Print a matrix of bus bandwidths on \p f.
  401. */
  402. void starpu_bus_print_bandwidth(FILE *f);
  403. /**
  404. Print the affinity devices on \p f.
  405. */
  406. void starpu_bus_print_affinity(FILE *f);
  407. /**
  408. Print on \p f the name of the files containing the matrix of bus bandwidths, the affinity devices and the latency.
  409. */
  410. void starpu_bus_print_filenames(FILE *f);
  411. /**
  412. Return the bandwidth of data transfer between two memory nodes
  413. */
  414. double starpu_transfer_bandwidth(unsigned src_node, unsigned dst_node);
  415. /**
  416. Return the latency of data transfer between two memory nodes
  417. */
  418. double starpu_transfer_latency(unsigned src_node, unsigned dst_node);
  419. /**
  420. Return the estimated time to transfer a given size between two memory nodes.
  421. */
  422. double starpu_transfer_predict(unsigned src_node, unsigned dst_node, size_t size);
  423. /**
  424. Performance model which just always return 1µs.
  425. */
  426. extern struct starpu_perfmodel starpu_perfmodel_nop;
  427. /** @} */
  428. #ifdef __cplusplus
  429. }
  430. #endif
  431. #endif /* __STARPU_PERFMODEL_H__ */