profiling.c 3.6 KB

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