Skip to content

Commit

Permalink
Merge pull request #227 from prabhuramachandran/misc-improvements
Browse files Browse the repository at this point in the history
Misc improvements
  • Loading branch information
prabhuramachandran committed Jun 23, 2019
2 parents f55d23f + 64553b6 commit 436c78c
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 6 deletions.
15 changes: 14 additions & 1 deletion pysph/solver/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ def _setup_argparse(self):
action="store_true",
dest="use_double",
default=False,
help="Use double precision for OpenCL code.")
help="Use double precision for OpenCL/CUDA code.")

# --kernel
all_kernels = list_all_kernels()
Expand All @@ -503,6 +503,12 @@ def _setup_argparse(self):
choices=all_kernels,
help="Use specified kernel from %s" % all_kernels)

parser.add_argument(
'--post-process', action="store",
dest="post_process", default=None,
help="Only perform post-processing and exit."
)

# Restart options
restart = parser.add_argument_group("Restart options",
"Restart options for PySPH")
Expand Down Expand Up @@ -805,6 +811,13 @@ def _process_command_line(self):
"""
options = self.options
if options.post_process:
self._message('-'*70)
self._message('Performing post processing alone.')
self.post_process(options.post_process)
# Exit right after this so even if the user
# has an app.post_process call, it doesn't call it.
sys.exit(0)
# save the path where we want to dump output
self.output_dir = abspath(options.output_dir)
mkdir(self.output_dir)
Expand Down
1 change: 1 addition & 0 deletions pysph/sph/acceleration_eval_cython.mako
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ from libc.math cimport *
from libc.math cimport fabs as abs
cimport numpy
import numpy
from cython import address
% if not helper.config.use_openmp:
from cython.parallel import threadid
prange = range
Expand Down
3 changes: 3 additions & 0 deletions pysph/sph/integrator_cython.mako
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ ${' '*4*level}${l}

from libc.math cimport *

from cython import address
from pysph.base.nnps_base cimport NNPS


${helper.get_helper_code()}

${helper.get_stepper_code()}


Expand Down
12 changes: 12 additions & 0 deletions pysph/sph/integrator_cython_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

# Local imports.
from pysph.sph.equation import get_array_names
from .acceleration_eval_cython_helper import get_helper_code
from compyle.api import CythonGenerator, get_func_definition


Expand Down Expand Up @@ -54,6 +55,17 @@ def setup_compiled_module(self, module, acceleration_eval):
def get_particle_array_names(self):
return ', '.join(sorted(self.object.steppers.keys()))

def get_helper_code(self):
helpers = []
for stepper in self.object.steppers.values():
if hasattr(stepper, '_get_helpers_'):
for helper in stepper._get_helpers_():
if helper not in helpers:
helpers.append(helper)

code = get_helper_code(helpers)
return '\n'.join(code)

def get_stepper_code(self):
classes = {}
for dest, stepper in self.object.steppers.items():
Expand Down
15 changes: 11 additions & 4 deletions pysph/sph/integrator_gpu_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
from .equation import get_array_names
from .integrator_cython_helper import IntegratorCythonHelper
from .acceleration_eval_gpu_helper import (
get_kernel_definition, get_converter, profile_kernel, wrap_code
get_kernel_definition, get_converter, profile_kernel, wrap_code,
get_helper_code
)


Expand Down Expand Up @@ -141,7 +142,8 @@ def _do_stage(self, method):

# Compute the remaining arguments.
args = [x() for x in args[3:]]
call(*(args + extra_args), block=(num_tpb, 1, 1), grid=(num_blocks, 1))
call(*(args + extra_args),
block=(num_tpb, 1, 1), grid=(num_blocks, 1))


class IntegratorGPUHelper(IntegratorCythonHelper):
Expand Down Expand Up @@ -239,16 +241,21 @@ def get_timestep_code(self):

def get_stepper_code(self):
classes = {}
for dest, stepper in self.object.steppers.items():
helpers = []
for stepper in self.object.steppers.values():
cls = stepper.__class__.__name__
classes[cls] = stepper
if hasattr(stepper, '_get_helpers_'):
for helper in stepper._get_helpers_():
if helper not in helpers:
helpers.append(helper)

known_types = dict(self.acceleration_eval_helper.known_types)

Converter = get_converter(self.acceleration_eval_helper.backend)
code_gen = Converter(known_types=known_types)

wrappers = []
wrappers = get_helper_code(helpers, code_gen, self.backend)
for cls in sorted(classes.keys()):
wrappers.append(code_gen.parse_instance(classes[cls]))
return '\n'.join(wrappers)
Expand Down
57 changes: 56 additions & 1 deletion pysph/sph/tests/test_integrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from pysph.base.nnps import LinkedListNNPS
from pysph.sph.sph_compiler import SPHCompiler
from pysph.sph.integrator import (LeapFrogIntegrator, PECIntegrator,
PEFRLIntegrator)
PEFRLIntegrator, EulerIntegrator)
from pysph.sph.integrator_step import (
IntegratorStep, LeapFrogStep, PEFRLStep, TwoStageRigidBodyStep
)
Expand Down Expand Up @@ -156,6 +156,19 @@ def py_stage2(self, dest, t, dt):
dest.x += 0.5


def my_helper(dt=0.0):
return dt*2.0


class StepWithHelper(IntegratorStep):

def _get_helpers_(self):
return [my_helper]

def stage1(self, d_idx, d_u, d_au, dt):
d_u[d_idx] += d_au[d_idx] * my_helper(dt)


class TestLeapFrogIntegrator(TestIntegratorBase):
def test_leapfrog(self):
# Given.
Expand Down Expand Up @@ -290,6 +303,27 @@ def callback(t):
self.assertTrue(err2 < err1)
self.assertAlmostEqual(err1/err2, 4.0, places=2)

def test_helper_can_be_used_with_stepper(self):
# Given.
integrator = EulerIntegrator(fluid=StepWithHelper())
equations = [SHM(dest="fluid", sources=None)]
self._setup_integrator(equations=equations, integrator=integrator)

# When
tf = 1.0
dt = tf/2

def callback(t):
pass

self._integrate(integrator, dt, tf, callback)

# Then
if self.pa.gpu is not None:
self.pa.gpu.pull('u')
u = self.pa.u
self.assertEqual(u, -2.0*self.pa.x)


class TestPEFRLIntegrator(TestIntegratorBase):
def test_pefrl(self):
Expand Down Expand Up @@ -428,6 +462,27 @@ def _cleanup():
self.addCleanup(_cleanup)
self.test_leapfrog()

def test_helper_can_be_used_with_stepper_on_gpu(self):
# Given.
integrator = EulerIntegrator(fluid=StepWithHelper())
equations = [SHM(dest="fluid", sources=None)]
self._setup_integrator(equations=equations, integrator=integrator)

# When
tf = 1.0
dt = tf/2

def callback(t):
pass

self._integrate(integrator, dt, tf, callback)

# Then
if self.pa.gpu is not None:
self.pa.gpu.pull('u')
u = self.pa.u
self.assertEqual(u, -2.0*self.pa.x)


class TestLeapFrogIntegratorCUDA(TestLeapFrogIntegratorGPU):
def _setup_integrator(self, equations, integrator):
Expand Down

0 comments on commit 436c78c

Please sign in to comment.