Skip to content

Commit

Permalink
Refactor distributed::context
Browse files Browse the repository at this point in the history
Changes to distributed::context ctors.
  • Loading branch information
jszuppe committed Aug 23, 2016
1 parent 745e88f commit 2e9f542
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 39 deletions.
70 changes: 67 additions & 3 deletions include/boost/compute/distributed/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<cl_context_properties*> &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])
);
}
}
Expand All @@ -52,13 +68,40 @@ 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)
{

}

/// Creates a new distributed context using contexts from range
/// [\p first, \p last).
template <class Iterator>
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)
Expand Down Expand Up @@ -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
Expand Down
58 changes: 22 additions & 36 deletions test/test_distributed_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,24 @@ namespace bc = boost::compute;

BOOST_AUTO_TEST_CASE(construct_from_devices)
{
std::vector<bc::device> all_devices;
std::vector<std::vector<bc::device> > all_devices;

const std::vector<bc::platform> &platforms = bc::system::platforms();
for(size_t i = 0; i < platforms.size(); i++){
const bc::platform &platform = platforms[i];

std::vector<bc::device> platform_devices = platform.devices();
std::vector<cl_context_properties*> 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
Expand All @@ -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<bc::context> contexts;
Expand Down Expand Up @@ -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<bc::int_> 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()

0 comments on commit 2e9f542

Please sign in to comment.