提交 ff120e15 authored 作者: Arnaud Bergeron's avatar Arnaud Bergeron 提交者: Frederic Bastien

Now that we have dicts on GpuContext get rid of the horrible prop hack.

上级 f7596c63
......@@ -27,7 +27,7 @@ except ImportError:
# This is for documentation not to depend on the availability of pygpu
from .type import (GpuArrayType, GpuArrayVariable, GpuArrayConstant,
GpuArraySharedVariable, gpuarray_shared_constructor,
reg_context, get_context, ContextNotDefined, _get_props)
reg_context, get_context, ContextNotDefined)
from .basic_ops import as_gpuarray_variable
from . import fft, dnn, opt, nerv, extra_ops, multinomial, reduction
......@@ -50,68 +50,65 @@ def init_dev(dev, name=None):
# This is for the C headers API
if pygpu.gpuarray.api_version()[0] < 0:
raise ValueError("Your installed libgpuarray is too old, please update")
need_preallocate = False
if init_dev.dnn_version is None and dev.startswith('cuda'):
try:
init_dev.dnn_version = dnn.version()
# 5200 should not print warning with cudnn 5.1 final.
if init_dev.dnn_version >= 5200:
warnings.warn("Your cuDNN version is more recent than "
"Theano. If you encounter problems, try "
"updating Theano or downgrading cuDNN to "
"version 5.1.")
if config.print_active_device:
print("Using cuDNN version %d" % (init_dev.dnn_version, name),
file=sys.stderr)
except Exception:
pass
if dev not in init_dev.devmap:
ctx = pygpu.init(dev,
disable_alloc_cache=config.gpuarray.preallocate < 0,
single_stream=config.gpuarray.single_stream,
sched=config.gpuarray.sched)
ctx.dev = dev
init_dev.devmap[dev] = ctx
if dev.startswith('cuda'):
ctx.cudnn_handle = dnn._make_handle(ctx)
if config.gpuarray.preallocate < 0:
print("Disabling allocation cache on %s" % (dev,))
elif config.gpuarray.preallocate > 0:
need_preallocate = True
MB = (1024 * 1024)
if config.gpuarray.preallocate <= 1:
gmem = min(config.gpuarray.preallocate, 0.95) * context.total_gmem
else:
gmem = config.gpuarray.preallocate * MB
if gmem > context.free_gmem - 50 * MB:
print ("WARNING: Preallocating too much memory can prevent cudnn and cublas from working properly")
# This will allocate and immediatly free an object of size gmem
# which will reserve that amount of memory on the GPU.
pygpu.empty((gmem,), dtype='int8', context=ctx)
if config.print_active_device:
print("Preallocating %d/%d Mb (%f) on %s" %
(gmem//MB, ctx.total_gmem//MB, gmem/ctx.total_gmem, dev),
file=sys.stderr)
context = init_dev.devmap[dev]
# This will map the context name to the real context object.
reg_context(name, context)
if config.print_active_device:
try:
pcibusid = context.pcibusid
pcibusid = '(' + context.pcibusid + ')'
except pygpu.gpuarray.UnsupportedException:
pcibusid = '(unsupported for device %s)' % dev
pcibusid = ''
print("Mapped name %s to device %s: %s" %
(name, dev, context.devname),
print("Mapped name %s to device %s: %s %s" %
(name, dev, context.devname, pcibusid),
file=sys.stderr)
print("PCI Bus ID:", pcibusid, file=sys.stderr)
pygpu_activated = True
ctx_props = _get_props(name)
ctx_props['dev'] = dev
if dev.startswith('cuda'):
if 'cudnn_version' not in ctx_props:
try:
ctx_props['cudnn_version'] = dnn.version()
# 5200 should not print warning with cudnn 5.1 final.
if ctx_props['cudnn_version'] >= 5200:
warnings.warn("Your cuDNN version is more recent than "
"Theano. If you encounter problems, try "
"updating Theano or downgrading cuDNN to "
"version 5.1.")
if config.print_active_device:
print("Using cuDNN version %d on context %s" %
(ctx_props['cudnn_version'], name), file=sys.stderr)
ctx_props['cudnn_handle'] = dnn._make_handle(context)
except Exception:
pass
if need_preallocate:
MB = (1024 * 1024)
if config.gpuarray.preallocate <= 1:
gmem = min(config.gpuarray.preallocate, 0.95) * context.total_gmem
else:
gmem = config.gpuarray.preallocate * MB
if gmem > context.free_gmem - 50 * MB:
print ("WARNING: Preallocating too much memory can prevent cudnn and cublas from working properly")
# This will allocate and immediatly free an object of size gmem
# which will reserve that amount of memory on the GPU.
pygpu.empty((gmem,), dtype='int8', context=ctx)
if config.print_active_device:
print("Preallocating %d/%d Mb (%f) on %s" %
(gmem//MB, ctx.total_gmem//MB, gmem/ctx.total_gmem, dev),
file=sys.stderr)
# This maps things like 'cuda0' to the context object on that device.
init_dev.devmap = {}
init_dev.dnn_version = None
if pygpu:
try:
......
......@@ -30,7 +30,7 @@ from theano.tensor.signal.pool import (
Pool, MaxPoolGrad, AveragePoolGrad)
from . import pygpu
from .type import (get_context, gpu_context_type, list_contexts,
get_prop, set_prop, GpuArraySharedVariable)
GpuArraySharedVariable)
from .basic_ops import (as_gpuarray_variable, infer_context_name,
gpu_contiguous, gpu_alloc_empty,
empty_like, GpuArrayType)
......@@ -209,14 +209,12 @@ class DnnBase(COp):
return node.outputs[0].type.context_name
def get_params(self, node):
try:
return get_prop(self.dnn_context(node), 'cudnn_handle_param')
except KeyError:
pass
ptr = get_prop(self.dnn_context(node), 'cudnn_handle').value
res = handle_type.make_value(ptr)
set_prop(self.dnn_context(node), 'cudnn_handle_param', res)
return res
ctx = self.dnn_context(node)
if not hasattr(ctx, 'cudnn_handle_param'):
ptr = ctx.cudnn_handle.value
res = handle_type.make_value(ptr)
ctx.cudnn_handle_param = res
return ctx.cudnn_handle_param
def __init__(self, files=None, c_func=None):
if files is None:
......
......@@ -97,26 +97,6 @@ def list_contexts():
"""
return _context_reg.keys()
# Mappings of properties to contexts. Please never use this if you
# can avoid it.
# This is basically a way to store "global" variables that depend on
# the context.
_props_map = {}
def _get_props(name):
ctx = get_context(name)
return _props_map[ctx]
def get_prop(name, k):
return _get_props(name)[k]
def set_prop(name, k, v):
_get_props(name)[k] = v
# Private method
def _name_for_ctx(ctx):
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论