starpu_perfmodel.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2011 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. #ifndef __STARPU_PERFMODEL_H__
  19. #define __STARPU_PERFMODEL_H__
  20. #include <starpu.h>
  21. #include <stdio.h>
  22. #include <starpu_util.h>
  23. #if ! defined(_MSC_VER)
  24. # include <pthread.h>
  25. #endif
  26. #ifdef __cplusplus
  27. extern "C"
  28. {
  29. #endif
  30. struct starpu_task;
  31. struct starpu_htbl32_node;
  32. struct starpu_history_list;
  33. struct starpu_buffer_descr;
  34. /*
  35. it is possible that we have multiple versions of the same kind of workers,
  36. for instance multiple GPUs or even different CPUs within the same machine
  37. so we do not use the archtype enum type directly for performance models
  38. */
  39. enum starpu_perf_archtype
  40. {
  41. STARPU_CPU_DEFAULT = 0,
  42. /* CPU combined workers between 0 and STARPU_MAXCPUS-1 */
  43. STARPU_CUDA_DEFAULT = STARPU_MAXCPUS,
  44. STARPU_OPENCL_DEFAULT = STARPU_CUDA_DEFAULT + STARPU_MAXCUDADEVS,
  45. /* STARPU_OPENCL_DEFAULT + devid */
  46. STARPU_GORDON_DEFAULT = STARPU_OPENCL_DEFAULT + STARPU_MAXOPENCLDEVS
  47. };
  48. #ifdef __STDC_VERSION__
  49. # if __STDC_VERSION__ > 199901L || STARPU_GNUC_PREREQ(4, 6)
  50. /* Make sure the following assertions hold, since StarPU relies on it. */
  51. _Static_assert(STARPU_CPU_DEFAULT == 0,
  52. "invalid STARPU_CPU_DEFAULT value");
  53. _Static_assert(STARPU_CUDA_DEFAULT > STARPU_CPU_DEFAULT,
  54. "invalid STARPU_CPU_DEFAULT value");
  55. _Static_assert(STARPU_CUDA_DEFAULT < STARPU_OPENCL_DEFAULT,
  56. "invalid STARPU_{CUDA,OPENCL}_DEFAULT values");
  57. # endif
  58. #endif
  59. #define STARPU_NARCH_VARIATIONS (STARPU_GORDON_DEFAULT+1)
  60. struct starpu_history_entry
  61. {
  62. //double measured;
  63. /* mean_n = 1/n sum */
  64. double mean;
  65. /* n dev_n = sum2 - 1/n (sum)^2 */
  66. double deviation;
  67. /* sum of samples */
  68. double sum;
  69. /* sum of samples^2 */
  70. double sum2;
  71. // /* sum of ln(measured) */
  72. // double sumlny;
  73. //
  74. // /* sum of ln(size) */
  75. // double sumlnx;
  76. // double sumlnx2;
  77. //
  78. // /* sum of ln(size) ln(measured) */
  79. // double sumlnxlny;
  80. //
  81. unsigned nsample;
  82. uint32_t footprint;
  83. #ifdef STARPU_HAVE_WINDOWS
  84. unsigned size; /* in bytes */
  85. #else
  86. size_t size; /* in bytes */
  87. #endif
  88. };
  89. struct starpu_history_list
  90. {
  91. struct starpu_history_list *next;
  92. struct starpu_history_entry *entry;
  93. };
  94. struct starpu_model_list
  95. {
  96. struct starpu_model_list *next;
  97. struct starpu_perfmodel *model;
  98. };
  99. struct starpu_regression_model
  100. {
  101. /* sum of ln(measured) */
  102. double sumlny;
  103. /* sum of ln(size) */
  104. double sumlnx;
  105. double sumlnx2;
  106. /* minimum/maximum(size) */
  107. unsigned long minx;
  108. unsigned long maxx;
  109. /* sum of ln(size) ln(measured) */
  110. double sumlnxlny;
  111. /* y = alpha size ^ beta */
  112. double alpha;
  113. double beta;
  114. unsigned valid;
  115. /* y = a size ^b + c */
  116. double a, b, c;
  117. unsigned nl_valid;
  118. unsigned nsample;
  119. };
  120. struct starpu_per_arch_perfmodel
  121. {
  122. double (*cost_model)(struct starpu_buffer_descr *t) STARPU_DEPRECATED; /* returns expected duration in µs */
  123. double (*cost_function)(struct starpu_task *task, enum starpu_perf_archtype arch, unsigned nimpl); /* returns expected duration in µs */
  124. size_t (*size_base)(struct starpu_task *, enum starpu_perf_archtype arch, unsigned nimpl);
  125. /* internal variables */
  126. struct starpu_htbl32_node *history;
  127. struct starpu_history_list *list;
  128. struct starpu_regression_model regression;
  129. #ifdef STARPU_MODEL_DEBUG
  130. char debug_path[256];
  131. #endif
  132. };
  133. enum starpu_perfmodel_type
  134. {
  135. STARPU_PER_ARCH, /* Application-provided per-arch cost model function */
  136. STARPU_COMMON, /* Application-provided common cost model function, with per-arch factor */
  137. STARPU_HISTORY_BASED, /* Automatic history-based cost model */
  138. STARPU_REGRESSION_BASED, /* Automatic linear regression-based cost model (alpha * size ^ beta) */
  139. STARPU_NL_REGRESSION_BASED /* Automatic non-linear regression-based cost model (a * size ^ b + c) */
  140. };
  141. struct starpu_perfmodel
  142. {
  143. /* which model is used for that task ? */
  144. enum starpu_perfmodel_type type;
  145. /* single cost model (STARPU_COMMON), returns expected duration in µs */
  146. double (*cost_model)(struct starpu_buffer_descr *) STARPU_DEPRECATED;
  147. double (*cost_function)(struct starpu_task *, unsigned nimpl);
  148. size_t (*size_base)(struct starpu_task *, unsigned nimpl);
  149. /* per-architecture model */
  150. struct starpu_per_arch_perfmodel per_arch[STARPU_NARCH_VARIATIONS][STARPU_MAXIMPLEMENTATIONS];
  151. /* Name of the performance model, this is used as a file name when saving history-based performance models */
  152. const char *symbol;
  153. /* Internal variables */
  154. unsigned is_loaded;
  155. unsigned benchmarking;
  156. #if defined(_MSC_VER)
  157. void *model_rwlock;
  158. #else
  159. pthread_rwlock_t model_rwlock;
  160. #endif
  161. };
  162. enum starpu_perf_archtype starpu_worker_get_perf_archtype(int workerid);
  163. /* This function is intended to be used by external tools that should read the
  164. * performance model files */
  165. int starpu_load_history_debug(const char *symbol, struct starpu_perfmodel *model);
  166. void starpu_perfmodel_debugfilepath(struct starpu_perfmodel *model, enum starpu_perf_archtype arch, char *path, size_t maxlen, unsigned nimpl);
  167. void starpu_perfmodel_get_arch_name(enum starpu_perf_archtype arch, char *archname, size_t maxlen, unsigned nimpl);
  168. int starpu_list_models(FILE *output);
  169. void starpu_force_bus_sampling(void);
  170. void starpu_bus_print_bandwidth(FILE *f);
  171. #ifdef __cplusplus
  172. }
  173. #endif
  174. #endif /* __STARPU_PERFMODEL_H__ */