xref: /petsc/include/petscdevicetypes.h (revision a4af0ceea8a251db97ee0dc5c0d52d4adf50264a)
1 #if !defined(PETSCDEVICETYPES_H)
2 #define PETSCDEVICETYPES_H
3 
4 /* for PETSC_HAVE_CUDA/HIP/KOKKOS etc */
5 #include <petscsys.h> /*I petscsys.h I*/
6 
7 /*E
8   PetscMemType - Memory type of a pointer
9 
10   Developer Note:
11   Encoding of the bitmask in binary: xxxxyyyz
12 
13 $ z = 0                - Host memory
14 $ z = 1                - Device memory
15 $ yyy = 000            - CUDA-related memory
16 $ yyy = 001            - HIP-related memory
17 $ xxxxyyy1 = 0000,0001 - CUDA memory
18 $ xxxxyyy1 = 0001,0001 - CUDA NVSHMEM memory
19 $ xxxxyyy1 = 0000,0011 - HIP memory
20 
21   Other types of memory, e.g., CUDA managed memory, can be added when needed.
22 
23   Level: beginner
24 
25 .seealso: VecGetArrayAndMemType(), PetscSFBcastWithMemTypeBegin(), PetscSFReduceWithMemTypeBegin()
26 E*/
27 typedef enum {
28   PETSC_MEMTYPE_HOST    = 0,
29   PETSC_MEMTYPE_DEVICE  = 0x01,
30   PETSC_MEMTYPE_CUDA    = 0x01,
31   PETSC_MEMTYPE_NVSHMEM = 0x11,
32   PETSC_MEMTYPE_HIP     = 0x03
33 } PetscMemType;
34 
35 #define PetscMemTypeHost(m)    (((m) & 0x1) == PETSC_MEMTYPE_HOST)
36 #define PetscMemTypeDevice(m)  (((m) & 0x1) == PETSC_MEMTYPE_DEVICE)
37 #define PetscMemTypeCUDA(m)    (((m) & 0xF) == PETSC_MEMTYPE_CUDA)
38 #define PetscMemTypeHIP(m)     (((m) & 0xF) == PETSC_MEMTYPE_HIP)
39 #define PetscMemTypeNVSHMEM(m) ((m) == PETSC_MEMTYPE_NVSHMEM)
40 
41 /*E
42   PetscOffloadMask - indicates which memory (CPU, GPU, or none) contains valid data
43 
44 $ PETSC_OFFLOAD_UNALLOCATED - no memory contains valid matrix entries; NEVER used for vectors
45 $ PETSC_OFFLOAD_GPU         - GPU has valid vector/matrix entries
46 $ PETSC_OFFLOAD_CPU         - CPU has valid vector/matrix entries
47 $ PETSC_OFFLOAD_BOTH        - Both GPU and CPU have valid vector/matrix entries and they match
48 $ PETSC_OFFLOAD_VECKOKKOS   - Reserved for Vec_Kokkos. The offload is managed by Kokkos, thus this flag is not used in Vec_Kokkos.
49 
50   Level: developer
51 E*/
52 typedef enum {
53   PETSC_OFFLOAD_UNALLOCATED = 0x0,
54   PETSC_OFFLOAD_CPU         = 0x1,
55   PETSC_OFFLOAD_GPU         = 0x2,
56   PETSC_OFFLOAD_BOTH        = 0x3,
57   PETSC_OFFLOAD_VECKOKKOS   = 0x100
58 } PetscOffloadMask;
59 
60 /*E
61   PetscDeviceInitType - Initialization strategy for PetscDevice
62 
63 $ PETSC_DEVICE_INIT_NONE  - PetscDevice is never initialized
64 $ PETSC_DEVICE_INIT_LAZY  - PetscDevice is initialized on demand
65 $ PETSC_DEVICE_INIT_EAGER - PetscDevice is initialized as soon as possible
66 
67   Notes:
68   PETSC_DEVICE_INIT_NONE implies that any initialization of PetscDevice is disallowed and
69   doing so results in an error. Useful to ensure that no accelerator is used in a program.
70 
71   Level: beginner
72 
73 .seealso: PetscDevice, PetscDeviceType, PetscDeviceInitialize(), PetscDeviceInitialized(), PetscDeviceCreate()
74 E*/
75 typedef enum {
76   PETSC_DEVICE_INIT_NONE,
77   PETSC_DEVICE_INIT_LAZY,
78   PETSC_DEVICE_INIT_EAGER
79 } PetscDeviceInitType;
80 PETSC_EXTERN const char *const PetscDeviceInitTypes[];
81 
82 /*E
83   PetscDeviceType - Kind of accelerator device backend
84 
85 $ PETSC_DEVICE_INVALID - Invalid type, do not use
86 $ PETSC_DEVICE_CUDA    - CUDA enabled GPU
87 $ PETSC_DEVICE_HIP     - ROCM/HIP enabled GPU
88 $ PETSC_DEVICE_DEFAULT - Automatically select backend based on availability
89 $ PETSC_DEVICE_MAX     - Always 1 greater than the largest valid PetscDeviceType, invalid type, do not use
90 
91   Notes:
92   PETSC_DEVICE_DEFAULT is selected in the following order: PETSC_DEVICE_HIP, PETSC_DEVICE_CUDA, PETSC_DEVICE_INVALID.
93 
94   Level: beginner
95 
96 .seealso: PetscDevice, PetscDeviceInitType, PetscDeviceCreate()
97 E*/
98 typedef enum {
99   PETSC_DEVICE_INVALID,
100   PETSC_DEVICE_CUDA,
101   PETSC_DEVICE_HIP,
102   PETSC_DEVICE_MAX
103 } PetscDeviceType;
104 PETSC_EXTERN const char *const PetscDeviceTypes[];
105 #if PetscDefined(HAVE_HIP)
106 #  define PETSC_DEVICE_DEFAULT PETSC_DEVICE_HIP
107 #elif PetscDefined(HAVE_CUDA)
108 #  define PETSC_DEVICE_DEFAULT PETSC_DEVICE_CUDA
109 #else
110 #  define PETSC_DEVICE_DEFAULT PETSC_DEVICE_INVALID
111 #endif
112 
113 /*S
114   PetscDevice - Handle to an accelerator "device" (usually a GPU)
115 
116   Notes:
117   This object is used to house configuration and state of a device, but does not offer any ability to interact with or
118   drive device computation. This functionality is facilitated instead by the PetscDeviceContext object.
119 
120   Level: beginner
121 
122 .seealso: PetscDeviceType, PetscDeviceInitType, PetscDeviceCreate(), PetscDeviceConfigure(), PetscDeviceDestroy(), PetscDeviceContext, PetscDeviceContextSetDevice(), PetscDeviceContextGetDevice()
123 S*/
124 typedef struct _n_PetscDevice *PetscDevice;
125 
126 /*E
127   PetscStreamType - Stream blocking mode, indicates how a stream implementation will interact with the default "NULL"
128   stream, which is usually blocking.
129 
130 $ PETSC_STREAM_GLOBAL_BLOCKING    - Alias for NULL stream. Any stream of this type will block the host for all other streams to finish work before starting its operations.
131 $ PETSC_STREAM_DEFAULT_BLOCKING   - Stream will act independent of other streams, but will still be blocked by actions on the NULL stream.
132 $ PETSC_STREAM_GLOBAL_NONBLOCKING - Stream is truly asynchronous, and is blocked by nothing, not even the NULL stream.
133 $ PETSC_STREAM_MAX                - Always 1 greater than the largest PetscStreamType, do not use
134 
135   Level: intermediate
136 
137 .seealso: PetscDeviceContextSetStreamType(), PetscDeviceContextGetStreamType()
138 E*/
139 typedef enum {
140   PETSC_STREAM_GLOBAL_BLOCKING,
141   PETSC_STREAM_DEFAULT_BLOCKING,
142   PETSC_STREAM_GLOBAL_NONBLOCKING,
143   PETSC_STREAM_MAX
144 } PetscStreamType;
145 PETSC_EXTERN const char *const PetscStreamTypes[];
146 
147 /*E
148   PetscDeviceContextJoinMode - Describes the type of join operation to perform in PetscDeviceContextJoin()
149 
150 $ PETSC_DEVICE_CONTEXT_DESTROY - Destroy all incoming sub-contexts after join.
151 $ PETSC_CONTEXT_JOIN_SYNC      - Synchronize incoming sub-contexts after join.
152 $ PETSC_CONTEXT_JOIN_NO_SYNC   - Do not synchronize incoming sub-contexts after join.
153 
154   Level: beginner
155 
156 .seealso: PetscDeviceContext, PetscDeviceContextFork(), PetscDeviceContextJoin()
157 E*/
158 typedef enum {
159   PETSC_DEVICE_CONTEXT_JOIN_DESTROY,
160   PETSC_DEVICE_CONTEXT_JOIN_SYNC,
161   PETSC_DEVICE_CONTEXT_JOIN_NO_SYNC
162 } PetscDeviceContextJoinMode;
163 PETSC_EXTERN const char *const PetscDeviceContextJoinModes[];
164 
165 /*S
166   PetscDeviceContext - Container to manage stream dependencies and the various solver handles for asynchronous device compute.
167 
168   Level: beginner
169 
170 .seealso: PetscDevice, PetscDeviceContextCreate(), PetscDeviceContextSetDevice(), PetscDeviceContextDestroy(),
171 PetscDeviceContextFork(), PetscDeviceContextJoin()
172 S*/
173 typedef struct _n_PetscDeviceContext *PetscDeviceContext;
174 #endif /* PETSCDEVICETYPES_H */
175