Beispiel #1
0
    def test_leapfrog_with_double(self):
        orig = get_config().use_double

        def _cleanup():
            get_config().use_double = orig
        get_config().use_double = True
        self.addCleanup(_cleanup)
        self.test_leapfrog()
Beispiel #2
0
    def test_default_global_config_is_really_global(self):
        # Given.
        config = get_config()
        self.assertTrue(isinstance(config, Config))

        # When
        config.use_openmp = 100

        # Then.
        config1 = get_config()
        self.assertEqual(config1.use_openmp, 100)
Beispiel #3
0
def test_c_struct_helper():
    # Given
    class Fruit(object):
        pass

    f = Fruit()
    f.apple = 1
    f.banana = 2.0
    f.pear = 1.5
    h = CStructHelper(f)

    # When
    result = h.get_code()

    # Then
    expect = dedent('''
    typedef struct Fruit {
        int apple;
        double banana;
        double pear;
    } Fruit;
    ''')
    assert result.strip() == expect.strip()

    # When/Then
    array = h.get_array()
    use_double = get_config().use_double
    fdtype = np.float64 if use_double else np.float32
    expect = np.dtype([('apple', np.int32), ('banana', fdtype),
                       ('pear', fdtype)])

    assert array.dtype == expect
    assert array['apple'] == 1
    assert array['banana'] == 2.0
    assert array['pear'] == 1.5
 def __init__(self, acceleration_eval):
     self.object = acceleration_eval
     self.config = get_config()
     self.all_array_names = get_all_array_names(self.object.particle_arrays)
     self.known_types = get_known_types_for_arrays(self.all_array_names)
     self._ext_mod = None
     self._module = None
 def __init__(self, helper, c_acceleration_eval):
     self.helper = helper
     self.acceleration_eval = c_acceleration_eval
     self.nnps = None
     self.parallel_manager = None
     self._post_stage_callback = None
     self._use_double = get_config().use_double
     self._setup_methods()
Beispiel #6
0
 def _get_extra_args(self):
     if get_config().use_openmp:
         if sys.platform == 'win32':
             return ['/openmp'], []
         else:
             return ['-fopenmp'], ['-fopenmp']
     else:
         return [], []
Beispiel #7
0
 def _get_backend(self, backend):
     if not backend:
         cfg = get_config()
         if cfg.use_opencl:
             backend = 'opencl'
         else:
             backend = 'cython'
     return backend
Beispiel #8
0
    def test_set_global(self):
        # Given.
        self.config.use_openmp = 200
        set_config(self.config)

        # When
        config = get_config()

        # Then.
        self.assertEqual(config.use_openmp, 200)
Beispiel #9
0
 def setUp(self):
     NNPSTestCase.setUp(self)
     cl = importorskip("pyopencl")
     from pysph.base import gpu_nnps
     ctx = cl.create_some_context(interactive=False)
     cfg = get_config()
     self._orig_use_double = cfg.use_double
     cfg.use_double = True
     self.nps = gpu_nnps.ZOrderGPUNNPS(dim=3,
                                       particles=self.particles,
                                       radius_scale=2.0,
                                       ctx=ctx)
    def test_precomputed_should_work_on_gpu_with_double(self):
        orig = get_config().use_double

        def _cleanup():
            get_config().use_double = orig
        get_config().use_double = True
        self.addCleanup(_cleanup)
        # Given
        pa = self.pa
        equations = [SummationDensity(dest='fluid', sources=['fluid'])]
        a_eval = self._make_accel_eval(equations)

        # When
        a_eval.compute(0.1, 0.1)

        # Then
        expect = np.asarray([7.357, 9.0, 9., 9., 9., 9., 9., 9.,  9.,  7.357])
        pa.gpu.pull('rho')

        print(pa.rho, pa.gpu.rho)
        self.assertTrue(np.allclose(expect, pa.rho, atol=1e-2))
Beispiel #11
0
def test_wrapping_class():
    # Given
    class Dummy(object):
        '''Class Docstring'''
        def __init__(self, x=0, f=0.0, s=''):
            "Constructor docstring"
            self.x = x
            self.f = f
            self.s = s
            self._private = 1

        def method(self):
            '''Method docstring.
            '''
            pass

    obj = Dummy()

    # When
    c = CConverter()
    result = c.parse_instance(obj)

    # Then
    expect = dedent('''
    typedef struct Dummy {
        double f;
        int x;
    } Dummy;


    void Dummy_method(Dummy* self)
    {
        ;
    }
    ''')
    assert result.strip() == expect.strip()

    # When
    h = CStructHelper(obj)
    use_double = get_config().use_double
    fdtype = np.float64 if use_double else np.float32
    dtype = np.dtype([('f', fdtype), ('x', np.int32)])
    expect = np.zeros(1, dtype)
    assert h.get_array() == expect
    def __init__(self, known_types=None, python_methods=False):
        """
        Parameters
        -----------

        - known_types: dict: provides default types for known arguments.

        - python_methods: bool: generate python wrapper functions.

             specifies if convenient Python friendly wrappers are to be
             generated in addition to the low-level c wrappers.
        """

        self.code = ''
        self.python_methods = python_methods
        # Methods to not wrap.
        self.ignore_methods = ['_cython_code_']
        self.known_types = known_types if known_types is not None else {}
        self._config = get_config()
Beispiel #13
0
    def test_honors_use_openmp_setting(self):
        # When
        get_config().use_openmp = True
        # Then
        cg = CythonGenerator()
        cg.parse(EqWithMethod())
        expect = dedent("""
        cdef class EqWithMethod:
            cdef public list _hidden
            cdef public double c
            cdef public double rho
            def __init__(self, **kwargs):
                for key, value in kwargs.items():
                    setattr(self, key, value)

            cdef inline void func(self, long d_idx, double* d_x) nogil:
                cdef double tmp
                tmp = abs(self.rho*self.c)*sin(pi*self.c)
                d_x[d_idx] = d_x[d_idx]*tmp
        """)
        self.assert_code_equal(cg.get_code().strip(), expect.strip())
Beispiel #14
0
 def setUp(self):
     get_config().use_openmp = False
Beispiel #15
0
 def _cleanup():
     get_config().use_double = orig
Beispiel #16
0
 def tearDown(self):
     super(ZOrderGPUDoubleNNPSTestCase, self).tearDown()
     get_config().use_double = self._orig_use_double
Beispiel #17
0
 def tearDown(self):
     super(BruteForceNNPSTestCase, self).tearDown()
     get_config().use_double = self._orig_use_double
Beispiel #18
0
 def __init__(self, obj):
     self._use_double = get_config().use_double
     self.parse(obj)
Beispiel #19
0
def convert_to_float_if_needed(code):
    use_double = get_config().use_double
    if not use_double:
        code = re.sub(r'\bdouble\b', 'float', code)
    return code
Beispiel #20
0
 def __init__(self, helper):
     self.helper = helper
     self.particle_arrays = helper.object.particle_arrays
     self.nnps = None
     self._queue = helper._queue
     self._use_double = get_config().use_double