profiling.c 4.5 KB

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