diff --git a/pystencils/backends/cbackend.py b/pystencils/backends/cbackend.py index c4d1786399c7a7859975a53dd86b953d1d5db039..d590fd87c5b0c2cf8fb25739bb45f3371bd52daa 100644 --- a/pystencils/backends/cbackend.py +++ b/pystencils/backends/cbackend.py @@ -123,7 +123,7 @@ def get_headers(ast_node: Node) -> Set[str]: for h in headers: assert HEADER_REGEX.match(h), f'header /{h}/ does not follow the pattern /"..."/ or /<...>/' - return sorted(headers) + return headers # --------------------------------------- Backend Specific Nodes ------------------------------------------------------- diff --git a/pystencils/cpu/cpujit.py b/pystencils/cpu/cpujit.py index 7144a58bdae9c69cae195039ef0a05a7365053af..745bbcc84292ab100cb9349fbbd5185659bcf582 100644 --- a/pystencils/cpu/cpujit.py +++ b/pystencils/cpu/cpujit.py @@ -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.include import get_pystencils_include_path 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): @@ -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) if not os.path.exists(object_file): - with file_handle_for_atomic_write(src_file) as f: - code.write_to_file(f) + try: + with open(src_file, 'xw') as f: + code.write_to_file(f) + except FileExistsError: + pass if windows: compile_cmd = ['cl.exe', '/c', '/EHsc'] + compiler_config['flags'].split() diff --git a/pystencils/gpucuda/cudajit.py b/pystencils/gpucuda/cudajit.py index 03dfc824552607a57aabbb172f7d837f2abb5c1f..3f752700bdc85204f11dd486146801e3e75f414d 100644 --- a/pystencils/gpucuda/cudajit.py +++ b/pystencils/gpucuda/cudajit.py @@ -41,7 +41,7 @@ def make_python_function(kernel_function_node, argument_dict=None, custom_backen argument_dict = {} 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 += "#define FUNC_PREFIX __global__\n" diff --git a/pystencils/utils.py b/pystencils/utils.py index bdeab95363c651e4a7b31348521726d82e82c646..3afdbc582ef7dece1933dbaf5b00be149f9cbd30 100644 --- a/pystencils/utils.py +++ b/pystencils/utils.py @@ -51,33 +51,13 @@ def recursive_dict_update(d, u): 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 def atomic_file_write(file_path): target_folder = os.path.dirname(os.path.abspath(file_path)) with NamedTemporaryFile(delete=False, dir=target_folder) as f: f.file.close() yield f.name - os.rename(f.name, file_path) + os.replace(f.name, file_path) def fully_contains(l1, l2):