From 2e9f5425c470a7d740f24095ddc4f283a95818f4 Mon Sep 17 00:00:00 2001 From: Jakub Szuppe Date: Tue, 23 Aug 2016 13:50:49 +0200 Subject: [PATCH] Refactor distributed::context Changes to distributed::context ctors. --- include/boost/compute/distributed/context.hpp | 70 ++++++++++++++++++- test/test_distributed_context.cpp | 58 ++++++--------- 2 files changed, 89 insertions(+), 39 deletions(-) diff --git a/include/boost/compute/distributed/context.hpp b/include/boost/compute/distributed/context.hpp index 70f203a36..9686d45de 100644 --- a/include/boost/compute/distributed/context.hpp +++ b/include/boost/compute/distributed/context.hpp @@ -29,13 +29,29 @@ class context { } - /// Creates a new distributed context for \p devices. - explicit context(const std::vector< ::boost::compute::device> &devices) + /// Creates a new distributed context for \p devices, containing + /// boost::compute::context objects, each constructed using corresponding + /// vector of OpenCL devices and \p properties. + context(const std::vector< std::vector< ::boost::compute::device> > &devices, + const std::vector &properties) { m_contexts = std::vector< ::boost::compute::context>(); for(size_t i = 0; i < devices.size(); i++) { m_contexts.push_back( - ::boost::compute::context(devices[i], 0) + ::boost::compute::context(devices[i], properties[i]) + ); + } + } + + /// Creates a new distributed context for \p devices, containing + /// boost::compute::context objects, each constructed using corresponding + /// vector of OpenCL devices and default properties. + explicit context(const std::vector< std::vector< ::boost::compute::device> > &devices) + { + m_contexts = std::vector< ::boost::compute::context>(); + for(size_t i = 0; i < devices.size(); i++) { + m_contexts.push_back( + ::boost::compute::context(devices[i]) ); } } @@ -52,6 +68,17 @@ class context } } + /// Creates a new distributed context for \p devices + explicit context(const std::vector< ::boost::compute::device> &devices) + { + m_contexts = std::vector< ::boost::compute::context>(); + for(size_t i = 0; i < devices.size(); i++) { + m_contexts.push_back( + ::boost::compute::context(devices[i]) + ); + } + } + /// Creates a new distributed context using \p contexts. explicit context(const std::vector< ::boost::compute::context>& contexts) : m_contexts(contexts) @@ -59,6 +86,22 @@ class context } + /// Creates a new distributed context using contexts from range + /// [\p first, \p last). + template + explicit context(Iterator first, Iterator last) + : m_contexts(first, last) + { + + } + + /// Creates a new distributed context from one \p context. + explicit context(const ::boost::compute::context& context) + : m_contexts(1, context) + { + + } + /// Creates a new context object as a copy of \p other. context(const context &other) : m_contexts(other.m_contexts) @@ -145,6 +188,27 @@ class context std::vector< ::boost::compute::context> m_contexts; }; + +inline bool operator==(const ::boost::compute::context &lhs, const context& rhs) +{ + return (rhs.size() == 1) && (rhs.get(0) == lhs); +} + +inline bool operator==(const context& lhs, const ::boost::compute::context &rhs) +{ + return (lhs.size() == 1) && (lhs.get(0) == rhs); +} + +inline bool operator!=(const ::boost::compute::context &lhs, const context& rhs) +{ + return !(lhs == rhs); +} + +inline bool operator!=(const context& lhs, const ::boost::compute::context &rhs) +{ + return !(lhs == rhs); +} + } // end distributed namespace } // end compute namespace } // end boost namespace diff --git a/test/test_distributed_context.cpp b/test/test_distributed_context.cpp index 1b2752ddb..792d5d361 100644 --- a/test/test_distributed_context.cpp +++ b/test/test_distributed_context.cpp @@ -24,20 +24,24 @@ namespace bc = boost::compute; BOOST_AUTO_TEST_CASE(construct_from_devices) { - std::vector all_devices; + std::vector > all_devices; const std::vector &platforms = bc::system::platforms(); for(size_t i = 0; i < platforms.size(); i++){ const bc::platform &platform = platforms[i]; + std::vector platform_devices = platform.devices(); + std::vector properties(platform_devices.size(), 0); // create a distributed context for devices in current platform - bc::distributed::context ctx(platform_devices); + bc::distributed::context ctx1(platform_devices); + bc::distributed::context ctx2(platform_devices, properties); // check context count - BOOST_CHECK_EQUAL(ctx.size(), platform.device_count()); + BOOST_CHECK_EQUAL(ctx1.size(), platform_devices.size()); + BOOST_CHECK_EQUAL(ctx2.size(), platform_devices.size()); - all_devices.insert(all_devices.end(), platform_devices.begin(), platform_devices.end()); + all_devices.push_back(platform_devices); } // create a distributed context for devices in current platform @@ -58,14 +62,25 @@ BOOST_AUTO_TEST_CASE(construct_from_contexts) bc::context ctx(platform.devices()); contexts.push_back(ctx); } - bc::distributed::context ctx(contexts); - BOOST_CHECK_EQUAL(ctx.size(), contexts.size()); + bc::distributed::context ctx1(contexts); + bc::distributed::context ctx2(contexts.begin(), contexts.end()); + + BOOST_CHECK_EQUAL(ctx1.size(), contexts.size()); + BOOST_CHECK_EQUAL(ctx2.size(), contexts.size()); for(size_t i = 0; i < contexts.size(); i++) { - BOOST_CHECK_EQUAL(ctx.get(i), contexts[i]); + BOOST_CHECK_EQUAL(ctx1.get(i), contexts[i]); + BOOST_CHECK_EQUAL(ctx2.get(i), contexts[i]); } } +BOOST_AUTO_TEST_CASE(construct_from_context) +{ + bc::distributed::context ctx(context); + BOOST_CHECK_EQUAL(ctx.size(), 1); + BOOST_CHECK_EQUAL(ctx.get(0), context); +} + BOOST_AUTO_TEST_CASE(copy_ctor) { std::vector contexts; @@ -137,33 +152,4 @@ BOOST_AUTO_TEST_CASE(get_context) } } -//BOOST_AUTO_TEST_CASE(test) -//{ -// bc::platform platform = Context::queue.get_context().get_device().platform(); -// // create a context for containing all devices in the platform -// bc::context ctx(platform.devices()); -// -// for(size_t i = 0; i < platform.devices().size(); i++) -// { -// std::cout << platform.devices()[i].name() << std::endl; -// } -// -// bc::vector vec(64, ctx); -// -// bc::command_queue q0(ctx, platform.devices()[0]); -// bc::command_queue q1(ctx, platform.devices()[1]); -// -// bc::fill(vec.begin(), vec.begin() + 32, bc::int_(4), q0); -// q0.finish(); -// bc::fill(vec.begin() + 32, vec.end(), bc::int_(3), q1); -// q1.finish(); -// -// bc::fill(vec.begin(), vec.end(), bc::int_(5), q1); -// q0.finish(); -// -//// for(size_t i = 0; i < vec.size(); i++) { -//// std::cout << vec[i] << std::endl; -//// } -//} - BOOST_AUTO_TEST_SUITE_END()