-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlecture14.tex
420 lines (316 loc) · 11.4 KB
/
lecture14.tex
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
%Template
%Copyright (C) 2019 Patrick Diehl
%
%This program is free software: you can redistribute it and/or modify
%it under the terms of the GNU General Public License as published by
%the Free Software Foundation, either version 3 of the License, or
%(at your option) any later version.
%This program is distributed in the hope that it will be useful,
%but WITHOUT ANY WARRANTY; without even the implied warranty of
%MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
%GNU General Public License for more details.
%You should have received a copy of the GNU General Public License
%along with this program. If not, see <http://www.gnu.org/licenses/>.
\providecommand\classoption{12pt}
\documentclass[\classoption]{beamer}
\input{./template/packages}
\input{template/variables.tex}
% frame slide
\title{\coursename}
\subtitle{Lecture 14: Serial partition-based 1D heat equation }
\author{\tiny Patrick Diehl \orcid{0000-0003-3922-8419}}
%\institute {
% \href{}{\tt \scriptsize \today}
%}
\date {
\tiny \url{\courseurl}
\vspace{2cm}
\doclicenseThis
}
\pgfplotsset{compat=1.16}
\usepackage{ifxetex}
\ifxetex
\usepackage{fontspec}
\setmainfont{Raleway}
\fi
\ifluatex
\usepackage{fontspec}
\setmainfont{Raleway}
\fi
\begin{document} {
\setbeamertemplate{footline}{}
\frame {
\titlepage
}
}
\frame{
\tableofcontents
}
\AtBeginSection[]{
\begin{frame}
\vfill
\centering
\begin{beamercolorbox}[sep=8pt,center,shadow=true,rounded=true]{title}
\usebeamerfont{title}\insertsectionhead\par%
\end{beamercolorbox}
\vfill
\end{frame}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Reminder}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Lecture 13}
\begin{block}{What you should know from last lecture}
\begin{itemize}
\item \lstinline|hpx::make_reday_future|
\item \lstinline|hpx::dataflow|
\item \lstinline|hpx::unwrapping|
\item \lstinline|hpx::shared_future|
\end{itemize}
\end{block}
\end{frame}
\section{Recap of the previous implementation}
\begin{frame}{Scaling results from previous example}
\begin{center}
\begin{tikzpicture}[scale=0.75]
\mode<beamer>{
\begin{axis}[xlabel=Grid points,ylabel=Execution time, grid=both,title= Stencil 2,legend pos=north west,legend style = {text=white,fill=background,draw=background}]
\addplot[azure] table [x=Grid_Points, y=Execution_Time_sec, col sep=comma] {./data/stencil_2_1.dat};
\addlegendentry{1 CPU}
\addplot[awesome] table [x=Grid_Points, y=Execution_Time_sec, col sep=comma] {./data/stencil_2_2.dat};
\addlegendentry{2 CPU}
\addplot[asparagus] table [x=Grid_Points, y=Execution_Time_sec, col sep=comma] {./data/stencil_2_4.dat};
\addlegendentry{4 CPU}
\addplot[ atomictangerine] table [x=Grid_Points, y=Execution_Time_sec, col sep=comma] {./data/stencil_2_6.dat};
\addlegendentry{6 CPU}
\end{axis}
}
\mode<handout>{
\selectcolormodel{gray}
\begin{axis}[xlabel=Grid points,ylabel=Execution time, grid=both,title= Stencil 2,legend pos=north west]
\addplot table [x=Grid_Points, y=Execution_Time_sec, col sep=comma] {./data/stencil_2_1.dat};
\addlegendentry{1 CPU}
\addplot table [x=Grid_Points, y=Execution_Time_sec, col sep=comma] {./data/stencil_2_2.dat};
\addlegendentry{2 CPU}
\addplot table [x=Grid_Points, y=Execution_Time_sec, col sep=comma] {./data/stencil_2_4.dat};
\addlegendentry{4 CPU}
\addplot table [x=Grid_Points, y=Execution_Time_sec, col sep=comma] {./data/stencil_2_6.dat};
\addlegendentry{6 CPU}
\end{axis}
}
\end{tikzpicture}
\end{center}
Note that we need to control the grain size (the amount of work) to get better scalability.
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Introducing partitions}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Goal of this week's lectures}
We will reuse the serial implementation of Lecture 12 and
\begin{itemize}
\item introduces a partitioning of the 1D grid into groups of grid partitions
\item which are handled at the same time.
\end{itemize}
So we can control the the amount of work performed (grain size) to improve the scalability of our program. \\
\vspace{0.25cm}
Note that before, we had exact one discrete mesh point handle per \lstinline|hpx::future| and now we want to have multiple discrete mesh points.
\end{frame}
\begin{frame}[fragile]{Generating partitions}
\begin{center}
\begin{tikzpicture}
\foreach \i in {1,...,9}{
\node at (\i,0) {\pgfuseplotmark{square*}};
}
\foreach \i in {1,...,9}{
\node[below] at (\i,-0.1) {$x_\i$};
}
\draw (1,-.6) -- (1,-1);
\draw (3.5,-.6) -- (3.5,-1);
\draw (6.5,-.6) -- (6.5,-1);
\draw (9,-.6) -- (9,-1);
\node[below] at (2.5,-1) {$n_1$};
\node[below] at (5,-1) {$n_2$};
\node[below] at (8,-1) {$n_3$};
\end{tikzpicture}
\end{center}
\begin{block}{Struct holding the data for each partition}
\begin{lstlisting}
struct partition_data
{
private:
std::vector<double> data_;
};
\end{lstlisting}
\end{block}
\end{frame}
\begin{frame}[fragile]{Data structure for the partitions I}
\begin{lstlisting}
struct partition_data
{
partition_data(std::size_t size = 0)
: data_(size)
{}
partition_data(std::size_t size, double int_value)
: data_(size)
{
double base_value =
double(int_value * size);
for (std::size_t i = 0; i != size; ++i)
data_[i] = base_value + double(i);
}
}
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]{Data structure for the partitions II}
\begin{lstlisting}
struct partition_data
{
double& operator[](std::size_t idx) {
return data_[idx];
}
double operator[](std::size_t idx) const {
return data_[idx];
}
std::size_t size() const
{
return data_.size();
}
}
\end{lstlisting}
\end{frame}
\section{Swapping the partitions}
\begin{frame}{New swapping scheme}
\vspace{-0.5cm}
\begin{center}
\begin{tikzpicture}
\foreach \j in {0,...,1}{
\foreach \i in {1,...,4}{
\node at (\j*5+\i,1.5) {\pgfuseplotmark{square*}};
}
\foreach \i in {1,...,4}{
\node at (\j*5+\i,0.75) {\pgfuseplotmark{square*}};
}
\foreach \i in {1,...,4}{
\node at (\j*5+\i,0) {\pgfuseplotmark{square*}};
}
\foreach \i in {1,...,4}{
\node[below] at (\j*5+\i,-0.1) {$x_\i$};
}
\node[left] at (\j*5+0.5,0) {\small U[0][\j]};
\node[left] at (\j*5+0.5,0.75) {\small U[1][\j]};
\node[left] at (\j*5+0.5,1.5) {\small U[0][\j]};
\draw (\j*5+2,0) -- (\j*5+3,0.75);
\draw (\j*5+3,0) -- (\j*5+3,0.75);
\draw (\j*5+4,0) -- (\j*5+3,0.75);
\draw (\j*5+2,0.75) -- (\j*5+3,1.5);
\draw (\j*5+3,0.75) -- (\j*5+3,1.5);
\draw (\j*5+4,0.75) -- (\j*5+3,1.5);
}
\node[right] at (9.5,0) {t=1};
\node[right] at (9.5,0.75) {t=2};
\node[right] at (9.5,1.5) {t=3};
\end{tikzpicture}
\end{center}
\end{frame}
\begin{frame}[fragile]{Adapted class stepper}
\begin{lstlisting}
class stepper
{
// Our data for one time step
typedef partition_data partition;
typedef std::vector<partition> space;
std::vector<space> U(2);
for (space& s: U)
// np is the number of partitions
s.resize(np);
// Initial conditions: f(0, i) = i
for (std::size_t i = 0; i != np; ++i)
U[0][i] = partition_data(nx, double(i));
// Return the solution at time-step 'nt'.
return U[nt % 2];
}
\end{lstlisting}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{New C++ and HPX features }
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{Moving objects with \lstinline|std::move|\footnote{\tiny\url{https://en.cppreference.com/w/cpp/utility/move}}}
We use \lstinline|std::move| to indicate that the object may be moved to another object. This allows the efficient transfer of resources to another object.
\begin{block}{Example}
\begin{lstlisting}
std::string str = "Hello";
std::vector<std::string> v;
v.push_back(std::move(str));
\end{lstlisting}
\end{block}
\begin{block}{Be aware of undefined states, but valid states}
\begin{lstlisting}
str.clear{} //Ok, since clear has no preconditions
str.back() //Undefined behavior if size()==0
\end{lstlisting}
\end{block}
\end{frame}
\begin{frame}{Semaphore}
\begin{block}{Analogy}
Imagine a public library lending books with no late fee. The have 5 copies of the Hitchhiker's Guide to the Galaxy~\cite{adams2017hitchhiker} to borrow. Five people can borrow these copies and the library does not care to get them back in a feasible amount of time, since they avoided to introduce late fees. If one is waiting for a copy the copy will be assigned to this person, but if none is waiting the copy just goes back to the shelf until one asks for it.
\end{block}
\vspace{0.5cm}
The concept of semaphores was introduced by E. Dijkstra~\cite{dijkstra1962over}. More details~\cite{downey2008little}.
\end{frame}
\begin{frame}{P and V operations on a semaphore objects}
\begin{block}{Variables}
\begin{itemize}
\item maximum count
\item current count
\end{itemize}
\end{block}
\begin{block}{Operations}
\begin{itemize}
\item Taking ownership with the \lstinline|wait| function, which decrements the semaphore. The so--called $P$ operation from Dijkstra's paper.
\item Releasing ownership with the \lstinline|signal| function, the increments the semaphore. The so--called $V$ operation from Dijkstra's paper.
\end{itemize}
\end{block}
\end{frame}
\begin{frame}{More details}
\begin{block}{$P$-Operation}
If the \lstinline|wait| function is called, the current count is decreased. If the count is $\geq$ zero then the decrement just happens and the function will return. If the count is zero the function will wait until one other thread called the \lstinline|signal| function.
\end{block}
\begin{block}{$V$-Operation}
If the \lstinline|signal| function is called, the current count is increased. If the count was zero before you called \lstinline|signal| function and another thread was blocked in \lstinline|wait| then that thread will be executed. If multiple threads are waiting, only one will be executed and the reaming ones have to wait for another increment of the counter.
\end{block}
\end{frame}
\begin{frame}[fragile]{Semaphores in the C++ standard}
We looked into \lstinline|std::mutex| in Lecture 6, which is tied to one thread and only one thread can lock or unlock the mutex. Any thread can access the ownership on a semaphore. \\
\begin{center}
\textcolor{red}{The C++ standard does not define semaphores.}
\end{center}
\begin{lstlisting}
// Generate a semaphore with maximal count nd
hpx::lcos::local::sliding_semaphore sem(nd);
// Release ownership for t
sem.signal(t);
// Obtain ownership for t
sem.wait(t);
\end{lstlisting}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Summary}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Summary}
\begin{block}{After this lecture, you should know}
\begin{itemize}
\item Using the partitions to control the grain size
\item \lstinline|std::move| for moving objects
\item Semaphores and \lstinline|hpx::lcos::local::sliding_semaphore|
\end{itemize}
\end{block}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{References}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[t,allowframebreaks]
\frametitle{References}
\bibliographystyle{plain}
\bibliography{bib}
\end{frame}
\end{document}