• 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 //[point_centroid_example
12 
13 #include <iostream>
14 
15 #include <boost/compute/algorithm/copy.hpp>
16 #include <boost/compute/algorithm/accumulate.hpp>
17 #include <boost/compute/container/vector.hpp>
18 #include <boost/compute/types/fundamental.hpp>
19 
20 namespace compute = boost::compute;
21 
22 // the point centroid example calculates and displays the
23 // centroid of a set of 3D points stored as float4's
main()24 int main()
25 {
26     using compute::float4_;
27 
28     // get default device and setup context
29     compute::device device = compute::system::default_device();
30     compute::context context(device);
31     compute::command_queue queue(context, device);
32 
33     // point coordinates
34     float points[] = { 1.0f, 2.0f, 3.0f, 0.0f,
35                        -2.0f, -3.0f, 4.0f, 0.0f,
36                        1.0f, -2.0f, 2.5f, 0.0f,
37                        -7.0f, -3.0f, -2.0f, 0.0f,
38                        3.0f, 4.0f, -5.0f, 0.0f };
39 
40     // create vector for five points
41     compute::vector<float4_> vector(5, context);
42 
43     // copy point data to the device
44     compute::copy(
45         reinterpret_cast<float4_ *>(points),
46         reinterpret_cast<float4_ *>(points) + 5,
47         vector.begin(),
48         queue
49     );
50 
51     // calculate sum
52     float4_ sum = compute::accumulate(
53         vector.begin(), vector.end(), float4_(0, 0, 0, 0), queue
54     );
55 
56     // calculate centroid
57     float4_ centroid;
58     for(size_t i = 0; i < 3; i++){
59         centroid[i] = sum[i] / 5.0f;
60     }
61 
62     // print centroid
63     std::cout << "centroid: " << centroid << std::endl;
64 
65     return 0;
66 }
67 
68 //]
69