Eötvös Quantum Utilities  v4.9.146
Providing the Horsepowers in the Quantum Realm
Parallel.m
Go to the documentation of this file.
1 %% Eotvos Quantum Transport Utilities - Parallel
2 % Copyright (C) 2009-2015 Peter Rakyta, Ph.D.
3 %
4 % This program is free software: you can redistribute it and/or modify
5 % it under the terms of the GNU General Public License as published by
6 % the Free Software Foundation, either version 3 of the License, or
7 % (at your option) any later version.
8 %
9 % This program is distributed in the hope that it will be useful,
10 % but WITHOUT ANY WARRANTY; without even the implied warranty of
11 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 % GNU General Public License for more details.
13 %
14 % You should have received a copy of the GNU General Public License
15 % along with this program. If not, see http://www.gnu.org/licenses/.
16 %
17 %> @addtogroup basic Basic Functionalities
18 %> @{
19 %> @file Parallel.m
20 %> @brief A class for controlling the parallel pool for paralell computations. (compatble only wih MATLAB)
21 %> @}
22 %> @brief A class for controlling the parallel pool for paralell computations. (compatble only wih MATLAB)
23 %> @Available
24 %> EQuUs v4.6 or later
25 %%
27 
28 
29 properties ( Access = protected )
30  %>The parallel.Pool object, see the documentation for more details.
31  poolobj
32  %>Number of attempts to open the parallel pool.
33  try2open
34  %>Maximal number of attemts to open the parallel pool.
35  try2open_max
36  %>A period to wait between the attempts to open the parallel pool.
37  waitsec
38 end
39 
40 properties ( Access = public )
41  %> Number of workers
42  NumWorkers
43 end
44 
45 methods
46 %% Contructor of the class
47 %> @brief Constructor of the class.
48 %> @param Opt An instance of the structure Opt.
49 %> @return An instance of the class
50  function obj = Parallel( Opt )
51  obj = obj@Messages( Opt );
52  obj.poolobj = [];
53  obj.try2open = 0;
54  obj.try2open_max = 20;
55  obj.waitsec = 30;
56  end
57 
58 %% openPool
59 %> @brief Opens a parallel pool for parallel computations.
60  function openPool( obj )
61 
62  if obj.isOctave()
63  % Parallell computation of EQuUs is not yet supported in octave
64  obj.poolobj = [];
65  obj.NumWorkers = 0;
66  end
67 
68  obj.try2open = obj.try2open + 1;
69 
70  %try to open the parallel pool
71  try
72 
73  if exist('parpool' )
74  poolobj = gcp('nocreate');
75  if isempty(poolobj)
76  if ~isempty(obj.Opt.workers) && obj.Opt.workers>0
77  obj.poolobj = parpool(obj.Opt.workers);
78  obj.NumWorkers = obj.poolobj.NumWorkers;
79  elseif (~isempty(obj.Opt.workers) && obj.Opt.workers<=0) || isempty(obj.Opt.workers)
80  obj.poolobj = [];
81  obj.NumWorkers = 0;
82  else
83  obj.poolobj = parpool();
84  obj.NumWorkers = obj.poolobj.NumWorkers;
85  end
86  else
87  % if parallel pool is already opened, nothing is done here
88  obj.poolobj = [];
89  obj.NumWorkers = poolobj.NumWorkers;
90  end
91  else
92  if 0 == matlabpool('size')
93  if ~isempty(obj.Opt.workers) && obj.Opt.workers>0
94  matlabpool(obj.Opt.workers);
95  obj.poolobj = struct('NumWorkers', obj.Opt.workers);
96  elseif (~isempty(obj.Opt.workers) && obj.Opt.workers<=0) || isempty(obj.Opt.workers)
97  obj.poolobj = [];
98  else
99  matlabpool();
100  obj.poolobj = struct('NumWorkers', matlabpool('size'));
101  end
102  else
103  % if parallel pool is already opened, nothing is done here
104  obj.poolobj = [];
105  end
106  obj.NumWorkers = matlabpool('size');
107  end
108 
109  % try to open the parallel pool again
110  catch errCause
111  if obj.try2open >= obj.try2open_max
112  err = MException('Parallel:openPool', 'Error occured when opening the parallel pool.');
113  err = addCause(err, errCause);
114  save('Error_Parallel_openPool.mat');
115  throw( err );
116  else
117  obj.display( ['Failed to open the parallel pool. Try to open again in ', num2str(obj.waitsec), ' seconds.'], 1)
118  pause( obj.waitsec );
119  obj.openPool()
120  end
121 
122 
123  end
124 
125  end
126 
127 %% closePool
128 %> @brief Closes a parallel pool.
129  function closePool( obj )
130  if ~isempty(obj.poolobj)
131  if exist('parpool' )
132  delete(obj.poolobj)
133  else
134  matlabpool('close');
135  end
136  end
137 
138  obj.NumWorkers = [];
139  end
140 
141 
142 
143 
144 
145  end % methods
146 
147 end
A class containing methodes for displaying several standard messages.
Definition: Messages.m:24
A class for controlling the parallel pool for paralell computations.
Definition: Parallel.m:26
Structure Opt contains the basic computational parameters used in EQuUs.
Definition: structures.m:60
function Transport(Energy, B)
Calculates the conductance at a given energy value.
A class containing common basic functionalities.
function()
Structure param contains data structures describing the physical parameters of the scattering center ...
Definition: structures.m:45