MCPcopy Create free account
hub / github.com/boostorg/compute / main

Function main

example/monte_carlo.cpp:23–68  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

21namespace compute = boost::compute;
22
23int 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}

Callers

nothing calls this directly

Calls 6

count_ifFunction · 0.85
nameMethod · 0.45
generateMethod · 0.45
beginMethod · 0.45
endMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected