Permalink
Browse files

Implement cl_create_buffer().

  • Loading branch information...
1 parent 444a584 commit 46443c46190fc5af94991abbcabb0c33e48fe361 @rsky committed Sep 2, 2012
Showing with 96 additions and 7 deletions.
  1. +13 −2 examples/mandelbrot.php
  2. +58 −4 memory.c
  3. +1 −0 memory.h
  4. +19 −1 opencl.c
  5. +5 −0 php_opencl.h
View
@@ -1,23 +1,34 @@
<?php
+$width = 500;
+$height = 500;
+
$platforms = cl_get_platform_ids();
var_dump($platforms);
-//print_r(cl_get_platform_info($platforms[0]));
+print_r(cl_get_platform_info($platforms[0]));
$devices = cl_get_device_ids($platforms[0], CL_DEVICE_TYPE_ALL);
var_dump($devices);
-//print_r(cl_get_device_info($devices[0]));
+print_r(cl_get_device_info($devices[0]));
$context = cl_create_context($devices[0]);
var_dump($context);
+print_r(cl_get_context_info($context));
$queue = cl_create_command_queue($context);
var_dump($queue);
+print_r(cl_get_command_queue_info($queue));
$source = __DIR__ . '/mandelbrot.cl';
$program = cl_create_program_with_source($context, $source, $err);
var_dump($program, $err);
//cl_build_program($program, $devices[0]);
cl_build_program($program);
+print_r(cl_get_program_info($program));
$kernel = cl_create_kernel($program, 'Mandelbrot');
var_dump($kernel);
+print_r(cl_get_kernel_info($kernel));
+
+$memroy = cl_create_buffer($context, CL_MEM_WRITE_ONLY, $width * $height);
+var_dump($memroy);
+print_r(cl_get_mem_object_info($memroy));
View
@@ -90,7 +90,7 @@ static void _get_mem_object_info_by_name(
PHP_FUNCTION(cl_get_mem_object_info)
{
zval *zid = NULL;
- cl_mem memobj = NULL;
+ phpcl_memobj_t *mem = NULL;
long name = 0;
RETVAL_FALSE;
@@ -99,17 +99,71 @@ PHP_FUNCTION(cl_get_mem_object_info)
"r|l", &zid, &name) == FAILURE) {
return;
}
- ZEND_FETCH_RESOURCE(memobj, cl_mem, &zid, -1,
+ ZEND_FETCH_RESOURCE(mem, phpcl_memobj_t *, &zid, -1,
"cl_mem", phpcl_le_mem());
if (ZEND_NUM_ARGS() == 2) {
- _get_mem_object_info_by_name(INTERNAL_FUNCTION_PARAM_PASSTHRU, memobj, (cl_int)name);
+ _get_mem_object_info_by_name(INTERNAL_FUNCTION_PARAM_PASSTHRU, mem->memobj, (cl_int)name);
} else {
- _get_mem_object_info_all(INTERNAL_FUNCTION_PARAM_PASSTHRU, memobj);
+ _get_mem_object_info_all(INTERNAL_FUNCTION_PARAM_PASSTHRU, mem->memobj);
+ }
+}
+
+/* }}} */
+/* {{{ _check_mem_flags() */
+
+static void _check_mem_flags(long *flags_arg TSRMLS_DC)
+{
+ cl_mem_flags flags = (cl_mem_flags)(*flags_arg);
+ if (flags & CL_MEM_USE_HOST_PTR) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING,
+ "CL_MEM_USE_HOST_PTR flag is not yet supported.");
+ }
+ if (flags & CL_MEM_ALLOC_HOST_PTR) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING,
+ "CL_MEM_ALLOC_HOST_PTR flag is not yet supported.");
}
+ if (flags & CL_MEM_COPY_HOST_PTR) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING,
+ "CL_MEM_COPY_HOST_PTR flag is not yet supported.");
+ }
+ flags = flags & (CL_MEM_READ_WRITE | CL_MEM_WRITE_ONLY | CL_MEM_READ_ONLY);
+ *flags_arg = (long)flags;
}
/* }}} */
+/* {{{ resource cl_mem cl_create_buffer(resource cl_context context, int flags, int size) */
+
+PHP_FUNCTION(cl_create_buffer)
+{
+ cl_int errcode = CL_SUCCESS;
+ zval *zcontext = NULL;
+ phpcl_context_t *ctx = NULL;
+ long flags = 0;
+ long size = 0;
+ void *host_ptr = NULL;
+ cl_mem memobj = NULL;
+
+ RETVAL_FALSE;
+
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
+ "rll", &zcontext, &flags, &size) == FAILURE) {
+ return;
+ }
+ ZEND_FETCH_RESOURCE(ctx, phpcl_context_t *, &zcontext, -1,
+ "cl_context", phpcl_le_context());
+
+ _check_mem_flags(&flags TSRMLS_CC);
+
+ memobj = clCreateBuffer(ctx->context, (cl_mem_flags)flags, (size_t)size,
+ host_ptr, &errcode);
+ if (memobj) {
+ phpcl_memobj_t *mem = emalloc(sizeof(phpcl_memobj_t));
+ mem->memobj = memobj;
+ mem->ptr = host_ptr;
+ ZEND_REGISTER_RESOURCE(return_value, mem, phpcl_le_mem());
+ }
+}
/*
* Local variables:
View
@@ -14,6 +14,7 @@
PHP_FUNCTION(cl_get_mem_object_info);
/*PHP_FUNCTION(cl_get_image_info);*/
+PHP_FUNCTION(cl_create_buffer);
#endif
View
@@ -109,6 +109,12 @@ ZEND_END_ARG_INFO()
ZEND_ARG_INFO(0, name)
ZEND_END_ARG_INFO()*/
+ZEND_BEGIN_ARG_INFO_EX(arginfo_create_buffer, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 3)
+ ZEND_ARG_INFO(0, context)
+ ZEND_ARG_INFO(0, flags)
+ ZEND_ARG_INFO(0, size)
+ZEND_END_ARG_INFO()
+
/* program */
ZEND_BEGIN_ARG_INFO_EX(arginfo_get_program_info, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 1)
ZEND_ARG_INFO(0, program)
@@ -171,6 +177,7 @@ static zend_function_entry phpcl_functions[] = {
/* mem */
PHP_FE(cl_get_mem_object_info, arginfo_get_mem_object_info)
/*PHP_FE(cl_get_image_info, arginfo_get_image_info)*/
+ PHP_FE(cl_create_buffer, arginfo_create_buffer)
/* program */
PHP_FE(cl_get_program_info, arginfo_get_program_info)
PHP_FE(cl_create_program_with_source, arginfo_create_program_with_source)
@@ -798,16 +805,23 @@ static void _destroy_context(zend_rsrc_list_entry *rsrc TSRMLS_DC)
zval_ptr_dtor(&ctx->data);
}
efree(ctx);
+ rsrc->ptr = NULL;
}
static void _destroy_command_queue(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
clReleaseCommandQueue((cl_command_queue)rsrc->ptr);
+ rsrc->ptr = NULL;
}
static void _destroy_mem(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
- clReleaseMemObject((cl_mem)rsrc->ptr);
+ phpcl_memobj_t *mem = (phpcl_memobj_t *)rsrc->ptr;
+ clReleaseMemObject(mem->memobj);
+ if (mem->ptr) {
+ efree(mem->ptr);
+ }
+ rsrc->ptr = NULL;
}
static void _destroy_program(zend_rsrc_list_entry *rsrc TSRMLS_DC)
@@ -821,21 +835,25 @@ static void _destroy_program(zend_rsrc_list_entry *rsrc TSRMLS_DC)
zval_ptr_dtor(&prg->data);
}
efree(prg);
+ rsrc->ptr = NULL;
}
static void _destroy_kernel(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
clReleaseKernel((cl_kernel)rsrc->ptr TSRMLS_CC);
+ rsrc->ptr = NULL;
}
static void _destroy_event(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
clReleaseEvent((cl_event)rsrc->ptr);
+ rsrc->ptr = NULL;
}
static void _destroy_sampler(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
clReleaseSampler((cl_sampler)rsrc->ptr);
+ rsrc->ptr = NULL;
}
/* }}} */
View
@@ -32,6 +32,11 @@ typedef struct {
} phpcl_context_t;
typedef struct {
+ cl_mem memobj;
+ void *ptr;
+} phpcl_memobj_t;
+
+typedef struct {
cl_program program;
zval *callback;
zval *data;

0 comments on commit 46443c4

Please sign in to comment.