@@ -215,6 +215,56 @@ \section{MPI data distribution}
215
215
216
216
\end {frame }
217
217
218
+ \begin {frame }[fragile]{Problems in multiprocessing environment}
219
+ There are some typical errors that may occur in multiprocessing environment.
220
+ Case when there are variables allocated on all processes in quite common.
221
+ \lstset {style=CStyle, caption=Common variables usage example}
222
+ \ begin{lstlisting}
223
+ ...
224
+ int main(int argc, char** argv) {
225
+ ...
226
+ // Pay attention to "number" variable
227
+ int number; // NOTE: in this case data will be created on each process
228
+ // If you leave this data uninitialized it may lead to an undefined behavior
229
+ // on specific processes
230
+ if (world_rank == 0) {
231
+ // If we are rank 0, "number" variable will be initialized with -1.
232
+ // OK here
233
+ number = -1;
234
+ MPI_Send(&number, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
235
+ printf("Process 0 sent number %d to process 1\n", number);
236
+ } else if (world_rank == 1) {
237
+ // If we are rank 1, receive the number from process 0
238
+ // OK here
239
+ MPI_Recv(&number, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
240
+ printf("Process 1 received number %d from process 0\n", number);
241
+ } else {
242
+ // WARNING: If we are rank 2 and more, there is uninitialized "number" variable!
243
+ // Potential mistake
244
+ }
245
+ ...
246
+ }
247
+ \end {lstlisting }
248
+ \end {frame }
249
+
250
+ \begin {frame }[fragile]{Problems in multiprocessing environment solution?}
251
+ Example on previous slide may lead to an undefined behavior which is not obvious.
252
+
253
+ \begin {itemize }
254
+ \item In case of number of processes <= 2 there is no issue.
255
+ \item In case of number of processes > 2 there is potential gap if we use \texttt {number } variable afterwards.
256
+ \end {itemize }
257
+
258
+ How to avoid memory issues?
259
+ \begin {itemize }
260
+ \item Do not initialize \texttt {number } variable outside \texttt {if } (create \texttt {number } inside \texttt {if } scope)
261
+ \item Initialize \texttt {number } variable (e.g. \texttt {int number = 0; })
262
+ \end {itemize }
263
+
264
+ Important! This is the only one simple case of potential error in multiprocessing environment.
265
+ Pay attention to the safety because debugging in multiprocessing environment is more challenging that in single process environment.
266
+ \end {frame }
267
+
218
268
\begin {frame }[fragile]{\texttt {MPI\_ Send() }}
219
269
\texttt {int MPI\_ Send(const void *buf, int count, MPI\_ Datatype datatype, int dest, int tag, MPI\_ Comm comm) }
220
270
0 commit comments