Newer
Older
\renewcommand{\FIGREP}{src/compilation_basics/figures}
\section{Compilation}
\label{sec:compilation}
\intersec{deneb}
\subsection{The big picture}
\label{sec:big-picture}
\frametitle{Compilation}
\framesubtitle{0100101110101001010...}
\item A computer only understands ON and OFF states (1 and 0)
\item It would be very inconvenient for us to code in binary
\item We therefore use different levels of abstraction (languages), e.g. C, C++, Fortran
\item We need a translator!
\begin{frame}
\frametitle{Compilation}
\framesubtitle{The four compilation steps}
\item Translation is made by a compiler in 4 steps
\begin{description}
\item[Preprocessing] Format source code to make it ready for compilation (remove comments, execute preprocessing directives such as \cmd{\#include}, etc.)
\item[Compiling] Translate the source code (C, C++, Fortran, etc) into assembly, a very basic CPU-dependent language
\item[Assembly] Translate the assembly into machine code and store it in object files
\item[Linking] Link all the object files into one executable
\end{description}
\item In practice, the first three steps are combined together and simply
called ``compiling''
\begin{frame}[t,fragile]
\frametitle{Compilation}
\framesubtitle{The four compilation steps (visually)}
\hspace{6cm}
\begin{minipage}{0.5\textwidth}
\begin{itemize}
\item<5> Note that in reality, everything is done transparently
\begin{bashcode}
$> gcc -c file_1.c
$> gcc -c file_2.c
$> gcc file_1.o file_2.o -lexample -o exec
\end{bashcode}%$
\end{itemize}
\end{minipage}
\onslide<1>\addimage[width=12cm]{\FIGREP/compilation_steps_0.pdf}{2cm}{1cm}
\onslide<2>\addimage[width=12cm]{\FIGREP/compilation_steps_1.pdf}{2cm}{1cm}
\onslide<3>\addimage[width=12cm]{\FIGREP/compilation_steps_2.pdf}{2cm}{1cm}
\onslide<4>\addimage[width=12cm]{\FIGREP/compilation_steps_3.pdf}{2cm}{1cm}
\onslide<5>\addimage[width=12cm]{\FIGREP/compilation_steps_4.pdf}{2cm}{1cm}
\subsection{Basics of compilation}
\label{sec:basics-compilation}
\begin{frame}
\frametitle{Compilation}
\framesubtitle{Let's start from the beginning}
\begin{itemize}
\item There are many compilers for different languages (here C, C++, and
Fortran)
\begin{itemize}
\item GNU (\cmd{gcc}, \cmd{g++}, \cmd{gfortran})
\item Clang/LLVM
\item Intel (\cmd{icc}, \cmd{icpc}, \cmd{ifort}) and Intel OneAPI (\cmd{icx}, \cmd{icpx}, \cmd{ifx})
\item IBM (\cmd{xlc}, \cmd{xlc++}, \cmd{xlf})
\item etc
\end{itemize}
\item Each vendor has specific options and usage
\end{itemize}
\vfill
\textbf{In the following, we will use GNU compilers, but everything can be
adapted to other vendors}
\end{frame}
\begin{frame}[fragile]
\frametitle{Compiling a single source file}
\framesubtitle{}
\begin{itemize}
\item In general, the command to compile a single source file is
\begin{bashcode}
<compiler> <compiling options> <source file> <linking options>
\end{bashcode}
\item For example
\begin{bashcode}
gcc -o my_exe code.c
\end{bashcode}
I want to compile the file \cmd{code.c} using the GNU C compiler and with
option \cmd{-o my_exe} (rename the executable to \cmd{my_exe} instead
of the default \cmd{a.out})
\subsection{Compilation options}
\label{sec:compilation-options}
\begin{frame}
\frametitle{Compilation}
\framesubtitle{Compilation options}
Many compilation options can be used to manipulate, optimize or debug such as:
\item "Manipulation": \cmd{-o}, \cmd{-c}, etc.
\item "Optimization": \cmd{-On}, \cmd{-fastmath}, etc.
\item "Debug": \cmd{-g}, \cmd{-Wall}, \cmd{-fcheck=all} etc.
\item Option summary for GNU: \url{https://gcc.gnu.org/onlinedocs/gcc/Invoking-GCC.html\#Invoking-GCC}
\end{itemize}
\end{frame}
\frametitle{Compilation}
\framesubtitle{Exercise simpleCompilation}
The following code performs a simple saxpy, \ie{} $\vec{z} = \alpha \vec{x} + \vec{y}$
\begin{itemize}
\item Load gcc module with \cmd{module load gcc}
\item Go to the directory \cmd{simpleCompilation}
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
\item Compile the code \cmd{saxpy.F90} with no options and execute it. What
happens?
\item Make the code compile with \cmd{-O0 -g -Werror -Wall -Wextra
-fcheck=all} and run it. What happens?
\item Try different compilation optimization options (\cmd{-O0} to
\cmd{-O3}) and compare the timing displayed from the corresponding
runs
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Discussion}
\framesubtitle{Exercise simpleCompilation}
\begin{itemize}
\item Compilers greatly help you to debug and optimize your code. Use them
properly!
\item We advise you to begin by disabling optimizations and activating warnings
\begin{itemize}
\item \cmd{-O0}: turn off most optimizations (default with GCC)
\item \cmd{-Wall -Wextra}: activate most of the warnings
\item \cmd{-Werror}: treat warnings as errors
\item \cmd{-fcheck=all}: enable all run-time checks
\end{itemize}
\item Some errors can be easily avoided using the proper flags!
\item Once Everything is correct, remove those options and try optimization
flags
\item \cmd{-O3} is not necessarily faster than \cmd{-O2}. Test it!
\end{itemize}
\end{frame}
\frametitle{Compilation}
\framesubtitle{Exercise compilationWith2Files}
\begin{itemize}
\item Go to the directory \cmd{compilationWith2Files}
\item The previous code has been split into two files: \cmd{main.F90} and \cmd{saxpy.F90}
\item Compile and execute the code. Hint, use the \cmd{-c} option.
\item Check the results compared to the \cmd{simpleCompilation} exercise
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Discussion}
\framesubtitle{Exercise compilationWith2Files}
\begin{itemize}
\item With multiple source files, one must compile them separately and link
them together at the end
\item The \cmd{-c} option compiles, but does not link
\item Remember to pass all the options to each file!
\item This quickly becomes cumbersome as the number of files increases. We
will see possible solutions later.
\end{itemize}
\end{frame}
\subsection{The preprocessor}
\label{sec:preprocessor}
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
\begin{frame}[t,fragile]
\frametitle{Preprocessor}
\framesubtitle{}
\begin{itemize}
\item The C preprocessor, \cmd{cpp}, is a tool that modifies the source code prior to
compilation
\item It treats all the lines starting with \cmd{\#}, \eg{}
\begin{bashcode}
#include <iostream>
\end{bashcode}
\item You can define variables, macros, etc.
\pause
\item It can also take variables from the compilation command, \eg{}
\begin{bashcode}
$> gcc -DMYVAR code.c
\end{bashcode}%$
The option \cmd{-D} will pass variable \cmd{MYVAR} to the preprocessor
\pause
\item By default, \cmd{gcc} calls the preprocessor before compiling
\item For \cmd{gfortran}, you have to ask it:
\begin{itemize}
\item Use capital file extensions, \eg{} \cmd{.F90}, \cmd{.F}, \cmd{.F03}, etc.
\item Add the \cmd{-cpp} option to the compiler
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}[t,fragile, exercise]
\frametitle{Preprocessor}
\framesubtitle{}
\end{frame}
%%% TeX-master: "../../SCM_slides"