perf_counters_02.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2019 Inria
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <starpu.h>
  17. #include <assert.h>
  18. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  19. /* global counters */
  20. static int id_g_total_submitted;
  21. static int id_g_peak_submitted;
  22. static int id_g_peak_ready;
  23. /* per worker counters */
  24. static int id_w_total_executed;
  25. static int id_w_cumul_execution_time;
  26. /* per_codelet counters */
  27. static int id_c_total_submitted;
  28. static int id_c_peak_submitted;
  29. static int id_c_peak_ready;
  30. static int id_c_total_executed;
  31. static int id_c_cumul_execution_time;
  32. void g_listener_cb(struct starpu_perf_counter_listener *listener, struct starpu_perf_counter_sample *sample, void *context)
  33. {
  34. (void) listener;
  35. (void) context;
  36. int64_t g_total_submitted = starpu_perf_counter_sample_get_int64_value(sample, id_g_total_submitted);
  37. int64_t g_peak_submitted = starpu_perf_counter_sample_get_int64_value(sample, id_g_peak_submitted);
  38. int64_t g_peak_ready = starpu_perf_counter_sample_get_int64_value(sample, id_g_peak_ready);
  39. printf("global: g_total_submitted = %ld, g_peak_submitted = %ld, g_peak_ready = %ld\n", g_total_submitted, g_peak_submitted, g_peak_ready);
  40. }
  41. void w_listener_cb(struct starpu_perf_counter_listener *listener, struct starpu_perf_counter_sample *sample, void *context)
  42. {
  43. (void) listener;
  44. (void) context;
  45. int workerid = starpu_worker_get_id();
  46. int64_t w_total_executed = starpu_perf_counter_sample_get_int64_value(sample, id_w_total_executed);
  47. double w_cumul_execution_time = starpu_perf_counter_sample_get_double_value(sample, id_w_cumul_execution_time);
  48. printf("worker[%d]: w_total_executed = %ld, w_cumul_execution_time = %lf\n", workerid, w_total_executed, w_cumul_execution_time);
  49. }
  50. void c_listener_cb(struct starpu_perf_counter_listener *listener, struct starpu_perf_counter_sample *sample, void *context)
  51. {
  52. (void) listener;
  53. struct starpu_codelet *cl = context;
  54. int64_t c_total_submitted = starpu_perf_counter_sample_get_int64_value(sample, id_c_total_submitted);
  55. int64_t c_peak_submitted = starpu_perf_counter_sample_get_int64_value(sample, id_c_peak_submitted);
  56. int64_t c_peak_ready = starpu_perf_counter_sample_get_int64_value(sample, id_c_peak_ready);
  57. int64_t c_total_executed = starpu_perf_counter_sample_get_int64_value(sample, id_c_total_executed);
  58. double c_cumul_execution_time = starpu_perf_counter_sample_get_double_value(sample, id_c_cumul_execution_time);
  59. if (cl->name == NULL)
  60. {
  61. printf("codelet[%s]: c_total_submitted = %ld, c_peak_submitted = %ld, c_peak_ready = %ld, c_total_executed = %ld, c_cumul_execution_time = %lf\n", cl->name, c_total_submitted, c_peak_submitted, c_peak_ready, c_total_executed, c_cumul_execution_time);
  62. }
  63. else
  64. {
  65. printf("codelet[%p]: c_total_submitted = %ld, c_peak_submitted = %ld, c_peak_ready = %ld, c_total_executed = %ld, c_cumul_execution_time = %lf\n", cl, c_total_submitted, c_peak_submitted, c_peak_ready, c_total_executed, c_cumul_execution_time);
  66. }
  67. }
  68. void f(void *buffers[], void *cl_args)
  69. {
  70. int *int_vector = (int*)STARPU_VECTOR_GET_PTR(buffers[0]);
  71. int NX = (int)STARPU_VECTOR_GET_NX(buffers[0]);
  72. const int niters;
  73. starpu_codelet_unpack_args(cl_args, &niters);
  74. int i;
  75. for (i=0; i<niters; i++)
  76. {
  77. int_vector[i % NX] += i;
  78. }
  79. }
  80. struct starpu_codelet cl =
  81. {
  82. .cpu_funcs = {f},
  83. .cpu_funcs_name = {"f"},
  84. .nbuffers = 1,
  85. .name = "perf_counter_f"
  86. };
  87. const enum starpu_perf_counter_scope g_scope = starpu_perf_counter_scope_global;
  88. const enum starpu_perf_counter_scope w_scope = starpu_perf_counter_scope_per_worker;
  89. const enum starpu_perf_counter_scope c_scope = starpu_perf_counter_scope_per_codelet;
  90. #define NVECTORS 5
  91. #define NTASKS 1000
  92. #define NITER 1000
  93. #define VECTOR_LEN 2
  94. int main(int argc, char **argv)
  95. {
  96. int ret;
  97. ret = starpu_init(NULL);
  98. if (ret == -ENODEV)
  99. return 77;
  100. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  101. struct starpu_perf_counter_set *g_set = starpu_perf_counter_set_alloc(g_scope);
  102. STARPU_ASSERT(g_set != NULL);
  103. struct starpu_perf_counter_set *w_set = starpu_perf_counter_set_alloc(w_scope);
  104. STARPU_ASSERT(w_set != NULL);
  105. struct starpu_perf_counter_set *c_set = starpu_perf_counter_set_alloc(c_scope);
  106. STARPU_ASSERT(c_set != NULL);
  107. id_g_total_submitted = starpu_perf_counter_name_to_id(g_scope, "starpu.task.g_total_submitted");
  108. STARPU_ASSERT(id_g_total_submitted != -1);
  109. id_g_peak_submitted = starpu_perf_counter_name_to_id(g_scope, "starpu.task.g_peak_submitted");
  110. STARPU_ASSERT(id_g_peak_submitted != -1);
  111. id_g_peak_ready = starpu_perf_counter_name_to_id(g_scope, "starpu.task.g_peak_ready");
  112. STARPU_ASSERT(id_g_peak_ready != -1);
  113. id_w_total_executed = starpu_perf_counter_name_to_id(w_scope, "starpu.task.w_total_executed");
  114. STARPU_ASSERT(id_w_total_executed != -1);
  115. id_w_cumul_execution_time = starpu_perf_counter_name_to_id(w_scope, "starpu.task.w_cumul_execution_time");
  116. STARPU_ASSERT(id_w_cumul_execution_time != -1);
  117. id_c_total_submitted = starpu_perf_counter_name_to_id(c_scope, "starpu.task.c_total_submitted");
  118. STARPU_ASSERT(id_c_total_submitted != -1);
  119. id_c_peak_submitted = starpu_perf_counter_name_to_id(c_scope, "starpu.task.c_peak_submitted");
  120. STARPU_ASSERT(id_c_peak_submitted != -1);
  121. id_c_peak_ready = starpu_perf_counter_name_to_id(c_scope, "starpu.task.c_peak_ready");
  122. STARPU_ASSERT(id_c_peak_ready != -1);
  123. id_c_total_executed = starpu_perf_counter_name_to_id(c_scope, "starpu.task.c_total_executed");
  124. STARPU_ASSERT(id_c_total_executed != -1);
  125. id_c_cumul_execution_time = starpu_perf_counter_name_to_id(c_scope, "starpu.task.c_cumul_execution_time");
  126. STARPU_ASSERT(id_c_cumul_execution_time != -1);
  127. starpu_perf_counter_set_enable_id(g_set, id_g_total_submitted);
  128. starpu_perf_counter_set_enable_id(g_set, id_g_peak_submitted);
  129. starpu_perf_counter_set_enable_id(g_set, id_g_peak_ready);
  130. starpu_perf_counter_set_enable_id(w_set, id_w_total_executed);
  131. starpu_perf_counter_set_enable_id(w_set, id_w_cumul_execution_time);
  132. starpu_perf_counter_set_enable_id(c_set, id_c_total_submitted);
  133. starpu_perf_counter_set_enable_id(c_set, id_c_peak_submitted);
  134. starpu_perf_counter_set_enable_id(c_set, id_c_peak_ready);
  135. starpu_perf_counter_set_enable_id(c_set, id_c_total_executed);
  136. starpu_perf_counter_set_enable_id(c_set, id_c_cumul_execution_time);
  137. struct starpu_perf_counter_listener * g_listener = starpu_perf_counter_listener_init(g_set, g_listener_cb, (void *)(uintptr_t)42);
  138. struct starpu_perf_counter_listener * w_listener = starpu_perf_counter_listener_init(w_set, w_listener_cb, (void *)(uintptr_t)17);
  139. struct starpu_perf_counter_listener * c_listener = starpu_perf_counter_listener_init(c_set, c_listener_cb, (void *)(uintptr_t)76);
  140. starpu_perf_counter_set_global_listener(g_listener);
  141. starpu_perf_counter_set_all_per_worker_listeners(w_listener);
  142. starpu_perf_counter_set_per_codelet_listener(&cl, c_listener);
  143. int* vector[NVECTORS];
  144. starpu_data_handle_t vector_h[NVECTORS];
  145. int v;
  146. for (v=0; v<NVECTORS; v++)
  147. {
  148. vector[v] = calloc(VECTOR_LEN, sizeof(*vector));
  149. STARPU_ASSERT(vector[v] != NULL);
  150. {
  151. int i;
  152. for (i=0; i<VECTOR_LEN; i++)
  153. {
  154. vector[v][i] = i;
  155. }
  156. }
  157. starpu_vector_data_register(&vector_h[v], STARPU_MAIN_RAM, (uintptr_t)vector[v], VECTOR_LEN, sizeof(*vector[v]));
  158. }
  159. {
  160. int i;
  161. for (i=0; i<NTASKS; i++)
  162. {
  163. v = i % NVECTORS;
  164. const int niter = NITER;
  165. starpu_insert_task(&cl,
  166. STARPU_RW, vector_h[v],
  167. STARPU_VALUE, &niter, sizeof(int),
  168. 0);
  169. }
  170. }
  171. for (v=0; v<NVECTORS; v++)
  172. {
  173. starpu_data_unregister(vector_h[v]);
  174. free(vector[v]);
  175. }
  176. starpu_perf_counter_unset_per_codelet_listener(&cl);
  177. starpu_perf_counter_unset_all_per_worker_listeners();
  178. starpu_perf_counter_unset_global_listener();
  179. starpu_perf_counter_listener_exit(c_listener);
  180. starpu_perf_counter_listener_exit(w_listener);
  181. starpu_perf_counter_listener_exit(g_listener);
  182. starpu_perf_counter_set_disable_id(c_set, id_c_cumul_execution_time);
  183. starpu_perf_counter_set_disable_id(c_set, id_c_total_executed);
  184. starpu_perf_counter_set_disable_id(c_set, id_c_peak_ready);
  185. starpu_perf_counter_set_disable_id(c_set, id_c_peak_submitted);
  186. starpu_perf_counter_set_disable_id(c_set, id_c_total_submitted);
  187. starpu_perf_counter_set_disable_id(w_set, id_w_cumul_execution_time);
  188. starpu_perf_counter_set_disable_id(w_set, id_w_total_executed);
  189. starpu_perf_counter_set_disable_id(g_set, id_g_peak_ready);
  190. starpu_perf_counter_set_disable_id(g_set, id_g_peak_submitted);
  191. starpu_perf_counter_set_disable_id(g_set, id_g_total_submitted);
  192. starpu_perf_counter_set_free(c_set);
  193. c_set = NULL;
  194. starpu_perf_counter_set_free(w_set);
  195. w_set = NULL;
  196. starpu_perf_counter_set_free(g_set);
  197. g_set = NULL;
  198. starpu_shutdown();
  199. return 0;
  200. }