• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <iostream>
12 #include <iterator>
13 
14 #include <boost/compute/algorithm/max_element.hpp>
15 #include <boost/compute/container/vector.hpp>
16 #include <boost/compute/functional/geometry.hpp>
17 #include <boost/compute/iterator/transform_iterator.hpp>
18 #include <boost/compute/types/fundamental.hpp>
19 
20 namespace compute = boost::compute;
21 
22 // this example shows how to use the max_element() algorithm along with
23 // a transform_iterator and the length() function to find the longest
24 // 4-component vector in an array of vectors
main()25 int main()
26 {
27     using compute::float4_;
28 
29     // vectors data
30     float data[] = { 1.0f, 2.0f, 3.0f, 0.0f,
31                      4.0f, 5.0f, 6.0f, 0.0f,
32                      7.0f, 8.0f, 9.0f, 0.0f,
33                      0.0f, 0.0f, 0.0f, 0.0f };
34 
35     // create device vector with the vector data
36     compute::vector<float4_> vector(
37         reinterpret_cast<float4_ *>(data),
38         reinterpret_cast<float4_ *>(data) + 4
39     );
40 
41     // find the longest vector
42     compute::vector<float4_>::const_iterator iter =
43         compute::max_element(
44             compute::make_transform_iterator(
45                 vector.begin(), compute::length<float4_>()
46             ),
47             compute::make_transform_iterator(
48                 vector.end(), compute::length<float4_>()
49             )
50         ).base();
51 
52     // print the index of the longest vector
53     std::cout << "longest vector index: "
54               << std::distance(vector.begin(), iter)
55               << std::endl;
56 
57     return 0;
58 }
59