From 6d3532348d5098d83c7e56e9f75b46ccccdf3162 Mon Sep 17 00:00:00 2001
From: Marco Ratto <marco.ratto@ec.europa.eu>
Date: Wed, 26 Jan 2022 18:27:06 +0100
Subject: [PATCH] use Save_files to save different names under parallel
 execution, to avoid crashes from different threads attempting to write on the
 same file

---
 matlab/optimization/mr_gstep.m   |  4 ++--
 matlab/optimization/mr_hessian.m |  2 +-
 matlab/optimization/newrat.m     | 10 +++++-----
 3 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/matlab/optimization/mr_gstep.m b/matlab/optimization/mr_gstep.m
index 6a403ea7ad..6f3d6dc984 100644
--- a/matlab/optimization/mr_gstep.m
+++ b/matlab/optimization/mr_gstep.m
@@ -83,11 +83,11 @@ while i<n
     end
     x = check_bounds(x,bounds);
     if Save_files
-        save('gstep.mat','x','h1','f0')
+        save(['gstep' int2str(Save_files) '.mat'],'x','h1','f0')
     end
 end
 if Save_files
-    save('gstep.mat','x','h1','f0')
+    save(['gstep' int2str(Save_files) '.mat'],'x','h1','f0')
 end
 
 return
diff --git a/matlab/optimization/mr_hessian.m b/matlab/optimization/mr_hessian.m
index f13bdd63f1..616d7501d2 100644
--- a/matlab/optimization/mr_hessian.m
+++ b/matlab/optimization/mr_hessian.m
@@ -256,7 +256,7 @@ if outer_product_gradient
     end
     hh1=hess_info.h1;
     if Save_files
-        save('hess.mat','hessian_mat')
+        save(['hess' int2str(Save_files) '.mat'],'hessian_mat')
     end
 else
     hessian_mat=[];
diff --git a/matlab/optimization/newrat.m b/matlab/optimization/newrat.m
index d1d75f3b37..6125491a02 100644
--- a/matlab/optimization/newrat.m
+++ b/matlab/optimization/newrat.m
@@ -132,7 +132,7 @@ if Verbose
     end
 end
 if Save_files
-    save('m1.mat','x','hh','g','hhg','igg','fval0')
+    save(['m' int2str(Save_files) '.mat'],'x','hh','g','hhg','igg','fval0')
 end
 
 igrad=1;
@@ -248,11 +248,11 @@ while norm(gg)>gtol && check==0 && jit<nit
         if norm(x(:,icount)-xparam1)>1.e-12 && analytic_derivation==0
             try
                 if Save_files
-                    save('m1.mat','x','fval0','nig','-append')
+                    save(['m' int2str(Save_files) '.mat'],'x','fval0','nig','-append')
                 end
             catch
                 if Save_files
-                    save('m1.mat','x','fval0','nig')
+                    save(['m' int2str(Save_files) '.mat'],'x','fval0','nig')
                 end
             end
             [dum, gg, htol0, igg, hhg, h1, hess_info]=mr_hessian(xparam1,func0,penalty,flagit,htol,hess_info,bounds,prior_std,Save_files,varargin{:});
@@ -297,12 +297,12 @@ while norm(gg)>gtol && check==0 && jit<nit
         disp_verbose(['Elapsed time for iteration ',num2str(t),' s.'],Verbose)
         g(:,icount+1)=gg;
         if Save_files
-            save('m1.mat','x','hh','g','hhg','igg','fval0','nig','H')
+            save(['m' int2str(Save_files) '.mat'],'x','hh','g','hhg','igg','fval0','nig','H')
         end
     end
 end
 if Save_files
-    save('m1.mat','x','hh','g','hhg','igg','fval0','nig')
+    save(['m' int2str(Save_files) '.mat'],'x','hh','g','hhg','igg','fval0','nig')
 end
 if ftol>ftol0
     skipline()
-- 
GitLab