ParaMonte MATLAB 3.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation.
Paradram.m
Go to the documentation of this file.
1%> \brief
2%> This is the ParaDRAM class for generating instances of serial and parallel
3%> Delayed-Rejection Adaptive Metropolis-Hastings Markov Chain Monte Carlo
4%> sampler of the ParaMonte MATLAB library.<br>
5%>
6%> \details
7%> For more information, see the documentation of the class
8%> constructor [pm.sampling.Paradram::Paradram](@ref Paradram::Paradram).<br>
9%>
10%> \note
11%> See the documentation of the class constructor for usage interface and examples.<br>
12%>
13%> \final
14%>
15%> \author
16%> \AmirShahmoradi, May 16 2016, 9:03 AM, Oden Institute for Computational Engineering and Sciences (ICES), UT Austin<br>
17classdef Paradram < pm.sampling.Sampler
18
19 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
20
21 methods(Access = public)
22
23 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
24
25 %> \brief
26 %> Generate and return an instance of the serial and parallel
27 %> Delayed-Rejection Adaptive Metropolis-Hastings Markov Chain Monte Carlo
28 %> sampler of the ParaMonte MATLAB library.<br>
29 %>
30 %> \details
31 %> This function is the constructor of the [pm.sampling.Paradram](@ref Paradram) sampler class.<br>
32 %> Once you assign the desired simulation specifications to the corresponding attributes within the
33 %> component `spec` of an object of class [pm.sampling.Paradram](@ref Paradram), call the ParaDRAM
34 %> sampler via the object method [pm.sampling.Paradram.run()](@ref Paradram::run).<br>
35 %>
36 %> While the constructor of this class does not take any input arguments,
37 %> all ParaDRAM simulation specifications can be set after creating the object.<br>
38 %>
39 %> \return
40 %> ``sampler`` : The output scalar object of class [pm.sampling.Paradram](@ref Paradram).<br>
41 %>
42 %> \interface{Paradram}
43 %> \code{.m}
44 %>
45 %> sampler = pm.sampling.Paradram();
46 %>
47 %> \endcode
48 %>
49 %> \warning
50 %> When using the ParaMonte MATLAB library functionalities, particularly ParaMonte samplers in parallel,
51 %> it would be best to close any such aggressive software/applications as **Dropbox**, **ZoneAlarm**, ...
52 %> that interfere with your ParaMonte MATLAB library output files, potentially causing the tasks
53 %> to fail and crash before successful completion.<br>
54 %> These situations scarcely happen.<br>
55 %>
56 %> \note
57 %> On Windows systems, when restarting an old interrupted ParaDRAM simulation,
58 %> ensure your MATLAB session is also restarted before the simulation restart.<br>
59 %> This may be needed as **Windows frequently locks access to some or all simulation output files**.<br>
60 %>
61 %> \note
62 %> To unset an already-set input simulation specification, simply set the
63 %> simulation attribute to empty double `[]` or re-instantiate an object
64 %> of class [pm.sampling.Paradram.run()](@ref Paradram).<br>
65 %>
66 %> \see
67 %> [ParaDRAM simulation specifications listing](\pmdoc_usage_sampling/paradram/specifications/)<br>
68 %> [ParaDRAM simulation restart functionality](\pmdoc_usage_sampling/paradram/restart/)<br>
69 %> [ParaDRAM simulation output files](\pmdoc_usage_sampling/paradram/output/)<br>
70 %>
71 %> ParaDRAM Simulation Specifications
72 %> ----------------------------------
73 %>
74 %> The ParaDRAM simulation specifications have lengthy comprehensive descriptions
75 %> that appear in full in the output report files of every ParaDRAM simulation.<br>
76 %>
77 %> The best way to learn about individual ParaDRAM simulation attributes
78 %> is to a run a minimal serial simulation as given in the above.<br>
79 %> You can also use the ``sampler.spec.doc()`` method:
80 %>
81 %> \code{.m}
82 %> sampler = pm.sampling.Paradram();
83 %> sampler.spec.doc()
84 %> \endcode
85 %>
86 %> Example Usage: Serial
87 %> ---------------------
88 %>
89 %> First, ensure the ParaMonte ``+pm`` package (i.e., folder) is available in your MATLAB paths.
90 %>
91 %> Here is a MATLAB script ``main.m`` for a serial ParaDRAM simulation.<br>
92 %> Copy and paste the following code into your MATLAB session:<br>
93 %>
94 %> \code{.m}
95 %>
96 %> ndim = 4;
97 %> sampler = pm.sampling.Paradram();
98 %> sampler.run ( @(x) - sum(x.^2) ... getLogFunc: the natural log of the objective function.
99 %> , ndim ... ndim: the number of dimensions of the objective function.
100 %> );
101 %> samples = sampler.readSample();
102 %> sample = samples{1};
103 %> tile = pm.vis.TileLine(sample.df);
104 %> tile.make("coly", sample.slfc + 1 : sample.slfc + ndim, "colc", "sampleLogFunc");
105 %>
106 %> \endcode
107 %>
108 %> The mathematical objective function in the above example is a
109 %> is a multivariate Normal distribution centered at the origin,
110 %> whose natural logarithm is returned by the lambda (``Anonymous``)
111 %> function defined as a function handle input to the ParaDRAM sampler.<br>
112 %>
113 %> Running this code will generate a set of simulation output files (in the current working directory of MATLAB).<br>
114 %> Among these, the file suffixed with "_report.txt" contains the full description of all input specifications
115 %> of the ParaDRAM simulation as well as other information about the simulation results.<br>
116 %>
117 %> Example Usage: Thread-Parallel
118 %> ------------------------------
119 %>
120 %> First, ensure the ParaMonte ``+pm`` package (i.e., folder) is available in your MATLAB paths.<br>
121 %>
122 %> Threading parallelism is possible as of ParaMonte MATLAB version ``3.0.0``.<br>
123 %> However, only ``singleChain`` ParaDRAM simulations are supported.<br>
124 %>
125 %> Here is a MATLAB script ``main.m`` for a thread-parallel ParaDRAM simulation.<br>
126 %> Copy and paste the following code and paste into your MATLAB session:<br>
127 %>
128 %> \code{.m}
129 %>
130 %> sampler = pm.sampling.Paradram();
131 %> sampler.spec.parallelismNumThread = 0; % use all available threads.
132 %> sampler.run ( @(x) - sum(x.^2) ... getLogFunc: the natural log of the objective function.
133 %> , 4 ... ndim: the number of dimensions of the objective function.
134 %> );
135 %> samples = sampler.readSample();
136 %> sample = samples{1};
137 %> pm.vis.tile(sample.contents)
138 %>
139 %> \endcode
140 %>
141 %> The mathematical objective function in the above example is a
142 %> is a multivariate Normal distribution centered at the origin,
143 %> whose natural logarithm is returned by the lambda (``Anonymous``)
144 %> function defined as a function handle input to the ParaDRAM sampler.<br>
145 %>
146 %> Running this code will generate a set of simulation output files (in the current working directory of MATLAB).<br>
147 %> Among these, the file suffixed with ``"_report.txt"`` contains the full description of all input specifications
148 %> of the ParaDRAM simulation as well as other information about the simulation results.<br>
149 %>
150 %> Specifying ``0`` as the number of threads will lead to using
151 %> all available CPU threads for thread-parallel ParaDRAM simulation.<br>
152 %>
153 %> \note
154 %> **Benefits of thread-parallelism**<br>
155 %> Thread-parallel simulations offer a much more flexible
156 %> and easier approach to benefiting from parallelism without
157 %> going through the hassle of MPI-parallel simulations.<br>
158 %> But they can still potentially offer much faster speed than serial simulations.<br>
159 %> The actual speedup depends on a lot of factors.<br>
160 %> Moreover, the number of threads is limited to maximum
161 %> number of physical cores available on your system.<br>
162 %> As such, thread-parallel simulations are not scalable.<br>
163 %> If you need scalability, checkout MPI-parallelism below.<br>
164 %>
165 %> Example Usage: MPI-Parallel
166 %> ---------------------------
167 %>
168 %> First, ensure the ParaMonte ``+pm`` package (i.e., folder) is available in your MATLAB paths.<br>
169 %>
170 %> MPI-parallel simulations can be slightly more cumbersome than thread-parallel simulations
171 %> described above because MPI-parallel simulations cannot be performed from within a MATLAB GUI
172 %> session and require launching MATLAB via a compatible ``mpiexec`` launcher.<br>
173 %>
174 %> <ol>
175 %> <li> Ensure you need and will get a speedup by running the an MPI-parallel simulation.<br>
176 %> Typically, your simulation may then benefit from parallelism only if a single
177 %> evaluation of the objective function takes longer than a few milliseconds.<br>
178 %>
179 %> <li> Ensure the required MPI libraries are installed on your system
180 %> (You can skip this step if you know that you already have
181 %> a compatible MPI library installed on your system).<br>
182 %> On the MATLAB command line type the following,<br>
183 %> \code{.m}
184 %> pm.lib.verify();
185 %> \endcode
186 %> This will verify the existence of a valid MPI library on your system and,
187 %> if missing, will guide you to install the MPI library on your system.<br>
188 %>
189 %> <li> Once the MPI installation is verified, copy and paste the following
190 %> code into your MATLAB session:
191 %> \code{.m}
192 %>
193 %> fid = fopen("main_mpi.m", "w");
194 %> sourceCode = ...
195 %> "sampler = pm.sampling.Paradram();" + newline + ...
196 %> "%sampler.mpiname = ''; % set this to an explicit MPI library name if needed." + newline + ...
197 %> "sampler.run( @(x) - sum(x.^2) ... getLogFunc: the natural log of the objective function." + newline + ...
198 %> " , 4 ... ndim: the number of dimensions of the objective function" + newline + ...
199 %> " );";
200 %> fprintf(fid, "%s\n", sourceCode);
201 %> fclose(fid);
202 %>
203 %> \endcode
204 %>
205 %> <li> This will generate a ``main_mpi.m`` MATLAB script file in the current working directory of your MATLAB session.<br>
206 %> Now, you can execute this MATLAB script file (``main_mpi.m``) in parallel.<br>
207 %> To do so, you need to call MATLAB on a command-line, **out of MATLAB GUI**.<br>
208 %> <ol>
209 %> <li> **On Windows**:<br>
210 %> From within command prompt that recognizes both MATLAB and ``mpiexec``,
211 %> ideally, the Intel dedicated command-prompt that is shipped with Intel MPI library,
212 %> type the following,
213 %> \code{.m}
214 %>
215 %> mpiexec -localonly -n 3 matlab -batch "main_mpi"
216 %>
217 %> \endcode
218 %>
219 %> \note
220 %> In the above MPI launcher command for Windows OS,
221 %> we assumed that you would be using the Intel MPI library, hence,
222 %> the reason for the extra flag ``-localonly``.<br>
223 %> This flag runs the parallel code only on one node, but in doing so,
224 %> it avoids the use of Hydra service and its registration.<br>
225 %> If you are not on a Windows cluster, (e.g., you are using your personal device),
226 %> then we recommend specifying this flag.<br>
227 %>
228 %> <li> **On macOS/Linux**:<br>
229 %> From within a Bash terminal that recognizes both MATLAB and ``mpiexec``,
230 %> type the following,
231 %> \code{.m}
232 %>
233 %> mpiexec -n 3 matlab -batch "main_mpi"
234 %>
235 %> \endcode
236 %>
237 %> \note
238 %> In both cases in the above, the script ``main_mpi.m`` will run on 3 processors.<br>
239 %> Feel free to change the number of processors to any number desired.<br>
240 %> But do not request more than the available number of physical cores on your system.<br>
241 %> </ol>
242 %> </ol>
243 %>
244 %> \warning
245 %> Do not add postprocessing codes (such as reading and plotting the output samples) in your MPI-parallel code.<br>
246 %> There is no point in doing so, since MATLAB will run in ``-batch`` mode for parallel simulations, disabling all plotting capabilities.<br>
247 %> Moreover, if you read and postprocess the output files in parallel mode, the task will be done
248 %> by all parallel processes, potentially overwriting external IO activities of each other.<br>
249 %> Only perform the sampling as described above in MPI-parallel mode.<br>
250 %>
251 %> \example{himmelblau}
252 %> \include{lineno} example/sampling/Paradram/himmelblau/main.m
253 %> \output{himmelblau}
254 %> \include{lineno} example/sampling/Paradram/himmelblau/main.out.m
255 %> \vis{himmelblau}
256 %> \image html example/sampling/Paradram/himmelblau/Paradram.himmelblau.domain.2d.png width=700
257 %> <br><br>
258 %> \image html example/sampling/Paradram/himmelblau/Paradram.himmelblau.domain.3d.png width=700
259 %> <br><br>
260 %> \image html example/sampling/Paradram/himmelblau/Paradram.himmelblau.triplex.lshc2.png width=700
261 %> <br><br>
262 %> \image html example/sampling/Paradram/himmelblau/Paradram.himmelblau.triplex.lshc3.png width=700
263 %> <br><br>
264 %> \image html example/sampling/Paradram/himmelblau/Paradram.himmelblau.triplex.lshcf.png width=700
265 %> <br><br>
266 %> \image html example/sampling/Paradram/himmelblau/Paradram.himmelblau.traceplot.png width=700
267 %> <br><br>
268 %> \image html example/sampling/Paradram/himmelblau/Paradram.himmelblau.proposalCov.png width=700
269 %> <br><br>
270 %> \image html example/sampling/Paradram/himmelblau/Paradram.himmelblau.proposalAdaptation.png width=700
271 %> <br><br>
272 %> \image html example/sampling/Paradram/himmelblau/Paradram.himmelblau.proposalAdaptation.line.png width=700
273 %> <br><br>
274 %> \image html example/sampling/Paradram/himmelblau/Paradram.himmelblau.proposalAdaptation.scatter.png width=700
275 %> <br><br>
276 %> \image html example/sampling/Paradram/himmelblau/Paradram.himmelblau.parallelism.speedup.scaling.strong.sameeff.png width=700
277 %> <br><br>
278 %> \image html example/sampling/Paradram/himmelblau/Paradram.himmelblau.parallelism.speedup.scaling.strong.zeroeff.png width=700
279 %>
280 %> \final{Paradram}
281 %>
282 %> \author
283 %> \AmirShahmoradi, May 16 2016, 9:03 AM, Oden Institute for Computational Engineering and Sciences (ICES), UT Austin<br>
284 function self = Paradram()
285 self = self@pm.sampling.Sampler("ParaDRAM")
286 end
288 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
289
290 run(self, getLogFunc, ndim)
291
292 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
293
294 chainMarkovList = readChainMarkov(self, pattern, sep)
295
296 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
297
298 end
299
300 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
301
302 methods(Hidden)
303
304 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
305
306 ppm = getppm(self)
307
308 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
309
310 end
311
312 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
313
314end
function verify(in varval, in vartype, in varsize, in varname)
Verify the type and number of elements of the input varval match the specified input vartype and vars...
function version(in silent)
Return a scalar MATLAB string containing the latest available ParaMonte MATLAB version newer than the...
This is the ParaDRAM class for generating instances of serial and parallel Delayed-Rejection Adaptive...
Definition: Paradram.m:18
function getppm(in self)
Generate and return the relevant Post-Processing Message (ppm) for the current ParaMonte sampler to b...
function run(in self, in getLogFunc, in ndim)
Run the ParaDRAM sampler and return nothing.
function Paradram()
Generate and return an instance of the serial and parallel Delayed-Rejection Adaptive Metropolis-Hast...
function readChainMarkov(in self, in pattern, in sep)
Return a list of objects of class pm.sampling.FileContentsChainDRAM containing the content(s) of the ...
This is the base class for the ParaMonte sampler routines.
Definition: Sampler.m:21
function copy(in from, in to, in field, in exclude)
Copy the contents of the struct/object from to the struct/object to recursively and without destroyin...
function getLogFunc(in point)
function install(in vendor, in isyes)
Install or provide instructions to install the MPI library from the requested input MPI vendor.
function parallel()
Return a scalar MATLAB logical that is true if and only if the current installation of MATLAB contain...