Skip to content

Commit

Permalink
Merge pull request #200 from prabhuramachandran/change-cache-nnps-dyn…
Browse files Browse the repository at this point in the history
…amically

Allow user to set the caching dynamically ...
  • Loading branch information
prabhuramachandran committed Apr 7, 2019
2 parents 87909b9 + 74680ad commit 2203262
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 9 deletions.
6 changes: 6 additions & 0 deletions pysph/base/gpu_nnps_base.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,12 @@ cdef class GPUNNPS(NNPSBase):
self.particles[pa_index].gpu.align(indices)
callback()

def set_use_cache(self, bint use_cache):
self.use_cache = use_cache
if use_cache:
for cache in self.cache:
cache.update()

cdef void find_neighbor_lengths(self, nbr_lengths):
raise NotImplementedError("NNPS :: find_neighbor_lengths called")

Expand Down
2 changes: 1 addition & 1 deletion pysph/base/nnps_base.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ cdef class NNPSBase:
cdef public list particles # list of particle arrays
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 bint use_cache # Use cache or not.
cdef public list cache # The neighbor cache.
cdef int src_index, dst_index # The current source and dest indices

Expand Down
6 changes: 6 additions & 0 deletions pysph/base/nnps_base.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1238,6 +1238,12 @@ cdef class NNPS(NNPSBase):
def set_in_parallel(self, bint in_parallel):
self.domain.manager.in_parallel = in_parallel

def set_use_cache(self, bint use_cache):
self.use_cache = use_cache
if use_cache:
for cache in self.cache:
cache.update()

def update_domain(self):
self.domain.update()

Expand Down
25 changes: 25 additions & 0 deletions pysph/base/tests/test_neighbor_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,31 @@ def test_cache_updates_with_changed_particles(self):
nb_c = nb_cached.get_npy_array()
self.assertTrue(np.all(nb_e == nb_c))

def test_setting_use_cache_does_cache(self):
# Given
pa = self._make_random_parray('pa1', 3)
pa.h[:] = 1.0
nnps = LinkedListNNPS(dim=3, particles=[pa], cache=False)
n = pa.get_number_of_particles()

# When
nnps.set_use_cache(True)
nbrs = UIntArray()
nnps.set_context(0, 0)
for i in range(n):
nnps.get_nearest_particles(0, 0, i, nbrs)

# Then
self.assertEqual(nbrs.length, n)
# Find the length of all cached neighbors,
# in this case, each particle has n neighbors,
# so we should have n*n neighbors in all.
total_length = sum(
x.length for x in nnps.cache[0]._neighbor_arrays
)
self.assertEqual(total_length, n*n)



if __name__ == '__main__':
unittest.main()
20 changes: 12 additions & 8 deletions pysph/solver/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,9 @@ def __init__(self, fname=None, domain=None):
self.output_dir = abspath(self._get_output_dir_from_fname())
self.particles = []
self.inlet_outlet = []
# The default value that is overridden by the command line
# options passed or in initializee.
self.cache_nnps = False

self.initialize()
self.scheme = self.create_scheme()
Expand Down Expand Up @@ -577,7 +580,7 @@ def _setup_argparse(self):
"--cache-nnps",
dest="cache_nnps",
action="store_true",
default=False,
default=self.cache_nnps,
help="Option to enable the use of neighbor caching.")

nnps_options.add_argument(
Expand Down Expand Up @@ -908,20 +911,21 @@ def _create_particles(self, particle_factory, *args, **kw):
def _configure_options(self):
options = self.options
# Setup configuration options.
config = get_config()
if options.with_openmp is not None:
get_config().use_openmp = options.with_openmp
config.use_openmp = options.with_openmp
if options.omp_schedule is not None:
get_config().set_omp_schedule(options.omp_schedule)
config.set_omp_schedule(options.omp_schedule)
if options.with_opencl:
get_config().use_opencl = True
config.use_opencl = True
if options.with_local_memory:
leaf_size = int(options.octree_leaf_size)
get_config().wgs = leaf_size
get_config().use_local_memory = True
config.wgs = leaf_size
config.use_local_memory = True
if options.use_double:
get_config().use_double = options.use_double
config.use_double = options.use_double
if options.profile:
get_config().profile = options.profile
config.profile = options.profile
for pa in self.particles:
pa.update_backend()

Expand Down

0 comments on commit 2203262

Please sign in to comment.