[compute] 46/49: Add array support to BOOST_COMPUTE_ADAPT_STRUCT()

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Fri Dec 18 17:58:22 UTC 2015


This is an automated email from the git hooks/post-receive script.

ghisvail-guest pushed a commit to branch master
in repository compute.

commit 68155f7597fe704a951f1bee4f8bee4fc66b8189
Author: Kyle Lutz <kyle.r.lutz at gmail.com>
Date:   Sat Sep 19 18:54:51 2015 -0400

    Add array support to BOOST_COMPUTE_ADAPT_STRUCT()
    
    This adds support for C-style arrays (e.g. "int array[10]") to the
    BOOST_COMPUTE_ADAPT_STRUCT() macro.
    
    Thanks to Fabian Bösch for providing the code.
---
 include/boost/compute/types/struct.hpp |  9 ++++++++
 test/test_struct.cpp                   | 38 ++++++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+)

diff --git a/include/boost/compute/types/struct.hpp b/include/boost/compute/types/struct.hpp
index b910532..92aeaed 100644
--- a/include/boost/compute/types/struct.hpp
+++ b/include/boost/compute/types/struct.hpp
@@ -38,6 +38,15 @@ inline std::string adapt_struct_insert_member(T Struct::*, const char *name)
     return s.str();
 }
 
+
+template<class Struct, class T, int N>
+inline std::string adapt_struct_insert_member(T (Struct::*)[N], const char *name)
+{
+    std::stringstream s;
+    s << "    " << type_name<T>() << " " << name << "[" << N << "]" << ";\n";
+    return s.str();
+}
+
 } // end detail namespace
 } // end compute namespace
 } // end boost namespace
diff --git a/test/test_struct.cpp b/test/test_struct.cpp
index 4fe1ab3..d7a7b65 100644
--- a/test/test_struct.cpp
+++ b/test/test_struct.cpp
@@ -46,6 +46,13 @@ struct Atom
 // adapt the chemistry::Atom class
 BOOST_COMPUTE_ADAPT_STRUCT(chemistry::Atom, Atom, (x, y, z, number))
 
+struct StructWithArray {
+    int value;
+    int array[3];
+};
+
+BOOST_COMPUTE_ADAPT_STRUCT(StructWithArray, StructWithArray, (value, array))
+
 #include "check_macros.hpp"
 #include "context_setup.hpp"
 
@@ -124,4 +131,35 @@ BOOST_AUTO_TEST_CASE(custom_kernel)
     queue.enqueue_1d_range_kernel(custom_kernel, 0, atoms.size(), 1);
 }
 
+// Creates a StructWithArray containing 'x', 'y', 'z'.
+StructWithArray make_struct_with_array(int x, int y, int z)
+{
+    StructWithArray s;
+    s.value = 0;
+    s.array[0] = x;
+    s.array[1] = y;
+    s.array[2] = z;
+    return s;
+}
+
+BOOST_AUTO_TEST_CASE(struct_with_array)
+{
+    compute::vector<StructWithArray> structs(context);
+
+    structs.push_back(make_struct_with_array(1, 2, 3), queue);
+    structs.push_back(make_struct_with_array(4, 5, 6), queue);
+    structs.push_back(make_struct_with_array(7, 8, 9), queue);
+
+    BOOST_COMPUTE_FUNCTION(int, sum_array, (StructWithArray x),
+    {
+        return x.array[0] + x.array[1] + x.array[2];
+    });
+
+    compute::vector<int> results(structs.size(), context);
+    compute::transform(
+        structs.begin(), structs.end(), results.begin(), sum_array, queue
+    );
+    CHECK_RANGE_EQUAL(int, 3, results, (6, 15, 24));
+}
+
 BOOST_AUTO_TEST_SUITE_END()

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/compute.git



More information about the debian-science-commits mailing list