From 73b4c8b746142bbe6e8eb1f71b6e7572ded9aa15 Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Wed, 26 May 2010 21:08:51 -0400 Subject: [PATCH] First part of cl_khr_gl_sharing support. --- doc/make_constants.py | 21 ++++++++++++++++ doc/source/misc.rst | 4 +++ examples/matrix-multiply.py | 8 +++--- src/wrapper/wrap_cl.cpp | 7 ++++++ src/wrapper/wrap_cl.hpp | 49 +++++++++++++++++++++++++++++++------ 5 files changed, 77 insertions(+), 12 deletions(-) diff --git a/doc/make_constants.py b/doc/make_constants.py index 77e97462..2160c3c6 100644 --- a/doc/make_constants.py +++ b/doc/make_constants.py @@ -1,14 +1,35 @@ import pyopencl as cl +ctxp = cl.context_properties +ext_lookup = { + ctxp: { + ctxp.GL_CONTEXT_KHR: "cl_khr_gl_sharing", + ctxp.EGL_DISPLAY_KHR: "cl_khr_gl_sharing", + ctxp.GLX_DISPLAY_KHR: "cl_khr_gl_sharing", + ctxp.WGL_HDC_KHR: "cl_khr_gl_sharing", + ctxp.CGL_SHAREGROUP_KHR: "cl_khr_gl_sharing", + } + } + def doc_class(cls): print ".. class :: %s" % cls.__name__ print if cls.__name__.startswith("gl_"): print " Only available when PyOpenCL is compiled with GL support. See :func:`have_gl`." print + + cls_ext = ext_lookup.get(cls, {}) for i in sorted(dir(cls)): if not i.startswith("_") and not i == "to_string": print " .. attribute :: %s" % i + value = getattr(cls, i) + + if value in cls_ext: + print + print " Available with the ``%s`` extension." % ( + cls_ext[value]) + print + print " .. method :: to_string(value)" print print " Returns a :class:`str` representing *value*." diff --git a/doc/source/misc.rst b/doc/source/misc.rst index f7883f7c..2cb720d6 100644 --- a/doc/source/misc.rst +++ b/doc/source/misc.rst @@ -71,6 +71,10 @@ Version 0.92 Version 0.92 is currently under development. You can get snapshots from PyOpenCL's git version control. +* Add support for the + `cl_khr_gl_sharin `_ + extension. + Version 0.91.5 -------------- diff --git a/examples/matrix-multiply.py b/examples/matrix-multiply.py index 3dff0ff1..791bd0e9 100644 --- a/examples/matrix-multiply.py +++ b/examples/matrix-multiply.py @@ -149,10 +149,10 @@ else: b_height = a_width b_width = 50*block_size - h_a = numpy.random.rand(a_height, a_width).astype(numpy.float32) h_b = numpy.random.rand(a_width, a_height).astype(numpy.float32) h_c = numpy.empty((a_height, a_height)).astype(numpy.float32) +print h_a.shape, h_b.shape mf = cl.mem_flags @@ -163,6 +163,7 @@ kernel_params = {"block_size": block_size, prg = cl.Program(ctx, kernel_code % kernel_params, ).build(options="-cl-mad-enable -cl-fast-relaxed-math") kernel = prg.matrixMul +#print prg.binaries[0] #def __call__(self, queue, tgt, src, shape): # w, h = shape @@ -193,7 +194,7 @@ event = kernel(queue, (a_height,a_height), d_c_buf, d_a_buf, d_b_buf, event.wait() t1 = time() -count = 2 +count = 20 for i in range(count): event = kernel(queue, (a_height,a_height), d_c_buf, d_a_buf, d_b_buf, cl.LocalMemory(4* block_size**2), @@ -202,7 +203,6 @@ for i in range(count): event.wait() -#print event.profile.end - event.profile.start gpu_time = time()-t1 @@ -223,7 +223,7 @@ print "COMPUTE ", gpu_time/count print "COMPUTE2 ", (event.profile.end-event.profile.start)*1e-9 gflop = h_c.size * (a_width * 2.) / (1000**3.) -gflops = gflop / gpu_time +gflops = gflop / (gpu_time/count) print "gflops:", gflops do_cpu = False diff --git a/src/wrapper/wrap_cl.cpp b/src/wrapper/wrap_cl.cpp index 5175c279..a9d948b3 100644 --- a/src/wrapper/wrap_cl.cpp +++ b/src/wrapper/wrap_cl.cpp @@ -213,6 +213,13 @@ BOOST_PYTHON_MODULE(_cl) { py::class_ cls("context_properties", py::no_init); ADD_ATTR(CONTEXT_, PLATFORM); +#if defined(cl_khr_gl_sharing) && (cl_khr_gl_sharing >= 1) + ADD_ATTR( ,GL_CONTEXT_KHR); + ADD_ATTR( ,EGL_DISPLAY_KHR); + ADD_ATTR( ,GLX_DISPLAY_KHR); + ADD_ATTR( ,WGL_HDC_KHR); + ADD_ATTR( ,CGL_SHAREGROUP_KHR); +#endif } { diff --git a/src/wrapper/wrap_cl.hpp b/src/wrapper/wrap_cl.hpp index 52fbf54c..8fd478a8 100644 --- a/src/wrapper/wrap_cl.hpp +++ b/src/wrapper/wrap_cl.hpp @@ -317,6 +317,10 @@ namespace pyopencl case CL_INVALID_BUFFER_SIZE: return "invalid buffer size"; case CL_INVALID_MIP_LEVEL: return "invalid mip level"; +#if defined(cl_khr_gl_sharing) && (cl_khr_gl_sharing >= 1) + case CL_INVALID_GL_SHAREGROUP_REFERENCE_KHR: return "invalid gl sharegroup reference number"; +#endif + default: return "invalid error code"; } } @@ -591,6 +595,13 @@ namespace pyopencl switch (key) { case CL_CONTEXT_PLATFORM: +#if defined(cl_khr_gl_sharing) && (cl_khr_gl_sharing >= 1) + case CL_GL_CONTEXT_KHR: + case CL_EGL_DISPLAY_KHR: + case CL_GLX_DISPLAY_KHR: + case CL_WGL_HDC_KHR: + case CL_CGL_SHAREGROUP_KHR: +#endif { value = py::object( handle_from_new_ptr(new platform( @@ -618,13 +629,12 @@ namespace pyopencl - context *create_context(py::object py_devices, py::object py_properties, - py::object py_dev_type) + + std::vector parse_context_properties( + py::object py_properties) { - // parse context properties - cl_context_properties *props_ptr = 0; std::vector props; - + if (py_properties.ptr() != Py_None) { PYTHON_FOREACH(prop_tuple, py_properties) @@ -634,8 +644,16 @@ namespace pyopencl cl_context_properties prop = py::extract(prop_tuple[0]); props.push_back(prop); - - if (prop == CL_CONTEXT_PLATFORM) + + if (prop == CL_CONTEXT_PLATFORM +#if defined(cl_khr_gl_sharing) && (cl_khr_gl_sharing >= 1) + || prop == CL_GL_CONTEXT_KHR + || prop == CL_EGL_DISPLAY_KHR + || prop == CL_GLX_DISPLAY_KHR + || prop == CL_WGL_HDC_KHR + || prop == CL_CGL_SHAREGROUP_KHR +#endif + ) { py::extract value(prop_tuple[1]); props.push_back( @@ -645,9 +663,24 @@ namespace pyopencl throw error("Context", CL_INVALID_VALUE, "invalid context property"); } props.push_back(0); - props_ptr = props.empty( ) ? NULL : &props.front(); } + return props; + } + + + + + context *create_context(py::object py_devices, py::object py_properties, + py::object py_dev_type) + { + // parse context properties + std::vector props + = parse_context_properties(py_properties); + + cl_context_properties *props_ptr + = props.empty( ) ? NULL : &props.front(); + cl_int status_code; cl_context ctx; -- GitLab