-
Notifications
You must be signed in to change notification settings - Fork 0
/
MultiThreadingInAlgorithms.html
175 lines (137 loc) · 8.99 KB
/
MultiThreadingInAlgorithms.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
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Multithreading in Algorithms</title>
<link rel="stylesheet" href="_static/bootstrap-sphinx.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<link rel="stylesheet" type="text/css" href="_static/custom.css" />
<script type="text/javascript" id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<script async="async" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></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"><img src="_static/Mantid_Logo_Transparent.png">
</a>
<span class="navbar-text navbar-version pull-left"><b>master</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="http://download.mantidproject.org">Download</a></li>
<li><a href="http://www.mantidproject.org">Wiki</a></li>
<li><a href="http://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>
</div>
<div class="container">
<div class="row">
<div class="body col-md-12 content" role="main">
<div class="section" id="multithreading-in-algorithms">
<h1>Multithreading in Algorithms<a class="headerlink" href="#multithreading-in-algorithms" title="Permalink to this headline">¶</a></h1>
<p>Mantid uses <a class="reference external" href="http://openmp.org/wp/about-openmp/">OpenMP</a> to improve
performance within algorithms 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://computing.llnl.gov/tutorials/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">MultiThreading.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 canonical way to use OpenMP in an algorithm loop (typically
one over the spectra in a workspace) is as follows:</p>
<div class="code cpp highlight-default 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="p">::</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="o">*</span><span class="n">outputWS</span><span class="p">))</span>
<span class="k">for</span> <span class="p">(</span><span class="nb">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o"><</span> <span class="n">numSpec</span><span class="p">;</span> <span class="o">++</span><span class="n">i</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">PARALLEL_START_INTERUPT_REGION</span>
<span class="o">//</span> <span class="o">....</span> <span class="n">algorithm</span> <span class="n">code</span> <span class="o">....</span>
<span class="n">PARALLEL_END_INTERUPT_REGION</span>
<span class="p">}</span>
<span class="n">PARALLEL_CHECK_INTERUPT_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. 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>
<div class="section" id="ensuring-thread-safety">
<h2>Ensuring thread-safety<a class="headerlink" href="#ensuring-thread-safety" title="Permalink to this headline">¶</a></h2>
<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.
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="code cpp highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o">//</span> <span class="n">Get</span> <span class="n">references</span> <span class="n">to</span> <span class="n">the</span> <span class="n">x</span> <span class="n">data</span>
<span class="n">const</span> <span class="n">auto</span><span class="o">&</span> <span class="n">xIn</span> <span class="o">=</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="n">auto</span><span class="o">&</span> <span class="n">xOut</span> <span class="o">=</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->x()</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>
</div>
</div>
</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>