From 0a10314f2690968c042246ed848c32f9a9c60f7f Mon Sep 17 00:00:00 2001 From: Fabio Luporini Date: Wed, 14 Jun 2017 14:42:33 +0200 Subject: [PATCH] Fix data allocation and cleanup potential leaks and avoid extra array zeroing sweeps --- devito/interfaces.py | 47 ++++++++++++-------------------------------- devito/memory.py | 1 - 2 files changed, 13 insertions(+), 35 deletions(-) diff --git a/devito/interfaces.py b/devito/interfaces.py index aee9df1b19..7454b247e0 100644 --- a/devito/interfaces.py +++ b/devito/interfaces.py @@ -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'] @@ -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): @@ -304,21 +301,13 @@ 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): @@ -326,16 +315,15 @@ def data(self): :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): @@ -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) @@ -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): @@ -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""" diff --git a/devito/memory.py b/devito/memory.py index 2a37532ac8..15727c6eb3 100644 --- a/devito/memory.py +++ b/devito/memory.py @@ -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)