driver_cpu.c 6.4 KB

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