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