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