Commit aeef3fc0 authored by Dominik Charousset's avatar Dominik Charousset

Remove calls to deprecated APIs

parent 4356a5d4
......@@ -29,7 +29,6 @@
#include "caf/channel.hpp"
#include "caf/to_string.hpp"
#include "cppa/tuple_cast.hpp"
#include "caf/intrusive_ptr.hpp"
#include "caf/detail/int_list.hpp"
......@@ -47,26 +46,28 @@ namespace opencl {
class opencl_metainfo;
template <typename Signature>
template <class Signature>
class actor_facade;
template <typename Ret, typename... Args>
template <class Ret, typename... Args>
class actor_facade<Ret(Args...)> : public abstract_actor {
friend class command<actor_facade, Ret>;
public:
using args_tuple =
cow_tuple<typename std::decay<Args>::type...>;
using arg_mapping = std::function<optional<args_tuple>(message)>;
using arg_types = detail::type_list<typename std::decay<Args>::type...>;
using arg_mapping = std::function<optional<message> (message&)>;
using result_mapping = std::function<message(Ret&)>;
using evnt_vec = std::vector<cl_event>;
using args_vec = std::vector<mem_ptr>;
using command_type = command<actor_facade, Ret>;
static intrusive_ptr<actor_facade>
create(const program& prog, const char* kernel_name, arg_mapping map_args,
result_mapping map_result, const dim_vec& global_dims,
const dim_vec& offsets, const dim_vec& local_dims,
size_t result_size) {
create(const program& prog, const char* kernel_name,
const dim_vec& global_dims, const dim_vec& offsets,
const dim_vec& local_dims, size_t result_size,
arg_mapping map_args = arg_mapping{},
result_mapping map_result = result_mapping{}) {
if (global_dims.empty()) {
auto str = "OpenCL kernel needs at least 1 global dimension.";
CAF_LOGM_ERROR("caf::opencl::actor_facade", str);
......@@ -84,32 +85,50 @@ class actor_facade<Ret(Args...)> : public abstract_actor {
check_vec(offsets, "offsets");
check_vec(local_dims, "local dimensions");
kernel_ptr kernel;
kernel.adopt(v2get(CAF_CLF(clCreateKernel), prog.m_program.get(),
kernel_name));
kernel.reset(v2get(CAF_CLF(clCreateKernel), prog.m_program.get(),
kernel_name),
false);
if (result_size == 0) {
result_size = std::accumulate(global_dims.begin(), global_dims.end(),
size_t{1}, std::multiplies<size_t>{});
}
return new actor_facade<Ret(Args...)>{
prog, kernel, global_dims, offsets,
local_dims, std::move(map_args), std::move(map_result), result_size};
return new actor_facade(prog, kernel, global_dims, offsets, local_dims,
result_size, std::move(map_args),
std::move(map_result));
}
void enqueue(const actor_addr &sender, message_id mid, message content,
execution_unit*) override {
CAF_LOG_TRACE("");
typename detail::il_indices<detail::type_list<Args...>>::type indices;
enqueue_impl(sender, mid, std::move(content), indices);
if (m_map_args) {
auto mapped = m_map_args(content);
if (!mapped) {
return;
}
content = std::move(*mapped);
}
typename detail::il_indices<arg_types>::type indices;
if (!content.match_elements(arg_types{})) {
return;
}
response_promise handle{this->address(), sender, mid.response_id()};
evnt_vec events;
args_vec arguments;
add_arguments_to_kernel<Ret>(events, arguments, m_result_size,
content, indices);
auto cmd = detail::make_counted<command_type>(handle, this,
std::move(events),
std::move(arguments),
m_result_size,
std::move(content));
cmd->enqueue();
}
private:
using evnt_vec = std::vector<cl_event>;
using args_vec = std::vector<mem_ptr>;
actor_facade(const program& prog, kernel_ptr kernel,
const dim_vec& global_dimensions, const dim_vec& global_offsets,
const dim_vec& local_dimensions, arg_mapping map_args,
result_mapping map_result, size_t result_size)
const dim_vec& local_dimensions, size_t result_size,
arg_mapping map_args, result_mapping map_result)
: m_kernel(kernel),
m_program(prog.m_program),
m_context(prog.m_context),
......@@ -117,45 +136,16 @@ class actor_facade<Ret(Args...)> : public abstract_actor {
m_global_dimensions(global_dimensions),
m_global_offsets(global_offsets),
m_local_dimensions(local_dimensions),
m_result_size(result_size),
m_map_args(std::move(map_args)),
m_map_result(std::move(map_result)),
m_result_size(result_size) {
m_map_result(std::move(map_result)) {
CAF_LOG_TRACE("id: " << this->id());
}
template <long... Is>
void enqueue_impl(const actor_addr& sender, message_id mid, message msg,
detail::int_list<Is...>) {
auto opt = m_map_args(std::move(msg));
if (opt) {
response_promise handle{this->address(), sender, mid.response_id()};
evnt_vec events;
args_vec arguments;
add_arguments_to_kernel<Ret>(events, arguments, m_result_size,
get_ref<Is>(*opt)...);
auto cmd = detail::make_counted<command<actor_facade, Ret>>(
handle, this, std::move(events), std::move(arguments), m_result_size,
*opt);
cmd->enqueue();
} else {
CAF_LOGMF(CAF_ERROR, "actor_facade::enqueue() tuple_cast failed.");
}
}
kernel_ptr m_kernel;
program_ptr m_program;
context_ptr m_context;
command_queue_ptr m_queue;
dim_vec m_global_dimensions;
dim_vec m_global_offsets;
dim_vec m_local_dimensions;
arg_mapping m_map_args;
result_mapping m_map_result;
size_t m_result_size;
void add_arguments_to_kernel_rec(evnt_vec&, args_vec& arguments) {
void add_arguments_to_kernel_rec(evnt_vec&, args_vec& arguments, message&,
detail::int_list<>) {
// rotate left (output buffer to the end)
rotate(begin(arguments), begin(arguments) + 1, end(arguments));
std::rotate(arguments.begin(), arguments.begin() + 1, arguments.end());
for (cl_uint i = 0; i < arguments.size(); ++i) {
v1callcl(CAF_CLF(clSetKernelArg), m_kernel.get(), i,
sizeof(cl_mem), static_cast<void*>(&arguments[i]));
......@@ -163,34 +153,48 @@ class actor_facade<Ret(Args...)> : public abstract_actor {
clFlush(m_queue.get());
}
template <typename T0, typename... Ts>
template <long I, long... Is>
void add_arguments_to_kernel_rec(evnt_vec& events, args_vec& arguments,
T0& arg0, Ts&... args) {
size_t buffer_size = sizeof(typename T0::value_type) * arg0.size();
message& msg, detail::int_list<I, Is...>) {
using value_type = typename detail::tl_at<arg_types, I>::type;
auto& arg = msg.get_as<value_type>(I);
size_t buffer_size = sizeof(value_type) * arg.size();
auto buffer = v2get(CAF_CLF(clCreateBuffer), m_context.get(),
cl_mem_flags{CL_MEM_READ_ONLY}, buffer_size, nullptr);
cl_event event = v1get<cl_event>(CAF_CLF(clEnqueueWriteBuffer),
m_queue.get(), buffer, cl_bool{CL_FALSE},
cl_uint{0},buffer_size, arg0.data());
cl_uint{0}, buffer_size, arg.data());
events.push_back(std::move(event));
mem_ptr tmp;
tmp.adopt(std::move(buffer));
tmp.reset(buffer, false);
arguments.push_back(tmp);
add_arguments_to_kernel_rec(events, arguments, args...);
add_arguments_to_kernel_rec(events, arguments, msg,
detail::int_list<Is...>{});
}
template <typename R, typename... Ts>
template <class R, class Token>
void add_arguments_to_kernel(evnt_vec& events, args_vec& arguments,
size_t ret_size, Ts&&... args) {
size_t ret_size, message& msg, Token tk) {
arguments.clear();
auto buf = v2get(CAF_CLF(clCreateBuffer), m_context.get(),
cl_mem_flags{CL_MEM_WRITE_ONLY},
sizeof(typename R::value_type) * ret_size, nullptr);
mem_ptr tmp;
tmp.adopt(std::move(buf));
tmp.reset(buf, false);
arguments.push_back(tmp);
add_arguments_to_kernel_rec(events, arguments, std::forward<Ts>(args)...);
add_arguments_to_kernel_rec(events, arguments, msg, tk);
}
kernel_ptr m_kernel;
program_ptr m_program;
context_ptr m_context;
command_queue_ptr m_queue;
dim_vec m_global_dimensions;
dim_vec m_global_offsets;
dim_vec m_local_dimensions;
size_t m_result_size;
arg_mapping m_map_args;
result_mapping m_map_result;
};
} // namespace opencl
......
......@@ -25,7 +25,6 @@
#include "caf/actor_cast.hpp"
#include "caf/optional.hpp"
#include "cppa/cow_tuple.hpp"
#include "caf/detail/limited_vector.hpp"
......@@ -48,127 +47,98 @@ struct carr_to_vec<T*> {
using type = std::vector<T>;
};
template <typename Signature, typename SecondSignature = void>
template <class Signature>
struct cl_spawn_helper;
template <typename R, typename... Ts>
struct cl_spawn_helper<R(Ts...), void> {
using result_type = typename carr_to_vec<R>::type;
using impl = opencl::actor_facade<
result_type(typename carr_to_vec<typename carr_to_vec<Ts>::type>::type...)>;
using map_arg_fun = typename impl::arg_mapping;
struct cl_spawn_helper<R(Ts...)> {
using res_t = typename carr_to_vec<R>::type;
using impl = opencl::actor_facade<res_t (typename carr_to_vec<Ts>::type...)>;
using map_arg_fun = std::function<optional<message> (message&)>;
using map_res_fun = typename impl::result_mapping;
template <typename... Us>
actor operator()(map_arg_fun f0, map_res_fun f1, const opencl::program& p,
const char* fname, Us&&... args) const {
using std::move;
using std::forward;
return actor_cast<actor>(
impl::create(p, fname, move(f0), move(f1), forward<Us>(args)...));
}
template <typename... Us>
actor operator()(const opencl::program& p, const char* fname,
Us&&... args) const {
using std::move;
using std::forward;
map_arg_fun f0 = [](message msg) {
return tuple_cast<typename std::decay<
typename carr_to_vec<Ts>::type>::type...>(msg);
};
map_res_fun f1 = [](result_type& result) {
return make_message(move(result));
};
return actor_cast<actor>(
impl::create(p, fname, move(f0), move(f1), forward<Us>(args)...));
actor operator()(const opencl::program& p, const char* fn, Us&&... vs) const {
return actor_cast<actor>(impl::create(p, fn, std::forward<Us>(vs)...));
}
};
template <typename R, typename... Ts>
struct cl_spawn_helper<std::function<optional<cow_tuple<Ts...>>(message)>,
std::function<message(R&)>>
: cl_spawn_helper<R(Ts...)> {};
} // namespace detail
/**
* @brief Creates a new actor facade for an OpenCL kernel that invokes
* the function named @p fname from @p prog.
* Creates a new actor facade for an OpenCL kernel that invokes
* the function named `fname` from `prog`.
* @throws std::runtime_error if more than three dimensions are set,
* <tt>dims.empty()</tt>, or @p clCreateKernel
* failed.
* `dims.empty()`, or `clCreateKernel` failed.
*/
template <typename Signature, typename... Ts>
inline actor
spawn_cl(const opencl::program& prog, const char* fname,
const opencl::dim_vec& dims, const opencl::dim_vec& offset = {},
const opencl::dim_vec& local_dims = {}, size_t result_size = 0) {
using std::move;
template <class Signature>
actor spawn_cl(const opencl::program& prog,
const char* fname,
const opencl::dim_vec& dims,
const opencl::dim_vec& offset = {},
const opencl::dim_vec& local_dims = {},
size_t result_size = 0) {
detail::cl_spawn_helper<Signature> f;
return f(prog, fname, dims, offset, local_dims, result_size);
}
/**
* @brief Compiles @p source and creates a new actor facade for an OpenCL kernel
* that invokes the function named @p fname.
* Compiles `source` and creates a new actor facade for an OpenCL kernel
* that invokes the function named `fname`.
* @throws std::runtime_error if more than three dimensions are set,
* <tt>dims.empty()</tt>, a compilation error
* occured, or @p clCreateKernel failed.
*/
template <typename Signature, typename... Ts>
inline actor
spawn_cl(const char* source, const char* fname, const opencl::dim_vec& dims,
template <class Signature>
actor spawn_cl(const char* source,
const char* fname,
const opencl::dim_vec& dims,
const opencl::dim_vec& offset = {},
const opencl::dim_vec& local_dims = {}, size_t result_size = 0) {
using std::move;
return spawn_cl<Signature, Ts...>(opencl::program::create(source), fname,
const opencl::dim_vec& local_dims = {},
size_t result_size = 0) {
return spawn_cl<Signature>(opencl::program::create(source), fname,
dims, offset, local_dims, result_size);
}
/**
* @brief Creates a new actor facade for an OpenCL kernel that invokes
* the function named @p fname from @p prog, using @p map_args
* to extract the function arguments from incoming messages and
* @p map_result to transform the result before sending it as response.
* Creates a new actor facade for an OpenCL kernel that invokes
* the function named `fname` from `prog`.
* @throws std::runtime_error if more than three dimensions are set,
* <tt>dims.empty()</tt>, or @p clCreateKernel
* failed.
* `dims.empty()`, or `clCreateKernel` failed.
*/
template <typename MapArgs, typename MapResult>
inline actor
spawn_cl(const opencl::program& prog, const char* fname, MapArgs map_args,
MapResult map_result, const opencl::dim_vec& dims,
template <class Signature, class Fun>
actor spawn_cl(const opencl::program& prog,
const char* fname,
std::function<optional<message> (message&)> map_args,
Fun map_result,
const opencl::dim_vec& dims,
const opencl::dim_vec& offset = {},
const opencl::dim_vec& local_dims = {}, size_t result_size = 0) {
using std::move;
using f0 = typename detail::get_callable_trait<MapArgs>::fun_type;
using f1 = typename detail::get_callable_trait<MapResult>::fun_type;
detail::cl_spawn_helper<f0, f1> f;
return f(f0{move(map_args)}, f1{move(map_result)}, prog, fname, dims, offset,
local_dims, result_size);
const opencl::dim_vec& local_dims = {},
size_t result_size = 0) {
detail::cl_spawn_helper<Signature> f;
return f(prog, fname, dims, offset, local_dims, result_size,
std::move(map_args), std::move(map_result));
}
/**
* @brief Compiles @p source and creates a new actor facade for an OpenCL kernel
* that invokes the function named @p fname, using @p map_args
* to extract the function arguments from incoming messages and
* @p map_result to transform the result before sending it as response.
* Compiles `source` and creates a new actor facade for an OpenCL kernel
* that invokes the function named `fname`.
* @throws std::runtime_error if more than three dimensions are set,
* <tt>dims.empty()</tt>, a compilation error
* occured, or @p clCreateKernel failed.
*/
template <typename MapArgs, typename MapResult>
inline actor
spawn_cl(const char* source, const char* fun_name, MapArgs map_args,
MapResult map_result, const opencl::dim_vec& dims,
template <class Signature, class Fun>
actor spawn_cl(const char* source,
const char* fname,
std::function<optional<message> (message&)> map_args,
Fun map_result,
const opencl::dim_vec& dims,
const opencl::dim_vec& offset = {},
const opencl::dim_vec& local_dims = {}, size_t result_size = 0) {
using std::move;
return spawn_cl(opencl::program::create(source), fun_name, move(map_args),
move(map_result), dims, offset, local_dims, result_size);
const opencl::dim_vec& local_dims = {},
size_t result_size = 0) {
detail::cl_spawn_helper<Signature> f;
return f(opencl::program::create(source), fname, dims, offset, local_dims,
result_size, std::move(map_args), std::move(map_result));
}
} // namespace caf
......
......@@ -142,28 +142,31 @@ void multiplier(event_based_actor* self) {
cout << "calculating square of matrix:" << endl
<< to_string(m1) << endl;
auto unbox_args = [](message& msg) -> optional<message> {
return msg.apply(
[](matrix_type& lhs, matrix_type& rhs) {
return make_message(std::move(lhs.data()), std::move(rhs.data()));
}
);
};
auto box_res = [] (fvec& result) -> message {
return make_message(matrix_type{move(result)});
};
// spawn an opencl actor
// 1st arg: source code of one or more opencl kernels
// 2nd arg: name of the kernel to use
auto worker = spawn_cl(kernel_source, kernel_name,
auto worker = spawn_cl<fvec (fvec&, fvec&)>(kernel_source, kernel_name,
// 3rd arg: the opencl function operates on vectors,
// this function converts a tuple of two matrices
// to a tuple of vectors; note that this function returns
// an option (an empty results causes the actor to ignore
// the message)
[] (message msg) -> optional<cow_tuple<fvec, fvec>> {
auto opt = tuple_cast<matrix_type, matrix_type>(msg);
if (opt) {
return make_cow_tuple(move(get_ref<0>(*opt).data()),
move(get_ref<1>(*opt).data()));
}
return none;
},
unbox_args,
// 4th arg: converts the ouptut vector back to a matrix that is then
// used as response message
[] (fvec& result) -> message {
return make_message(matrix_type{move(result)});
},
box_res,
// 5th arg: global dimension arguments for opencl's enqueue,
// creates matrix_size * matrix_size global work items
{matrix_size, matrix_size}
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment