def test_new_kernel_file(tmpdir, monkeypatch): ''' Check that we write out the transformed kernel to the CWD. ''' from fparser.two import Fortran2003, parser from fparser.common.readfortran import FortranFileReader # Ensure kernel-output directory is uninitialised config = Config.get() monkeypatch.setattr(config, "_kernel_output_dir", "") monkeypatch.setattr(config, "_kernel_naming", "multiple") # Change to temp dir (so kernel written there) old_cwd = tmpdir.chdir() psy, invoke = get_invoke("nemolite2d_alg_mod.f90", api="gocean1.0", idx=0) sched = invoke.schedule kern = sched.children[0].loop_body[0].loop_body[0] rtrans = ACCRoutineTrans() _, _ = rtrans.apply(kern) # Generate the code (this triggers the generation of a new kernel) code = str(psy.gen).lower() # Work out the value of the tag used to re-name the kernel tag = re.search('use continuity(.+?)_mod', code).group(1) assert ("use continuity{0}_mod, only: continuity{0}_code".format(tag) in code) assert "call continuity{0}_code(".format(tag) in code # The kernel and module name should have gained the tag just identified # and be written to the CWD filename = os.path.join(str(tmpdir), "continuity{0}_mod.f90".format(tag)) assert os.path.isfile(filename) # Parse the new kernel file f2003_parser = parser.ParserFactory().create() reader = FortranFileReader(filename) prog = f2003_parser(reader) # Check that the module has the right name modules = walk_ast(prog.content, [Fortran2003.Module_Stmt]) assert str(modules[0].items[1]) == "continuity{0}_mod".format(tag) # Check that the subroutine has the right name subs = walk_ast(prog.content, [Fortran2003.Subroutine_Stmt]) found = False for sub in subs: if str(sub.items[1]) == "continuity{0}_code".format(tag): found = True break assert found # Check that the kernel type has been re-named dtypes = walk_ast(prog.content, [Fortran2003.Derived_Type_Def]) names = walk_ast(dtypes[0].content, [Fortran2003.Type_Name]) assert str(names[0]) == "continuity{0}_type".format(tag) from gocean1p0_build import GOcean1p0Build # If compilation fails this will raise an exception GOcean1p0Build(tmpdir).compile_file(filename) old_cwd.chdir()
def perform(self, targs): reader = readfortran.FortranFileReader(targs.data, ignore_comments=False) parser = fortranparser.ParserFactory().create(std="f2008") tree = parser(reader) if targs.output: with open(targs.output, "wb") as f: synnode = Node(tree, None) pickle.dump(synnode, f) self.add_forward(data=tree)