Skip to content
Snippets Groups Projects
Commit 13a1f590 authored by Markus Holzer's avatar Markus Holzer
Browse files

Some fixes

parent dadb684e
No related branches found
No related tags found
1 merge request!272Testing
...@@ -123,7 +123,7 @@ def get_headers(ast_node: Node) -> Set[str]: ...@@ -123,7 +123,7 @@ def get_headers(ast_node: Node) -> Set[str]:
for h in headers: for h in headers:
assert HEADER_REGEX.match(h), f'header /{h}/ does not follow the pattern /"..."/ or /<...>/' assert HEADER_REGEX.match(h), f'header /{h}/ does not follow the pattern /"..."/ or /<...>/'
return sorted(headers) return headers
# --------------------------------------- Backend Specific Nodes ------------------------------------------------------- # --------------------------------------- Backend Specific Nodes -------------------------------------------------------
......
...@@ -63,7 +63,7 @@ from pystencils.backends.cbackend import generate_c, get_headers, CFunction ...@@ -63,7 +63,7 @@ from pystencils.backends.cbackend import generate_c, get_headers, CFunction
from pystencils.data_types import cast_func, VectorType, vector_memory_access from pystencils.data_types import cast_func, VectorType, vector_memory_access
from pystencils.include import get_pystencils_include_path from pystencils.include import get_pystencils_include_path
from pystencils.kernel_wrapper import KernelWrapper from pystencils.kernel_wrapper import KernelWrapper
from pystencils.utils import atomic_file_write, file_handle_for_atomic_write, recursive_dict_update from pystencils.utils import atomic_file_write, recursive_dict_update
def make_python_function(kernel_function_node, custom_backend=None): def make_python_function(kernel_function_node, custom_backend=None):
...@@ -601,8 +601,11 @@ def compile_module(code, code_hash, base_dir, compile_flags=None): ...@@ -601,8 +601,11 @@ def compile_module(code, code_hash, base_dir, compile_flags=None):
object_file = os.path.join(base_dir, code_hash + object_suffix) object_file = os.path.join(base_dir, code_hash + object_suffix)
if not os.path.exists(object_file): if not os.path.exists(object_file):
with file_handle_for_atomic_write(src_file) as f: try:
code.write_to_file(f) with open(src_file, 'xw') as f:
code.write_to_file(f)
except FileExistsError:
pass
if windows: if windows:
compile_cmd = ['cl.exe', '/c', '/EHsc'] + compiler_config['flags'].split() compile_cmd = ['cl.exe', '/c', '/EHsc'] + compiler_config['flags'].split()
......
...@@ -41,7 +41,7 @@ def make_python_function(kernel_function_node, argument_dict=None, custom_backen ...@@ -41,7 +41,7 @@ def make_python_function(kernel_function_node, argument_dict=None, custom_backen
argument_dict = {} argument_dict = {}
header_list = ['<cstdint>'] + list(get_headers(kernel_function_node)) header_list = ['<cstdint>'] + list(get_headers(kernel_function_node))
includes = "\n".join(["#include %s" % (include_file,) for include_file in header_list]) includes = "\n".join([f"#include {include_file}" for include_file in header_list])
code = includes + "\n" code = includes + "\n"
code += "#define FUNC_PREFIX __global__\n" code += "#define FUNC_PREFIX __global__\n"
......
...@@ -51,33 +51,13 @@ def recursive_dict_update(d, u): ...@@ -51,33 +51,13 @@ def recursive_dict_update(d, u):
return d return d
@contextmanager
def file_handle_for_atomic_write(file_path):
"""Open temporary file object that atomically moves to destination upon exiting.
Allows reading and writing to and from the same filename.
The file will not be moved to destination in case of an exception.
Args:
file_path: path to file to be opened
"""
target_folder = os.path.dirname(os.path.abspath(file_path))
with NamedTemporaryFile(delete=False, dir=target_folder, mode='w') as f:
try:
yield f
finally:
f.flush()
os.fsync(f.fileno())
os.rename(f.name, file_path)
@contextmanager @contextmanager
def atomic_file_write(file_path): def atomic_file_write(file_path):
target_folder = os.path.dirname(os.path.abspath(file_path)) target_folder = os.path.dirname(os.path.abspath(file_path))
with NamedTemporaryFile(delete=False, dir=target_folder) as f: with NamedTemporaryFile(delete=False, dir=target_folder) as f:
f.file.close() f.file.close()
yield f.name yield f.name
os.rename(f.name, file_path) os.replace(f.name, file_path)
def fully_contains(l1, l2): def fully_contains(l1, l2):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment