1 /*
2  * Copyright © 2009 CNRS
3  * Copyright © 2009-2017 Inria.  All rights reserved.
4  * Copyright © 2009-2010 Université Bordeaux
5  * Copyright © 2011 Cisco Systems, Inc.  All rights reserved.
6  * See COPYING in top-level directory.
7  */
8 
9 #include "hwloc.h"
10 
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <assert.h>
15 
16 /*
17  * check hwloc_get_closest_objs()
18  *
19  * - get the last object of the last level
20  * - get all closest objects
21  * - get the common ancestor of last level and its less close object.
22  * - check that the ancestor is the system level
23  */
24 
25 int
main(void)26 main (void)
27 {
28   hwloc_topology_t topology;
29   int depth;
30   hwloc_obj_t last;
31   hwloc_obj_t *closest;
32   unsigned found;
33   int err;
34   unsigned numprocs;
35   hwloc_obj_t ancestor;
36 
37   err = hwloc_topology_init (&topology);
38   if (err)
39     return EXIT_FAILURE;
40 
41   hwloc_topology_set_synthetic (topology, "2 3 4 5");
42 
43   err = hwloc_topology_load (topology);
44   if (err)
45     return EXIT_FAILURE;
46 
47   depth = hwloc_topology_get_depth(topology);
48 
49   /* get the last object of last level */
50   numprocs =  hwloc_get_nbobjs_by_depth(topology, depth-1);
51   last = hwloc_get_obj_by_depth(topology, depth-1, numprocs-1);
52 
53   /* allocate the array of closest objects */
54   closest = malloc(numprocs * sizeof(*closest));
55   assert(closest);
56 
57   /* get closest levels */
58   found = hwloc_get_closest_objs (topology, last, closest, numprocs);
59   printf("looked for %u closest entries, found %u\n", numprocs, found);
60   assert(found == numprocs-1);
61 
62   /* check first found is closest */
63   assert(closest[0] == hwloc_get_obj_by_depth(topology, depth-1, numprocs-5 /* arity is 5 on last level */));
64   /* check some other expected positions */
65   assert(closest[found-1] == hwloc_get_obj_by_depth(topology, depth-1, 1*3*4*5-1 /* last of first half */));
66   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 */));
67   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 */));
68 
69   /* get ancestor of last and less close object */
70   ancestor = hwloc_get_common_ancestor_obj(topology, last, closest[found-1]);
71   assert(hwloc_obj_is_in_subtree(topology, last, ancestor));
72   assert(hwloc_obj_is_in_subtree(topology, closest[found-1], ancestor));
73   assert(ancestor == hwloc_get_root_obj(topology));
74   printf("ancestor type %d (%s) depth %d number %u is the root object\n",
75 	 (int) ancestor->type, hwloc_obj_type_string(ancestor->type), ancestor->depth, ancestor->logical_index);
76 
77   free(closest);
78   hwloc_topology_destroy (topology);
79 
80   return EXIT_SUCCESS;
81 }
82