Peano
Loading...
Searching...
No Matches
OMPPrinter.py
Go to the documentation of this file.
1import exahype2.dslhype.printers.CPPPrinter as CPPPrinter
2import exahype2.dslhype.SyntaxTree as SyntaxTree
3import os
4import copy
5
6class OMPPrinter(CPPPrinter):
7 def print_function_definition(self, function_definition: SyntaxTree.FunctionDefinition, indent_level = 0):
8 namespace_header = f"""namespace {"::".join(function_definition._namespaces)}::omp {{"""
9 namespace_footer = "}"
10 template_string = ""
11 if function_definition._template is not None:
12 template_strings = [argument.print(self) for argument in function_definition._template]
13 template_string = "template <" + ",".join(template_strings) + ">\n"
14
15 current_indent = indent_level + 1 # Indentation increases inside namespace
16 statement_prints = [statement.print(self, current_indent + 1) for statement in function_definition._body]
17 argument_prints = [argument.print(self) for argument in function_definition._arguments]
18
19 return f"""{namespace_header}
20{template_string}{self.get_indent(indent_level)}{function_definition._return_type.print(self)} {function_definition.id}({', '.join(argument_prints)}) {{
21{os.linesep.join(statement_prints)}
22{self.get_indent(indent_level)}}}
23{namespace_footer}
24"""
25
26 def print_function_declaration(self, function_definition: SyntaxTree.FunctionDefinition, indent_level=0):
27 namespace_header = f"""namespace {"::".join(function_definition._namespaces)}::omp {{"""
28 namespace_footer = "}"
29 template_string = ""
30 if function_definition._template is not None:
31 template_strings = [argument.print(self) for argument in function_definition._template]
32 template_string = "template <" + ",".join(template_strings) + ">\n"
33
34 current_indent = indent_level + 1 # Indentation increases inside namespace
35 argument_prints = [argument.print(self) for argument in function_definition._arguments]
36
37 return f"""{namespace_header}
38{template_string}{self.get_indent(indent_level)}{function_definition._return_type.print(self)} {function_definition.id}({', '.join(argument_prints)});
39{namespace_footer}
40"""
41
42 def print_function_definition_with_timer(self, function_definition: SyntaxTree.FunctionDefinition, indent_level=0):
43 namespace_header = f"""namespace {"::".join(function_definition._namespaces)}::omp {{"""
44 namespace_footer = "}"
45 template_string = ""
46 function_call_string = function_definition.id
47 if function_definition._template is not None:
48 template_strings = [argument.print(self) for argument in function_definition._template]
49 template_string = "template <" + ",".join(template_strings) + ">\n"
50 function_call_string += "<" + ",".join([template.split(' ')[1] for template in template_strings]) + ">"
51
52 arguments = copy.deepcopy(function_definition._arguments)
53 arguments.append(SyntaxTree.Argument("measurement", SyntaxTree.TCustom("tarch::timing::Measurement&")))
54
55 current_indent = indent_level + 1 # Indentation increases inside namespace
56 argument_prints = [argument.print(self) for argument in arguments]
57
58 return f"""{namespace_header}
59{template_string}{self.get_indent(indent_level)}{function_definition._return_type.print(self)} {function_definition.id}({', '.join(argument_prints)}) {{
60tarch::timing::Watch watch("{"::".join(function_definition._namespaces)}", "{function_definition.id}", false, true);
61{function_call_string}({",".join([argument.print(self).split(' ')[-1] for argument in function_definition._arguments])});
62watch.stop();
63measurement.setValue(watch.getCalendarTime());
64{self.get_indent(indent_level)}}}
65{namespace_footer}
66"""
67
68 def print_function_declaration_with_timer(self, function_definition: SyntaxTree.FunctionDefinition, indent_level=0):
69 namespace_header = f"""namespace {"::".join(function_definition._namespaces)}::omp {{"""
70 namespace_footer = "}"
71 template_string = ""
72 if function_definition._template is not None:
73 template_strings = [argument.print(self) for argument in function_definition._template]
74 template_string = "template <" + ",".join(template_strings) + ">\n"
75
76 arguments = copy.deepcopy(function_definition._arguments)
77 arguments.append(SyntaxTree.Argument("measurement", SyntaxTree.TCustom("tarch::timing::Measurement&")))
78
79 current_indent = indent_level + 1 # Indentation increases inside namespace
80 argument_prints = [argument.print(self) for argument in arguments]
81
82 return f"""{namespace_header}
83{template_string}{self.get_indent(indent_level)}{function_definition._return_type.print(self)} {function_definition.id}({', '.join(argument_prints)});
84{namespace_footer}
85"""
86
87 def print_memory_allocation(self, memory_allocation: SyntaxTree.MemoryAllocation, indent_level=0):
88 if SyntaxTree.Node.use_accelerator == False:
89 if len(memory_allocation._dimensions) == 1:
90 return f"""{self.get_indent(indent_level)}{memory_allocation._type.print(self) + "* " if memory_allocation._specify_type else ""}{memory_allocation._name.print(self)} = new {memory_allocation._type.print(self)}[{memory_allocation._dimensions[0].print(self)}];"""
91 else:
92 return f"""{self.get_indent(indent_level)}{memory_allocation._type.print(self)}** {memory_allocation._name.print(self)} = new {memory_allocation._type.print(self)}*[{memory_allocation._dimensions[-1].print(self)}];
93{memory_allocation._loop.print(self, indent_level)}"""
94 else:
95 if len(memory_allocation._dimensions) == 1:
96 if SyntaxTree.Node.use_memory_manager == True:
97 return f"""{self.get_indent(indent_level)}{memory_allocation._type.print(self) + "* " if memory_allocation._specify_type else ""}{memory_allocation._name.print(self)} = tarch::accelerator::GPUMemoryManager::getInstance().allocate<{memory_allocation._type.print(self)}>({memory_allocation._dimensions[0].print(self)}, targetDevice);"""
98 else:
99 return f"""{self.get_indent(indent_level)}{memory_allocation._type.print(self) + "* " if memory_allocation._specify_type else ""}{memory_allocation._name.print(self)} = ({memory_allocation._type.print(self)}*)omp_target_alloc(sizeof({memory_allocation._type.print(self)}) * {memory_allocation._dimensions[0].print(self)}, targetDevice);"""
100 else:
101 if SyntaxTree.Node.use_memory_manager == True:
102 return f"""{self.get_indent(indent_level)}{memory_allocation._type.print(self)}* {memory_allocation._name.print(self)} = tarch::accelerator::GPUMemoryManager::getInstance().allocate<{memory_allocation._type.print(self)}>({(memory_allocation._size * memory_allocation._dimensions[-1]).print(self)}, targetDevice);"""
103 else:
104 return f"""{self.get_indent(indent_level)}{memory_allocation._type.print(self)}* {memory_allocation._name.print(self)} = ({memory_allocation._type.print(self)}*)omp_target_alloc(sizeof({memory_allocation._type.print(self)}) * {(memory_allocation._size * memory_allocation._dimensions[-1]).print(self)}, targetDevice);"""
105
106 def print_memory_deallocation(self, memory_deallocation: SyntaxTree.MemoryDeallocation, indent_level=0):
107 if SyntaxTree.Node.use_accelerator == False:
108 if len(memory_deallocation._dimensions) == 1:
109 return f"""{self.get_indent(indent_level)}delete[] {memory_deallocation._name.print(self)};"""
110 else:
111 return f"""{memory_deallocation._loop.print(self, indent_level)}
112{self.get_indent(indent_level)}delete[] {memory_deallocation._name.print(self)};"""
113 else:
114 if SyntaxTree.Node.use_memory_manager:
115 return f"""{self.get_indent(indent_level)}tarch::accelerator::GPUMemoryManager::getInstance().free({memory_deallocation._name.print(self)}, targetDevice);"""
116 else:
117 return f"""{self.get_indent(indent_level)}omp_target_free({memory_deallocation._name.print(self)}, targetDevice);"""
118
119 def print_data_block_unary_max(self, unary_max: SyntaxTree.DataBlockUnaryMax, indent_level=0):
120 if SyntaxTree.Node.use_accelerator:
121 return f"""{self.get_indent(indent_level)}omp_target_memset(maxEigenvalues, 0, N * sizeof(double), targetDevice);
122{self.get_indent(indent_level)}for (int {unary_max._loop._iteration_variable.print(self)} = {unary_max._loop._iteration_range[0].print(self)}; {unary_max._loop._iteration_variable.print(self)} < {unary_max._loop._iteration_range[1].print(self)}; {unary_max._loop._iteration_variable.print(self)}++) {{
123{self.get_indent(indent_level)}#pragma omp target teams loop collapse(Dimensions) reduction(max:{unary_max._outputVariable.multidimensional_index([unary_max._loop.get_iteration_variable()]).print(self)})
124{unary_max._loop._statements[1].print(self, indent_level + 1)}
125{self.get_indent(indent_level)}}}
126"""
127 else:
128 return unary_max._loop.print(self, indent_level)
129
130 def print_data_block_construction_from_function(self, construction: SyntaxTree.DataBlockConstructionFromFunction, indent_level=0):
131 if SyntaxTree.Node.use_accelerator == True:
132 omp_pragma = "#pragma omp target teams loop collapse(Dimensions + 1) device(targetDevice)"
133 else:
134 omp_pragma = "#pragma omp parallel for simd collapse(Dimensions + 1)"
135 return f"""{construction.memoryAllocation.print(self, indent_level)}
136{self.get_indent(indent_level)}{omp_pragma}
137{construction.initialisation.print(self, indent_level)}"""
138
139 def print_data_block_assignment(self, assignment: SyntaxTree.DataBlockAssignment, indent_level=0):
140 if SyntaxTree.Node.use_accelerator == True:
141 omp_pragma = "#pragma omp target teams loop collapse(Dimensions + 1) device(targetDevice)"
142 else:
143 omp_pragma = "#pragma omp parallel for simd collapse(Dimensions + 1)"
144 return f"""{self.get_indent(indent_level)}{omp_pragma}
145{assignment._loop.print(self, indent_level)}
146"""
print_data_block_construction_from_function(self, SyntaxTree.DataBlockConstructionFromFunction construction, indent_level=0)
print_function_declaration_with_timer(self, SyntaxTree.FunctionDefinition function_definition, indent_level=0)
Definition OMPPrinter.py:68
print_memory_allocation(self, SyntaxTree.MemoryAllocation memory_allocation, indent_level=0)
Definition OMPPrinter.py:87
print_memory_deallocation(self, SyntaxTree.MemoryDeallocation memory_deallocation, indent_level=0)
print_function_declaration(self, SyntaxTree.FunctionDefinition function_definition, indent_level=0)
Definition OMPPrinter.py:26
print_function_definition_with_timer(self, SyntaxTree.FunctionDefinition function_definition, indent_level=0)
Definition OMPPrinter.py:42
print_function_definition(self, SyntaxTree.FunctionDefinition function_definition, indent_level=0)
Definition OMPPrinter.py:7
print_data_block_unary_max(self, SyntaxTree.DataBlockUnaryMax unary_max, indent_level=0)
print_data_block_assignment(self, SyntaxTree.DataBlockAssignment assignment, indent_level=0)