Skip to content

Commit

Permalink
use char16 to pass parameter names to host
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Tessore committed Jun 24, 2015
1 parent 31bb54d commit feff513
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 29 deletions.
6 changes: 3 additions & 3 deletions kernel/object.cl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ enum
// structure that holds parameter definition
struct param
{
char name[32];
int type;
float2 bounds;
constant char name[16];
int type;
float2 bounds;
};
30 changes: 12 additions & 18 deletions src/input/objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,14 @@ void add_object(input* inp, const char* id, const char* name)
cl_ulong meta_size;
cl_mem meta_npar_mem;
cl_ulong meta_npar;
cl_mem meta_nnam_mem;
cl_ulong meta_nnam;

// parameter info kernel
char* param_kernam;
cl_kernel param_kernel;

// parameter information
cl_mem param_names_mem;
cl_char* param_names;
cl_char16* param_names;
cl_mem param_types_mem;
cl_int* param_types;
cl_mem param_bounds_mem;
Expand Down Expand Up @@ -109,8 +107,7 @@ void add_object(input* inp, const char* id, const char* name)
meta_type_mem = clCreateBuffer(lcl->context, CL_MEM_WRITE_ONLY, sizeof(cl_int), NULL, NULL);
meta_size_mem = clCreateBuffer(lcl->context, CL_MEM_WRITE_ONLY, sizeof(cl_ulong), NULL, NULL);
meta_npar_mem = clCreateBuffer(lcl->context, CL_MEM_WRITE_ONLY, sizeof(cl_ulong), NULL, NULL);
meta_nnam_mem = clCreateBuffer(lcl->context, CL_MEM_WRITE_ONLY, sizeof(cl_ulong), NULL, NULL);
if(!meta_type_mem || !meta_size_mem || !meta_npar_mem || !meta_nnam_mem)
if(!meta_type_mem || !meta_size_mem || !meta_npar_mem)
error("object %s: failed to create buffer for metadata", id);

// setup and run kernel to get meta_data
Expand All @@ -121,7 +118,6 @@ void add_object(input* inp, const char* id, const char* name)
err |= clSetKernelArg(meta_kernel, 0, sizeof(cl_mem), &meta_type_mem);
err |= clSetKernelArg(meta_kernel, 1, sizeof(cl_mem), &meta_size_mem);
err |= clSetKernelArg(meta_kernel, 2, sizeof(cl_mem), &meta_npar_mem);
err |= clSetKernelArg(meta_kernel, 3, sizeof(cl_mem), &meta_nnam_mem);
if(err != CL_SUCCESS)
error("object %s: failed to set kernel arguments for metadata", id);
err = clEnqueueTask(queue, meta_kernel, 0, NULL, NULL);
Expand All @@ -132,7 +128,6 @@ void add_object(input* inp, const char* id, const char* name)
err |= clEnqueueReadBuffer(queue, meta_type_mem, CL_TRUE, 0, sizeof(cl_int), &meta_type, 0, NULL, NULL);
err |= clEnqueueReadBuffer(queue, meta_size_mem, CL_TRUE, 0, sizeof(cl_ulong), &meta_size, 0, NULL, NULL);
err |= clEnqueueReadBuffer(queue, meta_npar_mem, CL_TRUE, 0, sizeof(cl_ulong), &meta_npar, 0, NULL, NULL);
err |= clEnqueueReadBuffer(queue, meta_nnam_mem, CL_TRUE, 0, sizeof(cl_ulong), &meta_nnam, 0, NULL, NULL);
if(err != CL_SUCCESS)
error("object %s: failed to get metadata", id);

Expand All @@ -146,9 +141,9 @@ void add_object(input* inp, const char* id, const char* name)
error("object %s: invalid type (should be LENS, SOURCE or FOREGROUND)", id);

// buffers for kernel parameters
param_names_mem = clCreateBuffer(lcl->context, CL_MEM_WRITE_ONLY, obj->npars*meta_nnam*sizeof(cl_char), NULL, NULL);
param_types_mem = clCreateBuffer(lcl->context, CL_MEM_WRITE_ONLY, obj->npars*sizeof(cl_int), NULL, NULL);
param_bounds_mem = clCreateBuffer(lcl->context, CL_MEM_WRITE_ONLY, obj->npars*sizeof(cl_float2), NULL, NULL);
param_names_mem = clCreateBuffer(lcl->context, CL_MEM_WRITE_ONLY, obj->npars*sizeof(cl_char16), NULL, NULL);
param_types_mem = clCreateBuffer(lcl->context, CL_MEM_WRITE_ONLY, obj->npars*sizeof(cl_int), NULL, NULL);
param_bounds_mem = clCreateBuffer(lcl->context, CL_MEM_WRITE_ONLY, obj->npars*sizeof(cl_float2), NULL, NULL);
if(!param_types_mem || !param_names_mem || !param_bounds_mem)
error("object %s: failed to create buffer for parameters", id);

Expand All @@ -167,16 +162,16 @@ void add_object(input* inp, const char* id, const char* name)
error("object %s: failed to run kernel for parameters", id);

// arrays for kernel parameters
param_names = malloc(obj->npars*meta_nnam*sizeof(cl_char));
param_names = malloc(obj->npars*sizeof(cl_char16));
param_types = malloc(obj->npars*sizeof(cl_int));
param_bounds = malloc(obj->npars*sizeof(cl_float2));
if(!param_types || !param_names || !param_bounds)
errori("object %s", id);

// get kernel parameters from buffer
err |= clEnqueueReadBuffer(queue, param_names_mem, CL_TRUE, 0, obj->npars*meta_nnam*sizeof(cl_char), param_names, 0, NULL, NULL);
err |= clEnqueueReadBuffer(queue, param_types_mem, CL_TRUE, 0, obj->npars*sizeof(cl_int), param_types, 0, NULL, NULL);
err |= clEnqueueReadBuffer(queue, param_bounds_mem, CL_TRUE, 0, obj->npars*sizeof(cl_float2), param_bounds, 0, NULL, NULL);
err |= clEnqueueReadBuffer(queue, param_names_mem, CL_TRUE, 0, obj->npars*sizeof(cl_char16), param_names, 0, NULL, NULL);
err |= clEnqueueReadBuffer(queue, param_types_mem, CL_TRUE, 0, obj->npars*sizeof(cl_int), param_types, 0, NULL, NULL);
err |= clEnqueueReadBuffer(queue, param_bounds_mem, CL_TRUE, 0, obj->npars*sizeof(cl_float2), param_bounds, 0, NULL, NULL);
if(err != CL_SUCCESS)
error("object %s: failed to get parameters", id);

Expand All @@ -195,13 +190,13 @@ void add_object(input* inp, const char* id, const char* name)
char* id;

// allocate name
name = malloc(meta_nnam);
name = malloc(16);
if(!name)
errori(NULL);

// copy name
for(size_t j = 0; j < meta_nnam; ++j)
name[j] = param_names[i*meta_nnam + j];
for(size_t j = 0; j < 16; ++j)
name[j] = param_names[i].s[j];

// create id
id = malloc(strlen(obj->id) + 1 + strlen(name) + 1);
Expand Down Expand Up @@ -239,7 +234,6 @@ void add_object(input* inp, const char* id, const char* name)
clReleaseMemObject(meta_type_mem);
clReleaseMemObject(meta_size_mem);
clReleaseMemObject(meta_npar_mem);
clReleaseMemObject(meta_nnam_mem);

for(int i = 0; i < nkernels; ++i)
free((void*)kernels[i]);
Expand Down
12 changes: 4 additions & 8 deletions src/kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,22 @@ static const size_t NMAINKERNS = sizeof(MAINKERNS)/sizeof(MAINKERNS[0]);
// kernel to get meta-data for object
static const char METAKERN[] =
"kernel void meta_<name>(global int* type, global ulong* size,\n"
" global ulong* npar, global ulong* nnam)\n"
" global ulong* npar)\n"
"{\n"
" *type = type_<name>;\n"
" *size = sizeof(struct data_<name>);\n"
" *npar = sizeof(parlst_<name>)/sizeof(struct param);\n"
" *nnam = sizeof(((struct param*)0)->name);\n"
"}\n"
;

// kernel to get parameters for object
static const char PARSKERN[] =
"kernel void params_<name>(global char* names, global int* types,\n"
"kernel void params_<name>(global char16* names, global int* types,\n"
" global float2* bounds)\n"
"{\n"
" const size_t n = sizeof(parlst_<name>)/sizeof(struct param);\n"
" const size_t l = sizeof(((struct param*)0)->name);\n"
" for(size_t i = 0; i < n; ++i)\n"
" for(size_t i = 0; i < sizeof(parlst_<name>)/sizeof(struct param); ++i)\n"
" {\n"
" for(size_t j = 0; j < l; ++j)\n"
" names[i*l + j] = parlst_<name>[i].name[j];\n"
" names[i] = vload16(0, parlst_<name>[i].name);\n"
" types[i] = parlst_<name>[i].type;\n"
" bounds[i] = parlst_<name>[i].bounds;\n"
" }\n"
Expand Down

0 comments on commit feff513

Please sign in to comment.