Commit aeef3fc0 authored by Dominik Charousset's avatar Dominik Charousset

Remove calls to deprecated APIs

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