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
8def translate_dslhype_function(
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 if target == Target.Accelerator and stateless == False:
30 raise Exception("Accelerated kernels must be stateless")
31
32 reduced_functor_parameters = []
33 for functor in functor_parameters:
34 function_in_pde_terms = False
35 for pde_term in pde_terms:
36 if pde_term.NAME == functor.id:
37 function_in_pde_terms = True
38 if function_in_pde_terms:
39 reduced_functor_parameters.append(functor)
40
41 parser = dslhype.Parser()
42 tree = parser.parse(
43 function,
44 template_parameters,
45 reduced_functor_parameters,
46 pde_terms,
47 namespace=function_namespace,
48 stateless=stateless,
49 use_accelerator=(target==Target.Accelerator),
50 function_name=function_name,
51 )
52
53 function_declaration = tree.print_declaration()
54 if target == Target.Accelerator:
55 function_definition = tree.print_omp()
56 else:
57 function_definition = tree.print_cpp()
58 function_declaration_with_timer = tree.print_declaration_with_timer()
59 function_definition_with_timer = tree.print_definition_with_timer()
60
61 kernels_directory = os.path.join(output_directory, "kernels")
62 os.makedirs(kernels_directory, exist_ok=True)
63 file = open(os.path.join(kernels_directory, f"{output_file_name}.h"), "a")
64 file.write(function_declaration)
65 file.write(function_declaration_with_timer)
66 file.close()
67
68 file = open(os.path.join(kernels_directory, f"{output_file_name}.cpph"), "a")
69 file.write(function_definition)
70 file.write(function_definition_with_timer)
71 file.close()
72
73# if target == Target.Serial:
74# file = open(f"{file_name}.h", "a")
75# file.write(function_declaration)
76# file.write(function_declaration_with_timer)
77# file.close()
78# elif target == Target.Multicore:
79# file = open(f"{file_name}.h", "a")
80# file.write("#if defined(SharedOMP)")
81# file.write(function_declaration)
82# file.write(function_declaration_with_timer)
83# file.write("#endif")
84# file.close()
85# else: # Target.Accelerator
86# file = open(f"{file_name}.h", "a")
87# file.write("#if defined(GPUOffloadingOMP)")
88# file.write(function_declaration)
89# file.write(function_declaration_with_timer)
90# file.write("#endif")
91# file.close()
92#
93# if target == Target.Serial:
94# function_definition = tree.print_cpp()
95# file = open(f"{file_name}.cpph", "a")
96# file.write(function_definition)
97# file.write(function_definition_with_timer)
98# file.close()
99# elif target == Target.Multicore:
100# function_definition = tree.print_omp()
101# file = open(f"{file_name}.cpph", "a")
102# file.write("#if defined(SharedOMP)")
103# file.write(function_definition)
104# file.write(function_definition_with_timer)
105# file.write("#endif")
106# file.close()
107# else: # Target.Accelerator
108# function_definition = tree.print_omp()
109# file = open(f"{file_name}.cpph", "a")
110# file.write("#if defined(GPUOffloadingOMP)")
111# file.write(function_definition)
112# file.write(function_definition_with_timer)
113# file.write("#endif")
114# file.close()