提交 130b5abc authored 作者: Olivier Delalleau's avatar Olivier Delalleau

Simplified code for In.borrow and Param.borrow

This commit simplifies the code so that In.borrow and Param.borrow can only be True or False (not None). It also makes the corresponding code for these two objects more similar. There should be no functional change, it just makes the code easier to understand.
上级 3733f854
...@@ -161,11 +161,10 @@ class In(SymbolicInput): ...@@ -161,11 +161,10 @@ class In(SymbolicInput):
True: permit the compiled function to modify the python object being passed as the input True: permit the compiled function to modify the python object being passed as the input
False: do not permit the compiled function to modify the python object being passed as the input. False: do not permit the compiled function to modify the python object being passed as the input.
borrow: Bool (default: False if mutable evaluate to False, True otherwise) borrow: Bool (default: take the same value as mutable)
True: permit the output of the compiled function to be aliased to the input True: permit the output of the compiled function to be aliased to the input
False: do not permit any output to be aliased to the input False: do not permit any output to be aliased to the input
strict: Bool (default: False) strict: Bool (default: False)
True: means that the value you pass for this input must have exactly the right type True: means that the value you pass for this input must have exactly the right type
False: the value you pass for this input may be cast automatically to the proper type False: the value you pass for this input may be cast automatically to the proper type
...@@ -201,24 +200,25 @@ class In(SymbolicInput): ...@@ -201,24 +200,25 @@ class In(SymbolicInput):
#in the function or from the caller #in the function or from the caller
self.shared = shared self.shared = shared
# mutable implies the output can be both aliased to the input and that the input can be if borrow is None:
# destroyed. borrow simply implies the output can be aliased to the input. Thus self.borrow = mutable
# mutable=True should require borrow=True. Raise warning when borrow is explicitely set else:
# to False with mutable=True. self.borrow = borrow
# mutable implies the output can be both aliased to the input and that
# the input can be destroyed. borrow simply implies the output can be
# aliased to the input. Thus mutable=True should require borrow=True.
# Raise warning when borrow is explicitely set to False with
# mutable=True.
if mutable: if mutable:
if borrow == False: if not self.borrow:
_logger.warning("Symbolic input for variable %s (name=%s) has " _logger.warning("Symbolic input for variable %s (name=%s) has "
"flags mutable=True, borrow=False. This combination is " "flags mutable=True, borrow=False. This combination is "
"incompatible since mutable=True implies that the " "incompatible since mutable=True implies that the "
"input variable may be both aliased (borrow=True) and " "input variable may be both aliased (borrow=True) and "
"over-written. We set borrow=True and continue.", "over-written. We set borrow=True and continue.",
variable, name) variable, name)
borrow = True self.borrow = True
# borrow=None basically means False. We can't set default value to False because of the
# above business with mutable.
if borrow is None:
borrow = False
if implicit is None: if implicit is None:
implicit = (isinstance(value, gof.Container) or implicit = (isinstance(value, gof.Container) or
...@@ -233,7 +233,6 @@ class In(SymbolicInput): ...@@ -233,7 +233,6 @@ class In(SymbolicInput):
autoname=autoname, autoname=autoname,
implicit=implicit) implicit=implicit)
self.value = value self.value = value
self.borrow = borrow
if self.implicit and value is None: if self.implicit and value is None:
raise TypeError('An implicit input must be given a default value') raise TypeError('An implicit input must be given a default value')
......
...@@ -268,9 +268,9 @@ class Param(object): ...@@ -268,9 +268,9 @@ class Param(object):
:param mutable: True -> function is allowed to modify this argument. :param mutable: True -> function is allowed to modify this argument.
:param borrow: True -> function is allowed to alias some output to :param borrow: Whether the function is allowed to alias some output to
this input this input. Using None (default) means we re-use the same value as the
`mutable` flag.
False: do not permit any output to be aliased to the input False: do not permit any output to be aliased to the input
:param strict: False -> function arguments may be copied or cast to match the :param strict: False -> function arguments may be copied or cast to match the
...@@ -289,25 +289,29 @@ class Param(object): ...@@ -289,25 +289,29 @@ class Param(object):
self.default = default self.default = default
self.name = name self.name = name
self.mutable = mutable self.mutable = mutable
# mutable implies the output can be both aliased to the input and that the input can be
# destroyed. borrow simply implies the output can be aliased to the input. Thus if borrow is None:
# mutable=True should require borrow=True. Raise warning when borrow is explicitely set self.borrow = self.mutable
# to False with mutable=True. else:
self.borrow = borrow
# mutable implies the output can be both aliased to the input and that
# the input can be destroyed. borrow simply implies the output can be
# aliased to the input. Thus mutable=True should require borrow=True.
# Raise warning when borrow is explicitly set to False with
# mutable=True.
if mutable: if mutable:
# Do not compare with "if not borrow", because borrow can be None, if not self.borrow:
# and "not None" would be True.
if borrow is False:
_logger.warning("Symbolic input for variable %s (name=%s) has " _logger.warning("Symbolic input for variable %s (name=%s) has "
"flags mutable=True, borrow=False. This combination is " "flags mutable=True, borrow=False. This combination is "
"incompatible since mutable=True implies that the " "incompatible since mutable=True implies that the "
"input variable may be both aliased (borrow=True) and " "input variable may be both aliased (borrow=True) and "
"over-written. We set borrow=True and continue.", "over-written. We set borrow=True and continue.",
variable, name) variable, name)
borrow = True self.borrow = True
self.strict = strict self.strict = strict
self.allow_downcast = allow_downcast self.allow_downcast = allow_downcast
self.implicit = implicit self.implicit = implicit
self.borrow = borrow
def pfunc(params, outputs=None, mode=None, updates=[], givens=[], def pfunc(params, outputs=None, mode=None, updates=[], givens=[],
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论