Skip to content

Commit

Permalink
tests/restrict: don't make the topology a global variable
Browse files Browse the repository at this point in the history
Signed-off-by: Brice Goglin <Brice.Goglin@inria.fr>
  • Loading branch information
bgoglin committed Mar 22, 2023
1 parent 22a1931 commit f8000a5
Showing 1 changed file with 22 additions and 23 deletions.
45 changes: 22 additions & 23 deletions tests/hwloc/hwloc_topology_restrict.c
@@ -1,5 +1,5 @@
/*
* Copyright © 2011-2021 Inria. All rights reserved.
* Copyright © 2011-2023 Inria. All rights reserved.
* Copyright © 2011 Université Bordeaux. All rights reserved.
* See COPYING in top-level directory.
*/
Expand All @@ -12,8 +12,6 @@
#include <stdint.h>
#include <assert.h>

static hwloc_topology_t topology;

static void print_distances(const struct hwloc_distances_s *distances)
{
unsigned nbobjs = distances->nbobjs;
Expand All @@ -36,7 +34,7 @@ static void print_distances(const struct hwloc_distances_s *distances)
}
}

static void check(unsigned nbgroups, unsigned nbnodes, unsigned nbcores, unsigned nbpus)
static void check(hwloc_topology_t topology, unsigned nbgroups, unsigned nbnodes, unsigned nbcores, unsigned nbpus)
{
int depth;
unsigned nb;
Expand Down Expand Up @@ -67,7 +65,7 @@ static void check(unsigned nbgroups, unsigned nbnodes, unsigned nbcores, unsigne
assert(total_memory == nbnodes * 1024*1024*1024); /* synthetic topology puts 1GB per node */
}

static void check_distances(unsigned nbnodes, unsigned nbcores)
static void check_distances(hwloc_topology_t topology, unsigned nbnodes, unsigned nbcores)
{
struct hwloc_distances_s *distance;
unsigned nr;
Expand Down Expand Up @@ -108,6 +106,7 @@ int main(void)
hwloc_obj_t nodes[3], cores[6];
hwloc_uint64_t node_distances[9], core_distances[36];
hwloc_distances_add_handle_t handle;
hwloc_topology_t topology, topology2;
hwloc_obj_t obj;
unsigned i,j;
int err;
Expand Down Expand Up @@ -148,8 +147,8 @@ int main(void)

/* entire topology */
printf("starting from full topology\n");
check(3, 3, 6, 24);
check_distances(3, 6);
check(topology, 3, 3, 6, 24);
check_distances(topology, 3, 6);

/* restrict to nothing, impossible */
printf("restricting to nothing, must fail\n");
Expand All @@ -162,43 +161,43 @@ int main(void)
assert(err < 0 && errno == EINVAL);
err = hwloc_topology_refresh(topology);
assert(!err);
check(3, 3, 6, 24);
check_distances(3, 6);
check(topology, 3, 3, 6, 24);
check_distances(topology, 3, 6);

/* restrict to everything, will do nothing */
printf("restricting to everything, does nothing\n");
hwloc_bitmap_fill(cpuset);
err = hwloc_topology_restrict(topology, cpuset, 0);
assert(!err);
check(3, 3, 6, 24);
check_distances(3, 6);
check(topology, 3, 3, 6, 24);
check_distances(topology, 3, 6);

/* remove a single pu (second PU of second core of second node) */
printf("removing second PU of second core of second node\n");
hwloc_bitmap_fill(cpuset);
hwloc_bitmap_clr(cpuset, 13);
err = hwloc_topology_restrict(topology, cpuset, 0);
assert(!err);
check(3, 3, 6, 23);
check_distances(3, 6);
check(topology, 3, 3, 6, 23);
check_distances(topology, 3, 6);

/* remove the entire second core of first node */
printf("removing entire second core of first node\n");
hwloc_bitmap_fill(cpuset);
hwloc_bitmap_clr_range(cpuset, 4, 7);
err = hwloc_topology_restrict(topology, cpuset, 0);
assert(!err);
check(3, 3, 5, 19);
check_distances(3, 5);
check(topology, 3, 3, 5, 19);
check_distances(topology, 3, 5);

/* remove the entire third node */
printf("removing all PUs under third node, but keep that CPU-less node\n");
hwloc_bitmap_fill(cpuset);
hwloc_bitmap_clr_range(cpuset, 16, 23);
err = hwloc_topology_restrict(topology, cpuset, 0);
assert(!err);
check(3, 3, 3, 11);
check_distances(3, 3);
check(topology, 3, 3, 3, 11);
check_distances(topology, 3, 3);

/* only keep three PUs (first and last of first core, and last of last core of second node) */
printf("restricting to 3 PUs in 2 cores in 2 nodes, and remove the CPU-less node, and auto-merge groups\n");
Expand All @@ -208,17 +207,17 @@ int main(void)
hwloc_bitmap_set(cpuset, 15);
err = hwloc_topology_restrict(topology, cpuset, HWLOC_RESTRICT_FLAG_REMOVE_CPULESS);
assert(!err);
check(0, 2, 2, 3);
check_distances(2, 2);
check(topology, 0, 2, 2, 3);
check_distances(topology, 2, 2);

/* restrict to the third node, impossible */
printf("restricting to only some already removed node, must fail\n");
hwloc_bitmap_zero(cpuset);
hwloc_bitmap_set_range(cpuset, 16, 23);
err = hwloc_topology_restrict(topology, cpuset, 0);
assert(err == -1 && errno == EINVAL);
check(0, 2, 2, 3);
check_distances(2, 2);
check(topology, 0, 2, 2, 3);
check_distances(topology, 2, 2);

hwloc_topology_destroy(topology);

Expand Down Expand Up @@ -247,13 +246,13 @@ int main(void)
err = hwloc_topology_restrict(topology, cpuset, HWLOC_RESTRICT_FLAG_BYNODESET);
assert(!err);
hwloc_topology_check(topology);
check(3, 2, 6, 24);
check(topology, 3, 2, 6, 24);
printf("further restricting bynodeset to a single numa node\n");
hwloc_bitmap_only(cpuset, 1);
err = hwloc_topology_restrict(topology, cpuset, HWLOC_RESTRICT_FLAG_BYNODESET|HWLOC_RESTRICT_FLAG_REMOVE_MEMLESS);
assert(!err);
hwloc_topology_check(topology);
check(0, 1, 2, 8);
check(topology, 0, 1, 2, 8);
printf("restricting with invalid flags\n");
err = hwloc_topology_restrict(topology, cpuset, HWLOC_RESTRICT_FLAG_REMOVE_MEMLESS);
assert(err == -1);
Expand Down

0 comments on commit f8000a5

Please sign in to comment.