From 74510aa0a2f914fabfaf989cae9c7795f435662f Mon Sep 17 00:00:00 2001
From: Behzad Safaei <iwia103h@alex1.nhr.fau.de>
Date: Tue, 8 Apr 2025 18:48:45 +0200
Subject: [PATCH] Fix Likwid marker placements

---
 runtime/timers.hpp                           |  3 ++-
 src/pairs/code_gen/cgen.py                   | 16 +++++++++-------
 src/pairs/ir/module.py                       |  3 ---
 src/pairs/mapping/funcs.py                   |  3 +++
 src/pairs/transformations/instrumentation.py | 10 +++++-----
 5 files changed, 19 insertions(+), 16 deletions(-)

diff --git a/runtime/timers.hpp b/runtime/timers.hpp
index bb73df8..697a209 100644
--- a/runtime/timers.hpp
+++ b/runtime/timers.hpp
@@ -36,7 +36,8 @@ public:
     void writeToFile(int rank, int world_size){
         std::string filename = "timers_" + std::to_string(world_size) + ".txt";
         if (rank==0) std::cout << "Writing timers log to: " << filename << std::endl;
-
+        std::remove(filename.c_str());
+        
         MPI_File file;
         MPI_File_open(MPI_COMM_WORLD, filename.c_str(), MPI_MODE_WRONLY | MPI_MODE_CREATE, MPI_INFO_NULL, &file);
 
diff --git a/src/pairs/code_gen/cgen.py b/src/pairs/code_gen/cgen.py
index 4627631..272e1a9 100644
--- a/src/pairs/code_gen/cgen.py
+++ b/src/pairs/code_gen/cgen.py
@@ -43,7 +43,7 @@ class CGen:
         self.target = None
         self.print = None
         self.kernel_context = False
-        self.loop_scope = False
+        self.loop_depth = False
         self.generate_full_object_names = False
         self.ref = ref
         self.debug = debug
@@ -537,13 +537,13 @@ class CGen:
             self.print.add_indent(-4)
 
         if isinstance(ast_node, Continue):
-            if self.loop_scope:
+            if self.loop_depth:
                 self.print("continue;")
             else:
                 self.print("return;")
 
         if isinstance(ast_node, Break):
-            if self.loop_scope:
+            if self.loop_depth:
                 self.print("break;")
             else:
                 self.print("return;")
@@ -775,9 +775,10 @@ class CGen:
                 self.print("#pragma omp parallel for")
 
             self.print(f"for(int {iterator} = {lower_range}; {iterator} < {upper_range}; {iterator}++) {{")
-            self.loop_scope = True
+            parent_loop_scope = self.loop_depth
+            self.loop_depth = True
             self.generate_statement(ast_node.block)
-            self.loop_scope = False
+            self.loop_depth = parent_loop_scope
             self.print("}")
 
 
@@ -983,9 +984,10 @@ class CGen:
         if isinstance(ast_node, While):
             cond = self.generate_expression(ast_node.cond)
             self.print(f"while({cond}) {{")
-            self.loop_scope = True
+            parent_loop_scope = self.loop_depth
+            self.loop_depth = True
             self.generate_statement(ast_node.block)
-            self.loop_scope = False
+            self.loop_depth = parent_loop_scope
             self.print("}")
 
         if isinstance(ast_node, Return):
diff --git a/src/pairs/ir/module.py b/src/pairs/ir/module.py
index c7937ed..8b12358 100644
--- a/src/pairs/ir/module.py
+++ b/src/pairs/ir/module.py
@@ -38,9 +38,6 @@ class Module(ASTNode):
         self._return_type = Types.Void
         self._profile = profile
         
-        if profile:
-            self.sim.enable_profiler()
-
         if interface:
             sim.add_interface_module(self)
         else:
diff --git a/src/pairs/mapping/funcs.py b/src/pairs/mapping/funcs.py
index 062dfe3..bfa6f87 100644
--- a/src/pairs/mapping/funcs.py
+++ b/src/pairs/mapping/funcs.py
@@ -331,6 +331,9 @@ def compute(sim, func, cutoff_radius=None, symbols={}, parameters={}, compute_gl
     sim.init_block()
     sim.module_name(func.__name__)
 
+    if profile:
+        sim.enable_profiler()
+        
     if nparams == 1:
         for i in OneBodyKernel(sim, func.__name__, run_on_device=run_on_device, profile=profile):
             ir = BuildParticleIR(sim, symbols, parameters)
diff --git a/src/pairs/transformations/instrumentation.py b/src/pairs/transformations/instrumentation.py
index 5d039b8..049737d 100644
--- a/src/pairs/transformations/instrumentation.py
+++ b/src/pairs/transformations/instrumentation.py
@@ -15,15 +15,15 @@ class AddModulesInstrumentation(Mutator):
 
         if module.name == 'initialize' or module.name == 'end' or module.return_type!=Types.Void:
             return ast_node
-
-        if module.must_profile():
-            start_marker = Call_Void(ast_node.sim, "LIKWID_MARKER_START", [module.name])
-            stop_marker = Call_Void(ast_node.sim, "LIKWID_MARKER_STOP", [module.name])
-            module._block =  Block.from_list(ast_node.sim, [start_marker, module._block, stop_marker])
         
         timer_id = module.module_id + Timers.Offset
         start_timer = Call_Void(ast_node.sim, "pairs::start_timer", [timer_id])
         stop_timer = Call_Void(ast_node.sim, "pairs::stop_timer", [timer_id])
         module._block = Block.from_list(ast_node.sim, [start_timer, module._block, stop_timer])
 
+        if module.must_profile():
+            start_marker = Call_Void(ast_node.sim, "LIKWID_MARKER_START", [module.name])
+            stop_marker = Call_Void(ast_node.sim, "LIKWID_MARKER_STOP", [module.name])
+            module._block =  Block.from_list(ast_node.sim, [start_marker, module._block, stop_marker])
+            
         return ast_node
-- 
GitLab