Skip to content

Commit e825f58

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 6a96f52 commit e825f58

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
@@ -2516,7 +2516,7 @@ typedef enum ur_mem_type_t {
25162516
///////////////////////////////////////////////////////////////////////////////
25172517
/// @brief Memory Information type
25182518
typedef enum ur_mem_info_t {
2519-
UR_MEM_INFO_SIZE = 0, ///< [size_t] actual size of of memory object in bytes
2519+
UR_MEM_INFO_SIZE = 0, ///< [size_t] actual size of the memory object in bytes
25202520
UR_MEM_INFO_CONTEXT = 1, ///< [::ur_context_handle_t] context in which the memory object was created
25212521
/// @cond
25222522
UR_MEM_INFO_FORCE_UINT32 = 0x7fffffff
@@ -4122,7 +4122,14 @@ urPhysicalMemRelease(
41224122
///////////////////////////////////////////////////////////////////////////////
41234123
/// @brief Physical memory range info queries.
41244124
typedef enum ur_physical_mem_info_t {
4125-
UR_PHYSICAL_MEM_INFO_REFERENCE_COUNT = 0, ///< [uint32_t] Reference count of the physical memory object.
4125+
UR_PHYSICAL_MEM_INFO_CONTEXT = 0, ///< [::ur_context_handle_t] context in which the physical memory object
4126+
///< was created.
4127+
UR_PHYSICAL_MEM_INFO_DEVICE = 1, ///< [::ur_device_handle_t] device associated with this physical memory
4128+
///< object.
4129+
UR_PHYSICAL_MEM_INFO_SIZE = 2, ///< [size_t] actual size of the physical memory object in bytes.
4130+
UR_PHYSICAL_MEM_INFO_PROPERTIES = 3, ///< [::ur_physical_mem_properties_t] properties set when creating this
4131+
///< physical memory object.
4132+
UR_PHYSICAL_MEM_INFO_REFERENCE_COUNT = 4, ///< [uint32_t] Reference count of the physical memory object.
41264133
///< The reference count returned should be considered immediately stale.
41274134
///< It is unsuitable for general use in applications. This feature is
41284135
///< provided for identifying memory leaks.

include/ur_print.hpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7401,6 +7401,18 @@ inline std::ostream &operator<<(std::ostream &os, const struct ur_physical_mem_p
74017401
/// std::ostream &
74027402
inline std::ostream &operator<<(std::ostream &os, enum ur_physical_mem_info_t value) {
74037403
switch (value) {
7404+
case UR_PHYSICAL_MEM_INFO_CONTEXT:
7405+
os << "UR_PHYSICAL_MEM_INFO_CONTEXT";
7406+
break;
7407+
case UR_PHYSICAL_MEM_INFO_DEVICE:
7408+
os << "UR_PHYSICAL_MEM_INFO_DEVICE";
7409+
break;
7410+
case UR_PHYSICAL_MEM_INFO_SIZE:
7411+
os << "UR_PHYSICAL_MEM_INFO_SIZE";
7412+
break;
7413+
case UR_PHYSICAL_MEM_INFO_PROPERTIES:
7414+
os << "UR_PHYSICAL_MEM_INFO_PROPERTIES";
7415+
break;
74047416
case UR_PHYSICAL_MEM_INFO_REFERENCE_COUNT:
74057417
os << "UR_PHYSICAL_MEM_INFO_REFERENCE_COUNT";
74067418
break;
@@ -7420,6 +7432,56 @@ inline ur_result_t printTagged(std::ostream &os, const void *ptr, ur_physical_me
74207432
}
74217433

74227434
switch (value) {
7435+
case UR_PHYSICAL_MEM_INFO_CONTEXT: {
7436+
const ur_context_handle_t *tptr = (const ur_context_handle_t *)ptr;
7437+
if (sizeof(ur_context_handle_t) > size) {
7438+
os << "invalid size (is: " << size << ", expected: >=" << sizeof(ur_context_handle_t) << ")";
7439+
return UR_RESULT_ERROR_INVALID_SIZE;
7440+
}
7441+
os << (const void *)(tptr) << " (";
7442+
7443+
ur::details::printPtr(os,
7444+
*tptr);
7445+
7446+
os << ")";
7447+
} break;
7448+
case UR_PHYSICAL_MEM_INFO_DEVICE: {
7449+
const ur_device_handle_t *tptr = (const ur_device_handle_t *)ptr;
7450+
if (sizeof(ur_device_handle_t) > size) {
7451+
os << "invalid size (is: " << size << ", expected: >=" << sizeof(ur_device_handle_t) << ")";
7452+
return UR_RESULT_ERROR_INVALID_SIZE;
7453+
}
7454+
os << (const void *)(tptr) << " (";
7455+
7456+
ur::details::printPtr(os,
7457+
*tptr);
7458+
7459+
os << ")";
7460+
} break;
7461+
case UR_PHYSICAL_MEM_INFO_SIZE: {
7462+
const size_t *tptr = (const size_t *)ptr;
7463+
if (sizeof(size_t) > size) {
7464+
os << "invalid size (is: " << size << ", expected: >=" << sizeof(size_t) << ")";
7465+
return UR_RESULT_ERROR_INVALID_SIZE;
7466+
}
7467+
os << (const void *)(tptr) << " (";
7468+
7469+
os << *tptr;
7470+
7471+
os << ")";
7472+
} break;
7473+
case UR_PHYSICAL_MEM_INFO_PROPERTIES: {
7474+
const ur_physical_mem_properties_t *tptr = (const ur_physical_mem_properties_t *)ptr;
7475+
if (sizeof(ur_physical_mem_properties_t) > size) {
7476+
os << "invalid size (is: " << size << ", expected: >=" << sizeof(ur_physical_mem_properties_t) << ")";
7477+
return UR_RESULT_ERROR_INVALID_SIZE;
7478+
}
7479+
os << (const void *)(tptr) << " (";
7480+
7481+
os << *tptr;
7482+
7483+
os << ")";
7484+
} break;
74237485
case UR_PHYSICAL_MEM_INFO_REFERENCE_COUNT: {
74247486
const uint32_t *tptr = (const uint32_t *)ptr;
74257487
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
--- #--------------------------------------------------------------------------

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
@@ -935,13 +935,9 @@ struct urPhysicalMemTest : urVirtualMemGranularityTest {
935935
void SetUp() override {
936936
UUR_RETURN_ON_FATAL_FAILURE(urVirtualMemGranularityTest::SetUp());
937937
size = granularity * 256;
938-
ur_physical_mem_properties_t props{
939-
UR_STRUCTURE_TYPE_PHYSICAL_MEM_PROPERTIES,
940-
nullptr,
941-
0 /*flags*/,
942-
};
943-
ASSERT_SUCCESS(
944-
urPhysicalMemCreate(context, device, size, &props, &physical_mem));
938+
939+
ASSERT_SUCCESS(urPhysicalMemCreate(context, device, size, &properties,
940+
&physical_mem));
945941
ASSERT_NE(physical_mem, nullptr);
946942
}
947943

@@ -954,6 +950,11 @@ struct urPhysicalMemTest : urVirtualMemGranularityTest {
954950

955951
size_t size = 0;
956952
ur_physical_mem_handle_t physical_mem = nullptr;
953+
ur_physical_mem_properties_t properties{
954+
UR_STRUCTURE_TYPE_PHYSICAL_MEM_PROPERTIES,
955+
nullptr,
956+
0 /*flags*/,
957+
};
957958
};
958959

959960
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)