From f04c233af55e4c558ef5f698f91aebe03dd23e1d Mon Sep 17 00:00:00 2001
From: Andreas Kloeckner <inform@tiker.net>
Date: Sun, 24 Apr 2011 02:57:03 -0400
Subject: [PATCH] Retry context allocation after GC if memory-related failure.

---
 src/wrapper/wrap_cl.hpp | 29 ++++++++++++++++++++++++++++-
 1 file changed, 28 insertions(+), 1 deletion(-)

diff --git a/src/wrapper/wrap_cl.hpp b/src/wrapper/wrap_cl.hpp
index 2356d7df..88dcec92 100644
--- a/src/wrapper/wrap_cl.hpp
+++ b/src/wrapper/wrap_cl.hpp
@@ -780,7 +780,7 @@ namespace pyopencl
 
 
   inline
-  context *create_context(py::object py_devices, py::object py_properties,
+  context *create_context_inner(py::object py_devices, py::object py_properties,
       py::object py_dev_type)
   {
     std::vector<cl_context_properties> props
@@ -844,6 +844,33 @@ namespace pyopencl
 
 
 
+  inline
+  context *create_context(py::object py_devices, py::object py_properties,
+      py::object py_dev_type)
+  {
+    try
+    {
+      return create_context_inner(py_devices, py_properties, py_dev_type);
+    }
+    catch (pyopencl::error &e)
+    { 
+      if (!e.is_out_of_memory())
+        throw;
+    }
+
+    // If we get here, we got an error from CL.
+    // We should run the Python GC to try and free up
+    // some memory references.
+    run_python_gc();
+
+    // Now retry the allocation. If it fails again,
+    // let it fail.
+    return create_context_inner(py_devices, py_properties, py_dev_type);
+  }
+
+
+
+
 
   // }}}
 
-- 
GitLab