Commit 96ad4c3d authored by Joseph Noir's avatar Joseph Noir

code optimizations

parent 6e2420a4
......@@ -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,
......
......@@ -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;
......
......@@ -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 {
......
......@@ -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();
......
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