Skip to content

Commit

Permalink
refactor more allocator apis
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzalobg committed Aug 12, 2024
1 parent a848a47 commit c37bbec
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/all_pairs.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ void all_pairs_collapsed_force(System<T, N>& 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);
Expand Down
4 changes: 4 additions & 0 deletions src/alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <cstdint>
#include <cstdlib>
#include <memory>
#include <vector>

template <typename T>
void alloc(T*& ptr, std::size_t n) {
Expand Down Expand Up @@ -50,3 +51,6 @@ template <typename T, typename U>
constexpr bool operator!=(allocator<T> const &, allocator<U> const &) noexcept {
return false;
}

template <typename T>
using vector = std::vector<T, allocator<T>>;
6 changes: 3 additions & 3 deletions src/bvh.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void hilbert_sort(System<T, N>& system, aabb<T, N> bbox) {

// Compute the Hilbert index for each body in the Cartesian grid
auto bids = system.body_indices();
static std::vector<uint64_t> hilbert_ids(system.size);
static vector<uint64_t> 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:
Expand All @@ -59,7 +59,7 @@ void hilbert_sort(System<T, N>& system, aabb<T, N> 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<std::pair<uint64_t, std::size_t>> hilbert_index_map(system.size);
static vector<std::pair<uint64_t, std::size_t>> hilbert_index_map(system.size);
std::for_each_n(par_unseq, counting_iterator<std::size_t>(0), system.size,
[hmap = hilbert_index_map.data(), hids = hilbert_ids.data()](std::size_t idx) {
hmap[idx] = std::make_pair(hids[idx], idx);
Expand All @@ -70,7 +70,7 @@ void hilbert_sort(System<T, N>& system, aabb<T, N> bbox) {

// create temp copy of system so that we don't get race conditions when
// rearranging values in the next step
static std::vector<std::tuple<vec<T, N>, T, vec<T, N>, vec<T, N>, vec<T, N>>> tmp_system(system.size);
static vector<std::tuple<vec<T, N>, T, vec<T, N>, vec<T, N>, vec<T, N>>> tmp_system(system.size);
std::for_each_n(par_unseq, counting_iterator<std::size_t>(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) {
Expand Down
5 changes: 2 additions & 3 deletions src/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include <cmath>
#include <functional>
#include <random>
#include <vector>

#include "alloc.h"
#include "execution.h"
Expand All @@ -16,8 +15,8 @@ class System {
index_t const max_tree_node_size;
T const dt;
T const constant;
std::vector<T, allocator<T>> m;
std::vector<vec<T, N>, allocator<vec<T, N>>> x, v, a, ao;
vector<T> m;
vector<vec<T, N>> x, v, a, ao;

// random generation
std::mt19937 gen{42}; // fix random generation
Expand Down

0 comments on commit c37bbec

Please sign in to comment.