2012-04-20 16:56:19 +02:00
|
|
|
//
|
|
|
|
|
// Copyright 2012 Francisco Jerez
|
|
|
|
|
//
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
// copy of this software and associated documentation files (the "Software"),
|
|
|
|
|
// to deal in the Software without restriction, including without limitation
|
|
|
|
|
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
|
// and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
|
// Software is furnished to do so, subject to the following conditions:
|
|
|
|
|
//
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
|
//
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
2013-04-21 13:52:08 -07:00
|
|
|
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
|
|
|
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
|
|
|
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
|
|
|
// OTHER DEALINGS IN THE SOFTWARE.
|
2012-04-20 16:56:19 +02:00
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#include "api/util.hpp"
|
|
|
|
|
#include "core/context.hpp"
|
2013-09-16 21:13:47 -07:00
|
|
|
#include "core/platform.hpp"
|
2012-04-20 16:56:19 +02:00
|
|
|
|
|
|
|
|
using namespace clover;
|
|
|
|
|
|
2013-10-06 13:52:02 -07:00
|
|
|
CLOVER_API cl_context
|
2013-09-15 20:50:30 -07:00
|
|
|
clCreateContext(const cl_context_properties *d_props, cl_uint num_devs,
|
2013-09-15 20:06:57 -07:00
|
|
|
const cl_device_id *d_devs,
|
2012-04-20 16:56:19 +02:00
|
|
|
void (CL_CALLBACK *pfn_notify)(const char *, const void *,
|
|
|
|
|
size_t, void *),
|
2013-09-15 20:50:30 -07:00
|
|
|
void *user_data, cl_int *r_errcode) try {
|
2013-09-16 21:13:47 -07:00
|
|
|
auto props = obj<property_list_tag>(d_props);
|
2013-09-15 20:50:30 -07:00
|
|
|
auto devs = objs(d_devs, num_devs);
|
2012-04-20 16:56:19 +02:00
|
|
|
|
2013-09-15 20:06:57 -07:00
|
|
|
if (!pfn_notify && user_data)
|
2012-04-20 16:56:19 +02:00
|
|
|
throw error(CL_INVALID_VALUE);
|
|
|
|
|
|
2013-09-16 21:13:47 -07:00
|
|
|
for (auto &prop : props) {
|
|
|
|
|
if (prop.first == CL_CONTEXT_PLATFORM)
|
2021-08-05 16:34:51 +10:00
|
|
|
find_platform(prop.second.as<cl_platform_id>());
|
2013-09-16 21:13:47 -07:00
|
|
|
else
|
2012-04-20 16:56:19 +02:00
|
|
|
throw error(CL_INVALID_PROPERTY);
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-30 23:25:59 -04:00
|
|
|
const auto notify = (!pfn_notify ? context::notify_action() :
|
|
|
|
|
[=](const char *s) {
|
|
|
|
|
pfn_notify(s, NULL, 0, user_data);
|
|
|
|
|
});
|
|
|
|
|
|
2013-09-15 20:50:30 -07:00
|
|
|
ret_error(r_errcode, CL_SUCCESS);
|
2015-10-30 23:25:59 -04:00
|
|
|
return desc(new context(props, devs, notify));
|
2012-04-20 16:56:19 +02:00
|
|
|
|
2013-09-15 20:50:30 -07:00
|
|
|
} catch (error &e) {
|
|
|
|
|
ret_error(r_errcode, e);
|
2012-04-20 16:56:19 +02:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-06 13:52:02 -07:00
|
|
|
CLOVER_API cl_context
|
2013-09-15 20:50:30 -07:00
|
|
|
clCreateContextFromType(const cl_context_properties *d_props,
|
2012-04-20 16:56:19 +02:00
|
|
|
cl_device_type type,
|
|
|
|
|
void (CL_CALLBACK *pfn_notify)(
|
|
|
|
|
const char *, const void *, size_t, void *),
|
2013-09-15 20:50:30 -07:00
|
|
|
void *user_data, cl_int *r_errcode) try {
|
|
|
|
|
cl_platform_id d_platform;
|
2013-04-06 14:35:00 +02:00
|
|
|
cl_uint num_platforms;
|
2012-04-20 16:56:19 +02:00
|
|
|
cl_int ret;
|
2013-04-11 10:37:55 -04:00
|
|
|
std::vector<cl_device_id> devs;
|
|
|
|
|
cl_uint num_devices;
|
2012-04-20 16:56:19 +02:00
|
|
|
|
2013-09-15 20:50:30 -07:00
|
|
|
ret = clGetPlatformIDs(1, &d_platform, &num_platforms);
|
2013-04-06 14:35:00 +02:00
|
|
|
if (ret || !num_platforms)
|
|
|
|
|
throw error(CL_INVALID_PLATFORM);
|
|
|
|
|
|
2013-04-11 10:37:55 -04:00
|
|
|
ret = clGetDeviceIDs(d_platform, type, 0, NULL, &num_devices);
|
|
|
|
|
if (ret)
|
|
|
|
|
throw error(CL_DEVICE_NOT_FOUND);
|
|
|
|
|
devs.resize(num_devices);
|
|
|
|
|
ret = clGetDeviceIDs(d_platform, type, num_devices, devs.data(), 0);
|
2013-04-06 14:35:00 +02:00
|
|
|
if (ret)
|
|
|
|
|
throw error(CL_DEVICE_NOT_FOUND);
|
2012-04-20 16:56:19 +02:00
|
|
|
|
2013-04-11 10:37:55 -04:00
|
|
|
return clCreateContext(d_props, num_devices, devs.data(), pfn_notify,
|
|
|
|
|
user_data, r_errcode);
|
2013-04-06 14:35:00 +02:00
|
|
|
|
2013-09-15 20:50:30 -07:00
|
|
|
} catch (error &e) {
|
|
|
|
|
ret_error(r_errcode, e);
|
2013-04-06 14:35:00 +02:00
|
|
|
return NULL;
|
2012-04-20 16:56:19 +02:00
|
|
|
}
|
|
|
|
|
|
2013-10-06 13:52:02 -07:00
|
|
|
CLOVER_API cl_int
|
2013-09-15 20:50:30 -07:00
|
|
|
clRetainContext(cl_context d_ctx) try {
|
|
|
|
|
obj(d_ctx).retain();
|
2012-04-20 16:56:19 +02:00
|
|
|
return CL_SUCCESS;
|
2013-09-15 20:50:30 -07:00
|
|
|
|
|
|
|
|
} catch (error &e) {
|
|
|
|
|
return e.get();
|
2012-04-20 16:56:19 +02:00
|
|
|
}
|
|
|
|
|
|
2013-10-06 13:52:02 -07:00
|
|
|
CLOVER_API cl_int
|
2013-09-15 20:50:30 -07:00
|
|
|
clReleaseContext(cl_context d_ctx) try {
|
|
|
|
|
if (obj(d_ctx).release())
|
|
|
|
|
delete pobj(d_ctx);
|
2012-04-20 16:56:19 +02:00
|
|
|
|
|
|
|
|
return CL_SUCCESS;
|
2013-09-15 20:50:30 -07:00
|
|
|
|
|
|
|
|
} catch (error &e) {
|
|
|
|
|
return e.get();
|
2012-04-20 16:56:19 +02:00
|
|
|
}
|
|
|
|
|
|
2013-10-06 13:52:02 -07:00
|
|
|
CLOVER_API cl_int
|
2013-09-15 20:50:30 -07:00
|
|
|
clGetContextInfo(cl_context d_ctx, cl_context_info param,
|
2013-10-06 13:49:49 -07:00
|
|
|
size_t size, void *r_buf, size_t *r_size) try {
|
|
|
|
|
property_buffer buf { r_buf, size, r_size };
|
2013-09-15 20:50:30 -07:00
|
|
|
auto &ctx = obj(d_ctx);
|
2012-04-20 16:56:19 +02:00
|
|
|
|
|
|
|
|
switch (param) {
|
|
|
|
|
case CL_CONTEXT_REFERENCE_COUNT:
|
2013-09-15 20:50:30 -07:00
|
|
|
buf.as_scalar<cl_uint>() = ctx.ref_count();
|
2013-10-06 13:49:49 -07:00
|
|
|
break;
|
2012-04-20 16:56:19 +02:00
|
|
|
|
|
|
|
|
case CL_CONTEXT_NUM_DEVICES:
|
2014-02-18 13:12:52 +01:00
|
|
|
buf.as_scalar<cl_uint>() = ctx.devices().size();
|
2013-10-06 13:49:49 -07:00
|
|
|
break;
|
2012-04-20 16:56:19 +02:00
|
|
|
|
|
|
|
|
case CL_CONTEXT_DEVICES:
|
2014-02-18 13:12:52 +01:00
|
|
|
buf.as_vector<cl_device_id>() = descs(ctx.devices());
|
2013-10-06 13:49:49 -07:00
|
|
|
break;
|
2012-04-20 16:56:19 +02:00
|
|
|
|
|
|
|
|
case CL_CONTEXT_PROPERTIES:
|
2014-02-18 13:12:52 +01:00
|
|
|
buf.as_vector<cl_context_properties>() = desc(ctx.properties());
|
2013-10-06 13:49:49 -07:00
|
|
|
break;
|
2012-04-20 16:56:19 +02:00
|
|
|
|
|
|
|
|
default:
|
2013-10-06 13:49:49 -07:00
|
|
|
throw error(CL_INVALID_VALUE);
|
2012-04-20 16:56:19 +02:00
|
|
|
}
|
2013-10-06 13:49:49 -07:00
|
|
|
|
|
|
|
|
return CL_SUCCESS;
|
|
|
|
|
|
|
|
|
|
} catch (error &e) {
|
|
|
|
|
return e.get();
|
2012-04-20 16:56:19 +02:00
|
|
|
}
|
2020-11-06 15:08:51 +10:00
|
|
|
|
|
|
|
|
CLOVER_API cl_int
|
|
|
|
|
clSetContextDestructorCallback(cl_context d_ctx,
|
|
|
|
|
void (CL_CALLBACK *pfn_notify)(cl_context, void *),
|
|
|
|
|
void *user_data) try {
|
|
|
|
|
CLOVER_NOT_SUPPORTED_UNTIL("3.0");
|
|
|
|
|
auto &ctx = obj(d_ctx);
|
|
|
|
|
|
|
|
|
|
if (!pfn_notify)
|
|
|
|
|
return CL_INVALID_VALUE;
|
|
|
|
|
|
|
|
|
|
ctx.destroy_notify([=]{ pfn_notify(d_ctx, user_data); });
|
|
|
|
|
|
|
|
|
|
return CL_SUCCESS;
|
|
|
|
|
|
|
|
|
|
} catch (error &e) {
|
|
|
|
|
return e.get();
|
|
|
|
|
}
|