Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implement cl_create_context().

  • Loading branch information...
commit 2a040e8fd40fb45a38886b0d4c0c1d70df1933d1 1 parent 1cea64a
@rsky authored
View
2  .gitignore
@@ -31,7 +31,7 @@ run-tests.php
# Numerous always-ignore extensions
###################
*.diff
-*.err
+*.errcode
*.orig
*.log
*.rej
View
83 context.c
@@ -12,6 +12,11 @@
#define get_info ((phpcl_get_info_func_t)_get_context_info)
#define get_info_ex NULL
+/* {{{ type definitions */
+
+typedef void (*create_context_callback_func_t)(const char *, const void *, size_t, void *);
+
+/* }}} */
/* {{{ globals */
static const phpcl_info_param_t context_info_params[] = {
@@ -105,6 +110,84 @@ PHP_FUNCTION(cl_get_context_info)
}
/* }}} */
+/* {{{ resource cl_context cl_create_context(mixed device[, array properties[, callback callback[, mixed user_data[, int &errcode]]]] */
+
+PHP_FUNCTION(cl_create_context)
+{
+ cl_int errcode = CL_SUCCESS;
+ cl_context context = NULL;
+ zval *zdevice = NULL;
+ cl_device_id *devices_ptr = NULL;
+ cl_device_id devices[1] = {NULL};
+ cl_uint num_devices = 0;
+ HashTable *properties_ht = NULL;
+ cl_context_properties *properties = NULL;
+ zval *zcallback = NULL;
+ create_context_callback_func_t pfn_notify = NULL;
+ zval *zdata = NULL;
+ void *user_data = NULL;
+ zval *zerrcode = NULL;
+
+ RETVAL_FALSE;
+
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
+ "z/|h!z/z/z", &zdevice, &properties_ht,
+ &zcallback, &zdata, &zerrcode) == FAILURE) {
+ return;
+ }
+
+ if (Z_TYPE_P(zdevice) == IS_RESOURCE) {
+ cl_device_id device;
+ ZEND_FETCH_RESOURCE(device, cl_device_id, &zdevice, -1,
+ "cl_device", phpcl_le_device());
+ devices[0] = device;
+ devices_ptr = devices;
+ num_devices = 1;
+ } else {
+ /* TODO: support multiple devices */
+ php_error(E_WARNING,
+ "%s() expects parameter 1 to be a valid resource",
+ get_active_function_name(TSRMLS_C));
+ return;
+ }
+
+ if (properties_ht) {
+ /* TODO: support properties */
+ }
+
+ if (zcallback) {
+ if (!zend_is_callable(zcallback, 0, NULL TSRMLS_CC)) {
+ php_error(E_WARNING,
+ "%s() expects parameter 3 to be a valid callback",
+ get_active_function_name(TSRMLS_C));
+ return;
+ }
+ }
+
+ context = clCreateContext(properties, num_devices, devices_ptr,
+ pfn_notify, user_data, &errcode);
+
+ if (zerrcode) {
+ zval_dtor(zerrcode);
+ ZVAL_LONG(zerrcode, (long)errcode);
+ }
+
+ if (context) {
+ phpcl_context_t *ctx = emalloc(sizeof(phpcl_context_t));
+ ctx->context = context;
+ if (zcallback) {
+ Z_ADDREF_P(zcallback);
+ ctx->callback = zcallback;
+ if (zdata) {
+ Z_ADDREF_P(zdata);
+ ctx->data = zdata;
+ }
+ }
+ ZEND_REGISTER_RESOURCE(return_value, ctx, phpcl_le_context());
+ }
+}
+
+/* }}} */
/*
* Local variables:
View
1  context.h
@@ -13,6 +13,7 @@
#define PHPCL_CONTEXT_H
PHP_FUNCTION(cl_get_context_info);
+PHP_FUNCTION(cl_create_context);
#endif
View
24 device.c
@@ -112,7 +112,7 @@ static zval *_get_device_info_ex(cl_device_id device,
void *reserved,
cl_device_info name TSRMLS_DC)
{
- cl_int err = CL_SUCCESS;
+ cl_int errcode = CL_SUCCESS;
zval *zinfo;
MAKE_STD_ZVAL(zinfo);
@@ -120,9 +120,9 @@ static zval *_get_device_info_ex(cl_device_id device,
cl_uint max_work_item_dimensions = 0;
size_t siz, *sizes;
- err = clGetDeviceInfo(device, CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS,
+ errcode = clGetDeviceInfo(device, CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS,
sizeof(cl_uint), &max_work_item_dimensions, NULL);
- if (err != CL_SUCCESS) {
+ if (errcode != CL_SUCCESS) {
return zinfo;
}
@@ -132,8 +132,8 @@ static zval *_get_device_info_ex(cl_device_id device,
return zinfo;
}
- err = clGetDeviceInfo(device, CL_DEVICE_MAX_WORK_ITEM_SIZES, siz, sizes, NULL);
- if (err == CL_SUCCESS) {
+ errcode = clGetDeviceInfo(device, CL_DEVICE_MAX_WORK_ITEM_SIZES, siz, sizes, NULL);
+ if (errcode == CL_SUCCESS) {
cl_uint i;
array_init_size(zinfo, max_work_item_dimensions);
for (i = 0; i < max_work_item_dimensions; i++) {
@@ -231,7 +231,7 @@ PHP_FUNCTION(cl_get_device_ids)
long ltype = 0L;
cl_device_type device_type = CL_DEVICE_TYPE_DEFAULT;
- cl_int err = CL_SUCCESS;
+ cl_int errcode = CL_SUCCESS;
cl_uint num_entries = 0;
cl_device_id *devices = NULL;
cl_uint index = 0;
@@ -250,10 +250,10 @@ PHP_FUNCTION(cl_get_device_ids)
device_type = (cl_device_type)ltype;
}
- err = clGetDeviceIDs(platform, device_type, 0, NULL, &num_entries);
- if (err != CL_SUCCESS) {
+ errcode = clGetDeviceIDs(platform, device_type, 0, NULL, &num_entries);
+ if (errcode != CL_SUCCESS) {
php_error_docref(NULL TSRMLS_CC, E_WARNING,
- "clGetDeviceIDs() failed [%s]", phpcl_errstr(err));
+ "clGetDeviceIDs() failed [%s]", phpcl_errstr(errcode));
efree(devices);
return;
}
@@ -263,10 +263,10 @@ PHP_FUNCTION(cl_get_device_ids)
return;
}
- err = clGetDeviceIDs(platform, device_type, num_entries, devices, NULL);
- if (err != CL_SUCCESS) {
+ errcode = clGetDeviceIDs(platform, device_type, num_entries, devices, NULL);
+ if (errcode != CL_SUCCESS) {
php_error_docref(NULL TSRMLS_CC, E_WARNING,
- "clGetDeviceIDs() failed [%s]", phpcl_errstr(err));
+ "clGetDeviceIDs() failed [%s]", phpcl_errstr(errcode));
efree(devices);
return;
}
View
55 opencl.c
@@ -76,6 +76,14 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_get_context_info, ZEND_SEND_BY_VAL, ZEND_RETURN_V
ZEND_ARG_INFO(0, name)
ZEND_END_ARG_INFO()
+ZEND_BEGIN_ARG_INFO_EX(arginfo_create_context, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 1)
+ ZEND_ARG_INFO(0, device)
+ ZEND_ARG_ARRAY_INFO(0, properties, 1)
+ ZEND_ARG_INFO(0, callback)
+ ZEND_ARG_INFO(0, user_data)
+ ZEND_ARG_INFO(1, errcode)
+ZEND_END_ARG_INFO()
+
/* command queue */
ZEND_BEGIN_ARG_INFO_EX(arginfo_get_command_queue_info, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 1)
ZEND_ARG_INFO(0, command_queue)
@@ -129,6 +137,7 @@ static zend_function_entry phpcl_functions[] = {
PHP_FE(cl_get_device_ids, arginfo_get_device_ids)
/* context */
PHP_FE(cl_get_context_info, arginfo_get_context_info)
+ PHP_FE(cl_create_context, arginfo_create_context)
/* command queue */
PHP_FE(cl_get_command_queue_info, arginfo_get_command_queue_info)
/* mem */
@@ -196,9 +205,9 @@ static PHP_MINFO_FUNCTION(opencl)
/* }}} */
/* {{{ phpcl_errstr() */
-const char *phpcl_errstr(cl_int err)
+const char *phpcl_errstr(cl_int errcode)
{
- switch(err) {
+ switch(errcode) {
case CL_SUCCESS:
return "SUCCESS";
case CL_DEVICE_NOT_FOUND:
@@ -749,7 +758,15 @@ static void _register_resources(int module_number TSRMLS_DC)
static void _destroy_context(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
- clReleaseContext((cl_context)rsrc->ptr TSRMLS_CC);
+ phpcl_context_t *ctx = (phpcl_context_t *)rsrc->ptr;
+ clReleaseContext(ctx->context TSRMLS_CC);
+ if (ctx->callback) {
+ zval_ptr_dtor(&ctx->callback);
+ }
+ if (ctx->data) {
+ zval_ptr_dtor(&ctx->data);
+ }
+ efree(ctx);
}
static void _destroy_command_queue(zend_rsrc_list_entry *rsrc TSRMLS_DC)
@@ -790,7 +807,7 @@ zval *phpcl_get_info(phpcl_get_info_func_t get_info,
void *obj1, void *obj2,
const phpcl_info_param_t *param TSRMLS_DC)
{
- cl_int err = CL_SUCCESS;
+ cl_int errcode = CL_SUCCESS;
zval *zinfo = NULL;
if (param->type != INFO_TYPE_EXTRA) {
@@ -800,8 +817,8 @@ zval *phpcl_get_info(phpcl_get_info_func_t get_info,
switch (param->type) {
case INFO_TYPE_BITFIELD: {
cl_bitfield val = 0;
- err = get_info(obj1, obj2, param->name, sizeof(cl_bitfield), &val, NULL);
- if (err == CL_SUCCESS) {
+ errcode = get_info(obj1, obj2, param->name, sizeof(cl_bitfield), &val, NULL);
+ if (errcode == CL_SUCCESS) {
ZVAL_LONG(zinfo, (long)val);
}
}
@@ -809,8 +826,8 @@ zval *phpcl_get_info(phpcl_get_info_func_t get_info,
case INFO_TYPE_BOOL: {
cl_bool val = 0;
- err = get_info(obj1, obj2, param->name, sizeof(cl_bool), &val, NULL);
- if (err == CL_SUCCESS) {
+ errcode = get_info(obj1, obj2, param->name, sizeof(cl_bool), &val, NULL);
+ if (errcode == CL_SUCCESS) {
ZVAL_LONG(zinfo, (long)val);
}
}
@@ -818,8 +835,8 @@ zval *phpcl_get_info(phpcl_get_info_func_t get_info,
case INFO_TYPE_SIZE: {
size_t val = 0;
- err = get_info(obj1, obj2, param->name, sizeof(size_t), &val, NULL);
- if (err == CL_SUCCESS) {
+ errcode = get_info(obj1, obj2, param->name, sizeof(size_t), &val, NULL);
+ if (errcode == CL_SUCCESS) {
ZVAL_LONG(zinfo, (long)val);
}
}
@@ -827,8 +844,8 @@ zval *phpcl_get_info(phpcl_get_info_func_t get_info,
case INFO_TYPE_UINT: {
cl_uint val = 0;
- err = get_info(obj1, obj2, param->name, sizeof(cl_uint), &val, NULL);
- if (err == CL_SUCCESS) {
+ errcode = get_info(obj1, obj2, param->name, sizeof(cl_uint), &val, NULL);
+ if (errcode == CL_SUCCESS) {
ZVAL_LONG(zinfo, (long)val);
}
}
@@ -836,8 +853,8 @@ zval *phpcl_get_info(phpcl_get_info_func_t get_info,
case INFO_TYPE_ULONG: {
cl_ulong val = 0;
- err = get_info(obj1, obj2, param->name, sizeof(cl_ulong), &val, NULL);
- if (err == CL_SUCCESS) {
+ errcode = get_info(obj1, obj2, param->name, sizeof(cl_ulong), &val, NULL);
+ if (errcode == CL_SUCCESS) {
ZVAL_LONG(zinfo, (long)val);
}
}
@@ -846,8 +863,8 @@ zval *phpcl_get_info(phpcl_get_info_func_t get_info,
case INFO_TYPE_STRING: {
char buf[1024] = { 0 };
size_t len = 0;
- err = get_info(obj1, obj2, param->name, sizeof(buf), buf, &len);
- if (err == CL_SUCCESS) {
+ errcode = get_info(obj1, obj2, param->name, sizeof(buf), buf, &len);
+ if (errcode == CL_SUCCESS) {
ZVAL_STRINGL(zinfo, buf, len, 1);
}
}
@@ -855,8 +872,8 @@ zval *phpcl_get_info(phpcl_get_info_func_t get_info,
case INFO_TYPE_PLATFORM: {
cl_platform_id platform;
- err = get_info(obj1, obj2, param->name, sizeof(cl_platform_id), &platform, NULL);
- if (err == CL_SUCCESS) {
+ errcode = get_info(obj1, obj2, param->name, sizeof(cl_platform_id), &platform, NULL);
+ if (errcode == CL_SUCCESS) {
ZEND_REGISTER_RESOURCE(zinfo, platform, le_platform);
}
}
@@ -874,7 +891,7 @@ zval *phpcl_get_info(phpcl_get_info_func_t get_info,
ZVAL_NULL(zinfo);
}
- if (err != CL_SUCCESS) {
+ if (errcode != CL_SUCCESS) {
ZVAL_NULL(zinfo);
}
View
10 php_opencl.h
@@ -25,6 +25,14 @@
/* {{{ type definitions */
+typedef struct {
+ cl_context context;
+ zval *callback;
+ zval *data;
+ zend_fcall_info fci;
+ zend_fcall_info_cache fcc;
+} phpcl_context_t;
+
typedef enum {
INFO_TYPE_BITFIELD = 0,
INFO_TYPE_BOOL,
@@ -57,7 +65,7 @@ typedef zval * (*phpcl_get_info_ex_func_t)(void *, /* obj1 */
/* }}} */
/* {{{ common functions */
-const char *phpcl_errstr(cl_int err);
+const char *phpcl_errstr(cl_int errcode);
zval *phpcl_get_info(phpcl_get_info_func_t get_info,
phpcl_get_info_ex_func_t get_info_ex,
View
14 platform.c
@@ -114,7 +114,7 @@ PHP_FUNCTION(cl_get_platform_info)
PHP_FUNCTION(cl_get_platform_ids)
{
- cl_int err = CL_SUCCESS;
+ cl_int errcode = CL_SUCCESS;
cl_uint num_entries = 0;
cl_platform_id *platforms = NULL;
cl_uint index = 0;
@@ -125,10 +125,10 @@ PHP_FUNCTION(cl_get_platform_ids)
WRONG_PARAM_COUNT;
}
- err = clGetPlatformIDs(0, NULL, &num_entries);
- if (err != CL_SUCCESS) {
+ errcode = clGetPlatformIDs(0, NULL, &num_entries);
+ if (errcode != CL_SUCCESS) {
php_error_docref(NULL TSRMLS_CC, E_WARNING,
- "clGetPlatformIDs() failed [%s]", phpcl_errstr(err));
+ "clGetPlatformIDs() failed [%s]", phpcl_errstr(errcode));
}
platforms = ecalloc(num_entries, sizeof(cl_platform_id));
@@ -136,10 +136,10 @@ PHP_FUNCTION(cl_get_platform_ids)
return;
}
- err = clGetPlatformIDs(num_entries, platforms, NULL);
- if (err != CL_SUCCESS) {
+ errcode = clGetPlatformIDs(num_entries, platforms, NULL);
+ if (errcode != CL_SUCCESS) {
php_error_docref(NULL TSRMLS_CC, E_WARNING,
- "clGetPlatformIDs() failed [%s]", phpcl_errstr(err));
+ "clGetPlatformIDs() failed [%s]", phpcl_errstr(errcode));
efree(platforms);
return;
}
Please sign in to comment.
Something went wrong with that request. Please try again.