-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path01-intro.tex
282 lines (223 loc) · 10.3 KB
/
01-intro.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
\documentclass{beamer}
% Theme choice
\usetheme{Madrid}
% Optional packages
\usepackage{graphicx} % For including images
\usepackage{amsmath} % For math symbols and formulas
\usepackage{hyperref} % For hyperlinks
\usepackage{listings}
\usepackage{xcolor}
\usepackage[T1]{fontenc}
\lstdefinestyle{CStyle}{
language=C, % Set the language to C
basicstyle=\ttfamily\footnotesize\linespread{0.9}\tiny, % Set font style and size
keywordstyle=\color{blue}, % Color of keywords
commentstyle=\color{gray}, % Color of comments
stringstyle=\color{red}, % Color of strings
showstringspaces=false, % Do not mark spaces in strings
breaklines=true, % Enable line breaks at appropriate places
breakatwhitespace=false, % Break lines at any character, not just whitespace
numbers=left, % Show line numbers on the left
numberstyle=\tiny\color{gray}, % Style for line numbers
tabsize=4, % Set tab width
keepspaces=true, % Keep indentation spaces
frame=single, % Add a border around the code
aboveskip=0pt, % Reduce space above the code block
belowskip=0pt, % Reduce space below the code block
xleftmargin=7.5pt, % Add left padding (approx. 2.8mm or 10px)
xrightmargin=15pt, % Add left padding (approx. 2.8mm or 10px)
}
% Title, author, date, and institute (optional)
\title[Parallel Programming. Introduction]{Parallel Programming course. Introduction}
\author{Obolenskiy Arseniy, Nesterov Alexander}
\institute{Nizhny Novgorod State University}
\date{\today} % or \date{Month Day, Year}
% Redefine the footline to display both the short title and the university name
\setbeamertemplate{footline}{
\leavevmode%
\hbox{%
\begin{beamercolorbox}[wd=.45\paperwidth,ht=2.5ex,dp=1ex,leftskip=1em,center]{author in head/foot}%
\usebeamerfont{author in head/foot}\insertshortinstitute % Displays the university name
\end{beamercolorbox}%
\begin{beamercolorbox}[wd=.45\paperwidth,ht=2.5ex,dp=1ex,leftskip=1em,center]{author in head/foot}%
\usebeamerfont{author in head/foot}\insertshorttitle % Displays the short title
\end{beamercolorbox}%
\begin{beamercolorbox}[wd=.1\paperwidth,ht=2.5ex,dp=1ex,rightskip=1em,center]{author in head/foot}%
\usebeamerfont{author in head/foot}\insertframenumber{} / \inserttotalframenumber
\end{beamercolorbox}}%
\vskip0pt%
}
\begin{document}
% Title slide
\begin{frame}
\titlepage
\end{frame}
% Table of Contents (optional)
\begin{frame}{Contents}
\tableofcontents
\end{frame}
% Section
\section{Introduction to MPI}
\begin{frame}[fragile]{What is MPI?}
MPI (Message Passing Interface) is a standardized and portable message-passing system, designed to function on a variety of parallel computing architectures.
Primarily used in high-performance computing (HPC) to allow different processes to communicate with each other in a distributed memory environment.
\end{frame}
\begin{frame}[fragile]{MPI: library vs standard}
\begin{table}[h!]
\begin{tabular}{| p{2.1cm} | p{4.2 cm} | p{4.2 cm} |}
\hline
\textbf{Aspect} & \textbf{MPI Standard} & \textbf{MPI Library} \\
\hline
\textbf{Definition} & A formal set of specifications & A concrete software implementation \\
\hline
\textbf{Purpose} & Defines the behavior of message-passing systems & Provides a runnable implementation of the standard \\
\hline
\textbf{Portability} & Platform-agnostic guidelines & Implementations may be platform-specific \\
\hline
\textbf{Performance} & No direct impact on performance & Optimized for different platforms and hardware \\
\hline
\textbf{Examples} & MPI-1, MPI-2, MPI-3, MPI-4 (specifications) & MPICH, Open MPI, Intel MPI \\
\hline
\end{tabular}
\caption{Key Differences Between MPI Standard and MPI Library}
\end{table}
\end{frame}
\section{"Hello, World" in MPI}
% "Hello, World" in MPI
\begin{frame}[fragile]{"Hello, World" in MPI}
\lstset{style=CStyle, caption=Basic application written using MPI}
\begin{lstlisting}
#include <mpi.h>
#include <iostream>
int main(int argc, char** argv) {
MPI_Init(&argc, &argv);
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
char processor_name[MPI_MAX_PROCESSOR_NAME];
int len_chars;
MPI_Get_processor_name(processor_name, &len_chars);
MPI_Barrier(MPI_COMM_WORLD);
std::cout << "Processor = " << processor_name << std::endl;
std::cout << "Rank = " << world_rank << std::endl;
std::cout << "Number of processors = " << world_size << std::endl;
MPI_Finalize();
return 0;
}
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]{Compiling MPI application}
Linux: \\
\texttt{mpicc -o hello\_mpi hello\_mpi.c}
Windows: \\
\texttt{cl /I"C:}\texttt{\textbackslash}\texttt{Program Files (x86)}\texttt{\textbackslash}\texttt{Microsoft SDKs}\texttt{\textbackslash}\texttt{MPI}\texttt{\textbackslash}\texttt{Include" hello\_mpi.c /link /LIBPATH:"C:}\texttt{\textbackslash}\texttt{Program Files (x86)}\texttt{\textbackslash}\texttt{Microsoft SDKs}\texttt{\textbackslash}\texttt{MPI}\texttt{\textbackslash}\texttt{Lib}\texttt{\textbackslash}\texttt{x64" msmpi.lib}
\end{frame}
\begin{frame}[fragile]{Running MPI application}
Important! If you run application directly (\texttt{./hello\_mpi}) you will not get expected result!
Linux: \\
\texttt{mpiexec -n 4 ./hello\_mpi}
Windows: \\
\texttt{mpirun -n 4 hello\_mpi.exe}
\end{frame}
\section{Brief API calls overview}
\begin{frame}[fragile]{MPI initialization: \texttt{MPI\_Init()}}
\texttt{int MPI\_Init(int *argc, char ***argv)}
It initializes the MPI environment and must be called before any other MPI function.
\lstset{style=CStyle, caption=Basic application written using MPI}
\begin{lstlisting}
#include <mpi.h>
#include <stdio.h>
int main(int argc, char** argv) {
// Initialize the MPI environment
MPI_Init(&argc, &argv);
...
return 0;
}
\end{lstlisting}
\end{frame}
\section{MPI data distribution}
\begin{frame}[fragile]{MPI data distribution example}
\lstset{style=CStyle, caption=MPI data distribution example}
\begin{lstlisting}
#include <mpi.h>
#include <stdio.h>
int main(int argc, char** argv) {
// Initialize the MPI environment
MPI_Init(&argc, &argv);
// Get the number of processes
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
// Get the rank of the process
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
// Define message
int number;
if (world_rank == 0) {
// If we are rank 0, set number to -1 and send it to process 1
number = -1;
MPI_Send(&number, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
printf("Process 0 sent number %d to process 1\n", number);
} else if (world_rank == 1) {
// If we are rank 1, receive the number from process 0
MPI_Recv(&number, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
printf("Process 1 received number %d from process 0\n", number);
}
// Finalize the MPI environment
MPI_Finalize();
return 0;
}
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]{\texttt{MPI\_Send()}}
\texttt{int MPI\_Send(const void *buf, int count, MPI\_Datatype datatype, int dest, int tag, MPI\_Comm comm)}
Parameters:
\begin{itemize}
\item buf: The starting address of the data buffer to be sent.
\item count: The number of elements in the buffer.
\item datatype: The type of data being sent (e.g., \texttt{MPI\_INT}, \texttt{MPI\_FLOAT}).
\item dest: The rank (ID) of the destination process.
\item tag: A user-defined message identifier to differentiate messages.
\item comm: The communicator that defines the group of processes within which the message is being sent (e.g., \texttt{MPI\_COMM\_WORLD}).
\end{itemize}
\end{frame}
\begin{frame}[fragile]{\texttt{MPI\_Recv()}}
\texttt{int MPI\_Recv(void *buf, int count, MPI\_Datatype datatype, int source, int tag, MPI\_Comm comm, MPI\_Status *status)}
Parameters:
\begin{itemize}
\item buf: The starting address of the buffer where the received data will be stored.
\item count: The maximum number of elements that the buffer can hold.
\item datatype: The type of data being received (e.g., \texttt{MPI\_INT}, \texttt{MPI\_FLOAT}).
\item source: The rank of the sending process. Use \texttt{MPI\_ANY\_SOURCE} to receive from any process.
\item tag: The message identifier (tag). Use \texttt{MPI\_ANY\_TAG} to receive any message regardless of the tag.
\item comm: The communicator for the group of processes within which the message is being received (e.g., \texttt{MPI\_COMM\_WORLD}).
\item status: A structure that contains information about the received message, such as the actual source and tag.
\end{itemize}
\end{frame}
\begin{frame}[fragile]{Performance measurement in MPI: \texttt{MPI\_Wtime()}}
\texttt{double MPI\_Wtime(void)}
\begin{itemize}
\item \texttt{MPI\_Wtime()} is a function provided by the MPI standard to measure the wall-clock time (in seconds) since some arbitrary point in the past.
\item This function is often used for performance analysis in parallel programs to measure the execution time of sections of code.
\item It returns a double precision floating-point number representing the current time. The returned time is in seconds.
\item \texttt{MPI\_Wtime()} is local to the process and does not guarantee synchronization between processes, meaning each process may have a different starting time reference.
\end{itemize}
Usage example:
\lstset{style=CStyle}
\begin{lstlisting}
double start = MPI_Wtime();
// Code to time
double end = MPI_Wtime();
double elapsed = end - start;
\end{lstlisting}
Documentation reference: \texttt{\href{https://www.mpich.org/static/docs/v3.2/www3/MPI_Wtime.html}{https://www.mpich.org/static/docs/v3.2/www3/MPI\_Wtime.html}}
\end{frame}
% Thank You Slide
\begin{frame}
\centering
\Huge{Thank You!}
\end{frame}
% Optional references slide
\begin{frame}{References}
\end{frame}
\end{document}