1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013-2014 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 TestMappedView
12 #include <boost/test/unit_test.hpp>
13
14 #include <boost/compute/system.hpp>
15 #include <boost/compute/algorithm/copy.hpp>
16 #include <boost/compute/algorithm/sort.hpp>
17 #include <boost/compute/algorithm/reduce.hpp>
18 #include <boost/compute/container/mapped_view.hpp>
19
20 #include "check_macros.hpp"
21 #include "context_setup.hpp"
22
23 namespace compute = boost::compute;
24
BOOST_AUTO_TEST_CASE(fill)25 BOOST_AUTO_TEST_CASE(fill)
26 {
27 int data[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
28 for(int i = 0; i < 8; i++){
29 BOOST_CHECK_EQUAL(data[i], i+1);
30 }
31
32 compute::mapped_view<int> view(data, 8, context);
33 compute::fill(view.begin(), view.end(), 4, queue);
34
35 view.map(CL_MAP_READ, queue);
36 for(int i = 0; i < 8; i++){
37 BOOST_CHECK_EQUAL(data[i], 4);
38 }
39 view.unmap(queue);
40 }
41
BOOST_AUTO_TEST_CASE(sort)42 BOOST_AUTO_TEST_CASE(sort)
43 {
44 int data[] = { 5, 2, 3, 1, 8, 7, 4, 9 };
45 compute::mapped_view<int> view(data, 8, context);
46
47 compute::sort(view.begin(), view.end(), queue);
48
49 view.map(CL_MAP_READ, queue);
50 BOOST_CHECK_EQUAL(data[0], 1);
51 BOOST_CHECK_EQUAL(data[7], 9);
52 view.unmap(queue);
53 }
54
BOOST_AUTO_TEST_CASE(mapped_view_reduce_doctest)55 BOOST_AUTO_TEST_CASE(mapped_view_reduce_doctest)
56 {
57 //! [reduce]
58 // create data array on the host
59 int data[] = { 5, 2, 3, 1, 8, 7, 4, 9 };
60 boost::compute::mapped_view<int> view(data, 8, context);
61
62 // use reduce() to calculate the sum on the device
63 int sum = 0;
64 boost::compute::reduce(view.begin(), view.end(), &sum, queue);
65 //! [reduce]
66
67 BOOST_CHECK_EQUAL(sum, 39);
68 }
69
70 BOOST_AUTO_TEST_SUITE_END()
71