profiling.c 3.9 KB

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