Implemented dissemination barriers.

Validation (ck_barrier_dissemination.c) is included.
ck_pring
David Joseph 14 years ago
parent 489bbc058b
commit 480db1321c

@ -82,4 +82,30 @@ void ck_barrier_combining(ck_barrier_combining_t *,
ck_barrier_combining_group_t *,
ck_barrier_combining_state_t *);
struct ck_barrier_dissemination_flags {
unsigned int *tflags[2];
unsigned int **pflags[2];
};
typedef struct ck_barrier_dissemination_flags ck_barrier_dissemination_flags_t;
struct ck_barrier_dissemination_state {
int parity;
unsigned int sense;
};
typedef struct ck_barrier_dissemination_state ck_barrier_dissemination_state_t;
#define CK_BARRIER_DISSEMINATION_STATE_INITIALIZER {0, ~0}
void ck_barrier_dissemination_flags_init(ck_barrier_dissemination_flags_t *,
int);
void ck_barrier_dissemination_state_init(ck_barrier_dissemination_state_t *);
int ck_barrier_dissemination_size(unsigned int);
void ck_barrier_dissemination(ck_barrier_dissemination_flags_t *,
ck_barrier_dissemination_state_t *,
int,
int);
#endif /* _CK_BARRIER_H */

@ -1,6 +1,6 @@
.PHONY: clean distribution
OBJECTS=ck_barrier_centralized ck_barrier_combining
OBJECTS=ck_barrier_centralized ck_barrier_combining ck_barrier_dissemination
all: $(OBJECTS)
@ -10,6 +10,9 @@ ck_barrier_centralized: ck_barrier_centralized.c ../../../include/ck_barrier.h .
ck_barrier_combining: ck_barrier_combining.c ../../../include/ck_barrier.h ../../../src/ck_barrier.c
$(CC) $(CFLAGS) -o ck_barrier_combining ck_barrier_combining.c ../../../src/ck_barrier.c
ck_barrier_dissemination: ck_barrier_dissemination.c ../../../include/ck_barrier.h ../../../src/ck_barrier.c
$(CC) $(CFLAGS) -o ck_barrier_dissemination ck_barrier_dissemination.c ../../../src/ck_barrier.c
clean:
rm -rf *.dSYM *~ *.o $(OBJECTS)

@ -129,9 +129,8 @@ main(int argc, char *argv[])
a.delta = atoi(argv[3]);
for (i = 0; i < ngroups; i++) {
for (i = 0; i < ngroups; i++)
ck_barrier_combining_group_init(&barrier, groupings + i, nthr);
}
fprintf(stderr, "Creating threads (barrier)...");
for (i = 0; i < (nthr * ngroups); i++) {

@ -0,0 +1,145 @@
/*
* Copyright 2011 Samy Al Bahra.
* Copyright 2011 David Joseph.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <errno.h>
#include <inttypes.h>
#include <pthread.h>
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#include <sys/time.h>
#include <ck_pr.h>
#include <ck_barrier.h>
#include "../../common.h"
#ifndef ITERATE
#define ITERATE 5000000
#endif
#ifndef ENTRIES
#define ENTRIES 512
#endif
static struct affinity a;
static int nthr;
static int tid;
static int counters[ENTRIES];
static int barrier_wait;
static void *
thread(void *allflags)
{
ck_barrier_dissemination_state_t state = CK_BARRIER_DISSEMINATION_STATE_INITIALIZER;
int j, k, counter, id;
int i = 0;
aff_iterate(&a);
id = ck_pr_faa_int(&tid, 1);
ck_pr_inc_int(&barrier_wait);
while (ck_pr_load_int(&barrier_wait) != nthr)
ck_pr_stall();
for (j = 0, k = 0; j < ITERATE; j++, k++) {
i = j++ & (ENTRIES - 1);
ck_pr_inc_int(&counters[i]);
ck_barrier_dissemination(allflags, &state, id, nthr);
counter = ck_pr_load_int(&counters[i]);
if (counter != nthr * (j / ENTRIES + 1)) {
fprintf(stderr, "FAILED [%d:%d]: %d != %d\n", i, j - 1, counter, nthr);
exit(EXIT_FAILURE);
}
}
return (NULL);
}
int
main(int argc, char *argv[])
{
ck_barrier_dissemination_flags_t *allflags;
pthread_t *threads;
int i, size;
if (argc != 3) {
fprintf(stderr, "Usage: correct <number of threads> <affinity delta>\n");
exit(EXIT_FAILURE);
}
nthr = atoi(argv[1]);
if (nthr <= 0) {
fprintf(stderr, "ERROR: Number of threads must be greater than 0\n");
exit(EXIT_FAILURE);
}
threads = malloc(sizeof(pthread_t) * nthr);
if (threads == NULL) {
fprintf(stderr, "ERROR: Could not allocate thread structures\n");
exit(EXIT_FAILURE);
}
a.delta = atoi(argv[2]);
allflags = malloc(sizeof(ck_barrier_dissemination_flags_t) * nthr);
if (allflags == NULL) {
fprintf(stderr, "ERROR: Could not allocate thread structures\n");
exit(EXIT_FAILURE);
}
size = ck_barrier_dissemination_size(nthr);
for (i = 0; i < nthr; i++) {
allflags[i].tflags[0] = malloc(sizeof(unsigned int) * size);
allflags[i].tflags[1] = malloc(sizeof(unsigned int) * size);
allflags[i].pflags[0] = malloc(sizeof(unsigned int *) * size);
allflags[i].pflags[1] = malloc(sizeof(unsigned int *) * size);
}
ck_barrier_dissemination_flags_init(allflags, nthr);
fprintf(stderr, "Creating threads (barrier)...");
for (i = 0; i < nthr; i++) {
if (pthread_create(&threads[i], NULL, thread, allflags)) {
fprintf(stderr, "ERROR: Could not create thread %d\n", i);
exit(EXIT_FAILURE);
}
}
fprintf(stderr, "done\n");
fprintf(stderr, "Waiting for threads to finish correctness regression...");
for (i = 0; i < nthr; i++)
pthread_join(threads[i], NULL);
fprintf(stderr, "done (passed)\n");
return (0);
}

@ -30,6 +30,44 @@
#include <ck_pr.h>
#include <ck_spinlock.h>
#include <stdio.h>
/*
* Algorithm from: http://graphics.stanford.edu/~seander/bithacks.html
*/
CK_CC_INLINE static unsigned int
ck_barrier_internal_log(unsigned int v)
{
static const unsigned int b[] = {0xAAAAAAAA, 0xCCCCCCCC, 0xF0F0F0F0,
0xFF00FF00, 0xFFFF0000};
register unsigned int r = (v & b[0]) != 0;
int i;
for (i = 4; i > 0; i--) {
r |= ((v & b[i]) != 0) << i;
}
return (r);
}
/*
* Algorithm from: http://graphics.stanford.edu/~seander/bithacks.html
*/
CK_CC_INLINE static unsigned int
ck_barrier_internal_power_2(unsigned int v)
{
--v;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
++v;
return (v);
}
struct ck_barrier_combining_queue {
struct ck_barrier_combining_group *head;
struct ck_barrier_combining_group *tail;
@ -60,7 +98,6 @@ CK_CC_INLINE static void
ck_barrier_combining_queue_enqueue(struct ck_barrier_combining_queue *queue,
struct ck_barrier_combining_group *node_value)
{
node_value->next = NULL;
if (queue->head == NULL) {
@ -92,7 +129,6 @@ ck_barrier_combining_try_insert(struct ck_barrier_combining_group *parent,
struct ck_barrier_combining_group *tnode,
struct ck_barrier_combining_group **child)
{
if (*child == NULL) {
*child = tnode;
tnode->parent = parent;
@ -109,7 +145,6 @@ ck_barrier_combining_aux(struct ck_barrier_combining *barrier,
struct ck_barrier_combining_group *tnode,
unsigned int sense)
{
if (ck_pr_faa_uint(&tnode->count, 1) == tnode->k - 1) {
if (tnode->parent != NULL)
ck_barrier_combining_aux(barrier, tnode->parent, sense);
@ -163,7 +198,6 @@ void
ck_barrier_combining_init(struct ck_barrier_combining *root,
struct ck_barrier_combining_group *init_root)
{
init_root->k = 0;
init_root->count = 0;
init_root->sense = 0;
@ -178,9 +212,80 @@ ck_barrier_combining(struct ck_barrier_combining *barrier,
struct ck_barrier_combining_group *tnode,
struct ck_barrier_combining_state *state)
{
ck_barrier_combining_aux(barrier, tnode, state->sense);
state->sense = ~state->sense;
return;
}
void
ck_barrier_dissemination_flags_init(struct ck_barrier_dissemination_flags *allflags,
int nthr)
{
int i, j, k, size, offset;
size = (ck_barrier_internal_log(ck_barrier_internal_power_2(nthr)));
for (i = 0; i < nthr; ++i) {
for (k = 0, offset = 1; k < size; ++k, offset = 1) {
/* Determine the thread's partner, j, for the current round. */
offset <<= k;
if ((nthr & (nthr - 1)) == 0)
j = (i + offset) & (nthr - 1);
else
j = (i + offset) % nthr;
/* Set the thread's partner for round k. */
allflags[i].pflags[0][k] = &allflags[j].tflags[0][k];
allflags[i].pflags[1][k] = &allflags[j].tflags[1][k];
/* Set the thread's flags to false. */
allflags[i].tflags[0][k] = allflags[i].tflags[1][k] = 0;
}
}
return;
}
void
ck_barrier_dissemination_state_init(struct ck_barrier_dissemination_state *state)
{
state->parity = 0;
state->sense = ~0;
return;
}
int
ck_barrier_dissemination_size(unsigned int nthr)
{
return (ck_barrier_internal_log(ck_barrier_internal_power_2(nthr)));
}
void
ck_barrier_dissemination(struct ck_barrier_dissemination_flags *allflags,
struct ck_barrier_dissemination_state *state,
int tid,
int nthr)
{
int i, size;
size = (ck_barrier_internal_log(ck_barrier_internal_power_2(nthr)));
for (i = 0; i < size; ++i) {
/* Unblock current partner. */
ck_pr_store_uint(allflags[tid].pflags[state->parity][i], state->sense);
/* Wait until some other thread unblocks this one. */
while (ck_pr_load_uint(&allflags[tid].tflags[state->parity][i]) != state->sense)
ck_pr_stall();
}
/*
* Dissemination barriers use two sets of flags to prevent race conditions
* between successive calls to the barrier. It also uses
* a sense reversal technique to avoid re-initialization of the flags
* for every two calls to the barrier.
*/
if (state->parity == 1)
state->sense = ~state->sense;
state->parity = 1 - state->parity;
return;
}

Loading…
Cancel
Save