Ejemplo n.º 1
0
    def test_openmp(self):
        compiler = CppSwigCompiler()
        input_path = EXAMPLES_ROOTS['cpp14'].joinpath('matmul_openmp.cpp')

        output_dir = make_swig_tmp_folder(input_path)
        with input_path.open() as input_file:
            code = input_file.read()
        output_path = compiler.compile(code, input_path, output_dir)
        self.assertIsInstance(output_path, pathlib.Path)
        binder = Binder()
        with binder.temporarily_bind(output_path) as binding:
            print(type(binding.multiply_martices_example))
            # print(help(binding.main))
            with _TIME.measure('matmul') as timer:
                ret_val = binding.multiply_martices_example(1, 3000, 3000)
            self.assertGreater(timer.elapsed, 0)
            self.assertEqual(ret_val, 0)
            _LOG.info('%s', _TIME.summary)
            json_to_file(_TIME.summary,
                         PERFORMANCE_RESULTS_ROOT.joinpath('{}.run.matmul.json'.format(__name__)))
        output_path.unlink()
        try:
            output_dir.rmdir()
        except OSError:
            pass
Ejemplo n.º 2
0
    def test_bind_object_default(self):
        binder = Binder()
        with self.assertRaises(ValueError):
            binder.bind_object(pathlib.Path(__file__))

        binding = binder.bind_object(EXAMPLES_ROOTS['python3'].joinpath('matmul.py'))
        self.assertIsInstance(binding, object)
        del sys.modules['matmul']
Ejemplo n.º 3
0
    def test_bind(self):
        binder = Binder()
        path = pathlib.Path(__file__)
        binding = binder.bind(path)
        self.assertIsNotNone(binding)
        del sys.modules[path.with_suffix('').name]

        with self.assertRaises(ValueError):
            binder.bind(EXAMPLES_ROOTS['f77'].joinpath('matmul.f'))
Ejemplo n.º 4
0
    def test_run_fundamentals(self, input_path):
        output_dir = make_f2py_tmp_folder(input_path)

        code_reader = CodeReader()
        code = code_reader.read_file(input_path)

        compiler = F2PyCompiler()
        output_path = compiler.compile(code, input_path, output_dir)
        binder = Binder()
        with binder.temporarily_bind(output_path) as binding:
            self.assertIsInstance(binding, types.ModuleType)
            prefix = {
                'fundamentals': '',
                'fundamentals_arrays': 'itemwise_'
            }[input_path.stem]
            shape = None if prefix == '' else (1024 * 1024, )
            for type_ in ('integer', 'real'):
                py_type = {'integer': int, 'real': float}[type_]
                input1 = random_data(shape, dtype=py_type)
                input2 = random_data(shape, dtype=py_type)
                for operation in ('add', 'subtract', 'multiply'):
                    py_operator = {
                        'add': operator.add,
                        'subtract': operator.sub,
                        'multiply': operator.mul
                    }[operation]
                    expected = py_operator(input1, input2)
                    function_name = '{}{}_{}'.format(prefix, operation, type_)
                    function = getattr(binding, function_name)
                    with self.subTest(function=function_name):
                        with _TIME.measure('run.{}.{}.{}'.format(
                                input_path.name.replace('.', '_'), type_,
                                '{}{}'.format(prefix, operation))) as timer:
                            output = function(input1, input2)
                        _LOG.warning('ran %s from "%s" in %fs', function_name,
                                     input_path, timer.elapsed)
                        if type_ == 'integer':
                            self.assertTrue(np.all(np.equal(expected, output)),
                                            msg=(input1, input2, output,
                                                 expected))
                        else:
                            self.assertTrue(np.allclose(expected,
                                                        output,
                                                        atol=1e-4),
                                            msg=(input1, input2, output,
                                                 expected))
Ejemplo n.º 5
0
    def test_compile_and_bind_examples(self, input_path):
        output_dir = make_f2py_tmp_folder(input_path)

        code_reader = CodeReader()
        code = code_reader.read_file(input_path)
        compiler = F2PyCompiler()
        with _TIME.measure('compile.{}'.format(
                input_path.name.replace('.', '_'))) as timer:
            output_path = compiler.compile(code, input_path, output_dir)
        binder = Binder()
        with binder.temporarily_bind(output_path) as binding:
            self.assertIsInstance(binding, types.ModuleType)
        _LOG.warning('compiled "%s" in %fs', input_path, timer.elapsed)

        output_path.unlink()
        try:
            output_dir.rmdir()
        except OSError:
            pass
Ejemplo n.º 6
0
 def test_compile_examples(self):
     compiler = CppSwigCompiler()
     for input_path in EXAMPLES_CPP14_FILES:
         output_dir = pathlib.Path(
             EXAMPLES_RESULTS_ROOT, input_path.parent.name,
             'swig_tmp_{}'.format(
                 datetime.datetime.now().strftime('%Y%m%d%H%M%S%f')))
         if not output_dir.is_dir():
             output_dir.mkdir()
         with input_path.open() as input_file:
             code = input_file.read()
         with self.subTest(input_path=input_path):
             output_path = compiler.compile(code, input_path, output_dir)
             self.assertIsInstance(output_path, pathlib.Path)
             binder = Binder()
             with binder.temporarily_bind(output_path) as binding:
                 self.assertIsNotNone(binding)
             output_path.unlink()
         try:
             output_dir.rmdir()
         except OSError:
             pass
Ejemplo n.º 7
0
    def test_openmp(self):
        code_reader = CodeReader()
        binder = Binder()

        input_path = EXAMPLES_ROOTS['f77'].joinpath('matmul.f')
        code = code_reader.read_file(input_path)
        output_dir = make_f2py_tmp_folder(input_path)
        compiler = F2PyCompiler()
        output_path = compiler.compile(code, input_path, output_dir)
        with binder.temporarily_bind(output_path) as binding:
            self.assertIsInstance(binding, types.ModuleType)
            with _TIME.measure('run.matmul.simple'):
                ret_val = binding.intmatmul(20, 1024, 1024)
        self.assertEqual(ret_val, 0)
        output_path.unlink()

        input_path = EXAMPLES_ROOTS['f77'].joinpath('matmul_openmp.f')
        code = code_reader.read_file(input_path)
        output_dir = make_f2py_tmp_folder(input_path)
        compiler_omp = F2PyCompiler(GfortranInterface({'OpenMP'}))
        output_path = compiler_omp.compile(code, input_path, output_dir)
        with binder.temporarily_bind(output_path) as binding:
            self.assertIsInstance(binding, types.ModuleType)
            with _TIME.measure('run.matmul.openmp'):
                ret_val = binding.intmatmul(20, 1024, 1024)
        self.assertEqual(ret_val, 0)
        timings_name = '.'.join([__name__, 'run', 'matmul'])
        summary = timing.query_cache(timings_name).summary
        _LOG.warning('%s', summary)
        json_to_file(summary,
                     PERFORMANCE_RESULTS_ROOT.joinpath(timings_name + '.json'))
        output_path.unlink()

        try:
            output_dir.rmdir()
        except OSError:
            pass
Ejemplo n.º 8
0
    def test_bind_object(self):
        binder = Binder()
        binding = binder.bind_object('unittest', 'TestCase')
        self.assertIs(binding, unittest.TestCase)

        path = pathlib.Path(__file__)
        binding = binder.bind_object(path, 'Binder')
        self.assertIs(binding, Binder)
        del sys.modules[path.with_suffix('').name]

        with self.assertRaises(TypeError):
            binder.bind_object(3, 1415)
Ejemplo n.º 9
0
 def test_bind_module(self):
     binder = Binder()
     binding = binder.bind_module('unittest')
     self.assertIsNotNone(binding)
     binding = binder.bind_module('numpy')
     self.assertIsNotNone(binding)
Ejemplo n.º 10
0
 def test_construct(self):
     binder = Binder()
     self.assertIsNotNone(binder)
Ejemplo n.º 11
0
    def test_directives(self):
        binder = Binder()
        compiler_f95 = F2PyCompiler()
        compiler_f95_omp = F2PyCompiler(GfortranInterface({'OpenMP'}))
        compiler_f95_acc = F2PyCompiler(PgifortranInterface({'OpenACC'}))
        test_acc = shutil.which(
            str(compiler_f95_acc.f2py.f_compiler.executable(
                'compile'))) is not None

        name = 'itemwise_calc'
        variants = {
            'f95':
            compiler_f95.compile_file(EXAMPLES_ROOTS['f95'].joinpath(name +
                                                                     '.f90')),
            'f95_openmp':
            compiler_f95_omp.compile_file(
                EXAMPLES_ROOTS['f95'].joinpath(name + '_openmp.f90'))
        }
        if test_acc:
            variants['f95_openacc'] = compiler_f95_acc.compile_file(
                EXAMPLES_ROOTS['f95'].joinpath(name + '_openacc.f90'))

        arrays = [
            np.array(np.random.random_sample((array_size, )), dtype=np.double)
            for array_size in range(4 * KB, 64 * KB + 1, 4 * KB)
        ]
        arrays += [
            np.array(np.random.random_sample((array_size, )), dtype=np.double)
            for array_size in range(128 * KB, MB + 1, 128 * KB)
        ]
        if test_acc:
            arrays += [
                np.array(np.random.random_sample((array_size, )),
                         dtype=np.double)
                for array_size in range(4 * MB, 32 * MB + 1, 4 * MB)
            ]
            arrays += [
                np.array(np.random.random_sample((array_size, )),
                         dtype=np.double)
                for array_size in range(64 * MB, 256 * MB + 1, 64 * MB)
            ]

        for variant, path in variants.items():
            with binder.temporarily_bind(path) as binding:
                tested_function = getattr(binding, name)
                for array in arrays:
                    with self.subTest(variant=variant,
                                      path=path,
                                      array_size=array.size):
                        # with _TIME.measure('{}.{}.{}'.format(name, segments, variant)):
                        for _ in _TIME.measure_many(
                                'run.{}.{}.{}'.format(name, array.size,
                                                      variant), 10):
                            results = tested_function(array)
                        # self.assertListEqual(array.tolist(), array_copy.tolist())
                        self.assertTrue(results.shape, array.shape)

        for array in arrays:
            timings_name = '.'.join([__name__, 'run', name, str(array.size)])
            summary = timing.query_cache(timings_name).summary
            _LOG.info('%s', summary)
            json_to_file(
                summary,
                PERFORMANCE_RESULTS_ROOT.joinpath(timings_name + '.json'))