The following mechanism
creates a default CUDA context and pushes it on the CUDA context stack. Unfortunately, it does not pop the context until the program exits and this does not play nicely with other libraries using CUDA since their contexts, if initialised before PyMORESANE, will be replaced by PyMORESANE's, for all time (or at least the length of the program).
It would be nice if PyMORESANE handled it's context manually by pushing before executing CUDA code, and popping once its done:
import pycuda.tools
class ContextWrapper(object):
def __init__(self, context):
self._ctx = context
def __enter__(self):
self._ctx.push()
return self
def __exit__(self, evalue, etype, etraceback):
self._ctx.pop()
__internal_context = None
def context_wrapper():
if __internal_context is None:
context = pycuda.tools.make_default_context()
context.pop() # Contexts seem to push on creation
__internal_context = ContextWrapper(context)
return __internal_context
def moresane_gpu_func(...):
with context_wrapper():
run_gpu_code()
The following mechanism
creates a default CUDA context and pushes it on the CUDA context stack. Unfortunately, it does not pop the context until the program exits and this does not play nicely with other libraries using CUDA since their contexts, if initialised before PyMORESANE, will be replaced by PyMORESANE's, for all time (or at least the length of the program).
It would be nice if PyMORESANE handled it's context manually by pushing before executing CUDA code, and popping once its done: