Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions offload/libomptarget/OpenMP/API.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -683,3 +683,20 @@ EXTERN void *omp_get_mapped_ptr(const void *Ptr, int DeviceNum) {

return TPR.TargetPointer;
}

EXTERN void *omp_get_device_ptr_if_present(void *Ptr, int DeviceNum) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This routine is not part of the OpenMP API. Please use a different prefix. If the routine only is only to be called from generated code, it may be prudent to prefix it with __ to put into the compiler's "namespace".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Name changed to __omp_get_device_ptr_if_present. Thanks

if (!Ptr) {
return nullptr;
}
// Validate device number
if (DeviceNum < 0 || DeviceNum >= omp_get_num_devices()) {
return Ptr;
}
// If not present on the device, it should already be a device ptr
if (!omp_target_is_present(Ptr, DeviceNum)) {
return Ptr;
}
// Get the mapped device pointer
void *DevicePtr = omp_get_mapped_ptr(Ptr, DeviceNum);
? return DevicePtr ? DevicePtr : Ptr;
}
1 change: 1 addition & 0 deletions offload/libomptarget/exports
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ VERS1.0 {
omp_get_num_devices;
omp_get_device_num;
omp_get_initial_device;
omp_get_device_ptr_if_present;
omp_target_alloc;
omp_target_free;
omp_target_is_present;
Expand Down
86 changes: 86 additions & 0 deletions offload/test/api/omp_get_device_ptr_if_present.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// RUN: %libomptarget-compile-run-and-check-generic
// REQUIRES: unified_shared_memory

#include <assert.h>
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>

#define N 1024
#define OFFSET 16

int main(int argc, char *argv[]) {
int *host_data = (int *)malloc(sizeof(int) * N);
int device_num = omp_get_default_device();

// Initialize data
for (int i = 0; i < N; i++) {
host_data[i] = i;
}

// Test 1: NULL pointer should return NULL
void *result = omp_get_device_ptr_if_present(NULL, device_num);
assert(result == NULL && "NULL input should return NULL");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not use assert, but make this a hard failure with exit(1) or return 1. With the assert the test will depend on whether assertions have been enabled, and the test should correctly test for all possible build configurations.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the tests to return 1 on failure, following what others tests in offload do.


// Test 2: Invalid device number should return original pointer
result = omp_get_device_ptr_if_present(host_data, -1);
assert(result == host_data &&
"Invalid device should return original pointer");

result = omp_get_device_ptr_if_present(host_data, omp_get_num_devices() + 1);
assert(result == host_data &&
"Out of range device should return original pointer");

// Test 3: Unmapped pointer should return original pointer
result = omp_get_device_ptr_if_present(host_data, device_num);
assert(result == host_data && "Unmapped pointer should return original");

// Map data to device
#pragma omp target enter data map(to : host_data[:N])

// Test 4: Mapped pointer should return device pointer
result = omp_get_device_ptr_if_present(host_data, device_num);
void *expected_device_ptr = omp_get_mapped_ptr(host_data, device_num);
assert(result == expected_device_ptr &&
"Should return device pointer for mapped data");
assert(result != host_data &&
"Device pointer should differ from host pointer");

// Test 5: Pointer with offset should also work
result = omp_get_device_ptr_if_present(host_data + OFFSET, device_num);
expected_device_ptr = omp_get_mapped_ptr(host_data + OFFSET, device_num);
assert(result == expected_device_ptr && "Should handle offset correctly");

// Test 6: Verify device pointer works in target region
void *ptr_from_device = NULL;
void *ptr_from_api = omp_get_device_ptr_if_present(host_data, device_num);

#pragma omp target map(from : ptr_from_device)
{ ptr_from_device = host_data; }

assert(ptr_from_device == ptr_from_api &&
"Device pointer should match in target region");

// Unmap data
#pragma omp target exit data map(delete : host_data[:N])

// Test 7: After unmapping, should return original pointer again
result = omp_get_device_ptr_if_present(host_data, device_num);
assert(result == host_data &&
"After unmapping should return original pointer");

// Test 8: Already device pointer scenario
// Allocate directly on device
void *device_alloc = omp_target_alloc(sizeof(int) * N, device_num);
if (device_alloc) {
// This pointer is not mapped, so should return as-is
result = omp_get_device_ptr_if_present(device_alloc, device_num);
assert(result == device_alloc &&
"Device-allocated pointer should return as-is");
omp_target_free(device_alloc, device_num);
}

free(host_data);
printf("PASS\n");
return 0;
}