driver_cpu.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. unsigned calibrate_model = 0;
  30. int workerid = cpu_args->workerid;
  31. struct starpu_task *task = j->task;
  32. struct starpu_codelet_t *cl = task->cl;
  33. STARPU_ASSERT(cl);
  34. STARPU_ASSERT(cl->cpu_func);
  35. if (cl->model && cl->model->benchmarking)
  36. calibrate_model = 1;
  37. ret = _starpu_fetch_task_input(task, 0);
  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. struct starpu_task_profiling_info *profiling_info;
  45. profiling_info = task->profiling_info;
  46. if (profiling_info || calibrate_model)
  47. {
  48. starpu_clock_gettime(&codelet_start);
  49. _starpu_worker_register_executing_start_date(workerid, &codelet_start);
  50. }
  51. cpu_args->status = STATUS_EXECUTING;
  52. task->status = STARPU_TASK_RUNNING;
  53. cl_func func = cl->cpu_func;
  54. func(task->interface, task->cl_arg);
  55. cl->per_worker_stats[workerid]++;
  56. if (profiling_info || calibrate_model)
  57. starpu_clock_gettime(&codelet_end);
  58. STARPU_TRACE_END_CODELET_BODY(j);
  59. cpu_args->status = STATUS_UNKNOWN;
  60. _starpu_push_task_output(task, 0);
  61. _starpu_driver_update_job_feedback(j, cpu_args, profiling_info, calibrate_model,
  62. &codelet_start, &codelet_end);
  63. return 0;
  64. }
  65. void *_starpu_cpu_worker(void *arg)
  66. {
  67. struct starpu_worker_s *cpu_arg = arg;
  68. struct starpu_jobq_s *jobq = cpu_arg->jobq;
  69. unsigned memnode = cpu_arg->memory_node;
  70. int workerid = cpu_arg->workerid;
  71. int devid = cpu_arg->devid;
  72. #ifdef STARPU_USE_FXT
  73. _starpu_fxt_register_thread(cpu_arg->bindid);
  74. #endif
  75. STARPU_TRACE_WORKER_INIT_START(STARPU_FUT_CPU_KEY, devid, memnode);
  76. _starpu_bind_thread_on_cpu(cpu_arg->config, cpu_arg->bindid);
  77. #ifdef STARPU_VERBOSE
  78. fprintf(stderr, "cpu worker %d is ready on logical cpu %d\n", devid, cpu_arg->bindid);
  79. #endif
  80. _starpu_set_local_memory_node_key(&memnode);
  81. _starpu_set_local_queue(jobq);
  82. _starpu_set_local_worker_key(cpu_arg);
  83. snprintf(cpu_arg->name, 32, "CPU %d", devid);
  84. cpu_arg->status = STATUS_UNKNOWN;
  85. STARPU_TRACE_WORKER_INIT_END
  86. /* tell the main thread that we are ready */
  87. PTHREAD_MUTEX_LOCK(&cpu_arg->mutex);
  88. cpu_arg->worker_is_initialized = 1;
  89. PTHREAD_COND_SIGNAL(&cpu_arg->ready_cond);
  90. PTHREAD_MUTEX_UNLOCK(&cpu_arg->mutex);
  91. starpu_job_t j;
  92. int res;
  93. struct starpu_sched_policy_s *policy = _starpu_get_sched_policy();
  94. struct starpu_jobq_s *queue = policy->get_local_queue(policy);
  95. while (_starpu_machine_is_running())
  96. {
  97. STARPU_TRACE_START_PROGRESS(memnode);
  98. _starpu_datawizard_progress(memnode, 1);
  99. STARPU_TRACE_END_PROGRESS(memnode);
  100. _starpu_execute_registered_progression_hooks();
  101. _starpu_jobq_lock(queue);
  102. /* perhaps there is some local task to be executed first */
  103. j = _starpu_pop_local_task(cpu_arg);
  104. /* otherwise ask a task to the scheduler */
  105. if (!j)
  106. j = _starpu_pop_task();
  107. if (j == NULL)
  108. {
  109. if (_starpu_worker_can_block(memnode))
  110. _starpu_block_worker(workerid, &queue->activity_cond, &queue->activity_mutex);
  111. _starpu_jobq_unlock(queue);
  112. continue;
  113. };
  114. _starpu_jobq_unlock(queue);
  115. /* can a cpu perform that task ? */
  116. if (!STARPU_CPU_MAY_PERFORM(j))
  117. {
  118. /* put it and the end of the queue ... XXX */
  119. _starpu_push_task(j, 0);
  120. continue;
  121. }
  122. _starpu_set_current_task(j->task);
  123. res = execute_job_on_cpu(j, cpu_arg);
  124. _starpu_set_current_task(NULL);
  125. if (res) {
  126. switch (res) {
  127. case -EAGAIN:
  128. _starpu_push_task(j, 0);
  129. continue;
  130. default:
  131. assert(0);
  132. }
  133. }
  134. _starpu_handle_job_termination(j, 0);
  135. }
  136. STARPU_TRACE_WORKER_DEINIT_START
  137. /* In case there remains some memory that was automatically
  138. * allocated by StarPU, we release it now. Note that data
  139. * coherency is not maintained anymore at that point ! */
  140. _starpu_free_all_automatically_allocated_buffers(memnode);
  141. STARPU_TRACE_WORKER_DEINIT_END(STARPU_FUT_CPU_KEY);
  142. pthread_exit(NULL);
  143. }