int main(int argc, char *argv[]) { static hwloc_topology_t orig; hwloc_obj_t nodes[3]; uint64_t node_distances[9]; unsigned i,j; int err, ret, ret2; if (argc > 1) { int fd; unsigned long forced_addr; unsigned long fileoffset; size_t shmem_length; int synthetic_with_distances; if (argc < 6) { printf("needs 5 arguments\n"); return EXIT_FAILURE; } printf(" opening %s\n", argv[1]); fd = open(argv[1], O_RDONLY); if (fd < 0) { perror("open"); return EXIT_FAILURE; } fileoffset = strtoul(argv[2], NULL, 0); forced_addr = strtoul(argv[3], NULL, 0); shmem_length = strtoul(argv[4], NULL, 0); synthetic_with_distances = atoi(argv[5]); ret = adopt(fd, fileoffset, forced_addr, shmem_length, synthetic_with_distances); close(fd); exit(ret); } printf("########################\n"); printf("creating native topology\n"); err = hwloc_topology_init(&orig); assert(!err); err = hwloc_topology_set_all_types_filter(orig, HWLOC_TYPE_FILTER_KEEP_ALL); assert(!err); err = hwloc_topology_load(orig); assert(!err); ret = test(orig, argv[0]); printf("destroying original\n"); hwloc_topology_destroy(orig); printf("###############################################\n"); printf("creating synthetic topo with distances topology\n"); err = hwloc_topology_init(&orig); assert(!err); err = hwloc_topology_set_synthetic(orig, "node:3 core:2 pu:4"); assert(!err); err = hwloc_topology_load(orig); assert(!err); printf("adding distance matrix\n"); for(i=0; i<3; i++) { nodes[i] = hwloc_get_obj_by_type(orig, HWLOC_OBJ_NUMANODE, i); for(j=0; j<3; j++) node_distances[i*3+j] = (i == j ? 10 : 20); } err = hwloc_distances_add(orig, 3, nodes, node_distances, HWLOC_DISTANCES_KIND_MEANS_LATENCY|HWLOC_DISTANCES_KIND_FROM_USER, HWLOC_DISTANCES_ADD_FLAG_GROUP); assert(!err); ret2 = test(orig, argv[0]); printf("destroying original\n"); hwloc_topology_destroy(orig); /* we caught errors above. * return SKIP if both returned SKIP. otherwise SUCCESS */ if (ret == EXIT_SKIP && ret2 == EXIT_SKIP) ret = EXIT_SKIP; else ret = EXIT_SUCCESS; return ret; }
int main(int argc, const char * const argv[]) { hwloc_topology_t topo; hwloc_obj_t pu; const char *basedir; const char *callname; char *path; size_t pathlen; unsigned idx = (unsigned) -1; int err; int ret = EXIT_SUCCESS; callname = argv[0]; argc--; argv++; hwloc_utils_check_api_version(callname); if (!hwloc_have_x86_cpuid()) { fprintf(stderr, "CPUID not supported.\n"); ret = EXIT_FAILURE; goto out; } while (argc > 0 && argv[0][0] == '-' && argv[0][1] != '\0') { if (argc >= 2 && !strcmp(argv[0], "-c")) { idx = atoi(argv[1]); argc -= 2; argv += 2; } else if (argc >= 1 && (!strcmp(argv[0], "-s") || !strcmp(argv[0], "--silent"))) { verbose--; argc--; argv++; } else if (!strcmp(argv[0], "-h") || !strcmp(argv[0], "--help")) { usage(callname, stdout); goto out; } else { usage(callname, stderr); ret = EXIT_FAILURE; goto out; } } basedir = "./cpuid"; if (argc >= 1) basedir = argv[0]; if (!getenv("HWLOC_COMPONENTS")) putenv((char *) "HWLOC_COMPONENTS=no_os,stop"); hwloc_topology_init(&topo); hwloc_topology_set_all_types_filter(topo, HWLOC_TYPE_FILTER_KEEP_NONE); err = hwloc_topology_load(topo); if (err < 0) { fprintf(stderr, "Failed to load topology\n"); ret = EXIT_FAILURE; goto out; } if (!hwloc_topology_is_thissystem(topo)) { fprintf(stderr, "%s must run on the current system topology, while this topology doesn't come from this system.\n", callname); ret = EXIT_FAILURE; goto out; } if (!strcmp(basedir, "-")) { if (verbose) printf("Gathering on stdout ...\n"); if (idx == (unsigned) -1) { fprintf(stderr, "Cannot gather multiple PUs on stdout.\n"); ret = EXIT_FAILURE; goto out; } path = NULL; pathlen = 0; } else { err = mkdir(basedir, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH); if (err < 0) { if (access(basedir, X_OK|W_OK) < 0) { fprintf(stderr, "Could not create/open destination directory %s\n", basedir); ret = EXIT_FAILURE; goto out_with_topo; } } if (verbose) printf("Gathering in directory %s ...\n", basedir); pathlen = strlen(basedir) + 20; /* for '/pu%u' or '/hwloc-cpuid-info' */ path = malloc(pathlen); } if (idx == (unsigned) -1) { FILE *file; pu = NULL; while ((pu = hwloc_get_next_obj_by_type(topo, HWLOC_OBJ_PU, pu)) != NULL) { idx = pu->os_index; if (path) snprintf(path, pathlen, "%s/pu%u", basedir, idx); dump_one_proc(topo, pu, path); } snprintf(path, pathlen, "%s/hwloc-cpuid-info", basedir); file = fopen(path, "w"); if (file) { fprintf(file, "Architecture: x86\n"); fclose(file); if (verbose) printf("Summary written to %s\n", path); } else { fprintf(stderr, "Failed to open summary file '%s' for writing: %s\n", path, strerror(errno)); } } else { pu = hwloc_get_pu_obj_by_os_index(topo, idx); if (!pu) { fprintf(stderr, "Cannot find PU P#%u\n", idx); ret = EXIT_FAILURE; goto out_with_path; } else { if (path) snprintf(path, pathlen, "%s/pu%u", basedir, idx); dump_one_proc(topo, pu, path); } } if (verbose) printf("\n" "WARNING: Do not post these files on a public list or website unless you\n" "WARNING: are sure that no information about this platform is sensitive.\n"); out_with_path: free(path); out_with_topo: hwloc_topology_destroy(topo); out: return ret; }
tm_topology_t* get_local_topo_with_hwloc(void) { hwloc_topology_t topology; tm_topology_t *res = NULL; hwloc_obj_t *objs = NULL; unsigned topodepth,depth; int nb_nodes,i; /* Build the topology */ hwloc_topology_init(&topology); #if HWLOC_API_VERSION >= 0x00020000 hwloc_topology_set_all_types_filter(topology, HWLOC_TYPE_FILTER_KEEP_STRUCTURE); #else /* HWLOC_API_VERSION >= 0x00020000 */ hwloc_topology_ignore_all_keep_structure(topology); #endif /* HWLOC_API_VERSION >= 0x00020000 */ hwloc_topology_load(topology); /* Test if symetric */ if(!symetric(topology)){ if(tm_get_verbose_level() >= CRITICAL) fprintf(stderr,"Local toplogy not symetric!\n"); exit(-1); } /* work on depth */ topodepth = hwloc_topology_get_depth(topology); res = (tm_topology_t*)MALLOC(sizeof(tm_topology_t)); res->nb_constraints = 0; res->constraints = NULL; res->nb_levels = topodepth; res->node_id = (int**)MALLOC(sizeof(int*)*res->nb_levels); res->node_rank = (int**)MALLOC(sizeof(int*)*res->nb_levels); res->nb_nodes = (size_t*)MALLOC(sizeof(size_t)*res->nb_levels); res->arity = (int*)MALLOC(sizeof(int)*res->nb_levels); /* Build TreeMatch topology */ for( depth = 0 ; depth < topodepth ; depth++ ){ nb_nodes = hwloc_get_nbobjs_by_depth(topology, depth); res->nb_nodes[depth] = nb_nodes; res->node_id[depth] = (int*)MALLOC(sizeof(int)*nb_nodes); res->node_rank[depth] = (int*)MALLOC(sizeof(int)*nb_nodes); objs = (hwloc_obj_t*)MALLOC(sizeof(hwloc_obj_t)*nb_nodes); objs[0] = hwloc_get_next_obj_by_depth(topology,depth,NULL); hwloc_get_closest_objs(topology,objs[0],objs+1,nb_nodes-1); res->arity[depth] = objs[0]->arity; if (depth == topodepth -1){ res->nb_constraints = nb_nodes; res->nb_proc_units = nb_nodes; } /* printf("%d:",res->arity[depth]); */ /* Build process id tab */ for (i = 0; i < nb_nodes; i++){ res->node_id[depth][i] = objs[i]->os_index; res->node_rank[depth][objs[i]->os_index] = i; /* if(depth==topodepth-1) */ } FREE(objs); } /* Destroy HWLOC topology object. */ hwloc_topology_destroy(topology); /* printf("\n"); */ return res; }
int main(int argc, char *argv[]) { hwloc_obj_type_t type; unsigned old_index, new_index; const char *callname = argv[0]; hwloc_topology_t topology; int err; if (argc < 6) { usage(stderr, callname); exit(EXIT_FAILURE); } #ifdef HWLOC2 err = hwloc_type_sscanf(argv[3], &type, NULL, 0); #else err = hwloc_obj_type_sscanf(argv[3], &type, NULL, NULL, 0); #endif if (err < 0) { fprintf(stderr, "Failed to recognize type `%s'\n", argv[3]); usage(stderr, callname); exit(EXIT_FAILURE); } if (type != HWLOC_OBJ_PU && type != HWLOC_OBJ_NUMANODE) { fprintf(stderr, "Invalid type `%s', should be PU or NUMA node\n", argv[3]); usage(stderr, callname); exit(EXIT_FAILURE); } old_index = atoi(argv[4]); new_index = atoi(argv[5]); if (old_index == new_index) { fprintf(stderr, "Nothing to do\n"); exit(EXIT_SUCCESS); } err = hwloc_topology_init(&topology); if (err < 0) { fprintf(stderr, "hwloc_topology_init() failed (%s)\n", strerror(errno)); usage(stderr, callname); exit(EXIT_FAILURE); } err = hwloc_topology_set_xml(topology, argv[1]); if (err < 0) { fprintf(stderr, "hwloc_topology_set_xml() on file `%s' failed (%s)\n", argv[1], strerror(errno)); usage(stderr, callname); exit(EXIT_FAILURE); } #ifdef HWLOC2 err = hwloc_topology_set_flags(topology, HWLOC_TOPOLOGY_FLAG_WHOLE_SYSTEM); err = hwloc_topology_set_all_types_filter(topology, HWLOC_TYPE_FILTER_KEEP_ALL); #else err = hwloc_topology_set_flags(topology, HWLOC_TOPOLOGY_FLAG_WHOLE_SYSTEM | HWLOC_TOPOLOGY_FLAG_WHOLE_IO | HWLOC_TOPOLOGY_FLAG_ICACHES); #endif err = hwloc_topology_load(topology); if (err < 0) { fprintf(stderr, "hwloc_topology_load() failed (%s)\n", strerror(errno)); usage(stderr, callname); exit(EXIT_FAILURE); } if (HWLOC_OBJ_PU == type) { hwloc_const_bitmap_t cpset = hwloc_topology_get_complete_cpuset(topology); if (!hwloc_bitmap_isset(cpset, old_index)) { fprintf(stderr, "Old PU os_index %u doesn't exist\n", old_index); usage(stderr, callname); exit(EXIT_FAILURE); } if (hwloc_bitmap_isset(cpset, new_index)) { fprintf(stderr, "New PU os_index %u already exists\n", new_index); usage(stderr, callname); exit(EXIT_FAILURE); } switch_pu_index(hwloc_get_root_obj(topology), old_index, new_index); } else if (HWLOC_OBJ_NUMANODE == type) { hwloc_const_bitmap_t cnset = hwloc_topology_get_complete_nodeset(topology); if (!cnset || hwloc_bitmap_isfull(cnset)) { fprintf(stderr, "Topology doesn't have NUMA nodes\n"); usage(stderr, callname); exit(EXIT_FAILURE); } if (!hwloc_bitmap_isset(cnset, old_index)) { fprintf(stderr, "Old NUMA node os_index %u doesn't exist\n", old_index); usage(stderr, callname); exit(EXIT_FAILURE); } if (hwloc_bitmap_isset(cnset, new_index)) { fprintf(stderr, "New NUMA node os_index %u already exists\n", new_index); usage(stderr, callname); exit(EXIT_FAILURE); } switch_numa_index(hwloc_get_root_obj(topology), old_index, new_index); } err = hwloc_topology_export_xml(topology, argv[2], 0); if (err < 0) { fprintf(stderr, "hwloc_topology_export_xml() on file `%s' failed (%s)\n", argv[2], strerror(errno)); usage(stderr, callname); exit(EXIT_FAILURE); } hwloc_topology_destroy(topology); printf("Beware that hwloc may warn about out-of-order objects when reloading %s\n", argv[2]); return 0; }
tm_topology_t* hwloc_to_tm(char *filename) { hwloc_topology_t topology; tm_topology_t *res = NULL; hwloc_obj_t *objs = NULL; unsigned topodepth,depth; unsigned int nb_nodes; double *cost; int err, l; unsigned int i; int vl = tm_get_verbose_level(); /* Build the topology */ hwloc_topology_init(&topology); err = hwloc_topology_set_xml(topology,filename); if(err == -1){ if(vl >= CRITICAL) fprintf(stderr,"Error: %s is a bad xml topology file!\n",filename); exit(-1); } #if HWLOC_API_VERSION >= 0x00020000 hwloc_topology_set_all_types_filter(topology, HWLOC_TYPE_FILTER_KEEP_STRUCTURE); #else /* HWLOC_API_VERSION >= 0x00020000 */ hwloc_topology_ignore_all_keep_structure(topology); #endif /* HWLOC_API_VERSION >= 0x00020000 */ hwloc_topology_load(topology); /* Test if symetric */ if(!symetric(topology)){ if(tm_get_verbose_level() >= CRITICAL) fprintf(stderr,"%s not symetric!\n",filename); exit(-1); } /* work on depth */ topodepth = hwloc_topology_get_depth(topology); res = (tm_topology_t*)MALLOC(sizeof(tm_topology_t)); res->oversub_fact = 1; res->nb_constraints = 0; res->constraints = NULL; res->nb_levels = topodepth; res->node_id = (int**)MALLOC(sizeof(int*)*res->nb_levels); res->node_rank = (int**)MALLOC(sizeof(int*)*res->nb_levels); res->nb_nodes = (size_t*)MALLOC(sizeof(size_t)*res->nb_levels); res->arity = (int*)MALLOC(sizeof(int)*res->nb_levels); if(vl >= INFO) printf("topodepth = %d\n",topodepth); /* Build TreeMatch topology */ for( depth = 0 ; depth < topodepth ; depth++ ){ nb_nodes = hwloc_get_nbobjs_by_depth(topology, depth); res->nb_nodes[depth] = nb_nodes; res->node_id[depth] = (int*)MALLOC(sizeof(int)*nb_nodes); res->node_rank[depth] = (int*)MALLOC(sizeof(int)*nb_nodes); objs = (hwloc_obj_t*)MALLOC(sizeof(hwloc_obj_t)*nb_nodes); objs[0] = hwloc_get_next_obj_by_depth(topology,depth,NULL); hwloc_get_closest_objs(topology,objs[0],objs+1,nb_nodes-1); res->arity[depth] = objs[0]->arity; if (depth == topodepth -1){ res->nb_constraints = nb_nodes; res->nb_proc_units = nb_nodes; } if(vl >= DEBUG) printf("\n--%d(%d) **%d**:--\n",res->arity[depth],nb_nodes,res->arity[0]); /* Build process id tab */ for (i = 0; i < nb_nodes; i++){ if(objs[i]->os_index > nb_nodes){ if(vl >= CRITICAL){ fprintf(stderr, "Index of object %d of level %d is %d and larger than number of nodes : %d\n", i, depth, objs[i]->os_index, nb_nodes); } exit(-1); } res->node_id[depth][i] = objs[i]->os_index; res->node_rank[depth][objs[i]->os_index] = i; /* if(depth==topodepth-1) */ } FREE(objs); } cost = (double*)CALLOC(res->nb_levels,sizeof(double)); for(l=0; l<res->nb_levels; l++){ cost[l] = link_cost(l); } res->cost = cost; /* Destroy topology object. */ hwloc_topology_destroy(topology); if(tm_get_verbose_level() >= INFO) printf("\n"); return res; }