Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

code optimizations

  • Loading branch information...
commit 96ad4c3da088530e498c392a8250d6cd925f6df3 1 parent 6e2420a
@josephnoir josephnoir authored
View
32 cppa/opencl/actor_facade.hpp
@@ -33,6 +33,7 @@
#define CPPA_OPENCL_ACTOR_FACADE_HPP
#include <ostream>
+#include <algorithm>
#include <stdexcept>
#include "cppa/cppa.hpp"
@@ -131,28 +132,19 @@ class actor_facade<Ret(Args...)> : public actor {
template<long... Is>
void enqueue_impl(const actor_ptr& sender, any_tuple msg, message_id id, util::int_list<Is...>) {
- //auto opt = tuple_cast<Args...>(msg);
+ if (m_global_dimensions.empty()) {
+ std::ostringstream oss;
+ oss << "actor_facade::enqueue() global dimensions can't be empty!";
+ CPPA_LOG_ERROR(oss.str());
+ throw std::runtime_error(oss.str());
+ }
auto opt = m_map_args(msg);
if (opt) {
response_handle handle{this, sender, id};
- size_t number_of_values = 1;
- if (!m_global_dimensions.empty()) {
- for (auto s : m_global_dimensions) {
- number_of_values *= s;
- }
- }
- else {
- number_of_values = get<0>(*opt).size();
- m_global_dimensions.push_back(number_of_values);
- m_global_dimensions.push_back(1);
- m_global_dimensions.push_back(1);
- }
- if (m_global_dimensions.empty() || number_of_values <= 0) {
- std::ostringstream oss;
- oss << "actor_facade::enqueue() can't handle dimension sizes!";
- CPPA_LOG_ERROR(oss.str());
- throw std::runtime_error(oss.str());
- }
+ size_t number_of_values{1};
+ std::for_each(m_global_dimensions.begin(),
+ m_global_dimensions.end(),
+ [&](const size_t& s) { number_of_values *= s; });
Ret result_buf(number_of_values);
std::vector<mem_ptr> arguments;
add_arguments_to_kernel(arguments,
@@ -164,7 +156,7 @@ class actor_facade<Ret(Args...)> : public actor {
enqueue_to_dispatcher(m_dispatcher,
make_counted<command_impl<Ret>>(handle,
m_kernel,
- arguments,
+ std::move(arguments),
m_global_dimensions,
m_global_offsets,
m_local_dimensions,
View
17 cppa/opencl/command.hpp
@@ -33,6 +33,7 @@
#define CPPA_OPENCL_COMMAND_HPP
#include <vector>
+#include <algorithm>
#include <functional>
#include "cppa/actor.hpp"
@@ -69,10 +70,10 @@ class command_impl : public command {
command_impl(response_handle handle,
kernel_ptr kernel,
std::vector<mem_ptr> arguments,
- std::vector<size_t> global_dimensions,
- std::vector<size_t> global_offsets,
- std::vector<size_t> local_dimensions,
- std::function<any_tuple(T&)> map_result)
+ const std::vector<size_t>& global_dimensions,
+ const std::vector<size_t>& global_offsets,
+ const std::vector<size_t>& local_dimensions,
+ const std::function<any_tuple(T&)>& map_result)
: m_number_of_values(1)
, m_handle(handle)
, m_kernel(kernel)
@@ -83,9 +84,9 @@ class command_impl : public command {
, m_map_result(map_result)
{
m_kernel_event.adopt(cl_event());
- for (size_t s : m_global_dimensions) {
- m_number_of_values *= s;
- }
+ std::for_each(m_global_dimensions.begin(),
+ m_global_dimensions.end(),
+ [&](const size_t& s) { m_number_of_values *= s; });
}
void enqueue (command_queue_ptr queue) {
@@ -131,7 +132,7 @@ class command_impl : public command {
private:
- int m_number_of_values;
+ int m_number_of_values;
response_handle m_handle;
kernel_ptr m_kernel;
event_ptr m_kernel_event;
View
5 src/opencl/command_dispatcher.cpp
@@ -87,7 +87,10 @@ struct command_dispatcher::worker {
}
}
catch (exception& e) {
- CPPA_LOG_ERROR("worker loop, what(): " << e.what());
+ ostringstream oss;
+ oss << "worker loop, e.what(): '" << e.what() << "'.";
+ CPPA_LOG_ERROR(oss.str());
+ throw runtime_error(oss.str());
}
}
else {
View
75 unit_testing/test_opencl.cpp
@@ -39,7 +39,8 @@ namespace { constexpr const char* kernel_source = R"__(
int l_dim_1 = get_local_id(1);
int g_off_0 = get_global_offset(0);
int g_off_1 = get_global_offset(1);
- output[(g_dim_0-g_off_0)+(g_dim_1-g_off_1)*g_size] = g_dim_0 * 10.0f + g_dim_1 * 1.0f + l_dim_0 * 0.1f + l_dim_1* 0.01f;
+ output[(g_dim_0-g_off_0)+(g_dim_1-g_off_1)*g_size] =
+ g_dim_0 * 10.0f + g_dim_1 * 1.0f + l_dim_0 * 0.1f + l_dim_1* 0.01f;
}
)__"; }
@@ -54,7 +55,6 @@ int main() {
command_dispatcher* disp =
cppa::detail::singleton_manager::get_command_dispatcher();
-
auto matrix_global = disp->spawn<vector<float>,
vector<float>,
vector<float>>(prog, "matrix",
@@ -69,14 +69,15 @@ int main() {
send(matrix_global, m1, m2);
receive (
- on_arg_match >> [&] (const vector<float>& result) {
- cout << "results:" << endl;
- for (unsigned y{0}; y < size; ++y) {
- for (unsigned x{0}; x < size; ++x) {
- cout << fixed << setprecision(2) << setw(8) << result[x+y*size];
- }
- cout << endl;
- }
+ on_arg_match >> [&] (const vector<float>&) {
+ cout << "done!" << endl;
+// cout << "results:" << endl;
+// for (unsigned y{0}; y < size; ++y) {
+// for (unsigned x{0}; x < size; ++x) {
+// cout << fixed << setprecision(2) << setw(8) << result[x+y*size];
+// }
+// cout << endl;
+// }
},
others() >> [=]() {
cout << "Unexpected message: '"
@@ -84,33 +85,33 @@ int main() {
}
);
- cout << endl;
- auto matrix_local = disp->spawn<vector<float>,
- vector<float>>(prog, "dimensions",
- {size, size},
- {1,2},
- {(size/3), (size/2)});
-
- m1.clear();
- m1.push_back(0.0); // dummy
-
- send(matrix_local, m1);
-
- receive (
- on_arg_match >> [&] (const vector<float>& result) {
- cout << "dimenson example:" << endl;
- for (unsigned y{0}; y < size; ++y) {
- for (unsigned x{0}; x < size; ++x) {
- cout << fixed << setprecision(2) << setw(6) << result[x+y*size];
- }
- cout << endl;
- }
- },
- others() >> [=]() {
- cout << "Unexpected message: '"
- << to_string(self->last_dequeued()) << "'.\n";
- }
- );
+// cout << endl;
+// auto matrix_local = disp->spawn<vector<float>,
+// vector<float>>(prog, "dimensions",
+// {size, size},
+// {1,2},
+// {(size/3), (size/2)});
+
+// m1.clear();
+// m1.push_back(0.0); // dummy
+
+// send(matrix_local, m1);
+
+// receive (
+// on_arg_match >> [&] (const vector<float>& result) {
+// cout << "dimenson example: " << endl;
+// for (unsigned y{0}; y < size; ++y) {
+// for (unsigned x{0}; x < size; ++x) {
+// cout << fixed << setprecision(2) << setw(6) << result[x+y*size];
+// }
+// cout << endl;
+// }
+// },
+// others() >> [=]() {
+// cout << "Unexpected message: '"
+// << to_string(self->last_dequeued()) << "'.\n";
+// }
+// );
cppa::await_all_others_done();
cppa::shutdown();

1 comment on commit 96ad4c3

@Neverlord
Owner

Throwing an exception in enqueue_impl is not a good idea, since this will throw at the client actor that's using send. The exception should be thrown during construction of the actor, e.g., via a factory function.

Please sign in to comment.
Something went wrong with that request. Please try again.