123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625 |
- /* StarPU --- Runtime system for heterogeneous multicore architectures.
- *
- * Copyright (C) 2010, 2011 Université de Bordeaux 1
- * Copyright (C) 2010 Centre National de la Recherche Scientifique
- *
- * StarPU is free software; you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as published by
- * the Free Software Foundation; either version 2.1 of the License, or (at
- * your option) any later version.
- *
- * StarPU is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- *
- * See the GNU Lesser General Public License in COPYING.LGPL for more details.
- */
- /* Distributed queues using performance modeling to assign tasks */
- #include <limits.h>
- #include <core/workers.h>
- #include <sched_policies/fifo_queues.h>
- #include <core/perfmodel/perfmodel.h>
- #include <starpu_parameters.h>
- static unsigned nworkers;
- static struct starpu_fifo_taskq_s *queue_array[STARPU_NMAXWORKERS];
- static pthread_cond_t sched_cond[STARPU_NMAXWORKERS];
- static pthread_mutex_t sched_mutex[STARPU_NMAXWORKERS];
- static double alpha = STARPU_DEFAULT_ALPHA;
- static double beta = STARPU_DEFAULT_BETA;
- static double _gamma = STARPU_DEFAULT_GAMMA;
- #ifdef STARPU_VERBOSE
- static long int total_task_cnt = 0;
- static long int ready_task_cnt = 0;
- #endif
- static int count_non_ready_buffers(struct starpu_task *task, uint32_t node)
- {
- int cnt = 0;
- starpu_buffer_descr *descrs = task->buffers;
- unsigned nbuffers = task->cl->nbuffers;
- unsigned index;
- for (index = 0; index < nbuffers; index++)
- {
- starpu_buffer_descr *descr;
- starpu_data_handle handle;
- descr = &descrs[index];
- handle = descr->handle;
-
- int is_valid;
- starpu_data_query_status(handle, node, NULL, &is_valid, NULL);
- if (!is_valid)
- cnt++;
- }
- return cnt;
- }
- static struct starpu_task *_starpu_fifo_pop_first_ready_task(struct starpu_fifo_taskq_s *fifo_queue, unsigned node)
- {
- struct starpu_task *task = NULL, *current;
- if (fifo_queue->ntasks == 0)
- return NULL;
- if (fifo_queue->ntasks > 0)
- {
- fifo_queue->ntasks--;
- task = starpu_task_list_back(&fifo_queue->taskq);
- int first_task_priority = task->priority;
- current = task;
- int non_ready_best = INT_MAX;
- while (current)
- {
- int priority = current->priority;
- if (priority <= first_task_priority)
- {
- int non_ready = count_non_ready_buffers(current, node);
- if (non_ready < non_ready_best)
- {
- non_ready_best = non_ready;
- task = current;
- if (non_ready == 0)
- break;
- }
- }
- current = current->prev;
- }
-
- starpu_task_list_erase(&fifo_queue->taskq, task);
- STARPU_TRACE_JOB_POP(task, 0);
- }
-
- return task;
- }
- static struct starpu_task *dmda_pop_ready_task(void)
- {
- struct starpu_task *task;
- int workerid = starpu_worker_get_id();
- struct starpu_fifo_taskq_s *fifo = queue_array[workerid];
- unsigned node = starpu_worker_get_memory_node(workerid);
- task = _starpu_fifo_pop_first_ready_task(fifo, node);
- if (task) {
- double model = task->predicted;
-
- fifo->exp_len -= model;
- fifo->exp_start = starpu_timing_now() + model;
- fifo->exp_end = fifo->exp_start + fifo->exp_len;
- #ifdef STARPU_VERBOSE
- if (task->cl)
- {
- int non_ready = count_non_ready_buffers(task, starpu_worker_get_memory_node(workerid));
- if (non_ready == 0)
- ready_task_cnt++;
- }
- total_task_cnt++;
- #endif
- }
- return task;
- }
- static struct starpu_task *dmda_pop_task(void)
- {
- struct starpu_task *task;
- int workerid = starpu_worker_get_id();
- struct starpu_fifo_taskq_s *fifo = queue_array[workerid];
- task = _starpu_fifo_pop_task(fifo, -1);
- if (task) {
- double model = task->predicted;
-
- fifo->exp_len -= model;
- fifo->exp_start = starpu_timing_now() + model;
- fifo->exp_end = fifo->exp_start + fifo->exp_len;
- #ifdef STARPU_VERBOSE
- if (task->cl)
- {
- int non_ready = count_non_ready_buffers(task, starpu_worker_get_memory_node(workerid));
- if (non_ready == 0)
- ready_task_cnt++;
- }
- total_task_cnt++;
- #endif
- }
- return task;
- }
- static struct starpu_task *dmda_pop_every_task(void)
- {
- struct starpu_task *new_list;
- int workerid = starpu_worker_get_id();
- struct starpu_fifo_taskq_s *fifo = queue_array[workerid];
- new_list = _starpu_fifo_pop_every_task(fifo, &sched_mutex[workerid], workerid);
- while (new_list)
- {
- double model = new_list->predicted;
- fifo->exp_len -= model;
- fifo->exp_start = starpu_timing_now() + model;
- fifo->exp_end = fifo->exp_start + fifo->exp_len;
-
- new_list = new_list->next;
- }
- return new_list;
- }
- int _starpu_fifo_push_sorted_task(struct starpu_fifo_taskq_s *fifo_queue, pthread_mutex_t *sched_mutex, pthread_cond_t *sched_cond, struct starpu_task *task)
- {
- struct starpu_task_list *list = &fifo_queue->taskq;
- PTHREAD_MUTEX_LOCK(sched_mutex);
- STARPU_TRACE_JOB_PUSH(task, 0);
- if (list->head == NULL)
- {
- list->head = task;
- list->tail = task;
- task->prev = NULL;
- task->next = NULL;
- }
- else {
- struct starpu_task *current = list->head;
- struct starpu_task *prev = NULL;
- while (current)
- {
- if (current->priority >= task->priority)
- break;
- prev = current;
- current = current->next;
- }
- if (prev == NULL)
- {
- /* Insert at the front of the list */
- list->head->prev = task;
- task->prev = NULL;
- task->next = list->head;
- list->head = task;
- }
- else {
- if (current)
- {
- /* Insert between prev and current */
- task->prev = prev;
- prev->next = task;
- task->next = current;
- current->prev = task;
- }
- else {
- /* Insert at the tail of the list */
- list->tail->next = task;
- task->next = NULL;
- task->prev = list->tail;
- list->tail = task;
- }
- }
- }
- fifo_queue->ntasks++;
- fifo_queue->nprocessed++;
- PTHREAD_COND_SIGNAL(sched_cond);
- PTHREAD_MUTEX_UNLOCK(sched_mutex);
- return 0;
- }
- static int push_task_on_best_worker(struct starpu_task *task, int best_workerid, double predicted, int prio)
- {
- /* make sure someone coule execute that task ! */
- STARPU_ASSERT(best_workerid != -1);
- struct starpu_fifo_taskq_s *fifo;
- fifo = queue_array[best_workerid];
- fifo->exp_end += predicted;
- fifo->exp_len += predicted;
- task->predicted = predicted;
- unsigned memory_node = starpu_worker_get_memory_node(best_workerid);
- if (starpu_get_prefetch_flag())
- starpu_prefetch_task_input_on_node(task, memory_node);
- switch (prio) {
- case 1:
- return _starpu_fifo_push_prio_task(queue_array[best_workerid],
- &sched_mutex[best_workerid], &sched_cond[best_workerid], task);
- case 2:
- return _starpu_fifo_push_sorted_task(queue_array[best_workerid],
- &sched_mutex[best_workerid], &sched_cond[best_workerid], task);
- default:
- return _starpu_fifo_push_task(queue_array[best_workerid],
- &sched_mutex[best_workerid], &sched_cond[best_workerid], task);
- }
- }
- static int _dm_push_task(struct starpu_task *task, unsigned prio)
- {
- /* find the queue */
- struct starpu_fifo_taskq_s *fifo;
- unsigned worker;
- int best = -1;
- double best_exp_end = 0.0;
- double model_best = 0.0;
- int ntasks_best = -1;
- double ntasks_best_end = 0.0;
- /* A priori, we know all estimations */
- int unknown = 0;
- for (worker = 0; worker < nworkers; worker++)
- {
- double exp_end;
-
- fifo = queue_array[worker];
- fifo->exp_start = STARPU_MAX(fifo->exp_start, starpu_timing_now());
- fifo->exp_end = STARPU_MAX(fifo->exp_end, starpu_timing_now());
- if (!starpu_worker_may_execute_task(worker, task))
- {
- /* no one on that queue may execute this task */
- continue;
- }
- enum starpu_perf_archtype perf_arch = starpu_worker_get_perf_archtype(worker);
- double local_length = starpu_task_expected_length(task, perf_arch);
- double ntasks_end = fifo->ntasks / starpu_worker_get_relative_speedup(perf_arch);
- if (ntasks_best == -1 || ntasks_end < ntasks_best_end) {
- ntasks_best_end = ntasks_end;
- ntasks_best = worker;
- }
- if (local_length <= 0.0)
- /* there is no prediction available for that task
- * with that arch yet, we want to speed-up calibration time
- * so we switch to distributing tasks greedily */
- unknown = 1;
- if (unknown)
- continue;
- exp_end = fifo->exp_start + fifo->exp_len + local_length;
- if (best == -1 || exp_end < best_exp_end)
- {
- /* a better solution was found */
- best_exp_end = exp_end;
- best = worker;
- model_best = local_length;
- }
- }
- if (unknown) {
- best = ntasks_best;
- model_best = 0.0;
- }
-
- /* we should now have the best worker in variable "best" */
- return push_task_on_best_worker(task, best, model_best, prio);
- }
- static int _dmda_push_task(struct starpu_task *task, unsigned prio)
- {
- /* find the queue */
- struct starpu_fifo_taskq_s *fifo;
- unsigned worker;
- int best = -1;
-
- /* this flag is set if the corresponding worker is selected because
- there is no performance prediction available yet */
- int forced_best = -1;
- double local_task_length[nworkers];
- double local_data_penalty[nworkers];
- double local_power[nworkers];
- double exp_end[nworkers];
- double fitness[nworkers];
- double best_exp_end = 10e240;
- double model_best = 0.0;
- double penality_best = 0.0;
- int ntasks_best = -1;
- double ntasks_best_end = 0.0;
- /* A priori, we know all estimations */
- int unknown = 0;
- for (worker = 0; worker < nworkers; worker++)
- {
- fifo = queue_array[worker];
- fifo->exp_start = STARPU_MAX(fifo->exp_start, starpu_timing_now());
- fifo->exp_end = STARPU_MAX(fifo->exp_end, starpu_timing_now());
- if (!starpu_worker_may_execute_task(worker, task))
- {
- /* no one on that queue may execute this task */
- continue;
- }
- enum starpu_perf_archtype perf_arch = starpu_worker_get_perf_archtype(worker);
- local_task_length[worker] = starpu_task_expected_length(task, perf_arch);
- unsigned memory_node = starpu_worker_get_memory_node(worker);
- local_data_penalty[worker] = starpu_data_expected_penalty(memory_node, task);
- double ntasks_end = fifo->ntasks / starpu_worker_get_relative_speedup(perf_arch);
- if (ntasks_best == -1 || ntasks_end < ntasks_best_end) {
- ntasks_best_end = ntasks_end;
- ntasks_best = worker;
- }
- if (local_task_length[worker] <= 0.0)
- /* there is no prediction available for that task
- * with that arch yet, we want to speed-up calibration time
- * so we switch to distributing tasks greedily */
- unknown = 1;
- if (unknown)
- continue;
- exp_end[worker] = fifo->exp_start + fifo->exp_len + local_task_length[worker];
- if (exp_end[worker] < best_exp_end)
- {
- /* a better solution was found */
- best_exp_end = exp_end[worker];
- }
- local_power[worker] = starpu_task_expected_power(task, perf_arch);
- if (local_power[worker] == -1.0)
- local_power[worker] = 0.;
- }
- if (unknown)
- forced_best = ntasks_best;
- double best_fitness = -1;
-
- if (forced_best == -1)
- {
- for (worker = 0; worker < nworkers; worker++)
- {
- fifo = queue_array[worker];
-
- if (!starpu_worker_may_execute_task(worker, task))
- {
- /* no one on that queue may execute this task */
- continue;
- }
-
- fitness[worker] = alpha*(exp_end[worker] - best_exp_end)
- + beta*(local_data_penalty[worker])
- + _gamma*(local_power[worker]);
- if (best == -1 || fitness[worker] < best_fitness)
- {
- /* we found a better solution */
- best_fitness = fitness[worker];
- best = worker;
- // _STARPU_DEBUG("best fitness (worker %d) %le = alpha*(%le) + beta(%le) +gamma(%le)\n", worker, best_fitness, exp_end[worker] - best_exp_end, local_data_penalty[worker], local_power[worker]);
- }
- }
- }
- STARPU_ASSERT(forced_best != -1 || best != -1);
-
- if (forced_best != -1)
- {
- /* there is no prediction available for that task
- * with that arch we want to speed-up calibration time
- * so we force this measurement */
- best = forced_best;
- model_best = 0.0;
- penality_best = 0.0;
- }
- else
- {
- model_best = local_task_length[best];
- penality_best = local_data_penalty[best];
- }
- /* we should now have the best worker in variable "best" */
- return push_task_on_best_worker(task, best, model_best, prio);
- }
- static int dmda_push_sorted_task(struct starpu_task *task)
- {
- return _dmda_push_task(task, 2);
- }
- static int dm_push_prio_task(struct starpu_task *task)
- {
- return _dm_push_task(task, 1);
- }
- static int dm_push_task(struct starpu_task *task)
- {
- if (task->priority > 0)
- return _dm_push_task(task, 1);
- return _dm_push_task(task, 0);
- }
- static int dmda_push_prio_task(struct starpu_task *task)
- {
- return _dmda_push_task(task, 1);
- }
- static int dmda_push_task(struct starpu_task *task)
- {
- if (task->priority > 0)
- return _dmda_push_task(task, 1);
- return _dmda_push_task(task, 0);
- }
- static void initialize_dmda_policy(struct starpu_machine_topology_s *topology,
- __attribute__ ((unused)) struct starpu_sched_policy_s *_policy)
- {
- nworkers = topology->nworkers;
- const char *strval_alpha = getenv("STARPU_SCHED_ALPHA");
- if (strval_alpha)
- alpha = atof(strval_alpha);
- const char *strval_beta = getenv("STARPU_SCHED_BETA");
- if (strval_beta)
- beta = atof(strval_beta);
- const char *strval_gamma = getenv("STARPU_SCHED_GAMMA");
- if (strval_gamma)
- _gamma = atof(strval_gamma);
- unsigned workerid;
- for (workerid = 0; workerid < nworkers; workerid++)
- {
- queue_array[workerid] = _starpu_create_fifo();
-
- PTHREAD_MUTEX_INIT(&sched_mutex[workerid], NULL);
- PTHREAD_COND_INIT(&sched_cond[workerid], NULL);
-
- starpu_worker_set_sched_condition(workerid, &sched_cond[workerid], &sched_mutex[workerid]);
- }
- }
- static void initialize_dmda_sorted_policy(struct starpu_machine_topology_s *topology,
- struct starpu_sched_policy_s *_policy)
- {
- initialize_dmda_policy(topology, _policy);
- /* The application may use any integer */
- starpu_sched_set_min_priority(INT_MIN);
- starpu_sched_set_max_priority(INT_MAX);
- }
- static void deinitialize_dmda_policy(struct starpu_machine_topology_s *topology,
- __attribute__ ((unused)) struct starpu_sched_policy_s *_policy)
- {
- unsigned workerid;
- for (workerid = 0; workerid < topology->nworkers; workerid++)
- _starpu_destroy_fifo(queue_array[workerid]);
- _STARPU_DEBUG("total_task_cnt %ld ready_task_cnt %ld -> %f\n", total_task_cnt, ready_task_cnt, (100.0f*ready_task_cnt)/total_task_cnt);
- }
- struct starpu_sched_policy_s _starpu_sched_dm_policy = {
- .init_sched = initialize_dmda_policy,
- .deinit_sched = deinitialize_dmda_policy,
- .push_task = dm_push_task,
- .push_prio_task = dm_push_prio_task,
- .pop_task = dmda_pop_task,
- .post_exec_hook = NULL,
- .pop_every_task = dmda_pop_every_task,
- .policy_name = "dm",
- .policy_description = "performance model"
- };
- struct starpu_sched_policy_s _starpu_sched_dmda_policy = {
- .init_sched = initialize_dmda_policy,
- .deinit_sched = deinitialize_dmda_policy,
- .push_task = dmda_push_task,
- .push_prio_task = dmda_push_prio_task,
- .pop_task = dmda_pop_task,
- .post_exec_hook = NULL,
- .pop_every_task = dmda_pop_every_task,
- .policy_name = "dmda",
- .policy_description = "data-aware performance model"
- };
- struct starpu_sched_policy_s _starpu_sched_dmda_sorted_policy = {
- .init_sched = initialize_dmda_sorted_policy,
- .deinit_sched = deinitialize_dmda_policy,
- .push_task = dmda_push_sorted_task,
- .push_prio_task = dmda_push_sorted_task,
- .pop_task = dmda_pop_ready_task,
- .post_exec_hook = NULL,
- .pop_every_task = dmda_pop_every_task,
- .policy_name = "dmdas",
- .policy_description = "data-aware performance model (sorted)"
- };
- struct starpu_sched_policy_s _starpu_sched_dmda_ready_policy = {
- .init_sched = initialize_dmda_policy,
- .deinit_sched = deinitialize_dmda_policy,
- .push_task = dmda_push_task,
- .push_prio_task = dmda_push_prio_task,
- .pop_task = dmda_pop_ready_task,
- .post_exec_hook = NULL,
- .pop_every_task = dmda_pop_every_task,
- .policy_name = "dmdar",
- .policy_description = "data-aware performance model (ready)"
- };
|