Skip to content
Snippets Groups Projects

Fix field size

Merged Markus Holzer requested to merge holzer/pystencils:FixFieldSize into master
1 unresolved thread
Files
3
+ 34
20
@@ -375,8 +375,11 @@ def create_function_boilerplate_code(parameter_info, name, ast_node, insert_chec
np_dtype = field.dtype.numpy_dtype
item_size = np_dtype.itemsize
aligned = any([a.lhs.args[2] for a in ast_node.assignments if
isinstance(a.lhs, cast_func) and isinstance(a.lhs.dtype, VectorType)])
aligned = False
if ast_node.assignments:
aligned = any([a.lhs.args[2] for a in ast_node.assignments
if hasattr(a, 'lhs') and isinstance(a.lhs, cast_func)
and hasattr(a.lhs, 'dtype') and isinstance(a.lhs.dtype, VectorType)])
if ast_node.instruction_set and aligned:
byte_width = ast_node.instruction_set['width'] * item_size
@@ -497,12 +500,16 @@ class ExtensionModuleCode:
self._ast_nodes = []
self._function_names = []
self._custom_backend = custom_backend
self._code_string = str()
self._code_hash = None
def add_function(self, ast, name=None):
self._ast_nodes.append(ast)
self._function_names.append(name if name is not None else ast.function_name)
def write_to_file(self, restrict_qualifier, function_prefix, file):
def create_code_string(self, restrict_qualifier, function_prefix):
self._code_string = str()
headers = {'<math.h>', '<stdint.h>'}
for ast in self._ast_nodes:
headers.update(get_headers(ast))
@@ -511,19 +518,28 @@ class ExtensionModuleCode:
header_list.insert(0, '"Python.h"')
includes = "\n".join(["#include %s" % (include_file,) for include_file in header_list])
print(includes, file=file)
print("\n", file=file)
print(f"#define RESTRICT {restrict_qualifier}", file=file)
print(f"#define FUNC_PREFIX {function_prefix}", file=file)
print("\n", file=file)
self._code_string += includes
self._code_string += "\n"
self._code_string += f"#define RESTRICT {restrict_qualifier} \n"
self._code_string += f"#define FUNC_PREFIX {function_prefix}"
self._code_string += "\n"
for ast, name in zip(self._ast_nodes, self._function_names):
old_name = ast.function_name
ast.function_name = "kernel_" + name
print(generate_c(ast, custom_backend=self._custom_backend), file=file)
print(create_function_boilerplate_code(ast.get_parameters(), name, ast), file=file)
self._code_string += generate_c(ast, custom_backend=self._custom_backend)
self._code_string += create_function_boilerplate_code(ast.get_parameters(), name, ast)
ast.function_name = old_name
print(create_module_boilerplate_code(self.module_name, self._function_names), file=file)
self._code_hash = "mod_" + hashlib.sha256(self._code_string.encode()).hexdigest()
self._code_string += create_module_boilerplate_code(self._code_hash, self._function_names)
def get_hash_of_code(self):
return self._code_hash
def write_to_file(self, file):
assert self._code_string, "The code must be generated first"
print(self._code_string, file=file)
def compile_module(code, code_hash, base_dir):
@@ -531,12 +547,10 @@ def compile_module(code, code_hash, base_dir):
extra_flags = ['-I' + get_paths()['include'], '-I' + get_pystencils_include_path()]
if compiler_config['os'].lower() == 'windows':
function_prefix = '__declspec(dllexport)'
lib_suffix = '.pyd'
object_suffix = '.obj'
windows = True
else:
function_prefix = ''
lib_suffix = '.so'
object_suffix = '.o'
windows = False
@@ -547,7 +561,7 @@ def compile_module(code, code_hash, base_dir):
if not os.path.exists(object_file):
with file_handle_for_atomic_write(src_file) as f:
code.write_to_file(compiler_config['restrict_qualifier'], function_prefix, f)
code.write_to_file(f)
if windows:
compile_cmd = ['cl.exe', '/c', '/EHsc'] + compiler_config['flags'].split()
@@ -581,15 +595,15 @@ def compile_module(code, code_hash, base_dir):
def compile_and_load(ast, custom_backend=None):
cache_config = get_cache_config()
generated_code = generate_c(ast, dialect='c', custom_backend=custom_backend)
fields_accessed = str(ast.fields_accessed)
compiler_config = get_compiler_config()
function_prefix = '__declspec(dllexport)' if compiler_config['os'].lower() == 'windows' else ''
# Also the information of the field size should be contained in the hash string. Due to padding the generated code
# can look similar for different field sizes.
code_hash_str = "mod_" + hashlib.sha256((generated_code + fields_accessed).encode()).hexdigest()
code = ExtensionModuleCode(module_name=code_hash_str, custom_backend=custom_backend)
code = ExtensionModuleCode(custom_backend=custom_backend)
code.add_function(ast, ast.function_name)
code.create_code_string(compiler_config['restrict_qualifier'], function_prefix)
code_hash_str = code.get_hash_of_code()
if cache_config['object_cache'] is False:
with TemporaryDirectory() as base_dir:
lib_file = compile_module(code, code_hash_str, base_dir)