job/dispatch: explicitly specify the dispatchable region

This commit is contained in:
Danny Robson 2020-03-12 12:45:45 +11:00
parent 8697c103d6
commit fb13c0fb0f
2 changed files with 62 additions and 38 deletions

View File

@ -6,13 +6,13 @@
* Copyright 2018 Danny Robson <danny@nerdcruft.net>
*/
#ifndef CRUFT_UTIL_JOB_DISPATCH_HPP
#define CRUFT_UTIL_JOB_DISPATCH_HPP
#pragma once
#include "queue.hpp"
#include "../extent.hpp"
#include "../region.hpp"
#include "../concepts.hpp"
#include <vector>
@ -27,42 +27,54 @@ namespace cruft::job {
/// if one is supplied.
///
/// TODO: extend to 1d and 3d
///
/// \param area The total size of the work unit
/// \param chunk The size of the subdivided units to be send to threads.
template <
typename ContainerT,
typename FunctionT,
typename ...Args,
typename ...ArgsT,
size_t DimensionV
>
requires requires (
ContainerT t,
cruft::point<DimensionV,int> p,
FunctionT f,
ArgsT ...args
) {
{ t[p] = std::invoke (f, p, args...) };
}
auto
dispatch (
cruft::job::queue &q,
ContainerT &data,
cruft::region<DimensionV,int> total_area,
cruft::extent<DimensionV,int> chunk,
FunctionT &&func,
Args ...args)
{
auto chunked_func = [&func] (ContainerT &inner_data,
auto param,
Args... inner_args)
{
for (auto p: param) {
if (!inner_data.extent ().exclusive (p))
continue;
ArgsT ...args
) {
auto chunked_func = [&func] (
ContainerT &inner_data,
auto param,
ArgsT... inner_args
) {
for (cruft::point<DimensionV,int> p: param)
inner_data[p] = std::invoke (func, p, inner_args...);
}
};
std::vector<queue::cookie> cookies;
const extent2i count = (data.extent () + chunk - 1) / chunk;
extent2i const count = (total_area.e + chunk - 1) / chunk;
for (auto index: count.step ()) {
cruft::point<DimensionV,int> base = index * chunk;
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);
cookies.push_back (
q.submit (
chunked_func,
std::ref (data),
cruft::region<DimensionV,int> {base, chunk}.step (),
constrained_work.step (),
args...
)
);
@ -71,5 +83,3 @@ namespace cruft::job {
return std::tuple (std::forward<FunctionT> (func), std::move (cookies));
}
}
#endif

View File

@ -14,39 +14,53 @@
///////////////////////////////////////////////////////////////////////////////
struct value {
template <typename T>
void operator= (T &&) { }
};
struct container {
cruft::point2i
operator[] (cruft::point2i p)&
{ return values.emplace_back () = p; }
bool is_unique (void)
{
cruft::coord::ordering<cruft::point2i> comp {};
std::sort (values.begin (), values.end (), comp);
auto const pos = std::adjacent_find (values.begin (), values.end ());
return pos == values.end ();
}
//-----------------------------------------------------------------------------
struct data_t {
cruft::extent2i extent (void) const { return { 4, 4 }; }
auto operator[] (cruft::point2i) { return value {};};
std::size_t size (void) const { return values.size (); }
std::vector<cruft::point2i> values;
};
//-----------------------------------------------------------------------------
void test_dispatch_uniqueness (cruft::TAP::logger &tap)
{
std::vector<cruft::point2i> points;
data_t data;
container store;
cruft::region2i const work_region {
cruft::point2i { 0 },
cruft::extent2i { 512 }
};
cruft::job::queue q (1, 512 * 512 + 1);
cruft::job::dispatch (
q, data,
q, store,
work_region,
cruft::extent2i {2},
[&] (cruft::point2i p)
{
points.push_back (p);
return 0;
});
[] (auto &&arg) { return arg; }
);
cruft::coord::ordering<cruft::point2i> comp {};
std::sort (points.begin (), points.end (), comp);
auto const pos = std::adjacent_find (points.begin (), points.end ());
tap.expect (pos == points.end (), "job::queue indices are unique");
tap.expect (
store.is_unique (),
"job::queue indices are unique"
);
tap.expect_eq (
store.size (),
std::size_t (work_region.magnitude ().area ()),
"job::queue evaluated expected times"
);
}