Skip to content

Commit 075b660

Browse files
committed
Added additional enums to urPhysicalMemGetInfo along with splitting the CTS test out from a switch statement to separate tests.
Fixed a typo in memory.yml.
1 parent aa41ed8 commit 075b660

File tree

9 files changed

+237
-38
lines changed

9 files changed

+237
-38
lines changed

include/ur_api.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2519,7 +2519,7 @@ typedef enum ur_mem_type_t {
25192519
///////////////////////////////////////////////////////////////////////////////
25202520
/// @brief Memory Information type
25212521
typedef enum ur_mem_info_t {
2522-
UR_MEM_INFO_SIZE = 0, ///< [size_t] actual size of of memory object in bytes
2522+
UR_MEM_INFO_SIZE = 0, ///< [size_t] actual size of memory object in bytes
25232523
UR_MEM_INFO_CONTEXT = 1, ///< [::ur_context_handle_t] context in which the memory object was created
25242524
UR_MEM_INFO_REFERENCE_COUNT = 2, ///< [uint32_t] Reference count of the memory object.
25252525
///< The reference count returned should be considered immediately stale.
@@ -4130,7 +4130,14 @@ urPhysicalMemRelease(
41304130
///////////////////////////////////////////////////////////////////////////////
41314131
/// @brief Physical memory range info queries.
41324132
typedef enum ur_physical_mem_info_t {
4133-
UR_PHYSICAL_MEM_INFO_REFERENCE_COUNT = 0, ///< [uint32_t] Reference count of the physical memory object.
4133+
UR_PHYSICAL_MEM_INFO_CONTEXT = 0, ///< [::ur_context_handle_t] context in which the physical memory object
4134+
///< was created.
4135+
UR_PHYSICAL_MEM_INFO_DEVICE = 1, ///< [::ur_device_handle_t] device associated with this physical memory
4136+
///< object.
4137+
UR_PHYSICAL_MEM_INFO_SIZE = 2, ///< [size_t] actual size of the physical memory object in bytes.
4138+
UR_PHYSICAL_MEM_INFO_PROPERTIES = 3, ///< [::ur_physical_mem_properties_t] properties set when creating this
4139+
///< physical memory object.
4140+
UR_PHYSICAL_MEM_INFO_REFERENCE_COUNT = 4, ///< [uint32_t] Reference count of the physical memory object.
41344141
///< The reference count returned should be considered immediately stale.
41354142
///< It is unsuitable for general use in applications. This feature is
41364143
///< provided for identifying memory leaks.

include/ur_print.hpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7447,6 +7447,18 @@ inline std::ostream &operator<<(std::ostream &os, const struct ur_physical_mem_p
74477447
/// std::ostream &
74487448
inline std::ostream &operator<<(std::ostream &os, enum ur_physical_mem_info_t value) {
74497449
switch (value) {
7450+
case UR_PHYSICAL_MEM_INFO_CONTEXT:
7451+
os << "UR_PHYSICAL_MEM_INFO_CONTEXT";
7452+
break;
7453+
case UR_PHYSICAL_MEM_INFO_DEVICE:
7454+
os << "UR_PHYSICAL_MEM_INFO_DEVICE";
7455+
break;
7456+
case UR_PHYSICAL_MEM_INFO_SIZE:
7457+
os << "UR_PHYSICAL_MEM_INFO_SIZE";
7458+
break;
7459+
case UR_PHYSICAL_MEM_INFO_PROPERTIES:
7460+
os << "UR_PHYSICAL_MEM_INFO_PROPERTIES";
7461+
break;
74507462
case UR_PHYSICAL_MEM_INFO_REFERENCE_COUNT:
74517463
os << "UR_PHYSICAL_MEM_INFO_REFERENCE_COUNT";
74527464
break;
@@ -7466,6 +7478,56 @@ inline ur_result_t printTagged(std::ostream &os, const void *ptr, ur_physical_me
74667478
}
74677479

74687480
switch (value) {
7481+
case UR_PHYSICAL_MEM_INFO_CONTEXT: {
7482+
const ur_context_handle_t *tptr = (const ur_context_handle_t *)ptr;
7483+
if (sizeof(ur_context_handle_t) > size) {
7484+
os << "invalid size (is: " << size << ", expected: >=" << sizeof(ur_context_handle_t) << ")";
7485+
return UR_RESULT_ERROR_INVALID_SIZE;
7486+
}
7487+
os << (const void *)(tptr) << " (";
7488+
7489+
ur::details::printPtr(os,
7490+
*tptr);
7491+
7492+
os << ")";
7493+
} break;
7494+
case UR_PHYSICAL_MEM_INFO_DEVICE: {
7495+
const ur_device_handle_t *tptr = (const ur_device_handle_t *)ptr;
7496+
if (sizeof(ur_device_handle_t) > size) {
7497+
os << "invalid size (is: " << size << ", expected: >=" << sizeof(ur_device_handle_t) << ")";
7498+
return UR_RESULT_ERROR_INVALID_SIZE;
7499+
}
7500+
os << (const void *)(tptr) << " (";
7501+
7502+
ur::details::printPtr(os,
7503+
*tptr);
7504+
7505+
os << ")";
7506+
} break;
7507+
case UR_PHYSICAL_MEM_INFO_SIZE: {
7508+
const size_t *tptr = (const size_t *)ptr;
7509+
if (sizeof(size_t) > size) {
7510+
os << "invalid size (is: " << size << ", expected: >=" << sizeof(size_t) << ")";
7511+
return UR_RESULT_ERROR_INVALID_SIZE;
7512+
}
7513+
os << (const void *)(tptr) << " (";
7514+
7515+
os << *tptr;
7516+
7517+
os << ")";
7518+
} break;
7519+
case UR_PHYSICAL_MEM_INFO_PROPERTIES: {
7520+
const ur_physical_mem_properties_t *tptr = (const ur_physical_mem_properties_t *)ptr;
7521+
if (sizeof(ur_physical_mem_properties_t) > size) {
7522+
os << "invalid size (is: " << size << ", expected: >=" << sizeof(ur_physical_mem_properties_t) << ")";
7523+
return UR_RESULT_ERROR_INVALID_SIZE;
7524+
}
7525+
os << (const void *)(tptr) << " (";
7526+
7527+
os << *tptr;
7528+
7529+
os << ")";
7530+
} break;
74697531
case UR_PHYSICAL_MEM_INFO_REFERENCE_COUNT: {
74707532
const uint32_t *tptr = (const uint32_t *)ptr;
74717533
if (sizeof(uint32_t) > size) {

scripts/core/memory.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ name: $x_mem_info_t
5959
typed_etors: True
6060
etors:
6161
- name: SIZE
62-
desc: "[size_t] actual size of of memory object in bytes"
62+
desc: "[size_t] actual size of the memory object in bytes"
6363
- name: CONTEXT
6464
desc: "[$x_context_handle_t] context in which the memory object was created"
6565
- name: REFERENCE_COUNT

scripts/core/virtual_memory.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,14 @@ class: $xPhysicalMem
311311
name: $x_physical_mem_info_t
312312
typed_etors: True
313313
etors:
314-
# properties and size too
314+
- name: CONTEXT
315+
desc: "[$x_context_handle_t] context in which the physical memory object was created."
316+
- name: DEVICE
317+
desc: "[$x_device_handle_t] device associated with this physical memory object."
318+
- name: SIZE
319+
desc: "[size_t] actual size of the physical memory object in bytes."
320+
- name: PROPERTIES
321+
desc: "[$x_physical_mem_properties_t] properties set when creating this physical memory object."
315322
- name: REFERENCE_COUNT
316323
desc: |
317324
[uint32_t] Reference count of the physical memory object.

source/adapters/cuda/physical_mem.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urPhysicalMemCreate(
3333
UR_CHECK_ERROR(Result);
3434
}
3535
try {
36-
*phPhysicalMem =
37-
new ur_physical_mem_handle_t_(ResHandle, hContext, hDevice);
36+
*phPhysicalMem = new ur_physical_mem_handle_t_(ResHandle, hContext, hDevice,
37+
size, *pProperties);
3838
} catch (std::bad_alloc &) {
3939
return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
4040
} catch (...) {
@@ -74,6 +74,18 @@ UR_APIEXPORT ur_result_t UR_APICALL urPhysicalMemGetInfo(
7474
UrReturnHelper ReturnValue(propSize, pPropValue, pPropSizeRet);
7575

7676
switch (propName) {
77+
case UR_PHYSICAL_MEM_INFO_CONTEXT: {
78+
return ReturnValue(hPhysicalMem->getContext());
79+
}
80+
case UR_PHYSICAL_MEM_INFO_DEVICE: {
81+
return ReturnValue(hPhysicalMem->getDevice());
82+
}
83+
case UR_PHYSICAL_MEM_INFO_SIZE: {
84+
return ReturnValue(hPhysicalMem->getSize());
85+
}
86+
case UR_PHYSICAL_MEM_INFO_PROPERTIES: {
87+
return ReturnValue(hPhysicalMem->getProperties());
88+
}
7789
case UR_PHYSICAL_MEM_INFO_REFERENCE_COUNT: {
7890
return ReturnValue(hPhysicalMem->getReferenceCount());
7991
}

source/adapters/cuda/physical_mem.hpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,13 @@ struct ur_physical_mem_handle_t_ {
2727
native_type PhysicalMem;
2828
ur_context_handle_t_ *Context;
2929
ur_device_handle_t Device;
30+
size_t Size;
31+
ur_physical_mem_properties_t Properties;
3032

3133
ur_physical_mem_handle_t_(native_type PhysMem, ur_context_handle_t_ *Ctx,
32-
ur_device_handle_t Device)
33-
: RefCount(1), PhysicalMem(PhysMem), Context(Ctx), Device(Device) {
34+
ur_device_handle_t Device, size_t Size, ur_physical_mem_properties_t Properties)
35+
: RefCount(1), PhysicalMem(PhysMem), Context(Ctx), Device(Device),
36+
Size(Size), Properties(Properties) {
3437
urContextRetain(Context);
3538
urDeviceRetain(Device);
3639
}
@@ -51,4 +54,8 @@ struct ur_physical_mem_handle_t_ {
5154
uint32_t decrementReferenceCount() noexcept { return --RefCount; }
5255

5356
uint32_t getReferenceCount() const noexcept { return RefCount; }
57+
58+
size_t getSize() const noexcept { return Size; }
59+
60+
ur_physical_mem_properties_t getProperties() const noexcept { return Properties; }
5461
};

source/loader/ur_ldrddi.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2547,10 +2547,55 @@ __urdlllocal ur_result_t UR_APICALL urPhysicalMemGetInfo(
25472547
hPhysicalMem =
25482548
reinterpret_cast<ur_physical_mem_object_t *>(hPhysicalMem)->handle;
25492549

2550+
// this value is needed for converting adapter handles to loader handles
2551+
size_t sizeret = 0;
2552+
if (pPropSizeRet == NULL) {
2553+
pPropSizeRet = &sizeret;
2554+
}
2555+
25502556
// forward to device-platform
25512557
result =
25522558
pfnGetInfo(hPhysicalMem, propName, propSize, pPropValue, pPropSizeRet);
25532559

2560+
if (UR_RESULT_SUCCESS != result) {
2561+
return result;
2562+
}
2563+
2564+
try {
2565+
if (pPropValue != nullptr) {
2566+
switch (propName) {
2567+
case UR_PHYSICAL_MEM_INFO_CONTEXT: {
2568+
ur_context_handle_t *handles =
2569+
reinterpret_cast<ur_context_handle_t *>(pPropValue);
2570+
size_t nelements = *pPropSizeRet / sizeof(ur_context_handle_t);
2571+
for (size_t i = 0; i < nelements; ++i) {
2572+
if (handles[i] != nullptr) {
2573+
handles[i] = reinterpret_cast<ur_context_handle_t>(
2574+
context->factories.ur_context_factory.getInstance(
2575+
handles[i], dditable));
2576+
}
2577+
}
2578+
} break;
2579+
case UR_PHYSICAL_MEM_INFO_DEVICE: {
2580+
ur_device_handle_t *handles =
2581+
reinterpret_cast<ur_device_handle_t *>(pPropValue);
2582+
size_t nelements = *pPropSizeRet / sizeof(ur_device_handle_t);
2583+
for (size_t i = 0; i < nelements; ++i) {
2584+
if (handles[i] != nullptr) {
2585+
handles[i] = reinterpret_cast<ur_device_handle_t>(
2586+
context->factories.ur_device_factory.getInstance(
2587+
handles[i], dditable));
2588+
}
2589+
}
2590+
} break;
2591+
default: {
2592+
} break;
2593+
}
2594+
}
2595+
} catch (std::bad_alloc &) {
2596+
result = UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
2597+
}
2598+
25542599
return result;
25552600
}
25562601

test/conformance/testing/include/uur/fixtures.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -941,13 +941,9 @@ struct urPhysicalMemTest : urVirtualMemGranularityTest {
941941
void SetUp() override {
942942
UUR_RETURN_ON_FATAL_FAILURE(urVirtualMemGranularityTest::SetUp());
943943
size = granularity * 256;
944-
ur_physical_mem_properties_t props{
945-
UR_STRUCTURE_TYPE_PHYSICAL_MEM_PROPERTIES,
946-
nullptr,
947-
0 /*flags*/,
948-
};
949-
ASSERT_SUCCESS(
950-
urPhysicalMemCreate(context, device, size, &props, &physical_mem));
944+
945+
ASSERT_SUCCESS(urPhysicalMemCreate(context, device, size, &properties,
946+
&physical_mem));
951947
ASSERT_NE(physical_mem, nullptr);
952948
}
953949

@@ -960,6 +956,11 @@ struct urPhysicalMemTest : urVirtualMemGranularityTest {
960956

961957
size_t size = 0;
962958
ur_physical_mem_handle_t physical_mem = nullptr;
959+
ur_physical_mem_properties_t properties{
960+
UR_STRUCTURE_TYPE_PHYSICAL_MEM_PROPERTIES,
961+
nullptr,
962+
0 /*flags*/,
963+
};
963964
};
964965

965966
template <class T>

test/conformance/virtual_memory/urPhysicalMemGetInfo.cpp

Lines changed: 81 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,90 @@
44
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
55
#include <uur/fixtures.h>
66

7-
using urPhysicalMemGetInfoWithFlagsParamTest =
8-
uur::urPhysicalMemTestWithParam<ur_physical_mem_info_t>;
9-
UUR_TEST_SUITE_P(urPhysicalMemGetInfoWithFlagsParamTest,
10-
::testing::Values(UR_PHYSICAL_MEM_INFO_REFERENCE_COUNT),
11-
uur::deviceTestWithParamPrinter<ur_physical_mem_info_t>);
7+
using urPhysicalMemGetInfoTest = uur::urPhysicalMemTest;
8+
UUR_INSTANTIATE_DEVICE_TEST_SUITE_P(urPhysicalMemGetInfoTest);
129

13-
TEST_P(urPhysicalMemGetInfoWithFlagsParamTest, Success) {
10+
TEST_P(urPhysicalMemGetInfoTest, Context) {
1411
size_t info_size = 0;
15-
ur_physical_mem_info_t info = getParam();
16-
ASSERT_SUCCESS(
17-
urPhysicalMemGetInfo(physical_mem, info, 0, nullptr, &info_size));
12+
13+
ASSERT_SUCCESS(urPhysicalMemGetInfo(
14+
physical_mem, UR_PHYSICAL_MEM_INFO_CONTEXT, 0, nullptr, &info_size));
15+
ASSERT_NE(info_size, 0);
16+
17+
std::vector<uint8_t> data(info_size);
18+
ASSERT_SUCCESS(urPhysicalMemGetInfo(physical_mem,
19+
UR_PHYSICAL_MEM_INFO_CONTEXT,
20+
data.size(), data.data(), nullptr));
21+
22+
auto returned_context =
23+
reinterpret_cast<ur_context_handle_t *>(data.data());
24+
ASSERT_EQ(context, *returned_context);
25+
}
26+
27+
TEST_P(urPhysicalMemGetInfoTest, Device) {
28+
size_t info_size = 0;
29+
30+
ASSERT_SUCCESS(urPhysicalMemGetInfo(
31+
physical_mem, UR_PHYSICAL_MEM_INFO_DEVICE, 0, nullptr, &info_size));
32+
ASSERT_NE(info_size, 0);
33+
34+
std::vector<uint8_t> data(info_size);
35+
ASSERT_SUCCESS(urPhysicalMemGetInfo(physical_mem,
36+
UR_PHYSICAL_MEM_INFO_DEVICE,
37+
data.size(), data.data(), nullptr));
38+
39+
auto returned_device = reinterpret_cast<ur_device_handle_t *>(data.data());
40+
ASSERT_EQ(device, *returned_device);
41+
}
42+
43+
TEST_P(urPhysicalMemGetInfoTest, Size) {
44+
size_t info_size = 0;
45+
46+
ASSERT_SUCCESS(urPhysicalMemGetInfo(physical_mem, UR_PHYSICAL_MEM_INFO_SIZE,
47+
0, nullptr, &info_size));
1848
ASSERT_NE(info_size, 0);
1949

2050
std::vector<uint8_t> data(info_size);
21-
ASSERT_SUCCESS(urPhysicalMemGetInfo(physical_mem, info, data.size(),
22-
data.data(), nullptr));
23-
24-
switch (info) {
25-
case UR_PHYSICAL_MEM_INFO_REFERENCE_COUNT: {
26-
const size_t ReferenceCount =
27-
*reinterpret_cast<const uint32_t *>(data.data());
28-
ASSERT_EQ(ReferenceCount, 1);
29-
} break;
30-
31-
default:
32-
FAIL() << "Unhandled ur_physical_mem_info_t enumeration: " << info;
33-
break;
34-
}
51+
ASSERT_SUCCESS(urPhysicalMemGetInfo(physical_mem, UR_PHYSICAL_MEM_INFO_SIZE,
52+
data.size(), data.data(), nullptr));
53+
54+
auto returned_size = reinterpret_cast<size_t *>(data.data());
55+
ASSERT_EQ(size, *returned_size);
56+
}
57+
58+
TEST_P(urPhysicalMemGetInfoTest, Properties) {
59+
size_t info_size = 0;
60+
61+
ASSERT_SUCCESS(urPhysicalMemGetInfo(
62+
physical_mem, UR_PHYSICAL_MEM_INFO_PROPERTIES, 0, nullptr, &info_size));
63+
ASSERT_NE(info_size, 0);
64+
65+
std::vector<uint8_t> data(info_size);
66+
ASSERT_SUCCESS(urPhysicalMemGetInfo(physical_mem,
67+
UR_PHYSICAL_MEM_INFO_PROPERTIES,
68+
data.size(), data.data(), nullptr));
69+
70+
auto returned_properties =
71+
reinterpret_cast<ur_physical_mem_properties_t *>(data.data());
72+
ASSERT_EQ(properties.stype, returned_properties->stype);
73+
ASSERT_EQ(properties.pNext, returned_properties->pNext);
74+
ASSERT_EQ(properties.flags, returned_properties->flags);
75+
}
76+
77+
TEST_P(urPhysicalMemGetInfoTest, ReferenceCount) {
78+
size_t info_size = 0;
79+
80+
ASSERT_SUCCESS(urPhysicalMemGetInfo(physical_mem,
81+
UR_PHYSICAL_MEM_INFO_REFERENCE_COUNT, 0,
82+
nullptr, &info_size));
83+
ASSERT_NE(info_size, 0);
84+
85+
std::vector<uint8_t> data(info_size);
86+
ASSERT_SUCCESS(urPhysicalMemGetInfo(physical_mem,
87+
UR_PHYSICAL_MEM_INFO_REFERENCE_COUNT,
88+
data.size(), data.data(), nullptr));
89+
90+
const size_t ReferenceCount =
91+
*reinterpret_cast<const uint32_t *>(data.data());
92+
ASSERT_EQ(ReferenceCount, 1);
3593
}

0 commit comments

Comments
 (0)