forked from petsc/petsc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpetscdevice.h
344 lines (265 loc) · 15.6 KB
/
petscdevice.h
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
333
334
335
336
337
338
339
340
341
342
343
344
#pragma once
#include <petscdevicetypes.h>
#include <petscviewertypes.h>
#if PETSC_CPP_VERSION >= 11 // C++11
#define PETSC_DEVICE_ALIGNOF(...) alignof(decltype(__VA_ARGS__))
#elif PETSC_C_VERSION >= 11 // C11
#ifdef __GNUC__
#define PETSC_DEVICE_ALIGNOF(...) _Alignof(__typeof__(__VA_ARGS__))
#else
#include <stddef.h> // max_align_t
// Note we cannot just do _Alignof(expression) since clang warns that "'_Alignof' applied to an
// expression is a GNU extension", so we just default to max_align_t which is ultra safe
#define PETSC_DEVICE_ALIGNOF(...) _Alignof(max_align_t)
#endif // __GNUC__
#else
#define PETSC_DEVICE_ALIGNOF(...) PETSC_MEMALIGN
#endif
/* SUBMANSEC = Sys */
// REVIEW ME: this should probably go somewhere better, configure-time?
#define PETSC_HAVE_HOST 1
/* logging support */
PETSC_EXTERN PetscClassId PETSC_DEVICE_CLASSID;
PETSC_EXTERN PetscClassId PETSC_DEVICE_CONTEXT_CLASSID;
PETSC_EXTERN PetscErrorCode PetscDeviceInitializePackage(void);
PETSC_EXTERN PetscErrorCode PetscDeviceFinalizePackage(void);
PETSC_EXTERN PetscErrorCode PetscGetMemType(const void *, PetscMemType *);
/* PetscDevice */
#if PetscDefined(HAVE_CXX)
PETSC_EXTERN PetscErrorCode PetscDeviceCreate(PetscDeviceType, PetscInt, PetscDevice *);
PETSC_EXTERN PetscErrorCode PetscDeviceDestroy(PetscDevice *);
PETSC_EXTERN PetscErrorCode PetscDeviceConfigure(PetscDevice);
PETSC_EXTERN PetscErrorCode PetscDeviceView(PetscDevice, PetscViewer);
PETSC_EXTERN PetscErrorCode PetscDeviceGetType(PetscDevice, PetscDeviceType *);
PETSC_EXTERN PetscErrorCode PetscDeviceGetDeviceId(PetscDevice, PetscInt *);
PETSC_EXTERN PetscDeviceType PETSC_DEVICE_DEFAULT(void);
PETSC_EXTERN PetscErrorCode PetscDeviceSetDefaultDeviceType(PetscDeviceType);
PETSC_EXTERN PetscErrorCode PetscDeviceInitialize(PetscDeviceType);
PETSC_EXTERN PetscBool PetscDeviceInitialized(PetscDeviceType);
#else
#define PetscDeviceCreate(PetscDeviceType, PetscInt, dev) (*(dev) = PETSC_NULLPTR, PETSC_SUCCESS)
#define PetscDeviceDestroy(dev) (*(dev) = PETSC_NULLPTR, PETSC_SUCCESS)
#define PetscDeviceConfigure(PetscDevice) PETSC_SUCCESS
#define PetscDeviceView(PetscDevice, PetscViewer) PETSC_SUCCESS
#define PetscDeviceGetType(PetscDevice, type) (*(type) = PETSC_DEVICE_DEFAULT(), PETSC_SUCCESS)
#define PetscDeviceGetDeviceId(PetscDevice, id) (*(id) = 0, PETSC_SUCCESS)
#define PETSC_DEVICE_DEFAULT() PETSC_DEVICE_HOST
#define PetscDeviceSetDefaultDeviceType(PetscDeviceType) PETSC_SUCCESS
#define PetscDeviceInitialize(PetscDeviceType) PETSC_SUCCESS
#define PetscDeviceInitialized(dtype) ((dtype) == PETSC_DEVICE_HOST)
#endif /* PetscDefined(HAVE_CXX) */
/* PetscDeviceContext */
#if PetscDefined(HAVE_CXX)
PETSC_EXTERN PetscErrorCode PetscDeviceContextCreate(PetscDeviceContext *);
PETSC_EXTERN PetscErrorCode PetscDeviceContextDestroy(PetscDeviceContext *);
PETSC_EXTERN PetscErrorCode PetscDeviceContextSetStreamType(PetscDeviceContext, PetscStreamType);
PETSC_EXTERN PetscErrorCode PetscDeviceContextGetStreamType(PetscDeviceContext, PetscStreamType *);
PETSC_EXTERN PetscErrorCode PetscDeviceContextSetDevice(PetscDeviceContext, PetscDevice);
PETSC_EXTERN PetscErrorCode PetscDeviceContextGetDevice(PetscDeviceContext, PetscDevice *);
PETSC_EXTERN PetscErrorCode PetscDeviceContextGetDeviceType(PetscDeviceContext, PetscDeviceType *);
PETSC_EXTERN PetscErrorCode PetscDeviceContextSetUp(PetscDeviceContext);
PETSC_EXTERN PetscErrorCode PetscDeviceContextDuplicate(PetscDeviceContext, PetscDeviceContext *);
PETSC_EXTERN PetscErrorCode PetscDeviceContextQueryIdle(PetscDeviceContext, PetscBool *);
PETSC_EXTERN PetscErrorCode PetscDeviceContextWaitForContext(PetscDeviceContext, PetscDeviceContext);
PETSC_EXTERN PetscErrorCode PetscDeviceContextForkWithStreamType(PetscDeviceContext, PetscStreamType, PetscInt, PetscDeviceContext **);
PETSC_EXTERN PetscErrorCode PetscDeviceContextFork(PetscDeviceContext, PetscInt, PetscDeviceContext **);
PETSC_EXTERN PetscErrorCode PetscDeviceContextJoin(PetscDeviceContext, PetscInt, PetscDeviceContextJoinMode, PetscDeviceContext **);
PETSC_EXTERN PetscErrorCode PetscDeviceContextSynchronize(PetscDeviceContext);
PETSC_EXTERN PetscErrorCode PetscDeviceContextSetFromOptions(MPI_Comm, PetscDeviceContext);
PETSC_EXTERN PetscErrorCode PetscDeviceContextView(PetscDeviceContext, PetscViewer);
PETSC_EXTERN PetscErrorCode PetscDeviceContextViewFromOptions(PetscDeviceContext, PetscObject, const char name[]);
PETSC_EXTERN PetscErrorCode PetscDeviceContextGetCurrentContext(PetscDeviceContext *);
PETSC_EXTERN PetscErrorCode PetscDeviceContextSetCurrentContext(PetscDeviceContext);
PETSC_EXTERN PetscErrorCode PetscDeviceContextGetStreamHandle(PetscDeviceContext, void **);
#else
#define PetscDeviceContextCreate(dctx) (*(dctx) = PETSC_NULLPTR, PETSC_SUCCESS)
#define PetscDeviceContextDestroy(dctx) (*(dctx) = PETSC_NULLPTR, PETSC_SUCCESS)
#define PetscDeviceContextSetStreamType(PetscDeviceContext, PetscStreamType) PETSC_SUCCESS
#define PetscDeviceContextGetStreamType(PetscDeviceContext, type) (*(type) = PETSC_STREAM_GLOBAL_BLOCKING, PETSC_SUCCESS)
#define PetscDeviceContextSetDevice(PetscDeviceContext, PetscDevice) PETSC_SUCCESS
#define PetscDeviceContextGetDevice(PetscDeviceContext, device) (*(device) = PETSC_NULLPTR, PETSC_SUCCESS)
#define PetscDeviceContextGetDeviceType(PetscDeviceContext, type) (*(type) = PETSC_DEVICE_DEFAULT(), PETSC_SUCCESS)
#define PetscDeviceContextSetUp(PetscDeviceContext) PETSC_SUCCESS
#define PetscDeviceContextDuplicate(PetscDeviceContextl, PetscDeviceContextr) (*(PetscDeviceContextr) = PETSC_NULLPTR, PETSC_SUCCESS)
#define PetscDeviceContextQueryIdle(PetscDeviceContext, idle) (*(idle) = PETSC_TRUE, PETSC_SUCCESS)
#define PetscDeviceContextWaitForContext(PetscDeviceContextl, PetscDeviceContextr) PETSC_SUCCESS
#define PetscDeviceContextForkWithStreamType(PetscDeviceContextp, PetscStreamType, PetscInt, PetscDeviceContextc) (*(PetscDeviceContextc) = PETSC_NULLPTR, PETSC_SUCCESS)
#define PetscDeviceContextFork(PetscDeviceContextp, PetscInt, PetscDeviceContextc) (*(PetscDeviceContextc) = PETSC_NULLPTR, PETSC_SUCCESS)
#define PetscDeviceContextJoin(PetscDeviceContextp, PetscInt, PetscDeviceContextJoinMode, PetscDeviceContextc) (*(PetscDeviceContextc) = PETSC_NULLPTR, PETSC_SUCCESS)
#define PetscDeviceContextSynchronize(PetscDeviceContext) PETSC_SUCCESS
#define PetscDeviceContextSetFromOptions(MPI_Comm, PetscDeviceContext) PETSC_SUCCESS
#define PetscDeviceContextView(PetscDeviceContext, PetscViewer) PETSC_SUCCESS
#define PetscDeviceContextViewFromOptions(PetscDeviceContext, PetscObject, PetscViewer) PETSC_SUCCESS
#define PetscDeviceContextGetCurrentContext(dctx) (*(dctx) = PETSC_NULLPTR, PETSC_SUCCESS)
#define PetscDeviceContextSetCurrentContext(PetscDeviceContext) PETSC_SUCCESS
#define PetscDeviceContextGetStreamHandle(PetscDeviceContext, handle) (*(handle) = PETSC_NULLPTR, PETSC_SUCCESS)
#endif /* PetscDefined(HAVE_CXX) */
/* memory */
#if PetscDefined(HAVE_CXX)
PETSC_EXTERN PetscErrorCode PetscDeviceAllocate_Private(PetscDeviceContext, PetscBool, PetscMemType, size_t, size_t, void **PETSC_RESTRICT);
PETSC_EXTERN PetscErrorCode PetscDeviceDeallocate_Private(PetscDeviceContext, void *PETSC_RESTRICT);
PETSC_EXTERN PetscErrorCode PetscDeviceMemcpy(PetscDeviceContext, void *PETSC_RESTRICT, const void *PETSC_RESTRICT, size_t);
PETSC_EXTERN PetscErrorCode PetscDeviceMemset(PetscDeviceContext, void *PETSC_RESTRICT, PetscInt, size_t);
#else
#include <string.h> // memset()
#define PetscDeviceAllocate_Private(PetscDeviceContext, clear, PetscMemType, size, alignment, ptr) PetscMallocA(1, (clear), __LINE__, PETSC_FUNCTION_NAME, __FILE__, (size), (ptr))
#define PetscDeviceDeallocate_Private(PetscDeviceContext, ptr) PetscFree((ptr))
#define PetscDeviceMemcpy(PetscDeviceContext, dest, src, size) PetscMemcpy((dest), (src), (size))
#define PetscDeviceMemset(PetscDeviceContext, ptr, v, size) ((void)memset((ptr), (unsigned char)(v), (size)), PETSC_SUCCESS)
#endif /* PetscDefined(HAVE_CXX) */
/*MC
PetscDeviceMalloc - Allocate device-aware memory
Synopsis:
#include <petscdevice.h>
PetscErrorCode PetscDeviceMalloc(PetscDeviceContext dctx, PetscMemType mtype, size_t n, Type **ptr)
Not Collective, Asynchronous, Auto-dependency aware
Input Parameters:
+ dctx - The `PetscDeviceContext` used to allocate the memory
. mtype - The type of memory to allocate
- n - The amount (in elements) to allocate
Output Parameter:
. ptr - The pointer to store the result in
Level: beginner
Notes:
Memory allocated with this function must be freed with `PetscDeviceFree()`.
If `n` is zero, then `ptr` is set to `PETSC_NULLPTR`.
This routine falls back to using `PetscMalloc1()` if PETSc was not configured with device
support. The user should note that `mtype` is ignored in this case, as `PetscMalloc1()`
allocates only host memory.
This routine uses the `sizeof()` of the memory type requested to determine the total memory
to be allocated, therefore you should not multiply the number of elements requested by the
`sizeof()` the type\:
.vb
PetscInt *arr;
// correct
PetscDeviceMalloc(dctx,PETSC_MEMTYPE_DEVICE,n,&arr);
// incorrect
PetscDeviceMalloc(dctx,PETSC_MEMTYPE_DEVICE,n*sizeof(*arr),&arr);
.ve
Note result stored `ptr` is immediately valid and the user may freely inspect or manipulate
its value on function return, i.e.\:
.vb
PetscInt *ptr;
PetscDeviceMalloc(dctx, PETSC_MEMTYPE_DEVICE, 20, &ptr);
PetscInt *sub_ptr = ptr + 10; // OK, no need to synchronize
ptr[0] = 10; // ERROR, directly accessing contents of ptr is undefined until synchronization
.ve
DAG representation:
.vb
time ->
-> dctx - |= CALL =| -\- dctx -->
\- ptr ->
.ve
.N ASYNC_API
.seealso: `PetscDeviceFree()`, `PetscDeviceCalloc()`, `PetscDeviceArrayCopy()`,
`PetscDeviceArrayZero()`
M*/
#define PetscDeviceMalloc(dctx, mtype, n, ptr) PetscDeviceAllocate_Private((dctx), PETSC_FALSE, (mtype), (size_t)(n) * sizeof(**(ptr)), PETSC_DEVICE_ALIGNOF(**(ptr)), (void **)(ptr))
/*MC
PetscDeviceCalloc - Allocate zeroed device-aware memory
Synopsis:
#include <petscdevice.h>
PetscErrorCode PetscDeviceCalloc(PetscDeviceContext dctx, PetscMemType mtype, size_t n, Type **ptr)
Not Collective, Asynchronous, Auto-dependency aware
Input Parameters:
+ dctx - The `PetscDeviceContext` used to allocate the memory
. mtype - The type of memory to allocate
- n - The amount (in elements) to allocate
Output Parameter:
. ptr - The pointer to store the result in
Level: beginner
Notes:
Has identical usage to `PetscDeviceMalloc()` except that the memory is zeroed before it is
returned. See `PetscDeviceMalloc()` for further discussion.
This routine falls back to using `PetscCalloc1()` if PETSc was not configured with device
support. The user should note that `mtype` is ignored in this case, as `PetscCalloc1()`
allocates only host memory.
.N ASYNC_API
.seealso: `PetscDeviceFree()`, `PetscDeviceMalloc()`, `PetscDeviceArrayCopy()`,
`PetscDeviceArrayZero()`
M*/
#define PetscDeviceCalloc(dctx, mtype, n, ptr) PetscDeviceAllocate_Private((dctx), PETSC_TRUE, (mtype), (size_t)(n) * sizeof(**(ptr)), PETSC_DEVICE_ALIGNOF(**(ptr)), (void **)(ptr))
/*MC
PetscDeviceFree - Free device-aware memory obtained with `PetscDeviceMalloc()` or `PetscDeviceCalloc()`
Synopsis:
#include <petscdevice.h>
PetscErrorCode PetscDeviceFree(PetscDeviceContext dctx, void *ptr)
Not Collective, Asynchronous, Auto-dependency aware
Input Parameters:
+ dctx - The `PetscDeviceContext` used to free the memory
- ptr - The pointer to free, may be `NULL`
Level: beginner
Notes:
`ptr` is set to `PETSC_NULLPTR` on successful deallocation.
`ptr` must have been allocated using `PetscDeviceMalloc()`, `PetscDeviceCalloc()` not `PetscMalloc()` or related routines
This routine falls back to using `PetscFree()` if PETSc was not configured with device
support. The user should note that `PetscFree()` frees only host memory.
DAG representation:
.vb
time ->
-> dctx -/- |= CALL =| - dctx ->
-> ptr -/
.ve
.N ASYNC_API
.seealso: `PetscDeviceMalloc()`, `PetscDeviceCalloc()`
M*/
#define PetscDeviceFree(dctx, ptr) ((PetscErrorCode)(PetscDeviceDeallocate_Private((dctx), (ptr)) || ((ptr) = PETSC_NULLPTR, PETSC_SUCCESS)))
/*MC
PetscDeviceArrayCopy - Copy memory in a device-aware manner
Synopsis:
#include <petscdevice.h>
PetscErrorCode PetscDeviceArrayCopy(PetscDeviceContext dctx, void *dest, const void *src, size_t n)
Not Collective, Asynchronous, Auto-dependency aware
Input Parameters:
+ dctx - The `PetscDeviceContext` used to copy the memory
. dest - The pointer to copy to
. src - The pointer to copy from
- n - The amount (in elements) to copy
Notes:
Both `dest` and `src` must have been allocated using `PetscDeviceMalloc()` or
`PetscDeviceCalloc()`.
This uses the `sizeof()` of the `src` memory type requested to determine the total memory to
be copied, therefore you should not multiply the number of elements by the `sizeof()` the
type\:
.vb
PetscInt *to,*from;
// correct
PetscDeviceArrayCopy(dctx,to,from,n);
// incorrect
PetscDeviceArrayCopy(dctx,to,from,n*sizeof(*from));
.ve
See `PetscDeviceMemcpy()` for further discussion.
Level: beginner
.N ASYNC_API
.seealso: `PetscDeviceMalloc()`, `PetscDeviceCalloc()`, `PetscDeviceFree()`,
`PetscDeviceArrayZero()`, `PetscDeviceMemcpy()`
M*/
#define PetscDeviceArrayCopy(dctx, dest, src, n) PetscDeviceMemcpy((dctx), (dest), (src), (size_t)(n) * sizeof(*(src)))
/*MC
PetscDeviceArrayZero - Zero memory in a device-aware manner
Synopsis:
#include <petscdevice.h>
PetscErrorCode PetscDeviceArrayZero(PetscDeviceContext dctx, void *ptr, size_t n)
Not Collective, Asynchronous, Auto-dependency aware
Input Parameters:
+ dctx - The `PetscDeviceContext` used to zero the memory
. ptr - The pointer to the memory
- n - The amount (in elements) to zero
Level: beginner
Notes:
`ptr` must have been allocated using `PetscDeviceMalloc()` or `PetscDeviceCalloc()`.
This uses the `sizeof()` of the memory type requested to determine the total memory to be
zeroed, therefore you should not multiply the number of elements by the `sizeof()` the type\:
.vb
PetscInt *ptr;
// correct
PetscDeviceArrayZero(dctx,ptr,n);
// incorrect
PetscDeviceArrayZero(dctx,ptr,n*sizeof(*ptr));
.ve
See `PetscDeviceMemset()` for further discussion.
.N ASYNC_API
.seealso: `PetscDeviceMalloc()`, `PetscDeviceCalloc()`, `PetscDeviceFree()`,
`PetscDeviceArrayCopy()`, `PetscDeviceMemset()`
M*/
#define PetscDeviceArrayZero(dctx, ptr, n) PetscDeviceMemset((dctx), (ptr), 0, (size_t)(n) * sizeof(*(ptr)))