diff --git a/include/aare/NDView.hpp b/include/aare/NDView.hpp index 4c16742..56bca2f 100644 --- a/include/aare/NDView.hpp +++ b/include/aare/NDView.hpp @@ -178,6 +178,22 @@ class NDView : public ArrayExpr, Ndim> { const T *data() const { return buffer_; } void print_all() const; + /** + * @brief Create a subview of a range of the first dimension. + * This is useful for splitting a batches of frames in parallel processing. + * @param first The first index of the subview (inclusive). + * @param last The last index of the subview (exclusive). + * @return A new NDView that is a subview of the current view. + * @throws std::runtime_error if the range is invalid. + */ + NDView sub_view(ssize_t first, ssize_t last) const { + if (first < 0 || last > shape_[0] || first >= last) + throw std::runtime_error(LOCATION + "Invalid sub_view range"); + auto new_shape = shape_; + new_shape[0] = last - first; + return NDView(buffer_ + first * strides_[0], new_shape); + } + private: T *buffer_{nullptr}; std::array strides_{}; diff --git a/include/aare/calibration.hpp b/include/aare/calibration.hpp index ce69e95..0677127 100644 --- a/include/aare/calibration.hpp +++ b/include/aare/calibration.hpp @@ -3,6 +3,7 @@ #include "aare/NDArray.hpp" #include "aare/NDView.hpp" #include "aare/defs.hpp" +#include "aare/utils/par.hpp" #include "aare/utils/task.hpp" #include #include @@ -111,35 +112,30 @@ void apply_calibration(NDView res, NDView raw_data, for (const auto &lim : limits) futures.push_back(std::async( static_cast, NDView, - NDView, NDView, int, int)>( + NDView, NDView, int, + int)>( apply_calibration_impl), res, raw_data, ped, cal, lim.first, lim.second)); for (auto &f : futures) f.get(); } + std::pair, NDArray> sum_and_count_per_gain(NDView raw_data); std::pair, NDArray> sum_and_count_g0(NDView raw_data); - template NDArray calculate_pedestal_g0(NDView raw_data, - ssize_t n_threads) { + ssize_t n_threads) { std::vector, NDArray>>> futures; futures.reserve(n_threads); - auto limits = split_task(0, raw_data.shape(0), n_threads); - // make subviews for each thread - std::vector> subviews; - for (const auto &lim : limits) { - subviews.emplace_back( - raw_data.data() + lim.first * raw_data.strides()[0], - std::array{lim.second - lim.first, raw_data.shape(1), - raw_data.shape(2)}); - } + + auto subviews = make_subviews(raw_data, n_threads); + for (auto view : subviews) { futures.push_back(std::async( static_cast, NDArray> (*)( @@ -157,26 +153,17 @@ NDArray calculate_pedestal_g0(NDView raw_data, } return safe_divide(accumulator, count); - } - template NDArray calculate_pedestal(NDView raw_data, ssize_t n_threads) { std::vector, NDArray>>> futures; futures.reserve(n_threads); - auto limits = split_task(0, raw_data.shape(0), n_threads); - // make subviews for each thread - std::vector> subviews; - for (const auto &lim : limits) { - subviews.emplace_back( - raw_data.data() + lim.first * raw_data.strides()[0], - std::array{lim.second - lim.first, raw_data.shape(1), - raw_data.shape(2)}); - } + auto subviews = make_subviews(raw_data, n_threads); + for (auto view : subviews) { futures.push_back(std::async( static_cast, NDArray> (*)( diff --git a/include/aare/utils/par.hpp b/include/aare/utils/par.hpp index e52c897..55f60f4 100644 --- a/include/aare/utils/par.hpp +++ b/include/aare/utils/par.hpp @@ -1,7 +1,10 @@ +#pragma once #include #include #include +#include "aare/utils/task.hpp" + namespace aare { template @@ -15,4 +18,17 @@ void RunInParallel(F func, const std::vector> &tasks) { thread.join(); } } + + +template +std::vector> make_subviews(NDView &data, ssize_t n_threads) { + std::vector> subviews; + subviews.reserve(n_threads); + auto limits = split_task(0, data.shape(0), n_threads); + for (const auto &lim : limits) { + subviews.push_back(data.sub_view(lim.first, lim.second)); + } + return subviews; +} + } // namespace aare \ No newline at end of file diff --git a/include/aare/utils/task.hpp b/include/aare/utils/task.hpp index a6ee142..63fe691 100644 --- a/include/aare/utils/task.hpp +++ b/include/aare/utils/task.hpp @@ -1,4 +1,4 @@ - +#pragma once #include #include