profiling.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), 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. /*
  17. * This examplifies how to get task execution profiling from the application.
  18. */
  19. #include <starpu.h>
  20. #include <assert.h>
  21. #include <unistd.h>
  22. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  23. #ifdef STARPU_QUICK_CHECK
  24. static unsigned niter = 50;
  25. #else
  26. static unsigned niter = 500;
  27. #endif
  28. void sleep_codelet(void *descr[], void *arg)
  29. {
  30. (void)descr;
  31. (void)arg;
  32. usleep(1000);
  33. }
  34. int main(int argc, char **argv)
  35. {
  36. int ret;
  37. if (argc == 2)
  38. niter = atoi(argv[1]);
  39. ret = starpu_init(NULL);
  40. if (ret == -ENODEV)
  41. return 77;
  42. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  43. /* Enable profiling */
  44. starpu_profiling_status_set(STARPU_PROFILING_ENABLE);
  45. #ifdef STARPU_QUICK_CHECK
  46. /* We should observe at least 50ms in the sleep time reported by every
  47. * worker. */
  48. usleep(50000);
  49. #else
  50. /* We should observe at least 500ms in the sleep time reported by every
  51. * worker. */
  52. usleep(500000);
  53. #endif
  54. struct starpu_codelet cl =
  55. {
  56. .cpu_funcs = {sleep_codelet},
  57. .cpu_funcs_name = {"sleep_codelet"},
  58. .cuda_funcs = {sleep_codelet},
  59. .opencl_funcs = {sleep_codelet},
  60. .nbuffers = 0,
  61. .name = "sleep"
  62. };
  63. struct starpu_task **tasks = (struct starpu_task **) malloc(niter*sizeof(struct starpu_task *));
  64. assert(tasks);
  65. unsigned i;
  66. for (i = 0; i < niter; i++)
  67. {
  68. struct starpu_task *task = starpu_task_create();
  69. task->cl = &cl;
  70. /* We will destroy the task structure by hand so that we can
  71. * query the profiling info before the task is destroyed. */
  72. task->destroy = 0;
  73. tasks[i] = task;
  74. ret = starpu_task_submit(task);
  75. if (STARPU_UNLIKELY(ret == -ENODEV))
  76. {
  77. FPRINTF(stderr, "No worker may execute this task\n");
  78. exit(0);
  79. }
  80. }
  81. starpu_task_wait_for_all();
  82. double delay_sum = 0.0;
  83. double length_sum = 0.0;
  84. for (i = 0; i < niter; i++)
  85. {
  86. struct starpu_task *task = tasks[i];
  87. struct starpu_profiling_task_info *info = task->profiling_info;
  88. /* How much time did it take before the task started ? */
  89. delay_sum += starpu_timing_timespec_delay_us(&info->submit_time, &info->start_time);
  90. /* How long was the task execution ? */
  91. length_sum += starpu_timing_timespec_delay_us(&info->start_time, &info->end_time);
  92. /* We don't need the task structure anymore */
  93. starpu_task_destroy(task);
  94. }
  95. free(tasks);
  96. if (niter)
  97. {
  98. FPRINTF(stderr, "Avg. delay : %2.2lf us\n", (delay_sum)/niter);
  99. FPRINTF(stderr, "Avg. length : %2.2lf us\n", (length_sum)/niter);
  100. }
  101. /* Display the occupancy of all workers during the test */
  102. unsigned worker;
  103. for (worker = 0; worker < starpu_worker_get_count(); worker++)
  104. {
  105. struct starpu_profiling_worker_info worker_info;
  106. ret = starpu_profiling_worker_get_info(worker, &worker_info);
  107. STARPU_ASSERT(!ret);
  108. double total_time = starpu_timing_timespec_to_us(&worker_info.total_time);
  109. double executing_time = starpu_timing_timespec_to_us(&worker_info.executing_time);
  110. double sleeping_time = starpu_timing_timespec_to_us(&worker_info.sleeping_time);
  111. double overhead_time = total_time - executing_time - sleeping_time;
  112. float executing_ratio = 100.0*executing_time/total_time;
  113. float sleeping_ratio = 100.0*sleeping_time/total_time;
  114. float overhead_ratio = 100.0 - executing_ratio - sleeping_ratio;
  115. char workername[128];
  116. starpu_worker_get_name(worker, workername, 128);
  117. FPRINTF(stderr, "Worker %s:\n", workername);
  118. FPRINTF(stderr, "\t%d task(s)\n", worker_info.executed_tasks);
  119. FPRINTF(stderr, "\ttotal time : %.2lf ms\n", total_time*1e-3);
  120. FPRINTF(stderr, "\texec time : %.2lf ms (%.2f %%)\n", executing_time*1e-3, executing_ratio);
  121. FPRINTF(stderr, "\tblocked time : %.2lf ms (%.2f %%)\n", sleeping_time*1e-3, sleeping_ratio);
  122. FPRINTF(stderr, "\toverhead time: %.2lf ms (%.2f %%)\n", overhead_time*1e-3, overhead_ratio);
  123. }
  124. starpu_shutdown();
  125. return 0;
  126. }