From 88a0ea4e9ec36e16bc02f5df3c60a9931770f6f8 Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Tue, 23 Nov 2010 00:41:06 -0500 Subject: [PATCH] Fix get_supported_image_formats(), printing of unknown image formats. --- pyopencl/__init__.py | 15 ++++++++++----- src/wrapper/wrap_cl.hpp | 18 ++++++++---------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/pyopencl/__init__.py b/pyopencl/__init__.py index 607b8696..c4a452ed 100644 --- a/pyopencl/__init__.py +++ b/pyopencl/__init__.py @@ -41,13 +41,16 @@ def _add_functionality(): ], } - def to_string(cls, value): + def to_string(cls, value, default_format=None): for name in dir(cls): if (not name.startswith("_") and getattr(cls, name) == value): return name - raise ValueError("a name for value %d was not found in %s" - % (value, cls.__name__)) + if default_format is None: + raise ValueError("a name for value %d was not found in %s" + % (value, cls.__name__)) + else: + return default_format % value for cls in CONSTANT_CLASSES: cls.to_string = classmethod(to_string) @@ -243,8 +246,10 @@ def _add_functionality(): # {{{ ImageFormat def image_format_repr(self): return "ImageFormat(%s, %s)" % ( - channel_order.to_string(self.channel_order), - channel_type.to_string(self.channel_data_type)) + channel_order.to_string(self.channel_order, + ""), + channel_type.to_string(self.channel_data_type, + "")) ImageFormat.__repr__ = image_format_repr # }}} diff --git a/src/wrapper/wrap_cl.hpp b/src/wrapper/wrap_cl.hpp index 233ea32a..6acdccbe 100644 --- a/src/wrapper/wrap_cl.hpp +++ b/src/wrapper/wrap_cl.hpp @@ -1565,17 +1565,15 @@ namespace pyopencl cl_mem_flags flags, cl_mem_object_type image_type) { - cl_uint num_image_formats = 1024; - std::vector formats(num_image_formats); - do - { - if (formats.size() < num_image_formats) - formats.resize(num_image_formats); + cl_uint num_image_formats; + PYOPENCL_CALL_GUARDED(clGetSupportedImageFormats, ( + ctx.data(), flags, image_type, + 0, NULL, &num_image_formats)); - PYOPENCL_CALL_GUARDED(clGetSupportedImageFormats, ( - ctx.data(), flags, image_type, - formats.size(), formats.empty( ) ? NULL : &formats.front(), &num_image_formats)); - } while (formats.size() < num_image_formats); + std::vector formats(num_image_formats); + PYOPENCL_CALL_GUARDED(clGetSupportedImageFormats, ( + ctx.data(), flags, image_type, + formats.size(), formats.empty( ) ? NULL : &formats.front(), NULL)); PYOPENCL_RETURN_VECTOR(cl_image_format, formats); } -- GitLab