Skip to content

Commit

Permalink
Merge pull request #178 from prabhuramachandran/fix-nnps-cache-bug
Browse files Browse the repository at this point in the history
BUG: Fix massive leak with caching nnps.
  • Loading branch information
prabhuramachandran committed Feb 27, 2019
2 parents 9d46b26 + 7b301a1 commit e57e9d4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
5 changes: 3 additions & 2 deletions pysph/base/nnps_base.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,8 @@ cdef class NeighborCache:
cdef UIntArray _start_stop
cdef IntArray _cached
cdef void **_neighbors
cdef list _neighbor_arrays
# This is made public purely for testing!
cdef public list _neighbor_arrays
cdef int _last_avg_nbr_size

cdef void get_neighbors_raw(self, size_t d_idx, UIntArray nbrs) nogil
Expand All @@ -265,7 +266,7 @@ cdef class NNPSBase:
cdef public list pa_wrappers # list of particle array wrappers
cdef public int narrays # Number of particle arrays
cdef public bint use_cache # Use cache or not.
cdef list cache # The neighbor cache.
cdef public list cache # The neighbor cache.
cdef int src_index, dst_index # The current source and dest indices

cdef public DomainManager domain # Domain manager
Expand Down
7 changes: 5 additions & 2 deletions pysph/base/nnps_base.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ ELSE:
cpdef int get_number_of_threads():
return 1
cpdef set_number_of_threads(int n):
print "OpenMP not available, cannot set number of threads."
print("OpenMP not available, cannot set number of threads.")


IF UNAME_SYSNAME == "Windows":
Expand Down Expand Up @@ -1003,8 +1003,11 @@ cdef class NeighborCache:
# This is an upper limit for the number of neighbors in a worst
# case scenario.
cdef size_t safety = 1024
cdef UIntArray arr
for i in range(n_threads):
(<UIntArray>self._neighbors[i]).c_reserve(
arr = <UIntArray>self._neighbors[i]
arr.c_reset()
arr.c_reserve(
self._last_avg_nbr_size*np/n_threads + safety
)

Expand Down
20 changes: 20 additions & 0 deletions pysph/base/tests/test_nnps.py
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,26 @@ def test_large_number_of_neighbors_linked_list():
assert nbrs.length == len(x)


def test_neighbor_cache_update_doesnt_leak():
# Given
x, y, z = numpy.random.random((3, 1000))
pa = get_particle_array(name='fluid', x=x, y=y, z=z, h=0.05)

nps = nnps.LinkedListNNPS(dim=3, particles=[pa], cache=True)
nps.set_context(0, 0)
nps.cache[0].find_all_neighbors()
old_length = sum(x.length for x in nps.cache[0]._neighbor_arrays)

# When
nps.update()
nps.set_context(0, 0)
nps.cache[0].find_all_neighbors()

# Then
new_length = sum(x.length for x in nps.cache[0]._neighbor_arrays)
assert new_length == old_length


nnps_classes = [
nnps.BoxSortNNPS,
nnps.CellIndexingNNPS,
Expand Down

0 comments on commit e57e9d4

Please sign in to comment.