Skip to content

Commit 2ffa659

Browse files
authored
Merge pull request #443 from igchor/get_providers
[uma] add function to retrieve providers
2 parents 8edca04 + 279fc63 commit 2ffa659

File tree

8 files changed

+117
-2
lines changed

8 files changed

+117
-2
lines changed

source/common/unified_memory_allocation/include/uma/memory_pool.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,18 @@ enum uma_result_t umaPoolGetLastResult(uma_memory_pool_handle_t hPool,
129129
/// \return handle to a memory pool that contains ptr or NULL if pointer does not belong to any UMA pool
130130
uma_memory_pool_handle_t umaPoolByPtr(const void *ptr);
131131

132+
///
133+
/// \brief Retrieve memory providers associated with a given pool.
134+
/// \param hPool specified memory pool
135+
/// \param hProviders [out] pointer to an array of memory providers. If numProviders is not equal to or
136+
/// greater than the real number of providers, UMA_RESULT_ERROR_INVALID_ARGUMENT is returned.
137+
/// \param numProviders [in] number of memory providers to return
138+
/// \param numProvidersRet pointer to the actual number of memory providers.
139+
enum uma_result_t
140+
umaPoolGetMemoryProviders(uma_memory_pool_handle_t hPool, size_t numProviders,
141+
uma_memory_provider_handle_t *hProviders,
142+
size_t *numProvidersRet);
143+
132144
#ifdef __cplusplus
133145
}
134146
#endif

source/common/unified_memory_allocation/src/memory_pool.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
*
77
*/
88

9+
#include "memory_provider_internal.h"
910
#include "memory_tracker.h"
11+
1012
#include <uma/memory_pool.h>
1113
#include <uma/memory_pool_ops.h>
1214

@@ -134,3 +136,25 @@ enum uma_result_t umaPoolGetLastResult(uma_memory_pool_handle_t hPool,
134136
uma_memory_pool_handle_t umaPoolByPtr(const void *ptr) {
135137
return umaMemoryTrackerGetPool(umaMemoryTrackerGet(), ptr);
136138
}
139+
140+
enum uma_result_t
141+
umaPoolGetMemoryProviders(uma_memory_pool_handle_t hPool, size_t numProviders,
142+
uma_memory_provider_handle_t *hProviders,
143+
size_t *numProvidersRet) {
144+
if (hProviders && numProviders < hPool->numProviders) {
145+
return UMA_RESULT_ERROR_INVALID_ARGUMENT;
146+
}
147+
148+
if (numProvidersRet) {
149+
*numProvidersRet = hPool->numProviders;
150+
}
151+
152+
if (hProviders) {
153+
for (size_t i = 0; i < hPool->numProviders; i++) {
154+
umaTrackingMemoryProviderGetUpstreamProvider(
155+
umaMemoryProviderGetPriv(hPool->providers[i]), hProviders + i);
156+
}
157+
}
158+
159+
return UMA_RESULT_SUCCESS;
160+
}

source/common/unified_memory_allocation/src/memory_provider.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*
77
*/
88

9+
#include "memory_provider_internal.h"
910
#include <uma/memory_provider.h>
1011

1112
#include <assert.h>
@@ -64,3 +65,7 @@ umaMemoryProviderGetLastResult(uma_memory_provider_handle_t hProvider,
6465
const char **ppMessage) {
6566
return hProvider->ops.get_last_result(hProvider->provider_priv, ppMessage);
6667
}
68+
69+
void *umaMemoryProviderGetPriv(uma_memory_provider_handle_t hProvider) {
70+
return hProvider->provider_priv;
71+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
*
3+
* Copyright (C) 2023 Intel Corporation
4+
*
5+
* SPDX-License-Identifier: MIT
6+
*
7+
*/
8+
9+
#ifndef UMA_MEMORY_PROVIDER_INTERNAL_H
10+
#define UMA_MEMORY_PROVIDER_INTERNAL_H 1
11+
12+
#include <uma/memory_provider.h>
13+
14+
#ifdef __cplusplus
15+
extern "C" {
16+
#endif
17+
18+
void *umaMemoryProviderGetPriv(uma_memory_provider_handle_t hProvider);
19+
20+
#ifdef __cplusplus
21+
}
22+
#endif
23+
24+
#endif /* UMA_MEMORY_PROVIDER_INTERNAL_H */

source/common/unified_memory_allocation/src/memory_tracker.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <uma/memory_provider.h>
1111
#include <uma/memory_provider_ops.h>
1212

13+
#include <cassert>
1314
#include <map>
1415
#include <mutex>
1516
#include <shared_mutex>
@@ -171,7 +172,8 @@ enum uma_result_t umaTrackingMemoryProviderCreate(
171172
uma_memory_provider_handle_t hUpstream, uma_memory_pool_handle_t hPool,
172173
uma_memory_provider_handle_t *hTrackingProvider) {
173174
uma_tracking_memory_provider_t params;
174-
params.hUpstream = hUpstream, params.hTracker = umaMemoryTrackerGet(),
175+
params.hUpstream = hUpstream;
176+
params.hTracker = umaMemoryTrackerGet();
175177
params.pool = hPool;
176178

177179
struct uma_memory_provider_ops_t trackingMemoryProviderOps;
@@ -185,4 +187,13 @@ enum uma_result_t umaTrackingMemoryProviderCreate(
185187
return umaMemoryProviderCreate(&trackingMemoryProviderOps, &params,
186188
hTrackingProvider);
187189
}
190+
191+
void umaTrackingMemoryProviderGetUpstreamProvider(
192+
uma_memory_provider_handle_t hTrackingProvider,
193+
uma_memory_provider_handle_t *hUpstream) {
194+
assert(hUpstream);
195+
uma_tracking_memory_provider_t *p =
196+
(uma_tracking_memory_provider_t *)hTrackingProvider;
197+
*hUpstream = p->hUpstream;
198+
}
188199
}

source/common/unified_memory_allocation/src/memory_tracker.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ enum uma_result_t umaTrackingMemoryProviderCreate(
2929
uma_memory_provider_handle_t hUpstream, uma_memory_pool_handle_t hPool,
3030
uma_memory_provider_handle_t *hTrackingProvider);
3131

32+
void umaTrackingMemoryProviderGetUpstreamProvider(
33+
uma_memory_provider_handle_t hTrackingProvider,
34+
uma_memory_provider_handle_t *hUpstream);
35+
3236
#ifdef __cplusplus
3337
}
3438
#endif

test/unified_memory_allocation/common/pool.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ struct malloc_pool : public pool_base {
6969
struct proxy_pool : public pool_base {
7070
uma_result_t initialize(uma_memory_provider_handle_t *providers,
7171
size_t numProviders) noexcept {
72-
EXPECT_EQ(numProviders, 1);
7372
this->provider = providers[0];
7473
return UMA_RESULT_SUCCESS;
7574
}

test/unified_memory_allocation/memoryPoolAPI.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "memoryPool.hpp"
1111

12+
#include <array>
1213
#include <string>
1314
#include <unordered_map>
1415

@@ -107,6 +108,28 @@ TEST_F(test, memoryPoolWithCustomProviders) {
107108
}
108109
}
109110

111+
TEST_F(test, retrieveMemoryProviders) {
112+
static constexpr size_t numProviders = 4;
113+
std::array<uma_memory_provider_handle_t, numProviders> providers = {
114+
(uma_memory_provider_handle_t)0x1, (uma_memory_provider_handle_t)0x2,
115+
(uma_memory_provider_handle_t)0x3, (uma_memory_provider_handle_t)0x4};
116+
117+
auto [ret, pool] = uma::poolMakeUnique<uma_test::proxy_pool>(
118+
providers.data(), numProviders);
119+
120+
std::array<uma_memory_provider_handle_t, numProviders> retProviders;
121+
size_t numProvidersRet = 0;
122+
123+
ret = umaPoolGetMemoryProviders(pool.get(), 0, nullptr, &numProvidersRet);
124+
ASSERT_EQ(ret, UMA_RESULT_SUCCESS);
125+
ASSERT_EQ(numProvidersRet, numProviders);
126+
127+
ret = umaPoolGetMemoryProviders(pool.get(), numProviders,
128+
retProviders.data(), nullptr);
129+
ASSERT_EQ(ret, UMA_RESULT_SUCCESS);
130+
ASSERT_EQ(retProviders, providers);
131+
}
132+
110133
template <typename Pool>
111134
static auto
112135
makePool(std::function<uma::provider_unique_handle_t()> makeProvider) {
@@ -185,3 +208,16 @@ TEST_P(poolInitializeTest, errorPropagation) {
185208
ASSERT_EQ(ret.first, this->GetParam());
186209
ASSERT_EQ(ret.second, nullptr);
187210
}
211+
212+
TEST_F(test, retrieveMemoryProvidersError) {
213+
static constexpr size_t numProviders = 4;
214+
std::array<uma_memory_provider_handle_t, numProviders> providers = {
215+
(uma_memory_provider_handle_t)0x1, (uma_memory_provider_handle_t)0x2,
216+
(uma_memory_provider_handle_t)0x3, (uma_memory_provider_handle_t)0x4};
217+
218+
auto [ret, pool] = uma::poolMakeUnique<uma_test::proxy_pool>(
219+
providers.data(), numProviders);
220+
221+
ret = umaPoolGetMemoryProviders(pool.get(), 1, providers.data(), nullptr);
222+
ASSERT_EQ(ret, UMA_RESULT_ERROR_INVALID_ARGUMENT);
223+
}

0 commit comments

Comments
 (0)