starpu_perfmodel.h 16 KB

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