@@ -94,7 +94,8 @@ EXTERN int omp_get_device_num(void) {
94
94
EXTERN int omp_get_initial_device (void ) {
95
95
TIMESCOPE ();
96
96
OMPT_IF_BUILT (ReturnAddressSetterRAII RA (__builtin_return_address (0 )));
97
- int HostDevice = omp_get_num_devices ();
97
+ int NumDevices = omp_get_num_devices ();
98
+ int HostDevice = NumDevices == 0 ? -1 : NumDevices;
98
99
DP (" Call to omp_get_initial_device returning %d\n " , HostDevice);
99
100
return HostDevice;
100
101
}
@@ -196,6 +197,48 @@ EXTERN int omp_target_is_present(const void *Ptr, int DeviceNum) {
196
197
return Rc;
197
198
}
198
199
200
+ // / Check whether a pointer is accessible from a device.
201
+ // / the functionality is available in OpenMP 5.1 and later
202
+ // / OpenMP 5.1
203
+ // / omp_target_is_accessible checks whether a host pointer is accessible from a
204
+ // / device OpenMP 6.0 removes restriction on pointer, allowing any pointer
205
+ // / interpreted as a pointer in the address space of the given device.
206
+ EXTERN int omp_target_is_accessible (const void *Ptr, size_t Size,
207
+ int DeviceNum) {
208
+ TIMESCOPE ();
209
+ OMPT_IF_BUILT (ReturnAddressSetterRAII RA (__builtin_return_address (0 )));
210
+ DP (" Call to omp_target_is_accessible for device %d, address " DPxMOD
211
+ " , size %zu\n " ,
212
+ DeviceNum, DPxPTR (Ptr), Size);
213
+
214
+ if (!Ptr) {
215
+ DP (" Call to omp_target_is_accessible with NULL ptr returning false\n " );
216
+ return false ;
217
+ }
218
+
219
+ if (DeviceNum == omp_get_initial_device ()) {
220
+ DP (" Call to omp_target_is_accessible on host, returning true\n " );
221
+ return true ;
222
+ }
223
+
224
+ // the device number must refer to a valid device
225
+ auto DeviceOrErr = PM->getDevice (DeviceNum);
226
+ if (!DeviceOrErr)
227
+ FATAL_MESSAGE (DeviceNum, " %s" , toString (DeviceOrErr.takeError ()).c_str ());
228
+
229
+ // for OpenMP 5.1 the routine checks whether a host pointer is accessible from
230
+ // the device this requires for the device to support unified shared memory
231
+ if (DeviceOrErr->supportsUnifiedMemory ()) {
232
+ DP (" Device %d supports unified memory, returning true\n " , DeviceNum);
233
+ return true ;
234
+ }
235
+
236
+ // functionality to check whether a device pointer is accessible from a device
237
+ // (OpenMP 6.0) from the host might not be possible
238
+ DP (" Device %d does not support unified memory, returning false\n " , DeviceNum);
239
+ return false ;
240
+ }
241
+
199
242
EXTERN int omp_target_memcpy (void *Dst, const void *Src, size_t Length,
200
243
size_t DstOffset, size_t SrcOffset, int DstDevice,
201
244
int SrcDevice) {
0 commit comments