Merge branch 'bqv2_buf_end'

This commit is contained in:
Thomas Preud'homme 2012-07-07 23:14:15 +02:00
commit d8c16a4aa3
4 changed files with 151 additions and 36 deletions

View File

@ -14,7 +14,7 @@ COMMDIR:=communication
# I know -finline-functions and -finline-functions-called-once are enabled by # I know -finline-functions and -finline-functions-called-once are enabled by
# -O3 but I did this in case gcc behaviour change one day # -O3 but I did this in case gcc behaviour change one day
CFLAGS:=-c -O3 -finline-functions -finline-functions-called-once -Wall -Wextra -Werror CFLAGS:=-c -O3 -finline-functions -finline-functions-called-once -Wall -Wextra -Werror
LDFLAGS:=-L$(LIBDIR) -lpthread -ldl LDFLAGS:=-L$(LIBDIR) -lpthread -ldl -lrt
# Executables # Executables
CC=gcc CC=gcc

View File

@ -1,6 +1,9 @@
#ifndef _BATCH_QUEUE_COMMON_COMM_H_ #ifndef _BATCH_QUEUE_COMMON_COMM_H_
#define _BATCH_QUEUE_COMMON_COMM_H_ 1 #define _BATCH_QUEUE_COMMON_COMM_H_ 1
#include <stdint.h>
#include <unistd.h>
/* Non standard include */ /* Non standard include */
#include <commtech.h> #include <commtech.h>
@ -10,11 +13,21 @@
struct channel struct channel
{ {
void * volatile buf[2 * BUF_SIZE / sizeof(void *)] __attribute__ ((aligned (CACHE_LINE_SIZE))); /* buf must be the first field to allow the buf_mask test */
void * volatile buf[2][BUF_SIZE / sizeof(void *)] __attribute__ ((aligned (CACHE_LINE_SIZE)));
int unused[20] __attribute__ ((aligned (CACHE_LINE_SIZE))); int unused[20] __attribute__ ((aligned (CACHE_LINE_SIZE)));
volatile unsigned int state:1 __attribute__ ((aligned (CACHE_LINE_SIZE))); volatile unsigned int state:1 __attribute__ ((aligned (CACHE_LINE_SIZE)));
unsigned int sender_idx __attribute__ ((aligned (CACHE_LINE_SIZE)));
unsigned int receiver_idx __attribute__ ((aligned (CACHE_LINE_SIZE))); /* accesses to buf[0] and state */
struct channel *mapping1 __attribute__ ((aligned (CACHE_LINE_SIZE)));
/* accesses to buf[1] */
struct channel *mapping2;
void * volatile *buf_end1;
void * volatile *buf_end2;
void * volatile *sender_ptr __attribute__ ((aligned (CACHE_LINE_SIZE)));
void * volatile *sender_ptr_end;
void * volatile *receiver_ptr __attribute__ ((aligned (CACHE_LINE_SIZE)));
}; };
@ -22,12 +35,22 @@ __BEGIN_DECLS
static inline void send(struct channel *channel, void **addr) static inline void send(struct channel *channel, void **addr)
{ {
channel->buf[channel->sender_idx++] = addr;
channel->sender_idx %= 2 * (BUF_SIZE / sizeof(void *)); *channel->sender_ptr++ = addr;
if (!(channel->sender_idx % (BUF_SIZE / sizeof(void *)))) if (unlikely(channel->sender_ptr == channel->sender_ptr_end))
{ {
while (channel->state); while (channel->state);
channel->state = 1; channel->state = 1;
if (channel->sender_ptr_end == channel->buf_end1)
{
channel->sender_ptr = channel->mapping2->buf[1];
channel->sender_ptr_end = channel->buf_end2;
}
else
{
channel->sender_ptr = channel->buf[0];
channel->sender_ptr_end = channel->buf_end1;
}
} }
} }

View File

@ -1,24 +1,99 @@
#define _BATCH_QUEUE_C_ 1
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <pthread.h> #include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdint.h>
#include <limits.h>
/* Non standard include */ /* Non standard include */
#include <commtech.h> #include <commtech.h>
#include <specific_comm.h> #include <specific_comm.h>
#define BASE_SHM_NAME "/channel"
#define ROUNDUP(size) ((size + page_size) & ~(page_size - 1))
static uintptr_t buf_mask;
static uintptr_t page_mask;
static int nb_buf_entries;
static unsigned int page_size;
void *create_comm_channel(void) void *create_comm_channel(void)
{ {
struct channel *channel; int ret, shm_fd;
static int chan_idx = 0;
char shm_name[NAME_MAX];
struct channel *channel1, *channel2;
if (!posix_memalign((void **) &channel, CACHE_LINE_SIZE, sizeof(struct channel))) channel1 = NULL;
channel2 = NULL;
ret = sysconf(_SC_PAGESIZE);
if (ret == -1)
{ {
channel->receiver_idx = 0; perror("BatchQueue init sysconf");
channel->state = 0; return NULL;
channel->sender_idx = 0;
return channel;
} }
return NULL; page_size = ret;
page_mask = ~((uintptr_t) (page_size - 1));
ret = snprintf(shm_name, NAME_MAX, BASE_SHM_NAME"%d\n", chan_idx);
if (ret < 0)
{
fprintf(stderr, "BatchQueue init snprintf failed\n");
return NULL;
} else if (ret >= NAME_MAX)
{
fprintf(stderr, "Too many channels created: impossible to ");
fprintf(stderr, "create a channel named "BASE_SHM_NAME);
fprintf(stderr, "%d\n", chan_idx);
return NULL;
}
shm_fd = shm_open(shm_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
if (shm_fd == -1)
{
perror("BatchQueue init shm_open failed");
return NULL;
}
ret = ftruncate(shm_fd, sizeof(*channel1));
if (ret == -1)
{
perror("BatchQueue init ftruncate failed");
goto close_file;
}
channel1 = mmap(NULL, ROUNDUP(sizeof(*channel1)),
PROT_READ | PROT_WRITE, MAP_PRIVATE, shm_fd, 0);
channel2 = mmap(NULL, ROUNDUP(sizeof(*channel1)),
PROT_READ | PROT_WRITE, MAP_PRIVATE, shm_fd, 0);
if ((channel1 == MAP_FAILED) || (channel2 == MAP_FAILED))
{
perror("Batchqueue init mmap failed");
channel1 = NULL;
goto close_file;
}
channel1->mapping1 = channel1;
channel1->mapping2 = channel2;
channel1->state = 0;
channel1->sender_ptr = channel1->buf[0];
channel1->receiver_ptr = channel1->buf[0];
channel1->buf_end1 = channel1->buf[0] +
(sizeof channel1->buf[0] / sizeof channel1->buf[0][0]);
channel1->buf_end2 = channel1->mapping2->buf[1] +
(sizeof channel1->buf[1] / sizeof channel1->buf[1][0]);
channel1->sender_ptr_end = channel1->buf_end1;
buf_mask = sizeof channel1->buf[0] - 1;
nb_buf_entries = sizeof channel1->buf[0] / sizeof channel1->buf[0][0];
close_file:
shm_unlink(shm_name);
return channel1;
} }
int end_producer(void *unused __attribute__ ((unused))) int end_producer(void *unused __attribute__ ((unused)))
@ -28,8 +103,17 @@ int end_producer(void *unused __attribute__ ((unused)))
int destroy_comm_channel(void *channel) int destroy_comm_channel(void *channel)
{ {
free(channel); size_t mapping_size;
return 0; struct channel *mapping1, *mapping2, *channel_ptr;
channel_ptr = (struct channel *) channel;
mapping1 = channel_ptr->mapping1;
mapping2 = channel_ptr->mapping2;
mapping_size = ROUNDUP(sizeof(*mapping1));
if (munmap(mapping1, mapping_size) || munmap(mapping2, mapping_size))
return -1;
else
return 0;
} }
/* /*
@ -44,12 +128,20 @@ void *recv_one_data(struct channel *channel)
{ {
void *result; void *result;
if (unlikely(!(channel->receiver_idx % (BUF_SIZE / sizeof(void *))))) if (unlikely(!((uintptr_t) channel->receiver_ptr & buf_mask)))
while (!channel->state); while (!channel->state);
result = channel->buf[channel->receiver_idx++]; result = *channel->receiver_ptr++;
channel->receiver_idx %= (2 * BUF_SIZE) / sizeof(void *); if (unlikely(!((uintptr_t) channel->receiver_ptr & buf_mask)))
if (unlikely(!(channel->receiver_idx % (BUF_SIZE / sizeof(void *))))) {
uintptr_t rcvr_ptr_high;
rcvr_ptr_high = (uintptr_t) channel->receiver_ptr & page_mask;
channel->state = 0; channel->state = 0;
if ((void *) rcvr_ptr_high == channel->mapping1)
channel->receiver_ptr = channel->mapping2->buf[1];
else
channel->receiver_ptr = channel->mapping1->buf[0];
}
return result; return result;
} }
@ -69,21 +161,20 @@ ssize_t recv_some_data(struct channel *channel, void **buf, size_t count)
nb_read = 0; nb_read = 0;
while (channel->state && nb_read < count) while (channel->state && nb_read < count)
{ {
int i, n; void * volatile *chanbuf_ptr;
/* uintptr_t rcvr_ptr_high;
* cur->receiver_idx point to the last buffer we have read.
* We go to the next cache line "+ (BUF_SIZE / sizeof(void *))" chanbuf_ptr = channel->receiver_ptr;
* (because the line is full of void * and then if we are after channel->receiver_ptr += nb_buf_entries;
* the second cache line we correct the pointer to point to the rcvr_ptr_high = (uintptr_t) channel->receiver_ptr & page_mask;
* first one (this is done by the modulo). for(; chanbuf_ptr != channel->receiver_ptr; chanbuf_ptr++)
*/ *buf++ = *chanbuf_ptr;
i = channel->receiver_idx; nb_read += nb_buf_entries;
n = channel->receiver_idx + (BUF_SIZE / sizeof(void *));
channel->receiver_idx = n % ((2 * BUF_SIZE) / sizeof(void *));
for(; i < n; i++)
*buf++ = channel->buf[i];
nb_read += BUF_SIZE / sizeof(void *);
channel->state = 0; channel->state = 0;
if ((void *) rcvr_ptr_high == channel->mapping1)
channel->receiver_ptr = channel->mapping2->buf[1];
else
channel->receiver_ptr = channel->mapping1->buf[0];
} }
return nb_read; return nb_read;
} }

View File

@ -432,7 +432,8 @@ static int consumer_data(node_param_t *node_param)
} }
} }
cons_check_value = init_calc_arg; cons_check_value = init_calc_arg;
for(i = 0; i < nb_bufs_sent; i++) { for(i = 0; i < nb_bufs_sent; i++)
{
for(j = 0; j < WORDS_PER_LINE; j++) for(j = 0; j < WORDS_PER_LINE; j++)
{ {
void *data; void *data;