clover: Switch context objects to the new model.

Tested-by: Tom Stellard <thomas.stellard@amd.com>
This commit is contained in:
Francisco Jerez 2013-09-15 20:50:30 -07:00
parent c9e009b74d
commit 9d06fb8fa8
13 changed files with 94 additions and 106 deletions

View file

@ -26,99 +26,97 @@
using namespace clover; using namespace clover;
PUBLIC cl_context 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, const cl_device_id *d_devs,
void (CL_CALLBACK *pfn_notify)(const char *, const void *, void (CL_CALLBACK *pfn_notify)(const char *, const void *,
size_t, void *), size_t, void *),
void *user_data, cl_int *errcode_ret) try { void *user_data, cl_int *r_errcode) try {
auto devs = map(addresses(), objs(d_devs, num_devs)); auto props = property_map(d_props);
auto mprops = property_map(props); auto devs = objs(d_devs, num_devs);
if (!pfn_notify && user_data) if (!pfn_notify && user_data)
throw error(CL_INVALID_VALUE); throw error(CL_INVALID_VALUE);
for (auto p : mprops) { for (auto prop : props) {
if (p.first != CL_CONTEXT_PLATFORM) if (prop.first != CL_CONTEXT_PLATFORM)
throw error(CL_INVALID_PROPERTY); throw error(CL_INVALID_PROPERTY);
} }
ret_error(errcode_ret, CL_SUCCESS); ret_error(r_errcode, CL_SUCCESS);
return new context(property_vector(mprops), devs); return desc(new context(property_vector(props), devs));
} catch(error &e) { } catch (error &e) {
ret_error(errcode_ret, e); ret_error(r_errcode, e);
return NULL; return NULL;
} }
PUBLIC cl_context PUBLIC cl_context
clCreateContextFromType(const cl_context_properties *props, clCreateContextFromType(const cl_context_properties *d_props,
cl_device_type type, cl_device_type type,
void (CL_CALLBACK *pfn_notify)( void (CL_CALLBACK *pfn_notify)(
const char *, const void *, size_t, void *), const char *, const void *, size_t, void *),
void *user_data, cl_int *errcode_ret) try { void *user_data, cl_int *r_errcode) try {
cl_platform_id platform; cl_platform_id d_platform;
cl_uint num_platforms; cl_uint num_platforms;
cl_device_id dev; cl_device_id d_dev;
cl_int ret; cl_int ret;
ret = clGetPlatformIDs(1, &platform, &num_platforms); ret = clGetPlatformIDs(1, &d_platform, &num_platforms);
if (ret || !num_platforms) if (ret || !num_platforms)
throw error(CL_INVALID_PLATFORM); throw error(CL_INVALID_PLATFORM);
ret = clGetDeviceIDs(platform, type, 1, &dev, 0); ret = clGetDeviceIDs(d_platform, type, 1, &d_dev, 0);
if (ret) if (ret)
throw error(CL_DEVICE_NOT_FOUND); 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) { } catch (error &e) {
ret_error(errcode_ret, e); ret_error(r_errcode, e);
return NULL; return NULL;
} }
PUBLIC cl_int PUBLIC cl_int
clRetainContext(cl_context ctx) { clRetainContext(cl_context d_ctx) try {
if (!ctx) obj(d_ctx).retain();
return CL_INVALID_CONTEXT;
ctx->retain();
return CL_SUCCESS; return CL_SUCCESS;
} catch (error &e) {
return e.get();
} }
PUBLIC cl_int PUBLIC cl_int
clReleaseContext(cl_context ctx) { clReleaseContext(cl_context d_ctx) try {
if (!ctx) if (obj(d_ctx).release())
return CL_INVALID_CONTEXT; delete pobj(d_ctx);
if (ctx->release())
delete ctx;
return CL_SUCCESS; return CL_SUCCESS;
} catch (error &e) {
return e.get();
} }
PUBLIC cl_int 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 { size_t size, void *r_buf, size_t *r_size) try {
property_buffer buf { r_buf, size, r_size }; property_buffer buf { r_buf, size, r_size };
auto &ctx = obj(d_ctx);
if (!ctx)
return CL_INVALID_CONTEXT;
switch (param) { switch (param) {
case CL_CONTEXT_REFERENCE_COUNT: case CL_CONTEXT_REFERENCE_COUNT:
buf.as_scalar<cl_uint>() = ctx->ref_count(); buf.as_scalar<cl_uint>() = ctx.ref_count();
break; break;
case CL_CONTEXT_NUM_DEVICES: case CL_CONTEXT_NUM_DEVICES:
buf.as_scalar<cl_uint>() = ctx->devs.size(); buf.as_scalar<cl_uint>() = ctx.devs.size();
break; break;
case CL_CONTEXT_DEVICES: case CL_CONTEXT_DEVICES:
buf.as_vector<cl_device_id>() = ctx->devs; buf.as_vector<cl_device_id>() = descs(map(derefs(), ctx.devs));
break; break;
case CL_CONTEXT_PROPERTIES: case CL_CONTEXT_PROPERTIES:
buf.as_vector<cl_context_properties>() = ctx->props(); buf.as_vector<cl_context_properties>() = ctx.props();
break; break;
default: default:

View file

@ -26,12 +26,11 @@
using namespace clover; using namespace clover;
PUBLIC cl_event PUBLIC cl_event
clCreateUserEvent(cl_context ctx, cl_int *errcode_ret) try { clCreateUserEvent(cl_context d_ctx, cl_int *errcode_ret) try {
if (!ctx) auto &ctx = obj(d_ctx);
throw error(CL_INVALID_CONTEXT);
ret_error(errcode_ret, CL_SUCCESS); ret_error(errcode_ret, CL_SUCCESS);
return new soft_event(*ctx, {}, false); return new soft_event(ctx, {}, false);
} catch(error &e) { } catch(error &e) {
ret_error(errcode_ret, e); ret_error(errcode_ret, e);

View file

@ -45,7 +45,7 @@ clCreateBuffer(cl_context ctx, cl_mem_flags flags, size_t size,
throw error(CL_INVALID_VALUE); throw error(CL_INVALID_VALUE);
ret_error(errcode_ret, CL_SUCCESS); 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) { } catch (error &e) {
ret_error(errcode_ret, e); ret_error(errcode_ret, e);
@ -91,12 +91,11 @@ clCreateSubBuffer(cl_mem obj, cl_mem_flags flags, cl_buffer_create_type op,
} }
PUBLIC cl_mem 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, const cl_image_format *format,
size_t width, size_t height, size_t row_pitch, size_t width, size_t height, size_t row_pitch,
void *host_ptr, cl_int *errcode_ret) try { void *host_ptr, cl_int *errcode_ret) try {
if (!ctx) auto &ctx = obj(d_ctx);
throw error(CL_INVALID_CONTEXT);
if (flags & ~(CL_MEM_READ_WRITE | CL_MEM_WRITE_ONLY | CL_MEM_READ_ONLY | if (flags & ~(CL_MEM_READ_WRITE | CL_MEM_WRITE_ONLY | CL_MEM_READ_ONLY |
CL_MEM_USE_HOST_PTR | CL_MEM_ALLOC_HOST_PTR | CL_MEM_USE_HOST_PTR | CL_MEM_ALLOC_HOST_PTR |
@ -117,7 +116,7 @@ clCreateImage2D(cl_context ctx, cl_mem_flags flags,
throw error(CL_IMAGE_FORMAT_NOT_SUPPORTED); throw error(CL_IMAGE_FORMAT_NOT_SUPPORTED);
ret_error(errcode_ret, CL_SUCCESS); 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); row_pitch, host_ptr);
} catch (error &e) { } catch (error &e) {
@ -126,13 +125,12 @@ clCreateImage2D(cl_context ctx, cl_mem_flags flags,
} }
PUBLIC cl_mem 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, const cl_image_format *format,
size_t width, size_t height, size_t depth, size_t width, size_t height, size_t depth,
size_t row_pitch, size_t slice_pitch, size_t row_pitch, size_t slice_pitch,
void *host_ptr, cl_int *errcode_ret) try { void *host_ptr, cl_int *errcode_ret) try {
if (!ctx) auto &ctx = obj(d_ctx);
throw error(CL_INVALID_CONTEXT);
if (flags & ~(CL_MEM_READ_WRITE | CL_MEM_WRITE_ONLY | CL_MEM_READ_ONLY | if (flags & ~(CL_MEM_READ_WRITE | CL_MEM_WRITE_ONLY | CL_MEM_READ_ONLY |
CL_MEM_USE_HOST_PTR | CL_MEM_ALLOC_HOST_PTR | CL_MEM_USE_HOST_PTR | CL_MEM_ALLOC_HOST_PTR |
@ -153,7 +151,7 @@ clCreateImage3D(cl_context ctx, cl_mem_flags flags,
throw error(CL_IMAGE_FORMAT_NOT_SUPPORTED); throw error(CL_IMAGE_FORMAT_NOT_SUPPORTED);
ret_error(errcode_ret, CL_SUCCESS); 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); row_pitch, slice_pitch, host_ptr);
} catch (error &e) { } catch (error &e) {
@ -162,11 +160,10 @@ clCreateImage3D(cl_context ctx, cl_mem_flags flags,
} }
PUBLIC cl_int 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_mem_object_type type, cl_uint count,
cl_image_format *buf, cl_uint *count_ret) try { cl_image_format *buf, cl_uint *count_ret) try {
if (!ctx) auto &ctx = obj(d_ctx);
throw error(CL_INVALID_CONTEXT);
if (flags & ~(CL_MEM_READ_WRITE | CL_MEM_WRITE_ONLY | CL_MEM_READ_ONLY | if (flags & ~(CL_MEM_READ_WRITE | CL_MEM_WRITE_ONLY | CL_MEM_READ_ONLY |
CL_MEM_USE_HOST_PTR | CL_MEM_ALLOC_HOST_PTR | CL_MEM_USE_HOST_PTR | CL_MEM_ALLOC_HOST_PTR |

View file

@ -26,14 +26,12 @@
using namespace clover; using namespace clover;
PUBLIC cl_program 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, const char **strings, const size_t *lengths,
cl_int *errcode_ret) try { cl_int *errcode_ret) try {
auto &ctx = obj(d_ctx);
std::string source; std::string source;
if (!ctx)
throw error(CL_INVALID_CONTEXT);
if (!count || !strings || if (!count || !strings ||
any_of(is_zero(), range(strings, count))) any_of(is_zero(), range(strings, count)))
throw error(CL_INVALID_VALUE); throw error(CL_INVALID_VALUE);
@ -46,7 +44,7 @@ clCreateProgramWithSource(cl_context ctx, cl_uint count,
// ...and create a program object for them. // ...and create a program object for them.
ret_error(errcode_ret, CL_SUCCESS); ret_error(errcode_ret, CL_SUCCESS);
return new program(*ctx, source); return new program(ctx, source);
} catch (error &e) { } catch (error &e) {
ret_error(errcode_ret, e); ret_error(errcode_ret, e);
@ -54,20 +52,18 @@ clCreateProgramWithSource(cl_context ctx, cl_uint count,
} }
PUBLIC cl_program 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 cl_device_id *d_devs, const size_t *lengths,
const unsigned char **binaries, cl_int *status_ret, const unsigned char **binaries, cl_int *status_ret,
cl_int *errcode_ret) try { cl_int *errcode_ret) try {
auto &ctx = obj(d_ctx);
auto devs = objs(d_devs, n); auto devs = objs(d_devs, n);
if (!ctx)
throw error(CL_INVALID_CONTEXT);
if (!lengths || !binaries) if (!lengths || !binaries)
throw error(CL_INVALID_VALUE); throw error(CL_INVALID_VALUE);
if (any_of([&](device &dev) { if (any_of([&](device &dev) {
return !ctx->has_device(&dev); return !ctx.has_device(dev);
}, devs)) }, devs))
throw error(CL_INVALID_DEVICE); throw error(CL_INVALID_DEVICE);
@ -102,7 +98,7 @@ clCreateProgramWithBinary(cl_context ctx, cl_uint n,
// initialize a program object with them. // initialize a program object with them.
ret_error(errcode_ret, CL_SUCCESS); 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) { } catch (error &e) {
ret_error(errcode_ret, e); ret_error(errcode_ret, e);
@ -145,7 +141,7 @@ clBuildProgram(cl_program prog, cl_uint count, const cl_device_id *devs,
if (devs) { if (devs) {
if (any_of([&](const cl_device_id dev) { 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))) }, range(devs, count)))
throw error(CL_INVALID_DEVICE); throw error(CL_INVALID_DEVICE);
@ -235,7 +231,7 @@ clGetProgramBuildInfo(cl_program prog, cl_device_id dev,
if (!prog) if (!prog)
return CL_INVALID_PROGRAM; return CL_INVALID_PROGRAM;
if (!prog->ctx.has_device(pobj(dev))) if (!prog->ctx.has_device(obj(dev)))
return CL_INVALID_DEVICE; return CL_INVALID_DEVICE;
switch (param) { switch (param) {

View file

@ -26,15 +26,13 @@
using namespace clover; using namespace clover;
PUBLIC cl_command_queue 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_command_queue_properties props,
cl_int *errcode_ret) try { cl_int *errcode_ret) try {
auto &ctx = obj(d_ctx);
auto &dev = obj(d_dev); auto &dev = obj(d_dev);
if (!ctx) if (!ctx.has_device(dev))
throw error(CL_INVALID_CONTEXT);
if (!ctx->has_device(&dev))
throw error(CL_INVALID_DEVICE); throw error(CL_INVALID_DEVICE);
if (props & ~(CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE | if (props & ~(CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE |
@ -42,7 +40,7 @@ clCreateCommandQueue(cl_context ctx, cl_device_id d_dev,
throw error(CL_INVALID_VALUE); throw error(CL_INVALID_VALUE);
ret_error(errcode_ret, CL_SUCCESS); ret_error(errcode_ret, CL_SUCCESS);
return new command_queue(*ctx, dev, props); return new command_queue(ctx, dev, props);
} catch (error &e) { } catch (error &e) {
ret_error(errcode_ret, e); ret_error(errcode_ret, e);

View file

@ -26,14 +26,13 @@
using namespace clover; using namespace clover;
PUBLIC cl_sampler 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_addressing_mode addr_mode, cl_filter_mode filter_mode,
cl_int *errcode_ret) try { cl_int *errcode_ret) try {
if (!ctx) auto &ctx = obj(d_ctx);
throw error(CL_INVALID_CONTEXT);
ret_error(errcode_ret, CL_SUCCESS); 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) { } catch (error &e) {
ret_error(errcode_ret, e); ret_error(errcode_ret, e);

View file

@ -26,12 +26,12 @@
using namespace clover; using namespace clover;
_cl_context::_cl_context(const std::vector<cl_context_properties> &props, context::context(const std::vector<cl_context_properties> &props,
const std::vector<device *> &devs) : const ref_vector<device> &devs) :
devs(devs), _props(props) { devs(map(addresses(), devs)), _props(props) {
} }
bool bool
_cl_context::has_device(clover::device *dev) const { context::has_device(device &dev) const {
return std::count(devs.begin(), devs.end(), dev); return std::count(devs.begin(), devs.end(), &dev);
} }

View file

@ -27,25 +27,23 @@
#include "core/device.hpp" #include "core/device.hpp"
namespace clover { namespace clover {
typedef struct _cl_context context; 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(device &dev) const;
const std::vector<cl_context_properties> &props() const {
return _props;
}
const std::vector<device *> devs;
private:
std::vector<cl_context_properties> _props;
};
} }
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;
bool has_device(clover::device *dev) const;
const std::vector<cl_context_properties> &props() const {
return _props;
}
const std::vector<clover::device *> devs;
private:
std::vector<cl_context_properties> _props;
};
#endif #endif

View file

@ -70,7 +70,7 @@ namespace clover {
friend class root_resource; friend class root_resource;
friend class hard_event; friend class hard_event;
friend std::set<cl_image_format> 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; clover::platform &platform;

View file

@ -29,7 +29,7 @@
namespace clover { namespace clover {
typedef struct _cl_command_queue command_queue; typedef struct _cl_command_queue command_queue;
typedef struct _cl_context context; class context;
class device; class device;
typedef struct _cl_event event; typedef struct _cl_event event;
class hard_event; class hard_event;

View file

@ -145,7 +145,7 @@ namespace clover {
} }
std::set<cl_image_format> 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; std::set<cl_image_format> s;
pipe_texture_target target = translate_target(type); pipe_texture_target target = translate_target(type);
unsigned bindings = (PIPE_BIND_SAMPLER_VIEW | unsigned bindings = (PIPE_BIND_SAMPLER_VIEW |
@ -154,7 +154,7 @@ namespace clover {
PIPE_BIND_TRANSFER_WRITE); PIPE_BIND_TRANSFER_WRITE);
for (auto f : formats) { 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) { [=](const device *dev) {
return dev->pipe->is_format_supported( return dev->pipe->is_format_supported(
dev->pipe, f.second, target, 1, bindings); dev->pipe, f.second, target, 1, bindings);

View file

@ -37,7 +37,7 @@ namespace clover {
/// Return all the image formats supported by a given context for /// Return all the image formats supported by a given context for
/// the given memory object type. /// 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); cl_mem_object_type type);
} }

View file

@ -179,6 +179,9 @@ namespace clover {
} }
} }
struct _cl_context :
public clover::descriptor<clover::context, _cl_context> {};
struct _cl_device_id : struct _cl_device_id :
public clover::descriptor<clover::device, _cl_device_id> {}; public clover::descriptor<clover::device, _cl_device_id> {};