-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpar-reduce.tex
63 lines (54 loc) · 1.51 KB
/
par-reduce.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
\subsection{Reductions}
\begin{frame}[t]{Reduction pattern}
\begin{itemize}
\item A \textmark{reduction} computes the sum of all elements
in a data set.
\vspace{1em}\pause
\item \textbad{Note}: \cppid{std::reduce} looks quite similar
to \cppid{std::accumulate} on the surface.
\begin{itemize}
\item Result is not deterministic unless the sum opration
is both associative and commutative.
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}[t,fragile]{Add all elements in a vector}
\begin{lstlisting}
void print_add(const std::vector<double> & v)
{
double r = std::reduce(std::execution::par,
v.begin(), v.end());
std::cout << "sum= " << r << "\n";
}
\end{lstlisting}
\vfill
\begin{itemize}
\item Initial value is \cppid{value\_type\{\}}.
\item Binary operation is \cppid{std::plus<>()}.
\end{itemize}
\end{frame}
\begin{frame}[t,fragile]{Providing initial value}
\begin{lstlisting}
void print_add(const std::vector<double> & v)
{
double r = std::reduce(std::execution::par,
v.begin(), v.end(), 100.0);
std::cout << "sum= " << r << "\n";
}
\end{lstlisting}
\begin{itemize}
\item Still reduction operation is \cppid{std::plus<>()}.
\end{itemize}
\end{frame}
\begin{frame}[t,fragile]{Providing reduction operator}
\begin{lstlisting}
void print_add(const std::vector<double> & v)
{
double r = std::reduce(std::execution::par,
v.begin(), v.end(), 0.0,
[](double x, double y) { return x+y; }
);
std::cout << "sum= " << r << "\n";
}
\end{lstlisting}
\end{frame}