Skip to content

Commit e3a5812

Browse files
committed
[OpenMP] Adds omp_target_is_accessible routine
Adds omp_target_is_accessible routine. Refactors common code from omp_target_is_present to work for both routines.
1 parent 0b2ab11 commit e3a5812

File tree

5 files changed

+85
-21
lines changed

5 files changed

+85
-21
lines changed

offload/include/omptarget.h

+1
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ int omp_get_initial_device(void);
280280
void *omp_target_alloc(size_t Size, int DeviceNum);
281281
void omp_target_free(void *DevicePtr, int DeviceNum);
282282
int omp_target_is_present(const void *Ptr, int DeviceNum);
283+
int omp_target_is_accessible(const void *Ptr, size_t Size, int DeviceNum);
283284
int omp_target_memcpy(void *Dst, const void *Src, size_t Length,
284285
size_t DstOffset, size_t SrcOffset, int DstDevice,
285286
int SrcDevice);

offload/libomptarget/OpenMP/API.cpp

+15-21
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ EXTERN void ompx_dump_mapping_tables() {
3939
using namespace llvm::omp::target::ompt;
4040
#endif
4141

42+
int checkTargetAddressMapping(const void *Ptr, size_t Size, int DeviceNum, const char *Name);
43+
4244
void *targetAllocExplicit(size_t Size, int DeviceNum, int Kind,
4345
const char *Name);
4446
void targetFreeExplicit(void *DevicePtr, int DeviceNum, int Kind,
@@ -168,33 +170,25 @@ EXTERN int omp_target_is_present(const void *Ptr, int DeviceNum) {
168170
DP("Call to omp_target_is_present for device %d and address " DPxMOD "\n",
169171
DeviceNum, DPxPTR(Ptr));
170172

171-
if (!Ptr) {
172-
DP("Call to omp_target_is_present with NULL ptr, returning false\n");
173-
return false;
174-
}
175-
176-
if (DeviceNum == omp_get_initial_device()) {
177-
DP("Call to omp_target_is_present on host, returning true\n");
178-
return true;
179-
}
180-
181-
auto DeviceOrErr = PM->getDevice(DeviceNum);
182-
if (!DeviceOrErr)
183-
FATAL_MESSAGE(DeviceNum, "%s", toString(DeviceOrErr.takeError()).c_str());
184-
185173
// omp_target_is_present tests whether a host pointer refers to storage that
186174
// is mapped to a given device. However, due to the lack of the storage size,
187175
// only check 1 byte. Cannot set size 0 which checks whether the pointer (zero
188176
// length array) is mapped instead of the referred storage.
189-
TargetPointerResultTy TPR =
190-
DeviceOrErr->getMappingInfo().getTgtPtrBegin(const_cast<void *>(Ptr), 1,
191-
/*UpdateRefCount=*/false,
192-
/*UseHoldRefCount=*/false);
193-
int Rc = TPR.isPresent();
194-
DP("Call to omp_target_is_present returns %d\n", Rc);
195-
return Rc;
177+
return checkTargetAddressMapping(Ptr, 1, DeviceNum, "omp_target_is_present");
196178
}
197179

180+
EXTERN int omp_target_is_accessible(const void *Ptr, size_t Size, int DeviceNum) {
181+
OMPT_IF_BUILT(ReturnAddressSetterRAII RA(__builtin_return_address(0)));
182+
DP("Call to omp_target_is_accessible for device %d and address " DPxMOD
183+
" with size %zu\n",
184+
DeviceNum, DPxPTR(Ptr), Size);
185+
186+
// omp_target_is_accessible tests whether a host pointer refers to storage
187+
// that is mapped to a given device and is accessible from the device. The
188+
// storage size is provided.
189+
return checkTargetAddressMapping(Ptr, Size, DeviceNum, "omp_target_is_accessible");
190+
}
191+
198192
EXTERN int omp_target_memcpy(void *Dst, const void *Src, size_t Length,
199193
size_t DstOffset, size_t SrcOffset, int DstDevice,
200194
int SrcDevice) {

offload/libomptarget/exports

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ VERS1.0 {
3737
__kmpc_push_target_tripcount_mapper;
3838
ompx_dump_mapping_tables;
3939
omp_get_mapped_ptr;
40+
omp_target_is_accessible;
4041
omp_get_num_devices;
4142
omp_get_device_num;
4243
omp_get_initial_device;

offload/libomptarget/omptarget.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,31 @@ static int32_t getParentIndex(int64_t Type) {
198198
return ((Type & OMP_TGT_MAPTYPE_MEMBER_OF) >> 48) - 1;
199199
}
200200

201+
int checkTargetAddressMapping(const void *Ptr, size_t Size, int DeviceNum, const char *Name) {
202+
if (!Ptr) {
203+
DP("Call to %s with NULL ptr, returning false\n", Name);
204+
return false;
205+
}
206+
207+
if (DeviceNum == omp_get_initial_device()) {
208+
DP("Call to %s on host, returning true\n", Name);
209+
return true;
210+
}
211+
212+
auto DeviceOrErr = PM->getDevice(DeviceNum);
213+
if (!DeviceOrErr)
214+
FATAL_MESSAGE(DeviceNum, "%s", toString(DeviceOrErr.takeError()).c_str());
215+
216+
TargetPointerResultTy TPR =
217+
DeviceOrErr->getMappingInfo().getTgtPtrBegin(const_cast<void *>(Ptr), Size,
218+
false,
219+
false);
220+
221+
int Rc = TPR.isPresent();
222+
DP("Call to %s returns %d\n", Name, Rc);
223+
return Rc;
224+
}
225+
201226
void *targetAllocExplicit(size_t Size, int DeviceNum, int Kind,
202227
const char *Name) {
203228
DP("Call to %s for device %d requesting %zu bytes\n", Name, DeviceNum, Size);
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// RUN: %libomptarget-compilexx-generic
2+
// RUN: %libomptarget-run-generic 2>&1 \
3+
// RUN: | %fcheck-generic
4+
5+
// REQUIRES: unified_shared_memory
6+
7+
#include <stdio.h>
8+
#include <iostream>
9+
#include <omp.h>
10+
#include <assert.h>
11+
12+
// The runtime considers unified shared memory to be always present.
13+
#pragma omp requires unified_shared_memory
14+
15+
int main() {
16+
int size = 10;
17+
int *x = (int *)malloc(size * sizeof(int));
18+
const int dev_num = omp_get_default_device();
19+
20+
int is_accessible = omp_target_is_accessible(x, size * sizeof(int), dev_num);
21+
int errors = 0;
22+
int uses_shared_memory = 0;
23+
24+
#pragma omp target map(to: uses_shared_memory)
25+
uses_shared_memory = 1;
26+
27+
assert(uses_shared_memory != is_accessible);
28+
29+
if (is_accessible) {
30+
#pragma omp target firstprivate(x)
31+
for (int i = 0; i < size; i++)
32+
x[i] = i * 3;
33+
34+
for (int i = 0; i < size; i++)
35+
errors += (x[i] == (i * 3) ? 1 : 0);
36+
}
37+
38+
free(x);
39+
// CHECK: x overwritten 0 times
40+
printf("x overwritten %d times\n", errors);
41+
42+
return errors;
43+
}

0 commit comments

Comments
 (0)