profiling.c 4.3 KB

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