-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexercise7.tex
82 lines (59 loc) · 2.72 KB
/
exercise7.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
\documentclass[11pt]{article}
\include{packages}
\begin{document}
% ========== Edit your name here
%\author{Your Name}
\title{\coursename~Quiz 7: Due by \DueDate{\coursedate}{51}}
\date{}
\maketitle
\medskip
% ========== Begin answering questions here
\section*{Exercises}
\begin{enumerate}
\item Programming on paper (2 credits): \\
Write a program that squares all elements in a \lstinline|std::vector<double>| and compute the sum of all elements using \lstinline|hpx::for_loop|.
\item Definitions: \\
\begin{enumerate}
\item Explain Amdahl's Law $S=\frac{1}{(1-P)+\frac{P}{N}}$, where $S$ is the speedup, $P$ is the proportion of parallel code, and $N$ the number of processors. (1 credit)
\item In the guest lecturer, the four horsemen of the apocalypse or the term SLOW was introduced. Write down each term one of the letters defines and explain the term. (1 credit)
\end{enumerate}
\end{enumerate}
\section*{Programming exercise}
\begin{enumerate}
\item $N$-body simulation: (1 credit)\\
The C++ standard library does not provide a nice way for range-based parallel for loops. HPX provides
\begin{lstlisting}
hpx::for_loop(
hpx::parallel::execution::par,
0,
values.size(),
[](boost::uint64_t i)
{
std::cout<< values[i] << std::endl;
}
);
});
\end{lstlisting}
which makes it convenient to access several \lstinline|std::vector| using a index. Rewrite the previous $N$-Body simulation using \lstinline|hpx::for_loop| and the HPX's parallel algorithms.
\item Numerical integration (5 credits)\\
The trapezoidal rule can be used to approximate the definite integral
\begin{align*}
\int\limits_a^b f(x) dx \approx \frac{h}{2} \sum\limits_{k=1}^N (f(x_{k-1}) + f(x_k))
\end{align*}
assuming a uniform grid in the interval $[a,b]$ with the grid size $h=\frac{b-a}{N}$.
\begin{enumerate}
\item Use \lstinline|hpx::future| and \lstinline|hpx::async| compute the solution asynchronously. (1 credit)
\item Let the user define the number of threads and store all \lstinline|hpx::future| in a \lstinline|std::vector| and use \lstinline|hpx::when_all| for synchronization. (1 credit)
\item Use the \lstinline|.then()| method of a \lstinline|hpx::future| to calculate the results and print the result. (1 credit)
\item HPX can launch a \lstinline|hpx::for_loop| and return a \lstinline|hpx::future|. Instead of calling \lstinline|hpx::async| use the future from the \lstinline|hpx::for_loop| to do the asynchronous programming. (2 credits)
\end{enumerate}
Validate your implementations against the solution
\begin{align*}
\int\limits_0^2 x^2 = \left\vert \frac{x^3}{3}\right\vert_0^2 = \frac{2^3}{3} - \frac{0^2}{3} = \frac{8}{3}\text{.}
\end{align*}
% ========== Continue adding items as needed
\end{enumerate}
\doclicenseThis
\end{document}
\grid
\grid