Permalink
Browse files

maintenance and documentation

{apply_args => call}.hpp, added new function: `call_mv` (calls a function
and moves all arguments), and documented spawn_cl function family
  • Loading branch information...
Neverlord committed Apr 16, 2013
1 parent 9bf318f commit 8be5138ac21a1a801c7778e89828e44770a1d736
View
@@ -142,10 +142,10 @@ cppa/tpartial_function.hpp
cppa/tuple_cast.hpp
cppa/uniform_type_info.hpp
cppa/util/abstract_uniform_type_info.hpp
-cppa/util/apply_args.hpp
cppa/util/arg_match_t.hpp
cppa/util/at.hpp
cppa/util/buffer.hpp
+cppa/util/call.hpp
cppa/util/callable_trait.hpp
cppa/util/comparable.hpp
cppa/util/compare_tuples.hpp
@@ -195,6 +195,8 @@ examples/hello_world.cpp
examples/message_passing/calculator.cpp
examples/message_passing/dancing_kirby.cpp
examples/message_passing/dining_philosophers.cpp
+examples/opencl/proper_matrix.cpp
+examples/opencl/simple_matrix.cpp
examples/qtsupport/chatwidget.cpp
examples/qtsupport/chatwidget.hpp
examples/qtsupport/chatwindow.ui
@@ -303,5 +305,3 @@ unit_testing/test_sync_send.cpp
unit_testing/test_tuple.cpp
unit_testing/test_uniform_type.cpp
unit_testing/test_yield_interface.cpp
-examples/opencl/simple_matrix.cpp
-examples/opencl/proper_matrix.cpp
@@ -33,9 +33,9 @@
#include <type_traits>
+#include "cppa/util/call.hpp"
#include "cppa/util/rm_ref.hpp"
#include "cppa/util/int_list.hpp"
-#include "cppa/util/apply_args.hpp"
#include "cppa/detail/tdata.hpp"
#include "cppa/scheduled_actor.hpp"
@@ -34,10 +34,10 @@
#include "cppa/option.hpp"
#include "cppa/guard_expr.hpp"
+#include "cppa/util/call.hpp"
#include "cppa/util/int_list.hpp"
#include "cppa/util/rm_option.hpp"
#include "cppa/util/type_list.hpp"
-#include "cppa/util/apply_args.hpp"
#include "cppa/util/left_or_right.hpp"
#include "cppa/util/get_result_type.hpp"
View
@@ -41,9 +41,9 @@
#include "cppa/option.hpp"
#include "cppa/util/at.hpp"
+#include "cppa/util/call.hpp"
#include "cppa/util/rm_ref.hpp"
#include "cppa/util/void_type.hpp"
-#include "cppa/util/apply_args.hpp"
#include "cppa/util/rebindable_reference.hpp"
#include "cppa/detail/tdata.hpp"
View
@@ -35,12 +35,12 @@
#include "cppa/guard_expr.hpp"
#include "cppa/tpartial_function.hpp"
+#include "cppa/util/call.hpp"
#include "cppa/util/rm_ref.hpp"
#include "cppa/util/int_list.hpp"
#include "cppa/util/type_list.hpp"
#include "cppa/util/rm_option.hpp"
#include "cppa/util/purge_refs.hpp"
-#include "cppa/util/apply_args.hpp"
#include "cppa/util/disjunction.hpp"
#include "cppa/util/left_or_right.hpp"
#include "cppa/util/deduce_ref_type.hpp"
View
@@ -33,97 +33,113 @@
#include "cppa/option.hpp"
#include "cppa/cow_tuple.hpp"
-
+#include "cppa/util/call.hpp"
#include "cppa/opencl/global.hpp"
#include "cppa/opencl/command_dispatcher.hpp"
namespace cppa {
-template<typename Signature>
+namespace detail {
+
+template<typename Signature, typename SecondSignature = void>
struct cl_spawn_helper;
template<typename R, typename... Ts>
-struct cl_spawn_helper<R (Ts...)> {
- template<typename... Us>
- actor_ptr operator()(const char* source, const char* fname, Us&&... args) {
- auto p = opencl::program::create(source);
- auto cd = opencl::get_command_dispatcher();
- return cd->spawn<R, Ts...>(p, fname, std::forward<Us>(args)...);
- }
+struct cl_spawn_helper<R (Ts...), void> {
template<typename... Us>
actor_ptr operator()(const opencl::program& p, const char* fname, Us&&... args) {
auto cd = opencl::get_command_dispatcher();
return cd->spawn<R, Ts...>(p, fname, std::forward<Us>(args)...);
}
};
-template<typename MapArgs, typename MapResult>
-struct get_cl_spawn_helper;
-
template<typename R, typename... Ts>
-struct get_cl_spawn_helper<std::function<option<cow_tuple<Ts...>> (any_tuple)>,
- std::function<any_tuple (R&)>> {
- typedef cl_spawn_helper<R (const Ts&...)> type;
-};
+struct cl_spawn_helper<std::function<option<cow_tuple<Ts...>> (any_tuple)>,
+ std::function<any_tuple (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.
+ * @throws std::runtime_error if more than three dimensions are set,
+ * <tt>dims.empty()</tt>, or @p clCreateKernel
+ * failed.
+ */
template<typename Signature, typename... Ts>
-actor_ptr spawn_cl(const char* source,
- const char* fun_name,
- std::vector<size_t> dimensions,
- std::vector<size_t> offset = {},
- std::vector<size_t> local_dims = {}) {
- using std::move;
- cl_spawn_helper<Signature> f;
- return f(source, fun_name, move(dimensions), move(offset), move(local_dims));
+inline actor_ptr spawn_cl(const opencl::program& prog,
+ const char* fname,
+ std::vector<size_t> dims,
+ std::vector<size_t> offset = {},
+ std::vector<size_t> local_dims = {}) {
+ detail::cl_spawn_helper<Signature> f;
+ return util::call_mv(f, prog, fname, dims, offset, local_dims);
}
-template<typename MapArgs, typename MapResult>
-actor_ptr spawn_cl(const char* source,
- const char* fun_name,
- MapArgs map_args,
- MapResult map_result,
- std::vector<size_t> dimensions,
- std::vector<size_t> offset = {},
- std::vector<size_t> local_dims = {}) {
- using std::move;
- typedef typename util::get_callable_trait<MapArgs>::type t0;
- typedef typename util::get_callable_trait<MapResult>::type t1;
- typedef typename t0::fun_type f0;
- typedef typename t1::fun_type f1;
- typename get_cl_spawn_helper<f0,f1>::type f;
- return f(source, fun_name,
- move(dimensions), move(offset), move(local_dims),
- f0{map_args}, f1{map_result});
+/**
+ * @brief Compiles @p source and creates a new actor facade for an OpenCL kernel
+ * that invokes the function named @p 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_ptr spawn_cl(const char* source,
+ const char* fname,
+ std::vector<size_t> dims,
+ std::vector<size_t> offset = {},
+ std::vector<size_t> local_dims = {}) {
+ auto prog = opencl::program::create(source);
+ detail::cl_spawn_helper<Signature> f;
+ return util::call_mv(f, prog, fname, dims, offset, local_dims);
}
-template<typename Signature, typename... Ts>
-actor_ptr spawn_cl(const opencl::program& prog,
- const char* fun_name,
- std::vector<size_t> dimensions,
- std::vector<size_t> offset = {},
- std::vector<size_t> local_dims = {}) {
- using std::move;
- cl_spawn_helper<Signature> f;
- return f(prog, fun_name, move(dimensions), move(offset), move(local_dims));
+/**
+ * @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.
+ * @throws std::runtime_error if more than three dimensions are set,
+ * <tt>dims.empty()</tt>, or @p clCreateKernel
+ * failed.
+ */
+template<typename MapArgs, typename MapResult>
+inline actor_ptr spawn_cl(const opencl::program& prog,
+ const char* fname,
+ MapArgs map_args,
+ MapResult map_result,
+ std::vector<size_t> dims,
+ std::vector<size_t> offset = {},
+ std::vector<size_t> local_dims = {}) {
+ typedef typename util::get_callable_trait<MapArgs>::fun_type f0;
+ typedef typename util::get_callable_trait<MapResult>::fun_type f1;
+ detail::cl_spawn_helper<f0,f1> f;
+ return util::call_mv(f, prog, fname, dims, offset, local_dims,
+ f0{map_args}, f1{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.
+ * @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>
-actor_ptr spawn_cl(const opencl::program& prog,
- const char* fun_name,
- MapArgs map_args,
- MapResult map_result,
- std::vector<size_t> dimensions,
- std::vector<size_t> offset = {},
- std::vector<size_t> local_dims = {}) {
+inline actor_ptr spawn_cl(const char* source,
+ const char* fun_name,
+ MapArgs map_args,
+ MapResult map_result,
+ std::vector<size_t> dims,
+ std::vector<size_t> offset = {},
+ std::vector<size_t> local_dims = {}) {
using std::move;
- typedef typename util::get_callable_trait<MapArgs>::type t0;
- typedef typename util::get_callable_trait<MapResult>::type t1;
- typedef typename t0::fun_type f0;
- typedef typename t1::fun_type f1;
- typename get_cl_spawn_helper<f0,f1>::type f;
- return f(prog, fun_name,
- move(dimensions), move(offset), move(local_dims),
- f0{map_args}, f1{map_result});
+ return spawn_cl(opencl::program::create(source), fun_name,
+ move(map_args), move(map_result),
+ move(dims), move(offset), move(local_dims));
}
} // namespace cppa
View
@@ -156,7 +156,7 @@ class command_impl : public command {
sizeof(typename T::value_type) * m_number_of_values,
result.data(),
0,
- NULL,
+ nullptr,
&read_event);
clReleaseEvent(read_event);
if (err != CL_SUCCESS) {
@@ -34,9 +34,9 @@
#include <cstddef>
#include <type_traits>
+#include "cppa/util/call.hpp"
#include "cppa/util/rm_ref.hpp"
#include "cppa/util/type_list.hpp"
-#include "cppa/util/apply_args.hpp"
#include "cppa/util/left_or_right.hpp"
#include "cppa/util/callable_trait.hpp"
#include "cppa/util/is_mutable_ref.hpp"
@@ -28,10 +28,8 @@
\******************************************************************************/
-#ifndef CPPA_APPLY_ARGS_HPP
-#define CPPA_APPLY_ARGS_HPP
-
-#include <cstddef>
+#ifndef CALL_HPP
+#define CALL_HPP
#include "cppa/get.hpp"
#include "cppa/util/int_list.hpp"
@@ -56,6 +54,11 @@ inline auto apply_args_suffxied(F& f, Tuple& tup, util::int_list<Is...>, Ts&&...
return f(get_cv_aware<Is>(tup)..., std::forward<Ts>(args)...);
}
+template<typename F, typename... Ts>
+inline auto call_mv(F& f, Ts&&... args) -> decltype(f(std::move(args)...)) {
+ return f(std::move(args)...);
+}
+
} } // namespace cppa::util
-#endif // CPPA_APPLY_ARGS_HPP
+#endif // CALL_HPP
@@ -91,16 +91,17 @@ struct get_callable_trait {
// type without cv qualifiers
typedef typename rm_ref<T>::type bare_type;
// if type is a function pointer, this typedef identifies the function
- typedef typename std::remove_pointer<bare_type>::type fun_type;
+ typedef typename std::remove_pointer<bare_type>::type signature_type;
typedef typename get_callable_trait_helper<
std::is_function<bare_type>::value
- || std::is_function<fun_type>::value,
+ || std::is_function<signature_type>::value,
std::is_member_function_pointer<bare_type>::value,
bare_type
>::type
type;
typedef typename type::result_type result_type;
typedef typename type::arg_types arg_types;
+ typedef typename type::fun_type fun_type;
};
template<typename C>
@@ -146,12 +146,12 @@ void command_dispatcher::initialize() {
int pid{0};
cl_uint num_devices{0};
cl_device_type dev_type{CL_DEVICE_TYPE_GPU};
- err = clGetDeviceIDs(ids[pid], dev_type, 0, NULL, &num_devices);
+ err = clGetDeviceIDs(ids[pid], dev_type, 0, nullptr, &num_devices);
if (err == CL_DEVICE_NOT_FOUND) {
CPPA_LOG_TRACE("No gpu devices found. Looking for cpu devices.");
cout << "No gpu devices found. Looking for cpu devices." << endl;
dev_type = CL_DEVICE_TYPE_CPU;
- err = clGetDeviceIDs(ids[pid], dev_type, 0, NULL, &num_devices);
+ err = clGetDeviceIDs(ids[pid], dev_type, 0, nullptr, &num_devices);
}
if (err != CL_SUCCESS) {
ostringstream oss;
@@ -162,7 +162,7 @@ void command_dispatcher::initialize() {
throw runtime_error(oss.str());
}
vector<cl_device_id> devices(num_devices);
- err = clGetDeviceIDs(ids[pid], dev_type, num_devices, devices.data(), NULL);
+ err = clGetDeviceIDs(ids[pid], dev_type, num_devices, devices.data(), nullptr);
if (err != CL_SUCCESS) {
ostringstream oss;
oss << "clGetDeviceIDs: '"
@@ -173,7 +173,7 @@ void command_dispatcher::initialize() {
}
/* create a context */
- m_context.adopt(clCreateContext(0, 1, devices.data(), NULL, NULL, &err));
+ m_context.adopt(clCreateContext(0, 1, devices.data(), nullptr, nullptr, &err));
if (err != CL_SUCCESS) {
ostringstream oss;
oss << "clCreateContext: '"
Oops, something went wrong.

0 comments on commit 8be5138

Please sign in to comment.