driver_cpu.c 6.4 KB

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