Permalink
Browse files

unit test changed to different calculations

validation missing
  • Loading branch information...
1 parent 9bcb308 commit 881858583bc8cd8f24f0473d055ba50fa12c19ec @josephnoir josephnoir committed Mar 22, 2013
Showing with 87 additions and 94 deletions.
  1. +87 −94 unit_testing/test_opencl.cpp
View
181 unit_testing/test_opencl.cpp
@@ -1,4 +1,5 @@
#include <vector>
+#include <iomanip>
#include "test.hpp"
@@ -13,6 +14,7 @@ using namespace cppa::opencl;
#define STRINGIFY(A) #A
std::string kernel_source = STRINGIFY(
+/*
float function_example(float a, float b) {
return a + b;
}
@@ -27,114 +29,105 @@ std::string kernel_source = STRINGIFY(
unsigned int i = get_global_id(0);
output[i] = input[i] * input[i];
}
+*/
+ __kernel void matrix(__global float* matrix1,
+ __global float* matrix2,
+ __global float* output) {
+ int size = get_global_size(0); // == get_global_size_(1);
+ int x = get_global_id(0);
+ int y = get_global_id(1);
+ int idx = 0;
+ float result = 0;
+ while (idx < size) {
+ float i = matrix1[idx+y*size];
+ float j = matrix2[x+idx*size];
+ float tmp = i*j;
+ result = result + tmp;
+ ++idx;
+ }
+ output[x+y*size] = result;
+ }
+
+ __kernel void dimensions(__global float* dummy, __global float* output) {
+ int size = get_global_size(0); // == get_global_size_(1);
+ int x = get_global_id(0);
+ int y = get_global_id(1);
+ int p = get_local_id(0);
+ int q = get_local_id(1);
+ output[x+y*size] = x * 10.0f + y * 1.0f + p * 0.1f + q* 0.01f;
+ }
);
int main() {
CPPA_TEST(test_opencl);
+ announce<vector<int>>();
announce<vector<float>>();
+ int size{6};
program prog{kernel_source};
command_dispatcher* disp =
cppa::detail::singleton_manager::get_command_dispatcher();
- {
-
- unsigned size{1024};
-
- vector<float> a;
- vector<float> b;
-
- a.reserve(size);
- b.reserve(size);
-
- for (unsigned i{0}; i < size; ++i) {
- a.push_back(i*1.0);
- b.push_back(i*1.0);
+ auto matrix_global = disp->spawn<vector<float>,
+ vector<float>,
+ vector<float>>(prog, "matrix",
+ size, size);
+
+ vector<float> m1(size * size);
+ vector<float> m2(size * size);
+
+ iota(m1.begin(), m1.end(), 0.0);
+ iota(m2.begin(), m2.end(), 0.0);
+
+ send(matrix_global, m1, m2);
+
+ receive (
+ on_arg_match >> [&] (const vector<float>& result) {
+ cout << "results:" << endl;
+ for (int y{0}; y < size; ++y) {
+ for (int x{0}; x < size; ++x) {
+ cout << fixed << setprecision(2) << setw(8) << 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,
+ (size/3), (size/2), 1);
+
+ 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 (int y{0}; y < size; ++y) {
+ for (int 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 << "add with 1 dimension size argument:" << endl;
- auto a_add_arguments = disp->spawn<vector<float>,
- vector<float>,
- vector<float>>(prog, "add", size);
- send(a_add_arguments, a, b);
- receive (
- on_arg_match >> [&](const vector<float>& result) {
-// copy(begin(result),
-// end(result),
-// std::ostream_iterator<float>(cout, "\n"));
- cout << "first ("
- << a.front() << "/" << b.front()
- << ") :" << result.front() << endl
- << "last ("
- << a.back() << "/" << b.back()
- << ") :" << result.back() << endl;
- },
- others() >> CPPA_UNEXPECTED_MSG_CB()
- );
-
- cout << "add without arguments:" << endl;
- auto a_add = disp->spawn<vector<float>,
- vector<float>,
- vector<float>>(prog, "add");
- send(a_add, a, b);
- receive (
- on_arg_match >> [&](const vector<float>& result) {
-// copy(begin(result),
-// end(result),
-// std::ostream_iterator<float>(cout, "\n"));
- cout << "first ("
- << a.front() << "/" << b.front()
- << ") :" << result.front() << endl
- << "last ("
- << a.back() << "/" << b.back()
- << ") :" << result.back() << endl;
- },
- others() >> CPPA_UNEXPECTED_MSG_CB()
- );
-
- cout << "square with 1 dimension size argument:" << endl;
- auto a_square = disp->spawn<vector<float>,
- vector<float>>(prog, "square", size);
- send(a_square, a);
- receive (
- on_arg_match >> [&](const vector<float>& result) {
-// copy(begin(result),
-// end(result),
-// std::ostream_iterator<float>(cout, "\n"));
- cout << "first ("
- << a.front() << "/" << b.front()
- << ") :" << result.front() << endl
- << "last ("
- << a.back() << "/" << b.back()
- << ") :" << result.back() << endl;
- },
- others() >> CPPA_UNEXPECTED_MSG_CB()
- );
-
- cout << "square without arguments:" << endl;
- auto a_square_arguments = disp->spawn<vector<float>,
- vector<float>>(prog, "square");
- send(a_square_arguments, a);
- receive (
- on_arg_match >> [&](const vector<float>& result) {
-// copy(begin(result),
-// end(result),
-// std::ostream_iterator<float>(cout, "\n"));
- cout << "first ("
- << a.front() << "/" << b.front()
- << ") :" << result.front() << endl
- << "last ("
- << a.back() << "/" << b.back()
- << ") :" << result.back() << endl;
- },
- others() >> CPPA_UNEXPECTED_MSG_CB()
- );
-
- cppa::await_all_others_done();
- cppa::shutdown();
- }
+ cppa::await_all_others_done();
+ cppa::shutdown();
return CPPA_TEST_RESULT();
}

0 comments on commit 8818585

Please sign in to comment.