Peano
Loading...
Searching...
No Matches
kernels.py
Go to the documentation of this file.
1import exahype2.api.dslhype as dslhype
2from exahype2.api import Target
3import os
4from enum import Enum
5
6
7
9 function,
10 function_namespace,
11 template_parameters,
12 functor_parameters,
13 pde_terms,
14 output_file_name,
15 target: Target,
16 stateless: bool,
17 function_name: str = None,
18 output_directory: str = ".",
19):
20 """!
21 function_name: str
22 Name of the generated C++ function. Defaults to the name of the
23 Python DSL function passed in as function.
24
25 output_directory: str
26 Directory the kernels/ subdirectory with the generated files is
27 created in, usually the Peano project's directory.
28
29 @remark Existing files are appended to, not overwritten
30 Several functions of one project end up in the same files
31 (kernels/{output_file_name}.h and .cpph/.cpp), so the translation appends.
32 Running a generator script a second time in the same directory therefore
33 duplicates the kernels, and the project does not compile
34 ("redefinition of ..."). Remove the kernels/ directory before you
35 regenerate.
36 @todo The generators should write every function into a file of its
37 own (and a thin header that includes them). Then each file can simply
38 be overwritten, and regenerating in place becomes safe.
39 """
40 if target == Target.Accelerator and stateless == False:
41 raise Exception("Accelerated kernels must be stateless")
42
43 reduced_functor_parameters = []
44 for functor in functor_parameters:
45 function_in_pde_terms = False
46 for pde_term in pde_terms:
47 if pde_term.NAME == functor.id:
48 function_in_pde_terms = True
49 if function_in_pde_terms:
50 reduced_functor_parameters.append(functor)
51
52 parser = dslhype.Parser()
53 tree = parser.parse(
54 function,
55 template_parameters,
56 reduced_functor_parameters,
57 pde_terms,
58 namespace=function_namespace,
59 stateless=stateless,
60 use_accelerator=(target==Target.Accelerator),
61 function_name=function_name,
62 )
63
64 function_declaration = tree.print_declaration()
65 if target == Target.Accelerator:
66 function_definition = tree.print_omp()
67 else:
68 function_definition = tree.print_cpp()
69 function_declaration_with_timer = tree.print_declaration_with_timer()
70 function_definition_with_timer = tree.print_definition_with_timer()
71
72 kernels_directory = os.path.join(output_directory, "kernels")
73 os.makedirs(kernels_directory, exist_ok=True)
74 file = open(os.path.join(kernels_directory, f"{output_file_name}.h"), "a")
75 file.write(function_declaration)
76 file.write(function_declaration_with_timer)
77 file.close()
78
79 file = open(os.path.join(kernels_directory, f"{output_file_name}.cpph"), "a")
80 file.write(function_definition)
81 file.write(function_definition_with_timer)
82 file.close()
83
84# if target == Target.Serial:
85# file = open(f"{file_name}.h", "a")
86# file.write(function_declaration)
87# file.write(function_declaration_with_timer)
88# file.close()
89# elif target == Target.Multicore:
90# file = open(f"{file_name}.h", "a")
91# file.write("#if defined(SharedOMP)")
92# file.write(function_declaration)
93# file.write(function_declaration_with_timer)
94# file.write("#endif")
95# file.close()
96# else: # Target.Accelerator
97# file = open(f"{file_name}.h", "a")
98# file.write("#if defined(GPUOffloadingOMP)")
99# file.write(function_declaration)
100# file.write(function_declaration_with_timer)
101# file.write("#endif")
102# file.close()
103#
104# if target == Target.Serial:
105# function_definition = tree.print_cpp()
106# file = open(f"{file_name}.cpph", "a")
107# file.write(function_definition)
108# file.write(function_definition_with_timer)
109# file.close()
110# elif target == Target.Multicore:
111# function_definition = tree.print_omp()
112# file = open(f"{file_name}.cpph", "a")
113# file.write("#if defined(SharedOMP)")
114# file.write(function_definition)
115# file.write(function_definition_with_timer)
116# file.write("#endif")
117# file.close()
118# else: # Target.Accelerator
119# function_definition = tree.print_omp()
120# file = open(f"{file_name}.cpph", "a")
121# file.write("#if defined(GPUOffloadingOMP)")
122# file.write(function_definition)
123# file.write(function_definition_with_timer)
124# file.write("#endif")
125# file.close()
translate_dslhype_function(function, function_namespace, template_parameters, functor_parameters, pde_terms, output_file_name, Target target, bool stateless, str function_name=None, str output_directory=".")
function_name: str Name of the generated C++ function.
Definition kernels.py:19