1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
3 //
4 // Distributed under the Boost Software License, Version 1.0
5 // See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt
7 //
8 // See http://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
10 
11 #define BOOST_TEST_MODULE TestUserDefinedTypes
12 #include <boost/test/unit_test.hpp>
13 
14 #include <iostream>
15 
16 #include <boost/compute/function.hpp>
17 #include <boost/compute/system.hpp>
18 #include <boost/compute/algorithm/sort.hpp>
19 #include <boost/compute/container/vector.hpp>
20 #include <boost/compute/types/struct.hpp>
21 
22 namespace compute = boost::compute;
23 
24 // user-defined data type containing two int's and a float
25 struct UDD
26 {
27     int a;
28     int b;
29     float c;
30 };
31 
32 // make UDD available to OpenCL
33 BOOST_COMPUTE_ADAPT_STRUCT(UDD, UDD, (a, b, c))
34 
35 // comparison operator for UDD
operator ==(const UDD & lhs,const UDD & rhs)36 bool operator==(const UDD &lhs, const UDD &rhs)
37 {
38     return lhs.a == rhs.a && lhs.b == rhs.b && lhs.c == rhs.c;
39 }
40 
41 // output stream operator for UDD
operator <<(std::ostream & stream,const UDD & x)42 std::ostream& operator<<(std::ostream &stream, const UDD &x)
43 {
44     return stream << "(" << x.a << ", " << x.b << ", " << x.c << ")";
45 }
46 
47 // function to generate a random UDD on the host
rand_UDD()48 UDD rand_UDD()
49 {
50     UDD udd;
51     udd.a = rand() % 100;
52     udd.b = rand() % 100;
53     udd.c = (float)(rand() % 100) / 1.3f;
54 
55     return udd;
56 }
57 
58 // function to compare two UDD's on the host by their first component
compare_UDD_host(const UDD & lhs,const UDD & rhs)59 bool compare_UDD_host(const UDD &lhs, const UDD &rhs)
60 {
61     return lhs.a < rhs.a;
62 }
63 
64 // function to compate two UDD's on the device by their first component
65 BOOST_COMPUTE_FUNCTION(bool, compare_UDD_device, (UDD lhs, UDD rhs),
66 {
67     return lhs.a < rhs.a;
68 });
69 
70 #include "check_macros.hpp"
71 #include "context_setup.hpp"
72 
73 // see: issue #11 (https://github.com/boostorg/compute/issues/11)
BOOST_AUTO_TEST_CASE(issue_11)74 BOOST_AUTO_TEST_CASE(issue_11)
75 {
76     if(device.vendor() == "NVIDIA" && device.platform().name() == "Apple"){
77         // FIXME: this test currently segfaults on NVIDIA GPUs on Apple
78         std::cerr << "skipping issue test on NVIDIA GPU on Apple platform" << std::endl;
79         return;
80     }
81 
82     // create vector of random values on the host
83     std::vector<UDD> host_vector(10);
84     std::generate(host_vector.begin(), host_vector.end(), rand_UDD);
85 
86     // transfer the values to the device
87     compute::vector<UDD> device_vector(host_vector.size(), context);
88     compute::copy(
89         host_vector.begin(), host_vector.end(), device_vector.begin(), queue
90     );
91 
92     // sort values on the device
93     compute::sort(
94         device_vector.begin(),
95         device_vector.end(),
96         compare_UDD_device,
97         queue
98     );
99 
100     // sort values on the host
101     std::sort(
102         host_vector.begin(),
103         host_vector.end(),
104         compare_UDD_host
105     );
106 
107     // copy sorted device values back to the host
108     std::vector<UDD> tmp(10);
109     compute::copy(
110         device_vector.begin(),
111         device_vector.end(),
112         tmp.begin(),
113         queue
114     );
115 
116     // verify sorted values
117     for(size_t i = 0; i < host_vector.size(); i++){
118         BOOST_CHECK_EQUAL(tmp[i], host_vector[i]);
119     }
120 }
121 
122 BOOST_AUTO_TEST_SUITE_END()
123