From d1a1e8eee410e2224c6901561189c0f7684dedaf Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Thu, 26 Jul 2012 00:53:19 -0400 Subject: [PATCH] Add dtype_to_c_struct. --- pyopencl/tools.py | 19 +++++++++++++++++++ test/test_array.py | 12 ++---------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/pyopencl/tools.py b/pyopencl/tools.py index f4a9bdfc..952302f3 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 4dea140a..7198ff38 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() { -- GitLab