From e6912c40485ecae1c36c9563b6c124c610b11065 Mon Sep 17 00:00:00 2001 From: Thomas Preud'homme Date: Thu, 11 Jun 2009 23:42:15 +0200 Subject: [PATCH] New technique in communication techniques bench * shared_mem become shared_mem_opt * Apparition of shared_mem (no optimisation) --- communication_techniques/Makefile | 2 +- .../include/shared_mem_comm.h | 8 +-- .../include/shared_mem_opt_comm.h | 44 ++++++++++++++ communication_techniques/src/shared_mem.c | 18 ++---- communication_techniques/src/shared_mem_opt.c | 57 +++++++++++++++++++ 5 files changed, 109 insertions(+), 20 deletions(-) create mode 100644 communication_techniques/include/shared_mem_opt_comm.h create mode 100644 communication_techniques/src/shared_mem_opt.c diff --git a/communication_techniques/Makefile b/communication_techniques/Makefile index 9c5e942..f2ba68f 100644 --- a/communication_techniques/Makefile +++ b/communication_techniques/Makefile @@ -18,7 +18,7 @@ LDFLAGS=-L$(LIBDIR) -L$(LOCALDIR)/$(PAPIHIGHLEVELLIBDIR) -Wl,-rpath-link,$(HOME) CC=gcc # Files -BINNAMES=asm_cache_comm c_cache_comm pipe_comm shared_mem_comm +BINNAMES=asm_cache_comm c_cache_comm pipe_comm shared_mem_comm shared_mem_opt_comm BINS=$(patsubst %, $(BINDIR)/%, $(BINNAMES)) MAIN_OBJS=main.o common.o COMMON_LIB_OBJS=common.o diff --git a/communication_techniques/include/shared_mem_comm.h b/communication_techniques/include/shared_mem_comm.h index ec38cc8..2d8c041 100644 --- a/communication_techniques/include/shared_mem_comm.h +++ b/communication_techniques/include/shared_mem_comm.h @@ -28,13 +28,7 @@ extern __thread int cons_idx; struct communication_assoc *create_comm_assoc(void); static inline void send(uintptr_t value) { - static __thread int local_cons_idx = 0; - - if (likely(((prod_idx + 1) % SHARED_SPACE_SIZE) == local_cons_idx)) - { - while (((prod_idx + 1) % SHARED_SPACE_SIZE) == cons_idx); - local_cons_idx = cons_idx; - } + while ((prod_idx + 1) % SHARED_SPACE_SIZE == cons_idx); shared_space[prod_idx] = value; prod_idx = (prod_idx + 1) % SHARED_SPACE_SIZE; } diff --git a/communication_techniques/include/shared_mem_opt_comm.h b/communication_techniques/include/shared_mem_opt_comm.h new file mode 100644 index 0000000..ec38cc8 --- /dev/null +++ b/communication_techniques/include/shared_mem_opt_comm.h @@ -0,0 +1,44 @@ +#ifndef __COMM_H_ +#define __COMM_H_ 1 + +#include + +/* Non standard include */ +#include + +#define SHARED_SPACE_SIZE (2 * BUF_SIZE) + +struct communication_assoc +{ + struct communication_assoc *next; + struct communication_assoc *prev; + pthread_t tid; + uintptr_t *shared_space; + int *cons_idx; + int *prod_idx; +}; + +extern struct communication_assoc assoc_root; + +__BEGIN_DECLS + +extern __thread uintptr_t *shared_space; +extern __thread int prod_idx; +extern __thread int cons_idx; + +struct communication_assoc *create_comm_assoc(void); +static inline void send(uintptr_t value) { + static __thread int local_cons_idx = 0; + + if (likely(((prod_idx + 1) % SHARED_SPACE_SIZE) == local_cons_idx)) + { + while (((prod_idx + 1) % SHARED_SPACE_SIZE) == cons_idx); + local_cons_idx = cons_idx; + } + shared_space[prod_idx] = value; + prod_idx = (prod_idx + 1) % SHARED_SPACE_SIZE; +} + +__END_DECLS + +#endif diff --git a/communication_techniques/src/shared_mem.c b/communication_techniques/src/shared_mem.c index 15249f6..ac03b0d 100644 --- a/communication_techniques/src/shared_mem.c +++ b/communication_techniques/src/shared_mem.c @@ -37,20 +37,14 @@ void reception(void (*on_receive)(uintptr_t)) cur = assoc_root.next; while(cur != &assoc_root) { - int cons_idx, prod_idx; + int cons_idx; - cons_idx = *cur->cons_idx; - do + for(cons_idx = *cur->cons_idx; cons_idx != *cur->prod_idx; cons_idx = (cons_idx + 1) % SHARED_SPACE_SIZE, *cur->cons_idx = cons_idx) { - prod_idx = *cur->prod_idx; - for(; cons_idx != prod_idx; cons_idx = (cons_idx + 1) % SHARED_SPACE_SIZE) - { - uintptr_t tmp; - tmp = cur->shared_space[cons_idx]; - on_receive(tmp); - } - } while (prod_idx != *cur->prod_idx); - *cur->cons_idx = cons_idx; + uintptr_t tmp; + tmp = cur->shared_space[cons_idx]; + on_receive(tmp); + } cur = cur->next; } } diff --git a/communication_techniques/src/shared_mem_opt.c b/communication_techniques/src/shared_mem_opt.c new file mode 100644 index 0000000..15249f6 --- /dev/null +++ b/communication_techniques/src/shared_mem_opt.c @@ -0,0 +1,57 @@ +#include +#include +#include +#include + +/* Non standard include */ +#include +#include + + +__thread uintptr_t *shared_space; +__thread int cons_idx = 0; +__thread int prod_idx = 0; + +struct communication_assoc *create_comm_assoc(void) +{ + struct communication_assoc *assoc; + + shared_space = (uintptr_t *) malloc(SHARED_SPACE_SIZE * sizeof(uintptr_t)); + assoc = (struct communication_assoc *) malloc(sizeof(struct communication_assoc)); + assoc->tid = pthread_self(); + assoc->shared_space = shared_space; + assoc->cons_idx = &cons_idx; + assoc->prod_idx = &prod_idx; + return assoc; +} + +void reception(void (*on_receive)(uintptr_t)) +{ + wait_initialization(); + /* printf("Activate the consumer...\n"); */ + while(cont) + { + struct communication_assoc *cur; + + discover_new_producers(); + cur = assoc_root.next; + while(cur != &assoc_root) + { + int cons_idx, prod_idx; + + cons_idx = *cur->cons_idx; + do + { + prod_idx = *cur->prod_idx; + for(; cons_idx != prod_idx; cons_idx = (cons_idx + 1) % SHARED_SPACE_SIZE) + { + uintptr_t tmp; + tmp = cur->shared_space[cons_idx]; + on_receive(tmp); + } + } while (prod_idx != *cur->prod_idx); + *cur->cons_idx = cons_idx; + cur = cur->next; + } + } +}