From f587e0f1d24c8dc02b6e898fb0fe99ee1b389f5b Mon Sep 17 00:00:00 2001
From: Marco Ratto <marco.ratto@jrc.ec.europa.eu>
Date: Fri, 8 Oct 2010 16:10:59 +0200
Subject: [PATCH] Added new parallel utilities in parallel subfolder; Add
 parallel to dynare path;

---
 matlab/dynare_config.m                        |  1 +
 matlab/parallel/CreateTimeString.m            | 41 +++++++++++++
 .../InitializeComputationalEnviroment.m       | 51 ++++++++++++++++
 matlab/parallel/dynareParallelDelete.m        | 46 ++++++++++++++
 matlab/parallel/dynareParallelDir.m           | 45 ++++++++++++++
 matlab/parallel/dynareParallelGetFiles.m      | 47 +++++++++++++++
 matlab/parallel/dynareParallelMkDir.m         | 49 +++++++++++++++
 matlab/parallel/dynareParallelRmDir.m         | 60 +++++++++++++++++++
 matlab/parallel/dynareParallelSendFiles.m     | 55 +++++++++++++++++
 9 files changed, 395 insertions(+)
 create mode 100644 matlab/parallel/CreateTimeString.m
 create mode 100644 matlab/parallel/InitializeComputationalEnviroment.m
 create mode 100644 matlab/parallel/dynareParallelDelete.m
 create mode 100644 matlab/parallel/dynareParallelDir.m
 create mode 100644 matlab/parallel/dynareParallelGetFiles.m
 create mode 100644 matlab/parallel/dynareParallelMkDir.m
 create mode 100644 matlab/parallel/dynareParallelRmDir.m
 create mode 100644 matlab/parallel/dynareParallelSendFiles.m

diff --git a/matlab/dynare_config.m b/matlab/dynare_config.m
index 91accb65b8..878160ab9e 100644
--- a/matlab/dynare_config.m
+++ b/matlab/dynare_config.m
@@ -44,6 +44,7 @@ addpath([dynareroot '/kalman/smoother'])
 addpath([dynareroot '/AIM/'])
 addpath([dynareroot '/partial_information/'])
 addpath([dynareroot '/swz/'])
+addpath([dynareroot '/parallel/'])
 
 % For functions that exist only under some Octave versions
 % or some MATLAB versions, and for which we provide some replacement functions
diff --git a/matlab/parallel/CreateTimeString.m b/matlab/parallel/CreateTimeString.m
new file mode 100644
index 0000000000..e71db69008
--- /dev/null
+++ b/matlab/parallel/CreateTimeString.m
@@ -0,0 +1,41 @@
+function [TiSt] = CreateTimeString()
+% PARALLEL CONTEXT
+% In a parallel context, this is ...
+%
+%
+% INPUT/OUTPUT description:
+%
+%
+%
+% Copyright (C) 2009-2010 Dynare Team
+%
+% This file is part of Dynare.
+%
+% Dynare is free software: you can redistribute it and/or modify
+% it under the terms of the GNU General Public License as published by
+% the Free Software Foundation, either version 3 of the License, or
+% (at your option) any later version.
+%
+% Dynare is distributed in the hope that it will be useful,
+% but WITHOUT ANY WARRANTY; without even the implied warranty of
+% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+% GNU General Public License for more details.
+%
+% You should have received a copy of the GNU General Public License
+% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
+
+
+
+TiSt=[];
+T=fix(clock);
+
+
+S1=num2str(T(1));
+S2=num2str(T(2));
+S3=num2str(T(3));
+S4=num2str(T(4));
+S5=num2str(T(5));
+S6=num2str(T(6));
+
+
+TiSt=[S1 '-' S2 '-' S3 '-' S4 'h' S5 'm' S6 's'];
diff --git a/matlab/parallel/InitializeComputationalEnviroment.m b/matlab/parallel/InitializeComputationalEnviroment.m
new file mode 100644
index 0000000000..7ddb22de43
--- /dev/null
+++ b/matlab/parallel/InitializeComputationalEnviroment.m
@@ -0,0 +1,51 @@
+function [ErrorCode] = InitializeComputationalEnviroment(DataInput)
+
+% PARALLEL CONTEXT
+% In a parallel context, this function is used to Initialize the computational enviroment according with
+% the user request.
+% If no error happen the function return 0.
+%
+% INPUT/OUTPUT description:
+%
+%
+%
+%
+% The variable ErrorCode is initialized at 0. If there are non problems 
+% the ErrorCode is unchanged, in the others cases is set equak to 1, 2 , ... The values
+% table is below.
+%
+%
+%   Table for ErrorCode Values.
+%
+%
+% Then at the point call of this function it is possible react in a best way, in accord
+% with the ErrorCode.
+
+% Copyright (C) 2009-2010 Dynare Team
+%
+% This file is part of Dynare.
+%
+% Dynare is free software: you can redistribute it and/or modify
+% it under the terms of the GNU General Public License as published by
+% the Free Software Foundation, either version 3 of the License, or
+% (at your option) any later version.
+%
+% Dynare is distributed in the hope that it will be useful,
+% but WITHOUT ANY WARRANTY; without even the implied warranty of
+% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+% GNU General Public License for more details.
+%
+% You should have received a copy of the GNU General Public License
+% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
+
+ErrorCode=0;
+
+% Invoke masterParallel with 8 arguments and the last equal to 1. With this shape
+% for input data, masterParallel only create a new directory for remote
+% computation. The name of this directory is time depending. For local
+% parallel computations with Strategy == 1 delete the traces (if exists) of
+% previous computations. 
+delete(['P_slave_*End.txt'])
+masterParallel(DataInput.parallel,[],[],[],[],[],[],DataInput.parallel_info,1);
+
+return
\ No newline at end of file
diff --git a/matlab/parallel/dynareParallelDelete.m b/matlab/parallel/dynareParallelDelete.m
new file mode 100644
index 0000000000..7f11d8f300
--- /dev/null
+++ b/matlab/parallel/dynareParallelDelete.m
@@ -0,0 +1,46 @@
+function dynareParallelDelete(fname,pname,Parallel)
+% PARALLEL CONTEXT
+% In a parallel context, this is a specialized version of delete() function.
+%
+%
+% INPUT/OUTPUT description:
+%
+%
+%
+%
+% Copyright (C) 2009-2010 Dynare Team
+%
+% This file is part of Dynare.
+%
+% Dynare is free software: you can redistribute it and/or modify
+% it under the terms of the GNU General Public License as published by
+% the Free Software Foundation, either version 3 of the License, or
+% (at your option) any later version.
+%
+% Dynare is distributed in the hope that it will be useful,
+% but WITHOUT ANY WARRANTY; without even the implied warranty of
+% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+% GNU General Public License for more details.
+%
+% You should have received a copy of the GNU General Public License
+% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
+
+if nargin ==0,
+    disp('dynareParallelDelete(fname)')
+    return
+end
+
+if nargin ==1,
+    pname='';
+else
+    pname=[pname,filesep];
+end
+
+for indPC=1:length(Parallel),
+    if isunix
+        system(['ssh ',Parallel(indPC).user,'@',Parallel(indPC).PcName,' rm -f ',Parallel(indPC).RemoteFolder,'/',pname,fname]);
+    else
+        delete(['\\',Parallel(indPC).PcName,'\',Parallel(indPC).RemoteDrive,'$\',Parallel(indPC).RemoteFolder,'\',pname,fname]);
+    end
+    
+end
diff --git a/matlab/parallel/dynareParallelDir.m b/matlab/parallel/dynareParallelDir.m
new file mode 100644
index 0000000000..f63331cb60
--- /dev/null
+++ b/matlab/parallel/dynareParallelDir.m
@@ -0,0 +1,45 @@
+function dirlist = dynareParallelDir(filename,PRCDir,Parallel)
+% PARALLEL CONTEXT
+% In a parallel context, this is ...
+%
+%
+% INPUT/OUTPUT description:
+%
+%
+%
+% Copyright (C) 2009-2010 Dynare Team
+%
+% This file is part of Dynare.
+%
+% Dynare is free software: you can redistribute it and/or modify
+% it under the terms of the GNU General Public License as published by
+% the Free Software Foundation, either version 3 of the License, or
+% (at your option) any later version.
+%
+% Dynare is distributed in the hope that it will be useful,
+% but WITHOUT ANY WARRANTY; without even the implied warranty of
+% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+% GNU General Public License for more details.
+%
+% You should have received a copy of the GNU General Public License
+% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
+
+dirlist=[];
+for indPC=1:length(Parallel),
+    if isunix || (~matlab_ver_less_than('7.4') && ismac),
+        if Parallel(indPC).Local==0,
+            [check, ax]=system(['ssh ',Parallel(indPC).user,'@',Parallel(indPC).PcName,' ls ',Parallel(indPC).RemoteFolder,'/',PRCDir,'/',filename]);
+        else
+            ax=ls(filename);
+
+        end
+        dirlist = [dirlist, ax];
+    else
+        if Parallel(indPC).Local==0,
+            ax=ls(['\\',Parallel(indPC).PcName,'\',Parallel(indPC).RemoteDrive,'$\',Parallel(indPC).RemoteFolder,'\',PRCDir,'\',filename]);
+        else
+            ax=ls(filename);
+        end
+    dirlist = [dirlist; ax];
+    end
+end
diff --git a/matlab/parallel/dynareParallelGetFiles.m b/matlab/parallel/dynareParallelGetFiles.m
new file mode 100644
index 0000000000..ed7c77f1ce
--- /dev/null
+++ b/matlab/parallel/dynareParallelGetFiles.m
@@ -0,0 +1,47 @@
+function dynareParallelGetFiles(NamFileInput,PRCDir,Parallel)
+% PARALLEL CONTEXT
+% In a parallel context, this is ...
+%
+%
+% INPUT/OUTPUT description:
+%
+%
+%
+% Copyright (C) 2009-2010 Dynare Team
+%
+% This file is part of Dynare.
+%
+% Dynare is free software: you can redistribute it and/or modify
+% it under the terms of the GNU General Public License as published by
+% the Free Software Foundation, either version 3 of the License, or
+% (at your option) any later version.
+%
+% Dynare is distributed in the hope that it will be useful,
+% but WITHOUT ANY WARRANTY; without even the implied warranty of
+% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+% GNU General Public License for more details.
+%
+% You should have received a copy of the GNU General Public License
+% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
+
+
+if ischar(NamFileInput),
+    for j=1:size(NamFileInput,1),
+        NamFile(j,:)={['.',filesep],deblank(NamFileInput(j,:))};
+    end
+    NamFileInput = NamFile;
+end
+
+for indPC=1:length(Parallel),
+    if Parallel(indPC).Local==0,
+        if isunix || (~matlab_ver_less_than('7.4') && ismac),
+            for jfil=1:size(NamFileInput,1),
+                system(['scp ',Parallel(indPC).user,'@',Parallel(indPC).PcName,':',Parallel(indPC).RemoteFolder,'/',PRCDir,'/',NamFileInput{jfil,1},NamFileInput{jfil,2},' ',NamFileInput{jfil,1}]);
+            end
+        else
+            for jfil=1:size(NamFileInput,1)
+                copyfile(['\\',Parallel(indPC).PcName,'\',Parallel(indPC).RemoteDrive,'$\',Parallel(indPC).RemoteFolder,'\',PRCDir,'\',NamFileInput{jfil,1},NamFileInput{jfil,2}],NamFileInput{jfil,1})
+            end
+        end
+    end
+end
diff --git a/matlab/parallel/dynareParallelMkDir.m b/matlab/parallel/dynareParallelMkDir.m
new file mode 100644
index 0000000000..70065fe770
--- /dev/null
+++ b/matlab/parallel/dynareParallelMkDir.m
@@ -0,0 +1,49 @@
+function dynareParallelMkDir(PRCDir,Parallel)
+% PARALLEL CONTEXT
+% In a parallel context, this is a specialized version of rmdir() function.
+%
+%
+% INPUT/OUTPUT description:
+%
+%
+%
+%
+%
+% Then at the point call of this function it is possible react in a best way, in accord
+% with the ErrorCode.
+
+% Copyright (C) 2009-2010 Dynare Team
+%
+% This file is part of Dynare.
+%
+% Dynare is free software: you can redistribute it and/or modify
+% it under the terms of the GNU General Public License as published by
+% the Free Software Foundation, either version 3 of the License, or
+% (at your option) any later version.
+%
+% Dynare is distributed in the hope that it will be useful,
+% but WITHOUT ANY WARRANTY; without even the implied warranty of
+% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+% GNU General Public License for more details.
+%
+% You should have received a copy of the GNU General Public License
+% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
+
+
+
+if nargin ==0,
+    disp('dynareParallelMkDir(dirname,Parallel)')
+    return
+end
+
+for indPC=1:length(Parallel)
+    if Parallel(indPC).Local==0,
+        if isunix || (~matlab_ver_less_than('7.4') && ismac),
+            system(['ssh ',Parallel(indPC).user,'@',Parallel(indPC).PcName,' mkdir -p ',Parallel(indPC).RemoteFolder,'/',PRCDir])
+        else
+            [NonServeS NonServeD]=mkdir(['\\',Parallel(indPC).PcName,'\',Parallel(indPC).RemoteDrive,'$\',Parallel(indPC).RemoteFolder,'\',PRCDir]);
+        end
+    end
+end
+
+return
\ No newline at end of file
diff --git a/matlab/parallel/dynareParallelRmDir.m b/matlab/parallel/dynareParallelRmDir.m
new file mode 100644
index 0000000000..e1c1703d0f
--- /dev/null
+++ b/matlab/parallel/dynareParallelRmDir.m
@@ -0,0 +1,60 @@
+function dynareParallelRmDir(PRCDir,Parallel)
+% PARALLEL CONTEXT
+% In a parallel context, this is a specialized version of rmdir() function.
+%
+%
+% INPUT/OUTPUT description:
+%
+%
+%
+%
+%
+% Then at the point call of this function it is possible react in a best way, in accord
+% with the ErrorCode.
+
+% Copyright (C) 2009-2010 Dynare Team
+%
+% This file is part of Dynare.
+%
+% Dynare is free software: you can redistribute it and/or modify
+% it under the terms of the GNU General Public License as published by
+% the Free Software Foundation, either version 3 of the License, or
+% (at your option) any later version.
+%
+% Dynare is distributed in the hope that it will be useful,
+% but WITHOUT ANY WARRANTY; without even the implied warranty of
+% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+% GNU General Public License for more details.
+%
+% You should have received a copy of the GNU General Public License
+% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
+
+
+
+if nargin ==0,
+    disp('dynareParallelRmDir(fname)')
+    return
+end
+
+for indPC=1:length(Parallel),
+    while (1)
+        %         keyboard;
+        if isunix
+            stat = system(['ssh ',Parallel(indPC).user,'@',Parallel(indPC).PcName,' rm -fr ',Parallel(indPC).RemoteFolder,'/',PRCDir]);
+            break;
+        else
+            [stat, mess, id] = rmdir(['\\',Parallel(indPC).PcName,'\',Parallel(indPC).RemoteDrive,'$\',Parallel(indPC).RemoteFolder,'\',PRCDir],'s');
+            if stat==1,
+                break,
+            else
+                if isempty(dynareParallelDir(PRCDir,'',Parallel));
+                    break,
+                else
+                    pause(1);
+                end
+            end
+        end
+    end
+end
+
+return
\ No newline at end of file
diff --git a/matlab/parallel/dynareParallelSendFiles.m b/matlab/parallel/dynareParallelSendFiles.m
new file mode 100644
index 0000000000..fbbabafe8f
--- /dev/null
+++ b/matlab/parallel/dynareParallelSendFiles.m
@@ -0,0 +1,55 @@
+function dynareParallelSendFiles(NamFileInput,PRCDir,Parallel)
+% PARALLEL CONTEXT
+% In a parallel context, this is ...
+%
+%
+% INPUT/OUTPUT description:
+%
+%
+%
+% Copyright (C) 2009-2010 Dynare Team
+%
+% This file is part of Dynare.
+%
+% Dynare is free software: you can redistribute it and/or modify
+% it under the terms of the GNU General Public License as published by
+% the Free Software Foundation, either version 3 of the License, or
+% (at your option) any later version.
+%
+% Dynare is distributed in the hope that it will be useful,
+% but WITHOUT ANY WARRANTY; without even the implied warranty of
+% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+% GNU General Public License for more details.
+%
+% You should have received a copy of the GNU General Public License
+% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
+
+
+if ischar(NamFileInput),
+    for j=1:size(NamFileInput,1),
+        NamFile(j,:)={'',deblank(NamFileInput(j,:))};
+    end
+    NamFileInput = NamFile;
+end
+
+for indPC=1:length(Parallel),
+    if Parallel(indPC).Local==0,
+        if isunix || (~matlab_ver_less_than('7.4') && ismac),
+            for jfil=1:size(NamFileInput,1),
+                if ~isempty(NamFileInput{jfil,1})
+                    system(['ssh ',Parallel(indPC).user,'@',Parallel(indPC).PcName,' mkdir -p ',Parallel(indPC).RemoteFolder,'/',PRCDir,'/',NamFileInput{jfil,1}])
+                end
+                system(['scp ',NamFileInput{jfil,1},NamFileInput{jfil,2},' ',Parallel(indPC).user,'@',Parallel(indPC).PcName,':',Parallel(indPC).RemoteFolder,'/',PRCDir,'/',NamFileInput{jfil,1}]);
+            end
+        else
+            for jfil=1:size(NamFileInput,1)
+                if ~isempty(NamFileInput{jfil,1})
+                    if isempty(dynareParallelDir(NamFileInput{jfil,1},PRCDir,Parallel(indPC)))
+                        mkdir(['\\',Parallel(indPC).PcName,'\',Parallel(indPC).RemoteDrive,'$\',Parallel(indPC).RemoteFolder,'\',PRCDir,'\',NamFileInput{jfil,1}]);
+                    end
+                end
+                copyfile([NamFileInput{jfil,1},NamFileInput{jfil,2}],['\\',Parallel(indPC).PcName,'\',Parallel(indPC).RemoteDrive,'$\',Parallel(indPC).RemoteFolder,'\',PRCDir,'\',NamFileInput{jfil,1}])
+            end
+        end
+    end
+end
-- 
GitLab