diff --git a/src/cell.c b/src/cell.c index 97a67583a7872999d7b7e7c5a0eb9787c83b4bd3..6f74f63fd9d4e217fff470f9cd13b431307e3614 100644 --- a/src/cell.c +++ b/src/cell.c @@ -269,7 +269,7 @@ int cell_locktree(struct cell *c) { /* Undo the holds up to finger. */ for (struct cell *finger2 = c->parent; finger2 != finger; finger2 = finger2->parent) - __sync_fetch_and_sub(&finger2->hold, 1); + atomic_dec(&finger2->hold); /* Unlock this cell. */ if (lock_unlock(&c->lock) != 0) error("Failed to unlock cell."); @@ -309,7 +309,7 @@ int cell_glocktree(struct cell *c) { if (lock_trylock(&finger->glock) != 0) break; /* Increment the hold. */ - __sync_fetch_and_add(&finger->ghold, 1); + atomic_inc(&finger->ghold); /* Unlock the cell. */ if (lock_unlock(&finger->glock) != 0) error("Failed to unlock cell."); @@ -327,7 +327,7 @@ int cell_glocktree(struct cell *c) { /* Undo the holds up to finger. */ for (struct cell *finger2 = c->parent; finger2 != finger; finger2 = finger2->parent) - __sync_fetch_and_sub(&finger2->ghold, 1); + atomic_dec(&finger2->ghold); /* Unlock this cell. */ if (lock_unlock(&c->glock) != 0) error("Failed to unlock cell."); @@ -353,7 +353,7 @@ void cell_unlocktree(struct cell *c) { /* Climb up the tree and unhold the parents. */ for (struct cell *finger = c->parent; finger != NULL; finger = finger->parent) - __sync_fetch_and_sub(&finger->hold, 1); + atomic_dec(&finger->hold); TIMER_TOC(timer_locktree); } @@ -367,7 +367,7 @@ void cell_gunlocktree(struct cell *c) { /* Climb up the tree and unhold the parents. */ for (struct cell *finger = c->parent; finger != NULL; finger = finger->parent) - __sync_fetch_and_sub(&finger->ghold, 1); + atomic_dec(&finger->ghold); TIMER_TOC(timer_locktree); } diff --git a/src/lock.h b/src/lock.h index 735058e15f09d31396bc97d6906c5853fed17db9..5eb97de15f81d17bad45e85226cb0881d24482ba 100644 --- a/src/lock.h +++ b/src/lock.h @@ -23,6 +23,7 @@ #include <pthread.h> /* Includes. */ +#include "atomic.h" #include "inline.h" #ifdef PTHREAD_SPINLOCK @@ -48,14 +49,14 @@ #define lock_init(l) (*(l) = 0) #define lock_destroy(l) 0 INLINE static int lock_lock(volatile int *l) { - while (__sync_val_compare_and_swap(l, 0, 1) != 0) + while (atomic_cas(l, 0, 1) != 0) ; // while( *l ); return 0; } -#define lock_trylock(l) ((*(l)) ? 1 : __sync_val_compare_and_swap(l, 0, 1)) -#define lock_unlock(l) (__sync_val_compare_and_swap(l, 1, 0) != 1) -#define lock_unlock_blind(l) __sync_val_compare_and_swap(l, 1, 0) +#define lock_trylock(l) ((*(l)) ? 1 : atomic_cas(l, 0, 1)) +#define lock_unlock(l) (atomic_cas(l, 1, 0) != 1) +#define lock_unlock_blind(l) atomic_cas(l, 1, 0) #endif #endif /* SWIFT_LOCK_H */ diff --git a/src/map.c b/src/map.c index fbe57fde7b1e29c49b0f27d86d177245dd9a27e2..f4f9ac7cfa7141606b578739517b66e951e65eab 100644 --- a/src/map.c +++ b/src/map.c @@ -18,15 +18,23 @@ * ******************************************************************************/ -#include "map.h" +/* Config parameters. */ +#include "../config.h" + +/* Some standard headers. */ #include <stdio.h> #include <stdlib.h> + +/* This object's header. */ +#include "map.h" + +/* Local headers. */ +#include "atomic.h" #include "error.h" /** * @brief Mapping function to draw a specific cell (gnuplot). */ - void map_cells_plot(struct cell *c, void *data) { int depth = *(int *)data; @@ -80,24 +88,21 @@ void map_cells_plot(struct cell *c, void *data) { /** * @brief Mapping function for checking if each part is in its box. */ +void map_check(struct part *p, struct cell *c, void *data) { -/* void map_check ( struct part *p , struct cell *c , void *data ) { - - if ( p->x[0] < c->loc[0] || p->x[0] > c->loc[0]+c->h[0] || - p->x[0] < c->loc[0] || p->x[0] > c->loc[0]+c->h[0] || - p->x[0] < c->loc[0] || p->x[0] > c->loc[0]+c->h[0] ) - printf( "map_check: particle %i is outside of its box.\n" , p->id ); - - } */ + if (p->x[0] < c->loc[0] || p->x[0] > c->loc[0] + c->h[0] || + p->x[0] < c->loc[0] || p->x[0] > c->loc[0] + c->h[0] || + p->x[0] < c->loc[0] || p->x[0] > c->loc[0] + c->h[0]) + printf("map_check: particle %lld is outside of its box.\n", p->id); +} /** * @brief Mapping function for neighbour count. */ - void map_cellcheck(struct cell *c, void *data) { int *count = (int *)data; - __sync_fetch_and_add(count, c->count); + atomic_add(count, c->count); /* Loop over all parts and check if they are in the cell. */ for (int k = 0; k < c->count; k++) { @@ -133,7 +138,6 @@ void map_cellcheck(struct cell *c, void *data) { /** * @brief Mapping function for maxdepth cell count. */ - void map_maxdepth(struct cell *c, void *data) { int maxdepth = ((int *)data)[0]; @@ -147,7 +151,6 @@ void map_maxdepth(struct cell *c, void *data) { /** * @brief Mapping function for neighbour count. */ - void map_count(struct part *p, struct cell *c, void *data) { double *wcount = (double *)data; @@ -156,7 +159,6 @@ void map_count(struct part *p, struct cell *c, void *data) { *wcount += p->density.wcount; } - void map_wcount_min(struct part *p, struct cell *c, void *data) { struct part **p2 = (struct part **)data; @@ -188,7 +190,6 @@ void map_h_max(struct part *p, struct cell *c, void *data) { /** * @brief Mapping function for neighbour count. */ - void map_icount(struct part *p, struct cell *c, void *data) { // int *count = (int *)data; @@ -201,7 +202,6 @@ void map_icount(struct part *p, struct cell *c, void *data) { /** * @brief Mapping function to print the particle position. */ - void map_dump(struct part *p, struct cell *c, void *data) { double *shift = (double *)data; diff --git a/src/timers.h b/src/timers.h index 92b685ebe9b11d49c4703e5837d35cffdca81c4d..342f46a9703501bfd4a05d25342b4dcff4bef052 100644 --- a/src/timers.h +++ b/src/timers.h @@ -23,6 +23,7 @@ #define SWIFT_TIMERS_H /* Includes. */ +#include "atomic.h" #include "cycle.h" #include "inline.h" @@ -71,7 +72,7 @@ extern ticks timers[timer_count]; #define TIMER_TOC2(t) timers_toc(t, tic2) INLINE static ticks timers_toc(int t, ticks tic) { ticks d = (getticks() - tic); - __sync_add_and_fetch(&timers[t], d); + atomic_add(&timers[t], d); return d; } #else