Repository navigation
Expand file tree
/
Copy pathmemory_model.tex
More file actions
111 lines (93 loc) · 4.98 KB
/
Copy pathmemory_model.tex
File metadata and controls
111 lines (93 loc) · 4.98 KB
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
\section{Memory Consistency Model}
The \pencil memory model tells programmers what they can expect from an OpenCL
implementation; which memory operations are guaranteed to happen in which order
and which memory values each read operation will return.
The memory model also tells compiler writers which restrictions they must follow
when implementing compiler optimizations.
The memory consistency model in \pencil is inspired from the memory model of
the ISO C11~\ref{c11} and the OpenCL 2.0~\ref{opencl2} programming languages.
The main difference between the \pencil memory model and the C11/OpenCL 2.0
memory models is that \pencil has sequential semantics whereas C11 and OpenCL
2.0 do not.
In \pencil, two consistency memory models exist:
%TODO (ulysse): provide a correct definition.
\begin{itemize}
\item Relaxed consistency: assuming that two statements that access two
different memory locations a and b are marked to have relaxed consistency,
then the compiler is allowed do any reordering between these
two statements.
\item Sequential consistency: assuming that two statements that access two
different memory locations a and b are marked to have \eph{sequential
consistency}, if the code generated from \pencil is a parallel code
(i.e. the code is split to be executed by multiple threads), then:
- if the two statements are executed by the same thread,
no reordering of the two statements is possible.
- if the two statements are executed by different threads, then this
execution is required to appear as if one of the statements is executed
first and then the second statement is executed (interleaving
of execution).
\end{itemize}
\pencil defines the enumerated type \lstlisting!memory_order!
which defines these two memory models:
\lstinline!memory_order_relaxed! stands for the relaxed consistency memory
model and \lstinline!memory_order_seq_cst! stands for the sequential
consistency memory model.
\lstinline!enum memory_order {
memory_order_relaxed,
memory_order_seq_cst
};!
\subsection{Atomic Operations and Memory Fences}
There are two types of synchronization operations: \emph{atomic operations} and
\emph{fences}.
\subsubsection{Atomic Operations}
Atomic operations are indivisible. They either occure completely or not at all.
These operations are used to order memory operations between units of execution
and hence they are parameterized by the \pencil memory consistency model
(they can follow either the relaxed consistency model or the seuential
consistency model).
The atomic operations for \pencil are similar to the corresponding operations
defined by the C11 standard~\ref{c11}.
The \pencil atomic operations apply to variables of an atomic type.
The following C11 atomic builtins are defined in \pencil:
atomic_init, atomic_store, atomic_store_explicit, atomic_load,
atomic_load_explicit, atomic_exchange, atomic_exchange_explicit,
atomic_compare_exchange_strong, atomic_compare_exchange_strong_explicit,
atomic_compare_exchange_weak, atomic_compare_exchange_weak_explicit,
atomic_fetch_key, atomic_fetch_key_explicit (where key can be either
add, sub, or, xor, and, min, max),
atomic_flag_test_and_set, atomic_flag_test_and_set_explicit,
atomic_flag_clear, atomic_flag_clear_explicit.
They have exactly the same semantics as their equivalent C11 functions.
As \pencil does not support function overloading (to be consistent
with C99), every one of the atomic builtin functions has multiple equivalent
functions in \pencil with prefixes for the different argument types.
For example, the \emph{atomic_load} function has the following equivalent
\pencil functions
\begin{itemize}
\item \emph{atomic_load}: used for \emph{atomic_int} arguments;
\item \emph{satomic_load}: used for \emph{atomic_short} arguments;
\item \emph{latomic_load}: used for \emph{atomic_long} arguments;
\item \emph{llatomic_load}: used for \emph{atomic_llong} arguments;
\item \emph{oatomic_load}: used for \emph{atomic_bool} arguments;
\item \emph{tatomic_load}: used for \emph{atomic_size_t} arguments;
\item unsigned versions have a \emph{u} prefix
(\emph{usatomic_load}, \emph{uatomic_load}, \emph{ulatomic_load},
\emph{ullatomic_load}).
\end{itemize}
The other atomic functions defined in the C11 standard are not defined in
\pencil as they do not have an equivalent in OpenCL.
\subsubsection{Memory Fences}
A memory fence is an atomic operation that is not associated to a memory
location.
\lstinline!void pencil_fence();!
%TODO (ulysse): Explain the semantics of a fence.
%TODO (ulysse): Provide an example.
\subsection{Atomic Types and Initialization}
\pencil supports the following atomic types defined by C11 (since these are
also supported by OpenCL 2.0):
atomic_flag, atomic_int, atomic_uint, atomic_long, atomic_ulong, atomic_llong,
atomic_ullong, and atomic_size_t.
The C11 ATOMIC_FLAG_INIT macro is also defined in \pencil.
%TODO (ulysse): envoyer ce fichier sur pencil-discussion [AT] inria.fr pour que
% les autres donnent leurs avis et faire des petites modifications s'ils
% proposent des modifications.