Newer
Older
PYOPENCL_CALL_GUARDED(clCreateSubDevices,
std::vector<cl_device_id> result;
result.resize(num_entries);
PYOPENCL_CALL_GUARDED(clCreateSubDevices,
(m_device, props_ptr, num_entries, &result.front(), nullptr));
for (cl_device_id did: result)
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
py_result.append(handle_from_new_ptr(
new pyopencl::device(did, /*retain*/true,
device::REF_CL_1_2)));
return py_result;
}
#endif
};
inline py::list platform::get_devices(cl_device_type devtype)
{
cl_uint num_devices = 0;
PYOPENCL_PRINT_CALL_TRACE("clGetDeviceIDs");
{
cl_int status_code;
status_code = clGetDeviceIDs(m_platform, devtype, 0, 0, &num_devices);
if (status_code == CL_DEVICE_NOT_FOUND)
num_devices = 0;
else if (status_code != CL_SUCCESS) \
throw pyopencl::error("clGetDeviceIDs", status_code);
}
if (num_devices == 0)
return py::list();
std::vector<cl_device_id> devices(num_devices);
PYOPENCL_CALL_GUARDED(clGetDeviceIDs,
(m_platform, devtype,
num_devices, devices.empty( ) ? nullptr : &devices.front(), &num_devices));
for (cl_device_id did: devices)
result.append(handle_from_new_ptr(
new device(did)));
return result;
}
// }}}
class context : public noncopyable
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
{
private:
cl_context m_context;
public:
context(cl_context ctx, bool retain)
: m_context(ctx)
{
if (retain)
PYOPENCL_CALL_GUARDED(clRetainContext, (ctx));
}
~context()
{
PYOPENCL_CALL_GUARDED_CLEANUP(clReleaseContext,
(m_context));
}
cl_context data() const
{
return m_context;
}
PYOPENCL_EQUALITY_TESTS(context);
py::object get_info(cl_context_info param_name) const
{
switch (param_name)
{
case CL_CONTEXT_REFERENCE_COUNT:
Context, m_context, param_name, cl_uint);
case CL_CONTEXT_DEVICES:
{
std::vector<cl_device_id> result;
PYOPENCL_GET_VEC_INFO(Context, m_context, param_name, result);
py::list py_result;
for (cl_device_id did: result)
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
py_result.append(handle_from_new_ptr(
new pyopencl::device(did)));
return py_result;
}
case CL_CONTEXT_PROPERTIES:
{
std::vector<cl_context_properties> result;
PYOPENCL_GET_VEC_INFO(Context, m_context, param_name, result);
py::list py_result;
for (size_t i = 0; i < result.size(); i+=2)
{
cl_context_properties key = result[i];
py::object value;
switch (key)
{
case CL_CONTEXT_PLATFORM:
{
value = py::object(
handle_from_new_ptr(new platform(
reinterpret_cast<cl_platform_id>(result[i+1]))));
break;
}
#if defined(PYOPENCL_GL_SHARING_VERSION) && (PYOPENCL_GL_SHARING_VERSION >= 1)
#if defined(__APPLE__) && defined(HAVE_GL)
case CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE:
#else
case CL_GL_CONTEXT_KHR:
case CL_EGL_DISPLAY_KHR:
case CL_GLX_DISPLAY_KHR:
case CL_WGL_HDC_KHR:
case CL_CGL_SHAREGROUP_KHR:
#endif
value = py::cast(result[i+1]);
break;
#endif
case 0:
break;
default:
throw error("Context.get_info", CL_INVALID_VALUE,
"unknown context_property key encountered");
}
py_result.append(py::make_tuple(result[i], value));
}
return py_result;
}
#if PYOPENCL_CL_VERSION >= 0x1010
case CL_CONTEXT_NUM_DEVICES:
Context, m_context, param_name, cl_uint);
#endif
default:
throw error("Context.get_info", CL_INVALID_VALUE);
}
}
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
// not exposed to python
int get_hex_platform_version() const
{
std::vector<cl_device_id> devices;
PYOPENCL_GET_VEC_INFO(Context, m_context, CL_CONTEXT_DEVICES, devices);
if (devices.size() == 0)
throw error("Context._get_hex_version", CL_INVALID_VALUE,
"platform has no devices");
cl_platform_id plat;
PYOPENCL_CALL_GUARDED(clGetDeviceInfo,
(devices[0], CL_DEVICE_PLATFORM, sizeof(plat), &plat, nullptr));
std::string plat_version;
{
size_t param_value_size;
PYOPENCL_CALL_GUARDED(clGetPlatformInfo,
(plat, CL_PLATFORM_VERSION, 0, 0, ¶m_value_size));
std::vector<char> param_value(param_value_size);
PYOPENCL_CALL_GUARDED(clGetPlatformInfo,
(plat, CL_PLATFORM_VERSION, param_value_size,
param_value.empty( ) ? nullptr : ¶m_value.front(), ¶m_value_size));
plat_version =
param_value.empty( ) ? "" : std::string(¶m_value.front(), param_value_size-1);
}
int major_ver, minor_ver;
errno = 0;
int match_count = sscanf(plat_version.c_str(), "OpenCL %d.%d ", &major_ver, &minor_ver);
if (errno || match_count != 2)
throw error("Context._get_hex_version", CL_INVALID_VALUE,
"Platform version string did not have expected format");
return major_ver << 12 | minor_ver << 4;
}
};
inline
std::vector<cl_context_properties> parse_context_properties(
py::object py_properties)
{
std::vector<cl_context_properties> props;
if (py_properties.ptr() != Py_None)
{
for (py::handle prop_tuple_py: py_properties)
py::tuple prop_tuple(prop_tuple_py.cast<py::tuple>());
if (len(prop_tuple) != 2)
throw error("Context", CL_INVALID_VALUE, "property tuple must have length 2");
cl_context_properties prop = prop_tuple[0].cast<cl_context_properties>();
props.push_back(prop);
if (prop == CL_CONTEXT_PLATFORM)
{
props.push_back(
reinterpret_cast<cl_context_properties>(
prop_tuple[1].cast<const platform &>().data()));
}
#if defined(PYOPENCL_GL_SHARING_VERSION) && (PYOPENCL_GL_SHARING_VERSION >= 1)
#if defined(_WIN32)
else if (prop == CL_WGL_HDC_KHR)
{
// size_t is a stand-in for HANDLE, hopefully has the same size.
size_t hnd = (prop_tuple[1]).cast<size_t>();
props.push_back(hnd);
}
#endif
else if (
#if defined(__APPLE__) && defined(HAVE_GL)
prop == CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE
#else
prop == CL_GL_CONTEXT_KHR
|| prop == CL_EGL_DISPLAY_KHR
|| prop == CL_GLX_DISPLAY_KHR
|| prop == CL_CGL_SHAREGROUP_KHR
#endif
)
{
py::object ctypes = py::module::import("ctypes");
py::object prop = prop_tuple[1], c_void_p = ctypes.attr("c_void_p");
py::object ptr = ctypes.attr("cast")(prop, c_void_p);
props.push_back(ptr.attr("value").cast<cl_context_properties>());
}
#endif
else
throw error("Context", CL_INVALID_VALUE, "invalid context property");
}
props.push_back(0);
}
return props;
}
inline
context *create_context_inner(py::object py_devices, py::object py_properties,
py::object py_dev_type)
{
std::vector<cl_context_properties> props
= parse_context_properties(py_properties);
cl_context_properties *props_ptr
cl_int status_code;
cl_context ctx;
// from device list
if (py_devices.ptr() != Py_None)
{
if (py_dev_type.ptr() != Py_None)
throw error("Context", CL_INVALID_VALUE,
"one of 'devices' or 'dev_type' must be None");
std::vector<cl_device_id> devices;
devices.push_back(py_dev.cast<const device &>().data());
PYOPENCL_PRINT_CALL_TRACE("clCreateContext");
ctx = clCreateContext(
props_ptr,
devices.size(),
0, 0, &status_code);
}
// from dev_type
else
{
cl_device_type dev_type = CL_DEVICE_TYPE_DEFAULT;
if (py_dev_type.ptr() != Py_None)
dev_type = py_dev_type.cast<cl_device_type>();
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
PYOPENCL_PRINT_CALL_TRACE("clCreateContextFromType");
ctx = clCreateContextFromType(props_ptr, dev_type, 0, 0, &status_code);
}
if (status_code != CL_SUCCESS)
throw pyopencl::error("Context", status_code);
try
{
return new context(ctx, false);
}
catch (...)
{
PYOPENCL_CALL_GUARDED(clReleaseContext, (ctx));
throw;
}
}
inline
context *create_context(py::object py_devices, py::object py_properties,
py::object py_dev_type)
{
PYOPENCL_RETRY_RETURN_IF_MEM_ERROR(
return create_context_inner(py_devices, py_properties, py_dev_type);
)
}
// }}}
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
class command_queue
{
private:
cl_command_queue m_queue;
public:
command_queue(cl_command_queue q, bool retain)
: m_queue(q)
{
if (retain)
PYOPENCL_CALL_GUARDED(clRetainCommandQueue, (q));
}
command_queue(command_queue const &src)
: m_queue(src.m_queue)
{
PYOPENCL_CALL_GUARDED(clRetainCommandQueue, (m_queue));
}
command_queue(
const context &ctx,
const device *py_dev=nullptr,
py::object py_props=py::none())
{
cl_device_id dev;
if (py_dev)
dev = py_dev->data();
else
{
std::vector<cl_device_id> devs;
PYOPENCL_GET_VEC_INFO(Context, ctx.data(), CL_CONTEXT_DEVICES, devs);
if (devs.size() == 0)
throw pyopencl::error("CommandQueue", CL_INVALID_VALUE,
"context doesn't have any devices? -- don't know which one to default to");
dev = devs[0];
}
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
int hex_plat_version = ctx.get_hex_platform_version();
bool props_given_as_numeric;
cl_command_queue_properties num_props;
if (py_props.is_none())
{
num_props = 0;
props_given_as_numeric = true;
}
else
{
try
{
num_props = py::cast<cl_command_queue_properties>(py_props);
props_given_as_numeric = true;
}
catch (py::cast_error &)
{
props_given_as_numeric = false;
}
}
if (props_given_as_numeric)
{
#if PYOPENCL_CL_VERSION >= 0x2000
if (hex_plat_version >= 0x2000)
{
cl_queue_properties props_list[] = { CL_QUEUE_PROPERTIES, num_props, 0 };
cl_int status_code;
PYOPENCL_PRINT_CALL_TRACE("clCreateCommandQueueWithProperties");
m_queue = clCreateCommandQueueWithProperties(
ctx.data(), dev, props_list, &status_code);
if (status_code != CL_SUCCESS)
throw pyopencl::error("CommandQueue", status_code);
}
else
#endif
{
cl_int status_code;
PYOPENCL_PRINT_CALL_TRACE("clCreateCommandQueue");
#if defined(__GNUG__) && !defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
m_queue = clCreateCommandQueue(
ctx.data(), dev, num_props, &status_code);
#if defined(__GNUG__) && !defined(__clang__)
#pragma GCC diagnostic pop
#endif
if (status_code != CL_SUCCESS)
throw pyopencl::error("CommandQueue", status_code);
}
}
else
{
#if PYOPENCL_CL_VERSION < 0x2000
throw error("CommandQueue", CL_INVALID_VALUE,
"queue properties given as an iterable, "
"which is only allowed when PyOpenCL was built "
"against an OpenCL 2+ header");
if (hex_plat_version < 0x2000)
{
std::cerr <<
"queue properties given as an iterable, "
"which uses an OpenCL 2+-only interface, "
"but the context's platform does not "
"declare OpenCL 2 support. Proceeding "
"as requested, but the next thing you see "
"may be a crash." << std:: endl;
}
Andreas Klöckner
committed
PYOPENCL_STACK_CONTAINER(cl_queue_properties, props, py::len(py_props) + 1);
{
size_t i = 0;
for (auto prop: py_props)
props[i++] = py::cast<cl_queue_properties>(prop);
props[i++] = 0;
}
cl_int status_code;
PYOPENCL_PRINT_CALL_TRACE("clCreateCommandQueueWithProperties");
m_queue = clCreateCommandQueueWithProperties(
Andreas Klöckner
committed
ctx.data(), dev, PYOPENCL_STACK_CONTAINER_GET_PTR(props), &status_code);
if (status_code != CL_SUCCESS)
throw pyopencl::error("CommandQueue", status_code);
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
}
~command_queue()
{
PYOPENCL_CALL_GUARDED_CLEANUP(clReleaseCommandQueue,
(m_queue));
}
const cl_command_queue data() const
{ return m_queue; }
PYOPENCL_EQUALITY_TESTS(command_queue);
py::object get_info(cl_command_queue_info param_name) const
{
switch (param_name)
{
case CL_QUEUE_CONTEXT:
PYOPENCL_GET_OPAQUE_INFO(CommandQueue, m_queue, param_name,
cl_context, context);
case CL_QUEUE_DEVICE:
PYOPENCL_GET_OPAQUE_INFO(CommandQueue, m_queue, param_name,
cl_device_id, device);
case CL_QUEUE_REFERENCE_COUNT:
PYOPENCL_GET_TYPED_INFO(CommandQueue, m_queue, param_name,
cl_uint);
case CL_QUEUE_PROPERTIES:
PYOPENCL_GET_TYPED_INFO(CommandQueue, m_queue, param_name,
cl_command_queue_properties);
default:
throw error("CommandQueue.get_info", CL_INVALID_VALUE);
}
}
std::unique_ptr<context> get_context() const
{
cl_context param_value;
PYOPENCL_CALL_GUARDED(clGetCommandQueueInfo,
(m_queue, CL_QUEUE_CONTEXT, sizeof(param_value), ¶m_value, 0));
return std::unique_ptr<context>(
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
new context(param_value, /*retain*/ true));
}
#if PYOPENCL_CL_VERSION < 0x1010
cl_command_queue_properties set_property(
cl_command_queue_properties prop,
bool enable)
{
cl_command_queue_properties old_prop;
PYOPENCL_CALL_GUARDED(clSetCommandQueueProperty,
(m_queue, prop, PYOPENCL_CAST_BOOL(enable), &old_prop));
return old_prop;
}
#endif
void flush()
{ PYOPENCL_CALL_GUARDED(clFlush, (m_queue)); }
void finish()
{ PYOPENCL_CALL_GUARDED_THREADED(clFinish, (m_queue)); }
};
// }}}
class event : noncopyable
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
{
private:
cl_event m_event;
public:
event(cl_event event, bool retain)
: m_event(event)
{
if (retain)
PYOPENCL_CALL_GUARDED(clRetainEvent, (event));
}
event(event const &src)
: m_event(src.m_event)
{ PYOPENCL_CALL_GUARDED(clRetainEvent, (m_event)); }
virtual ~event()
{
PYOPENCL_CALL_GUARDED_CLEANUP(clReleaseEvent,
(m_event));
}
const cl_event data() const
{ return m_event; }
PYOPENCL_EQUALITY_TESTS(event);
py::object get_info(cl_event_info param_name) const
{
switch (param_name)
{
case CL_EVENT_COMMAND_QUEUE:
PYOPENCL_GET_OPAQUE_INFO(Event, m_event, param_name,
cl_command_queue, command_queue);
case CL_EVENT_COMMAND_TYPE:
PYOPENCL_GET_TYPED_INFO(Event, m_event, param_name,
cl_command_type);
case CL_EVENT_COMMAND_EXECUTION_STATUS:
PYOPENCL_GET_TYPED_INFO(Event, m_event, param_name,
cl_int);
case CL_EVENT_REFERENCE_COUNT:
PYOPENCL_GET_TYPED_INFO(Event, m_event, param_name,
cl_uint);
#if PYOPENCL_CL_VERSION >= 0x1010
case CL_EVENT_CONTEXT:
PYOPENCL_GET_OPAQUE_INFO(Event, m_event, param_name,
cl_context, context);
#endif
default:
throw error("Event.get_info", CL_INVALID_VALUE);
}
}
py::object get_profiling_info(cl_profiling_info param_name) const
{
switch (param_name)
{
case CL_PROFILING_COMMAND_QUEUED:
case CL_PROFILING_COMMAND_SUBMIT:
case CL_PROFILING_COMMAND_START:
case CL_PROFILING_COMMAND_END:
#if PYOPENCL_CL_VERSION >= 0x2000
case CL_PROFILING_COMMAND_COMPLETE:
#endif
PYOPENCL_GET_TYPED_INFO(EventProfiling, m_event, param_name,
cl_ulong);
default:
throw error("Event.get_profiling_info", CL_INVALID_VALUE);
}
}
virtual void wait()
{
PYOPENCL_CALL_GUARDED_THREADED(clWaitForEvents, (1, &m_event));
}
// Called from a destructor context below:
// - Should not release the GIL
// - Should fail gracefully in the face of errors
virtual void wait_during_cleanup_without_releasing_the_gil()
{
PYOPENCL_CALL_GUARDED_CLEANUP(clWaitForEvents, (1, &m_event));
}
#if PYOPENCL_CL_VERSION >= 0x1010
// {{{ set_callback, by way of a a thread-based construction
private:
struct event_callback_info_t
{
std::mutex m_mutex;
std::condition_variable m_condvar;
py::object m_py_event;
py::object m_py_callback;
bool m_set_callback_suceeded;
Andreas Klöckner
committed
bool m_notify_thread_wakeup_is_genuine;
cl_event m_event;
cl_int m_command_exec_status;
event_callback_info_t(py::object py_event, py::object py_callback)
Andreas Klöckner
committed
: m_py_event(py_event), m_py_callback(py_callback), m_set_callback_suceeded(true),
m_notify_thread_wakeup_is_genuine(false)
static void CL_CALLBACK evt_callback(cl_event evt, cl_int command_exec_status, void *user_data)
{
event_callback_info_t *cb_info = reinterpret_cast<event_callback_info_t *>(user_data);
{
std::lock_guard<std::mutex> lg(cb_info->m_mutex);
cb_info->m_event = evt;
cb_info->m_command_exec_status = command_exec_status;
Andreas Klöckner
committed
cb_info->m_notify_thread_wakeup_is_genuine = true;
Andreas Klöckner
committed
cb_info->m_condvar.notify_one();
}
public:
void set_callback(cl_int command_exec_callback_type, py::object pfn_event_notify)
{
// The reason for doing this via a thread is that we're able to wait on
// acquiring the GIL. (which we can't in the callback)
std::unique_ptr<event_callback_info_t> cb_info_holder(
new event_callback_info_t(
handle_from_new_ptr(new event(*this)),
pfn_event_notify));
event_callback_info_t *cb_info = cb_info_holder.get();
std::thread notif_thread([cb_info]()
{
{
std::unique_lock<std::mutex> ulk(cb_info->m_mutex);
Andreas Klöckner
committed
cb_info->m_condvar.wait(
ulk,
[&](){ return cb_info->m_notify_thread_wakeup_is_genuine; });
// ulk no longer held here, cb_info ready for deletion
}
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
{
py::gil_scoped_acquire acquire;
if (cb_info->m_set_callback_suceeded)
{
try {
cb_info->m_py_callback(
// cb_info->m_py_event,
cb_info->m_command_exec_status);
}
catch (std::exception &exc)
{
std::cerr
<< "[pyopencl] event callback handler threw an exception, ignoring: "
<< exc.what()
<< std::endl;
}
}
// Need to hold GIL to delete py::object instances in
// event_callback_info_t
delete cb_info;
}
});
// Thread is away--it is now its responsibility to free cb_info.
cb_info_holder.release();
// notif_thread should no longer be coupled to the lifetime of the thread.
notif_thread.detach();
try
{
PYOPENCL_CALL_GUARDED(clSetEventCallback, (
data(), command_exec_callback_type, &event::evt_callback, cb_info));
}
catch (...) {
// Setting the callback did not succeed. The thread would never
// be woken up. Wake it up to let it know that it can stop.
{
std::lock_guard<std::mutex> lg(cb_info->m_mutex);
cb_info->m_set_callback_suceeded = false;
Andreas Klöckner
committed
cb_info->m_notify_thread_wakeup_is_genuine = true;
}
cb_info->m_condvar.notify_one();
throw;
}
}
// }}}
#endif
};
class nanny_event : public event
{
// In addition to everything an event does, the nanny event holds a reference
// to a Python object and waits for its own completion upon destruction.
protected:
std::unique_ptr<py_buffer_wrapper> m_ward;
nanny_event(cl_event evt, bool retain, std::unique_ptr<py_buffer_wrapper> &ward)
: event(evt, retain), m_ward(std::move(ward))
{
// It appears that Pybind can get very confused if we release the GIL here:
// https://github.com/inducer/pyopencl/issues/296
wait_during_cleanup_without_releasing_the_gil();
}
py::object get_ward() const
{
if (m_ward.get())
{
return py::reinterpret_borrow<py::object>(m_ward->m_buf.obj);
}
virtual void wait()
{
event::wait();
m_ward.reset();
}
virtual void wait_during_cleanup_without_releasing_the_gil()
{
event::wait_during_cleanup_without_releasing_the_gil();
m_ward.reset();
}
};
inline
void wait_for_events(py::object events)
{
cl_uint num_events_in_wait_list = 0;
std::vector<cl_event> event_wait_list(len(events));
event_wait_list[num_events_in_wait_list++] =
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
PYOPENCL_CALL_GUARDED_THREADED(clWaitForEvents, (
PYOPENCL_WAITLIST_ARGS));
}
#if PYOPENCL_CL_VERSION >= 0x1020
inline
event *enqueue_marker_with_wait_list(command_queue &cq,
py::object py_wait_for)
{
PYOPENCL_PARSE_WAIT_FOR;
cl_event evt;
PYOPENCL_CALL_GUARDED(clEnqueueMarkerWithWaitList, (
cq.data(), PYOPENCL_WAITLIST_ARGS, &evt));
PYOPENCL_RETURN_NEW_EVENT(evt);
}
inline
event *enqueue_barrier_with_wait_list(command_queue &cq,
py::object py_wait_for)
{
PYOPENCL_PARSE_WAIT_FOR;
cl_event evt;
PYOPENCL_CALL_GUARDED(clEnqueueBarrierWithWaitList,
(cq.data(), PYOPENCL_WAITLIST_ARGS, &evt));
PYOPENCL_RETURN_NEW_EVENT(evt);
}
#endif
// {{{ used internally for pre-OpenCL-1.2 contexts
inline
event *enqueue_marker(command_queue &cq)
{
cl_event evt;
PYOPENCL_CALL_GUARDED(clEnqueueMarker, (
cq.data(), &evt));
PYOPENCL_RETURN_NEW_EVENT(evt);
}
inline
void enqueue_wait_for_events(command_queue &cq, py::object py_events)
{
cl_uint num_events = 0;
std::vector<cl_event> event_list(len(py_events));
for (py::handle py_evt: py_events)
event_list[num_events++] = py_evt.cast<event &>().data();
PYOPENCL_CALL_GUARDED(clEnqueueWaitForEvents, (
cq.data(), num_events, event_list.empty( ) ? nullptr : &event_list.front()));
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
}
inline
void enqueue_barrier(command_queue &cq)
{
PYOPENCL_CALL_GUARDED(clEnqueueBarrier, (cq.data()));
}
// }}}
#if PYOPENCL_CL_VERSION >= 0x1010
class user_event : public event
{
public:
user_event(cl_event evt, bool retain)
: event(evt, retain)
{ }
void set_status(cl_int execution_status)
{
PYOPENCL_CALL_GUARDED(clSetUserEventStatus, (data(), execution_status));
}
};
inline
user_event *create_user_event(context &ctx)
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
{
cl_int status_code;
PYOPENCL_PRINT_CALL_TRACE("clCreateUserEvent");
cl_event evt = clCreateUserEvent(ctx.data(), &status_code);
if (status_code != CL_SUCCESS)
throw pyopencl::error("UserEvent", status_code);
try
{
return new user_event(evt, false);
}
catch (...)
{
clReleaseEvent(evt);
throw;
}
}
#endif
// }}}
py::object create_mem_object_wrapper(cl_mem mem, bool retain);
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
class memory_object_holder
{
public:
virtual const cl_mem data() const = 0;
PYOPENCL_EQUALITY_TESTS(memory_object_holder);
size_t size() const
{
size_t param_value;
PYOPENCL_CALL_GUARDED(clGetMemObjectInfo,
(data(), CL_MEM_SIZE, sizeof(param_value), ¶m_value, 0));
return param_value;
}
py::object get_info(cl_mem_info param_name) const;
};
class memory_object : noncopyable, public memory_object_holder
typedef std::unique_ptr<py_buffer_wrapper> hostbuf_t;
private:
bool m_valid;
cl_mem m_mem;
hostbuf_t m_hostbuf;
public:
memory_object(cl_mem mem, bool retain, hostbuf_t hostbuf=hostbuf_t())
: m_valid(true), m_mem(mem)
{
if (retain)
PYOPENCL_CALL_GUARDED(clRetainMemObject, (mem));
m_hostbuf = PYOPENCL_STD_MOVE_IF_NEW_BUF_INTF(hostbuf);
}
memory_object(memory_object &src)
m_hostbuf(PYOPENCL_STD_MOVE_IF_NEW_BUF_INTF(src.m_hostbuf))
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
{
PYOPENCL_CALL_GUARDED(clRetainMemObject, (m_mem));
}
memory_object(memory_object_holder const &src)
: m_valid(true), m_mem(src.data())
{
PYOPENCL_CALL_GUARDED(clRetainMemObject, (m_mem));
}
void release()
{
if (!m_valid)
throw error("MemoryObject.free", CL_INVALID_VALUE,
"trying to double-unref mem object");
PYOPENCL_CALL_GUARDED_CLEANUP(clReleaseMemObject, (m_mem));
m_valid = false;
}
virtual ~memory_object()
{
if (m_valid)
release();
}
py::object hostbuf()
{
if (m_hostbuf.get())
return py::reinterpret_borrow<py::object>(m_hostbuf->m_buf.obj);
}
const cl_mem data() const
{ return m_mem; }
};
#if PYOPENCL_CL_VERSION >= 0x1020
inline
event *enqueue_migrate_mem_objects(
command_queue &cq,
py::object py_mem_objects,
cl_mem_migration_flags flags,
py::object py_wait_for)
{
PYOPENCL_PARSE_WAIT_FOR;
std::vector<cl_mem> mem_objects;
for (py::handle mo: py_mem_objects)
mem_objects.push_back(mo.cast<const memory_object &>().data());