From 3e070c1f4cd4d444adb43731b455d508c5e964cd Mon Sep 17 00:00:00 2001 From: Gonzalo Brito Gadeschi Date: Mon, 12 Aug 2024 10:45:43 -0700 Subject: [PATCH] refactor more allocator apis --- src/all_pairs.h | 2 ++ src/alloc.h | 4 ++++ src/bvh.h | 6 +++--- src/system.h | 5 ++--- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/all_pairs.h b/src/all_pairs.h index 68d7cfa..fac5a9a 100644 --- a/src/all_pairs.h +++ b/src/all_pairs.h @@ -38,6 +38,8 @@ void all_pairs_collapsed_force(System& system) { return; } + // TODO: we should exploit the symmetry of the force pairs to do + // (N^2)/2 computations here by taking this "a" and doing "s.a[j] -= a / mj * mi". auto pi = s.x[i]; auto pj = s.x[j]; auto a = s.c * s.m[j] * (pj - pi) / dist3(pi, pj); diff --git a/src/alloc.h b/src/alloc.h index 1469047..9ab4602 100644 --- a/src/alloc.h +++ b/src/alloc.h @@ -2,6 +2,7 @@ #include #include #include +#include template void alloc(T*& ptr, std::size_t n) { @@ -50,3 +51,6 @@ template constexpr bool operator!=(allocator const &, allocator const &) noexcept { return false; } + +template +using vector = std::vector>; diff --git a/src/bvh.h b/src/bvh.h index 1f87632..2ee8186 100644 --- a/src/bvh.h +++ b/src/bvh.h @@ -35,7 +35,7 @@ void hilbert_sort(System& system, aabb bbox) { // Compute the Hilbert index for each body in the Cartesian grid auto bids = system.body_indices(); - static std::vector hilbert_ids(system.size); + static vector hilbert_ids(system.size); std::for_each(par_unseq, bids.begin(), bids.end(), [hids = hilbert_ids.data(), x = system.x.data(), mins = bbox.xmin, grid_cell_size](auto idx) { // Bucket the body into a Cartesian grid cell: @@ -59,7 +59,7 @@ void hilbert_sort(System& system, aabb bbox) { // TODO: sort an array of keys and then apply a permutation in O(N) time and O(1) storage // (instead of O(N) time and O(N) storage). - static std::vector> hilbert_index_map(system.size); + static vector> hilbert_index_map(system.size); std::for_each_n(par_unseq, counting_iterator(0), system.size, [hmap = hilbert_index_map.data(), hids = hilbert_ids.data()](std::size_t idx) { hmap[idx] = std::make_pair(hids[idx], idx); @@ -70,7 +70,7 @@ void hilbert_sort(System& system, aabb bbox) { // create temp copy of system so that we don't get race conditions when // rearranging values in the next step - static std::vector, T, vec, vec, vec>> tmp_system(system.size); + static vector, T, vec, vec, vec>> tmp_system(system.size); std::for_each_n(par_unseq, counting_iterator(0), system.size, [tmp_sys = tmp_system.data(), x = system.x.data(), m = system.m.data(), v = system.v.data(), a = system.a.data(), ao = system.ao.data()](std::size_t idx) { diff --git a/src/system.h b/src/system.h index f432d1b..5e08f30 100644 --- a/src/system.h +++ b/src/system.h @@ -2,7 +2,6 @@ #include #include #include -#include #include "alloc.h" #include "execution.h" @@ -16,8 +15,8 @@ class System { index_t const max_tree_node_size; T const dt; T const constant; - std::vector> m; - std::vector, allocator>> x, v, a, ao; + vector m; + vector> x, v, a, ao; // random generation std::mt19937 gen{42}; // fix random generation