Skip to content

Conversation

lslusarczyk
Copy link
Contributor

@lslusarczyk lslusarczyk commented Sep 11, 2025

@lslusarczyk lslusarczyk requested a review from a team as a code owner September 11, 2025 12:30
hProvider->provider_priv, deviceIndex, isAdding);
checkErrorAndSetLastProvider(res, hProvider);
return res;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

add empty line

Copy link
Contributor Author

Choose a reason for hiding this comment

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

applied

@@ -802,6 +802,15 @@ static umf_result_t cu_memory_provider_get_allocation_properties_size(
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
}

static umf_result_t
Copy link
Contributor

Choose a reason for hiding this comment

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

add a "default" handler in memory_provider.c (see umfDefaultCtlHandle) and do not modify CUDA provider

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Where exactly in the code you think "default" is missing?

do not modify CUDA provid

Rejected. umf_memory_provider_ops_t type received a new field and it should be somehow initialized in UMF_CUDA_MEMORY_PROVIDER_OPS as all other fields. Please explain if you still want me to remove this change.

Copy link
Contributor

Choose a reason for hiding this comment

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

Please initialize cuda ops .ext_resident_device_change to NULL.
Next, define the umfDefaultResidentDeviceChange in src/memory_provider.c that returns UMF_RESULT_ERROR_NOT_SUPPORTED and set this handler for each created provider if the ext_resident_device_change is set to NULL (see how this is done for other default handlers like "umfDefaultCloseIPCHandle")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, applied

@@ -1336,6 +1336,15 @@ static umf_result_t trackingGetAllocationPropertiesSize(
p->hUpstream, memory_property_id, size);
}

static umf_result_t trackingResidentDeviceChange(void *provider,
Copy link
Contributor

Choose a reason for hiding this comment

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

remove

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Rejected. umf_memory_provider_ops_t type received a new field and it should be somehow initialized in UMF_TRACKING_MEMORY_PROVIDER_OPS as all other fields. Please explain if you still want me to remove this change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

applied in the same way as in cuda provider

@@ -162,6 +162,15 @@ static umf_result_t nullGetAllocationPropertiesSize(
return UMF_RESULT_SUCCESS;
}

static umf_result_t nullResidentDeviceChange(void *provider,
Copy link
Contributor

Choose a reason for hiding this comment

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

remove

Copy link
Contributor Author

@lslusarczyk lslusarczyk Sep 12, 2025

Choose a reason for hiding this comment

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

rejected, same reason as above

@bratpiorka bratpiorka requested review from lplewa and ldorau September 11, 2025 12:53
if (!hParams) {
LOG_ERR("Level Zero memory provider params handle is NULL");
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
}

if (deviceCount && !hDevices) {
LOG_ERR("Resident devices array is NULL, but deviceCount is not zero");
if (residentDevicesCount && !residentDevicesIndices) {
Copy link
Contributor

Choose a reason for hiding this comment

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

please also check if residentDevicesCount == 0 but residentDevicesIndices != NULL

Copy link
Contributor

Choose a reason for hiding this comment

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

additionally, should indices be unique?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, added checking for uniqueness

@@ -930,6 +987,155 @@ static umf_result_t ze_memory_provider_get_allocation_properties_size(
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
}

struct ze_memory_provider_resident_device_change_data {
bool isAdding;
Copy link
Contributor

Choose a reason for hiding this comment

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

is_adding

change_data->source_memory_provider
->device_handles[change_data->peer_device_index];

// TODO: add assertions to UMF and change it to be an assertion
Copy link
Contributor

Choose a reason for hiding this comment

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

use ASSERT() macros from src/utils/utils_assert.h

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Postponed to some future PR. I need here a permanent ASSERT, not a one that compiles on in debug config like ASSERT does.

Copy link
Contributor

Choose a reason for hiding this comment

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

you assert only things which are can be an artefact of some internal umf error - if it is true debug only assert is fine. If an error can be a result of an user input you just handle the issue and return a error to the user

info->props.base, info->props.base_size);
} else {
result = ZE_RESULT_SUCCESS;
// TODO: currently not implemented call evict here
Copy link
Contributor

Choose a reason for hiding this comment

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

do you plan to add the missing code here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not in this PR.

if (ze_provider->resident_device_count == 0 ||
existing_peer_index == ze_provider->resident_device_count) {
// not found
if (!isAdding) { // impossible for UR, should be an assertion
Copy link
Contributor

Choose a reason for hiding this comment

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

if this should be an assertion, please use the ASSERT() macro from src/utils/utils_assert.h
Also, move the comment to the next line

Copy link
Contributor Author

@lslusarczyk lslusarczyk Sep 15, 2025

Choose a reason for hiding this comment

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

To be changed in future PR when umf will have permanent assertions. Comments moved.


static umf_result_t
ze_memory_provider_resident_device_change(void *provider, uint32_t device_index,
bool isAdding) {
Copy link
Contributor

Choose a reason for hiding this comment

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

is_adding

// adding case
if (ze_provider->device_count <=
ze_provider
->resident_device_count) { // impossible for UR, should be an assertion
Copy link
Contributor

Choose a reason for hiding this comment

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

if this should be an assertion, please use the ASSERT() macro from src/utils/utils_assert.h
Also, move the comment to the next line

Copy link
Contributor Author

Choose a reason for hiding this comment

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

postponed when UMF have permanent assertions, comments moved

}

if (ze_provider->device_count <=
device_index) { // impossible for UR, should be an assertion
Copy link
Contributor

Choose a reason for hiding this comment

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

.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

applied as above


} else {
// found
if (isAdding) { // impossible for UR, should be an assertion
Copy link
Contributor

Choose a reason for hiding this comment

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

.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

applied as above

LOG_ERR("umfMemoryTrackerIterateAll did not manage to do some change "
"numFailed:%d, numSuccess:%d",
privData.success_changes, privData.failed_changes);
return UMF_RESULT_ERROR_INVALID_ARGUMENT; // probably some other result is better, best just change into assertion
Copy link
Contributor

Choose a reason for hiding this comment

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

you could use UMF_RESULT_ERROR_MEMORY_PROVIDER_SPECIFIC
or change to assert

src/libumf.map Outdated
@@ -54,6 +54,7 @@ UMF_1.0 {
umfMemoryProviderPurgeForce;
umfMemoryProviderPurgeLazy;
umfMemoryProviderPutIPCHandle;
umfMemoryProviderResidentDeviceChange;
Copy link
Contributor

Choose a reason for hiding this comment

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

pls move down in the file (after a comment # Added in UMF_1.0)

Copy link
Contributor

Choose a reason for hiding this comment

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

actually, mea culpa, pls make a new sction, we're already past v1.0 - we should have now # Added in UMF_1.1 section

@@ -60,6 +60,7 @@ EXPORTS
umfMemoryProviderPurgeForce
umfMemoryProviderPurgeLazy
umfMemoryProviderPutIPCHandle
umfMemoryProviderResidentDeviceChange
Copy link
Contributor

Choose a reason for hiding this comment

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

pls move down in the file (after a comment ; Added in UMF_1.0)

Copy link
Contributor

Choose a reason for hiding this comment

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

  1. first of all, wrong function was moved - umfMemoryProviderResidentDeviceChange is the new one
  2. mea culpa, it should be already in ; Added in UMF_1.1 section

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved down in 01d74ba

@@ -606,3 +619,16 @@ umf_result_t umfMemoryProviderGetAllocationPropertiesSize(
checkErrorAndSetLastProvider(res, hProvider);
return res;
}

umf_result_t
umfMemoryProviderResidentDeviceChange(umf_memory_provider_handle_t hProvider,
Copy link
Contributor

Choose a reason for hiding this comment

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

do we have any tests for this function? including negative tests

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no, success path will be tested by UR tests
failure path does not happen as UR is the only one using this functionality and it never issues wrong parameters

If you think some test case is important and possible to test within UMF please write which case and please guide me where to add it and what other test to use as example.

Copy link
Contributor

Choose a reason for hiding this comment

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

UR is an separate project - we need at least few simple test cases to do not decrease code coverage.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, I've added some tests in 88f672b

Copy link
Contributor

Choose a reason for hiding this comment

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

still missing tests for new functions, even simple negative test would be better than nothing (you can put them in generic API tests: test/memoryProviderAPI.cpp)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, applied, In 53ec753 I've added UTs covering all new code paths.

Comment on lines 326 to 335
/// @brief Adds or removes devices on which allocations should be made
/// resident.
/// @param provider handle to the memory provider
/// @param device_index identifier of device
/// @param is_adding Boolean indicating if peer is to be removed or added
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on
/// failure.
umf_result_t (*ext_resident_device_change)(void *provider,
uint32_t device_index,
bool is_adding);

Copy link
Contributor

Choose a reason for hiding this comment

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

This function should not be in OPS. This is something very specific to L0 provider. Resident devices are passed to L0 provider thru provider specific params, so control of them should be olso be done through provider specific API, this is why i think with should be implemented through CTL

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Rejected. We discussed this in teams (4th Aug, 25) we do it as ops (Piotr, sorry in Polish): co do tego API co Ty robisz (w stylu daj mi wszystkie zaalokowane page'e) ja bym chyba sugerował zrobić API a nie robić przez CTLa, Rafał: no to faktycznie chyba dedykowane API lepiej pasuje niż CTL.

CTL should be for statistics, not a universal, hard-to-read tool to implement any API, Łukasz (me): Być może będziecie chcieli uprościć ext_ctl by nie był maszynką, którą mozna zaimplementować wszystko, a jedynie służył do statystyk. Ale to już ja się na tym nie znam - zostawiam do przemyśleń.

Copy link
Contributor

@lplewa lplewa Sep 15, 2025

Choose a reason for hiding this comment

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

We talked about API to iterate over all allocations, not about API to modify some internal settings of the L0

Copy link
Contributor

Choose a reason for hiding this comment

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

I can see it both ways. My comment during that discussion was about a generic functionality implementable for all providers. This isn't that.
I also remember making a point about CTL being useful for provider/pool-specific functionality.

On the other hand, adding an API function is simpler.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

On the other hand, adding an API function is simpler.

Thank you for the comment. Chosen simpler way.

Comment on lines 62 to 72
/// @brief Set the resident devices in the parameters struct.
/// @param hParams handle to the parameters of the Level Zero Memory Provider.
/// @param hDevices array of devices for which the memory should be made resident.
/// @param deviceCount number of devices for which the memory should be made resident.
/// @param hDevices array of all devices for which the memory can be made resident.
/// @param deviceCount number of devices for which the memory can be made resident.
/// @param residentDevicesIndices array of indices in all devices array to devices for which the memory should be made resident.
/// @param residentDevicesCount number of items in indices array.
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
umf_result_t umfLevelZeroMemoryProviderParamsSetResidentDevices(
umf_level_zero_memory_provider_params_handle_t hParams,
ze_device_handle_t *hDevices, uint32_t deviceCount);
ze_device_handle_t *hDevices, uint32_t deviceCount,
uint32_t *residentDevicesIndices, uint32_t residentDevicesCount);
Copy link
Contributor

Choose a reason for hiding this comment

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

This is an API(and ABI) break - we are post 1.0 release so you cannot do changes like this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Shall I bump some number? Only UR uses this API and I am changing UR right now as well.

Copy link
Contributor

Choose a reason for hiding this comment

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

Even if UR uses only this API, old UR should work with new umf. And we will not do 2.0 release just after 1.0.

You cannot brake API. If changes are needed we have to keep old function working correctly and add new one.

Copy link
Contributor

Choose a reason for hiding this comment

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

We can't break backwards compatibility with SYCL / UR. Changing a major version is a significant undertaking that involves updating the components across all the different layers (UMF is nearly the lowest-most component in the stack). We've been burned on this in the past, and it's very disruptive.

In this case, my suggestion would be to simply create a new function that sets the indices.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, applied, I rewrote code in 53ec753 to be backward compatible

Copy link
Contributor

@ldorau ldorau left a comment

Choose a reason for hiding this comment

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

14 / 16 files reviewed (will be continued)

/// failure.
umf_result_t (*ext_resident_device_change)(void *provider,
uint32_t device_index,
bool is_adding);
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd rather we have two functions - add/remove.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will not apply this, please, because this will introduce too much copy-pasted code.

@lplewa
Copy link
Contributor

lplewa commented Sep 19, 2025

I still oppose against adding provider specific code to ops interface.
If you don't want to do it thru CTL just implement it as umfLevelZeroMemoryProviderResidentDeviceChange() in provider_level_zero.h which do not go throught OPS. This function will act as an "level zero specific api extension".

note: remember that you will have to call umfMemoryProviderGetPriv() in this function to retrieve ze_memory_provider_t from provider handle

@@ -19,6 +19,7 @@ EXPORTS
umfCUDAMemoryProviderParamsSetContext
umfCUDAMemoryProviderParamsSetDevice
umfCUDAMemoryProviderParamsSetMemoryType
umfLevelZeroMemoryProviderParamsSetResidentDevices
Copy link
Contributor

Choose a reason for hiding this comment

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

please keep alphabetical order

@@ -174,11 +177,18 @@ static ze_memory_type_t umf2ze_memory_type(umf_usm_memory_type_t memory_type) {
}

static void init_ze_global_state(void) {

char *lib_name = getenv("UMF_ZE_LOADER_LIB_NAME");
Copy link
Contributor

Choose a reason for hiding this comment

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

why we need this code? will this code path be used by UR? if not please remove this

@@ -174,11 +177,18 @@ static ze_memory_type_t umf2ze_memory_type(umf_usm_memory_type_t memory_type) {
}

static void init_ze_global_state(void) {

char *lib_name = getenv("UMF_ZE_LOADER_LIB_NAME");
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you do this thru CTL please.

We created CTL to limit new envvariables.

Comment on lines 380 to 382
hParams->resident_device_handles = hDevices;
hParams->resident_device_count = deviceCount;

Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't we copy this array? - user after call to this function can do anything with this array.

ze_result = g_ze_ops.zeContextMakeMemoryResident(
ze_provider->context, ze_provider->resident_device_handles[i],
*resultPtr, size);
if (ze_result != ZE_RESULT_SUCCESS) {
utils_read_unlock(&ze_provider->resident_device_rwlock);
Copy link
Contributor

Choose a reason for hiding this comment

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

If we return an failure then it's means allocation failed, but allocation is still "done". IMHO we should free it here.

change_data->source_memory_provider
->device_handles[change_data->peer_device_index];

// TODO: add assertions to UMF and change it to be an assertion
Copy link
Contributor

Choose a reason for hiding this comment

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

you assert only things which are can be an artefact of some internal umf error - if it is true debug only assert is fine. If an error can be a result of an user input you just handle the issue and return a error to the user


// TODO: add assertions to UMF and change it to be an assertion
if (info->props.base != (void *)key) {
LOG_ERR("key:%p is different than base:%p", (void *)key,
Copy link
Contributor

Choose a reason for hiding this comment

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

LOG_FATAL

Comment on lines -27 to +45
#define LOG_DEBUG(...) utils_log(LOG_DEBUG, __func__, __VA_ARGS__);
#define LOG_INFO(...) utils_log(LOG_INFO, __func__, __VA_ARGS__);
#define LOG_WARN(...) utils_log(LOG_WARNING, __func__, __VA_ARGS__);
#define LOG_ERR(...) utils_log(LOG_ERROR, __func__, __VA_ARGS__);
#define LOG_FATAL(...) utils_log(LOG_FATAL, __func__, __VA_ARGS__);

#define LOG_PDEBUG(...) utils_plog(LOG_DEBUG, __func__, __VA_ARGS__);
#define LOG_PINFO(...) utils_plog(LOG_INFO, __func__, __VA_ARGS__);
#define LOG_PWARN(...) utils_plog(LOG_WARNING, __func__, __VA_ARGS__);
#define LOG_PERR(...) utils_plog(LOG_ERROR, __func__, __VA_ARGS__);
#define LOG_PFATAL(...) utils_plog(LOG_FATAL, __func__, __VA_ARGS__);
#ifdef UMF_DEVELOPER_MODE
#define UMF_STRINGIFY(x) #x
#define UMF_TOSTRING(x) UMF_STRINGIFY(x)
#define UMF_FUNC_DESC() __FILE__ ":" UMF_TOSTRING(__LINE__)
#else
#define UMF_FUNC_DESC() __func__
#endif

#define LOG_DEBUG(...) utils_log(LOG_DEBUG, UMF_FUNC_DESC(), __VA_ARGS__);
#define LOG_INFO(...) utils_log(LOG_INFO, UMF_FUNC_DESC(), __VA_ARGS__);
#define LOG_WARN(...) utils_log(LOG_WARNING, UMF_FUNC_DESC(), __VA_ARGS__);
#define LOG_ERR(...) utils_log(LOG_ERROR, UMF_FUNC_DESC(), __VA_ARGS__);
#define LOG_FATAL(...) utils_log(LOG_FATAL, UMF_FUNC_DESC(), __VA_ARGS__);

#define LOG_PDEBUG(...) utils_plog(LOG_DEBUG, UMF_FUNC_DESC(), __VA_ARGS__);
#define LOG_PINFO(...) utils_plog(LOG_INFO, UMF_FUNC_DESC(), __VA_ARGS__);
#define LOG_PWARN(...) utils_plog(LOG_WARNING, UMF_FUNC_DESC(), __VA_ARGS__);
#define LOG_PERR(...) utils_plog(LOG_ERROR, UMF_FUNC_DESC(), __VA_ARGS__);
#define LOG_PFATAL(...) utils_plog(LOG_FATAL, UMF_FUNC_DESC(), __VA_ARGS__);
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be a separate PR.

There are comments which i whould make - but i strongly believe that this discussion should happen in the separate PR to do not block this PR

// found
if (is_adding) {
utils_write_unlock(&ze_provider->resident_device_rwlock);
// impossible for UR, should be an assertion
Copy link
Contributor

Choose a reason for hiding this comment

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

imho we should not assume that user will not do something then is should be an assertion. If it comes it for any kind of user it should be validated an correct error should be returned


uint32_t existing_peer_index = 0;
utils_write_lock(&ze_provider->resident_device_rwlock);
while (existing_peer_index < ze_provider->resident_device_count &&
Copy link
Contributor

Choose a reason for hiding this comment

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

NIT: this code whould be much easier to read if it whould be a for loop

for (index = 0; index < count; index++) if (device[index] == index) break;

Comment on lines +1062 to +1065
const uint32_t new_capacity =
ze_provider->resident_device_capacity * 2 +
1; // +1 to work also with old capacity == 0
ze_device_handle_t *new_handles =
Copy link
Contributor

Choose a reason for hiding this comment

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

Do this code is performance critical, or will be called multiple times

If not i would prefer to have this array just always reallocated by one,
If yes can we abstract this vector like structure to separate module, or even better just use list structure with is included in umf.

.failed_changes = 0,
};

umf_result_t result = umfMemoryTrackerIterateAll(
Copy link
Contributor

Choose a reason for hiding this comment

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

This is "hacky" and it will not work if someone uses pool without tracker or just use provider without pool.

We allready solved this issue in other providers were we keep track allocation by the provider (for example please check os_provider)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants