Commit 036cf356 authored by Pedro Gonnet's avatar Pedro Gonnet
Browse files

simplify task activation, i.e. make it less error-prone.

parent e6056fc4
......@@ -915,13 +915,11 @@ int cell_unskip_tasks(struct cell *c, struct scheduler *s) {
if (t->type == task_type_pair) {
if (!(ci->sorted & (1 << t->flags))) {
atomic_or(&ci->sorts->flags, (1 << t->flags));
if (atomic_cas(&ci->sorts->skip, 1, 0))
scheduler_add_active(s, ci->sorts);
scheduler_activate(s, ci->sorts);
}
if (!(cj->sorted & (1 << t->flags))) {
atomic_or(&cj->sorts->flags, (1 << t->flags));
if (atomic_cas(&cj->sorts->skip, 1, 0))
scheduler_add_active(s, cj->sorts);
scheduler_activate(s, cj->sorts);
}
}
......@@ -948,19 +946,19 @@ int cell_unskip_tasks(struct cell *c, struct scheduler *s) {
l = l->next)
;
if (l == NULL) error("Missing link to send_xv task.");
if (atomic_cas(&l->t->skip, 1, 0)) scheduler_add_active(s, l->t);
scheduler_activate(s, l->t);
for (l = cj->send_rho; l != NULL && l->t->cj->nodeID != ci->nodeID;
l = l->next)
;
if (l == NULL) error("Missing link to send_rho task.");
if (atomic_cas(&l->t->skip, 1, 0)) scheduler_add_active(s, l->t);
scheduler_activate(s, l->t);
for (l = cj->send_ti; l != NULL && l->t->cj->nodeID != ci->nodeID;
l = l->next)
;
if (l == NULL) error("Missing link to send_ti task.");
if (atomic_cas(&l->t->skip, 1, 0)) scheduler_add_active(s, l->t);
scheduler_activate(s, l->t);
} else if (cj->nodeID != engine_rank) {
......@@ -974,19 +972,19 @@ int cell_unskip_tasks(struct cell *c, struct scheduler *s) {
l = l->next)
;
if (l == NULL) error("Missing link to send_xv task.");
if (atomic_cas(&l->t->skip, 1, 0)) scheduler_add_active(s, l->t);
scheduler_activate(s, l->t);
for (l = ci->send_rho; l != NULL && l->t->cj->nodeID != cj->nodeID;
l = l->next)
;
if (l == NULL) error("Missing link to send_rho task.");
if (atomic_cas(&l->t->skip, 1, 0)) scheduler_add_active(s, l->t);
scheduler_activate(s, l->t);
for (l = ci->send_ti; l != NULL && l->t->cj->nodeID != cj->nodeID;
l = l->next)
;
if (l == NULL) error("Missing link to send_ti task.");
if (atomic_cas(&l->t->skip, 1, 0)) scheduler_add_active(s, l->t);
scheduler_activate(s, l->t);
}
#endif
}
......@@ -994,25 +992,18 @@ int cell_unskip_tasks(struct cell *c, struct scheduler *s) {
/* Unskip all the other task types. */
for (struct link *l = c->gradient; l != NULL; l = l->next)
if (atomic_cas(&l->t->skip, 1, 0)) scheduler_add_active(s, l->t);
scheduler_activate(s, l->t);
for (struct link *l = c->force; l != NULL; l = l->next)
if (atomic_cas(&l->t->skip, 1, 0)) scheduler_add_active(s, l->t);
scheduler_activate(s, l->t);
for (struct link *l = c->grav; l != NULL; l = l->next)
if (atomic_cas(&l->t->skip, 1, 0)) scheduler_add_active(s, l->t);
if (c->extra_ghost != NULL && atomic_cas(&c->extra_ghost->skip, 1, 0))
scheduler_add_active(s, c->extra_ghost);
if (c->ghost != NULL && atomic_cas(&c->ghost->skip, 1, 0))
scheduler_add_active(s, c->ghost);
if (c->init != NULL && atomic_cas(&c->init->skip, 1, 0))
scheduler_add_active(s, c->init);
if (c->kick != NULL && atomic_cas(&c->kick->skip, 1, 0))
scheduler_add_active(s, c->kick);
if (c->cooling != NULL && atomic_cas(&c->cooling->skip, 1, 0))
scheduler_add_active(s, c->cooling);
if (c->sourceterms != NULL && atomic_cas(&c->sourceterms->skip, 1, 0))
scheduler_add_active(s, c->sourceterms);
if (c->grav_external != NULL && atomic_cas(&c->grav_external->skip, 1, 0))
scheduler_add_active(s, c->grav_external);
scheduler_activate(s, l->t);
if (c->extra_ghost != NULL) scheduler_activate(s, c->extra_ghost);
if (c->ghost != NULL) scheduler_activate(s, c->ghost);
if (c->init != NULL) scheduler_activate(s, c->init);
if (c->kick != NULL) scheduler_activate(s, c->kick);
if (c->cooling != NULL) scheduler_activate(s, c->cooling);
if (c->sourceterms != NULL) scheduler_activate(s, c->sourceterms);
if (c->grav_external != NULL) scheduler_activate(s, c->grav_external);
return 0;
}
......@@ -2019,10 +2019,7 @@ void engine_marktasks_mapper(void *map_data, int num_elements,
t->type == task_type_sourceterms || t->type == task_type_sub_self) {
/* Set this task's skip. */
if ((t->skip = (t->ci->ti_end_min > ti_end)))
continue;
else
scheduler_add_active(s, t);
if (t->ci->ti_end_min <= ti_end) scheduler_activate(s, t);
}
/* Pair? */
......@@ -2040,10 +2037,9 @@ void engine_marktasks_mapper(void *map_data, int num_elements,
*rebuild_space = 1;
/* Set this task's skip. */
if ((t->skip = (ci->ti_end_min > ti_end && cj->ti_end_min > ti_end)) == 1)
continue;
else scheduler_add_active(s, t);
if (ci->ti_end_min <= ti_end || cj->ti_end_min <= ti_end)
scheduler_activate(s, t);
/* If this is not a density task, we don't have to do any of the below. */
if (t->subtype != task_subtype_density) continue;
......@@ -2051,13 +2047,11 @@ void engine_marktasks_mapper(void *map_data, int num_elements,
if (t->type == task_type_pair) {
if (!(ci->sorted & (1 << t->flags))) {
atomic_or(&ci->sorts->flags, (1 << t->flags));
if (atomic_cas(&ci->sorts->skip, 1, 0))
scheduler_add_active(s, ci->sorts);
scheduler_activate(s, ci->sorts);
}
if (!(cj->sorted & (1 << t->flags))) {
atomic_or(&cj->sorts->flags, (1 << t->flags));
if (atomic_cas(&cj->sorts->skip, 1, 0))
scheduler_add_active(s, cj->sorts);
scheduler_activate(s, cj->sorts);
}
}
......@@ -2077,19 +2071,19 @@ void engine_marktasks_mapper(void *map_data, int num_elements,
l = l->next)
;
if (l == NULL) error("Missing link to send_xv task.");
if (atomic_cas(&l->t->skip, 1, 0)) scheduler_add_active(s, l->t);
scheduler_activate(s, l->t);
for (l = cj->send_rho; l != NULL && l->t->cj->nodeID != ci->nodeID;
l = l->next)
;
if (l == NULL) error("Missing link to send_rho task.");
if (atomic_cas(&l->t->skip, 1, 0)) scheduler_add_active(s, l->t);
scheduler_activate(s, l->t);
for (l = cj->send_ti; l != NULL && l->t->cj->nodeID != ci->nodeID;
l = l->next)
;
if (l == NULL) error("Missing link to send_ti task.");
if (atomic_cas(&l->t->skip, 1, 0)) scheduler_add_active(s, l->t);
scheduler_activate(s, l->t);
} else if (cj->nodeID != engine_rank) {
......@@ -2103,19 +2097,19 @@ void engine_marktasks_mapper(void *map_data, int num_elements,
l = l->next)
;
if (l == NULL) error("Missing link to send_xv task.");
if (atomic_cas(&l->t->skip, 1, 0)) scheduler_add_active(s, l->t);
scheduler_activate(s, l->t);
for (l = ci->send_rho; l != NULL && l->t->cj->nodeID != cj->nodeID;
l = l->next)
;
if (l == NULL) error("Missing link to send_rho task.");
if (atomic_cas(&l->t->skip, 1, 0)) scheduler_add_active(s, l->t);
scheduler_activate(s, l->t);
for (l = ci->send_ti; l != NULL && l->t->cj->nodeID != cj->nodeID;
l = l->next)
;
if (l == NULL) error("Missing link to send_ti task.");
if (atomic_cas(&l->t->skip, 1, 0)) scheduler_add_active(s, l->t);
scheduler_activate(s, l->t);
}
#endif
......@@ -2125,19 +2119,13 @@ void engine_marktasks_mapper(void *map_data, int num_elements,
else if (t->type == task_type_kick) {
t->ci->updated = 0;
t->ci->g_updated = 0;
if ((t->skip = (t->ci->ti_end_min > ti_end)))
continue;
else
scheduler_add_active(s, t);
if (t->ci->ti_end_min <= ti_end) scheduler_activate(s, t);
}
/* Init? */
else if (t->type == task_type_init) {
/* Set this task's skip. */
if ((t->skip = (t->ci->ti_end_min > ti_end)))
continue;
else
scheduler_add_active(s, t);
if (t->ci->ti_end_min <= ti_end) scheduler_activate(s, t);
}
}
}
......
......@@ -50,18 +50,6 @@
#include "task.h"
#include "timers.h"
/**
* @brief Add a task to the list of active tasks.
*
* @param s The #scheduler.
* @param t The task to be added.
*/
void scheduler_add_active(struct scheduler *s, struct task *t) {
int ind = atomic_inc(&s->active_count);
s->tid_active[ind] = t - s->tasks;
}
/**
* @brief Re-set the list of active tasks.
*/
......@@ -1137,8 +1125,8 @@ void scheduler_start(struct scheduler *s, unsigned int mask,
pthread_cond_broadcast(&s->sleep_cond);
pthread_mutex_unlock(&s->sleep_mutex);
message("enqueueing tasks took %.3f %s." ,
clocks_from_ticks( getticks() - tic ), clocks_getunit());
message("enqueueing tasks took %.3f %s.", clocks_from_ticks(getticks() - tic),
clocks_getunit());
}
/**
......
......@@ -33,6 +33,7 @@
/* Includes. */
#include "cell.h"
#include "inline.h"
#include "lock.h"
#include "queue.h"
#include "task.h"
......@@ -109,8 +110,23 @@ struct scheduler {
int nodeID;
};
/* Inlined functions (for speed). */
/**
* @brief Add a task to the list of active tasks.
*
* @param s The #scheduler.
* @param t The task to be added.
*/
__attribute__((always_inline)) INLINE static void scheduler_activate(
struct scheduler *s, struct task *t) {
if (atomic_cas(&t->skip, 1, 0)) {
int ind = atomic_inc(&s->active_count);
s->tid_active[ind] = t - s->tasks;
}
}
/* Function prototypes. */
void scheduler_add_active(struct scheduler *s, struct task *t);
void scheduler_clear_active(struct scheduler *s);
void scheduler_init(struct scheduler *s, struct space *space, int nr_tasks,
int nr_queues, unsigned int flags, int nodeID,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment