Newer
Older
return new image(mem, false, std::move(retained_buf_obj));
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
}
catch (...)
{
PYOPENCL_CALL_GUARDED(clReleaseMemObject, (mem));
throw;
}
}
#if PYOPENCL_CL_VERSION >= 0x1020
inline
image *create_image_from_desc(
context const &ctx,
cl_mem_flags flags,
cl_image_format const &fmt,
cl_image_desc &desc,
py::object buffer)
{
if (buffer.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;
std::unique_ptr<py_buffer_wrapper> retained_buf_obj;
if (buffer.ptr() != Py_None)
{
retained_buf_obj = std::unique_ptr<py_buffer_wrapper>(new py_buffer_wrapper);
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
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(buffer.ptr(), py_buf_flags);
buf = retained_buf_obj->m_buf.buf;
}
PYOPENCL_PRINT_CALL_TRACE("clCreateImage");
cl_int status_code;
cl_mem mem = clCreateImage(ctx.data(), flags, &fmt, &desc, buf, &status_code);
if (status_code != CL_SUCCESS)
throw pyopencl::error("clCreateImage", status_code);
if (!(flags & CL_MEM_USE_HOST_PTR))
retained_buf_obj.reset();
try
{
return new image(mem, false, std::move(retained_buf_obj));
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
}
catch (...)
{
PYOPENCL_CALL_GUARDED(clReleaseMemObject, (mem));
throw;
}
}
#endif
// }}}
// {{{ image transfers
inline
event *enqueue_read_image(
command_queue &cq,
image &img,
py::object py_origin, py::object py_region,
py::object buffer,
size_t row_pitch, size_t slice_pitch,
py::object py_wait_for,
bool is_blocking)
{
PYOPENCL_PARSE_WAIT_FOR;
COPY_PY_COORD_TRIPLE(origin);
COPY_PY_REGION_TRIPLE(region);
void *buf;
std::unique_ptr<py_buffer_wrapper> ward(new py_buffer_wrapper);
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
ward->get(buffer.ptr(), PyBUF_ANY_CONTIGUOUS | PyBUF_WRITABLE);
buf = ward->m_buf.buf;
cl_event evt;
PYOPENCL_RETRY_IF_MEM_ERROR(
PYOPENCL_CALL_GUARDED(clEnqueueReadImage, (
cq.data(),
img.data(),
PYOPENCL_CAST_BOOL(is_blocking),
origin, region, row_pitch, slice_pitch, buf,
PYOPENCL_WAITLIST_ARGS, &evt
));
);
PYOPENCL_RETURN_NEW_NANNY_EVENT(evt, ward);
}
inline
event *enqueue_write_image(
command_queue &cq,
image &img,
py::object py_origin, py::object py_region,
py::object buffer,
size_t row_pitch, size_t slice_pitch,
py::object py_wait_for,
bool is_blocking)
{
PYOPENCL_PARSE_WAIT_FOR;
COPY_PY_COORD_TRIPLE(origin);
COPY_PY_REGION_TRIPLE(region);
const void *buf;
std::unique_ptr<py_buffer_wrapper> ward(new py_buffer_wrapper);
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
ward->get(buffer.ptr(), PyBUF_ANY_CONTIGUOUS);
buf = ward->m_buf.buf;
cl_event evt;
PYOPENCL_RETRY_IF_MEM_ERROR(
PYOPENCL_CALL_GUARDED(clEnqueueWriteImage, (
cq.data(),
img.data(),
PYOPENCL_CAST_BOOL(is_blocking),
origin, region, row_pitch, slice_pitch, buf,
PYOPENCL_WAITLIST_ARGS, &evt
));
);
PYOPENCL_RETURN_NEW_NANNY_EVENT(evt, ward);
}
inline
event *enqueue_copy_image(
command_queue &cq,
memory_object_holder &src,
memory_object_holder &dest,
py::object py_src_origin,
py::object py_dest_origin,
py::object py_region,
py::object py_wait_for
)
{
PYOPENCL_PARSE_WAIT_FOR;
COPY_PY_COORD_TRIPLE(src_origin);
COPY_PY_COORD_TRIPLE(dest_origin);
COPY_PY_REGION_TRIPLE(region);
cl_event evt;
PYOPENCL_RETRY_IF_MEM_ERROR(
PYOPENCL_CALL_GUARDED(clEnqueueCopyImage, (
cq.data(), src.data(), dest.data(),
src_origin, dest_origin, region,
PYOPENCL_WAITLIST_ARGS, &evt
));
);
PYOPENCL_RETURN_NEW_EVENT(evt);
}
inline
event *enqueue_copy_image_to_buffer(
command_queue &cq,
memory_object_holder &src,
memory_object_holder &dest,
py::object py_origin,
py::object py_region,
size_t offset,
py::object py_wait_for
)
{
PYOPENCL_PARSE_WAIT_FOR;
COPY_PY_COORD_TRIPLE(origin);
COPY_PY_REGION_TRIPLE(region);
cl_event evt;
PYOPENCL_RETRY_IF_MEM_ERROR(
PYOPENCL_CALL_GUARDED(clEnqueueCopyImageToBuffer, (
cq.data(), src.data(), dest.data(),
origin, region, offset,
PYOPENCL_WAITLIST_ARGS, &evt
));
);
PYOPENCL_RETURN_NEW_EVENT(evt);
}
inline
event *enqueue_copy_buffer_to_image(
command_queue &cq,
memory_object_holder &src,
memory_object_holder &dest,
size_t offset,
py::object py_origin,
py::object py_region,
py::object py_wait_for
)
{
PYOPENCL_PARSE_WAIT_FOR;
COPY_PY_COORD_TRIPLE(origin);
COPY_PY_REGION_TRIPLE(region);
cl_event evt;
PYOPENCL_RETRY_IF_MEM_ERROR(
PYOPENCL_CALL_GUARDED(clEnqueueCopyBufferToImage, (
cq.data(), src.data(), dest.data(),
offset, origin, region,
PYOPENCL_WAITLIST_ARGS, &evt
));
);
PYOPENCL_RETURN_NEW_EVENT(evt);
}
// }}}
#if PYOPENCL_CL_VERSION >= 0x1020
inline
event *enqueue_fill_image(
command_queue &cq,
memory_object_holder &mem,
py::object color,
py::object py_origin, py::object py_region,
py::object py_wait_for
)
{
PYOPENCL_PARSE_WAIT_FOR;
COPY_PY_COORD_TRIPLE(origin);
COPY_PY_REGION_TRIPLE(region);
const void *color_buf;
std::unique_ptr<py_buffer_wrapper> ward(new py_buffer_wrapper);
ward->get(color.ptr(), PyBUF_ANY_CONTIGUOUS);
color_buf = ward->m_buf.buf;
cl_event evt;
PYOPENCL_RETRY_IF_MEM_ERROR(
PYOPENCL_CALL_GUARDED(clEnqueueFillImage, (
cq.data(),
mem.data(),
color_buf, origin, region,
PYOPENCL_WAITLIST_ARGS, &evt
));
);
PYOPENCL_RETURN_NEW_EVENT(evt);
}
#endif
// }}}
// {{{ pipe
class pipe : public memory_object
{
public:
pipe(cl_mem mem, bool retain)
: memory_object(mem, retain)
{ }
#if PYOPENCL_CL_VERSION < 0x2000
typedef void* cl_pipe_info;
#endif
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
py::object get_pipe_info(cl_pipe_info param_name) const
{
#if PYOPENCL_CL_VERSION >= 0x2000
switch (param_name)
{
case CL_PIPE_PACKET_SIZE:
case CL_PIPE_MAX_PACKETS:
PYOPENCL_GET_TYPED_INFO(Pipe, data(), param_name, cl_uint);
default:
throw error("Pipe.get_pipe_info", CL_INVALID_VALUE);
}
#else
throw error("Pipes not available. PyOpenCL was not compiled against a CL2+ header.",
CL_INVALID_VALUE);
#endif
}
};
#if PYOPENCL_CL_VERSION >= 0x2000
inline
pipe *create_pipe(
context const &ctx,
cl_mem_flags flags,
cl_uint pipe_packet_size,
cl_uint pipe_max_packets,
py::sequence py_props)
{
PYOPENCL_STACK_CONTAINER(cl_pipe_properties, props, py::len(py_props) + 1);
{
size_t i = 0;
for (auto prop: py_props)
props[i++] = py::cast<cl_pipe_properties>(prop);
props[i++] = 0;
}
#endif
if (py::len(py_props) != 0)
throw pyopencl::error("Pipe", CL_INVALID_VALUE, "non-empty properties "
"argument to Pipe not allowed");
cl_int status_code;
PYOPENCL_PRINT_CALL_TRACE("clCreatePipe");
cl_mem mem = clCreatePipe(
ctx.data(),
flags,
pipe_packet_size,
pipe_max_packets,
nullptr,
&status_code);
if (status_code != CL_SUCCESS)
throw pyopencl::error("Pipe", status_code);
try
{
return new pipe(mem, false);
}
catch (...)
{
PYOPENCL_CALL_GUARDED(clReleaseMemObject, (mem));
throw;
}
}
#endif
// }}}
// {{{ maps
class memory_map
{
private:
bool m_valid;
memory_object m_mem;
void *m_ptr;
public:
memory_map(std::shared_ptr<command_queue> cq, memory_object const &mem, void *ptr)
: m_valid(true), m_queue(cq), m_mem(mem), m_ptr(ptr)
{
}
~memory_map()
{
if (m_valid)
}
event *release(command_queue *cq, py::object py_wait_for)
{
PYOPENCL_PARSE_WAIT_FOR;
if (cq == 0)
cl_event evt;
PYOPENCL_CALL_GUARDED(clEnqueueUnmapMemObject, (
cq->data(), m_mem.data(), m_ptr,
PYOPENCL_WAITLIST_ARGS, &evt
));
m_valid = false;
PYOPENCL_RETURN_NEW_EVENT(evt);
}
};
// FIXME: Reenable in pypy
#ifndef PYPY_VERSION
inline
py::object enqueue_map_buffer(
memory_object_holder &buf,
cl_map_flags flags,
size_t offset,
py::object py_shape, py::object dtype,
py::object py_order, py::object py_strides,
py::object py_wait_for,
bool is_blocking
)
{
PYOPENCL_PARSE_WAIT_FOR;
PYOPENCL_PARSE_NUMPY_ARRAY_SPEC;
npy_uintp size_in_bytes = tp_descr->elsize;
for (npy_intp sdim: shape)
cl_event evt;
cl_int status_code;
PYOPENCL_PRINT_CALL_TRACE("clEnqueueMapBuffer");
void *mapped;
PYOPENCL_RETRY_IF_MEM_ERROR(
{
{
py::gil_scoped_release release;
mapped = clEnqueueMapBuffer(
cq->data(), buf.data(),
PYOPENCL_CAST_BOOL(is_blocking), flags,
offset, size_in_bytes,
PYOPENCL_WAITLIST_ARGS, &evt,
&status_code);
}
if (status_code != CL_SUCCESS)
throw pyopencl::error("clEnqueueMapBuffer", status_code);
} );
event evt_handle(evt, false);
std::unique_ptr<memory_map> map;
result = py::object(py::reinterpret_steal<py::object>(PyArray_NewFromDescr(
&PyArray_Type, tp_descr,
shape.size(),
shape.empty() ? nullptr : &shape.front(),
strides.empty() ? nullptr : &strides.front(),
mapped, ary_flags, /*obj*/nullptr)));
if (size_in_bytes != (npy_uintp) PyArray_NBYTES(result.ptr()))
throw pyopencl::error("enqueue_map_buffer", CL_INVALID_VALUE,
"miscalculated numpy array size (not contiguous?)");
map = std::unique_ptr<memory_map>(new memory_map(cq, buf, mapped));
}
catch (...)
{
PYOPENCL_CALL_GUARDED_CLEANUP(clEnqueueUnmapMemObject, (
cq->data(), buf.data(), mapped, 0, 0, 0));
py::object map_py(handle_from_new_ptr(map.release()));
PyArray_BASE(result.ptr()) = map_py.ptr();
Py_INCREF(map_py.ptr());
return py::make_tuple(
result,
handle_from_new_ptr(new event(evt_handle)));
}
// FIXME: Reenable in pypy
#ifndef PYPY_VERSION
inline
py::object enqueue_map_image(
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
memory_object_holder &img,
cl_map_flags flags,
py::object py_origin,
py::object py_region,
py::object py_shape, py::object dtype,
py::object py_order, py::object py_strides,
py::object py_wait_for,
bool is_blocking
)
{
PYOPENCL_PARSE_WAIT_FOR;
PYOPENCL_PARSE_NUMPY_ARRAY_SPEC;
COPY_PY_COORD_TRIPLE(origin);
COPY_PY_REGION_TRIPLE(region);
cl_event evt;
cl_int status_code;
PYOPENCL_PRINT_CALL_TRACE("clEnqueueMapImage");
size_t row_pitch, slice_pitch;
void *mapped;
PYOPENCL_RETRY_IF_MEM_ERROR(
{
{
py::gil_scoped_release release;
mapped = clEnqueueMapImage(
cq->data(), img.data(),
PYOPENCL_CAST_BOOL(is_blocking), flags,
origin, region, &row_pitch, &slice_pitch,
PYOPENCL_WAITLIST_ARGS, &evt,
&status_code);
}
if (status_code != CL_SUCCESS)
throw pyopencl::error("clEnqueueMapImage", status_code);
} );
event evt_handle(evt, false);
std::unique_ptr<memory_map> map;
map = std::unique_ptr<memory_map>(new memory_map(cq, img, mapped));
}
catch (...)
{
PYOPENCL_CALL_GUARDED_CLEANUP(clEnqueueUnmapMemObject, (
cq->data(), img.data(), mapped, 0, 0, 0));
py::object result = py::reinterpret_steal<py::object>(PyArray_NewFromDescr(
&PyArray_Type, tp_descr,
shape.size(),
shape.empty() ? nullptr : &shape.front(),
strides.empty() ? nullptr : &strides.front(),
mapped, ary_flags, /*obj*/nullptr));
py::object map_py(handle_from_new_ptr(map.release()));
PyArray_BASE(result.ptr()) = map_py.ptr();
Py_INCREF(map_py.ptr());
return py::make_tuple(
result,
handle_from_new_ptr(new event(evt_handle)),
row_pitch, slice_pitch);
}
// {{{ svm
#if PYOPENCL_CL_VERSION >= 0x2000
class svm_arg_wrapper
{
private:
void *m_ptr;
PYOPENCL_BUFFER_SIZE_T m_size;
std::unique_ptr<py_buffer_wrapper> ward;
public:
svm_arg_wrapper(py::object holder)
{
ward = std::unique_ptr<py_buffer_wrapper>(new py_buffer_wrapper);
#ifdef PYPY_VERSION
// FIXME: get a read-only buffer
// Not quite honest, but Pypy doesn't consider numpy arrays
// created from objects with the __aray_interface__ writeable.
ward->get(holder.ptr(), PyBUF_ANY_CONTIGUOUS);
#else
ward->get(holder.ptr(), PyBUF_ANY_CONTIGUOUS | PyBUF_WRITABLE);
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
m_ptr = ward->m_buf.buf;
m_size = ward->m_buf.len;
}
void *ptr() const
{
return m_ptr;
}
size_t size() const
{
return m_size;
}
};
class svm_allocation : noncopyable
{
private:
std::shared_ptr<context> m_context;
void *m_allocation;
public:
svm_allocation(std::shared_ptr<context> const &ctx, size_t size, cl_uint alignment, cl_svm_mem_flags flags)
: m_context(ctx)
{
PYOPENCL_PRINT_CALL_TRACE("clSVMalloc");
m_allocation = clSVMAlloc(
ctx->data(),
flags, size, alignment);
if (!m_allocation)
throw pyopencl::error("clSVMAlloc", CL_OUT_OF_RESOURCES);
}
~svm_allocation()
{
if (m_allocation)
release();
}
void release()
{
if (!m_allocation)
throw error("SVMAllocation.release", CL_INVALID_VALUE,
"trying to double-unref svm allocation");
clSVMFree(m_context->data(), m_allocation);
m_allocation = nullptr;
}
void enqueue_release(command_queue &queue, py::object py_wait_for)
{
PYOPENCL_PARSE_WAIT_FOR;
if (!m_allocation)
throw error("SVMAllocation.release", CL_INVALID_VALUE,
"trying to double-unref svm allocation");
cl_event evt;
PYOPENCL_CALL_GUARDED_CLEANUP(clEnqueueSVMFree, (
queue.data(), 1, &m_allocation,
nullptr, nullptr,
PYOPENCL_WAITLIST_ARGS, &evt));
m_allocation = nullptr;
}
void *ptr() const
{
return m_allocation;
}
intptr_t ptr_as_int() const
{
return (intptr_t) m_allocation;
}
bool operator==(svm_allocation const &other) const
{
return m_allocation == other.m_allocation;
}
bool operator!=(svm_allocation const &other) const
{
return m_allocation != other.m_allocation;
}
};
inline
event *enqueue_svm_memcpy(
command_queue &cq,
cl_bool is_blocking,
svm_arg_wrapper &dst, svm_arg_wrapper &src,
py::object py_wait_for
)
{
PYOPENCL_PARSE_WAIT_FOR;
if (src.size() != dst.size())
throw error("_enqueue_svm_memcpy", CL_INVALID_VALUE,
"sizes of source and destination buffer do not match");
cl_event evt;
PYOPENCL_CALL_GUARDED(
clEnqueueSVMMemcpy,
(
cq.data(),
is_blocking,
dst.ptr(), src.ptr(),
dst.size(),
PYOPENCL_WAITLIST_ARGS,
&evt
));
PYOPENCL_RETURN_NEW_EVENT(evt);
}
inline
event *enqueue_svm_memfill(
command_queue &cq,
svm_arg_wrapper &dst, py::object py_pattern,
py::object byte_count,
py::object py_wait_for
)
{
PYOPENCL_PARSE_WAIT_FOR;
const void *pattern_ptr;
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
PYOPENCL_BUFFER_SIZE_T pattern_len;
std::unique_ptr<py_buffer_wrapper> pattern_ward(new py_buffer_wrapper);
pattern_ward->get(py_pattern.ptr(), PyBUF_ANY_CONTIGUOUS);
pattern_ptr = pattern_ward->m_buf.buf;
pattern_len = pattern_ward->m_buf.len;
size_t fill_size = dst.size();
if (!byte_count.is_none())
fill_size = py::cast<size_t>(byte_count);
cl_event evt;
PYOPENCL_CALL_GUARDED(
clEnqueueSVMMemFill,
(
cq.data(),
dst.ptr(), pattern_ptr,
pattern_len,
fill_size,
PYOPENCL_WAITLIST_ARGS,
&evt
));
PYOPENCL_RETURN_NEW_EVENT(evt);
}
inline
event *enqueue_svm_map(
command_queue &cq,
cl_bool is_blocking,
cl_map_flags flags,
svm_arg_wrapper &svm,
py::object py_wait_for
)
{
PYOPENCL_PARSE_WAIT_FOR;
cl_event evt;
PYOPENCL_CALL_GUARDED(
clEnqueueSVMMap,
(
cq.data(),
is_blocking,
flags,
svm.ptr(), svm.size(),
PYOPENCL_WAITLIST_ARGS,
&evt
));
PYOPENCL_RETURN_NEW_EVENT(evt);
}
inline
event *enqueue_svm_unmap(
command_queue &cq,
svm_arg_wrapper &svm,
py::object py_wait_for
)
{
PYOPENCL_PARSE_WAIT_FOR;
cl_event evt;
PYOPENCL_CALL_GUARDED(
clEnqueueSVMUnmap,
(
cq.data(),
svm.ptr(),
PYOPENCL_WAITLIST_ARGS,
&evt
));
PYOPENCL_RETURN_NEW_EVENT(evt);
}
#endif
#if PYOPENCL_CL_VERSION >= 0x2010
inline
event *enqueue_svm_migratemem(
command_queue &cq,
py::sequence svms,
cl_mem_migration_flags flags,
py::object py_wait_for
)
{
PYOPENCL_PARSE_WAIT_FOR;
std::vector<const void *> svm_pointers;
std::vector<size_t> sizes;
for (py::handle py_svm: svms)
{
svm_arg_wrapper &svm(py::cast<svm_arg_wrapper &>(py_svm));
svm_pointers.push_back(svm.ptr());
sizes.push_back(svm.size());
}
cl_event evt;
PYOPENCL_CALL_GUARDED(
clEnqueueSVMMigrateMem,
(
cq.data(),
svm_pointers.size(),
svm_pointers.empty() ? nullptr : &svm_pointers.front(),
sizes.empty() ? nullptr : &sizes.front(),
flags,
PYOPENCL_WAITLIST_ARGS,
&evt
));
PYOPENCL_RETURN_NEW_EVENT(evt);
}
#endif
// }}}
class sampler : noncopyable
{
private:
cl_sampler m_sampler;
public:
#if PYOPENCL_CL_VERSION >= 0x2000
sampler(context const &ctx, py::sequence py_props)
{
int hex_plat_version = ctx.get_hex_platform_version();
if (hex_plat_version < 0x2000)
{
std::cerr <<
"sampler 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_sampler_properties, props, py::len(py_props) + 1);
{
size_t i = 0;
for (auto prop: py_props)
props[i++] = py::cast<cl_sampler_properties>(prop);
props[i++] = 0;
}
cl_int status_code;
PYOPENCL_PRINT_CALL_TRACE("clCreateSamplerWithProperties");
m_sampler = clCreateSamplerWithProperties(
ctx.data(),
Andreas Klöckner
committed
PYOPENCL_STACK_CONTAINER_GET_PTR(props),
&status_code);
if (status_code != CL_SUCCESS)
throw pyopencl::error("Sampler", status_code);
}
#endif
sampler(context const &ctx, bool normalized_coordinates,
cl_addressing_mode am, cl_filter_mode fm)
{
PYOPENCL_PRINT_CALL_TRACE("clCreateSampler");
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
int hex_plat_version = ctx.get_hex_platform_version();
#if PYOPENCL_CL_VERSION >= 0x2000
if (hex_plat_version >= 0x2000)
{
cl_sampler_properties props_list[] = {
CL_SAMPLER_NORMALIZED_COORDS, normalized_coordinates,
CL_SAMPLER_ADDRESSING_MODE, am,
CL_SAMPLER_FILTER_MODE, fm,
0,
};
cl_int status_code;
PYOPENCL_PRINT_CALL_TRACE("clCreateSamplerWithProperties");
m_sampler = clCreateSamplerWithProperties(
ctx.data(), props_list, &status_code);
if (status_code != CL_SUCCESS)
throw pyopencl::error("Sampler", status_code);
}
else
#endif
{
cl_int status_code;
#if defined(__GNUG__) && !defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
m_sampler = clCreateSampler(
ctx.data(),
normalized_coordinates,
am, fm, &status_code);
#if defined(__GNUG__) && !defined(__clang__)
#pragma GCC diagnostic pop
#endif
if (status_code != CL_SUCCESS)
throw pyopencl::error("Sampler", status_code);
}
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
}
sampler(cl_sampler samp, bool retain)
: m_sampler(samp)
{
if (retain)
PYOPENCL_CALL_GUARDED(clRetainSampler, (samp));
}
~sampler()
{
PYOPENCL_CALL_GUARDED_CLEANUP(clReleaseSampler, (m_sampler));
}
cl_sampler data() const
{
return m_sampler;
}
PYOPENCL_EQUALITY_TESTS(sampler);
py::object get_info(cl_sampler_info param_name) const
{
switch (param_name)
{
case CL_SAMPLER_REFERENCE_COUNT:
PYOPENCL_GET_TYPED_INFO(Sampler, m_sampler, param_name,
cl_uint);
case CL_SAMPLER_CONTEXT:
PYOPENCL_GET_OPAQUE_INFO(Sampler, m_sampler, param_name,
cl_context, context);
case CL_SAMPLER_ADDRESSING_MODE:
PYOPENCL_GET_TYPED_INFO(Sampler, m_sampler, param_name,
cl_addressing_mode);
case CL_SAMPLER_FILTER_MODE:
PYOPENCL_GET_TYPED_INFO(Sampler, m_sampler, param_name,
cl_filter_mode);
case CL_SAMPLER_NORMALIZED_COORDS:
PYOPENCL_GET_TYPED_INFO(Sampler, m_sampler, param_name,
#if PYOPENCL_CL_VERSION >= 0x3000
case CL_SAMPLER_PROPERTIES:
{
std::vector<cl_sampler_properties> result;
PYOPENCL_GET_VEC_INFO(Sampler, m_sampler, param_name, result);
PYOPENCL_RETURN_VECTOR(cl_sampler_properties, result);
}
#endif
#ifdef CL_SAMPLER_MIP_FILTER_MODE_KHR
case CL_SAMPLER_MIP_FILTER_MODE_KHR:
PYOPENCL_GET_TYPED_INFO(Sampler, m_sampler, param_name,
cl_filter_mode);
case CL_SAMPLER_LOD_MIN_KHR:
case CL_SAMPLER_LOD_MAX_KHR:
PYOPENCL_GET_TYPED_INFO(Sampler, m_sampler, param_name, float);
#endif
default:
throw error("Sampler.get_info", CL_INVALID_VALUE);
}
}
};
// }}}
enum program_kind_type { KND_UNKNOWN, KND_SOURCE, KND_BINARY, KND_IL };
private:
cl_program m_program;
program_kind_type m_program_kind;
public:
program(cl_program prog, bool retain, program_kind_type progkind=KND_UNKNOWN)