-
Notifications
You must be signed in to change notification settings - Fork 0
/
MultiThreadingInMantid.html
246 lines (198 loc) · 16.1 KB
/
MultiThreadingInMantid.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<!DOCTYPE html>
<html lang="en" data-content_root="./">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Multithreading in Mantid</title>
<link rel="stylesheet" type="text/css" href="_static/pygments.css?v=fa44fd50" />
<link rel="stylesheet" type="text/css" href="_static/bootstrap-sphinx.css?v=fadd4351" />
<link rel="stylesheet" type="text/css" href="_static/custom.css?v=77160d70" />
<script src="_static/documentation_options.js?v=a8da1a53"></script>
<script src="_static/doctools.js?v=9bcbadda"></script>
<script src="_static/sphinx_highlight.js?v=dc90522c"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="Python vs C++ Algorithms" href="PythonVSCppAlgorithms.html" />
<link rel="prev" title="Matplotlib in C++" href="MatplotlibInCpp.html" />
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-59110517-1', 'auto');
ga('send', 'pageview');
</script>
</head><body>
<div id="navbar" class="navbar navbar-default ">
<div class="container">
<div class="navbar-header">
<!-- .btn-navbar is used as the toggle for collapsed navbar content -->
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="http://www.mantidproject.org">
</a>
<span class="navbar-text navbar-version pull-left"><b>main</b></span>
</div>
<div class="collapse navbar-collapse nav-collapse">
<ul class="nav navbar-nav">
<li class="divider-vertical"></li>
<li><a href="index.html">Home</a></li>
<li><a href="https://download.mantidproject.org">Download</a></li>
<li><a href="https://docs.mantidproject.org">User Documentation</a></li>
<li><a href="http://www.mantidproject.org/contact">Contact Us</a></li>
</ul>
<form class="navbar-form navbar-right" action="search.html" method="get">
<div class="form-group">
<input type="text" name="q" class="form-control" placeholder="Search" />
</div>
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<p>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="nav-item nav-item-0"><a href="index.html">Documentation</a> »</li>
<li class="nav-item nav-item-this"><a href="">Multithreading in Mantid</a></li>
</ul>
</div> </p>
</div>
<div class="container">
<div class="row">
<div class="body col-md-12 content" role="main">
<section id="multithreading-in-mantid">
<h1>Multithreading in Mantid<a class="headerlink" href="#multithreading-in-mantid" title="Link to this heading">¶</a></h1>
<section id="c">
<h2>C++<a class="headerlink" href="#c" title="Link to this heading">¶</a></h2>
<p>Mantid uses <a class="reference external" href="http://openmp.org/wp/about-openmp/">OpenMP</a> in C++ to improve
performance by parallelizing <code class="docutils literal notranslate"><span class="pre">for</span></code> loops. A tutorial devoted to the technology can be found
<a class="reference external" href="https://hpc-tutorials.llnl.gov/openmp/">here</a>.</p>
<p>Access to the OpenMP API is via a set of macros defined in
<a class="reference external" href="https://github.com/mantidproject/mantid/blob/main/Framework/Kernel/inc/MantidKernel/MultiThreaded.h">MultiThreaded.h</a>.
This accomplishes seamless fall-back to single-threaded behaviour for
compilers that don’t have OpenMP available, as well as providing
protection against multithreading when non-thread-safe workspaces are in use.</p>
<p>The recommended way to use OpenMP in an algorithm loop (typically
one over the spectra in a workspace) is as follows:</p>
<div class="highlight-cpp notranslate"><div class="highlight"><pre><span></span><span class="n">PARALLEL_FOR_IF</span><span class="p">(</span><span class="n">Kernel</span><span class="o">::</span><span class="n">threadSafe</span><span class="p">(</span><span class="o">*</span><span class="n">inputWS</span><span class="p">,</span><span class="w"> </span><span class="o">*</span><span class="n">outputWS</span><span class="p">))</span>
<span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">numSpec</span><span class="p">;</span><span class="w"> </span><span class="o">++</span><span class="n">i</span><span class="p">)</span>
<span class="p">{</span>
<span class="w"> </span><span class="n">PARALLEL_START_INTERRUPT_REGION</span>
<span class="w"> </span><span class="c1">// .... algorithm code ....</span>
<span class="w"> </span><span class="n">PARALLEL_END_INTERRUPT_REGION</span>
<span class="p">}</span>
<span class="n">PARALLEL_CHECK_INTERRUPT_REGION</span>
</pre></div>
</div>
<p>The main work is in the first statement, which contains the
instruction invoking OpenMP, but only if the workspaces given are
thread-safe. Analogous macros are available for zero, 2 or 3 workspaces.
Any workspace that is accessed within the loop should be included.</p>
<p>There is then also a set of slightly verbose interrupt instructions, which
prevent exceptions escaping from a parallel region (which would
otherwise cause program termination) - this includes dealing with
algorithm cancellation.</p>
<p>If you need to use OpenMP in a way that is not covered by any of the specific macros in
<a class="reference external" href="https://github.com/mantidproject/mantid/blob/main/Framework/Kernel/inc/MantidKernel/MultiThreaded.h">MultiThreaded.h</a>
, you can make use of the general purpose <code class="docutils literal notranslate"><span class="pre">PRAGMA_OMP</span></code> macro. This is essentially the same as using <code class="docutils literal notranslate"><span class="pre">#pragma</span> <span class="pre">omp</span></code>
directly. Ideally, this should only be used if what you want to do is not already covered by the other macros.</p>
<div class="highlight-cpp notranslate"><div class="highlight"><pre><span></span><span class="c1">// Dynamic scheduling allows x to be split into chunks of size 1 processed by each thread,</span>
<span class="c1">// but assigned to threads in no particular order.</span>
<span class="n">PRAGMA_OMP</span><span class="p">(</span><span class="n">parallel</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">schedule</span><span class="p">(</span><span class="n">dynamic</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">))</span>
<span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">x</span><span class="p">.</span><span class="n">size</span><span class="p">();</span><span class="w"> </span><span class="o">++</span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">doThing</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Note: The set of <code class="docutils literal notranslate"><span class="pre">INTERRUPT</span></code> macros can only be used in Mantid algorithms. The rest can be used anywhere.</p>
<section id="ensuring-thread-safety">
<h3>Ensuring thread-safety<a class="headerlink" href="#ensuring-thread-safety" title="Link to this heading">¶</a></h3>
<p>The first rule is this: <strong>Don’t write to shared variables.</strong> Or, if you
do, protect the write with PARALLEL_CRITICAL or PARALLEL_ATOMIC calls.</p>
<div class="highlight-cpp notranslate"><div class="highlight"><pre><span></span><span class="c1">// Can only be used on simple operations, uses atomic access from machine hardware.</span>
<span class="n">PARALLEL_ATOMIC</span>
<span class="n">a</span><span class="o">++</span>
<span class="c1">// Can be used anywhere, but has a higher overhead.</span>
<span class="c1">// Can be named if two critical sections can be accessed simultaneously.</span>
<span class="n">PARALLEL_CRITICAL</span><span class="p">(</span><span class="s">"C1"</span><span class="p">)</span>
<span class="k">if</span><span class="p">(</span><span class="n">a</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="mi">4</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">update</span><span class="p">()</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Note that a write to a workspace data spectrum selected by the loop
index is not typically a shared write (though see below).</p>
<p>One gotcha comes from the use of copy-on-write pointers to store the
workspace data. Here’s an example:</p>
<div class="highlight-cpp notranslate"><div class="highlight"><pre><span></span><span class="c1">// Get references to the x data</span>
<span class="k">const</span><span class="w"> </span><span class="k">auto</span><span class="o">&</span><span class="w"> </span><span class="n">xIn</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">inputWS</span><span class="o">-></span><span class="n">x</span><span class="p">(</span><span class="n">i</span><span class="p">);</span>
<span class="k">auto</span><span class="o">&</span><span class="w"> </span><span class="n">xOut</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">outputWS</span><span class="o">-></span><span class="n">mutableX</span><span class="p">(</span><span class="n">i</span><span class="p">);</span>
</pre></div>
</div>
<p>This can cause problems in the case where the input and output
workspaces are the same. Although the call to <code class="docutils literal notranslate"><span class="pre">outputWS->mutableX()</span></code> to get a
reference to the output data may look innocuous, in the case where
different spectra are pointing to the same underlying data array this
call will cause the array to be copied, which will invalidate the
reference obtained to the input data in the previous line. The solution
is to make sure the non-const calls come before the const ones (in this
case by reversing the two lines).</p>
</section>
</section>
<section id="python">
<h2>Python<a class="headerlink" href="#python" title="Link to this heading">¶</a></h2>
<p>Tasks in python can be run outside of the main GUI thread by using the classes and functions defined in
<a class="reference external" href="https://github.com/mantidproject/mantid/blob/main/qt/python/mantidqt/mantidqt/utils/asynchronous.py">asynchronous.py</a></p>
<p>The simplest and most commonly used one is <code class="docutils literal notranslate"><span class="pre">AsyncTask</span></code>:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="bp">self</span><span class="o">.</span><span class="n">worker</span> <span class="o">=</span> <span class="n">AsyncTask</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">to_be_run</span><span class="p">,</span> <span class="p">(</span><span class="n">param_1</span><span class="p">,</span> <span class="n">param_2</span><span class="p">),</span>
<span class="n">error_cb</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_on_worker_error</span><span class="p">,</span>
<span class="n">finished_cb</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_on_worker_success</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">worker</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
</pre></div>
</div>
<p>For more OpenMP style multithreading, there is the functionality inside
<a class="reference external" href="https://github.com/mantidproject/mantid/blob/main/qt/python/mantidqt/mantidqt/utils/async_qt_adaptor.py">async_qt_adaptor.py</a></p>
<p>The methods you wish to run asynchronously must be inside a class that inherits from <code class="docutils literal notranslate"><span class="pre">IQtAsync</span></code>.
You can then overwrite any of the relevant callbacks and annotate async methods with the <code class="docutils literal notranslate"><span class="pre">@qt_async_task</span></code> decorator.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">DoesAsyncThings</span><span class="p">(</span><span class="n">IQtAsync</span><span class="p">):</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">finished_cb_slot</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-></span> <span class="kc">None</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">task_finished</span><span class="p">()</span>
<span class="nd">@qt_async_task</span>
<span class="k">def</span> <span class="nf">do_async</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">do_task</span><span class="p">()</span>
</pre></div>
</div>
<p>Note: These methods are only useful for stopping mantid from hanging while something else is processing.
Due to the nature of the Global Interpreter Lock (GIL), it is not possible to run concurrent threads in python.
For heavy lifting that would require multithreading you should use C++ instead.</p>
</section>
</section>
</div>
</div>
</div>
<footer class="footer">
<div class="container">
<ul class="nav navbar-nav" style=" float: right;">
<li>
<a href="MatplotlibInCpp.html" title="Previous Chapter: Matplotlib in C++"><span class="glyphicon glyphicon-chevron-left visible-sm"></span><span class="hidden-sm hidden-tablet">« Matplotlib in C++</span>
</a>
</li>
<li>
<a href="PythonVSCppAlgorithms.html" title="Next Chapter: Python vs C++ Algorithms"><span class="glyphicon glyphicon-chevron-right visible-sm"></span><span class="hidden-sm hidden-tablet">Python vs C++... »</span>
</a>
</li>
<li><a href="#">Back to top</a></li>
</ul>
<p>
</p>
</div>
</footer>
</body>
</html>