using namespace clover;
PUBLIC cl_context
-clCreateContext(const cl_context_properties *props, cl_uint num_devs,
+clCreateContext(const cl_context_properties *d_props, cl_uint num_devs,
const cl_device_id *d_devs,
void (CL_CALLBACK *pfn_notify)(const char *, const void *,
size_t, void *),
- void *user_data, cl_int *errcode_ret) try {
- auto devs = map(addresses(), objs(d_devs, num_devs));
- auto mprops = property_map(props);
+ void *user_data, cl_int *r_errcode) try {
+ auto props = property_map(d_props);
+ auto devs = objs(d_devs, num_devs);
if (!pfn_notify && user_data)
throw error(CL_INVALID_VALUE);
- for (auto p : mprops) {
- if (p.first != CL_CONTEXT_PLATFORM)
+ for (auto prop : props) {
+ if (prop.first != CL_CONTEXT_PLATFORM)
throw error(CL_INVALID_PROPERTY);
}
- ret_error(errcode_ret, CL_SUCCESS);
- return new context(property_vector(mprops), devs);
+ ret_error(r_errcode, CL_SUCCESS);
+ return desc(new context(property_vector(props), devs));
-} catch(error &e) {
- ret_error(errcode_ret, e);
+} catch (error &e) {
+ ret_error(r_errcode, e);
return NULL;
}
PUBLIC cl_context
-clCreateContextFromType(const cl_context_properties *props,
+clCreateContextFromType(const cl_context_properties *d_props,
cl_device_type type,
void (CL_CALLBACK *pfn_notify)(
const char *, const void *, size_t, void *),
- void *user_data, cl_int *errcode_ret) try {
- cl_platform_id platform;
+ void *user_data, cl_int *r_errcode) try {
+ cl_platform_id d_platform;
cl_uint num_platforms;
- cl_device_id dev;
+ cl_device_id d_dev;
cl_int ret;
- ret = clGetPlatformIDs(1, &platform, &num_platforms);
+ ret = clGetPlatformIDs(1, &d_platform, &num_platforms);
if (ret || !num_platforms)
throw error(CL_INVALID_PLATFORM);
- ret = clGetDeviceIDs(platform, type, 1, &dev, 0);
+ ret = clGetDeviceIDs(d_platform, type, 1, &d_dev, 0);
if (ret)
throw error(CL_DEVICE_NOT_FOUND);
- return clCreateContext(props, 1, &dev, pfn_notify, user_data, errcode_ret);
+ return clCreateContext(d_props, 1, &d_dev, pfn_notify, user_data, r_errcode);
-} catch(error &e) {
- ret_error(errcode_ret, e);
+} catch (error &e) {
+ ret_error(r_errcode, e);
return NULL;
}
PUBLIC cl_int
-clRetainContext(cl_context ctx) {
- if (!ctx)
- return CL_INVALID_CONTEXT;
-
- ctx->retain();
+clRetainContext(cl_context d_ctx) try {
+ obj(d_ctx).retain();
return CL_SUCCESS;
+
+} catch (error &e) {
+ return e.get();
}
PUBLIC cl_int
-clReleaseContext(cl_context ctx) {
- if (!ctx)
- return CL_INVALID_CONTEXT;
-
- if (ctx->release())
- delete ctx;
+clReleaseContext(cl_context d_ctx) try {
+ if (obj(d_ctx).release())
+ delete pobj(d_ctx);
return CL_SUCCESS;
+
+} catch (error &e) {
+ return e.get();
}
PUBLIC cl_int
-clGetContextInfo(cl_context ctx, cl_context_info param,
+clGetContextInfo(cl_context d_ctx, cl_context_info param,
size_t size, void *r_buf, size_t *r_size) try {
property_buffer buf { r_buf, size, r_size };
-
- if (!ctx)
- return CL_INVALID_CONTEXT;
+ auto &ctx = obj(d_ctx);
switch (param) {
case CL_CONTEXT_REFERENCE_COUNT:
- buf.as_scalar<cl_uint>() = ctx->ref_count();
+ buf.as_scalar<cl_uint>() = ctx.ref_count();
break;
case CL_CONTEXT_NUM_DEVICES:
- buf.as_scalar<cl_uint>() = ctx->devs.size();
+ buf.as_scalar<cl_uint>() = ctx.devs.size();
break;
case CL_CONTEXT_DEVICES:
- buf.as_vector<cl_device_id>() = ctx->devs;
+ buf.as_vector<cl_device_id>() = descs(map(derefs(), ctx.devs));
break;
case CL_CONTEXT_PROPERTIES:
- buf.as_vector<cl_context_properties>() = ctx->props();
+ buf.as_vector<cl_context_properties>() = ctx.props();
break;
default:
using namespace clover;
PUBLIC cl_event
-clCreateUserEvent(cl_context ctx, cl_int *errcode_ret) try {
- if (!ctx)
- throw error(CL_INVALID_CONTEXT);
+clCreateUserEvent(cl_context d_ctx, cl_int *errcode_ret) try {
+ auto &ctx = obj(d_ctx);
ret_error(errcode_ret, CL_SUCCESS);
- return new soft_event(*ctx, {}, false);
+ return new soft_event(ctx, {}, false);
} catch(error &e) {
ret_error(errcode_ret, e);
throw error(CL_INVALID_VALUE);
ret_error(errcode_ret, CL_SUCCESS);
- return new root_buffer(*ctx, flags, size, host_ptr);
+ return new root_buffer(obj(ctx), flags, size, host_ptr);
} catch (error &e) {
ret_error(errcode_ret, e);
}
PUBLIC cl_mem
-clCreateImage2D(cl_context ctx, cl_mem_flags flags,
+clCreateImage2D(cl_context d_ctx, cl_mem_flags flags,
const cl_image_format *format,
size_t width, size_t height, size_t row_pitch,
void *host_ptr, cl_int *errcode_ret) try {
- if (!ctx)
- throw error(CL_INVALID_CONTEXT);
+ auto &ctx = obj(d_ctx);
if (flags & ~(CL_MEM_READ_WRITE | CL_MEM_WRITE_ONLY | CL_MEM_READ_ONLY |
CL_MEM_USE_HOST_PTR | CL_MEM_ALLOC_HOST_PTR |
throw error(CL_IMAGE_FORMAT_NOT_SUPPORTED);
ret_error(errcode_ret, CL_SUCCESS);
- return new image2d(*ctx, flags, format, width, height,
+ return new image2d(ctx, flags, format, width, height,
row_pitch, host_ptr);
} catch (error &e) {
}
PUBLIC cl_mem
-clCreateImage3D(cl_context ctx, cl_mem_flags flags,
+clCreateImage3D(cl_context d_ctx, cl_mem_flags flags,
const cl_image_format *format,
size_t width, size_t height, size_t depth,
size_t row_pitch, size_t slice_pitch,
void *host_ptr, cl_int *errcode_ret) try {
- if (!ctx)
- throw error(CL_INVALID_CONTEXT);
+ auto &ctx = obj(d_ctx);
if (flags & ~(CL_MEM_READ_WRITE | CL_MEM_WRITE_ONLY | CL_MEM_READ_ONLY |
CL_MEM_USE_HOST_PTR | CL_MEM_ALLOC_HOST_PTR |
throw error(CL_IMAGE_FORMAT_NOT_SUPPORTED);
ret_error(errcode_ret, CL_SUCCESS);
- return new image3d(*ctx, flags, format, width, height, depth,
+ return new image3d(ctx, flags, format, width, height, depth,
row_pitch, slice_pitch, host_ptr);
} catch (error &e) {
}
PUBLIC cl_int
-clGetSupportedImageFormats(cl_context ctx, cl_mem_flags flags,
+clGetSupportedImageFormats(cl_context d_ctx, cl_mem_flags flags,
cl_mem_object_type type, cl_uint count,
cl_image_format *buf, cl_uint *count_ret) try {
- if (!ctx)
- throw error(CL_INVALID_CONTEXT);
+ auto &ctx = obj(d_ctx);
if (flags & ~(CL_MEM_READ_WRITE | CL_MEM_WRITE_ONLY | CL_MEM_READ_ONLY |
CL_MEM_USE_HOST_PTR | CL_MEM_ALLOC_HOST_PTR |
using namespace clover;
PUBLIC cl_program
-clCreateProgramWithSource(cl_context ctx, cl_uint count,
+clCreateProgramWithSource(cl_context d_ctx, cl_uint count,
const char **strings, const size_t *lengths,
cl_int *errcode_ret) try {
+ auto &ctx = obj(d_ctx);
std::string source;
- if (!ctx)
- throw error(CL_INVALID_CONTEXT);
-
if (!count || !strings ||
any_of(is_zero(), range(strings, count)))
throw error(CL_INVALID_VALUE);
// ...and create a program object for them.
ret_error(errcode_ret, CL_SUCCESS);
- return new program(*ctx, source);
+ return new program(ctx, source);
} catch (error &e) {
ret_error(errcode_ret, e);
}
PUBLIC cl_program
-clCreateProgramWithBinary(cl_context ctx, cl_uint n,
+clCreateProgramWithBinary(cl_context d_ctx, cl_uint n,
const cl_device_id *d_devs, const size_t *lengths,
const unsigned char **binaries, cl_int *status_ret,
cl_int *errcode_ret) try {
+ auto &ctx = obj(d_ctx);
auto devs = objs(d_devs, n);
- if (!ctx)
- throw error(CL_INVALID_CONTEXT);
-
if (!lengths || !binaries)
throw error(CL_INVALID_VALUE);
if (any_of([&](device &dev) {
- return !ctx->has_device(&dev);
+ return !ctx.has_device(dev);
}, devs))
throw error(CL_INVALID_DEVICE);
// initialize a program object with them.
ret_error(errcode_ret, CL_SUCCESS);
- return new program(*ctx, map(addresses(), devs), map(values(), modules));
+ return new program(ctx, map(addresses(), devs), map(values(), modules));
} catch (error &e) {
ret_error(errcode_ret, e);
if (devs) {
if (any_of([&](const cl_device_id dev) {
- return !prog->ctx.has_device(pobj(dev));
+ return !prog->ctx.has_device(obj(dev));
}, range(devs, count)))
throw error(CL_INVALID_DEVICE);
if (!prog)
return CL_INVALID_PROGRAM;
- if (!prog->ctx.has_device(pobj(dev)))
+ if (!prog->ctx.has_device(obj(dev)))
return CL_INVALID_DEVICE;
switch (param) {
using namespace clover;
PUBLIC cl_command_queue
-clCreateCommandQueue(cl_context ctx, cl_device_id d_dev,
+clCreateCommandQueue(cl_context d_ctx, cl_device_id d_dev,
cl_command_queue_properties props,
cl_int *errcode_ret) try {
+ auto &ctx = obj(d_ctx);
auto &dev = obj(d_dev);
- if (!ctx)
- throw error(CL_INVALID_CONTEXT);
-
- if (!ctx->has_device(&dev))
+ if (!ctx.has_device(dev))
throw error(CL_INVALID_DEVICE);
if (props & ~(CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE |
throw error(CL_INVALID_VALUE);
ret_error(errcode_ret, CL_SUCCESS);
- return new command_queue(*ctx, dev, props);
+ return new command_queue(ctx, dev, props);
} catch (error &e) {
ret_error(errcode_ret, e);
using namespace clover;
PUBLIC cl_sampler
-clCreateSampler(cl_context ctx, cl_bool norm_mode,
+clCreateSampler(cl_context d_ctx, cl_bool norm_mode,
cl_addressing_mode addr_mode, cl_filter_mode filter_mode,
cl_int *errcode_ret) try {
- if (!ctx)
- throw error(CL_INVALID_CONTEXT);
+ auto &ctx = obj(d_ctx);
ret_error(errcode_ret, CL_SUCCESS);
- return new sampler(*ctx, norm_mode, addr_mode, filter_mode);
+ return new sampler(ctx, norm_mode, addr_mode, filter_mode);
} catch (error &e) {
ret_error(errcode_ret, e);
using namespace clover;
-_cl_context::_cl_context(const std::vector<cl_context_properties> &props,
- const std::vector<device *> &devs) :
- devs(devs), _props(props) {
+context::context(const std::vector<cl_context_properties> &props,
+ const ref_vector<device> &devs) :
+ devs(map(addresses(), devs)), _props(props) {
}
bool
-_cl_context::has_device(clover::device *dev) const {
- return std::count(devs.begin(), devs.end(), dev);
+context::has_device(device &dev) const {
+ return std::count(devs.begin(), devs.end(), &dev);
}
#include "core/device.hpp"
namespace clover {
- typedef struct _cl_context context;
-}
-
-struct _cl_context : public clover::ref_counter {
-public:
- _cl_context(const std::vector<cl_context_properties> &props,
- const std::vector<clover::device *> &devs);
- _cl_context(const _cl_context &ctx) = delete;
+ class context : public ref_counter, public _cl_context {
+ public:
+ context(const std::vector<cl_context_properties> &props,
+ const ref_vector<device> &devs);
+ context(const context &ctx) = delete;
- bool has_device(clover::device *dev) const;
+ bool has_device(device &dev) const;
- const std::vector<cl_context_properties> &props() const {
- return _props;
- }
+ const std::vector<cl_context_properties> &props() const {
+ return _props;
+ }
- const std::vector<clover::device *> devs;
+ const std::vector<device *> devs;
-private:
- std::vector<cl_context_properties> _props;
-};
+ private:
+ std::vector<cl_context_properties> _props;
+ };
+}
#endif
friend class root_resource;
friend class hard_event;
friend std::set<cl_image_format>
- supported_formats(cl_context, cl_mem_object_type);
+ supported_formats(const context &, cl_mem_object_type);
clover::platform &platform;
namespace clover {
typedef struct _cl_command_queue command_queue;
- typedef struct _cl_context context;
+ class context;
class device;
typedef struct _cl_event event;
class hard_event;
}
std::set<cl_image_format>
- supported_formats(cl_context ctx, cl_mem_object_type type) {
+ supported_formats(const context &ctx, cl_mem_object_type type) {
std::set<cl_image_format> s;
pipe_texture_target target = translate_target(type);
unsigned bindings = (PIPE_BIND_SAMPLER_VIEW |
PIPE_BIND_TRANSFER_WRITE);
for (auto f : formats) {
- if (std::all_of(ctx->devs.begin(), ctx->devs.end(),
+ if (std::all_of(ctx.devs.begin(), ctx.devs.end(),
[=](const device *dev) {
return dev->pipe->is_format_supported(
dev->pipe, f.second, target, 1, bindings);
/// Return all the image formats supported by a given context for
/// the given memory object type.
///
- std::set<cl_image_format> supported_formats(cl_context ctx,
+ std::set<cl_image_format> supported_formats(const context &ctx,
cl_mem_object_type type);
}
}
}
+struct _cl_context :
+ public clover::descriptor<clover::context, _cl_context> {};
+
struct _cl_device_id :
public clover::descriptor<clover::device, _cl_device_id> {};