Skip to content

[WIP] mem props #1301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
393 changes: 168 additions & 225 deletions benchmark/ubench.c

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions docs/config/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ memory as well as functions that create, destroy and operate on the pool.
.. doxygenfile:: memory_pool.h
:sections: define enum typedef func var

TODO
------------------------------------------

TODO

.. doxygenfile:: memory_props.h
:sections: define enum typedef func var

Disjoint Pool
------------------------------------------

Expand Down
67 changes: 67 additions & 0 deletions include/umf/memory_props.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
*
* Copyright (C) 2025 Intel Corporation
*
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*
*/

#ifndef UMF_MEMORY_PROPS_H
#define UMF_MEMORY_PROPS_H 1

#include <umf/base.h>
#include <umf/memory_pool.h>

#ifdef __cplusplus
extern "C" {
#endif

/// @brief TODO
// write about experimental api
typedef enum umf_memory_property_id_t {
UMF_MEMORY_PROPERTY_INVALID = -1, ///< TODO

// UMF specyfic
UMF_MEMORY_PROVIDER_HANDLE,
UMF_MEMORY_PROVIDER_OPS, // == type?
UMF_MEMORY_POOL_HANDLE,
UMF_MEMORY_POOL_OPS, // == type?

// generic pointer properties
UMF_MEMORY_PROPERTY_POINTER_TYPE, // unreg host, reg host ??, dev, managed or umf_usm_memory_type_t?
UMF_MEMORY_PROPERTY_BASE_ADDRESS, // base address
UMF_MEMORY_PROPERTY_BASE_SIZE, // base size

// GPU specyfic
UMF_MEMORY_PROPERTY_DEVICE, // handle (ze) or id (cuda)
UMF_MEMORY_PROPERTY_BUFFER_ID, // unique id NOTE: this id is unique across all UMF allocs and != L0 or CUDA ID
UMF_MEMORY_PROPERTY_DEVICE_ATTRIBUTES, // ze_memory_allocation_properties_t ?

// all cuda + l0
// next other providers?
// todo return type?

/// @cond
UMF_MEMORY_PROPERTY_MAX_RESERVED = 0x1000, ///< Maximum reserved value
/// @endcond

} umf_memory_property_id_t;

typedef struct umf_memory_properties_t *umf_memory_properties_handle_t;

/// @brief TODO
umf_result_t
umfGetMemoryPropertiesHandle(void *ptr,
umf_memory_properties_handle_t *props_handle);

/// @brief TODO
umf_result_t umfGetMemoryProperty(umf_memory_properties_handle_t props_handle,
umf_memory_property_id_t memory_property_id,
void *value);

#ifdef __cplusplus
}
#endif

#endif /* UMF_MEMORY_PROPS_H */
6 changes: 6 additions & 0 deletions include/umf/providers/provider_cuda.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#ifndef UMF_PROVIDER_CUDA_H
#define UMF_PROVIDER_CUDA_H

#include <umf/memory_props.h>
#include <umf/memory_provider_gpu.h>

#ifdef __cplusplus
Expand Down Expand Up @@ -61,6 +62,11 @@ umf_result_t umfCUDAMemoryProviderParamsSetMemoryType(
umf_result_t umfCUDAMemoryProviderParamsSetAllocFlags(
umf_cuda_memory_provider_params_handle_t hParams, unsigned int flags);

/// TODO
umf_result_t umfCUDAMemoryProviderGetMemoryProperty(
umf_memory_properties_handle_t props_handle,
umf_memory_property_id_t memory_property_id, void *value);

const umf_memory_provider_ops_t *umfCUDAMemoryProviderOps(void);

#ifdef __cplusplus
Expand Down
6 changes: 6 additions & 0 deletions include/umf/providers/provider_level_zero.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#ifndef UMF_PROVIDER_LEVEL_ZERO_H
#define UMF_PROVIDER_LEVEL_ZERO_H

#include <umf/memory_props.h>
#include <umf/memory_provider_gpu.h>

#ifdef __cplusplus
Expand Down Expand Up @@ -91,6 +92,11 @@ umf_result_t umfLevelZeroMemoryProviderParamsSetDeviceOrdinal(
umf_level_zero_memory_provider_params_handle_t hParams,
uint32_t deviceOrdinal);

/// TODO
umf_result_t umfLevelZeroMemoryProviderGetMemoryProperty(
umf_memory_properties_handle_t props_handle,
umf_memory_property_id_t memory_property_id, void *value);

const umf_memory_provider_ops_t *umfLevelZeroMemoryProviderOps(void);

#ifdef __cplusplus
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ set(UMF_SOURCES
ipc.c
ipc_cache.c
memory_pool.c
memory_props.c
memory_provider.c
memory_provider_get_last_failed.c
memtarget.c
Expand Down
4 changes: 2 additions & 2 deletions src/ipc.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ umf_result_t umfGetIPCHandle(const void *ptr, umf_ipc_handle_t *umfIPCHandle,
return ret;
}

ret = umfPoolGetIPCHandleSize(allocInfo.pool, &ipcHandleSize);
ret = umfPoolGetIPCHandleSize(allocInfo.props->pool, &ipcHandleSize);
if (ret != UMF_RESULT_SUCCESS) {
LOG_ERR("cannot get IPC handle size.");
return ret;
Expand All @@ -79,7 +79,7 @@ umf_result_t umfGetIPCHandle(const void *ptr, umf_ipc_handle_t *umfIPCHandle,

// We cannot use umfPoolGetMemoryProvider function because it returns
// upstream provider but we need tracking one
umf_memory_provider_handle_t provider = allocInfo.pool->provider;
umf_memory_provider_handle_t provider = allocInfo.props->pool->provider;
assert(provider);

ret = umfMemoryProviderGetIPCHandle(provider, allocInfo.base,
Expand Down
2 changes: 2 additions & 0 deletions src/libumf.def
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ EXPORTS
umfCtlExec
umfCtlGet
umfCtlSet
umfGetMemoryPropertiesHandle
umfGetMemoryProperty
umfJemallocPoolParamsCreate
umfJemallocPoolParamsDestroy
umfJemallocPoolParamsSetNumArenas
2 changes: 2 additions & 0 deletions src/libumf.map
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ UMF_0.12 {
umfCtlExec;
umfCtlGet;
umfCtlSet;
umfGetMemoryPropertiesHandle;
umfGetMemoryProperty;
umfJemallocPoolParamsCreate;
umfJemallocPoolParamsDestroy;
umfJemallocPoolParamsSetNumArenas;
Expand Down
81 changes: 81 additions & 0 deletions src/memory_props.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
*
* Copyright (C) 2025 Intel Corporation
*
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*
*/

#include <umf/memory_props.h>
#include <umf/providers/provider_cuda.h>
#include <umf/providers/provider_level_zero.h>

#include "memory_props_internal.h"
#include "memory_provider_internal.h"
#include "provider/provider_tracking.h"

umf_result_t
umfGetMemoryPropertiesHandle(void *ptr,
umf_memory_properties_handle_t *props_handle) {
umf_alloc_info_t allocInfo = {NULL, 0, NULL};
umf_result_t ret = umfMemoryTrackerGetAllocInfo(ptr, &allocInfo);
if (ret != UMF_RESULT_SUCCESS) {
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
}

*props_handle = allocInfo.props;
return UMF_RESULT_SUCCESS;
}

umf_result_t umfGetMemoryProperty(umf_memory_properties_handle_t props_handle,
umf_memory_property_id_t memory_property_id,
void *value) {
if ((value == NULL) ||
(memory_property_id == UMF_MEMORY_PROPERTY_INVALID) ||
(memory_property_id >= UMF_MEMORY_PROPERTY_MAX_RESERVED) ||
props_handle == NULL) {
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
}

switch (memory_property_id) {
case UMF_MEMORY_POOL_HANDLE:
*(umf_memory_pool_handle_t *)value = props_handle->pool;
return UMF_RESULT_SUCCESS;

case UMF_MEMORY_PROPERTY_BUFFER_ID:
*(uint64_t *)value = props_handle->id;
return UMF_RESULT_SUCCESS;

default:
break;
}

// properties that are related to the memory provider
umf_memory_provider_t *provider = NULL;
umfPoolGetMemoryProvider(props_handle->pool, &provider);
assert(provider != NULL);

switch (memory_property_id) {
// GPU Memory Provider specyfic properties
case UMF_MEMORY_PROPERTY_POINTER_TYPE:
case UMF_MEMORY_PROPERTY_DEVICE:
case UMF_MEMORY_PROPERTY_DEVICE_ATTRIBUTES:
if (provider->ops.get_name(provider) ==
umfLevelZeroMemoryProviderOps()->get_name(provider)) {
return umfLevelZeroMemoryProviderGetMemoryProperty(
props_handle, memory_property_id, value);
} else if (provider->ops.get_name(provider) ==
umfCUDAMemoryProviderOps()->get_name(provider)) {
return umfCUDAMemoryProviderGetMemoryProperty(
props_handle, memory_property_id, value);
}
// no provider found
return UMF_RESULT_ERROR_UNKNOWN;

default:
break;
}

return UMF_RESULT_ERROR_NOT_SUPPORTED;
}
45 changes: 45 additions & 0 deletions src/memory_props_internal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
*
* Copyright (C) 2025 Intel Corporation
*
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*
*/

#ifndef UMF_MEMORY_PROPS_INTERNAL_H
#define UMF_MEMORY_PROPS_INTERNAL_H 1

#include <stdbool.h>

#include <umf/base.h>
#include <umf/memory_props.h>

#if UMF_BUILD_LEVEL_ZERO_PROVIDER
#include "ze_api.h"
#endif

#ifdef __cplusplus
extern "C" {
#endif

typedef struct umf_memory_properties_t {
void *ptr;
umf_memory_pool_handle_t pool;
uint64_t id;

// TODO
bool gpu_properties_initialized;
union {
#if UMF_BUILD_LEVEL_ZERO_PROVIDER
ze_memory_allocation_properties_t ze_properties;
#endif
int unused; // in case of no GPU support
} gpu;
} umf_memory_properties_t;

#ifdef __cplusplus
}
#endif

#endif // UMF_MEMORY_PROPS_INTERNAL_H
2 changes: 1 addition & 1 deletion src/memory_provider.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
#include <stdio.h>
#include <stdlib.h>

#include <umf/base.h>
#include <umf/memory_provider.h>

#include "base_alloc.h"
#include "base_alloc_global.h"
#include "libumf.h"
#include "memory_provider_internal.h"
#include "umf/base.h"
#include "utils_assert.h"

static int CTL_SUBTREE_HANDLER(by_handle_provider)(
Expand Down
44 changes: 44 additions & 0 deletions src/provider/provider_cuda.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include <umf.h>
#include <umf/providers/provider_cuda.h>

#include "memory_props_internal.h"
#include "memory_provider_internal.h"
#include "provider_cuda_internal.h"
#include "utils_load_library.h"
#include "utils_log.h"
Expand Down Expand Up @@ -704,6 +706,39 @@ const umf_memory_provider_ops_t *umfCUDAMemoryProviderOps(void) {
return &UMF_CUDA_MEMORY_PROVIDER_OPS;
}

umf_result_t umfCUDAMemoryProviderGetMemoryProperty(
umf_memory_properties_handle_t props_handle,
umf_memory_property_id_t memory_property_id, void *value) {

umf_memory_provider_t *provider = NULL;
umfPoolGetMemoryProvider(props_handle->pool, &provider);
assert(provider != NULL);
assert(provider->ops.get_name(provider) ==
umfCUDAMemoryProviderOps()->get_name(provider));

cu_memory_provider_t *cuda_provider =
(cu_memory_provider_t *)provider->provider_priv;

switch (memory_property_id) {
case UMF_MEMORY_PROPERTY_POINTER_TYPE:
*(umf_usm_memory_type_t *)value = cuda_provider->memory_type;
return UMF_RESULT_SUCCESS;

case UMF_MEMORY_PROPERTY_DEVICE:
*(CUdevice *)value = cuda_provider->device;
return UMF_RESULT_SUCCESS;

case UMF_MEMORY_PROPERTY_DEVICE_ATTRIBUTES:
assert(0); // TODO
return UMF_RESULT_ERROR_NOT_SUPPORTED;

default:
break;
}

return UMF_RESULT_ERROR_NOT_SUPPORTED;
}

#else // !UMF_BUILD_CUDA_PROVIDER

umf_result_t umfCUDAMemoryProviderParamsCreate(
Expand Down Expand Up @@ -753,6 +788,15 @@ umf_result_t umfCUDAMemoryProviderParamsSetAllocFlags(
return UMF_RESULT_ERROR_NOT_SUPPORTED;
}

umf_result_t umfCUDAMemoryProviderGetMemoryProperty(
umf_memory_properties_handle_t props_handle,
umf_memory_property_id_t memory_property_id, void *value) {
(void)props_handle;
(void)memory_property_id;
(void)value;
return UMF_RESULT_ERROR_NOT_SUPPORTED;
}

const umf_memory_provider_ops_t *umfCUDAMemoryProviderOps(void) {
// not supported
LOG_ERR("CUDA provider is disabled (UMF_BUILD_CUDA_PROVIDER is OFF)!");
Expand Down
Loading
Loading