static hwloc_obj_t insert_task(hwloc_topology_t topology, hwloc_cpuset_t cpuset, const char * name) { hwloc_obj_t group, obj; hwloc_bitmap_and(cpuset, cpuset, hwloc_topology_get_topology_cpuset(topology)); if (hwloc_bitmap_iszero(cpuset)) return NULL; /* try to insert a group at exact position */ group = hwloc_topology_alloc_group_object(topology); if (!group) return NULL; group->cpuset = hwloc_bitmap_dup(cpuset); group = hwloc_topology_insert_group_object(topology, group); if (!group) { /* try to insert in a larger parent */ char *s; hwloc_bitmap_asprintf(&s, cpuset); group = hwloc_get_obj_covering_cpuset(topology, cpuset); fprintf(stderr, "Inserting process `%s' below parent larger than cpuset %s\n", name, s); free(s); } obj = hwloc_topology_insert_misc_object(topology, group, name); if (!obj) fprintf(stderr, "Failed to insert process `%s'\n", name); else obj->subtype = strdup("Process"); return obj; }
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_topology_t topology; char *string = NULL; hwloc_obj_t obj; hwloc_bitmap_t set; hwloc_topology_init(&topology); hwloc_topology_set_synthetic(topology, SYNTHETIC_TOPOLOGY_DESCRIPTION); hwloc_topology_load(topology); set = hwloc_bitmap_alloc(); hwloc_bitmap_sscanf(set, GIVEN_CPUSET_STRING); obj = hwloc_get_obj_covering_cpuset(topology, set); assert(obj); fprintf(stderr, "found covering object type %s covering cpuset %s\n", hwloc_obj_type_string(obj->type), GIVEN_CPUSET_STRING); assert(hwloc_bitmap_isincluded(set, obj->cpuset)); hwloc_bitmap_asprintf(&string, obj->cpuset); fprintf(stderr, "covering object of %s is %s, expected %s\n", GIVEN_CPUSET_STRING, string, EXPECTED_CPUSET_STRING); assert(!strcmp(EXPECTED_CPUSET_STRING, string)); free(string); hwloc_bitmap_sscanf(set, GIVEN_LARGESPLIT_CPUSET_STRING); obj = hwloc_get_obj_covering_cpuset(topology, set); assert(obj == hwloc_get_root_obj(topology)); fprintf(stderr, "found system as covering object of first+last cpus cpuset %s\n", GIVEN_LARGESPLIT_CPUSET_STRING); hwloc_bitmap_sscanf(set, GIVEN_TOOLARGE_CPUSET_STRING); obj = hwloc_get_obj_covering_cpuset(topology, set); assert(!obj); fprintf(stderr, "found no covering object for too-large cpuset %s\n", GIVEN_TOOLARGE_CPUSET_STRING); hwloc_bitmap_free(set); hwloc_topology_destroy(topology); return EXIT_SUCCESS; }
int main(void) { hwloc_topology_t topology; hwloc_bitmap_t set, set2; hwloc_const_bitmap_t cset_available, cset_all; hwloc_obj_t obj; char *buffer; char type[64]; unsigned i; int err; /* create a topology */ err = hwloc_topology_init(&topology); if (err < 0) { fprintf(stderr, "failed to initialize the topology\n"); return EXIT_FAILURE; } err = hwloc_topology_load(topology); if (err < 0) { fprintf(stderr, "failed to load the topology\n"); hwloc_topology_destroy(topology); return EXIT_FAILURE; } /* retrieve the entire set of available PUs */ cset_available = hwloc_topology_get_topology_cpuset(topology); /* retrieve the CPU binding of the current entire process */ set = hwloc_bitmap_alloc(); if (!set) { fprintf(stderr, "failed to allocate a bitmap\n"); hwloc_topology_destroy(topology); return EXIT_FAILURE; } err = hwloc_get_cpubind(topology, set, HWLOC_CPUBIND_PROCESS); if (err < 0) { fprintf(stderr, "failed to get cpu binding\n"); hwloc_bitmap_free(set); hwloc_topology_destroy(topology); } /* display the processing units that cannot be used by this process */ if (hwloc_bitmap_isequal(set, cset_available)) { printf("this process can use all available processing units in the system\n"); } else { /* compute the set where we currently cannot run. * we can't modify cset_available because it's a system read-only one, * so we do set = available &~ set */ hwloc_bitmap_andnot(set, cset_available, set); hwloc_bitmap_asprintf(&buffer, set); printf("process cannot use %d process units (%s) among %u in the system\n", hwloc_bitmap_weight(set), buffer, hwloc_bitmap_weight(cset_available)); free(buffer); /* restore set where it was before the &~ operation above */ hwloc_bitmap_andnot(set, cset_available, set); } /* print the smallest object covering the current process binding */ obj = hwloc_get_obj_covering_cpuset(topology, set); hwloc_obj_type_snprintf(type, sizeof(type), obj, 0); printf("process is bound within object %s logical index %u\n", type, obj->logical_index); /* retrieve the single PU where the current thread actually runs within this process binding */ set2 = hwloc_bitmap_alloc(); if (!set2) { fprintf(stderr, "failed to allocate a bitmap\n"); hwloc_bitmap_free(set); hwloc_topology_destroy(topology); return EXIT_FAILURE; } err = hwloc_get_last_cpu_location(topology, set2, HWLOC_CPUBIND_THREAD); if (err < 0) { fprintf(stderr, "failed to get last cpu location\n"); hwloc_bitmap_free(set); hwloc_bitmap_free(set2); hwloc_topology_destroy(topology); } /* sanity checks that are not actually needed but help the reader */ /* this thread runs within the process binding */ assert(hwloc_bitmap_isincluded(set2, set)); /* this thread runs on a single PU at a time */ assert(hwloc_bitmap_weight(set2) == 1); /* print the logical number of the PU where that thread runs */ /* extract the PU OS index from the bitmap */ i = hwloc_bitmap_first(set2); obj = hwloc_get_pu_obj_by_os_index(topology, i); printf("thread is now running on PU logical index %u (OS/physical index %u)\n", obj->logical_index, i); /* migrate this single thread to where other PUs within the current binding */ hwloc_bitmap_andnot(set2, set, set2); err = hwloc_set_cpubind(topology, set2, HWLOC_CPUBIND_THREAD); if (err < 0) { fprintf(stderr, "failed to set thread binding\n"); hwloc_bitmap_free(set); hwloc_bitmap_free(set2); hwloc_topology_destroy(topology); } /* reprint the PU where that thread runs */ err = hwloc_get_last_cpu_location(topology, set2, HWLOC_CPUBIND_THREAD); if (err < 0) { fprintf(stderr, "failed to get last cpu location\n"); hwloc_bitmap_free(set); hwloc_bitmap_free(set2); hwloc_topology_destroy(topology); } /* print the logical number of the PU where that thread runs */ /* extract the PU OS index from the bitmap */ i = hwloc_bitmap_first(set2); obj = hwloc_get_pu_obj_by_os_index(topology, i); printf("thread is running on PU logical index %u (OS/physical index %u)\n", obj->logical_index, i); hwloc_bitmap_free(set); hwloc_bitmap_free(set2); /* retrieve the entire set of all PUs */ cset_all = hwloc_topology_get_complete_cpuset(topology); if (hwloc_bitmap_isequal(cset_all, cset_available)) { printf("all hardware PUs are available\n"); } else { printf("only %d hardware PUs are available in the machine among %d\n", hwloc_bitmap_weight(cset_available), hwloc_bitmap_weight(cset_all)); } hwloc_topology_destroy(topology); return EXIT_SUCCESS; }