int main(void) { hwloc_topology_t local, global; hwloc_obj_t sw1, sw2, sw11, sw12, sw21, sw22, root; int err; printf("Loading the local topology...\n"); hwloc_topology_init(&local); hwloc_topology_set_synthetic(local, "n:2 s:2 ca:1 core:2 ca:2 pu:2"); hwloc_topology_load(local); printf("Try to create an empty custom topology...\n"); hwloc_topology_init(&global); hwloc_topology_set_custom(global); err = hwloc_topology_load(global); assert(err == -1); assert(errno == EINVAL); hwloc_topology_destroy(global); printf("Creating a custom topology...\n"); hwloc_topology_init(&global); hwloc_topology_set_custom(global); printf("Inserting the local topology into the global one...\n"); root = hwloc_get_root_obj(global); sw1 = hwloc_custom_insert_group_object_by_parent(global, root, 0); sw11 = hwloc_custom_insert_group_object_by_parent(global, sw1, 1); hwloc_custom_insert_topology(global, sw11, local, NULL); hwloc_custom_insert_topology(global, sw11, local, NULL); sw12 = hwloc_custom_insert_group_object_by_parent(global, sw1, 1); hwloc_custom_insert_topology(global, sw12, local, NULL); hwloc_custom_insert_topology(global, sw12, local, NULL); sw2 = hwloc_custom_insert_group_object_by_parent(global, root, 0); sw21 = hwloc_custom_insert_group_object_by_parent(global, sw2, 1); hwloc_custom_insert_topology(global, sw21, local, NULL); hwloc_custom_insert_topology(global, sw21, local, NULL); hwloc_custom_insert_topology(global, sw21, local, NULL); sw22 = hwloc_custom_insert_group_object_by_parent(global, sw2, 1); hwloc_custom_insert_topology(global, sw22, local, NULL); /* only one to check that it won't get merged */ hwloc_topology_destroy(local); printf("Building the global topology...\n"); hwloc_topology_load(global); hwloc_topology_check(global); assert(hwloc_topology_get_depth(global) == 10); assert(hwloc_get_depth_type(global, 0) == HWLOC_OBJ_SYSTEM); assert(hwloc_get_nbobjs_by_type(global, HWLOC_OBJ_SYSTEM) == 1); assert(hwloc_get_depth_type(global, 1) == HWLOC_OBJ_GROUP); assert(hwloc_get_nbobjs_by_depth(global, 1) == 2); assert(hwloc_get_depth_type(global, 2) == HWLOC_OBJ_GROUP); assert(hwloc_get_nbobjs_by_depth(global, 2) == 4); /* the last group of this level shouldn't be merged */ assert(hwloc_get_depth_type(global, 3) == HWLOC_OBJ_MACHINE); assert(hwloc_get_nbobjs_by_type(global, HWLOC_OBJ_MACHINE) == 8); assert(hwloc_get_depth_type(global, 4) == HWLOC_OBJ_NODE); assert(hwloc_get_nbobjs_by_type(global, HWLOC_OBJ_NODE) == 16); assert(hwloc_get_depth_type(global, 5) == HWLOC_OBJ_SOCKET); assert(hwloc_get_nbobjs_by_type(global, HWLOC_OBJ_SOCKET) == 32); assert(hwloc_get_depth_type(global, 6) == HWLOC_OBJ_CACHE); assert(hwloc_get_nbobjs_by_depth(global, 6) == 32); assert(hwloc_get_depth_type(global, 7) == HWLOC_OBJ_CORE); assert(hwloc_get_nbobjs_by_type(global, HWLOC_OBJ_CORE) == 64); assert(hwloc_get_depth_type(global, 8) == HWLOC_OBJ_CACHE); assert(hwloc_get_nbobjs_by_depth(global, 8) == 128); assert(hwloc_get_depth_type(global, 9) == HWLOC_OBJ_PU); assert(hwloc_get_nbobjs_by_type(global, HWLOC_OBJ_PU) == 256); hwloc_topology_destroy(global); return 0; }
int main(void) { hwloc_topology_t topology1, topology2; char *xmlbuf; int xmlbuflen; char xmlfile[] = "hwloc_backends.tmpxml.XXXXXX"; int xmlbufok = 0, xmlfileok = 0, xmlfilefd; hwloc_obj_t sw; int err; printf("trying to export topology to XML buffer and file for later...\n"); hwloc_topology_init(&topology1); hwloc_topology_load(topology1); assert(hwloc_topology_is_thissystem(topology1)); if (hwloc_topology_export_xmlbuffer(topology1, &xmlbuf, &xmlbuflen) < 0) printf("XML buffer export failed (%s), ignoring\n", strerror(errno)); else xmlbufok = 1; xmlfilefd = mkstemp(xmlfile); if (xmlfilefd < 0 || hwloc_topology_export_xml(topology1, xmlfile) < 0) printf("XML file export failed (%s), ignoring\n", strerror(errno)); else xmlfileok = 1; printf("init...\n"); hwloc_topology_init(&topology2); if (xmlfileok) { printf("switching to xml...\n"); assert(!hwloc_topology_set_xml(topology2, xmlfile)); } if (xmlbufok) { printf("switching to xmlbuffer...\n"); assert(!hwloc_topology_set_xmlbuffer(topology2, xmlbuf, xmlbuflen)); } printf("switching to custom...\n"); hwloc_topology_set_custom(topology2); printf("switching to synthetic...\n"); hwloc_topology_set_synthetic(topology2, "machine:2 node:3 cache:2 pu:4"); printf("switching sysfs fsroot to // ...\n"); hwloc_topology_set_fsroot(topology2, "//"); /* valid path that won't be recognized as '/' */ printf("switching sysfs fsroot to / ...\n"); hwloc_topology_set_fsroot(topology2, "/"); hwloc_topology_destroy(topology2); if (xmlfileok) { printf("switching to xml and loading...\n"); hwloc_topology_init(&topology2); assert(!hwloc_topology_set_xml(topology2, xmlfile)); hwloc_topology_load(topology2); hwloc_topology_check(topology2); assert(!hwloc_topology_is_thissystem(topology2)); hwloc_topology_destroy(topology2); } if (xmlbufok) { printf("switching to xmlbuffer and loading...\n"); hwloc_topology_init(&topology2); assert(!hwloc_topology_set_xmlbuffer(topology2, xmlbuf, xmlbuflen)); hwloc_topology_load(topology2); hwloc_topology_check(topology2); assert(!hwloc_topology_is_thissystem(topology2)); hwloc_topology_destroy(topology2); } printf("switching to custom and loading...\n"); hwloc_topology_init(&topology2); hwloc_topology_set_custom(topology2); sw = hwloc_custom_insert_group_object_by_parent(topology2, hwloc_get_root_obj(topology2), 0); assert(sw); hwloc_custom_insert_topology(topology2, sw, topology1, NULL); hwloc_topology_load(topology2); hwloc_topology_check(topology2); assert(!hwloc_topology_is_thissystem(topology2)); hwloc_topology_destroy(topology2); printf("switching to synthetic and loading...\n"); hwloc_topology_init(&topology2); hwloc_topology_set_synthetic(topology2, "machine:2 node:3 cache:2 pu:4"); hwloc_topology_load(topology2); hwloc_topology_check(topology2); assert(!hwloc_topology_is_thissystem(topology2)); hwloc_topology_destroy(topology2); printf("switching sysfs fsroot to // and loading...\n"); hwloc_topology_init(&topology2); err = hwloc_topology_set_fsroot(topology2, "//"); /* '//' isn't recognized as the normal fsroot on Linux, and it fails and falls back to normal topology on !Linux */ hwloc_topology_load(topology2); hwloc_topology_check(topology2); assert(!hwloc_topology_is_thissystem(topology2) == !err); hwloc_topology_destroy(topology2); printf("switching sysfs fsroot to / and loading...\n"); hwloc_topology_init(&topology2); err = hwloc_topology_set_fsroot(topology2, "/"); hwloc_topology_load(topology2); hwloc_topology_check(topology2); assert(hwloc_topology_is_thissystem(topology2)); /* '/' is recognized as the normal fsroot on Linux, and it fails and falls back to normal topology on !Linux */ hwloc_topology_destroy(topology2); printf("switching to synthetic...\n"); hwloc_topology_init(&topology2); hwloc_topology_set_synthetic(topology2, "machine:2 node:3 cache:2 pu:4"); hwloc_topology_destroy(topology2); if (xmlbufok) hwloc_free_xmlbuffer(topology1, xmlbuf); if (xmlfilefd >= 0) { unlink(xmlfile); close(xmlfilefd); } hwloc_topology_destroy(topology1); return 0; }
int main(void) { hwloc_topology_t topology1, topology2; char *xmlbuf; int xmlbuflen; char xmlfile[] = "hwloc_backends.tmpxml.XXXXXX"; int xmlbufok = 0, xmlfileok = 0; hwloc_obj_t sw; int err; printf("trying to export topology to XML buffer and file for later...\n"); hwloc_topology_init(&topology1); hwloc_topology_load(topology1); assert(hwloc_topology_is_thissystem(topology1)); if (hwloc_topology_export_xmlbuffer(topology1, &xmlbuf, &xmlbuflen) < 0) printf("XML buffer export failed (%s), ignoring\n", strerror(errno)); else xmlbufok = 1; mktemp(xmlfile); if (hwloc_topology_export_xml(topology1, xmlfile) < 0) printf("XML file export failed (%s), ignoring\n", strerror(errno)); else xmlfileok = 1; printf("init...\n"); hwloc_topology_init(&topology2); if (xmlfileok) { printf("switching to xml...\n"); assert(!hwloc_topology_set_xml(topology2, xmlfile)); } if (xmlbufok) { printf("switching to xmlbuffer...\n"); assert(!hwloc_topology_set_xmlbuffer(topology2, xmlbuf, xmlbuflen)); } printf("switching to custom...\n"); hwloc_topology_set_custom(topology2); printf("switching to synthetic...\n"); hwloc_topology_set_synthetic(topology2, "machine:2 node:3 cache:2 pu:4"); printf("switching sysfs fsroot to // ...\n"); hwloc_topology_set_fsroot(topology2, "//"); /* valid path that won't be recognized as '/' */ printf("switching sysfs fsroot to / ...\n"); hwloc_topology_set_fsroot(topology2, "/"); if (xmlfileok) { printf("switching to xml and loading...\n"); assert(!hwloc_topology_set_xml(topology2, xmlfile)); hwloc_topology_load(topology2); hwloc_topology_check(topology2); assert(!hwloc_topology_is_thissystem(topology2)); } if (xmlbufok) { printf("switching to xmlbuffer and loading...\n"); assert(!hwloc_topology_set_xmlbuffer(topology2, xmlbuf, xmlbuflen)); hwloc_topology_load(topology2); hwloc_topology_check(topology2); assert(!hwloc_topology_is_thissystem(topology2)); } printf("switching to custom and loading...\n"); hwloc_topology_set_custom(topology2); sw = hwloc_custom_insert_group_object_by_parent(topology2, hwloc_get_root_obj(topology2), 0); assert(sw); hwloc_custom_insert_topology(topology2, sw, topology1, NULL); hwloc_topology_load(topology2); hwloc_topology_check(topology2); assert(!hwloc_topology_is_thissystem(topology2)); /* don't try fsroot here because it fails on !linux, we would revert back to custom, which requires some insert to make the topology valid */ printf("switching to synthetic and loading...\n"); hwloc_topology_set_synthetic(topology2, "machine:2 node:3 cache:2 pu:4"); hwloc_topology_load(topology2); hwloc_topology_check(topology2); assert(!hwloc_topology_is_thissystem(topology2)); printf("switching sysfs fsroot to // and loading...\n"); hwloc_topology_set_fsroot(topology2, "//"); /* valid path that won't be recognized as '/' */ hwloc_topology_load(topology2); hwloc_topology_check(topology2); assert(!hwloc_topology_is_thissystem(topology2)); /* earlier fsroot worked, or we're still synthetic */ printf("switching sysfs fsroot to / and loading...\n"); err = hwloc_topology_set_fsroot(topology2, "/"); hwloc_topology_load(topology2); hwloc_topology_check(topology2); assert(hwloc_topology_is_thissystem(topology2) == !err); /* on Linux, '/' is recognized as thissystem. on !Linux, set_fsroot() failed and we went back to synthetic */ printf("switching to synthetic...\n"); hwloc_topology_set_synthetic(topology2, "machine:2 node:3 cache:2 pu:4"); hwloc_topology_destroy(topology2); if (xmlbufok) hwloc_free_xmlbuffer(topology1, xmlbuf); if (xmlfileok) unlink(xmlfile); hwloc_topology_destroy(topology1); return 0; }