Skip to content

Commit ce4652d

Browse files
authored
Merge pull request #620 from bgoglin/master
add hwloc_topology_free_group() and fix/document when groups are freed
2 parents e22f340 + cbb876a commit ce4652d

File tree

5 files changed

+56
-2
lines changed

5 files changed

+56
-2
lines changed

doc/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@ man3_tinker_DATA = \
466466
$(DOX_MAN_DIR)/man3/hwloc_topology_allow.3 \
467467
$(DOX_MAN_DIR)/man3/hwloc_topology_insert_misc_object.3 \
468468
$(DOX_MAN_DIR)/man3/hwloc_topology_alloc_group_object.3 \
469+
$(DOX_MAN_DIR)/man3/hwloc_topology_free_group_object.3 \
469470
$(DOX_MAN_DIR)/man3/hwloc_topology_insert_group_object.3 \
470471
$(DOX_MAN_DIR)/man3/hwloc_obj_add_other_obj_sets.3 \
471472
$(DOX_MAN_DIR)/man3/hwloc_topology_refresh.3

hwloc/topology.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2031,6 +2031,22 @@ hwloc_topology_alloc_group_object(struct hwloc_topology *topology)
20312031
return hwloc_alloc_setup_object(topology, HWLOC_OBJ_GROUP, HWLOC_UNKNOWN_INDEX);
20322032
}
20332033

2034+
int
2035+
hwloc_topology_free_group_object(struct hwloc_topology *topology, hwloc_obj_t obj)
2036+
{
2037+
if (!(topology->state & HWLOC_TOPOLOGY_STATE_IS_LOADED)) {
2038+
/* this could actually work when IS_LOADING, see insert() below */
2039+
errno = EINVAL;
2040+
return -1;
2041+
}
2042+
if (topology->adopted_shmem_addr) {
2043+
errno = EPERM;
2044+
return -1;
2045+
}
2046+
hwloc_free_unlinked_object(obj);
2047+
return 0;
2048+
}
2049+
20342050
static void hwloc_propagate_symmetric_subtree(hwloc_topology_t topology, hwloc_obj_t root);
20352051
static void propagate_total_memory(hwloc_obj_t obj);
20362052
static void hwloc_set_group_depth(hwloc_topology_t topology);
@@ -2051,6 +2067,7 @@ hwloc_topology_insert_group_object(struct hwloc_topology *topology, hwloc_obj_t
20512067
return NULL;
20522068
}
20532069
if (topology->adopted_shmem_addr) {
2070+
hwloc_free_unlinked_object(obj);
20542071
errno = EPERM;
20552072
return NULL;
20562073
}
@@ -2104,6 +2121,7 @@ hwloc_topology_insert_group_object(struct hwloc_topology *topology, hwloc_obj_t
21042121
res = hwloc__insert_object_by_cpuset(topology, NULL, obj, NULL /* do not show errors on stdout */);
21052122
} else {
21062123
/* just merge root */
2124+
hwloc_free_unlinked_object(obj);
21072125
res = root;
21082126
}
21092127

include/hwloc.h

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2793,9 +2793,27 @@ HWLOC_DECLSPEC hwloc_obj_t hwloc_topology_insert_misc_object(hwloc_topology_t to
27932793
*
27942794
* \return The allocated object on success.
27952795
* \return \c NULL on error.
2796-
*/
2796+
*
2797+
* \note The only way to free this object is to pass it to hwloc_topology_insert_group_object().
2798+
* If properly inserted, it will be freed when the entire topology is freed.
2799+
* If insertion failed (e.g. \c NULL or empty CPU and node-sets),
2800+
* it is freed before returning the error.
2801+
*/
27972802
HWLOC_DECLSPEC hwloc_obj_t hwloc_topology_alloc_group_object(hwloc_topology_t topology);
27982803

2804+
/** \brief Free a group object allocated with hwloc_topology_alloc_group_object().
2805+
*
2806+
* This function is only useful if the group object was not given
2807+
* to hwloc_topology_insert_group_object() as planned.
2808+
*
2809+
* \note \p topology must be the same as the one previously passed
2810+
* to hwloc_topology_alloc_group_object().
2811+
*
2812+
* \return \c 0 on success.
2813+
* \return \c -1 on error, for instance if an invalid topology is given.
2814+
*/
2815+
HWLOC_DECLSPEC int hwloc_topology_free_group_object(hwloc_topology_t topology, hwloc_obj_t group);
2816+
27992817
/** \brief Add more structure to the topology by adding an intermediate Group
28002818
*
28012819
* The caller should first allocate a new Group object with hwloc_topology_alloc_group_object().
@@ -2833,6 +2851,11 @@ HWLOC_DECLSPEC hwloc_obj_t hwloc_topology_alloc_group_object(hwloc_topology_t to
28332851
* hence the existing objects may get reordered (including PUs and NUMA nodes),
28342852
* and their logical indexes may change.
28352853
*
2854+
* \note If the insertion fails, the input group object is freed.
2855+
*
2856+
* \note \p topology must be the same as the one previously passed
2857+
* to hwloc_topology_alloc_group_object().
2858+
*
28362859
* \return The inserted object if it was properly inserted.
28372860
*
28382861
* \return An existing object if the Group was merged or discarded

include/hwloc/rename.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ extern "C" {
178178

179179
#define hwloc_topology_insert_misc_object HWLOC_NAME(topology_insert_misc_object)
180180
#define hwloc_topology_alloc_group_object HWLOC_NAME(topology_alloc_group_object)
181+
#define hwloc_topology_free_group_object HWLOC_NAME(topology_free_group_object)
181182
#define hwloc_topology_insert_group_object HWLOC_NAME(topology_insert_group_object)
182183
#define hwloc_obj_add_other_obj_sets HWLOC_NAME(obj_add_other_obj_sets)
183184
#define hwloc_topology_refresh HWLOC_NAME(topology_refresh)

tests/hwloc/hwloc_groups.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright © 2011-2021 Inria. All rights reserved.
2+
* Copyright © 2011-2023 Inria. All rights reserved.
33
* See COPYING in top-level directory.
44
*/
55

@@ -31,6 +31,17 @@ int main(void)
3131
hwloc_topology_load(topology);
3232
root = hwloc_get_root_obj(topology);
3333
assert(hwloc_topology_get_depth(topology) == 3);
34+
/* free instead of inserting */
35+
group = hwloc_topology_alloc_group_object(topology);
36+
assert(group);
37+
group->cpuset = hwloc_bitmap_dup(root->cpuset);
38+
err = hwloc_topology_free_group_object(topology, group);
39+
assert(err == 0);
40+
/* insert without sets, fails */
41+
group = hwloc_topology_alloc_group_object(topology);
42+
assert(group);
43+
res = hwloc_topology_insert_group_object(topology, group);
44+
assert(res == NULL);
3445
/* insert a group identical to root, will be merged */
3546
group = hwloc_topology_alloc_group_object(topology);
3647
assert(group);

0 commit comments

Comments
 (0)