forked from boostorg/compute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_buffer.cpp
213 lines (172 loc) · 5.58 KB
/
test_buffer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//---------------------------------------------------------------------------//
// Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
//
// 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.
//---------------------------------------------------------------------------//
#define BOOST_TEST_MODULE TestBuffer
#include <boost/test/unit_test.hpp>
#include <boost/compute/buffer.hpp>
#include <boost/compute/system.hpp>
#include <boost/bind.hpp>
#ifdef BOOST_COMPUTE_USE_CPP11
#include <mutex>
#include <future>
#endif // BOOST_COMPUTE_USE_CPP11
#include "quirks.hpp"
#include "context_setup.hpp"
namespace bc = boost::compute;
BOOST_AUTO_TEST_CASE(size)
{
bc::buffer buffer(context, 100);
BOOST_CHECK_EQUAL(buffer.size(), size_t(100));
BOOST_VERIFY(buffer.max_size() > buffer.size());
}
BOOST_AUTO_TEST_CASE(cl_context)
{
bc::buffer buffer(context, 100);
BOOST_VERIFY(buffer.get_context() == context);
}
BOOST_AUTO_TEST_CASE(equality_operator)
{
bc::buffer a(context, 10);
bc::buffer b(context, 10);
BOOST_VERIFY(a == a);
BOOST_VERIFY(b == b);
BOOST_VERIFY(!(a == b));
BOOST_VERIFY(a != b);
a = b;
BOOST_VERIFY(a == b);
BOOST_VERIFY(!(a != b));
}
BOOST_AUTO_TEST_CASE(construct_from_cl_mem)
{
// create cl_mem
cl_mem mem = clCreateBuffer(context, CL_MEM_READ_WRITE, 16, 0, 0);
BOOST_VERIFY(mem);
// create boost::compute::buffer
boost::compute::buffer buffer(mem);
// check buffer
BOOST_CHECK(buffer.get() == mem);
BOOST_CHECK(buffer.get_context() == context);
BOOST_CHECK_EQUAL(buffer.size(), size_t(16));
// cleanup cl_mem
clReleaseMemObject(mem);
}
BOOST_AUTO_TEST_CASE(reference_count)
{
using boost::compute::uint_;
boost::compute::buffer buf(context, 16);
BOOST_CHECK_GE(buf.reference_count(), uint_(1));
}
BOOST_AUTO_TEST_CASE(get_size)
{
boost::compute::buffer buf(context, 16);
BOOST_CHECK_EQUAL(buf.size(), size_t(16));
BOOST_CHECK_EQUAL(buf.get_info<CL_MEM_SIZE>(), size_t(16));
BOOST_CHECK_EQUAL(buf.get_info<size_t>(CL_MEM_SIZE), size_t(16));
}
#ifndef BOOST_COMPUTE_NO_RVALUE_REFERENCES
BOOST_AUTO_TEST_CASE(move_constructor)
{
boost::compute::buffer buffer1(context, 16);
BOOST_CHECK(buffer1.get() != 0);
BOOST_CHECK_EQUAL(buffer1.size(), size_t(16));
boost::compute::buffer buffer2(std::move(buffer1));
BOOST_CHECK(buffer1.get() == 0);
BOOST_CHECK(buffer2.get() != 0);
BOOST_CHECK_EQUAL(buffer2.size(), size_t(16));
}
#endif // BOOST_COMPUTE_NO_RVALUE_REFERENCES
BOOST_AUTO_TEST_CASE(clone_buffer)
{
boost::compute::buffer buffer1(context, 16);
boost::compute::buffer buffer2 = buffer1.clone(queue);
BOOST_CHECK(buffer1.get() != buffer2.get());
BOOST_CHECK_EQUAL(buffer1.size(), buffer2.size());
BOOST_CHECK(buffer1.get_memory_flags() == buffer2.get_memory_flags());
}
#ifdef BOOST_COMPUTE_CL_VERSION_1_1
#ifdef BOOST_COMPUTE_USE_CPP11
std::mutex callback_mutex;
std::condition_variable callback_condition_variable;
static void BOOST_COMPUTE_CL_CALLBACK
destructor_callback_function(cl_mem, void *user_data)
{
std::lock_guard<std::mutex> lock(callback_mutex);
bool *flag = static_cast<bool *>(user_data);
*flag = true;
callback_condition_variable.notify_one();
}
BOOST_AUTO_TEST_CASE(destructor_callback)
{
REQUIRES_OPENCL_VERSION(1,2);
if(!supports_destructor_callback(device))
{
return;
}
bool invoked = false;
{
boost::compute::buffer buf(context, 128);
buf.set_destructor_callback(destructor_callback_function, &invoked);
}
std::unique_lock<std::mutex> lock(callback_mutex);
callback_condition_variable.wait_for(
lock, std::chrono::seconds(1), [&](){ return invoked; }
);
BOOST_CHECK(invoked == true);
}
static void BOOST_COMPUTE_CL_CALLBACK
destructor_templated_callback_function(bool *flag)
{
std::lock_guard<std::mutex> lock(callback_mutex);
*flag = true;
callback_condition_variable.notify_one();
}
BOOST_AUTO_TEST_CASE(destructor_templated_callback)
{
REQUIRES_OPENCL_VERSION(1,2);
if(!supports_destructor_callback(device))
{
return;
}
bool invoked = false;
{
boost::compute::buffer buf(context, 128);
buf.set_destructor_callback(boost::bind(destructor_templated_callback_function, &invoked));
}
std::unique_lock<std::mutex> lock(callback_mutex);
callback_condition_variable.wait_for(
lock, std::chrono::seconds(1), [&](){ return invoked; }
);
BOOST_CHECK(invoked == true);
}
#endif // BOOST_COMPUTE_USE_CPP11
BOOST_AUTO_TEST_CASE(create_subbuffer)
{
REQUIRES_OPENCL_VERSION(1, 1);
size_t base_addr_align = device.get_info<CL_DEVICE_MEM_BASE_ADDR_ALIGN>() / 8;
size_t multiplier = 16;
size_t buffer_size = base_addr_align * multiplier;
size_t subbuffer_size = 64;
boost::compute::buffer buffer(context, buffer_size);
for(size_t i = 0; i < multiplier; ++i)
{
boost::compute::buffer subbuffer = buffer.create_subbuffer(
boost::compute::buffer::read_write, base_addr_align * i, subbuffer_size);
BOOST_CHECK(buffer.get() != subbuffer.get());
BOOST_CHECK_EQUAL(subbuffer.size(), subbuffer_size);
}
}
#endif // BOOST_COMPUTE_CL_VERSION_1_1
BOOST_AUTO_TEST_CASE(create_buffer_doctest)
{
//! [constructor]
boost::compute::buffer buf(context, 32 * sizeof(float));
//! [constructor]
BOOST_CHECK_EQUAL(buf.size(), 32 * sizeof(float));
}
BOOST_AUTO_TEST_SUITE_END()