Skip to content
Snippets Groups Projects
Commit d130a177 authored by 's avatar
Browse files

Draft version of a fix for #302. There is no obvious reused variable to use a...

Draft version of a fix for #302. There is no obvious reused variable to use a temporary seed for each thread, so instead each thread has a thread-local variable which is uses to seed the rand_r call in scheduler_enqueue. I need to test this doesn't fail at any point (e.g. if we're not in a pthread region) but I'm not sure if it would be encountered outside of a pthread @nnrw56?
parent 5144801d
No related branches found
No related tags found
1 merge request!405Replace calls to rand() by calls to rand_r() in the scheduler
...@@ -1656,7 +1656,8 @@ void *runner_main(void *data) { ...@@ -1656,7 +1656,8 @@ void *runner_main(void *data) {
struct runner *r = (struct runner *)data; struct runner *r = (struct runner *)data;
struct engine *e = r->e; struct engine *e = r->e;
struct scheduler *sched = &e->sched; struct scheduler *sched = &e->sched;
int seed = r->id
pthread_setspecific(sched->local_seed_pointer, &seed);
/* Main loop. */ /* Main loop. */
while (1) { while (1) {
......
...@@ -1305,7 +1305,7 @@ void scheduler_enqueue(struct scheduler *s, struct task *t) { ...@@ -1305,7 +1305,7 @@ void scheduler_enqueue(struct scheduler *s, struct task *t) {
if (qid >= s->nr_queues) error("Bad computed qid."); if (qid >= s->nr_queues) error("Bad computed qid.");
/* If no previous owner, pick a random queue. */ /* If no previous owner, pick a random queue. */
if (qid < 0) qid = rand() % s->nr_queues; if (qid < 0) qid = rand_r((int*)pthread_getspecific(s->local_seed_pointer) ) % s->nr_queues;
/* Increase the waiting counter. */ /* Increase the waiting counter. */
atomic_inc(&s->waiting); atomic_inc(&s->waiting);
...@@ -1537,6 +1537,7 @@ void scheduler_init(struct scheduler *s, struct space *space, int nr_tasks, ...@@ -1537,6 +1537,7 @@ void scheduler_init(struct scheduler *s, struct space *space, int nr_tasks,
s->size = 0; s->size = 0;
s->tasks = NULL; s->tasks = NULL;
s->tasks_ind = NULL; s->tasks_ind = NULL;
pthread_key_create(&s->local_seed_pointer);
scheduler_reset(s, nr_tasks); scheduler_reset(s, nr_tasks);
} }
......
...@@ -102,6 +102,8 @@ struct scheduler { ...@@ -102,6 +102,8 @@ struct scheduler {
/* The node we are working on. */ /* The node we are working on. */
int nodeID; int nodeID;
pthread_key_t local_seed_pointer;
}; };
/* Inlined functions (for speed). */ /* Inlined functions (for speed). */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment