Skip to content

Commit

Permalink
Fix data allocation and cleanup
Browse files Browse the repository at this point in the history
potential leaks and avoid extra array zeroing sweeps
  • Loading branch information
FabioLuporini committed Jun 14, 2017
1 parent 832b66c commit 0a10314
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 35 deletions.
47 changes: 13 additions & 34 deletions devito/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
first_derivative, left, right,
second_derivative)
from devito.logger import debug, error, warning
from devito.memmap_manager import MemmapManager
from devito.memory import CMemory, first_touch

__all__ = ['DenseData', 'TimeData', 'Forward', 'Backward']
Expand Down Expand Up @@ -270,13 +269,11 @@ def __init__(self, *args, **kwargs):
self.indices = self._indices(**kwargs)
self.dtype = kwargs.get('dtype', np.float32)
self.space_order = kwargs.get('space_order', 1)
initializer = kwargs.get('initializer', None)
if initializer is not None:
assert(callable(initializer))
self.initializer = initializer
self.initializer = kwargs.get('initializer', None)
if self.initializer is not None:
assert(callable(self.initializer))
self.numa = kwargs.get('numa', False)
self._data = kwargs.get('_data', None)
MemmapManager.setup(self, *args, **kwargs)
self._data_object = None

@classmethod
def _indices(cls, **kwargs):
Expand Down Expand Up @@ -304,38 +301,29 @@ def _indices(cls, **kwargs):
return dimensions

def _allocate_memory(self):
"""Function to allocate memmory in terms of numpy ndarrays.
Note: memmap is a subclass of ndarray.
"""
if self.memmap:
self._data = np.memmap(filename=self.f, dtype=self.dtype, mode='w+',
shape=self.shape, order='C')
"""Allocate memory in terms of numpy ndarrays."""
debug("Allocating memory for %s (%s)" % (self.name, str(self.shape)))
self._data_object = CMemory(self.shape, dtype=self.dtype)
if self.numa:
first_touch(self)
else:
debug("Allocating memory for %s (%s)" % (self.name, str(self.shape)))
self._data_object = CMemory(self.shape, dtype=self.dtype)
self._data = self._data_object.ndpointer
if self.numa:
first_touch(self)
else:
self.data.fill(0)
self.data.fill(0)

@property
def data(self):
"""Reference to the :class:`numpy.ndarray` containing the data
:returns: The ndarray containing the data
"""
if self._data is None:
if self._data_object is None:
self._allocate_memory()

return self._data
return self._data_object.ndpointer

def initialize(self):
"""Apply the data initilisation function, if it is not None."""
if self.initializer is not None:
self.initializer(self.data)
# Ignore if no initializer exists - assume no initialisation necessary

@property
def dx(self):
Expand Down Expand Up @@ -521,7 +509,6 @@ class TimeData(DenseData):
def __init__(self, *args, **kwargs):
if not self._cached():
super(TimeData, self).__init__(*args, **kwargs)
self._full_data = self._data.view() if self._data else None
time_dim = kwargs.get('time_dim', None)
self.time_order = kwargs.get('time_order', 1)
self.save = kwargs.get('save', False)
Expand All @@ -543,9 +530,7 @@ def __init__(self, *args, **kwargs):

def initialize(self):
if self.initializer is not None:
if self._full_data is None:
self._allocate_memory()
self.initializer(self._full_data)
self.initializer(self.data)

@classmethod
def _indices(cls, **kwargs):
Expand All @@ -562,12 +547,6 @@ def _indices(cls, **kwargs):
_indices = DenseData._indices(**kwargs)
return tuple([tidx] + list(_indices))

def _allocate_memory(self):
"""function to allocate memmory in terms of numpy ndarrays."""
super(TimeData, self)._allocate_memory()

self._full_data = self._data.view()

@property
def dim(self):
"""Returns the spatial dimension of the data object"""
Expand Down
1 change: 0 additions & 1 deletion devito/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
class CMemory(object):
def __init__(self, shape, dtype=np.float32, alignment=None):
self.ndpointer, self.data_pointer = malloc_aligned(shape, alignment, dtype)
self.fill(0)

def __del__(self):
free(self.data_pointer)
Expand Down

0 comments on commit 0a10314

Please sign in to comment.