summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/compute/example/longest_vector.cpp
blob: faada33282449daac4a75f193e7426722f7c0e7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//---------------------------------------------------------------------------//
// Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
//
// Distributed under the Boost Software License, Version 1.0
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
//
// See http://boostorg.github.com/compute for more information.
//---------------------------------------------------------------------------//

#include <iostream>
#include <iterator>

#include <boost/compute/algorithm/max_element.hpp>
#include <boost/compute/container/vector.hpp>
#include <boost/compute/functional/geometry.hpp>
#include <boost/compute/iterator/transform_iterator.hpp>
#include <boost/compute/types/fundamental.hpp>

namespace compute = boost::compute;

// this example shows how to use the max_element() algorithm along with
// a transform_iterator and the length() function to find the longest
// 4-component vector in an array of vectors
int main()
{
    using compute::float4_;

    // vectors data
    float data[] = { 1.0f, 2.0f, 3.0f, 0.0f,
                     4.0f, 5.0f, 6.0f, 0.0f,
                     7.0f, 8.0f, 9.0f, 0.0f,
                     0.0f, 0.0f, 0.0f, 0.0f };

    // create device vector with the vector data
    compute::vector<float4_> vector(
        reinterpret_cast<float4_ *>(data),
        reinterpret_cast<float4_ *>(data) + 4
    );

    // find the longest vector
    compute::vector<float4_>::const_iterator iter =
        compute::max_element(
            compute::make_transform_iterator(
                vector.begin(), compute::length<float4_>()
            ),
            compute::make_transform_iterator(
                vector.end(), compute::length<float4_>()
            )
        ).base();

    // print the index of the longest vector
    std::cout << "longest vector index: "
              << std::distance(vector.begin(), iter)
              << std::endl;

    return 0;
}