Newer
Older
inline
program *create_program_with_binary(
context &ctx,
py::sequence py_devices,
py::sequence py_binaries)
{
std::vector<cl_device_id> devices;
std::vector<const unsigned char *> binaries;
std::vector<size_t> sizes;
size_t num_devices = len(py_devices);
if (len(py_binaries) != num_devices)
throw error("create_program_with_binary", CL_INVALID_VALUE,
"device and binary counts don't match");
for (size_t i = 0; i < num_devices; ++i)
(py_devices[i]).cast<device const &>().data());
const void *buf;
PYOPENCL_BUFFER_SIZE_T len;
#ifdef PYOPENCL_USE_NEW_BUFFER_INTERFACE
py_buffer_wrapper buf_wrapper;
buf_wrapper.get(py::object(py_binaries[i]).ptr(), PyBUF_ANY_CONTIGUOUS);
buf = buf_wrapper.m_buf.buf;
len = buf_wrapper.m_buf.len;
#else
if (PyObject_AsReadBuffer(
py::object(py_binaries[i]).ptr(), &buf, &len))
throw py::error_already_set();
#endif
binaries.push_back(reinterpret_cast<const unsigned char *>(buf));
sizes.push_back(len);
}
Andreas Klöckner
committed
PYOPENCL_STACK_CONTAINER(cl_int, binary_statuses, num_devices);
cl_int status_code;
PYOPENCL_PRINT_CALL_TRACE("clCreateProgramWithBinary");
cl_program result = clCreateProgramWithBinary(
ctx.data(), num_devices,
devices.empty( ) ? nullptr : &devices.front(),
sizes.empty( ) ? nullptr : &sizes.front(),
binaries.empty( ) ? nullptr : &binaries.front(),
Andreas Klöckner
committed
PYOPENCL_STACK_CONTAINER_GET_PTR(binary_statuses),
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
&status_code);
if (status_code != CL_SUCCESS)
throw pyopencl::error("clCreateProgramWithBinary", status_code);
/*
for (int i = 0; i < num_devices; ++i)
printf("%d:%d\n", i, binary_statuses[i]);
*/
try
{
return new program(result, false, program::KND_BINARY);
}
catch (...)
{
clReleaseProgram(result);
throw;
}
}
#if (PYOPENCL_CL_VERSION >= 0x1020) || \
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
((PYOPENCL_CL_VERSION >= 0x1030) && defined(__APPLE__))
inline
program *create_program_with_built_in_kernels(
context &ctx,
py::object py_devices,
std::string const &kernel_names)
{
PYOPENCL_PARSE_PY_DEVICES;
cl_int status_code;
PYOPENCL_PRINT_CALL_TRACE("clCreateProgramWithBuiltInKernels");
cl_program result = clCreateProgramWithBuiltInKernels(
ctx.data(), num_devices, devices,
kernel_names.c_str(), &status_code);
if (status_code != CL_SUCCESS)
throw pyopencl::error("clCreateProgramWithBuiltInKernels", status_code);
try
{
return new program(result, false);
}
catch (...)
{
clReleaseProgram(result);
throw;
}
}
#endif
#if PYOPENCL_CL_VERSION >= 0x1020
inline
program *link_program(
context &ctx,
py::object py_programs,
std::string const &options,
py::object py_devices
)
{
PYOPENCL_PARSE_PY_DEVICES;
std::vector<cl_program> programs;
for (py::handle py_prg: py_programs)
program &prg = (py_prg).cast<program &>();
programs.push_back(prg.data());
}
cl_int status_code;
PYOPENCL_PRINT_CALL_TRACE("clLinkProgram");
cl_program result = clLinkProgram(
ctx.data(), num_devices, devices,
options.c_str(),
programs.size(),
0, 0,
&status_code);
if (status_code != CL_SUCCESS)
throw pyopencl::error("clLinkProgram", result, status_code);
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
try
{
return new program(result, false);
}
catch (...)
{
clReleaseProgram(result);
throw;
}
}
#endif
#if PYOPENCL_CL_VERSION >= 0x1020
inline
void unload_platform_compiler(platform &plat)
{
PYOPENCL_CALL_GUARDED(clUnloadPlatformCompiler, (plat.data()));
}
#endif
// }}}
// {{{ kernel
class local_memory
{
private:
size_t m_size;
public:
local_memory(size_t size)
: m_size(size)
{ }
size_t size() const
{ return m_size; }
};
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
{
private:
cl_kernel m_kernel;
public:
kernel(cl_kernel knl, bool retain)
: m_kernel(knl)
{
if (retain)
PYOPENCL_CALL_GUARDED(clRetainKernel, (knl));
}
kernel(program const &prg, std::string const &kernel_name)
{
cl_int status_code;
PYOPENCL_PRINT_CALL_TRACE("clCreateKernel");
m_kernel = clCreateKernel(prg.data(), kernel_name.c_str(),
&status_code);
if (status_code != CL_SUCCESS)
throw pyopencl::error("clCreateKernel", status_code);
}
~kernel()
{
PYOPENCL_CALL_GUARDED_CLEANUP(clReleaseKernel, (m_kernel));
}
cl_kernel data() const
{
return m_kernel;
}
PYOPENCL_EQUALITY_TESTS(kernel);
void set_arg_null(cl_uint arg_index)
{
cl_mem m = 0;
PYOPENCL_CALL_GUARDED(clSetKernelArg, (m_kernel, arg_index,
sizeof(cl_mem), &m));
}
void set_arg_mem(cl_uint arg_index, memory_object_holder &moh)
{
cl_mem m = moh.data();
PYOPENCL_CALL_GUARDED(clSetKernelArg,
(m_kernel, arg_index, sizeof(cl_mem), &m));
}
void set_arg_local(cl_uint arg_index, local_memory const &loc)
{
PYOPENCL_CALL_GUARDED(clSetKernelArg,
(m_kernel, arg_index, loc.size(), 0));
}
void set_arg_sampler(cl_uint arg_index, sampler const &smp)
{
cl_sampler s = smp.data();
PYOPENCL_CALL_GUARDED(clSetKernelArg,
(m_kernel, arg_index, sizeof(cl_sampler), &s));
}
void set_arg_command_queue(cl_uint arg_index, command_queue const &queue)
{
cl_command_queue q = queue.data();
PYOPENCL_CALL_GUARDED(clSetKernelArg,
(m_kernel, arg_index, sizeof(cl_command_queue), &q));
}
void set_arg_buf(cl_uint arg_index, py::object py_buffer)
{
const void *buf;
PYOPENCL_BUFFER_SIZE_T len;
#ifdef PYOPENCL_USE_NEW_BUFFER_INTERFACE
py_buffer_wrapper buf_wrapper;
try
{
buf_wrapper.get(py_buffer.ptr(), PyBUF_ANY_CONTIGUOUS);
}
catch (py::error_already_set &)
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
{
PyErr_Clear();
throw error("Kernel.set_arg", CL_INVALID_VALUE,
"invalid kernel argument");
}
buf = buf_wrapper.m_buf.buf;
len = buf_wrapper.m_buf.len;
#else
if (PyObject_AsReadBuffer(py_buffer.ptr(), &buf, &len))
{
PyErr_Clear();
throw error("Kernel.set_arg", CL_INVALID_VALUE,
"invalid kernel argument");
}
#endif
PYOPENCL_CALL_GUARDED(clSetKernelArg,
(m_kernel, arg_index, len, buf));
}
#if PYOPENCL_CL_VERSION >= 0x2000
void set_arg_svm(cl_uint arg_index, svm_arg_wrapper const &wrp)
{
PYOPENCL_CALL_GUARDED(clSetKernelArgSVMPointer,
(m_kernel, arg_index, wrp.ptr()));
}
#endif
void set_arg(cl_uint arg_index, py::object arg)
{
if (arg.ptr() == Py_None)
{
set_arg_null(arg_index);
return;
}
set_arg_mem(arg_index, arg.cast<memory_object_holder &>());
#if PYOPENCL_CL_VERSION >= 0x2000
try
{
set_arg_svm(arg_index, arg.cast<svm_arg_wrapper const &>());
return;
}
catch (py::cast_error &) { }
#endif
set_arg_local(arg_index, arg.cast<local_memory>());
set_arg_sampler(arg_index, arg.cast<const sampler &>());
try
{
set_arg_command_queue(arg_index, arg.cast<const command_queue &>());
return;
}
catch (py::cast_error &) { }
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
set_arg_buf(arg_index, arg);
}
py::object get_info(cl_kernel_info param_name) const
{
switch (param_name)
{
case CL_KERNEL_FUNCTION_NAME:
PYOPENCL_GET_STR_INFO(Kernel, m_kernel, param_name);
case CL_KERNEL_NUM_ARGS:
case CL_KERNEL_REFERENCE_COUNT:
PYOPENCL_GET_INTEGRAL_INFO(Kernel, m_kernel, param_name,
cl_uint);
case CL_KERNEL_CONTEXT:
PYOPENCL_GET_OPAQUE_INFO(Kernel, m_kernel, param_name,
cl_context, context);
case CL_KERNEL_PROGRAM:
PYOPENCL_GET_OPAQUE_INFO(Kernel, m_kernel, param_name,
cl_program, program);
#if PYOPENCL_CL_VERSION >= 0x1020
case CL_KERNEL_ATTRIBUTES:
PYOPENCL_GET_STR_INFO(Kernel, m_kernel, param_name);
#endif
default:
throw error("Kernel.get_info", CL_INVALID_VALUE);
}
}
py::object get_work_group_info(
cl_kernel_work_group_info param_name,
device const &dev
) const
{
switch (param_name)
{
#define PYOPENCL_FIRST_ARG m_kernel, dev.data() // hackety hack
case CL_KERNEL_WORK_GROUP_SIZE:
PYOPENCL_GET_INTEGRAL_INFO(KernelWorkGroup,
PYOPENCL_FIRST_ARG, param_name,
size_t);
case CL_KERNEL_COMPILE_WORK_GROUP_SIZE:
{
std::vector<size_t> result;
PYOPENCL_GET_VEC_INFO(KernelWorkGroup,
PYOPENCL_FIRST_ARG, param_name, result);
PYOPENCL_RETURN_VECTOR(size_t, result);
}
case CL_KERNEL_LOCAL_MEM_SIZE:
#if PYOPENCL_CL_VERSION >= 0x1010
case CL_KERNEL_PRIVATE_MEM_SIZE:
#endif
PYOPENCL_GET_INTEGRAL_INFO(KernelWorkGroup,
PYOPENCL_FIRST_ARG, param_name,
cl_ulong);
#if PYOPENCL_CL_VERSION >= 0x1010
case CL_KERNEL_PREFERRED_WORK_GROUP_SIZE_MULTIPLE:
PYOPENCL_GET_INTEGRAL_INFO(KernelWorkGroup,
PYOPENCL_FIRST_ARG, param_name,
size_t);
#endif
default:
throw error("Kernel.get_work_group_info", CL_INVALID_VALUE);
#undef PYOPENCL_FIRST_ARG
}
}
#if PYOPENCL_CL_VERSION >= 0x1020
py::object get_arg_info(
cl_uint arg_index,
cl_kernel_arg_info param_name
) const
{
switch (param_name)
{
#define PYOPENCL_FIRST_ARG m_kernel, arg_index // hackety hack
case CL_KERNEL_ARG_ADDRESS_QUALIFIER:
PYOPENCL_GET_INTEGRAL_INFO(KernelArg,
PYOPENCL_FIRST_ARG, param_name,
cl_kernel_arg_address_qualifier);
case CL_KERNEL_ARG_ACCESS_QUALIFIER:
PYOPENCL_GET_INTEGRAL_INFO(KernelArg,
PYOPENCL_FIRST_ARG, param_name,
cl_kernel_arg_access_qualifier);
case CL_KERNEL_ARG_TYPE_NAME:
case CL_KERNEL_ARG_NAME:
PYOPENCL_GET_STR_INFO(KernelArg, PYOPENCL_FIRST_ARG, param_name);
#undef PYOPENCL_FIRST_ARG
default:
throw error("Kernel.get_arg_info", CL_INVALID_VALUE);
}
}
#endif
};
inline
py::list create_kernels_in_program(program &pgm)
{
cl_uint num_kernels;
PYOPENCL_CALL_GUARDED(clCreateKernelsInProgram, (
pgm.data(), 0, 0, &num_kernels));
std::vector<cl_kernel> kernels(num_kernels);
PYOPENCL_CALL_GUARDED(clCreateKernelsInProgram, (
pgm.data(), num_kernels,
kernels.empty( ) ? nullptr : &kernels.front(), &num_kernels));
for (cl_kernel knl: kernels)
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
result.append(handle_from_new_ptr(new kernel(knl, true)));
return result;
}
inline
event *enqueue_nd_range_kernel(
command_queue &cq,
kernel &knl,
py::object py_global_work_size,
py::object py_local_work_size,
py::object py_global_work_offset,
py::object py_wait_for,
bool g_times_l)
{
PYOPENCL_PARSE_WAIT_FOR;
cl_uint work_dim = len(py_global_work_size);
std::vector<size_t> global_work_size;
COPY_PY_LIST(size_t, global_work_size);
size_t *local_work_size_ptr = 0;
std::vector<size_t> local_work_size;
if (py_local_work_size.ptr() != Py_None)
{
if (g_times_l)
work_dim = std::max(work_dim, unsigned(len(py_local_work_size)));
else
if (work_dim != unsigned(len(py_local_work_size)))
throw error("enqueue_nd_range_kernel", CL_INVALID_VALUE,
"global/local work sizes have differing dimensions");
COPY_PY_LIST(size_t, local_work_size);
while (local_work_size.size() < work_dim)
local_work_size.push_back(1);
while (global_work_size.size() < work_dim)
global_work_size.push_back(1);
local_work_size_ptr = local_work_size.empty( ) ? nullptr : &local_work_size.front();
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
}
if (g_times_l && local_work_size_ptr)
{
for (cl_uint work_axis = 0; work_axis < work_dim; ++work_axis)
global_work_size[work_axis] *= local_work_size[work_axis];
}
size_t *global_work_offset_ptr = 0;
std::vector<size_t> global_work_offset;
if (py_global_work_offset.ptr() != Py_None)
{
if (work_dim != unsigned(len(py_global_work_offset)))
throw error("enqueue_nd_range_kernel", CL_INVALID_VALUE,
"global work size and offset have differing dimensions");
COPY_PY_LIST(size_t, global_work_offset);
if (g_times_l && local_work_size_ptr)
{
for (cl_uint work_axis = 0; work_axis < work_dim; ++work_axis)
global_work_offset[work_axis] *= local_work_size[work_axis];
}
global_work_offset_ptr = global_work_offset.empty( ) ? nullptr : &global_work_offset.front();
}
PYOPENCL_RETRY_RETURN_IF_MEM_ERROR( {
cl_event evt;
PYOPENCL_CALL_GUARDED(clEnqueueNDRangeKernel, (
cq.data(),
knl.data(),
work_dim,
global_work_offset_ptr,
global_work_size.empty( ) ? nullptr : &global_work_size.front(),
local_work_size_ptr,
PYOPENCL_WAITLIST_ARGS, &evt
));
PYOPENCL_RETURN_NEW_EVENT(evt);
} );
}
// }}}
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
// {{{ gl interop
inline
bool have_gl()
{
#ifdef HAVE_GL
return true;
#else
return false;
#endif
}
#ifdef HAVE_GL
#ifdef __APPLE__
inline
cl_context_properties get_apple_cgl_share_group()
{
CGLContextObj kCGLContext = CGLGetCurrentContext();
CGLShareGroupObj kCGLShareGroup = CGLGetShareGroup(kCGLContext);
return (cl_context_properties) kCGLShareGroup;
}
#endif /* __APPLE__ */
class gl_buffer : public memory_object
{
public:
gl_buffer(cl_mem mem, bool retain, hostbuf_t hostbuf=hostbuf_t())
: memory_object(mem, retain, PYOPENCL_STD_MOVE_IF_NEW_BUF_INTF(hostbuf))
{ }
};
class gl_renderbuffer : public memory_object
{
public:
gl_renderbuffer(cl_mem mem, bool retain, hostbuf_t hostbuf=hostbuf_t())
: memory_object(mem, retain, PYOPENCL_STD_MOVE_IF_NEW_BUF_INTF(hostbuf))
{ }
};
class gl_texture : public image
{
public:
gl_texture(cl_mem mem, bool retain, hostbuf_t hostbuf=hostbuf_t())
: image(mem, retain, PYOPENCL_STD_MOVE_IF_NEW_BUF_INTF(hostbuf))
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
{ }
py::object get_gl_texture_info(cl_gl_texture_info param_name)
{
switch (param_name)
{
case CL_GL_TEXTURE_TARGET:
PYOPENCL_GET_INTEGRAL_INFO(GLTexture, data(), param_name, GLenum);
case CL_GL_MIPMAP_LEVEL:
PYOPENCL_GET_INTEGRAL_INFO(GLTexture, data(), param_name, GLint);
default:
throw error("MemoryObject.get_gl_texture_info", CL_INVALID_VALUE);
}
}
};
#define PYOPENCL_WRAP_BUFFER_CREATOR(TYPE, NAME, CL_NAME, ARGS, CL_ARGS) \
inline \
TYPE *NAME ARGS \
{ \
cl_int status_code; \
PYOPENCL_PRINT_CALL_TRACE(#CL_NAME); \
cl_mem mem = CL_NAME CL_ARGS; \
\
if (status_code != CL_SUCCESS) \
throw pyopencl::error(#CL_NAME, status_code); \
\
try \
{ \
return new TYPE(mem, false); \
} \
catch (...) \
{ \
PYOPENCL_CALL_GUARDED(clReleaseMemObject, (mem)); \
throw; \
} \
}
PYOPENCL_WRAP_BUFFER_CREATOR(gl_buffer,
create_from_gl_buffer, clCreateFromGLBuffer,
(context &ctx, cl_mem_flags flags, GLuint bufobj),
(ctx.data(), flags, bufobj, &status_code));
PYOPENCL_WRAP_BUFFER_CREATOR(gl_texture,
create_from_gl_texture_2d, clCreateFromGLTexture2D,
(context &ctx, cl_mem_flags flags,
GLenum texture_target, GLint miplevel, GLuint texture),
(ctx.data(), flags, texture_target, miplevel, texture, &status_code));
PYOPENCL_WRAP_BUFFER_CREATOR(gl_texture,
create_from_gl_texture_3d, clCreateFromGLTexture3D,
(context &ctx, cl_mem_flags flags,
GLenum texture_target, GLint miplevel, GLuint texture),
(ctx.data(), flags, texture_target, miplevel, texture, &status_code));
PYOPENCL_WRAP_BUFFER_CREATOR(gl_renderbuffer,
create_from_gl_renderbuffer, clCreateFromGLRenderbuffer,
(context &ctx, cl_mem_flags flags, GLuint renderbuffer),
(ctx.data(), flags, renderbuffer, &status_code));
inline
gl_texture *create_from_gl_texture(
context &ctx, cl_mem_flags flags,
GLenum texture_target, GLint miplevel,
GLuint texture, unsigned dims)
{
if (dims == 2)
return create_from_gl_texture_2d(ctx, flags, texture_target, miplevel, texture);
else if (dims == 3)
return create_from_gl_texture_3d(ctx, flags, texture_target, miplevel, texture);
else
throw pyopencl::error("Image", CL_INVALID_VALUE,
"invalid dimension");
}
inline
py::tuple get_gl_object_info(memory_object_holder const &mem)
{
cl_gl_object_type otype;
GLuint gl_name;
PYOPENCL_CALL_GUARDED(clGetGLObjectInfo, (mem.data(), &otype, &gl_name));
return py::make_tuple(otype, gl_name);
}
#define WRAP_GL_ENQUEUE(what, What) \
inline \
event *enqueue_##what##_gl_objects( \
command_queue &cq, \
py::object py_mem_objects, \
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<memory_object_holder &>().data()); \
\
cl_event evt; \
PYOPENCL_CALL_GUARDED(clEnqueue##What##GLObjects, ( \
cq.data(), \
mem_objects.size(), mem_objects.empty( ) ? nullptr : &mem_objects.front(), \
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
PYOPENCL_WAITLIST_ARGS, &evt \
)); \
\
PYOPENCL_RETURN_NEW_EVENT(evt); \
}
WRAP_GL_ENQUEUE(acquire, Acquire);
WRAP_GL_ENQUEUE(release, Release);
#endif
#if defined(cl_khr_gl_sharing) && (cl_khr_gl_sharing >= 1)
inline
py::object get_gl_context_info_khr(
py::object py_properties,
cl_gl_context_info param_name,
py::object py_platform
)
{
std::vector<cl_context_properties> props
= parse_context_properties(py_properties);
typedef CL_API_ENTRY cl_int (CL_API_CALL
*func_ptr_type)(const cl_context_properties * /* properties */,
cl_gl_context_info /* param_name */,
size_t /* param_value_size */,
void * /* param_value */,
size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
func_ptr_type func_ptr;
#if PYOPENCL_CL_VERSION >= 0x1020
if (py_platform.ptr() != Py_None)
{
platform &plat = (py_platform).cast<platform &>();
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
func_ptr = (func_ptr_type) clGetExtensionFunctionAddressForPlatform(
plat.data(), "clGetGLContextInfoKHR");
}
else
{
PYOPENCL_DEPRECATED("get_gl_context_info_khr with platform=None", "2013.1", );
func_ptr = (func_ptr_type) clGetExtensionFunctionAddress(
"clGetGLContextInfoKHR");
}
#else
func_ptr = (func_ptr_type) clGetExtensionFunctionAddress(
"clGetGLContextInfoKHR");
#endif
if (!func_ptr)
throw error("Context.get_info", CL_INVALID_PLATFORM,
"clGetGLContextInfoKHR extension function not present");
cl_context_properties *props_ptr
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
switch (param_name)
{
case CL_CURRENT_DEVICE_FOR_GL_CONTEXT_KHR:
{
cl_device_id param_value;
PYOPENCL_CALL_GUARDED(func_ptr,
(props_ptr, param_name, sizeof(param_value), ¶m_value, 0));
return py::object(handle_from_new_ptr( \
new device(param_value, /*retain*/ true)));
}
case CL_DEVICES_FOR_GL_CONTEXT_KHR:
{
size_t size;
PYOPENCL_CALL_GUARDED(func_ptr,
(props_ptr, param_name, 0, 0, &size));
std::vector<cl_device_id> devices;
devices.resize(size / sizeof(devices.front()));
PYOPENCL_CALL_GUARDED(func_ptr,
(props_ptr, param_name, size,
devices.empty( ) ? nullptr : &devices.front(), &size));
for (cl_device_id did: devices)
result.append(handle_from_new_ptr(
new device(did)));
return result;
}
default:
throw error("get_gl_context_info_khr", CL_INVALID_VALUE);
}
}
#endif
// }}}
// {{{ deferred implementation bits
inline program *error::get_program() const
{
return new program(m_program, /* retain */ true);
}
inline py::object create_mem_object_wrapper(cl_mem mem, bool retain=true)
{
cl_mem_object_type mem_obj_type;
PYOPENCL_CALL_GUARDED(clGetMemObjectInfo, \
(mem, CL_MEM_TYPE, sizeof(mem_obj_type), &mem_obj_type, 0));
switch (mem_obj_type)
{
case CL_MEM_OBJECT_BUFFER:
return py::object(handle_from_new_ptr(
new buffer(mem, retain)));
case CL_MEM_OBJECT_IMAGE2D:
case CL_MEM_OBJECT_IMAGE3D:
#if PYOPENCL_CL_VERSION >= 0x1020
case CL_MEM_OBJECT_IMAGE2D_ARRAY:
case CL_MEM_OBJECT_IMAGE1D:
case CL_MEM_OBJECT_IMAGE1D_ARRAY:
case CL_MEM_OBJECT_IMAGE1D_BUFFER:
#endif
return py::object(handle_from_new_ptr(
new image(mem, retain)));
default:
return py::object(handle_from_new_ptr(
new memory_object(mem, retain)));
py::object memory_object_from_int(intptr_t cl_mem_as_int, bool retain)
return create_mem_object_wrapper((cl_mem) cl_mem_as_int, retain);
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
}
inline
py::object memory_object_holder::get_info(cl_mem_info param_name) const
{
switch (param_name)
{
case CL_MEM_TYPE:
PYOPENCL_GET_INTEGRAL_INFO(MemObject, data(), param_name,
cl_mem_object_type);
case CL_MEM_FLAGS:
PYOPENCL_GET_INTEGRAL_INFO(MemObject, data(), param_name,
cl_mem_flags);
case CL_MEM_SIZE:
PYOPENCL_GET_INTEGRAL_INFO(MemObject, data(), param_name,
size_t);
case CL_MEM_HOST_PTR:
throw pyopencl::error("MemoryObject.get_info", CL_INVALID_VALUE,
"Use MemoryObject.get_host_array to get host pointer.");
case CL_MEM_MAP_COUNT:
PYOPENCL_GET_INTEGRAL_INFO(MemObject, data(), param_name,
cl_uint);
case CL_MEM_REFERENCE_COUNT:
PYOPENCL_GET_INTEGRAL_INFO(MemObject, data(), param_name,
cl_uint);
case CL_MEM_CONTEXT:
PYOPENCL_GET_OPAQUE_INFO(MemObject, data(), param_name,
cl_context, context);
#if PYOPENCL_CL_VERSION >= 0x1010
case CL_MEM_ASSOCIATED_MEMOBJECT:
{
cl_mem param_value;
PYOPENCL_CALL_GUARDED(clGetMemObjectInfo, \
(data(), param_name, sizeof(param_value), ¶m_value, 0));
if (param_value == 0)
{
// no associated memory object? no problem.
}
return create_mem_object_wrapper(param_value);
}
case CL_MEM_OFFSET:
PYOPENCL_GET_INTEGRAL_INFO(MemObject, data(), param_name,
size_t);
#endif
default:
throw error("MemoryObjectHolder.get_info", CL_INVALID_VALUE);
}
}
// FIXME: Reenable in pypy
#ifndef PYPY_VERSION
py::object get_mem_obj_host_array(
py::object mem_obj_py,
py::object shape, py::object dtype,
py::object order_py)
{
memory_object_holder const &mem_obj =
(mem_obj_py).cast<memory_object_holder const &>();
PyArray_Descr *tp_descr;
if (PyArray_DescrConverter(dtype.ptr(), &tp_descr) != NPY_SUCCEED)
throw py::error_already_set();
cl_mem_flags mem_flags;
PYOPENCL_CALL_GUARDED(clGetMemObjectInfo,
(mem_obj.data(), CL_MEM_FLAGS, sizeof(mem_flags), &mem_flags, 0));
if (!(mem_flags & CL_MEM_USE_HOST_PTR))
throw pyopencl::error("MemoryObject.get_host_array", CL_INVALID_VALUE,
"Only MemoryObject with USE_HOST_PTR "
"is supported.");
std::vector<npy_intp> dims;
try
{
dims.push_back(py::cast<npy_intp>(shape));
}
catch (py::cast_error &)
{
for (auto it: shape)
dims.push_back(it.cast<npy_intp>());
}
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
NPY_ORDER order = PyArray_CORDER;
PyArray_OrderConverter(order_py.ptr(), &order);
int ary_flags = 0;
if (order == PyArray_FORTRANORDER)
ary_flags |= NPY_FARRAY;
else if (order == PyArray_CORDER)
ary_flags |= NPY_CARRAY;
else
throw std::runtime_error("unrecognized order specifier");
void *host_ptr;
size_t mem_obj_size;
PYOPENCL_CALL_GUARDED(clGetMemObjectInfo,
(mem_obj.data(), CL_MEM_HOST_PTR, sizeof(host_ptr),
&host_ptr, 0));
PYOPENCL_CALL_GUARDED(clGetMemObjectInfo,
(mem_obj.data(), CL_MEM_SIZE, sizeof(mem_obj_size),
&mem_obj_size, 0));
py::object result = py::reinterpret_steal<py::object>(PyArray_NewFromDescr(
dims.size(), &dims.front(), /*strides*/ nullptr,
host_ptr, ary_flags, /*obj*/nullptr));
if ((size_t) PyArray_NBYTES(result.ptr()) > mem_obj_size)
throw pyopencl::error("MemoryObject.get_host_array",
CL_INVALID_VALUE,
"Resulting array is larger than memory object.");
PyArray_BASE(result.ptr()) = mem_obj_py.ptr();
Py_INCREF(mem_obj_py.ptr());
return result;
}
// }}}
}
#endif
// vim: foldmethod=marker