profiling.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 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. int64_t delay_sum = 0;
  63. int64_t length_sum = 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. int64_t delay = (info->start_time - info->submit_time);
  70. delay_sum += delay;
  71. /* How long was the task execution ? */
  72. int64_t length = (info->end_time - info->start_time);
  73. length_sum += length;
  74. /* We don't need the task structure anymore */
  75. starpu_task_destroy(task);
  76. }
  77. free(tasks);
  78. fprintf(stderr, "Avg. delay : %2.2f us\n", ((double)delay_sum)/niter);
  79. fprintf(stderr, "Avg. length : %2.2f us\n", ((double)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. starpu_worker_get_profiling_info(worker, &worker_info);
  86. float executing_ratio = ((100.0*worker_info.executing_time)/worker_info.total_time);
  87. float sleeping_ratio = ((100.0*worker_info.sleeping_time)/worker_info.total_time);
  88. char workername[128];
  89. starpu_worker_get_name(worker, workername, 128);
  90. fprintf(stderr, "Worker %s:\n", workername);
  91. fprintf(stderr, "\ttotal time : %ld us\n", worker_info.total_time);
  92. fprintf(stderr, "\texec time : %ld us (%.2f %%)\n", worker_info.executing_time, executing_ratio);
  93. fprintf(stderr, "\tblocked time : %ld us (%.2f %%)\n", worker_info.sleeping_time, sleeping_ratio);
  94. }
  95. starpu_shutdown();
  96. return 0;
  97. }