Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
"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:
PYOPENCL_GET_INTEGRAL_INFO(
Context, m_context, param_name, cl_uint);
#endif
default:
throw error("Context.get_info", CL_INVALID_VALUE);
}
}
};
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)
{
PYTHON_FOREACH(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>());
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
}
#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
= props.empty( ) ? NULL : &props.front();
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;
PYTHON_FOREACH(py_dev, py_devices)
devices.push_back(py_dev.cast<const device &>().data());
PYOPENCL_PRINT_CALL_TRACE("clCreateContext");
ctx = clCreateContext(
props_ptr,
devices.size(),
devices.empty( ) ? NULL : &devices.front(),
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>();
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
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);
)
}
// }}}
1153
1154
1155
1156
1157
1158
1159
1160
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
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
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=0,
cl_command_queue_properties props=0)
{
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];
}
cl_int status_code;
PYOPENCL_PRINT_CALL_TRACE("clCreateCommandQueue");
m_queue = clCreateCommandQueue(
ctx.data(), dev, props, &status_code);
if (status_code != CL_SUCCESS)
throw pyopencl::error("CommandQueue", status_code);
}
~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_INTEGRAL_INFO(CommandQueue, m_queue, param_name,
cl_uint);
case CL_QUEUE_PROPERTIES:
PYOPENCL_GET_INTEGRAL_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>(
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
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
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
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
{
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_INTEGRAL_INFO(Event, m_event, param_name,
cl_command_type);
case CL_EVENT_COMMAND_EXECUTION_STATUS:
PYOPENCL_GET_INTEGRAL_INFO(Event, m_event, param_name,
cl_int);
case CL_EVENT_REFERENCE_COUNT:
PYOPENCL_GET_INTEGRAL_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_INTEGRAL_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));
}
};
#ifdef PYOPENCL_USE_NEW_BUFFER_INTERFACE
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))
{ }
~nanny_event()
{ wait(); }
py::object get_ward() const
{
if (m_ward.get())
{
return py::reinterpret_borrow<py::object>(m_ward->m_buf.obj);
1369
1370
1371
1372
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
}
virtual void wait()
{
event::wait();
m_ward.reset();
}
};
#else
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:
py::object m_ward;
public:
nanny_event(cl_event evt, bool retain, py::object ward)
: event(evt, retain), m_ward(ward)
{ }
nanny_event(nanny_event const &src)
: event(src), m_ward(src.m_ward)
{ }
~nanny_event()
{ wait(); }
py::object get_ward() const
{ return m_ward; }
virtual void wait()
{
event::wait();
}
};
#endif
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));
PYTHON_FOREACH(evt, events)
event_wait_list[num_events_in_wait_list++] =
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
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));
PYTHON_FOREACH(py_evt, py_events)
event_list[num_events++] = py_evt.cast<event &>().data();
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
PYOPENCL_CALL_GUARDED(clEnqueueWaitForEvents, (
cq.data(), num_events, event_list.empty( ) ? NULL : &event_list.front()));
}
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)
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
{
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
// }}}
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
// {{{ memory_object
py::object create_mem_object_wrapper(cl_mem mem);
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
{
public:
#ifdef PYOPENCL_USE_NEW_BUFFER_INTERFACE
typedef std::unique_ptr<py_buffer_wrapper> hostbuf_t;
#else
typedef py::object hostbuf_t;
#endif
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))
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
{
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()
{
#ifdef PYOPENCL_USE_NEW_BUFFER_INTERFACE
if (m_hostbuf.get())
return py::reinterpret_borrow<py::object>(m_hostbuf->m_buf.obj);
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
#else
return m_hostbuf;
#endif
}
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;
PYTHON_FOREACH(mo, py_mem_objects)
mem_objects.push_back(mo.cast<const memory_object &>().data());
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
cl_event evt;
PYOPENCL_RETRY_IF_MEM_ERROR(
PYOPENCL_CALL_GUARDED(clEnqueueMigrateMemObjects, (
cq.data(),
mem_objects.size(), mem_objects.empty( ) ? NULL : &mem_objects.front(),
flags,
PYOPENCL_WAITLIST_ARGS, &evt
));
);
PYOPENCL_RETURN_NEW_EVENT(evt);
}
#endif
#ifdef cl_ext_migrate_memobject
inline
event *enqueue_migrate_mem_object_ext(
command_queue &cq,
py::object py_mem_objects,
cl_mem_migration_flags_ext flags,
py::object py_wait_for)
{
PYOPENCL_PARSE_WAIT_FOR;
#if PYOPENCL_CL_VERSION >= 0x1020
// {{{ get platform
cl_device_id dev;
PYOPENCL_CALL_GUARDED(clGetCommandQueueInfo, (cq.data(), CL_QUEUE_DEVICE,
sizeof(dev), &dev, NULL));
cl_platform_id plat;
PYOPENCL_CALL_GUARDED(clGetDeviceInfo, (dev, CL_DEVICE_PLATFORM,
sizeof(plat), &plat, NULL));
// }}}
#endif
PYOPENCL_GET_EXT_FUN(plat,
clEnqueueMigrateMemObjectEXT, enqueue_migrate_fn);
std::vector<cl_mem> mem_objects;
PYTHON_FOREACH(mo, py_mem_objects)
mem_objects.push_back(mo.cast<memory_object &>().data());
cl_event evt;
PYOPENCL_RETRY_IF_MEM_ERROR(
PYOPENCL_CALL_GUARDED(enqueue_migrate_fn, (
cq.data(),
mem_objects.size(), mem_objects.empty( ) ? NULL : &mem_objects.front(),
flags,
PYOPENCL_WAITLIST_ARGS, &evt
));
);
PYOPENCL_RETURN_NEW_EVENT(evt);
}
#endif
// }}}
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
// {{{ buffer
inline cl_mem create_buffer(
cl_context ctx,
cl_mem_flags flags,
size_t size,
void *host_ptr)
{
cl_int status_code;
PYOPENCL_PRINT_CALL_TRACE("clCreateBuffer");
cl_mem mem = clCreateBuffer(ctx, flags, size, host_ptr, &status_code);
if (status_code != CL_SUCCESS)
throw pyopencl::error("create_buffer", status_code);
return mem;
}
inline cl_mem create_buffer_gc(
cl_context ctx,
cl_mem_flags flags,
size_t size,
void *host_ptr)
{
PYOPENCL_RETRY_RETURN_IF_MEM_ERROR(
return create_buffer(ctx, flags, size, host_ptr);
);
}
#if PYOPENCL_CL_VERSION >= 0x1010
inline cl_mem create_sub_buffer(
cl_mem buffer, cl_mem_flags flags, cl_buffer_create_type bct,
const void *buffer_create_info)
{
cl_int status_code;
PYOPENCL_PRINT_CALL_TRACE("clCreateSubBuffer");
cl_mem mem = clCreateSubBuffer(buffer, flags,
bct, buffer_create_info, &status_code);
if (status_code != CL_SUCCESS)
throw pyopencl::error("clCreateSubBuffer", status_code);
return mem;
}
inline cl_mem create_sub_buffer_gc(
cl_mem buffer, cl_mem_flags flags, cl_buffer_create_type bct,
const void *buffer_create_info)
{
PYOPENCL_RETRY_RETURN_IF_MEM_ERROR(
return create_sub_buffer(buffer, flags, bct, buffer_create_info);
);
}
#endif
class buffer : public memory_object
{
public:
buffer(cl_mem mem, bool retain, hostbuf_t hostbuf=hostbuf_t())
: memory_object(mem, retain, PYOPENCL_STD_MOVE_IF_NEW_BUF_INTF(hostbuf))
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
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
1843
1844
1845
1846
{ }
#if PYOPENCL_CL_VERSION >= 0x1010
buffer *get_sub_region(
size_t origin, size_t size, cl_mem_flags flags) const
{
cl_buffer_region region = { origin, size};
cl_mem mem = create_sub_buffer_gc(
data(), flags, CL_BUFFER_CREATE_TYPE_REGION, ®ion);
try
{
return new buffer(mem, false);
}
catch (...)
{
PYOPENCL_CALL_GUARDED(clReleaseMemObject, (mem));
throw;
}
}
buffer *getitem(py::slice slc) const
{
PYOPENCL_BUFFER_SIZE_T start, end, stride, length;
size_t my_length;
PYOPENCL_CALL_GUARDED(clGetMemObjectInfo,
(data(), CL_MEM_SIZE, sizeof(my_length), &my_length, 0));
#if PY_VERSION_HEX >= 0x03020000
if (PySlice_GetIndicesEx(slc.ptr(),
#else
if (PySlice_GetIndicesEx(reinterpret_cast<PySliceObject *>(slc.ptr()),
#endif
my_length, &start, &end, &stride, &length) != 0)
throw py::error_already_set();
if (stride != 1)
throw pyopencl::error("Buffer.__getitem__", CL_INVALID_VALUE,
"Buffer slice must have stride 1");
cl_mem_flags my_flags;
PYOPENCL_CALL_GUARDED(clGetMemObjectInfo,
(data(), CL_MEM_FLAGS, sizeof(my_flags), &my_flags, 0));
my_flags &= ~CL_MEM_COPY_HOST_PTR;
if (end <= start)
throw pyopencl::error("Buffer.__getitem__", CL_INVALID_VALUE,
"Buffer slice have end > start");
return get_sub_region(start, end-start, my_flags);
}
#endif
};
// {{{ buffer creation
inline
buffer *create_buffer_py(
context &ctx,
cl_mem_flags flags,
size_t size,
py::object py_hostbuf
)
{
if (py_hostbuf.ptr() != Py_None &&
!(flags & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR)))
PyErr_Warn(PyExc_UserWarning, "'hostbuf' was passed, "
"but no memory flags to make use of it.");
void *buf = 0;
#ifdef PYOPENCL_USE_NEW_BUFFER_INTERFACE
std::unique_ptr<py_buffer_wrapper> retained_buf_obj;
if (py_hostbuf.ptr() != Py_None)
{
retained_buf_obj = std::unique_ptr<py_buffer_wrapper>(new py_buffer_wrapper);
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
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
int py_buf_flags = PyBUF_ANY_CONTIGUOUS;
if ((flags & CL_MEM_USE_HOST_PTR)
&& ((flags & CL_MEM_READ_WRITE)
|| (flags & CL_MEM_WRITE_ONLY)))
py_buf_flags |= PyBUF_WRITABLE;
retained_buf_obj->get(py_hostbuf.ptr(), py_buf_flags);
buf = retained_buf_obj->m_buf.buf;
if (size > size_t(retained_buf_obj->m_buf.len))
throw pyopencl::error("Buffer", CL_INVALID_VALUE,
"specified size is greater than host buffer size");
if (size == 0)
size = retained_buf_obj->m_buf.len;
}
#else
py::object retained_buf_obj;
if (py_hostbuf.ptr() != Py_None)
{
PYOPENCL_BUFFER_SIZE_T len;
if ((flags & CL_MEM_USE_HOST_PTR)
&& ((flags & CL_MEM_READ_WRITE)
|| (flags & CL_MEM_WRITE_ONLY)))
{
if (PyObject_AsWriteBuffer(py_hostbuf.ptr(), &buf, &len))
throw py::error_already_set();
}
else
{
if (PyObject_AsReadBuffer(
py_hostbuf.ptr(), const_cast<const void **>(&buf), &len))
throw py::error_already_set();
}
if (flags & CL_MEM_USE_HOST_PTR)
retained_buf_obj = py_hostbuf;
if (size > size_t(len))
throw pyopencl::error("Buffer", CL_INVALID_VALUE,
"specified size is greater than host buffer size");
if (size == 0)
size = len;
}
#endif
cl_mem mem = create_buffer_gc(ctx.data(), flags, size, buf);
#ifdef PYOPENCL_USE_NEW_BUFFER_INTERFACE
if (!(flags & CL_MEM_USE_HOST_PTR))
retained_buf_obj.reset();
#endif
try
{
return new buffer(mem, false, PYOPENCL_STD_MOVE_IF_NEW_BUF_INTF(retained_buf_obj));
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
}
catch (...)
{
PYOPENCL_CALL_GUARDED(clReleaseMemObject, (mem));
throw;
}
}
// }}}
// {{{ buffer transfers
// {{{ byte-for-byte transfers
inline
event *enqueue_read_buffer(
command_queue &cq,
memory_object_holder &mem,
py::object buffer,
size_t device_offset,
py::object py_wait_for,
bool is_blocking)
{
PYOPENCL_PARSE_WAIT_FOR;
void *buf;
PYOPENCL_BUFFER_SIZE_T len;
#ifdef PYOPENCL_USE_NEW_BUFFER_INTERFACE
std::unique_ptr<py_buffer_wrapper> ward(new py_buffer_wrapper);
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
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
1978
1979
ward->get(buffer.ptr(), PyBUF_ANY_CONTIGUOUS | PyBUF_WRITABLE);
buf = ward->m_buf.buf;
len = ward->m_buf.len;
#else
py::object ward = buffer;
if (PyObject_AsWriteBuffer(buffer.ptr(), &buf, &len))
throw py::error_already_set();
#endif
cl_event evt;
PYOPENCL_RETRY_IF_MEM_ERROR(
PYOPENCL_CALL_GUARDED_THREADED(clEnqueueReadBuffer, (
cq.data(),
mem.data(),
PYOPENCL_CAST_BOOL(is_blocking),
device_offset, len, buf,
PYOPENCL_WAITLIST_ARGS, &evt
))
);
PYOPENCL_RETURN_NEW_NANNY_EVENT(evt, ward);
}
inline
event *enqueue_write_buffer(
command_queue &cq,
memory_object_holder &mem,
py::object buffer,
size_t device_offset,
py::object py_wait_for,
bool is_blocking)
{
PYOPENCL_PARSE_WAIT_FOR;
const void *buf;
PYOPENCL_BUFFER_SIZE_T len;
#ifdef PYOPENCL_USE_NEW_BUFFER_INTERFACE
std::unique_ptr<py_buffer_wrapper> ward(new py_buffer_wrapper);
ward->get(buffer.ptr(), PyBUF_ANY_CONTIGUOUS);
buf = ward->m_buf.buf;
len = ward->m_buf.len;
#else
py::object ward = buffer;
if (PyObject_AsReadBuffer(buffer.ptr(), &buf, &len))
throw py::error_already_set();
#endif
cl_event evt;
PYOPENCL_RETRY_IF_MEM_ERROR(
PYOPENCL_CALL_GUARDED_THREADED(clEnqueueWriteBuffer, (
cq.data(),
mem.data(),
PYOPENCL_CAST_BOOL(is_blocking),
device_offset, len, buf,
PYOPENCL_WAITLIST_ARGS, &evt
))