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

View File

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