static hwloc_obj_t insert_task(hwloc_topology_t topology, hwloc_cpuset_t cpuset, const char * name) { hwloc_obj_t obj; hwloc_bitmap_and(cpuset, cpuset, hwloc_topology_get_topology_cpuset(topology)); if (hwloc_bitmap_iszero(cpuset)) return NULL; /* try to insert at exact position */ obj = hwloc_topology_insert_misc_object_by_cpuset(topology, cpuset, name); if (!obj) { /* try to insert in a larger parent */ char *s; hwloc_bitmap_asprintf(&s, cpuset); obj = hwloc_get_obj_covering_cpuset(topology, cpuset); if (obj) { obj = hwloc_topology_insert_misc_object_by_parent(topology, obj, name); fprintf(stderr, "Inserted process `%s' below parent larger than cpuset %s\n", name, s); } else { fprintf(stderr, "Failed to insert process `%s' with cpuset %s\n", name, s); } free(s); } else { hwloc_obj_add_info(obj, "Type", "Process"); } return obj; }
int main(void) { hwloc_bitmap_t cpuset = hwloc_bitmap_alloc(); unsigned node_indexes[3], core_indexes[6]; float node_distances[9], core_distances[36]; unsigned i,j; int err; for(i=0; i<3; i++) { node_indexes[i] = i; for(j=0; j<3; j++) node_distances[i*3+j] = (i == j ? 10.f : 20.f); } for(i=0; i<6; i++) { core_indexes[i] = i; for(j=0; j<6; j++) core_distances[i*6+j] = (i == j ? 4.f : 8.f); } hwloc_topology_init(&topology); hwloc_topology_set_synthetic(topology, "node:3 core:2 pu:4"); hwloc_topology_set_distance_matrix(topology, HWLOC_OBJ_NODE, 3, node_indexes, node_distances); hwloc_topology_set_distance_matrix(topology, HWLOC_OBJ_CORE, 6, core_indexes, core_distances); hwloc_topology_load(topology); /* entire topology */ printf("starting from full topology\n"); check(3, 6, 24); check_distances(3, 6); /* restrict to nothing, impossible */ printf("restricting to nothing, must fail\n"); hwloc_bitmap_zero(cpuset); err = hwloc_topology_restrict(topology, cpuset, HWLOC_RESTRICT_FLAG_ADAPT_DISTANCES); assert(err < 0 && errno == EINVAL); check(3, 6, 24); check_distances(3, 6); /* restrict to everything, will do nothing */ printf("restricting to nothing, does nothing\n"); hwloc_bitmap_fill(cpuset); err = hwloc_topology_restrict(topology, cpuset, HWLOC_RESTRICT_FLAG_ADAPT_DISTANCES); assert(!err); check(3, 6, 24); check_distances(3, 6); /* remove a single pu (second PU of second core of second node) */ printf("removing one PU\n"); hwloc_bitmap_fill(cpuset); hwloc_bitmap_clr(cpuset, 13); err = hwloc_topology_restrict(topology, cpuset, HWLOC_RESTRICT_FLAG_ADAPT_DISTANCES); assert(!err); check(3, 6, 23); check_distances(3, 6); /* remove the entire second core of first node */ printf("removing one core\n"); hwloc_bitmap_fill(cpuset); hwloc_bitmap_clr_range(cpuset, 4, 7); err = hwloc_topology_restrict(topology, cpuset, HWLOC_RESTRICT_FLAG_ADAPT_DISTANCES); assert(!err); check(3, 5, 19); check_distances(3, 5); /* remove the entire third node */ printf("removing one node\n"); hwloc_bitmap_fill(cpuset); hwloc_bitmap_clr_range(cpuset, 16, 23); err = hwloc_topology_restrict(topology, cpuset, HWLOC_RESTRICT_FLAG_ADAPT_DISTANCES); assert(!err); check(2, 3, 11); check_distances(2, 3); /* 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, HWLOC_RESTRICT_FLAG_ADAPT_DISTANCES); assert(err == -1 && errno == EINVAL); check(2, 3, 11); check_distances(2, 3); /* only keep three PUs (first and last of first core, and last of last core of second node) */ printf("restricting to 3 PUs\n"); hwloc_bitmap_zero(cpuset); hwloc_bitmap_set(cpuset, 0); hwloc_bitmap_set(cpuset, 3); hwloc_bitmap_set(cpuset, 15); err = hwloc_topology_restrict(topology, cpuset, 0); assert(!err); check(2, 2, 3); check_distances(0, 0); /* only keep one PU (last of last core of second node) */ printf("restricting to a single PU\n"); hwloc_bitmap_zero(cpuset); hwloc_bitmap_set(cpuset, 15); err = hwloc_topology_restrict(topology, cpuset, 0); assert(!err); check(1, 1, 1); check_distances(0, 0); hwloc_topology_destroy(topology); /* check that restricting exactly on a Misc object keeps things coherent */ printf("restricting to a Misc covering only the of the PU level\n"); hwloc_topology_init(&topology); hwloc_topology_set_synthetic(topology, "pu:4"); hwloc_topology_load(topology); hwloc_bitmap_zero(cpuset); hwloc_bitmap_set_range(cpuset, 1, 2); hwloc_topology_insert_misc_object_by_cpuset(topology, cpuset, "toto"); hwloc_topology_restrict(topology, cpuset, 0); hwloc_topology_check(topology); hwloc_topology_destroy(topology); hwloc_bitmap_free(cpuset); return 0; }