forked from mpi-forum/mem-alloc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmem_alloc.tex
332 lines (269 loc) · 10.7 KB
/
mem_alloc.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
\renewcommand{\Color}[1]{}
\newenvironment{etabular}
{\edtable{tabular}}
{\endedtable}
\newcommand{\semanticstwo}[2]
{
\Color{green}
\begin{etabular}{l|l}
\hspace*{0.1cm} & #1 \\
& #2\\
\end{etabular}
\vspace*{0.2cm}
\Color{black}
}
\newcommand{\semanticsthree}[3]
{
\Color{green}
\begin{etabular}{l|l}
\hspace*{0.1cm} & #1 \\
& #2\\
& #3\\
\end{etabular}
\vspace*{0.2cm}
\Color{black}
}
\newcommand{\semanticsfour}[4]
{
\Color{green}
\begin{etabular}{l|l}
\hspace*{0.1cm} & #1 \\
& #2\\
& #3\\
& #4\\
\end{etabular}
\vspace*{0.2cm}
\Color{black}
}
\chapter{Overview}
\label{chap:overview}
Modern computing systems contain a variety of memory types, each
closely associated with a distinct type of computing hardware. For
example, compute accelerators such as GPUs typically feature their
own memory that is distinct from the memory attached to the host
processor. Additionally, GPUs from different vendors also differ
in their memory types. The differences in memory types influence
feature availability and performance behavior of an application
running on such modern systems. Hence, \mpi/ libraries need to be
aware of and support additional memory types. For a given type of
memory, \mpi/ libraries need to know the associated memory allocator and
the limitations on memory access. The different memory kinds capture
the differentiating information needed by \mpi/ libraries for different
memory types.
This \mpi/ side document defines the memory allocation kinds and their
associated restrictors that users can use to query the support for
different memory kinds provided by the \mpi/ library. These definitions
supplement those found in section 11.4.3 of the \mpi/ standard, which
also explains their usage model.
\chapter{Definitions}
\label{chap:definitions}
This section contains definitions of memory allocation kinds and
their restrictors.
\section{Kind: cuda}
The \infokey{cuda} memory kind refers to the memory allocated by the
CUDA runtime system~\cite{cudaref}. \exref{example:alloc-kind-spm-cuda}
showcases usage of some of the memory kinds defined in this section.
\subsubsection{Restrictors}
\begin{itemize}
\item \infokey{host}: Support for memory allocations on the host system
that are page-locked for direct access from the CUDA device (\eg,
memory allocations from the \function{cudaHostAlloc()} function).
These memory allocations are attributed with \function{cudaMemoryTypeHost}.
\item \infokey{device}: Support for memory allocated on a CUDA device
(\eg, memory allocations from the \function{cudaMalloc()} function).
These memory allocations are attributed with \function{cudaMemoryTypeDevice}.
\item \infokey{managed}: Support for memory that is managed by CUDA’s
Unified Memory system (\eg, memory allocations from the
\function{cudaMallocManaged()} function).
These memory allocations are attributed with \function{cudaMemoryTypeManaged}.
\end{itemize}
\section{Kind: rocm}
The \infokey{rocm} memory kind refers to the memory allocated by the ROCm
runtime system~\cite{rocmref}.
\subsubsection{Restrictors}
\begin{itemize}
\item \infokey{host}: Support for memory allocated on the host system that
is page-locked for direct access from the ROCm device (\eg, memory
allocations from the \function{hipHostMalloc()} function).
These memory allocations are attributed with \function{hipMemoryTypeHost}.
\item \infokey{device}: Support for memory allocated on the ROCm device
(\eg, memory allocations from the \function{hipMalloc()} function).
These memory allocations are attributed with \function{hipMemoryTypeDevice}.
\item \infokey{managed}: Support for memory that is managed automatically
by the ROCm runtime (\eg, memory allocations from the
\function{hipMallocManaged()} function).
These memory allocations are attributed with \function{hipMemoryTypeManaged}.
\end{itemize}
\section{Kind: levelzero}
The \infokey{levelzero} memory kind refers to the memory allocated by the
Level Zero runtime system~\cite{zeref}.
\subsubsection{Restrictors}
\begin{itemize}
\item \infokey{host}: Support for memory allocated on the host that is
accessible by Level Zero devices (\eg, memory allocations from the
\function{zeMemAllocHost()} function).
These memory allocations are attributed with \function{ZE\_MEMORY\_TYPE\_HOST}.
\item \infokey{device}: Support for memory allocated on a Level Zero device
(\eg, memory allocations from the \function{zeMemAllocDevice()} function).
These memory allocations are attributed with \function{ZE\_MEMORY\_TYPE\_DEVICE}.
\item \infokey{shared}: Support for memory allocated that will be shared
between the host and one or more Level Zero devices (\eg,
memory allocations from the \function{zeMemAllocShared()} function).
These memory allocations are attributed with \function{ZE\_MEMORY\_TYPE\_SHARED}.
\end{itemize}
\chapter{Examples}
\label{chap:examples}
This section includes examples demonstrating the usage of
memory kinds defined in \chapref{chap:definitions}.
\begin{example}
\label{example:alloc-kind-spm-cuda}
This CUDA example demonstrates the usage of the different
kinds to perform communication in a manner that is supported
by the underlying \mpi/ library.
%%HEADER
%%LANG: C
%%ENDHEADER
\begin{lstlisting}[language={[MPI]C}]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <mpi.h>
#include <cuda_runtime.h>
int main(int argc, char *argv[])
{
int cuda_device_aware = 0;
int cuda_managed_aware = 0;
int len = 0, flag = 0;
int *managed_buf = NULL;
int *device_buf = NULL, *system_buf = NULL;
int nranks = 0;
MPI_Info info;
MPI_Session session;
MPI_Group wgroup;
MPI_Comm system_comm;
MPI_Comm cuda_managed_comm = MPI_COMM_NULL;
MPI_Comm cuda_device_comm = MPI_COMM_NULL;
MPI_Info_create(&info);
MPI_Info_set(info, "mpi_memory_alloc_kinds",
"system,cuda:device,cuda:managed");
MPI_Session_init(info, MPI_ERRORS_ARE_FATAL, &session);
MPI_Info_free(&info);
MPI_Session_get_info(session, &info);
MPI_Info_get_string(info, "mpi_memory_alloc_kinds",
&len, NULL, &flag);
if (flag) {
char *val, *valptr, *kind;
val = valptr = (char *) malloc(len);
if (NULL == val) return 1;
MPI_Info_get_string(info, "mpi_memory_alloc_kinds",
&len, val, &flag);
while ((kind = strsep(&val, ",")) != NULL) {
if (strcasecmp(kind, "cuda:managed") == 0) {
cuda_managed_aware = 1;
}
else if (strcasecmp(kind, "cuda:device") == 0) {
cuda_device_aware = 1;
}
}
free(valptr);
}
MPI_Info_free(&info);
MPI_Group_from_session_pset(session, "mpi://WORLD" , &wgroup);
// Create a communicator for operations on system memory
MPI_Info_create(&info);
MPI_Info_set(info, "mpi_assert_memory_alloc_kinds", "system");
MPI_Comm_create_from_group(wgroup,
"org.mpi-side-doc.mem-kind.example.system",
info, MPI_ERRORS_ABORT, &system_comm);
MPI_Info_free(&info);
MPI_Comm_size(system_comm, &nranks);
/** Check for CUDA awareness **/
// Check if all processes have CUDA managed support
MPI_Allreduce(MPI_IN_PLACE, &cuda_managed_aware, 1, MPI_INT,
MPI_LAND, system_comm);
if (cuda_managed_aware) {
// Create a communicator for operations that use
// CUDA managed buffers.
MPI_Info_create(&info);
MPI_Info_set(info, "mpi_assert_memory_alloc_kinds",
"cuda:managed");
MPI_Comm_create_from_group(wgroup,
"org.mpi-side-doc.mem-kind.example.cuda.managed",
info, MPI_ERRORS_ABORT, &cuda_managed_comm);
MPI_Info_free(&info);
}
else {
// Check if all processes have CUDA device support
MPI_Allreduce(MPI_IN_PLACE, &cuda_device_aware, 1, MPI_INT,
MPI_LAND, system_comm);
if (cuda_device_aware) {
// Create a communicator for operations that use
// CUDA device buffers.
MPI_Info_create(&info);
MPI_Info_set(info, "mpi_assert_memory_alloc_kinds",
"cuda:device");
MPI_Comm_create_from_group(wgroup,
"org.mpi-side-doc.mem-kind.example.cuda.device",
info, MPI_ERRORS_ABORT, &cuda_device_comm);
MPI_Info_free(&info);
}
else {
printf("Warning: cuda alloc kind not supported\n");
}
}
MPI_Group_free(&wgroup);
/** Execute according to level of CUDA awareness **/
if (cuda_managed_aware) {
// Allocate managed buffer and initialize it
cudaMallocManaged(&managed_buf, sizeof(int));
*managed_buf = 1;
// Perform communication using cuda_managed_comm
// if it's available.
MPI_Allreduce(MPI_IN_PLACE, managed_buf, 1, MPI_INT,
MPI_SUM, cuda_managed_comm);
assert((*managed_buf) == nranks);
cudaFree(managed_buf);
}
else {
// Allocate system buffer and initialize it
system_buf = malloc(sizeof(int));
*system_buf = 1;
// Allocate CUDA device buffer and initialize it
cudaMalloc(&device_buf, sizeof(int));
cudaMemcpyAsync(device_buf, system_buf, sizeof(int),
cudaMemcpyHostToDevice);
cudaStreamSynchronize(0);
if (cuda_device_aware) {
// Perform communication using cuda_comm
// if it's available.
MPI_Allreduce(MPI_IN_PLACE, device_buf, 1, MPI_INT,
MPI_SUM, cuda_comm);
assert((*device_buf) == nranks);
}
else {
// Otherwise, copy data to a system buffer,
// use system_comm, and copy data back to device buffer
cudaMemcpyAsync(system_buf, device_buf, sizeof(int),
cudaMemcpyDeviceToHost);
cudaStreamSynchronize(0);
MPI_Allreduce(MPI_IN_PLACE, system_buf, 1, MPI_INT,
MPI_SUM, system_comm);
cudaMemcpyAsync(device_buf, system_buf, sizeof(int),
cudaMemcpyHostToDevice);
cudaStreamSynchronize(0);
assert((*system_buf) == nranks);
}
cudaFree(device_buf);
free(system_buf);
}
if (cuda_managed_comm != MPI_COMM_NULL)
MPI_Comm_disconnect(&cuda_managed_comm);
if (cuda_device_comm != MPI_COMM_NULL)
MPI_Comm_disconnect(&cuda_device_comm);
MPI_Comm_disconnect(&system_comm);
MPI_Session_finalize(&session);
return 0;
}
\end{lstlisting}
\end{example}