int main (void) { hwloc_topology_t topology; unsigned depth; hwloc_obj_t last; hwloc_obj_t *closest; unsigned found; int err; unsigned numprocs; hwloc_obj_t ancestor; err = hwloc_topology_init (&topology); if (err) return EXIT_FAILURE; hwloc_topology_set_synthetic (topology, "2 3 4 5"); err = hwloc_topology_load (topology); if (err) return EXIT_FAILURE; depth = hwloc_topology_get_depth(topology); /* get the last object of last level */ numprocs = hwloc_get_nbobjs_by_depth(topology, depth-1); last = hwloc_get_obj_by_depth(topology, depth-1, numprocs-1); /* allocate the array of closest objects */ closest = malloc(numprocs * sizeof(*closest)); assert(closest); /* get closest levels */ found = hwloc_get_closest_objs (topology, last, closest, numprocs); printf("looked for %u closest entries, found %u\n", numprocs, found); assert(found == numprocs-1); /* check first found is closest */ assert(closest[0] == hwloc_get_obj_by_depth(topology, depth-1, numprocs-5 /* arity is 5 on last level */)); /* check some other expected positions */ assert(closest[found-1] == hwloc_get_obj_by_depth(topology, depth-1, 1*3*4*5-1 /* last of first half */)); assert(closest[found/2-1] == hwloc_get_obj_by_depth(topology, depth-1, 1*3*4*5+2*4*5-1 /* last of second third of second half */)); assert(closest[found/2/3-1] == hwloc_get_obj_by_depth(topology, depth-1, 1*3*4*5+2*4*5+3*5-1 /* last of third quarter of third third of second half */)); /* get ancestor of last and less close object */ ancestor = hwloc_get_common_ancestor_obj(topology, last, closest[found-1]); assert(hwloc_obj_is_in_subtree(topology, last, ancestor)); assert(hwloc_obj_is_in_subtree(topology, closest[found-1], ancestor)); assert(ancestor == hwloc_get_root_obj(topology)->first_child); printf("ancestor type %u depth %u number %u is system level\n", ancestor->type, ancestor->depth, ancestor->logical_index); free(closest); hwloc_topology_destroy (topology); return EXIT_SUCCESS; }
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); hwloc_topology_ignore_all_keep_structure(topology); hwloc_topology_load(topology); /* Test if symetric */ if(!symetric(topology)){ if(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_levels = topodepth; res->node_id = (int**)MALLOC(sizeof(int*)*res->nb_levels); res->nb_nodes = (int*)MALLOC(sizeof(int)*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); 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; /* 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; /* if(depth==topodepth-1) */ } FREE(objs); } /* Destroy HWLOC topology object. */ hwloc_topology_destroy(topology); /* printf("\n"); */ return res; }
int topo_nb_proc(hwloc_topology_t topology,int N) { hwloc_obj_t *objs = NULL; int nb_proc; objs = (hwloc_obj_t*)MALLOC(sizeof(hwloc_obj_t)*N); objs[0] = hwloc_get_next_obj_by_type(topology,HWLOC_OBJ_PU,NULL); nb_proc = 1 + hwloc_get_closest_objs(topology,objs[0],objs+1,N-1); FREE(objs); return nb_proc; }
tm_topology_t* hwloc_to_tm(char *filename,double **pcost) { hwloc_topology_t topology; tm_topology_t *res = NULL; hwloc_obj_t *objs = NULL; unsigned topodepth,depth; int nb_nodes,i; double *cost; int err; /* Build the topology */ hwloc_topology_init(&topology); err = hwloc_topology_set_xml(topology,filename); if(err == -1){ if(get_verbose_level() >= CRITICAL) fprintf(stderr,"Error: %s is a bad xml topology file!\n",filename); exit(-1); } hwloc_topology_ignore_all_keep_structure(topology); hwloc_topology_load(topology); /* Test if symetric */ if(!symetric(topology)){ if(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->nb_levels = topodepth; res->node_id = (int**)MALLOC(sizeof(int*)*res->nb_levels); res->nb_nodes = (int*)MALLOC(sizeof(int)*res->nb_levels); res->arity = (int*)MALLOC(sizeof(int)*res->nb_levels); if(get_verbose_level() >= 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); 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(get_verbose_level() >= INFO) printf("%d(%d):",res->arity[depth],nb_nodes); /* Build process id tab */ for (i = 0; i < nb_nodes; i++){ res->node_id[depth][i] = objs[i]->os_index; /* if(depth==topodepth-1) */ } FREE(objs); } cost = (double*)CALLOC(res->nb_levels,sizeof(double)); for(i=0; i<res->nb_levels; i++){ cost[i] = speed(i); } *pcost = cost; /* Destroy topology object. */ hwloc_topology_destroy(topology); if(get_verbose_level() >= INFO) printf("\n"); return res; }
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; }
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; }