From 7295cf3d49d60fdae10d55bebdcae2cefba0ee15 Mon Sep 17 00:00:00 2001 From: Matthieu Schaller <schaller@strw.leidenuniv.nl> Date: Sun, 29 Mar 2020 15:32:34 +0200 Subject: [PATCH] Only use atomic operations when SWIFT_TASKS_WITHOUT_ATOMICS is not defined --- src/gravity/MultiSoftening/gravity.h | 4 ++++ src/gravity/Potential/gravity.h | 4 ++++ src/gravity_cache.h | 6 ++++++ src/mesh_gravity.c | 8 +++++++- src/multipole.h | 6 ++++++ src/runner_doiact_grav.c | 8 ++++++++ src/timestep_limiter_iact.h | 6 +++++- 7 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/gravity/MultiSoftening/gravity.h b/src/gravity/MultiSoftening/gravity.h index 7cb2c50434..ce47dbe5ce 100644 --- a/src/gravity/MultiSoftening/gravity.h +++ b/src/gravity/MultiSoftening/gravity.h @@ -78,7 +78,11 @@ __attribute__((always_inline)) INLINE static float gravity_get_softening( __attribute__((always_inline)) INLINE static void gravity_add_comoving_potential(struct gpart* restrict gp, float pot) { +#ifdef SWIFT_TASKS_WITHOUT_ATOMICS + gp->potential += pot; +#else atomic_add_f(&gp->potential, pot); +#endif } /** diff --git a/src/gravity/Potential/gravity.h b/src/gravity/Potential/gravity.h index ff85f29c0a..e467e05e0d 100644 --- a/src/gravity/Potential/gravity.h +++ b/src/gravity/Potential/gravity.h @@ -64,7 +64,11 @@ __attribute__((always_inline)) INLINE static float gravity_get_softening( __attribute__((always_inline)) INLINE static void gravity_add_comoving_potential(struct gpart* restrict gp, float pot) { +#ifdef SWIFT_TASKS_WITHOUT_ATOMICS + gp->potential += pot; +#else atomic_add_f(&gp->potential, pot); +#endif } /** diff --git a/src/gravity_cache.h b/src/gravity_cache.h index 6e55e3f443..23103f0509 100644 --- a/src/gravity_cache.h +++ b/src/gravity_cache.h @@ -491,9 +491,15 @@ __attribute__((always_inline)) INLINE static void gravity_cache_write_back( /* Write stuff back to the particles */ for (int i = 0; i < gcount; ++i) { if (active[i]) { +#ifdef SWIFT_TASKS_WITHOUT_ATOMICS + gparts[i].a_grav[0] += a_x[i]; + gparts[i].a_grav[1] += a_y[i]; + gparts[i].a_grav[2] += a_z[i]; +#else atomic_add_f(&gparts[i].a_grav[0], a_x[i]); atomic_add_f(&gparts[i].a_grav[1], a_y[i]); atomic_add_f(&gparts[i].a_grav[2], a_z[i]); +#endif gravity_add_comoving_potential(&gparts[i], pot[i]); } } diff --git a/src/mesh_gravity.c b/src/mesh_gravity.c index 7cabd7bbae..f36200718e 100644 --- a/src/mesh_gravity.c +++ b/src/mesh_gravity.c @@ -334,10 +334,16 @@ void mesh_to_gparts_CIC(struct gpart* gp, const double* pot, const int N, /* ---- */ /* Store things back */ - gravity_add_comoving_potential(gp, p); +#ifdef SWIFT_TASKS_WITHOUT_ATOMICS + gp->a_grav[0] += fac * a[0]; + gp->a_grav[1] += fac * a[1]; + gp->a_grav[2] += fac * a[2]; +#else atomic_add_f(&gp->a_grav[0], fac * a[0]); atomic_add_f(&gp->a_grav[1], fac * a[1]); atomic_add_f(&gp->a_grav[2], fac * a[2]); +#endif + gravity_add_comoving_potential(gp, p); #ifdef SWIFT_GRAVITY_FORCE_CHECKS gp->potential_PM = p; gp->a_grav_PM[0] = fac * a[0]; diff --git a/src/multipole.h b/src/multipole.h index c4a51451db..e7dcfbeb1f 100644 --- a/src/multipole.h +++ b/src/multipole.h @@ -2589,9 +2589,15 @@ INLINE static void gravity_L2P(const struct grav_tensor *lb, #endif /* Update the particle */ +#ifdef SWIFT_TASKS_WITHOUT_ATOMICS + gp->a_grav[0] += a_grav[0]; + gp->a_grav[1] += a_grav[1]; + gp->a_grav[2] += a_grav[2]; +#else atomic_add_f(&gp->a_grav[0], a_grav[0]); atomic_add_f(&gp->a_grav[1], a_grav[1]); atomic_add_f(&gp->a_grav[2], a_grav[2]); +#endif gravity_add_comoving_potential(gp, pot); #ifdef SWIFT_GRAVITY_FORCE_CHECKS diff --git a/src/runner_doiact_grav.c b/src/runner_doiact_grav.c index 6d7a63db7f..bb914dc47a 100644 --- a/src/runner_doiact_grav.c +++ b/src/runner_doiact_grav.c @@ -1359,6 +1359,7 @@ static INLINE void runner_dopair_grav_mm_symmetric(struct runner *r, cj->grav.ti_old_multipole, cj->nodeID, ci->nodeID, e->ti_current); #endif +#ifndef SWIFT_TASKS_WITHOUT_ATOMICS /* Lock the multipoles * Note we impose a hierarchy to solve the dining philosopher problem */ if (ci < cj) { @@ -1368,15 +1369,18 @@ static INLINE void runner_dopair_grav_mm_symmetric(struct runner *r, lock_lock(&cj->grav.mlock); lock_lock(&ci->grav.mlock); } +#endif /* Let's interact at this level */ gravity_M2L_symmetric(&ci->grav.multipole->pot, &cj->grav.multipole->pot, multi_i, multi_j, ci->grav.multipole->CoM, cj->grav.multipole->CoM, props, periodic, dim, r_s_inv); +#ifndef SWIFT_TASKS_WITHOUT_ATOMICS /* Unlock the multipoles */ if (lock_unlock(&ci->grav.mlock) != 0) error("Failed to unlock multipole"); if (lock_unlock(&cj->grav.mlock) != 0) error("Failed to unlock multipole"); +#endif TIMER_TOC(timer_dopair_grav_mm); } @@ -1424,6 +1428,7 @@ static INLINE void runner_dopair_grav_mm_nonsym(struct runner *r, cj->grav.ti_old_multipole, cj->nodeID, ci->nodeID, e->ti_current); #endif +#ifndef SWIFT_TASKS_WITHOUT_ATOMICS /* Lock the multipoles * Note we impose a hierarchy to solve the dining philosopher problem */ if (ci < cj) { @@ -1433,14 +1438,17 @@ static INLINE void runner_dopair_grav_mm_nonsym(struct runner *r, lock_lock(&cj->grav.mlock); lock_lock(&ci->grav.mlock); } +#endif /* Let's interact at this level */ gravity_M2L_nonsym(&ci->grav.multipole->pot, multi_j, ci->grav.multipole->CoM, cj->grav.multipole->CoM, props, periodic, dim, r_s_inv); +#ifndef SWIFT_TASKS_WITHOUT_ATOMICS /* Unlock the multipoles */ if (lock_unlock(&ci->grav.mlock) != 0) error("Failed to unlock multipole"); if (lock_unlock(&cj->grav.mlock) != 0) error("Failed to unlock multipole"); +#endif TIMER_TOC(timer_dopair_grav_mm); } diff --git a/src/timestep_limiter_iact.h b/src/timestep_limiter_iact.h index 8c8fa7cee6..accfd33d05 100644 --- a/src/timestep_limiter_iact.h +++ b/src/timestep_limiter_iact.h @@ -87,8 +87,12 @@ __attribute__((always_inline)) INLINE static void runner_iact_nonsym_limiter( /* Wake up the neighbour? */ if (pj->time_bin > pi->time_bin + time_bin_neighbour_max_delta_bin) { - /* Store the smallest time bin that woke up this particle */ + /* Store the smallest time bin that woke up this particle */ +#ifdef SWIFT_TASKS_WITHOUT_ATOMICS + pj->limiter_data.wakeup = max(pj->limiter_data.wakeup, -pi->time_bin); +#else atomic_max_c(&pj->limiter_data.wakeup, -pi->time_bin); +#endif } } -- GitLab