From 19fcec84d8d7d21e796c7624e521b60d28ee21ed Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 20:45:59 +0200 Subject: Adding upstream version 16.2.11+ds. Signed-off-by: Daniel Baumann --- src/boost/libs/compute/example/monte_carlo.cpp | 73 ++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/boost/libs/compute/example/monte_carlo.cpp (limited to 'src/boost/libs/compute/example/monte_carlo.cpp') diff --git a/src/boost/libs/compute/example/monte_carlo.cpp b/src/boost/libs/compute/example/monte_carlo.cpp new file mode 100644 index 000000000..8ae26209d --- /dev/null +++ b/src/boost/libs/compute/example/monte_carlo.cpp @@ -0,0 +1,73 @@ +//---------------------------------------------------------------------------// +// Copyright (c) 2013 Kyle Lutz +// +// 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 + +#include +#include +#include +#include +#include +#include +#include + +namespace compute = boost::compute; + +int main() +{ + // get default device and setup context + compute::device gpu = compute::system::default_device(); + compute::context context(gpu); + compute::command_queue queue(context, gpu); + + std::cout << "device: " << gpu.name() << std::endl; + + using compute::uint_; + using compute::uint2_; + + +#ifdef CI_BUILD // lower number of points for CI builds + size_t n = 2000; +#else + // ten million random points + size_t n = 10000000; +#endif + + // generate random numbers + compute::default_random_engine rng(queue); + compute::vector vector(n * 2, context); + rng.generate(vector.begin(), vector.end(), queue); + + // function returing true if the point is within the unit circle + BOOST_COMPUTE_FUNCTION(bool, is_in_unit_circle, (const uint2_ point), + { + const float x = point.x / (float) UINT_MAX - 1; + const float y = point.y / (float) UINT_MAX - 1; + + return (x*x + y*y) < 1.0f; + }); + + // iterate over vector as vector + compute::buffer_iterator start = + compute::make_buffer_iterator(vector.get_buffer(), 0); + compute::buffer_iterator end = + compute::make_buffer_iterator(vector.get_buffer(), vector.size() / 2); + + // count number of random points within the unit circle + size_t count = compute::count_if(start, end, is_in_unit_circle, queue); + + // print out values + float count_f = static_cast(count); + std::cout << "count: " << count << " / " << n << std::endl; + std::cout << "ratio: " << count_f / float(n) << std::endl; + std::cout << "pi = " << (count_f / float(n)) * 4.0f << std::endl; + + return 0; +} -- cgit v1.2.3