profiling.c 3.8 KB

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