]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/compute/example/monte_carlo.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / compute / example / monte_carlo.cpp
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
13 #include <boost/compute/function.hpp>
14 #include <boost/compute/system.hpp>
15 #include <boost/compute/algorithm/count_if.hpp>
16 #include <boost/compute/container/vector.hpp>
17 #include <boost/compute/iterator/buffer_iterator.hpp>
18 #include <boost/compute/random/default_random_engine.hpp>
19 #include <boost/compute/types/fundamental.hpp>
20
21 namespace compute = boost::compute;
22
23 int main()
24 {
25 // get default device and setup context
26 compute::device gpu = compute::system::default_device();
27 compute::context context(gpu);
28 compute::command_queue queue(context, gpu);
29
30 std::cout << "device: " << gpu.name() << std::endl;
31
32 using compute::uint_;
33 using compute::uint2_;
34
35 // ten million random points
36 size_t n = 10000000;
37
38 // generate random numbers
39 compute::default_random_engine rng(queue);
40 compute::vector<uint_> vector(n * 2, context);
41 rng.generate(vector.begin(), vector.end(), queue);
42
43 // function returing true if the point is within the unit circle
44 BOOST_COMPUTE_FUNCTION(bool, is_in_unit_circle, (const uint2_ point),
45 {
46 const float x = point.x / (float) UINT_MAX - 1;
47 const float y = point.y / (float) UINT_MAX - 1;
48
49 return (x*x + y*y) < 1.0f;
50 });
51
52 // iterate over vector<uint> as vector<uint2>
53 compute::buffer_iterator<uint2_> start =
54 compute::make_buffer_iterator<uint2_>(vector.get_buffer(), 0);
55 compute::buffer_iterator<uint2_> end =
56 compute::make_buffer_iterator<uint2_>(vector.get_buffer(), vector.size() / 2);
57
58 // count number of random points within the unit circle
59 size_t count = compute::count_if(start, end, is_in_unit_circle, queue);
60
61 // print out values
62 float count_f = static_cast<float>(count);
63 std::cout << "count: " << count << " / " << n << std::endl;
64 std::cout << "ratio: " << count_f / float(n) << std::endl;
65 std::cout << "pi = " << (count_f / float(n)) * 4.0f << std::endl;
66
67 return 0;
68 }