提交 06cccd43 authored 作者: Frederic's avatar Frederic

fix context problem with pycuda by checking it is imported before.

Also add the pycuda.init flag to force its import. It is not needed to set it to True in the buildbot, as all test file get imported before test start to run. So this init PyCUDA first if the theano flag device and init_gpu_device are not used.
上级 bb096349
...@@ -145,6 +145,18 @@ import theano and print the config variable, as in: ...@@ -145,6 +145,18 @@ import theano and print the config variable, as in:
This flag's value cannot be modified during the program execution. This flag's value cannot be modified during the program execution.
.. attribute:: config.pycuda.init
Bool value: either ``True`` or ``False``
Default: ``False``
If True, always initialize PyCUDA when Theano want to initilize
the GPU. Currently, we must always initialize PyCUDA before
Theano do it. Setting this flag to True, ensure that, but always
import PyCUDA. It can be done manually by importing
theano.misc.pycuda_init before theano initialize the GPU device.
.. attribute:: floatX .. attribute:: floatX
String value: either 'float64' or 'float32' String value: either 'float64' or 'float32'
......
...@@ -2,29 +2,28 @@ import os ...@@ -2,29 +2,28 @@ import os
import warnings import warnings
import theano import theano
import theano.sandbox.cuda as cuda import theano.sandbox.cuda
cuda_ndarray = cuda.cuda_ndarray.cuda_ndarray from theano import config
def set_gpu_from_theano(): def set_gpu_from_theano():
""" """
This set the GPU used by PyCUDA to the same as the one used by Theano. This set the GPU used by PyCUDA to the same as the one used by Theano.
""" """
#import pdb;pdb.set_trace() # Transfer the theano gpu binding to pycuda, for consistency
if cuda.use.device_number is None: if config.device.startswith("gpu") and len(config.device) > 3:
cuda.use("gpu", os.environ["CUDA_DEVICE"] = theano.config.device[3:]
force=False, elif (config.init_gpu_device.startswith("gpu") and
default_to_move_computation_to_gpu=False, len(config.init_gpu_device) > 3):
move_shared_float32_to_gpu=False, os.environ["CUDA_DEVICE"] = theano.config.init_gpu_device[3:]
enable_cuda=True,
test_driver=True)
assert cuda.use.device_number == cuda_ndarray.active_device_number()
# os.environ["CUDA_DEVICE"] = str(cuda.use.device_number)
set_gpu_from_theano() set_gpu_from_theano()
pycuda_available = False pycuda_available = False
if False: # If theano.sandbox.cuda don't exist, it is because we are importing
# it and it try to import this file! This mean we must init the device.
if (not hasattr(theano.sandbox, 'cuda') or
theano.sandbox.cuda.use.device_number is None):
try: try:
import pycuda import pycuda
import pycuda.autoinit import pycuda.autoinit
...@@ -33,3 +32,21 @@ if False: ...@@ -33,3 +32,21 @@ if False:
# presumably, the user wanted to use pycuda, else they wouldn't have # presumably, the user wanted to use pycuda, else they wouldn't have
# imported this module, so issue a warning that the import failed. # imported this module, so issue a warning that the import failed.
warnings.warn("PyCUDA import failed in theano.misc.pycuda_init") warnings.warn("PyCUDA import failed in theano.misc.pycuda_init")
except pycuda._driver.LogicError:
if theano.config.force_device:
raise
else:
if "CUDA_DEVICE" in os.environ:
del os.environ["CUDA_DEVICE"]
try:
import pycuda.autoinit
except pycuda._driver.LogicError:
pass
pycuda_available = True
else:
# Now we always import this file when we call theano.sandbox.cuda.use
# So this should not happen normally.
warnings.warn("For some unknow reason, theano.misc.pycuda_init was not"
" imported before Theano initialized the GPU. To fix the"
" problem, import theano.misc.pycuda_init manually before"
" using the GPU.")
...@@ -11,7 +11,7 @@ import theano ...@@ -11,7 +11,7 @@ import theano
from theano.compile import optdb from theano.compile import optdb
from theano.gof.cmodule import get_lib_extension from theano.gof.cmodule import get_lib_extension
from theano.gof.compilelock import get_lock, release_lock from theano.gof.compilelock import get_lock, release_lock
from theano.configparser import config, AddConfigVar, StrParam from theano.configparser import config, AddConfigVar, StrParam, BoolParam
import nvcc_compiler import nvcc_compiler
_logger_name = 'theano.sandbox.cuda' _logger_name = 'theano.sandbox.cuda'
...@@ -29,6 +29,16 @@ AddConfigVar('cuda.root', ...@@ -29,6 +29,16 @@ AddConfigVar('cuda.root',
""", """,
StrParam(os.getenv('CUDA_ROOT', "AUTO"))) StrParam(os.getenv('CUDA_ROOT', "AUTO")))
AddConfigVar('pycuda.init',
"""If True, always initialize PyCUDA when Theano want to
initilize the GPU. Currently, we must always initialize
PyCUDA before Theano do it. Setting this flag to True,
ensure that, but always import PyCUDA. It can be done
manually by importing theano.misc.pycuda_init before theano
initialize the GPU device.
""",
BoolParam(False))
if config.cuda.root == "AUTO": if config.cuda.root == "AUTO":
# set nvcc_path correctly and get the version # set nvcc_path correctly and get the version
nvcc_compiler.set_cuda_root() nvcc_compiler.set_cuda_root()
...@@ -328,10 +338,15 @@ def use(device, ...@@ -328,10 +338,15 @@ def use(device,
# No successful call to use() has been made yet # No successful call to use() has been made yet
if device != 'gpu' and device < 0: if device != 'gpu' and device < 0:
return return
if device in [None, ""]:
device = 0 # Do PyCUDA already initialized the GPU contect
pycuda_init_dev = False
if config.pycuda.init:
import theano.misc.pycuda_init
pycuda_init_dev = theano.misc.pycuda_init.pycuda_available
try: try:
if device != 'gpu': if (device != 'gpu') and not pycuda_init_dev:
assert isinstance(device, int) assert isinstance(device, int)
gpu_init(device) gpu_init(device)
use.device_number = device use.device_number = device
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论