reformat, following initial convention
This commit is contained in:
parent
f95dcaf491
commit
6b891cf338
5 changed files with 266 additions and 241 deletions
5
.clang-format
Normal file
5
.clang-format
Normal file
|
@ -0,0 +1,5 @@
|
|||
IndentWidth: 4
|
||||
InsertBraces: true
|
||||
BreakBeforeBraces: Linux
|
||||
AlwaysBreakAfterReturnType: TopLevelDefinitions
|
||||
SpaceBeforeParens: Never
|
|
@ -31,7 +31,9 @@ struct scheduler {
|
|||
int exit;
|
||||
};
|
||||
|
||||
static inline int sched_default_threads(void) {
|
||||
static inline int
|
||||
sched_default_threads(void)
|
||||
{
|
||||
return sysconf(_SC_NPROCESSORS_ONLN);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
#include "../includes/quicksort.h"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
return benchmark(argc, argv);
|
||||
}
|
||||
|
|
|
@ -1,20 +1,22 @@
|
|||
#include "../includes/quicksort.h"
|
||||
|
||||
int partition(int *a, int lo, int hi) {
|
||||
int
|
||||
partition(int *a, int lo, int hi)
|
||||
{
|
||||
int pivot = a[lo];
|
||||
int i = lo - 1;
|
||||
int j = hi + 1;
|
||||
int t;
|
||||
while (1) {
|
||||
while(1) {
|
||||
do {
|
||||
i++;
|
||||
} while (a[i] < pivot);
|
||||
} while(a[i] < pivot);
|
||||
|
||||
do {
|
||||
j--;
|
||||
} while (a[j] > pivot);
|
||||
} while(a[j] > pivot);
|
||||
|
||||
if (i >= j) {
|
||||
if(i >= j) {
|
||||
return j;
|
||||
}
|
||||
|
||||
|
@ -29,9 +31,11 @@ struct quicksort_args {
|
|||
int lo, hi;
|
||||
};
|
||||
|
||||
struct quicksort_args *new_args(int *a, int lo, int hi) {
|
||||
struct quicksort_args *
|
||||
new_args(int *a, int lo, int hi)
|
||||
{
|
||||
struct quicksort_args *args = malloc(sizeof(struct quicksort_args));
|
||||
if (args == NULL) {
|
||||
if(args == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
@ -41,10 +45,12 @@ struct quicksort_args *new_args(int *a, int lo, int hi) {
|
|||
return args;
|
||||
}
|
||||
|
||||
void quicksort_serial(int *a, int lo, int hi) {
|
||||
void
|
||||
quicksort_serial(int *a, int lo, int hi)
|
||||
{
|
||||
int p;
|
||||
|
||||
if (lo >= hi) {
|
||||
if(lo >= hi) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -53,7 +59,9 @@ void quicksort_serial(int *a, int lo, int hi) {
|
|||
quicksort_serial(a, p + 1, hi);
|
||||
}
|
||||
|
||||
void quicksort(void *closure, struct scheduler *s) {
|
||||
void
|
||||
quicksort(void *closure, struct scheduler *s)
|
||||
{
|
||||
struct quicksort_args *args = (struct quicksort_args *)closure;
|
||||
int *a = args->a;
|
||||
int lo = args->lo;
|
||||
|
@ -63,11 +71,11 @@ void quicksort(void *closure, struct scheduler *s) {
|
|||
|
||||
free(closure);
|
||||
|
||||
if (lo >= hi) {
|
||||
if(lo >= hi) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (hi - lo <= 128) {
|
||||
if(hi - lo <= 128) {
|
||||
quicksort_serial(a, lo, hi);
|
||||
return;
|
||||
}
|
||||
|
@ -79,7 +87,9 @@ void quicksort(void *closure, struct scheduler *s) {
|
|||
assert(rc >= 0);
|
||||
}
|
||||
|
||||
int benchmark(int argc, char **argv) {
|
||||
int
|
||||
benchmark(int argc, char **argv)
|
||||
{
|
||||
int *a;
|
||||
struct timespec begin, end;
|
||||
double delay;
|
||||
|
@ -88,12 +98,12 @@ int benchmark(int argc, char **argv) {
|
|||
int nthreads = -1;
|
||||
int serial = 0;
|
||||
|
||||
while (1) {
|
||||
while(1) {
|
||||
int opt = getopt(argc, argv, "sn:t:");
|
||||
if (opt < 0) {
|
||||
if(opt < 0) {
|
||||
break;
|
||||
}
|
||||
switch (opt) {
|
||||
switch(opt) {
|
||||
case 's':
|
||||
serial = 1;
|
||||
break;
|
||||
|
@ -108,28 +118,28 @@ int benchmark(int argc, char **argv) {
|
|||
}
|
||||
}
|
||||
|
||||
if (n <= 0) {
|
||||
if(n <= 0) {
|
||||
goto usage;
|
||||
}
|
||||
if (nthreads < 0 && !serial) {
|
||||
if(nthreads < 0 && !serial) {
|
||||
goto usage;
|
||||
}
|
||||
|
||||
a = malloc(n * sizeof(int));
|
||||
|
||||
unsigned long long s = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
for(int i = 0; i < n; i++) {
|
||||
s = s * 6364136223846793005ULL + 1442695040888963407;
|
||||
a[i] = (int)((s >> 33) & 0x7FFFFFFF);
|
||||
}
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &begin);
|
||||
|
||||
if (serial) {
|
||||
if(serial) {
|
||||
quicksort_serial(a, 0, n - 1);
|
||||
} else {
|
||||
rc =
|
||||
sched_init(nthreads, (n + 127) / 128, quicksort, new_args(a, 0, n - 1));
|
||||
rc = sched_init(nthreads, (n + 127) / 128, quicksort,
|
||||
new_args(a, 0, n - 1));
|
||||
assert(rc >= 0);
|
||||
}
|
||||
|
||||
|
@ -138,7 +148,7 @@ int benchmark(int argc, char **argv) {
|
|||
(begin.tv_sec + begin.tv_nsec / 1000000000.0);
|
||||
printf("Done in %lf seconds.\n", delay);
|
||||
|
||||
for (int i = 0; i < n - 1; i++) {
|
||||
for(int i = 0; i < n - 1; i++) {
|
||||
assert(a[i] <= a[i + 1]);
|
||||
}
|
||||
|
||||
|
|
38
src/sched.c
38
src/sched.c
|
@ -6,13 +6,15 @@
|
|||
static struct scheduler sched;
|
||||
|
||||
/* Lance une tâche de la pile */
|
||||
void *worker_routine(void *arg) {
|
||||
void *
|
||||
worker_routine(void *arg)
|
||||
{
|
||||
struct scheduler *s = (struct scheduler *)arg;
|
||||
|
||||
while (1) {
|
||||
while(1) {
|
||||
pthread_mutex_lock(&s->mutex);
|
||||
|
||||
if (s->exit == 1) {
|
||||
if(s->exit == 1) {
|
||||
pthread_mutex_unlock(&s->mutex);
|
||||
printf("finalement..rien a faire, ciao\n");
|
||||
|
||||
|
@ -20,7 +22,7 @@ void *worker_routine(void *arg) {
|
|||
}
|
||||
|
||||
// S'il on a rien à faire
|
||||
if (s->top == -1) {
|
||||
if(s->top == -1) {
|
||||
// printf("attend..\n");
|
||||
pthread_cond_wait(&s->cond, &s->mutex);
|
||||
pthread_mutex_unlock(&s->mutex);
|
||||
|
@ -39,7 +41,7 @@ void *worker_routine(void *arg) {
|
|||
f(closure, s);
|
||||
|
||||
// Signale s'il n'y a plus rien à faire
|
||||
if (s->top == -1) {
|
||||
if(s->top == -1) {
|
||||
printf("va falloir partir\n");
|
||||
pthread_mutex_lock(&s->mutex);
|
||||
s->exit = 1;
|
||||
|
@ -51,8 +53,10 @@ void *worker_routine(void *arg) {
|
|||
return NULL;
|
||||
}
|
||||
|
||||
int sched_init(int nthreads, int qlen, taskfunc f, void *closure) {
|
||||
if (nthreads == 0) {
|
||||
int
|
||||
sched_init(int nthreads, int qlen, taskfunc f, void *closure)
|
||||
{
|
||||
if(nthreads == 0) {
|
||||
nthreads = sched_default_threads();
|
||||
}
|
||||
|
||||
|
@ -63,31 +67,31 @@ int sched_init(int nthreads, int qlen, taskfunc f, void *closure) {
|
|||
sched.top = -1;
|
||||
sched.exit = 0;
|
||||
|
||||
if (pthread_mutex_init(&sched.mutex, NULL) != 0) {
|
||||
if(pthread_mutex_init(&sched.mutex, NULL) != 0) {
|
||||
fprintf(stderr, "Can't init mutex\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (pthread_cond_init(&sched.cond, NULL) != 0) {
|
||||
if(pthread_cond_init(&sched.cond, NULL) != 0) {
|
||||
fprintf(stderr, "Can't init condition variable\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
pthread_t threads[nthreads];
|
||||
for (int i = 0; i < nthreads; ++i) {
|
||||
if (pthread_create(&threads[i], NULL, worker_routine, &sched) != 0) {
|
||||
for(int i = 0; i < nthreads; ++i) {
|
||||
if(pthread_create(&threads[i], NULL, worker_routine, &sched) != 0) {
|
||||
fprintf(stderr, "Can't create threads\n");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (sched_spawn(f, closure, &sched) != 0) {
|
||||
if(sched_spawn(f, closure, &sched) != 0) {
|
||||
fprintf(stderr, "Can't create the initial task\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (int i = 0; i < nthreads; ++i) {
|
||||
if ((pthread_join(threads[i], NULL) != 0)) {
|
||||
for(int i = 0; i < nthreads; ++i) {
|
||||
if((pthread_join(threads[i], NULL) != 0)) {
|
||||
fprintf(stderr, "Can't wait the thread %d\n", i);
|
||||
return -1;
|
||||
}
|
||||
|
@ -96,10 +100,12 @@ int sched_init(int nthreads, int qlen, taskfunc f, void *closure) {
|
|||
return 1;
|
||||
}
|
||||
|
||||
int sched_spawn(taskfunc f, void *closure, struct scheduler *s) {
|
||||
int
|
||||
sched_spawn(taskfunc f, void *closure, struct scheduler *s)
|
||||
{
|
||||
pthread_mutex_lock(&s->mutex);
|
||||
|
||||
if (s->top + 1 >= MAX_TASKS) {
|
||||
if(s->top + 1 >= MAX_TASKS) {
|
||||
pthread_mutex_unlock(&s->mutex);
|
||||
errno = EAGAIN;
|
||||
fprintf(stderr, "Stack is full\n");
|
||||
|
|
Reference in a new issue