提交 98e759a6 authored 作者: Pierre Luc Carrier's avatar Pierre Luc Carrier

Modified where COp looks for the external implementation

上级 7fb90052
...@@ -827,7 +827,8 @@ this new version of the VectorTimesVector op : ...@@ -827,7 +827,8 @@ this new version of the VectorTimesVector op :
on the filesystem of the C implementation of this op. To do this, it on the filesystem of the C implementation of this op. To do this, it
gives the path of file containing the C code as well as the name of gives the path of file containing the C code as well as the name of
the function, in that file, that should be called to perform the the function, in that file, that should be called to perform the
computation. computation. The path should be given as a relative path from the
folder where the descendant of the ``COp`` class is defined.
* ``make_node()`` : the ``make_node()`` method is absolutely identical to * ``make_node()`` : the ``make_node()`` method is absolutely identical to
the one in our old example. Using the ``COp`` class doesn't change the one in our old example. Using the ``COp`` class doesn't change
......
...@@ -12,6 +12,7 @@ __contact__ = "theano-dev <theano-dev@googlegroups.com>" ...@@ -12,6 +12,7 @@ __contact__ = "theano-dev <theano-dev@googlegroups.com>"
__docformat__ = "restructuredtext en" __docformat__ = "restructuredtext en"
import inspect
import logging import logging
import numpy import numpy
import os import os
...@@ -1000,9 +1001,34 @@ class COp(Op): ...@@ -1000,9 +1001,34 @@ class COp(Op):
self.apply_code_marker] self.apply_code_marker]
# Load the external C code # Load the external C code
f = open(self.func_file, "r") try:
self.func_code = f.read() # Attempt to find the file self.func_file in the folder where the
f.close() # concrete type of the COp instance is defined
# Get the name of the folder where the concrete type of the COp is
# defined
path_concrete_type = inspect.getfile(self.__class__)
folder_concrete_type = os.path.dirname(path_concrete_type)
# Try to open the file from there
f = open(os.path.join(folder_concrete_type, self.func_file), "r")
self.func_code = f.read()
f.close()
except IOError:
# Add information to the exception message to inform the user
# on the locations in which the class COp will look for the
# specified file
message = ("The path to the external C implementation should "
"be given as a relative path from the folder "
"where the Op is defined. ")
# Can't update the exception's message by modifying e.args
# because IOErrors don't use their attribute args to generate
# their error message
e.strerror = message + e.strerror
raise e
# Separate the contents of the file in sections and validate that at # Separate the contents of the file in sections and validate that at
# lest one of the necessary code sections has been defined # lest one of the necessary code sections has been defined
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论