def setup(self): super().setup() if self.backend == 'cuda' and self.timers == 'hip-ext': raise ParameterError('hip-ext timers are not compatible with CUDA') template_file = os.path.join( os.path.dirname(os.path.abspath(__file__)), 'templates', self.template_file()) code = template.render(template_file, **self.template_args()) if self.print_code: print(cpphelpers.format_code(code)) self.compiler_flags = (self.default_compiler_flags() + ' ' + self.compiler_flags).strip() try: self.compiled = compilation.GnuLibrary(code, [self.compiler] + self.compiler_flags.split()) except compilation.CompilationError as error: raise ParameterError(*error.args) from error if self.verify and self.dry_runs: warnings.warn( 'using --dry-runs together with verification might lead to ' 'false negatives for stencils with read-write fields')
def test_compilation_error(self): code = ''' extern "C" int foo(float *f, int i) { return 0 // missing semicolon } ''' with self.assertRaises(compilation.CompilationError): compilation.GnuLibrary(code)
def test_c(self): code = ''' int func(int *i) { *i *= 2; return 0; }''' library = compilation.GnuLibrary(code, extension='.c') ival = ctypes.c_int(42) library.func(ctypes.byref(ival)) self.assertEqual(ival.value, 84)
def test_cpp(self): code = ''' extern "C" int func(float *f) { *f *= 2; return 0; } ''' library = compilation.GnuLibrary(code) fval = ctypes.c_float(42) library.func(ctypes.byref(fval)) self.assertEqual(fval.value, 84.0)
def setup(self): super().setup() template_file = os.path.join( os.path.dirname(os.path.abspath(__file__)), 'templates', self.template_file()) code = template.render(template_file, **self.template_args()) if self.print_code: print(cpphelpers.format_code(code)) if self.compiler.endswith('icpc'): os.environ['KMP_INIT_AT_FORK'] = '0' self.compiled = compilation.GnuLibrary(code, self.compile_command())
def test_output_capture(self): code = ''' #include <iostream> extern "C" int foo(float f, int i) { std::cout << (f * i) << std::endl; return 0; } ''' library = compilation.GnuLibrary(code) self.assertEqual( library.foo(0.5, 42, argtypes=[ctypes.c_float, ctypes.c_int]), '21\n') self.assertEqual( library.foo(2.0, 42, argtypes=[ctypes.c_float, ctypes.c_int]), '84\n')
def test_execution_error(self): code = ''' #include <iostream> extern "C" int foo(float *f, int i) { if (!f) { std::cerr << "nullpointer!" << std::endl; return 1; } *f *= i; return 0; } ''' library = compilation.GnuLibrary(code) fval = ctypes.c_float(0.5) self.assertEqual(library.foo(ctypes.byref(fval), 42), '') self.assertEqual(fval.value, 21.0) with self.assertRaisesRegex(compilation.ExecutionError, 'nullpointer!\n'): library.foo(ctypes.c_void_p(), 42)