From 4e64f6dbb33de11398cbe349fb65f4116d98e690 Mon Sep 17 00:00:00 2001
From: Giovanni La Mura <giovanni.lamura@inaf.it>
Date: Fri, 3 May 2024 19:24:02 +0200
Subject: [PATCH] Give each thread its own logger

---
 src/cluster/cluster.cpp | 34 +++++++++++++++++-----------------
 1 file changed, 17 insertions(+), 17 deletions(-)

diff --git a/src/cluster/cluster.cpp b/src/cluster/cluster.cpp
index 665dcc6a..422dc193 100644
--- a/src/cluster/cluster.cpp
+++ b/src/cluster/cluster.cpp
@@ -53,7 +53,7 @@ using namespace std;
 
 // I would like to put it all in a struct, but then I'd have to write a constructor for it, due to members defined as references, creating a worse nightmare than the one I'd like to simplify...
 
-int cluster_jxi488_cycle(int jxi488, ScattererConfiguration *sconf, GeometryConfiguration *gconf, ScatteringAngles *sa, ClusterIterationData *cid, FILE *output, const string& output_path, fstream& tppoan, Logger *logger);
+int cluster_jxi488_cycle(int jxi488, ScattererConfiguration *sconf, GeometryConfiguration *gconf, ScatteringAngles *sa, ClusterIterationData *cid, FILE *output, const string& output_path, fstream& tppoan);
 
 /*! \brief C++ implementation of CLU
  *
@@ -176,7 +176,7 @@ void cluster(const string& config_file, const string& data_file, const string& o
       // do the first iteration on jxi488 separately, since it seems to be different from the others
       int jxi488 = 1;
       chrono::time_point<chrono::high_resolution_clock> start_iter_1 = chrono::high_resolution_clock::now();
-      int jer = cluster_jxi488_cycle(jxi488, sconf, gconf, p_scattering_angles, cid, output, output_path, tppoan, logger);
+      int jer = cluster_jxi488_cycle(jxi488, sconf, gconf, p_scattering_angles, cid, output, output_path, tppoan);
       chrono::time_point<chrono::high_resolution_clock> end_iter_1 = chrono::high_resolution_clock::now();
       elapsed = start_iter_1 - t_start;
       message = "INFO: Calculation setup took " + to_string(elapsed.count()) + "s.\n";
@@ -222,7 +222,7 @@ void cluster(const string& config_file, const string& data_file, const string& o
 	// ok, now I can actually start the parallel calculations
 #pragma omp for
 	for (jxi488 = 2; jxi488 <= nxi; jxi488++) {
-	  int jer = cluster_jxi488_cycle(jxi488, sconf, gconf, p_scattering_angles, cid_2, output_2, output_path, *tppoanp_2, logger);
+	  int jer = cluster_jxi488_cycle(jxi488, sconf, gconf, p_scattering_angles, cid_2, output_2, output_path, *tppoanp_2);
 	}
 
 #pragma omp barrier
@@ -233,15 +233,12 @@ void cluster(const string& config_file, const string& data_file, const string& o
 	  tppoanp_2->close();
 	  delete tppoanp_2;
 	}
-#pragma omp barrier
-	{
-	  message = "INFO: Closing thread-local output files of thread " + to_string(myompthread) + " and syncing threads.\n";
-	  logger->log(message);
-	}
       } // closes pragma omp parallel
 #ifdef _OPENMP
 #pragma omp barrier
       {
+	message = "INFO: Thread-local output files closed and threads synchronized.\n";
+	logger->log(message);
 	// thread 0 already wrote on global files, skip it and take care of appending the others
 	chrono::time_point<chrono::high_resolution_clock> t_output_start = chrono::high_resolution_clock::now();
 	for (int ri = 1; ri < ompnumthreads; ri++) {
@@ -308,11 +305,12 @@ void cluster(const string& config_file, const string& data_file, const string& o
   delete logger;
 }
 
-int cluster_jxi488_cycle(int jxi488, ScattererConfiguration *sconf, GeometryConfiguration *gconf, ScatteringAngles *sa, ClusterIterationData *cid, FILE *output, const string& output_path, fstream& tppoan, Logger *logger)
+int cluster_jxi488_cycle(int jxi488, ScattererConfiguration *sconf, GeometryConfiguration *gconf, ScatteringAngles *sa, ClusterIterationData *cid, FILE *output, const string& output_path, fstream& tppoan)
 {
   int nxi = sconf->number_of_scales;
+  Logger *logger = new Logger(LOG_INFO);
   string message = "INFO: running scale iteration " + to_string(jxi488) + " of " + to_string(nxi) + ".\n";
-  // logger->log(message);
+  logger->log(message);
   chrono::duration<double> elapsed;
   chrono::time_point<chrono::high_resolution_clock> interval_start, interval_end;
   int jer = 0;
@@ -352,6 +350,7 @@ int cluster_jxi488_cycle(int jxi488, ScattererConfiguration *sconf, GeometryConf
   hjv(exri, vkarg, jer, lcalc, cid->arg, cid->c1, cid->c1ao, cid->c4);
   if (jer != 0) {
     fprintf(output, "  STOP IN HJV\n");
+    delete logger;
     return jer;
     // break; // rewrite this to go to the end of the function, to free locally allocated variables and return jer
   }
@@ -381,11 +380,13 @@ int cluster_jxi488_cycle(int jxi488, ScattererConfiguration *sconf, GeometryConf
 	  );
       if (jer != 0) {
 	fprintf(output, "  STOP IN DME\n");
+	delete logger;
 	return jer;
 	//break;
       }
     }
     if (jer != 0) {
+      delete logger;
       return jer;
       //break;
     }
@@ -395,13 +396,13 @@ int cluster_jxi488_cycle(int jxi488, ScattererConfiguration *sconf, GeometryConf
   interval_end = chrono::high_resolution_clock::now();
   elapsed = interval_end - interval_start;
   message = "INFO: matrix calculation for scale " + to_string(jxi488) + " took " + to_string(elapsed.count()) + "s.\n";
-  // logger->log(message);
+  logger->log(message);
   interval_start = chrono::high_resolution_clock::now();
   invert_matrix(cid->am, ndit, jer, mxndm);
   interval_end = chrono::high_resolution_clock::now();
   elapsed = interval_end - interval_start;
   message = "INFO: matrix inversion for scale " + to_string(jxi488) + " took " + to_string(elapsed.count()) + "s.\n";
-  // logger->log(message);
+  logger->log(message);
   if (jer != 0) {
     return jer;
     // break; // jxi488 loop: goes to memory clean
@@ -477,7 +478,7 @@ int cluster_jxi488_cycle(int jxi488, ScattererConfiguration *sconf, GeometryConf
   interval_end = chrono::high_resolution_clock::now();
   elapsed = interval_end - interval_start;
   message = "INFO: average calculation for scale " + to_string(jxi488) + " took " + to_string(elapsed.count()) + "s.\n";
-  // logger->log(message);
+  logger->log(message);
   interval_start = chrono::high_resolution_clock::now();
   double th = sa->th;
   for (int jth486 = 1; jth486 <= sa->nth; jth486++) { // OpenMP portable?
@@ -966,9 +967,8 @@ int cluster_jxi488_cycle(int jxi488, ScattererConfiguration *sconf, GeometryConf
   interval_end = chrono::high_resolution_clock::now();
   elapsed = interval_end - interval_start;
   message = "INFO: angle loop for scale " + to_string(jxi488) + " took " + to_string(elapsed.count()) + "s.\n";
-  // logger->log(message);
-  
-  // logger->log("INFO: finished scale iteration " + to_string(jxi488) + " of " + to_string(nxi) + ".\n");
-
+  logger->log(message);
+  logger->log("INFO: finished scale iteration " + to_string(jxi488) + " of " + to_string(nxi) + ".\n");
+  delete logger;
   return jer;
 }
-- 
GitLab