Skip to content

Commit 59adb75

Browse files
committed
Refactor distributed::context
Changes to distributed::context ctors.
1 parent 50b2457 commit 59adb75

File tree

2 files changed

+89
-39
lines changed

2 files changed

+89
-39
lines changed

include/boost/compute/distributed/context.hpp

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,29 @@ class context
2929
{
3030
}
3131

32-
/// Creates a new distributed context for \p devices.
33-
explicit context(const std::vector< ::boost::compute::device> &devices)
32+
/// Creates a new distributed context for \p devices, containing
33+
/// boost::compute::context objects, each constructed using corresponding
34+
/// vector of OpenCL devices and \p properties.
35+
context(const std::vector< std::vector< ::boost::compute::device> > &devices,
36+
const std::vector<cl_context_properties*> &properties)
3437
{
3538
m_contexts = std::vector< ::boost::compute::context>();
3639
for(size_t i = 0; i < devices.size(); i++) {
3740
m_contexts.push_back(
38-
::boost::compute::context(devices[i], 0)
41+
::boost::compute::context(devices[i], properties[i])
42+
);
43+
}
44+
}
45+
46+
/// Creates a new distributed context for \p devices, containing
47+
/// boost::compute::context objects, each constructed using corresponding
48+
/// vector of OpenCL devices and default properties.
49+
explicit context(const std::vector< std::vector< ::boost::compute::device> > &devices)
50+
{
51+
m_contexts = std::vector< ::boost::compute::context>();
52+
for(size_t i = 0; i < devices.size(); i++) {
53+
m_contexts.push_back(
54+
::boost::compute::context(devices[i])
3955
);
4056
}
4157
}
@@ -52,13 +68,40 @@ class context
5268
}
5369
}
5470

71+
/// Creates a new distributed context for \p devices
72+
explicit context(const std::vector< ::boost::compute::device> &devices)
73+
{
74+
m_contexts = std::vector< ::boost::compute::context>();
75+
for(size_t i = 0; i < devices.size(); i++) {
76+
m_contexts.push_back(
77+
::boost::compute::context(devices[i])
78+
);
79+
}
80+
}
81+
5582
/// Creates a new distributed context using \p contexts.
5683
explicit context(const std::vector< ::boost::compute::context>& contexts)
5784
: m_contexts(contexts)
5885
{
5986

6087
}
6188

89+
/// Creates a new distributed context using contexts from range
90+
/// [\p first, \p last).
91+
template <class Iterator>
92+
explicit context(Iterator first, Iterator last)
93+
: m_contexts(first, last)
94+
{
95+
96+
}
97+
98+
/// Creates a new distributed context from one \p context.
99+
explicit context(const ::boost::compute::context& context)
100+
: m_contexts(1, context)
101+
{
102+
103+
}
104+
62105
/// Creates a new context object as a copy of \p other.
63106
context(const context &other)
64107
: m_contexts(other.m_contexts)
@@ -145,6 +188,27 @@ class context
145188
std::vector< ::boost::compute::context> m_contexts;
146189
};
147190

191+
192+
inline bool operator==(const ::boost::compute::context &lhs, const context& rhs)
193+
{
194+
return (rhs.size() == 1) && (rhs.get(0) == lhs);
195+
}
196+
197+
inline bool operator==(const context& lhs, const ::boost::compute::context &rhs)
198+
{
199+
return (lhs.size() == 1) && (lhs.get(0) == rhs);
200+
}
201+
202+
inline bool operator!=(const ::boost::compute::context &lhs, const context& rhs)
203+
{
204+
return !(lhs == rhs);
205+
}
206+
207+
inline bool operator!=(const context& lhs, const ::boost::compute::context &rhs)
208+
{
209+
return !(lhs == rhs);
210+
}
211+
148212
} // end distributed namespace
149213
} // end compute namespace
150214
} // end boost namespace

test/test_distributed_context.cpp

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,24 @@ namespace bc = boost::compute;
2424

2525
BOOST_AUTO_TEST_CASE(construct_from_devices)
2626
{
27-
std::vector<bc::device> all_devices;
27+
std::vector<std::vector<bc::device> > all_devices;
2828

2929
const std::vector<bc::platform> &platforms = bc::system::platforms();
3030
for(size_t i = 0; i < platforms.size(); i++){
3131
const bc::platform &platform = platforms[i];
32+
3233
std::vector<bc::device> platform_devices = platform.devices();
34+
std::vector<cl_context_properties*> properties(platform_devices.size(), 0);
3335

3436
// create a distributed context for devices in current platform
35-
bc::distributed::context ctx(platform_devices);
37+
bc::distributed::context ctx1(platform_devices);
38+
bc::distributed::context ctx2(platform_devices, properties);
3639

3740
// check context count
38-
BOOST_CHECK_EQUAL(ctx.size(), platform.device_count());
41+
BOOST_CHECK_EQUAL(ctx1.size(), platform_devices.size());
42+
BOOST_CHECK_EQUAL(ctx2.size(), platform_devices.size());
3943

40-
all_devices.insert(all_devices.end(), platform_devices.begin(), platform_devices.end());
44+
all_devices.push_back(platform_devices);
4145
}
4246

4347
// create a distributed context for devices in current platform
@@ -58,14 +62,25 @@ BOOST_AUTO_TEST_CASE(construct_from_contexts)
5862
bc::context ctx(platform.devices());
5963
contexts.push_back(ctx);
6064
}
61-
bc::distributed::context ctx(contexts);
6265

63-
BOOST_CHECK_EQUAL(ctx.size(), contexts.size());
66+
bc::distributed::context ctx1(contexts);
67+
bc::distributed::context ctx2(contexts.begin(), contexts.end());
68+
69+
BOOST_CHECK_EQUAL(ctx1.size(), contexts.size());
70+
BOOST_CHECK_EQUAL(ctx2.size(), contexts.size());
6471
for(size_t i = 0; i < contexts.size(); i++) {
65-
BOOST_CHECK_EQUAL(ctx.get(i), contexts[i]);
72+
BOOST_CHECK_EQUAL(ctx1.get(i), contexts[i]);
73+
BOOST_CHECK_EQUAL(ctx2.get(i), contexts[i]);
6674
}
6775
}
6876

77+
BOOST_AUTO_TEST_CASE(construct_from_context)
78+
{
79+
bc::distributed::context ctx(context);
80+
BOOST_CHECK_EQUAL(ctx.size(), 1);
81+
BOOST_CHECK_EQUAL(ctx.get(0), context);
82+
}
83+
6984
BOOST_AUTO_TEST_CASE(copy_ctor)
7085
{
7186
std::vector<bc::context> contexts;
@@ -137,33 +152,4 @@ BOOST_AUTO_TEST_CASE(get_context)
137152
}
138153
}
139154

140-
//BOOST_AUTO_TEST_CASE(test)
141-
//{
142-
// bc::platform platform = Context::queue.get_context().get_device().platform();
143-
// // create a context for containing all devices in the platform
144-
// bc::context ctx(platform.devices());
145-
//
146-
// for(size_t i = 0; i < platform.devices().size(); i++)
147-
// {
148-
// std::cout << platform.devices()[i].name() << std::endl;
149-
// }
150-
//
151-
// bc::vector<bc::int_> vec(64, ctx);
152-
//
153-
// bc::command_queue q0(ctx, platform.devices()[0]);
154-
// bc::command_queue q1(ctx, platform.devices()[1]);
155-
//
156-
// bc::fill(vec.begin(), vec.begin() + 32, bc::int_(4), q0);
157-
// q0.finish();
158-
// bc::fill(vec.begin() + 32, vec.end(), bc::int_(3), q1);
159-
// q1.finish();
160-
//
161-
// bc::fill(vec.begin(), vec.end(), bc::int_(5), q1);
162-
// q0.finish();
163-
//
164-
//// for(size_t i = 0; i < vec.size(); i++) {
165-
//// std::cout << vec[i] << std::endl;
166-
//// }
167-
//}
168-
169155
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)