profiling.c 4.6 KB

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