How to use the cmdstanpy.compiler_opts.CompilerOptions function in cmdstanpy

To help you get started, we’ve selected a few cmdstanpy examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github stan-dev / cmdstanpy / test / test_compiler_opts.py View on Github external
def test_opts_cpp_opencl(self):
        cpp_opts = {'OPENCL_DEVICE_ID': 1}
        opts = CompilerOptions(cpp_options=cpp_opts)
        opts.validate()
        opts_list = opts.compose()
        self.assertTrue('STAN_OPENCL=TRUE' in opts_list)
        self.assertTrue('OPENCL_DEVICE_ID=1' in opts_list)

        cpp_opts = {'OPENCL_DEVICE_ID': 'BAD'}
        opts = CompilerOptions(cpp_options=cpp_opts)
        with self.assertRaises(ValueError):
            opts.validate()

        cpp_opts = {'OPENCL_DEVICE_ID': -1}
        opts = CompilerOptions(cpp_options=cpp_opts)
        with self.assertRaises(ValueError):
            opts.validate()

        cpp_opts = {'OPENCL_PLATFORM_ID': 'BAD'}
github stan-dev / cmdstanpy / test / test_compiler_opts.py View on Github external
def test_opts_cpp(self):
        cpp_opts = {}
        opts = CompilerOptions(cpp_options=cpp_opts)
        opts.validate()
        self.assertEqual(opts.compose(), [])

        cpp_opts['STAN_MPI'] = 'TRUE'
        opts = CompilerOptions(cpp_options=cpp_opts)
        opts.validate()
        self.assertEqual(opts.compose(), ['STAN_MPI=TRUE'])

        cpp_opts['STAN_BAD'] = 'TRUE'
        opts = CompilerOptions(cpp_options=cpp_opts)
        with self.assertRaises(ValueError):
            opts.validate()
github stan-dev / cmdstanpy / test / test_compiler_opts.py View on Github external
def test_opts_stanc_includes(self):
        path2 = os.path.join(HERE, 'data', 'optimize')
        paths_str = ','.join([DATAFILES_PATH, path2]).replace('\\', '/')
        expect = 'STANCFLAGS+=--include_paths=' + paths_str

        stanc_opts = {'include_paths': paths_str}
        opts = CompilerOptions(stanc_options=stanc_opts)
        opts.validate()
        opts_list = opts.compose()
        self.assertTrue(expect in opts_list)

        stanc_opts = {'include_paths': [DATAFILES_PATH, path2]}
        opts = CompilerOptions(stanc_options=stanc_opts)
        opts.validate()
        opts_list = opts.compose()
        self.assertTrue(expect in opts_list)
github stan-dev / cmdstanpy / test / test_compiler_opts.py View on Github external
def test_opts_stanc(self):
        stanc_opts = {}
        opts = CompilerOptions()
        opts.validate()
        self.assertEqual(opts.compose(), [])

        opts = CompilerOptions(stanc_options=stanc_opts)
        opts.validate()
        self.assertEqual(opts.compose(), [])

        stanc_opts['warn-uninitialized'] = True
        opts = CompilerOptions(stanc_options=stanc_opts)
        opts.validate()
        self.assertEqual(opts.compose(), ['STANCFLAGS+=--warn-uninitialized'])

        stanc_opts['name'] = 'foo'
        opts = CompilerOptions(stanc_options=stanc_opts)
        opts.validate()
        self.assertEqual(
github stan-dev / cmdstanpy / test / test_compiler_opts.py View on Github external
def test_opts_empty(self):
        opts = CompilerOptions()
        opts.validate()
        self.assertEqual(opts.compose(), [])
        self.assertEqual(
            opts.__repr__(), 'stanc_options=None, cpp_options=None',
        )

        stanc_opts = {}
        opts = CompilerOptions(stanc_options=stanc_opts)
        opts.validate()
        self.assertEqual(opts.compose(), [])

        cpp_opts = {}
        opts = CompilerOptions(cpp_options=cpp_opts)
        opts.validate()
        self.assertEqual(opts.compose(), [])
github stan-dev / cmdstanpy / test / test_compiler_opts.py View on Github external
def test_opts_cpp(self):
        cpp_opts = {}
        opts = CompilerOptions(cpp_options=cpp_opts)
        opts.validate()
        self.assertEqual(opts.compose(), [])

        cpp_opts['STAN_MPI'] = 'TRUE'
        opts = CompilerOptions(cpp_options=cpp_opts)
        opts.validate()
        self.assertEqual(opts.compose(), ['STAN_MPI=TRUE'])

        cpp_opts['STAN_BAD'] = 'TRUE'
        opts = CompilerOptions(cpp_options=cpp_opts)
        with self.assertRaises(ValueError):
            opts.validate()
github stan-dev / cmdstanpy / test / test_compiler_opts.py View on Github external
opts_list = opts.compose()
        self.assertTrue('STAN_OPENCL=TRUE' in opts_list)
        self.assertTrue('OPENCL_DEVICE_ID=1' in opts_list)

        cpp_opts = {'OPENCL_DEVICE_ID': 'BAD'}
        opts = CompilerOptions(cpp_options=cpp_opts)
        with self.assertRaises(ValueError):
            opts.validate()

        cpp_opts = {'OPENCL_DEVICE_ID': -1}
        opts = CompilerOptions(cpp_options=cpp_opts)
        with self.assertRaises(ValueError):
            opts.validate()

        cpp_opts = {'OPENCL_PLATFORM_ID': 'BAD'}
        opts = CompilerOptions(cpp_options=cpp_opts)
        with self.assertRaises(ValueError):
            opts.validate()

        cpp_opts = {'OPENCL_PLATFORM_ID': -1}
        opts = CompilerOptions(cpp_options=cpp_opts)
        with self.assertRaises(ValueError):
            opts.validate()
github stan-dev / cmdstanpy / test / test_compiler_opts.py View on Github external
def test_opts_add(self):
        stanc_opts = {'warn-uninitialized': True}
        cpp_opts = {'STAN_OPENCL': 'TRUE', 'OPENCL_DEVICE_ID': 1}
        opts = CompilerOptions(stanc_options=stanc_opts, cpp_options=cpp_opts)
        opts.validate()
        opts_list = opts.compose()
        self.assertTrue('STAN_OPENCL=TRUE' in opts_list)
        self.assertTrue('OPENCL_DEVICE_ID=1' in opts_list)
        new_opts = CompilerOptions(
            cpp_options={'STAN_OPENCL': 'FALSE', 'OPENCL_DEVICE_ID': 2}
        )
        opts.add(new_opts)
        opts_list = opts.compose()
        self.assertTrue('STAN_OPENCL=FALSE' in opts_list)
        self.assertTrue('OPENCL_DEVICE_ID=2' in opts_list)

        expect = 'STANCFLAGS+=--include_paths=' + DATAFILES_PATH.replace(
            '\\', '/'
        )
        stanc_opts2 = {'include_paths': DATAFILES_PATH}
github stan-dev / cmdstanpy / cmdstanpy / model.py View on Github external
:param force: When ``True``, always compile, even if the executable file
            is newer than the source file.  Used for Stan models which have
            ``#include`` directives in order to force recompilation when changes
            are made to the included files.

        :param compiler_options: Options for stanc and C++ compilers.

        :param override_options: When ``True``, override existing option.
            When ``False``, add/replace existing options.  Default is ``False``.
        """
        if not self._stan_file:
            raise RuntimeError('Please specify source file')

        compiler_options = None
        if not (stanc_options is None and cpp_options is None):
            compiler_options = CompilerOptions(
                stanc_options=stanc_options, cpp_options=cpp_options
            )
            compiler_options.validate()
            if self._compiler_options is None:
                self._compiler_options = compiler_options
            elif override_options:
                self._compiler_options = compiler_options
            else:
                self._compiler_options.add(compiler_options)

        compilation_failed = False
        with TemporaryCopiedFile(self._stan_file) as (stan_file, is_copied):
            exe_file, _ = os.path.splitext(os.path.abspath(stan_file))
            exe_file = Path(exe_file).as_posix() + EXTENSION
            do_compile = True
            if os.path.exists(exe_file):