1 /*
2  * Copyright © 2009 CNRS
3  * Copyright © 2009-2017 Inria.  All rights reserved.
4  * Copyright © 2009 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 /* check hwloc_get_largest_objs_inside_cpuset()
17  * and hwloc_get_first_largest_obj_inside_cpuset()
18  */
19 
20 #define SYNTHETIC_TOPOLOGY_DESCRIPTION "numa:6 pack:5 l2:4 core:3 pu:2" /* 736bits wide topology */
21 
22 #define GIVEN_LARGESPLIT_CPUSET_STRING "8000,,,,,,,,,,,,,,,,,,,,,,1" /* first and last(735th) bit set */
23 #define GIVEN_TOOLARGE_CPUSET_STRING "10000,,,,,,,,,,,,,,,,,,,,,,0" /* 736th bit is too large for the 720-wide topology */
24 #define GIVEN_HARD_CPUSET_STRING "07ff,ffffffff,e0000000"
25 
26 #define OBJ_MAX 16
27 
main(void)28 int main(void)
29 {
30   hwloc_topology_t topology;
31   hwloc_obj_t objs[OBJ_MAX];
32   hwloc_obj_t obj;
33   hwloc_bitmap_t set;
34   unsigned pus;
35   int ret;
36 
37   hwloc_topology_init(&topology);
38   hwloc_topology_set_synthetic(topology, SYNTHETIC_TOPOLOGY_DESCRIPTION);
39   hwloc_topology_load(topology);
40 
41   pus = hwloc_get_nbobjs_by_type(topology, HWLOC_OBJ_PU);
42 
43   /* just get the system object */
44   obj = hwloc_get_root_obj(topology);
45   ret = hwloc_get_largest_objs_inside_cpuset(topology, obj->cpuset, objs, 1);
46   assert(ret == 1);
47   assert(objs[0] == obj);
48   objs[0] = hwloc_get_first_largest_obj_inside_cpuset(topology, obj->cpuset);
49   assert(objs[0] == obj);
50 
51   /* just get the very last object */
52   obj = hwloc_get_obj_by_type(topology, HWLOC_OBJ_PU, pus-1);
53   ret = hwloc_get_largest_objs_inside_cpuset(topology, obj->cpuset, objs, 1);
54   assert(ret == 1);
55   assert(objs[0] == obj);
56 
57   /* try an empty one */
58   set = hwloc_bitmap_alloc();
59   ret = hwloc_get_largest_objs_inside_cpuset(topology, set, objs, 1);
60   assert(ret == 0);
61   objs[0] = hwloc_get_first_largest_obj_inside_cpuset(topology, set);
62   assert(objs[0] == NULL);
63   hwloc_bitmap_free(set);
64 
65   /* try an impossible one */
66   set = hwloc_bitmap_alloc();
67   hwloc_bitmap_sscanf(set, GIVEN_TOOLARGE_CPUSET_STRING);
68   ret = hwloc_get_largest_objs_inside_cpuset(topology, set, objs, 1);
69   assert(ret == -1);
70   objs[0] = hwloc_get_first_largest_obj_inside_cpuset(topology, set);
71   assert(objs[0] == NULL);
72   hwloc_bitmap_free(set);
73 
74   /* try a harder one with 1 obj instead of 2 needed */
75   set = hwloc_bitmap_alloc();
76   hwloc_bitmap_sscanf(set, GIVEN_LARGESPLIT_CPUSET_STRING);
77   ret = hwloc_get_largest_objs_inside_cpuset(topology, set, objs, 1);
78   assert(ret == 1);
79   assert(objs[0] == hwloc_get_obj_by_type(topology, HWLOC_OBJ_PU, 0));
80   objs[0] = hwloc_get_first_largest_obj_inside_cpuset(topology, set);
81   assert(objs[0] == hwloc_get_obj_by_type(topology, HWLOC_OBJ_PU, 0));
82   /* try a harder one with lots of objs instead of 2 needed */
83   ret = hwloc_get_largest_objs_inside_cpuset(topology, set, objs, 2);
84   assert(ret == 2);
85   assert(objs[0] == hwloc_get_obj_by_type(topology, HWLOC_OBJ_PU, 0));
86   assert(objs[1] == hwloc_get_obj_by_type(topology, HWLOC_OBJ_PU, pus-1));
87   objs[0] = hwloc_get_first_largest_obj_inside_cpuset(topology, set);
88   hwloc_bitmap_andnot(set, set, objs[0]->cpuset);
89   objs[1] = hwloc_get_first_largest_obj_inside_cpuset(topology, set);
90   hwloc_bitmap_andnot(set, set, objs[1]->cpuset);
91   objs[2] = hwloc_get_first_largest_obj_inside_cpuset(topology, set);
92   assert(objs[0] == hwloc_get_obj_by_type(topology, HWLOC_OBJ_PU, 0));
93   assert(objs[1] == hwloc_get_obj_by_type(topology, HWLOC_OBJ_PU, pus-1));
94   assert(objs[2] == NULL);
95   assert(hwloc_bitmap_iszero(set));
96   hwloc_bitmap_free(set);
97 
98   /* try a very hard one */
99   set = hwloc_bitmap_alloc();
100   hwloc_bitmap_sscanf(set, GIVEN_HARD_CPUSET_STRING);
101   ret = hwloc_get_largest_objs_inside_cpuset(topology, set, objs, OBJ_MAX);
102   assert(objs[0] == hwloc_get_obj_by_type(topology, HWLOC_OBJ_PU, 29));
103   assert(objs[1] == hwloc_get_obj_by_type(topology, HWLOC_OBJ_L2CACHE, 5));
104   assert(objs[2] == hwloc_get_obj_by_type(topology, HWLOC_OBJ_L2CACHE, 6));
105   assert(objs[3] == hwloc_get_obj_by_type(topology, HWLOC_OBJ_L2CACHE, 7));
106   assert(objs[4] == hwloc_get_obj_by_type(topology, HWLOC_OBJ_PACKAGE, 2));
107   assert(objs[5] == hwloc_get_obj_by_type(topology, HWLOC_OBJ_CORE, 36));
108   assert(objs[6] == hwloc_get_obj_by_type(topology, HWLOC_OBJ_PU, 74));
109   hwloc_bitmap_free(set);
110 
111   hwloc_topology_destroy(topology);
112 
113   return EXIT_SUCCESS;
114 }
115