2018-03-23 16:59:09 +11:00
|
|
|
/*
|
2018-08-04 15:14:06 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2018-03-23 16:59:09 +11:00
|
|
|
*
|
|
|
|
* Copyright 2018 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
2020-03-12 12:45:45 +11:00
|
|
|
#pragma once
|
2018-03-23 16:59:09 +11:00
|
|
|
|
|
|
|
#include "queue.hpp"
|
|
|
|
|
|
|
|
#include "../extent.hpp"
|
|
|
|
#include "../region.hpp"
|
2020-03-12 12:45:45 +11:00
|
|
|
#include "../concepts.hpp"
|
2018-03-23 16:59:09 +11:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
namespace cruft::job {
|
2019-03-08 09:42:15 +11:00
|
|
|
/// call a function across all elements of a container using the supplied
|
|
|
|
/// job queue.
|
|
|
|
///
|
|
|
|
/// threads will have work sizes dictated by a supplied extent.
|
|
|
|
///
|
|
|
|
/// returns a cookie that will block at destruction until all jobs have
|
|
|
|
/// completed. it will take ownership of an forwarding-reference function
|
|
|
|
/// if one is supplied.
|
|
|
|
///
|
|
|
|
/// TODO: extend to 1d and 3d
|
2020-03-12 12:45:45 +11:00
|
|
|
///
|
|
|
|
/// \param area The total size of the work unit
|
|
|
|
/// \param chunk The size of the subdivided units to be send to threads.
|
2018-03-23 16:59:09 +11:00
|
|
|
template <
|
|
|
|
typename ContainerT,
|
|
|
|
typename FunctionT,
|
2020-03-12 12:45:45 +11:00
|
|
|
typename ...ArgsT,
|
2018-03-23 16:59:09 +11:00
|
|
|
size_t DimensionV
|
|
|
|
>
|
2020-03-12 12:45:45 +11:00
|
|
|
requires requires (
|
|
|
|
ContainerT t,
|
|
|
|
cruft::point<DimensionV,int> p,
|
|
|
|
FunctionT f,
|
|
|
|
ArgsT ...args
|
|
|
|
) {
|
|
|
|
{ t[p] = std::invoke (f, p, args...) };
|
|
|
|
}
|
2018-03-27 16:20:56 +11:00
|
|
|
auto
|
2018-03-23 16:59:09 +11:00
|
|
|
dispatch (
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::job::queue &q,
|
2018-03-23 16:59:09 +11:00
|
|
|
ContainerT &data,
|
2020-03-12 12:45:45 +11:00
|
|
|
cruft::region<DimensionV,int> total_area,
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::extent<DimensionV,int> chunk,
|
2018-03-27 16:20:56 +11:00
|
|
|
FunctionT &&func,
|
2020-03-12 12:45:45 +11:00
|
|
|
ArgsT ...args
|
|
|
|
) {
|
|
|
|
auto chunked_func = [&func] (
|
|
|
|
ContainerT &inner_data,
|
|
|
|
auto param,
|
|
|
|
ArgsT... inner_args
|
|
|
|
) {
|
|
|
|
for (cruft::point<DimensionV,int> p: param)
|
2019-03-08 09:42:15 +11:00
|
|
|
inner_data[p] = std::invoke (func, p, inner_args...);
|
2018-03-23 16:59:09 +11:00
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<queue::cookie> cookies;
|
|
|
|
|
2020-03-12 12:45:45 +11:00
|
|
|
extent2i const count = (total_area.e + chunk - 1) / chunk;
|
2018-03-23 16:59:09 +11:00
|
|
|
|
|
|
|
for (auto index: count.step ()) {
|
2020-03-12 12:45:45 +11:00
|
|
|
cruft::point<DimensionV,int> base = index * chunk + total_area.p.template as<cruft::vector> ();
|
|
|
|
region<DimensionV,int> const oversized_work { base, chunk };
|
|
|
|
auto const constrained_work = intersection (oversized_work, total_area);
|
2018-03-23 16:59:09 +11:00
|
|
|
cookies.push_back (
|
|
|
|
q.submit (
|
|
|
|
chunked_func,
|
|
|
|
std::ref (data),
|
2020-03-12 12:45:45 +11:00
|
|
|
constrained_work.step (),
|
2018-03-23 16:59:09 +11:00
|
|
|
args...
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-03-27 16:20:56 +11:00
|
|
|
return std::tuple (std::forward<FunctionT> (func), std::move (cookies));
|
2018-03-23 16:59:09 +11:00
|
|
|
}
|
|
|
|
}
|