try to steal (not working..)
This commit is contained in:
parent
ffd3d52027
commit
37ff2cfb93
1 changed files with 45 additions and 11 deletions
56
src/sched.c
56
src/sched.c
|
@ -255,32 +255,66 @@ sched_worker(void *arg)
|
||||||
struct scheduler *s = (struct scheduler *)arg;
|
struct scheduler *s = (struct scheduler *)arg;
|
||||||
|
|
||||||
// Récupère le processus courant (index tableau)
|
// Récupère le processus courant (index tableau)
|
||||||
int curr_th = current_thread(s);
|
int curr_th;
|
||||||
|
if((curr_th = current_thread(s)) < 0) {
|
||||||
|
fprintf(stderr, "Worker thread not tracked, exiting...\n");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
while(1) {
|
while(1) {
|
||||||
pthread_mutex_lock(&s->mutex[curr_th]);
|
pthread_mutex_lock(&s->mutex[curr_th]);
|
||||||
|
|
||||||
// S'il on a rien à faire
|
// Si rien à faire
|
||||||
if(s->top[curr_th] == -1) {
|
if(s->top[curr_th] == -1) {
|
||||||
s->nthsleep++;
|
if(s->nthsleep + 1 == s->nthreads) {
|
||||||
if(s->nthsleep == s->nthreads) {
|
|
||||||
// Signal a tout les threads que il n'y a plus rien à faire
|
// Signal a tout les threads que il n'y a plus rien à faire
|
||||||
// si un thread attend une tâche
|
|
||||||
pthread_cond_broadcast(&s->cond[curr_th]);
|
pthread_cond_broadcast(&s->cond[curr_th]);
|
||||||
pthread_mutex_unlock(&s->mutex[curr_th]);
|
pthread_mutex_unlock(&s->mutex[curr_th]);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Essayer de voler une tâche à un autre coeur
|
// Cherche un thread (avec le + de tâches en attente) à voler
|
||||||
if(0) {
|
int stolen = -1;
|
||||||
// TODO:
|
for(int i = 0, size = -1; i < s->nthreads; ++i) {
|
||||||
// - Trouver un coeur avec le + de tâches en attente
|
if(i == curr_th) {
|
||||||
// - Prendre la tâche la plus ancienne (pas LIFO)
|
// On ne se vole pas soi-même
|
||||||
// - La rajouter sur notre pile
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pthread_mutex_lock(&s->mutex[i]);
|
||||||
|
|
||||||
|
if(s->top[i] > size) {
|
||||||
|
stolen = i;
|
||||||
|
size = s->top[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
pthread_mutex_unlock(&s->mutex[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Vole une tâche à un autre thread
|
||||||
|
if(stolen >= 0) {
|
||||||
|
struct task_info theft;
|
||||||
|
pthread_mutex_lock(&s->mutex[stolen]);
|
||||||
|
|
||||||
|
// Actuellement on prend la tâche la plus ancienne en
|
||||||
|
// inversant la première et la dernière
|
||||||
|
// TODO: Récupérer la premiere tâche tout en respectant l'ordre
|
||||||
|
theft = s->tasks[stolen][0];
|
||||||
|
s->tasks[stolen][0] = s->tasks[stolen][s->top[stolen]];
|
||||||
|
s->top[stolen]--;
|
||||||
|
|
||||||
|
pthread_mutex_unlock(&s->mutex[stolen]);
|
||||||
|
|
||||||
|
pthread_mutex_unlock(&s->mutex[curr_th]);
|
||||||
|
|
||||||
|
// Rajoute la tâche sur notre pile
|
||||||
|
sched_spawn_core(theft.f, theft.closure, s, curr_th);
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
s->nthsleep++;
|
||||||
pthread_cond_wait(&s->cond[curr_th], &s->mutex[curr_th]);
|
pthread_cond_wait(&s->cond[curr_th], &s->mutex[curr_th]);
|
||||||
s->nthsleep--;
|
s->nthsleep--;
|
||||||
pthread_mutex_unlock(&s->mutex[curr_th]);
|
pthread_mutex_unlock(&s->mutex[curr_th]);
|
||||||
|
|
Reference in a new issue