Skip to content

Commit

Permalink
Merge pull request #137 from RRZE-HPC/feature/pycparser_v2.21
Browse files Browse the repository at this point in the history
Update to pycparser v2.21 and fix also sympy test-cases
  • Loading branch information
cod3monk authored Feb 17, 2022
2 parents 1cfd179 + 2438d74 commit 2181b52
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 50 deletions.
64 changes: 17 additions & 47 deletions kerncraft/kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,42 +38,6 @@
from .pycparser_utils import clean_code, replace_id


class LessParanthesizingCGenerator(CGenerator):
def get_BinaryOp_precedence(self, n):
"""
Gives precedence for op of n, otherwise -1.
Lower number have precedence over higher numbers.
"""
binary_op_precedence = {
# based on https://en.cppreference.com/w/c/language/operator_precedence
'*': 3, '%': 3, '/': 3,
'-': 4, '+': 4,
'<<': 5, '>>': 5,
'<': 6, '<=': 6, '>': 6, '>=': 6,
'==': 7, '!=': 7,
'&': 8,
'^': 9,
'|': 10,
'&&': 11,
'||': 12
}
if not isinstance(n, c_ast.BinaryOp):
return -1
else:
return binary_op_precedence[n.op]

def visit_BinaryOp(self, n):
p = self.get_BinaryOp_precedence(n)
lval_str = self._parenthesize_if(n.left,
lambda d: not self._is_simple_node(d) and
self.get_BinaryOp_precedence(d) > p)
rval_str = self._parenthesize_if(n.right,
lambda d: not self._is_simple_node(d) and
self.get_BinaryOp_precedence(d) >= p)
return '%s %s %s' % (lval_str, n.op, rval_str)


@contextmanager
def set_recursionlimit(new_limit):
old_limit = sys.getrecursionlimit()
Expand Down Expand Up @@ -199,8 +163,13 @@ def transform_array_decl_to_malloc(decl, with_init=True, cl_size=32):
'*',
c_ast.UnaryOp(
'sizeof',
c_ast.Typename(None, [], c_ast.TypeDecl(
None, [], decl.type.type.type))),
c_ast.Typename(
name=None,
quals=[],
align=[],
type=c_ast.TypeDecl(
None, [], [], decl.type.type.type),
coord=[])),
decl.type.dim),
c_ast.Constant('int', str(cl_size))]))
decl.type = type_
Expand Down Expand Up @@ -1219,7 +1188,7 @@ def _build_const_declartions(self, with_init=True):
# const long long N = strtoul(argv[2])
# with increasing N and 1
# TODO change subscript of argv depending on constant count
type_decl = c_ast.TypeDecl(k.name, ['const'], c_ast.IdentifierType(index_type))
type_decl = c_ast.TypeDecl(k.name, ['const'], [], c_ast.IdentifierType(index_type))
init = None
if with_init:
init = c_ast.FuncCall(
Expand All @@ -1228,8 +1197,8 @@ def _build_const_declartions(self, with_init=True):
c_ast.Constant('int', str(i)))]))
i += 1
decls.append(c_ast.Decl(
k.name, ['const'], [], [],
type_decl, init, None))
name=k.name, quals=['const'], align=[], storage=[], funcspec=[],
type=type_decl, init=init, bitsize=None))

return decls

Expand Down Expand Up @@ -1346,10 +1315,11 @@ def _build_kernel_function_declaration(self, name='kernel'):
scalar_declarations = self.get_scalar_declarations(pointer=True, suffix='_')
const_declarations = self._build_const_declartions(with_init=False)
return c_ast.FuncDecl(args=c_ast.ParamList(params=array_declarations +
scalar_declarations +
const_declarations),
scalar_declarations +
const_declarations),
type=c_ast.TypeDecl(declname=name,
quals=[],
align=[],
type=c_ast.IdentifierType(names=['void'])))

def get_scalar_declarations(self, pointer=False, suffix=''):
Expand Down Expand Up @@ -1409,7 +1379,7 @@ def get_kernel_header(self, name='kernel'):
else: # lock_mode == fcntl.LOCK_EX
# needs update
func_decl = self._build_kernel_function_declaration(name=name)
code = LessParanthesizingCGenerator().visit(
code = CGenerator().visit(
c_ast.FileAST(ext=[func_decl]))
with open(file_path, 'w') as f:
f.write(code)
Expand Down Expand Up @@ -1466,13 +1436,13 @@ def get_kernel_code(self, openmp=False, name='kernel'):

function_ast = c_ast.FuncDef(decl=c_ast.Decl(
name=name, type=self._build_kernel_function_declaration(name=name), quals=[],
storage=[], funcspec=[], init=None, bitsize=None),
align=[], storage=[], funcspec=[], init=None, bitsize=None),
body=c_ast.Compound(block_items=kernel),
param_decls=None)

# Generate code
with set_recursionlimit(100000):
code = LessParanthesizingCGenerator().visit(function_ast)
code = CGenerator().visit(function_ast)

if not openmp:
# remove all omp pragmas
Expand Down Expand Up @@ -1592,7 +1562,7 @@ def get_main_code(self, kernel_function_name='kernel'):
replace_id(ast, "INIT_ARRAYS", self._build_array_initializations(array_dimensions))

# Generate code
code = LessParanthesizingCGenerator().visit(ast)
code = CGenerator().visit(ast)

# Insert missing #includes from template to top of code
code = '\n'.join([l for l in template_code.split('\n') if l.startswith("#include")]) + \
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def find_version(*file_paths):
'pycachesim>=0.2.3',
'numpy',
'requests',
'pycparser>=2.19',
'pycparser>=2.21',
'osaca>=0.4.0',
'psutil',
'compress_pickle',
Expand Down
5 changes: 3 additions & 2 deletions tests/test_layer_condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from sympy import oo

from kerncraft import kerncraft as kc
from kerncraft.kernel import symbol_pos_int
from kerncraft.prefixedunit import PrefixedUnit


Expand Down Expand Up @@ -66,7 +67,7 @@ def test_3d_7pt(self):
with open(store_file, 'rb') as f:
results = pickle.load(f)
result = next(iter(results.values()))
N, M, i, j, k = sympy.var('N, M, i, j, k')
N, M, i, j, k = [symbol_pos_int(c) for c in 'NMijk']
result_expected = {'accesses':
{'a': [(k - 1, j, i),
(k, j - 1, i),
Expand Down Expand Up @@ -167,7 +168,7 @@ def test_constantdim(self):
results = pickle.load(f)
result = next(iter(results.values()))

N, M, j, i = sympy.var('N'), sympy.var('M'), sympy.var('j'), sympy.var('i')
N, M, j, i = [symbol_pos_int(c) for c in 'NMji']
result_expected = \
{'accesses': {'W': [(0, j, i), (1, j, i)],
'a': [(j - 1, i), (j, i - 1), (j, i), (j, i + 1), (j + 1, i)],
Expand Down

0 comments on commit 2181b52

Please sign in to comment.