-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlecture7.tex
345 lines (275 loc) · 9.47 KB
/
lecture7.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
%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 7: Asynchronous programming }
\author{\tiny Patrick Diehl \orcid{0000-0003-3922-8419}}
%\institute {
% \href{}{\tt \scriptsize \today}
%}
\date {
\tiny \url{\courseurl}
\vspace{2cm}
\doclicenseThis
}
\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 6}
\begin{block}{What you should know from last lecture}
\begin{itemize}
\item Shared memory parallelism
\item Parallel algorithms and execution policies
\item Data races and dead locks
\end{itemize}
\end{block}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Asynchronous programming}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{Synchronous programming}
\begin{columns}[T]
\begin{column}{0.5\textwidth}
\begin{block}{Dependency graph}
\begin{center}
\begin{tikzpicture}
\node (a) [draw,circle] at (0,2) {$H$};
\node (b) [draw,circle] at (0,0) {$P$};
\node (c) [draw,circle] at (1,0) {$X$};
\draw [->] (a) -- (b);
\draw [->] (a) -- (c);
\end{tikzpicture}
\end{center}
\end{block}
\end{column}
\begin{column}{0.5\textwidth} %%<--- here
\begin{block}{Code}
\begin{lstlisting}
auto P = compute();
auto X = compute();
auto H = compute(P,X);
\end{lstlisting}
\end{block}
\end{column}
\end{columns}
\begin{itemize}
\item The program is executed line by line
\item Each time a function is called the code waits until the functions finishes
\item We can not compute P and X at the same time, since the data is independent
\end{itemize}
\end{frame}
\begin{frame}[fragile]{Asynchronous programming~\cite{williams2012c++} }
\begin{block}{Code}
\begin{lstlisting}
int P,X = 1;
std::future<int> f1 = std::async(compute,P);
auto f2 = std::async(compute,X);
std::cout << compute(f1.get() + f2.get()) << std::endl;
\end{lstlisting}
\end{block}
\begin{itemize}
\item The program is some times executed line by line
\item Calling \lstinline|std::async| the next line is executed, even if the function has not finished yet
\item We have to use the \lstinline|std::future| to synchronize the asynchronous function calls
\end{itemize}
\tiny More details: CppCon 2017: H. Kaiser “The Asynchronous C++ Parallel Programming Model”\footnote{\tiny\url{https://www.youtube.com/watch?v=js-e8xAMd1s}}
\end{frame}
\begin{frame}[fragile]{Asynchronous execution of functions\footnote{\tiny\url{http://www.cplusplus.com/reference/future/async/}}}
\begin{lstlisting}
bool is_prime (int x) {
std::cout << "Calculating. Please, wait...\n";
for (int i=2; i<x; ++i) if (x%i==0) return false;
return true;
}
std::future<bool> f = std::async (is_prime,313222313);
\end{lstlisting}
\begin{itemize}
\item The first argument \lstinline|fn| is a function pointer
\item The second argument is the first argument of the function, and so on
\item The return value is a \lstinline|std::future<T>| where \lstinline|T| is the return type of the function
\end{itemize}
For each call of \lstinline|std::async| launches a new thread to execute the function the function pointer \lstinline|fn| points to.
\end{frame}
\begin{frame}{Futurization\footnote{\tiny\url{https://en.cppreference.com/w/cpp/thread/future}}}
A \lstinline|std::future| provides a mechanism to access the result of asynchronous operations, like \lstinline|std::async| and provides methods for synchronization.
\begin{block}{Synchronization}
\begin{itemize}
\item \lstinline|.get()| returns the result of the functions and wait until the computation finished
\item \lstinline|.wait()| waits until the computation finished
\item \lstinline|.wait_for(std::chrono::seconds(1))| returns if it is not available for the specified timeout duration
\item \lstinline|.wait_until(std::chrono::seconds(1))| waits for a result to become available. It blocks until specified timeout time has been reached or the result becomes available, whichever comes first.
\end{itemize}
\end{block}
\end{frame}
\begin{frame}{Parallelism using asynchronous programming}
\begin{block}{Example: Taylor series}
$$
sin(x) = \sum\limits_{n=0}^n (-1)^{n-1} \frac{x^{2n}}{(2n)!}
$$
\end{block}
\begin{block}{Approach}
\begin{enumerate}
\item Split $n$ into slices, e.g. 2 times $\sfrac{n}{2}$ for two threads
\item Start two times \lstinline|std::async| where each thread computes $\sfrac{n}{2}$
\item Use the two futures to synchronize the results
\item Combine the two futures to obtain the result
\end{enumerate}
\end{block}
\end{frame}
\begin{frame}[fragile]{Implementation I}
\begin{block}{Function}
\begin{lstlisting}
double taylor(size_t begin, size_t end,
double x,size_t n){
double res = 0;
for( size_t i = begin ; i < end ; i++)
{
res += pow(-1,i-1) * pow(x,2*n) / factorial(2*n);
}
return res;
}
\end{lstlisting}
\end{block}
\begin{itemize}
\item With \lstinline|begin| and \lstinline|end|, the range is defined
\item The range needs to be adapted to the amount of threads you want to launch
\end{itemize}
\end{frame}
\begin{frame}[fragile]{Implementation II}
\begin{block}{Launching}
\begin{lstlisting}
auto f1 = std::async(taylor,0,49,2,100);
auto f2 = std::async(taylor,50,99,2,100);
\end{lstlisting}
\end{block}
\begin{block}{Gathering the results}
\begin{lstlisting}
double result = f1.get() + f2.get();
\end{lstlisting}
\end{block}
\begin{block}{Compilation}
\begin{lstlisting}[language=bash]
g++ main.cpp -o futures -phtread
\end{lstlisting}
We need to add \lstinline|-pthread| to our compiler to use the POSIX threads to launch the functions asynchronous (\lstinline|std::async|)
\end{block}
More details about POSIX threads~\cite{butenhof1997programming,kleiman1996programming}.
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Lambda functions}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{Lambda expression\footnote{\tiny{\url{https://en.cppreference.com/w/cpp/language/lambda}}} }
\begin{block}{Structure}
\begin{lstlisting}
[ capture clause ] (parameters) -> return-type
{
definition of method
}
\end{lstlisting}
\end{block}
\begin{block}{Notes}
\begin{itemize}
\item Generally return-type in lambda expression are evaluated by compiler
\item Capture clause:
\begin{itemize}
\item \lstinline|[&]| : capture all external variable by reference
\item \lstinline|[=]| : capture all external variable by value
\item \lstinline|[a, &b]| : capture a by value and b by reference
\end{itemize}
\end{itemize}
\end{block}
\vspace{-0.2cm}
\begin{center}
More about the capture clauses in lecture 11/12.
\end{center}
\end{frame}
\begin{frame}[fragile]{Practical example}
\begin{lstlisting}
std::vector<int> v {4, 1, 3, 5, 2, 3, 1, 7};
\end{lstlisting}
\begin{block}{Classical function}
\begin{lstlisting}
void print(int i){
std::cout << i << std::endl;
}
std::for_each(v.begin(), v.end(), print);
\end{lstlisting}
\end{block}
\begin{block}{Lambda expression}
\begin{lstlisting}
std::for_each(v.begin(),v.end(),
[](int i){std::cout<< i << std::endl;})
\end{lstlisting}
\end{block}
\end{frame}
\begin{frame}[fragile]{More examples}
\begin{block}{\lstinline|find_if|\footnote{\tiny\url{https://en.cppreference.com/w/cpp/algorithm/find}}}
\begin{lstlisting}
std::vector<int>:: iterator p = find_if(
v.begin(),
v.end(),
[](int i)
{
return i > 4;
});
std::cout << "First number greater than 4 is :
" << *p
<< endl;
\end{lstlisting}
\end{block}
Many more algorithms are available in the \lstinline|#include <algorithm>|\footnote{\tiny\url{https://en.cppreference.com/w/cpp/algorithm}}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Summary}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Summary}
\begin{block}{After this lecture, you should know}
\begin{itemize}
\item Asynchronous programming \lstinline|std::async| and \lstinline|std::future|
\item Lambda functions
\end{itemize}
\end{block}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{References}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[t, allowframebreaks]
\frametitle{References}
\bibliographystyle{plain}
\bibliography{bib}
\end{frame}
\end{document}