diff options
author | Mattias Andrée <maandree@kth.se> | 2024-08-28 16:42:05 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2024-08-28 16:42:05 +0200 |
commit | a24071ae913b223487df78859c8d830f9e69f580 (patch) | |
tree | e2ec712cc29461c82cfdd477e8b1ba961b50018d /barrier.c | |
parent | First commit (diff) | |
download | anysum-a24071ae913b223487df78859c8d830f9e69f580.tar.gz anysum-a24071ae913b223487df78859c8d830f9e69f580.tar.bz2 anysum-a24071ae913b223487df78859c8d830f9e69f580.tar.xz |
Second commit
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
-rw-r--r-- | barrier.c (renamed from createbarriergroup.c) | 58 |
1 files changed, 56 insertions, 2 deletions
diff --git a/createbarriergroup.c b/barrier.c index 694032b..cd7bae8 100644 --- a/createbarriergroup.c +++ b/barrier.c @@ -2,6 +2,35 @@ #include "common.h" +void +barrierwait(pthread_barrier_t *barrier) +{ +#ifndef SINGLE_THREADED + int r = pthread_barrier_wait(barrier); + if (r && r != PTHREAD_BARRIER_SERIAL_THREAD) { + errno = r; + eprintf("pthread_barrier_wait:"); + } +#else + (void) barrier; +#endif +} + + +void +barriersend(struct barrier_group *group, struct global_data *global, void (*action)(struct algorithm *, struct global_data *)) +{ + size_t index; + global->action = action; + if (group->nthreads) + barrierwait(&group->barrier); + for (index = 0; index < global->nalgorithms; index += group->nthreads + 1U) + (*action)(&global->algorithms[index], global); + if (group->nthreads) + barrierwait(&group->barrier); +} + + #ifndef SINGLE_THREADED static void * slaveloop(void *thread_param) @@ -31,8 +60,6 @@ createbarriergroup(struct barrier_group *group_out, size_t count, struct global_ #ifndef SINGLE_THREADED size_t i; - count = MAX(count, 64); - group_out->nthreads = count - 1U; group_out->threads = NULL; if (!group_out->nthreads) @@ -57,3 +84,30 @@ createbarriergroup(struct barrier_group *group_out, size_t count, struct global_ (void) count; #endif } + + +void +killbarriergroup(struct barrier_group *group, struct global_data *global) +{ +#ifndef SINGLE_THREADED + size_t i; + + if (!group->nthreads) + return; + + global->action = NULL; + for (i = 0; i < group->nthreads; i++) + group->threads[i].global = global; + barrierwait(&group->barrier); + + for (i = 0; i < group->nthreads; i++) + if ((errno = pthread_join(group->threads[i].thread, NULL))) + weprintf("pthread_join:"); + pthread_barrier_destroy(&group->barrier); + + free(group->threads); +#else + (void) group; + (void) global; +#endif +} |