2018-03-23 16:59:09 +11:00
|
|
|
/*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
|
|
|
* Copyright 2018 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CRUFT_UTIL_JOB_DISPATCH_HPP
|
|
|
|
#define CRUFT_UTIL_JOB_DISPATCH_HPP
|
|
|
|
|
|
|
|
#include "queue.hpp"
|
|
|
|
|
|
|
|
#include "../extent.hpp"
|
|
|
|
#include "../region.hpp"
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace util::job {
|
|
|
|
// call a function across all elements of a container using the supplied
|
|
|
|
// job queue.
|
|
|
|
//
|
|
|
|
// threads will have work sizes dictated by a supplied extent.
|
|
|
|
//
|
2018-03-27 16:20:56 +11:00
|
|
|
// 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.
|
2018-03-23 16:59:09 +11:00
|
|
|
//
|
|
|
|
// TODO: extend to 1d and 3d
|
|
|
|
template <
|
|
|
|
typename ContainerT,
|
|
|
|
typename FunctionT,
|
|
|
|
typename ...Args,
|
|
|
|
size_t DimensionV
|
|
|
|
>
|
2018-03-27 16:20:56 +11:00
|
|
|
auto
|
2018-03-23 16:59:09 +11:00
|
|
|
dispatch (
|
|
|
|
util::job::queue &q,
|
|
|
|
ContainerT &data,
|
|
|
|
util::extent<DimensionV,int> chunk,
|
2018-03-27 16:20:56 +11:00
|
|
|
FunctionT &&func,
|
2018-03-23 16:59:09 +11:00
|
|
|
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;
|
2018-03-27 16:16:35 +11:00
|
|
|
inner_data[p][0] = std::invoke (func, p, inner_args...);
|
2018-03-23 16:59:09 +11:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<queue::cookie> cookies;
|
|
|
|
|
|
|
|
const extent2i count = (data.extent () + chunk - 1) / chunk;
|
|
|
|
|
|
|
|
for (auto index: count.step ()) {
|
|
|
|
util::point<DimensionV,int> base = index * chunk;
|
|
|
|
cookies.push_back (
|
|
|
|
q.submit (
|
|
|
|
chunked_func,
|
|
|
|
std::ref (data),
|
|
|
|
util::region<DimensionV,int> {base, chunk}.step (),
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|