profiling.c 3.7 KB

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