From ddb7d1e6db9d4e84e55c54c094ccd0fd34bc3a3f Mon Sep 17 00:00:00 2001 From: Niklas Halle Date: Mon, 22 Jun 2020 16:30:10 +0200 Subject: stuff --- 04_exercise/main.c | 2 +- 04_exercise/quicksort.c | 16 +++++++++++----- 04_exercise/slotmap/slotmap.c | 13 ++++++++----- 04_exercise/threadpool.h | 27 +++++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 11 deletions(-) diff --git a/04_exercise/main.c b/04_exercise/main.c index 8c7b8db..98c0580 100644 --- a/04_exercise/main.c +++ b/04_exercise/main.c @@ -5,7 +5,7 @@ static TASK(long, fib, long) - long fib(long n) { +long fib(long n) { if (n <= 1) return n; diff --git a/04_exercise/quicksort.c b/04_exercise/quicksort.c index ade513b..1f0bfb5 100644 --- a/04_exercise/quicksort.c +++ b/04_exercise/quicksort.c @@ -7,14 +7,20 @@ static size_t partition(int v[], size_t len); -/* TODO: quicksort-Implementation parallelisieren */ +static TASK_QS(void, quicksort) static void quicksort(int list[], size_t len) { + fprintf(stderr, "quicksort call with %p, %lu\n", (void*)list, (ulong)len); + if (len <= 1) return; - + size_t mid = partition(list, len); - quicksort(list, mid); - quicksort(list + mid + 1, len - mid - 1); + + quicksort_fut *first = quicksortAsync((quicksort_fut[]){quicksortFuture(list, mid)}); + quicksort_fut *second = quicksortAsync((quicksort_fut[]){quicksortFuture(list + mid + 1, len - mid - 1)}); + + quicksortAwait(first); + quicksortAwait(second); } size_t partition(int v[], size_t len) { @@ -87,7 +93,7 @@ int main(int argc, const char *argv[]) { atexit(&tpRelease); /* read the length of the list from the command line */ - size_t list_len = 30000000; + size_t list_len = 5; if (argc >= 2) { long arg = strtol(argv[1], (char **)&argv[strlen(argv[1])], 10); if (arg > 0) diff --git a/04_exercise/slotmap/slotmap.c b/04_exercise/slotmap/slotmap.c index 57c4ef5..ca0460e 100644 --- a/04_exercise/slotmap/slotmap.c +++ b/04_exercise/slotmap/slotmap.c @@ -4,18 +4,21 @@ #include "slotmap.h" #include + +#define intptr_t __intptr_t + smHeader smInit(smEntry * slab, size_t size) { for (int i = 0; i < size; ++i) { - slab[i].value = (intptr_t)NULL; + slab[i].value = (__intptr_t)NULL; } return (smHeader){.slab = slab, .size=size}; } int smInsert(smHeader const * header, void * value) { for (size_t i = 0; i< header->size; ++i ) { - intptr_t ptr = atomic_load(&header->slab[i].value); + _Atomic(long) ptr = atomic_load(&header->slab[i].value); if((void *)ptr == NULL) { - intptr_t expected = (intptr_t) NULL; - if(atomic_compare_exchange_strong(&header->slab[i].value, &expected, (intptr_t) value)){ + __intptr_t expected = (__intptr_t) NULL; + if(atomic_compare_exchange_strong(&header->slab[i].value, &expected, (__intptr_t) value)){ return 0; } } @@ -23,7 +26,7 @@ int smInsert(smHeader const * header, void * value) { return -1; } void smDelete(smEntry * node) { - intptr_t oldval = atomic_exchange(&node->value, (intptr_t)(NULL)); + __intptr_t oldval = atomic_exchange(&node->value, (intptr_t)(NULL)); if((void *) oldval == NULL) { fprintf(stderr, "A Node has been double deleted"); } diff --git a/04_exercise/threadpool.h b/04_exercise/threadpool.h index 745c4dd..413f996 100644 --- a/04_exercise/threadpool.h +++ b/04_exercise/threadpool.h @@ -118,4 +118,31 @@ extern void tpAwait(Future *future); return tpAwait(&future->fut), future->res; \ } +#define TASK_QS(TYPE, NAME) \ + TYPE NAME(int *arg1, size_t arg2); \ + \ + typedef struct { \ + Future fut; \ + int* arg1; \ + size_t arg2; \ + } NAME ## _fut; \ + \ + static void NAME ## Thunk(void *args) { \ + NAME ## _fut *data = args; \ + NAME(data->arg1, data->arg2); \ + } \ + static inline NAME ## _fut NAME ## Future(int *arg1, size_t arg2) { \ + return (NAME ## _fut) { \ + .fut = { .fn = &NAME ## Thunk, .status=FUT_WAITING}, \ + .arg1 = arg1, \ + .arg2 = arg2 \ + }; \ + } \ + static inline NAME ## _fut* NAME ## Async(NAME ## _fut *future) { \ + return tpAsync(&future->fut), future; \ + } \ + static inline TYPE NAME ## Await(NAME ## _fut *future) { \ + tpAwait(&future->fut); \ + } + #endif -- cgit v1.2.3-54-g00ecf