driver_cpu.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 <math.h>
  17. #include <starpu.h>
  18. #include <starpu_profiling.h>
  19. #include <profiling/profiling.h>
  20. #include <drivers/driver_common/driver_common.h>
  21. #include <common/utils.h>
  22. #include <core/debug.h>
  23. #include "driver_cpu.h"
  24. #include <core/policies/sched_policy.h>
  25. static int execute_job_on_cpu(starpu_job_t j, struct starpu_worker_s *cpu_args)
  26. {
  27. int ret;
  28. struct timespec codelet_start, codelet_end;
  29. struct timespec codelet_start_comm, codelet_end_comm;
  30. unsigned calibrate_model = 0;
  31. int workerid = cpu_args->workerid;
  32. struct starpu_task *task = j->task;
  33. struct starpu_codelet_t *cl = task->cl;
  34. STARPU_ASSERT(cl);
  35. STARPU_ASSERT(cl->cpu_func);
  36. if (cl->model && cl->model->benchmarking)
  37. calibrate_model = 1;
  38. if (STARPU_BENCHMARK_COMM)
  39. starpu_clock_gettime(&codelet_start_comm);
  40. ret = _starpu_fetch_task_input(task, 0);
  41. if (STARPU_BENCHMARK_COMM)
  42. starpu_clock_gettime(&codelet_end_comm);
  43. if (ret != 0) {
  44. /* there was not enough memory so the codelet cannot be executed right now ... */
  45. /* push the codelet back and try another one ... */
  46. return -EAGAIN;
  47. }
  48. STARPU_TRACE_START_CODELET_BODY(j);
  49. struct starpu_task_profiling_info *profiling_info;
  50. profiling_info = task->profiling_info;
  51. if (profiling_info || calibrate_model || STARPU_BENCHMARK_COMM)
  52. {
  53. starpu_clock_gettime(&codelet_start);
  54. _starpu_worker_register_executing_start_date(workerid, &codelet_start);
  55. }
  56. cpu_args->status = STATUS_EXECUTING;
  57. task->status = STARPU_TASK_RUNNING;
  58. cl_func func = cl->cpu_func;
  59. func(task->interface, task->cl_arg);
  60. cl->per_worker_stats[workerid]++;
  61. if (profiling_info || calibrate_model || STARPU_BENCHMARK_COMM)
  62. starpu_clock_gettime(&codelet_end);
  63. STARPU_TRACE_END_CODELET_BODY(j);
  64. cpu_args->status = STATUS_UNKNOWN;
  65. _starpu_push_task_output(task, 0);
  66. _starpu_driver_update_job_feedback(j, cpu_args, profiling_info, calibrate_model,
  67. &codelet_start, &codelet_end, &codelet_start_comm, &codelet_end_comm);
  68. (void)STARPU_ATOMIC_ADD(&cpu_args->jobq->total_job_performed, 1);
  69. return 0;
  70. }
  71. void *_starpu_cpu_worker(void *arg)
  72. {
  73. struct starpu_worker_s *cpu_arg = arg;
  74. struct starpu_jobq_s *jobq = cpu_arg->jobq;
  75. unsigned memnode = cpu_arg->memory_node;
  76. int workerid = cpu_arg->workerid;
  77. int devid = cpu_arg->devid;
  78. #ifdef STARPU_USE_FXT
  79. _starpu_fxt_register_thread(cpu_arg->bindid);
  80. #endif
  81. STARPU_TRACE_WORKER_INIT_START(STARPU_FUT_CPU_KEY, devid, memnode);
  82. _starpu_bind_thread_on_cpu(cpu_arg->config, cpu_arg->bindid);
  83. #ifdef STARPU_VERBOSE
  84. fprintf(stderr, "cpu worker %d is ready on logical cpu %d\n", devid, cpu_arg->bindid);
  85. #endif
  86. _starpu_set_local_memory_node_key(&memnode);
  87. _starpu_set_local_queue(jobq);
  88. _starpu_set_local_worker_key(cpu_arg);
  89. snprintf(cpu_arg->name, 32, "CPU %d", devid);
  90. cpu_arg->status = STATUS_UNKNOWN;
  91. STARPU_TRACE_WORKER_INIT_END
  92. /* tell the main thread that we are ready */
  93. PTHREAD_MUTEX_LOCK(&cpu_arg->mutex);
  94. cpu_arg->worker_is_initialized = 1;
  95. PTHREAD_COND_SIGNAL(&cpu_arg->ready_cond);
  96. PTHREAD_MUTEX_UNLOCK(&cpu_arg->mutex);
  97. starpu_job_t j;
  98. int res;
  99. struct starpu_sched_policy_s *policy = _starpu_get_sched_policy();
  100. struct starpu_jobq_s *queue = policy->get_local_queue(policy);
  101. while (_starpu_machine_is_running())
  102. {
  103. STARPU_TRACE_START_PROGRESS(memnode);
  104. _starpu_datawizard_progress(memnode, 1);
  105. STARPU_TRACE_END_PROGRESS(memnode);
  106. _starpu_execute_registered_progression_hooks();
  107. _starpu_jobq_lock(queue);
  108. /* perhaps there is some local task to be executed first */
  109. j = _starpu_pop_local_task(cpu_arg);
  110. /* otherwise ask a task to the scheduler */
  111. if (!j)
  112. j = _starpu_pop_task();
  113. if (j == NULL)
  114. {
  115. if (_starpu_worker_can_block(memnode))
  116. _starpu_block_worker(workerid, &queue->activity_cond, &queue->activity_mutex);
  117. _starpu_jobq_unlock(queue);
  118. continue;
  119. };
  120. _starpu_jobq_unlock(queue);
  121. /* can a cpu perform that task ? */
  122. if (!STARPU_CPU_MAY_PERFORM(j))
  123. {
  124. /* put it and the end of the queue ... XXX */
  125. _starpu_push_task(j, 0);
  126. continue;
  127. }
  128. _starpu_set_current_task(j->task);
  129. res = execute_job_on_cpu(j, cpu_arg);
  130. _starpu_set_current_task(NULL);
  131. if (res) {
  132. switch (res) {
  133. case -EAGAIN:
  134. _starpu_push_task(j, 0);
  135. continue;
  136. default:
  137. assert(0);
  138. }
  139. }
  140. _starpu_handle_job_termination(j, 0);
  141. }
  142. STARPU_TRACE_WORKER_DEINIT_START
  143. /* In case there remains some memory that was automatically
  144. * allocated by StarPU, we release it now. Note that data
  145. * coherency is not maintained anymore at that point ! */
  146. _starpu_free_all_automatically_allocated_buffers(memnode);
  147. #ifdef STARPU_DATA_STATS
  148. fprintf(stderr, "CPU #%d computation %le comm %le (%lf \%%)\n", devid, jobq->total_computation_time, jobq->total_communication_time, jobq->total_communication_time*100.0/jobq->total_computation_time);
  149. #endif
  150. #ifdef STARPU_VERBOSE
  151. double ratio = 0;
  152. if (jobq->total_job_performed != 0)
  153. {
  154. ratio = jobq->total_computation_time_error/jobq->total_computation_time;
  155. }
  156. _starpu_print_to_logfile("MODEL ERROR: CPU %d ERROR %lf EXEC %lf RATIO %lf NTASKS %d\n", devid, jobq->total_computation_time_error, jobq->total_computation_time, ratio, jobq->total_job_performed);
  157. #endif
  158. STARPU_TRACE_WORKER_DEINIT_END(STARPU_FUT_CPU_KEY);
  159. pthread_exit(NULL);
  160. }