Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use filenames that are guaranteed to be unique for standalone #455

Merged
merged 3 commits into from
Apr 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions brian2/devices/cpp_standalone/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,32 @@ def get_array_name(self, var, access_data=True):
raise TypeError(('Do not have a name for variable of type '
'%s') % type(var))

def get_array_filename(self, var, basedir='results'):
'''
Return a file name for a variable.

Parameters
----------
var : `ArrayVariable`
The variable to get a filename for.
basedir : str
The base directory for the filename, defaults to ``'results'``.
Returns
-------
filename : str
A filename of the form
``'results/'+varname+'_'+str(hash(varname))``, where varname is the
name returned by `get_array_name`.

Notes
-----
The reason that the filename is not simply ``'results/' + varname`` is
that this could lead to file names that are not unique in file systems
that are not case sensitive (e.g. on Windows).
'''
varname = self.get_array_name(var, access_data=False)
return os.path.join(basedir, varname + '_' + str(hash(varname)))

def add_array(self, var):
# Note that a dynamic array variable is added to both the arrays and
# the _dynamic_array dictionary
Expand Down Expand Up @@ -317,8 +343,8 @@ def get_value(self, var, access_data=True):
# disk
if self.has_been_run:
dtype = var.dtype
fname = os.path.join(self.project_dir, 'results',
array_name)
fname = os.path.join(self.project_dir,
self.get_array_filename(var))
with open(fname, 'rb') as f:
data = np.fromfile(f, dtype=dtype)
# This is a bit of an heuristic, but our 2d dynamic arrays are
Expand Down Expand Up @@ -403,7 +429,8 @@ def generate_objects_source(self, writer, arange_arrays, synapses, static_array_
synapses=synapses,
clocks=self.clocks,
static_array_specs=static_array_specs,
networks=networks)
networks=networks,
get_array_filename=self.get_array_filename)
writer.write('objects.*', arr_tmp)

def generate_main_source(self, writer, main_includes):
Expand Down
Loading