DiFfRG
Loading...
Searching...
No Matches
utils.hh
Go to the documentation of this file.
1#pragma once
2
3// standard library
4#include <future>
5#include <memory>
6#include <tbb/tbb.h>
7#include <tuple>
8#include <vector>
9
10// DiFfRG
12
13namespace DiFfRG
14{
31 template <typename NT, typename FUN, typename Coordinates, typename... T>
33 std::shared_ptr<std::vector<std::future<NT>>> request_data(FUN &fun, Coordinates &coordinates, const double k,
34 std::tuple<T...> args)
35 {
36 return std::apply([&](auto... args) { return request_data<NT>(fun, coordinates, k, args...); }, args);
37 }
38
55 template <typename NT, typename FUN, typename Coordinates, typename... T>
57 std::shared_ptr<std::vector<std::future<NT>>> request_data(FUN &fun, Coordinates &coordinates, const double k,
58 T... args)
59 {
60 auto futures = std::make_shared<std::vector<std::future<NT>>>();
61 auto grid = make_grid(coordinates);
62
63 auto req_point = [&](auto... p) { return fun.template request<NT>(k, p..., args...); };
64
65 for (uint i = 0; i < coordinates.size(); ++i) {
66 auto p = grid[i];
67 std::apply([&](auto... p) { futures->emplace_back(std::move(req_point(p...))); }, p);
68 }
69
70 return futures;
71 }
72
89 template <typename NT, typename FUN, typename GRID, typename... T>
90 requires IsContainer<GRID>
91 std::shared_ptr<std::vector<std::future<NT>>> request_data(FUN &fun, const GRID &grid, const double k,
92 std::tuple<T...> args)
93 {
94 return std::apply([&](auto... args) { return request_data<NT>(fun, grid, k, args...); }, args);
95 }
96
113 template <typename NT, typename FUN, typename GRID, typename... T>
114 requires IsContainer<GRID>
115 std::shared_ptr<std::vector<std::future<NT>>> request_data(FUN &fun, const GRID &grid, const double k, T... args)
116 {
117 auto futures = std::make_shared<std::vector<std::future<NT>>>();
118
119 auto req_point = [&](auto... p) { return fun.template request<NT>(k, p..., args...); };
120
121 for (uint i = 0; i < grid.size(); ++i) {
122 auto p = grid[i];
123 if constexpr (std::is_same_v<decltype(p), double> || std::is_same_v<decltype(p), float>)
124 futures->emplace_back(std::move(req_point(p)));
125 else
126 std::apply([&](auto... p) { futures->emplace_back(std::move(req_point(p...))); }, p);
127 }
128
129 return futures;
130 }
131
142 template <typename NT1, typename NT2, typename... T>
143 void update_data(std::shared_ptr<std::vector<std::future<NT1>>> futures, NT2 *destination)
144 {
145 for (uint i = 0; i < futures->size(); ++i)
146 destination[i] = static_cast<NT2>((*futures)[i].get());
147 }
148
159 template <typename NT, typename INT, typename... T>
160 void update_interpolator(std::shared_ptr<std::vector<std::future<NT>>> futures, INT &destination)
161 {
162 for (uint i = 0; i < futures->size(); ++i)
163 destination[i] = (*futures)[i].get();
164 destination.update();
165 }
166
183 template <typename NT, typename FUN, typename GRID, typename... T>
184 void get_data(NT *dest, FUN &fun, const GRID &grid, const double k, std::tuple<T...> args)
185 {
186 std::apply([&](auto... args) { get_data<NT>(dest, fun, grid, k, args...); }, args);
187 }
188
205 template <typename NT, typename FUN, typename GRID, typename... T>
206 void get_data(NT *dest, FUN &fun, const GRID &grid, const double k, T... args)
207 {
208 auto req_point = [&](auto... p) { return fun.template get<NT>(k, p..., args...); };
209
210 // for (uint i = 0; i < grid.size(); ++i) {
211 tbb::parallel_for(tbb::blocked_range<uint>(0, grid.size()), [&](const tbb::blocked_range<uint> &r) {
212 for (uint i = r.begin(); i < r.end(); ++i) {
213 auto p = grid[i];
214 if constexpr (std::is_same_v<decltype(p), double> || std::is_same_v<decltype(p), float>)
215 dest[i] = req_point(p);
216 else
217 std::apply([&](auto... p) { dest[i] = req_point(p...); }, p);
218 }
219 });
220 }
221
222} // namespace DiFfRG
Definition coordinates.hh:15
Definition complex_math.hh:14
constexpr auto & get(named_tuple< tuple_type, strs... > &ob)
get a reference to the element with the given name
Definition tuples.hh:82
void get_data(NT *dest, FUN &fun, const GRID &grid, const double k, std::tuple< T... > args)
For a given grid and arguments, this function will call fun.request(k, grid[...].....
Definition utils.hh:184
void update_data(std::shared_ptr< std::vector< std::future< NT1 > > > futures, NT2 *destination)
Obtain data from a vector of futures and store it in a destination array.
Definition utils.hh:143
void update_interpolator(std::shared_ptr< std::vector< std::future< NT > > > futures, INT &destination)
Obtain data from a vector of futures and store it in an interpolator.
Definition utils.hh:160
unsigned int uint
Definition utils.hh:22
std::shared_ptr< std::vector< std::future< NT > > > request_data(FUN &fun, Coordinates &coordinates, const double k, std::tuple< T... > args)
For a given set of coordinates and arguments, this function will call fun.request(k,...
Definition utils.hh:33
auto make_grid(const Coordinates &coordinates)
Definition coordinates.hh:200