From 12d5da0f4e01d469995d479270da887501450488 Mon Sep 17 00:00:00 2001 From: Felix Spanier Date: Thu, 26 Jan 2023 13:39:13 +0100 Subject: [PATCH] NumMeth-22: MPI I/O --- nummeth-ws22/skript.tex | 151 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) diff --git a/nummeth-ws22/skript.tex b/nummeth-ws22/skript.tex index fb31c12..b56fd2d 100644 --- a/nummeth-ws22/skript.tex +++ b/nummeth-ws22/skript.tex @@ -8001,6 +8001,157 @@ passing \item non-blocking operations \end{itemize} +\subsubsection{A basic example} + +Just like POSIX I/O, you need to +\begin{itemize} +\item Open the file +\item Read or Write data to the file +\item Close the file +In MPI, these steps are almost the +same: +\begin{itemize} +\item Open the file: \lstinline{MPI_File_open} +\item Write to the file: \lstinline{MPI_File_write} +\item Close the file: \lstinline{MPI_File_close} +\end{itemize} + +\begin{lstlisting}[language=C] +#include +#include "mpi.h" +int main(int argc, char *argv[]) +{ + MPI_File fh; + int buf[1000], rank; + MPI_Init(0,0); + MPI_Comm_rank(MPI_COMM_WORLD, &rank); + MPI_File_open(MPI_COMM_WORLD, "test.out", + MPI_MODE_CREATE|MPI_MODE_WRONLY, + MPI_INFO_NULL, &fh); + if (rank == 0) + MPI_File_write(fh, buf, 1000, MPI_INT, MPI_STATUS_IGNORE); + MPI_File_close(&fh); + MPI_Finalize(); + return 0; +} +\end{lstlisting} + +\subsubsection{File access} + +File Open is collective over the communicator +\begin{itemize} +\item Will be used to support collective I/O, which we will +see is important for performance +\item Modes similar to Unix open +\item \lstinline{MPI_Info} provides additional hints for performance +\end{itemize} +File Write is independent (hence the test on +rank), while file close is collective; similar in style to +\lstinline{MPI_Comm_free}. + +Use \lstinline{MPI_File_write} or \lstinline{MPI_File_write_at} +Similar to POSIX calls \lstinline{MPI_MODE_WRONLY} or \lstinline{MPI_MODE_RDWR} +can be given as the flags to \lstinline{MPI_File_open}. If the file doesn’t exist previously, the flag \lstinline{MPI_MODE_CREATE} must also be passed to +\lstinline{MPI_File_open}. We can pass multiple flags by using +bitwise-or ‘|’ in C, or addition ‘+” in Fortran. + +\subsubsection{Ways to Access a Shared File} + +UNIX-like +\begin{itemize} +\item \lstinline{MPI_File_seek} +\item \lstinline{MPI_File_read} +\item \lstinline{MPI_File_write} +\end{itemize} +Combine seek and I/O for thread safety +\begin{itemize} +\item \lstinline{MPI_File_read_at} +\item \lstinline{MPI_File_write_at} +\end{itemize} +use shared file pointer +\begin{itemize} +\item \lstinline{MPI_File_read_shared} +\item \lstinline{MPI_File_write_shared} +\end{itemize} + +\begin{lstlisting}[language=C] +#include "mpi.h" +MPI_Status status; +MPI_File fh; +MPI_Offset offset; +MPI_File_open(MPI_COMM_WORLD, "/pfs/datafile", + MPI_MODE_RDONLY, MPI_INFO_NULL, &fh) +nints = FILESIZE / (nprocs*INTSIZE); +offset = rank * nints * INTSIZE; +MPI_File_read_at(fh, offset, buf, nints, MPI_INT, + &status); +MPI_Get_count(&status, MPI_INT, &count); +printf("process %d read %d ints\n", rank, count); + +MPI_File_close(&fh); +\end{lstlisting} + +Is there any use for independent I/O? Sometimes the synchronization of +collective calls is not natural, sometimes the overhead of collective calls outweighs their +benefits. Example: very small I/O during header reads. + +Noncontiguous accesses are cmmon in parallel applications. Example: distributed arrays stored in files. A big advantage of MPI I/O over Unix I/O is the ability to specify noncontiguous accesses in memory and file within a single function call +by using derived datatypes. POSIX only supports non-contiguous in file, and only +with IOVs. This allows implementation to optimize the access. Collective I/O combined with noncontiguous accesses yields the highest performance + +File views are specified by a triplet (displacement, etype, and filetype) passed to +\lstinline{MPI_File_set_view} +\begin{itemize} +\item \lstinline{displacement} = number of bytes to be +skipped from the start of the file, e.g., to skip a file header +\item \lstinline{etype} = basic unit of data access (can be +any basic or derived datatype) +\item \lstinline{filetype }= specifies which portion of the +file is visible to the process +\end{itemize} + +\begin{lstlisting}[language=C] +MPI_Aint lb, extent; +MPI_Datatype etype, filetype, contig; +MPI_Offset disp; +MPI_Type_contiguous(2, MPI_INT, &contig); +lb = 0; extent = 6 * sizeof(int); +MPI_Type_create_resized(contig, lb, extent, &filetype); +MPI_Type_commit(&filetype); +disp = 5 * sizeof(int); etype = MPI_INT; +MPI_File_open(MPI_COMM_WORLD, "/pfs/datafile", +MPI_MODE_CREATE | MPI_MODE_RDWR, MPI_INFO_NULL, &fh); +MPI_File_set_view(fh, disp, etype, filetype, "native", +MPI_INFO_NULL); +MPI_File_write(fh, buf, 1000, MPI_INT, MPI_STATUS_IGNORE); +\end{lstlisting} + +\subsubsection{Collective I/O and MPI} + +The collective I/O is a critical optimization in parallel I/O. All processes (in the communicator) must call the collective I/O function. This allows communication of “big picture” to file system, i.e., the framework for I/O optimizations at the MPI-IO layer. The basic idea is: build large blocks, so that reads/writes in I/O system will be large. +Requests from different processes may be merged together, which is particularly effective when the accesses of different processes are noncontiguous and interleaved. + +Functions in collective I/O: +\begin{itemize} +\item \lstinline{MPI_File_write_at_all}, etc.\\ +\lstinline{_all} indicates that all processes in the group +specified by the communicator passed to \lstinline{MPI_File_open} will call this function\\ +\lstinline{_at} indicates that the position in the file is specified as part of the call; this provides thread-safety and clearer code than using a separate “seek” call. Each process specifies only its own access information — the argument list is the same as for the non-collective functions +\item \lstinline{MPI_File_seek} like UNIX +\item \lstinline{MPI_File_read_all} like UNIX +\item \lstinline{MPI_File_write_all} like UNIX +\item \lstinline{MPI_File_read_at_all} combine seek and I/O for thread safety +\item \lstinline{MPI_File_write_at_all} combine seek and I/O for thread safety +\item \lstinline{MPI_File_read_ordered} use shared file pointer +\item \lstinline{MPI_File_write_ordered} use shared file pointer +\end{itemize} + +\subsubsection{Noncontiguous I/O in File} +Each process describes the part of the file for which it is responsible. This is the “file view”, which is described in MPI with an offset (useful for +headers) and an \lstinline{MPI_Datatype}. Only the part of the file described by the +file view is visible to the process; reads and writes access these locations +This provides an efficient way to perform +noncontiguous accesses \section{Using the local cluster}