diff --git a/pyopencl/tools.py b/pyopencl/tools.py index f4a9bdfc4f76670a00e06b4fbbba5b27219dde01..952302f3e715d1737cfa1af70a480497f7872f6a 100644 --- a/pyopencl/tools.py +++ b/pyopencl/tools.py @@ -293,4 +293,23 @@ def get_gl_sharing_context_properties(): + +def dtype_to_c_struct(dtype): + dtype = np.dtype(dtype) + + fields = sorted(dtype.fields.iteritems(), + key=lambda (name, (dtype, offset)): offset) + + # FIXME check that this matches C alignment fulres + c_fields = [] + for name, (field_dtype, offset) in fields: + c_fields.append(" %s %s;" % (dtype_to_ctype(field_dtype), name)) + + return "typedef struct {\n%s\n} %s;" % ( + "\n".join(c_fields), + dtype_to_ctype(dtype)) + + + + # vim: foldmethod=marker diff --git a/test/test_array.py b/test/test_array.py index 4dea140af8b2deed70d6bf836eba263c43df8cab..7198ff3809be2853e500d16789f893e4bd0cbebf 100644 --- a/test/test_array.py +++ b/test/test_array.py @@ -704,16 +704,8 @@ def test_struct_reduce(ctx_factory): context = ctx_factory() queue = cl.CommandQueue(context) - preamble = r"""//CL// - struct minmax_collector - { - int cur_min; - int cur_max; - // Workaround for OS X Lion GPU CL. Mystifying. - int pad; - }; - - typedef struct minmax_collector minmax_collector; + from pyopencl.tools import dtype_to_c_struct + preamble = dtype_to_c_struct(mmc_dtype) + r"""//CL// minmax_collector mmc_neutral() {