-
Notifications
You must be signed in to change notification settings - Fork 24
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
add memspace filter functions #743
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -426,3 +426,146 @@ umfMemspaceMemtargetRemove(umf_memspace_handle_t hMemspace, | |
hMemspace->size--; | ||
return UMF_RESULT_SUCCESS; | ||
} | ||
|
||
// Helper function - returns zero on success, negative in case of error in filter function | ||
// and positive error code, in case of other errors. | ||
static int umfMemspaceFilterHelper(umf_memspace_handle_t memspace, | ||
umf_memspace_filter_func_t filter, | ||
void *args) { | ||
|
||
if (!memspace || !filter) { | ||
return UMF_RESULT_ERROR_INVALID_ARGUMENT; | ||
} | ||
|
||
size_t idx = 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add empty line after There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
int ret; | ||
umf_memtarget_handle_t *nodesToRemove = | ||
umf_ba_global_alloc(sizeof(*nodesToRemove) * memspace->size); | ||
if (!nodesToRemove) { | ||
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY; | ||
} | ||
|
||
for (size_t i = 0; i < memspace->size; i++) { | ||
ret = filter(memspace, memspace->nodes[i], args); | ||
if (ret < 0) { | ||
LOG_ERR("filter function failed"); | ||
goto free_mem; | ||
} else if (ret == 0) { | ||
nodesToRemove[idx++] = memspace->nodes[i]; | ||
} | ||
} | ||
|
||
size_t i = 0; | ||
for (; i < idx; i++) { | ||
ret = umfMemspaceMemtargetRemove(memspace, nodesToRemove[i]); | ||
if (ret != UMF_RESULT_SUCCESS) { | ||
goto re_add; | ||
bratpiorka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
umf_ba_global_free(nodesToRemove); | ||
return UMF_RESULT_SUCCESS; | ||
|
||
re_add: | ||
// If target removal failed, add back previously removed targets. | ||
for (size_t j = 0; j < i; j++) { | ||
umf_result_t ret2 = umfMemspaceMemtargetAdd(memspace, nodesToRemove[j]); | ||
if (ret2 != UMF_RESULT_SUCCESS) { | ||
ret = | ||
UMF_RESULT_ERROR_UNKNOWN; // indicate that memspace is corrupted | ||
break; | ||
} | ||
} | ||
free_mem: | ||
umf_ba_global_free(nodesToRemove); | ||
return ret; | ||
} | ||
|
||
umf_result_t umfMemspaceUserFilter(umf_memspace_handle_t memspace, | ||
umf_memspace_filter_func_t filter, | ||
void *args) { | ||
|
||
if (!memspace || !filter) { | ||
return UMF_RESULT_ERROR_INVALID_ARGUMENT; | ||
} | ||
|
||
int ret = umfMemspaceFilterHelper(memspace, filter, args); | ||
if (ret < 0) { | ||
return UMF_RESULT_ERROR_USER_SPECIFIC; | ||
} | ||
|
||
return ret; | ||
} | ||
|
||
typedef struct filter_by_id_args { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a comment here |
||
unsigned *ids; // array of numa nodes ids | ||
size_t size; // size of the array | ||
} filter_by_id_args_t; | ||
|
||
/* | ||
* The following predefined filter callbacks returns umf_result_t codes as negative value | ||
* as negative only value are treated as an error. umfMemspaceFilter will passthru this error code | ||
* so and calling filter function has to translate this code to positive umf_result_t code. | ||
*/ | ||
|
||
static int filterById(umf_const_memspace_handle_t memspace, | ||
umf_const_memtarget_handle_t target, void *args) { | ||
if (!memspace || !target || !args) { | ||
return -UMF_RESULT_ERROR_INVALID_ARGUMENT; | ||
bratpiorka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
filter_by_id_args_t *filterArgs = args; | ||
for (size_t i = 0; i < filterArgs->size; i++) { | ||
unsigned id; | ||
umf_result_t ret = umfMemtargetGetId(target, &id); | ||
if (ret != UMF_RESULT_SUCCESS) { | ||
return -ret; | ||
} | ||
|
||
if (id == filterArgs->ids[i]) { | ||
return 1; | ||
} | ||
} | ||
return 0; | ||
} | ||
|
||
static int filterByCapacity(umf_const_memspace_handle_t memspace, | ||
umf_const_memtarget_handle_t target, void *args) { | ||
if (!memspace || !target || !args) { | ||
return -UMF_RESULT_ERROR_INVALID_ARGUMENT; | ||
} | ||
|
||
size_t capacity; | ||
umf_result_t ret = umfMemtargetGetCapacity(target, &capacity); | ||
if (ret != UMF_RESULT_SUCCESS) { | ||
return -ret; | ||
} | ||
|
||
size_t *targetCapacity = args; | ||
return (capacity >= *targetCapacity) ? 1 : 0; | ||
} | ||
|
||
umf_result_t umfMemspaceFilterById(umf_memspace_handle_t memspace, | ||
unsigned *ids, size_t size) { | ||
if (!memspace || !ids || size == 0) { | ||
return UMF_RESULT_ERROR_INVALID_ARGUMENT; | ||
} | ||
|
||
filter_by_id_args_t args = {ids, size}; | ||
int ret = umfMemspaceFilterHelper(memspace, &filterById, &args); | ||
|
||
// if umfMemspaceFilter() returned negative umf_result_t change it to positive | ||
return ret < 0 ? -ret : ret; | ||
bratpiorka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
umf_result_t umfMemspaceFilterByCapacity(umf_memspace_handle_t memspace, | ||
size_t capacity) { | ||
if (!memspace) { | ||
return UMF_RESULT_ERROR_INVALID_ARGUMENT; | ||
} | ||
|
||
int ret = umfMemspaceFilterHelper(memspace, &filterByCapacity, &capacity); | ||
|
||
// if umfMemspaceFilter() returned negative umf_result_t change it to positive | ||
return ret < 0 ? -ret : ret; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so maybe
umfMemspaceFilterByCapacityLT
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i don't know..... I do not like this LT. Looks to much like assert.