From 4be39aa830675e932bcaabb95147522f22b9ebe9 Mon Sep 17 00:00:00 2001 From: Pedro Gonnet <gonnet@google.com> Date: Thu, 10 May 2018 12:54:32 +0200 Subject: [PATCH] extract space_getsid into a separate header since it is called a lot and should be inlined. --- src/Makefile.am | 5 +-- src/cell.c | 1 + src/runner_doiact.h | 3 ++ src/scheduler.c | 1 + src/space.c | 46 ------------------------- src/space.h | 2 -- src/space_getsid.h | 81 +++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 89 insertions(+), 50 deletions(-) create mode 100644 src/space_getsid.h diff --git a/src/Makefile.am b/src/Makefile.am index a7bc4774ea..49bedc4f6d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -47,7 +47,7 @@ include_HEADERS = space.h runner.h queue.h task.h lock.h cell.h part.h const.h \ sourceterms_struct.h statistics.h memswap.h cache.h runner_doiact_vec.h profiler.h \ dump.h logger.h active.h timeline.h xmf.h gravity_properties.h gravity_derivatives.h \ gravity_softened_derivatives.h vector_power.h collectgroup.h hydro_space.h sort_part.h \ - chemistry.h chemistry_io.h chemistry_struct.h cosmology.h restart.h + chemistry.h chemistry_io.h chemistry_struct.h cosmology.h restart.h space_getsid.h # Common source files AM_SOURCES = space.c runner.c queue.c task.c cell.c engine.c \ @@ -132,7 +132,8 @@ nobase_noinst_HEADERS = align.h approx_math.h atomic.h barrier.h cycle.h error.h chemistry/EAGLE/chemistry.h \ chemistry/EAGLE/chemistry_io.h \ chemistry/EAGLE/chemistry_struct.h\ - chemistry/EAGLE/chemistry_iact.h + chemistry/EAGLE/chemistry_iact.h \ + space_getsid.h # Sources and flags for regular library diff --git a/src/cell.c b/src/cell.c index ea036c9708..32c2a5b3b7 100644 --- a/src/cell.c +++ b/src/cell.c @@ -60,6 +60,7 @@ #include "minmax.h" #include "scheduler.h" #include "space.h" +#include "space_getsid.h" #include "timers.h" /* Global variables. */ diff --git a/src/runner_doiact.h b/src/runner_doiact.h index 2987d205e5..4081e67b46 100644 --- a/src/runner_doiact.h +++ b/src/runner_doiact.h @@ -125,6 +125,9 @@ #define _TIMER_DOPAIR_SUBSET(f) PASTE(timer_dopair_subset, f) #define TIMER_DOPAIR_SUBSET _TIMER_DOPAIR_SUBSET(FUNCTION) +/* Local headers. */ +#include "space_getsid.h" + /** * @brief Compute the interactions between a cell pair (non-symmetric case). * diff --git a/src/scheduler.c b/src/scheduler.c index 1513042937..187ad7093b 100644 --- a/src/scheduler.c +++ b/src/scheduler.c @@ -48,6 +48,7 @@ #include "queue.h" #include "sort_part.h" #include "space.h" +#include "space_getsid.h" #include "task.h" #include "timers.h" #include "version.h" diff --git a/src/space.c b/src/space.c index 3da35b64a3..93b7235d6d 100644 --- a/src/space.c +++ b/src/space.c @@ -103,52 +103,6 @@ struct index_data { int *ind; }; -/** - * @brief Get the shift-id of the given pair of cells, swapping them - * if need be. - * - * @param s The space - * @param ci Pointer to first #cell. - * @param cj Pointer second #cell. - * @param shift Vector from ci to cj. - * - * @return The shift ID and set shift, may or may not swap ci and cj. - */ -int space_getsid(struct space *s, struct cell **ci, struct cell **cj, - double *shift) { - - /* Get the relative distance between the pairs, wrapping. */ - const int periodic = s->periodic; - double dx[3]; - for (int k = 0; k < 3; k++) { - dx[k] = (*cj)->loc[k] - (*ci)->loc[k]; - if (periodic && dx[k] < -s->dim[k] / 2) - shift[k] = s->dim[k]; - else if (periodic && dx[k] > s->dim[k] / 2) - shift[k] = -s->dim[k]; - else - shift[k] = 0.0; - dx[k] += shift[k]; - } - - /* Get the sorting index. */ - int sid = 0; - for (int k = 0; k < 3; k++) - sid = 3 * sid + ((dx[k] < 0.0) ? 0 : ((dx[k] > 0.0) ? 2 : 1)); - - /* Switch the cells around? */ - if (runner_flip[sid]) { - struct cell *temp = *ci; - *ci = *cj; - *cj = temp; - for (int k = 0; k < 3; k++) shift[k] = -shift[k]; - } - sid = sortlistID[sid]; - - /* Return the sort ID. */ - return sid; -} - /** * @brief Recursively dismantle a cell tree. * diff --git a/src/space.h b/src/space.h index 76d9369db2..5f7f910b58 100644 --- a/src/space.h +++ b/src/space.h @@ -180,8 +180,6 @@ void space_gparts_sort(struct space *s, int *ind, size_t N, int min, int max, void space_sparts_sort(struct space *s, int *ind, size_t N, int min, int max, int verbose); void space_getcells(struct space *s, int nr_cells, struct cell **cells); -int space_getsid(struct space *s, struct cell **ci, struct cell **cj, - double *shift); void space_init(struct space *s, const struct swift_params *params, const struct cosmology *cosmo, double dim[3], struct part *parts, struct gpart *gparts, struct spart *sparts, diff --git a/src/space_getsid.h b/src/space_getsid.h new file mode 100644 index 0000000000..64fcbf1e37 --- /dev/null +++ b/src/space_getsid.h @@ -0,0 +1,81 @@ +/******************************************************************************* + * This file is part of SWIFT. + * Copyright (c) 2018 Pedro Gonnet (pedro.gonnet@durham.ac.uk) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + ******************************************************************************/ +#ifndef SWIFT_SPACE_GETSID_H +#define SWIFT_SPACE_GETSID_H + +/* Config parameters. */ +#include "../config.h" + +/* Some standard headers. */ +#include <stddef.h> + +/* Includes. */ +#include "cell.h" +#include "runner.h" +#include "space.h" + +/** + * @brief Get the shift-id of the given pair of cells, swapping them + * if need be. + * + * @param s The space + * @param ci Pointer to first #cell. + * @param cj Pointer second #cell. + * @param shift Vector from ci to cj. + * + * @return The shift ID and set shift, may or may not swap ci and cj. + */ +__attribute__((always_inline)) INLINE static int space_getsid(struct space *s, + struct cell **ci, + struct cell **cj, + double *shift) { + + /* Get the relative distance between the pairs, wrapping. */ + const int periodic = s->periodic; + double dx[3]; + for (int k = 0; k < 3; k++) { + dx[k] = (*cj)->loc[k] - (*ci)->loc[k]; + if (periodic && dx[k] < -s->dim[k] / 2) + shift[k] = s->dim[k]; + else if (periodic && dx[k] > s->dim[k] / 2) + shift[k] = -s->dim[k]; + else + shift[k] = 0.0; + dx[k] += shift[k]; + } + + /* Get the sorting index. */ + int sid = 0; + for (int k = 0; k < 3; k++) + sid = 3 * sid + ((dx[k] < 0.0) ? 0 : ((dx[k] > 0.0) ? 2 : 1)); + + /* Switch the cells around? */ + if (runner_flip[sid]) { + struct cell *temp = *ci; + *ci = *cj; + *cj = temp; + for (int k = 0; k < 3; k++) shift[k] = -shift[k]; + } + sid = sortlistID[sid]; + + /* Return the sort ID. */ + return sid; +} + +#endif /* SWIFT_SPACE_GETSID_H */ -- GitLab