From fb75b0bd7c218769931a27d3d748c442421bce6b Mon Sep 17 00:00:00 2001
From: Matthieu Schaller <schaller@strw.leidenuniv.nl>
Date: Thu, 2 Apr 2020 16:39:28 +0200
Subject: [PATCH] Use atomics also on the counters of interactions in the M2L
 function

---
 src/multipole.h | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/src/multipole.h b/src/multipole.h
index 347dd59baa..55de9c0bae 100644
--- a/src/multipole.h
+++ b/src/multipole.h
@@ -1608,12 +1608,19 @@ INLINE static void gravity_M2L_apply(
     const struct potential_derivatives_M2L *pot) {
 
 #ifdef SWIFT_DEBUG_CHECKS
-  /* Count interactions */
-  l_b->num_interacted += m_a->num_gpart;
+  /* Count all interactions
+   * Note that despite being in a section of the code protected by locks,
+   * we must use atomics here as the long-range task may update this
+   * counter in a lock-free section of code. */
+  accumulate_add_ll(&l_b->num_interacted, m_a->num_gpart);
 #endif
 
 #ifdef SWIFT_GRAVITY_FORCE_CHECKS
-  l_b->num_interacted_tree += m_a->num_gpart;
+  /* Count tree interactions
+   * Note that despite being in a section of the code protected by locks,
+   * we must use atomics here as the long-range task may update this
+   * counter in a lock-free section of code. */
+  accumulate_add_ll(&l_b->num_interacted_tree, m_a->num_gpart);
 #endif
 
   /* Record that this tensor has received contributions */
-- 
GitLab