profiling.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * StarPU
  3. * Copyright (C) Université Bordeaux 1, CNRS 2008-2010 (see AUTHORS file)
  4. *
  5. * This program 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. * This program 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 <starpu_profiling.h>
  18. #include <assert.h>
  19. #include <unistd.h>
  20. static unsigned niter = 500;
  21. void sleep_codelet(__attribute__ ((unused)) void *descr[],
  22. __attribute__ ((unused)) void *_args)
  23. {
  24. usleep(1000);
  25. }
  26. int main(int argc, char **argv)
  27. {
  28. if (argc == 2)
  29. niter = atoi(argv[1]);
  30. starpu_init(NULL);
  31. /* Enable profiling */
  32. starpu_profiling_status_set(STARPU_PROFILING_ENABLE);
  33. /* We should observe at least 500ms in the sleep time reported by every
  34. * worker. */
  35. usleep(500000);
  36. starpu_codelet cl =
  37. {
  38. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  39. .cpu_func = sleep_codelet,
  40. .cuda_func = sleep_codelet,
  41. .opencl_func = sleep_codelet,
  42. .nbuffers = 0
  43. };
  44. struct starpu_task **tasks = malloc(niter*sizeof(struct starpu_task *));
  45. assert(tasks);
  46. unsigned i;
  47. for (i = 0; i < niter; i++)
  48. {
  49. struct starpu_task *task = starpu_task_create();
  50. task->cl = &cl;
  51. /* We will destroy the task structure by hand so that we can
  52. * query the profiling info before the task is destroyed. */
  53. task->destroy = 0;
  54. tasks[i] = task;
  55. int ret = starpu_task_submit(task);
  56. if (STARPU_UNLIKELY(ret == -ENODEV))
  57. {
  58. fprintf(stderr, "No worker may execute this task\n");
  59. exit(0);
  60. }
  61. }
  62. starpu_task_wait_for_all();
  63. double delay_sum = 0.0;
  64. double length_sum = 0.0;
  65. for (i = 0; i < niter; i++)
  66. {
  67. struct starpu_task *task = tasks[i];
  68. struct starpu_task_profiling_info *info = task->profiling_info;
  69. /* How much time did it take before the task started ? */
  70. delay_sum += starpu_timing_timespec_delay_us(&info->submit_time, &info->start_time);
  71. /* How long was the task execution ? */
  72. length_sum += starpu_timing_timespec_delay_us(&info->start_time, &info->end_time);
  73. /* We don't need the task structure anymore */
  74. starpu_task_destroy(task);
  75. }
  76. free(tasks);
  77. fprintf(stderr, "Avg. delay : %2.2lf us\n", (delay_sum)/niter);
  78. fprintf(stderr, "Avg. length : %2.2lf us\n", (length_sum)/niter);
  79. /* Display the occupancy of all workers during the test */
  80. int worker;
  81. for (worker = 0; worker < starpu_worker_get_count(); worker++)
  82. {
  83. struct starpu_worker_profiling_info worker_info;
  84. int ret = starpu_worker_get_profiling_info(worker, &worker_info);
  85. STARPU_ASSERT(!ret);
  86. double total_time = starpu_timing_timespec_to_us(&worker_info.total_time);
  87. double executing_time = starpu_timing_timespec_to_us(&worker_info.executing_time);
  88. double sleeping_time = starpu_timing_timespec_to_us(&worker_info.sleeping_time);
  89. float executing_ratio = 100.0*executing_time/total_time;
  90. float sleeping_ratio = 100.0*sleeping_time/total_time;
  91. char workername[128];
  92. starpu_worker_get_name(worker, workername, 128);
  93. fprintf(stderr, "Worker %s:\n", workername);
  94. fprintf(stderr, "\ttotal time : %.2lf ms\n", total_time*1e-3);
  95. fprintf(stderr, "\texec time : %.2lf ms (%.2f %%)\n", executing_time*1e-3, executing_ratio);
  96. fprintf(stderr, "\tblocked time : %.2lf ms (%.2f %%)\n", sleeping_time*1e-3, sleeping_ratio);
  97. }
  98. starpu_shutdown();
  99. return 0;
  100. }