1 /*
2  * Copyright © 2018 Inria.  All rights reserved.
3  * See COPYING in top-level directory.
4  */
5 
6 #include "hwloc.h"
7 
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <stdint.h>
12 #include <assert.h>
13 
main(void)14 int main(void)
15 {
16   hwloc_topology_t topology;
17   hwloc_const_bitmap_t set;
18   hwloc_bitmap_t new = hwloc_bitmap_alloc();
19   int err;
20 
21   /* allow without HWLOC_TOPOLOGY_FLAG_INCLUDE_DISALLOWED should fail */
22   hwloc_topology_init(&topology);
23   hwloc_topology_set_synthetic(topology, "pack:5 node:3 core:2 pu:4");
24   hwloc_topology_load(topology);
25   err = hwloc_topology_allow(topology, NULL, NULL, HWLOC_ALLOW_FLAG_ALL);
26   assert(err == -1 && errno == EINVAL);
27   hwloc_topology_destroy(topology);
28 
29   hwloc_topology_init(&topology);
30   hwloc_topology_set_synthetic(topology, "pack:5 node:3 core:2 pu:4");
31   hwloc_topology_set_flags(topology, HWLOC_TOPOLOGY_FLAG_INCLUDE_DISALLOWED);
32   hwloc_topology_load(topology);
33 
34   set = hwloc_topology_get_allowed_cpuset(topology);
35   assert(set);
36   assert(hwloc_bitmap_weight(set) == 120);
37   assert(hwloc_bitmap_first(set) == 0);
38   assert(hwloc_bitmap_last(set) == 119);
39 
40   set = hwloc_topology_get_allowed_nodeset(topology);
41   assert(set);
42   assert(hwloc_bitmap_weight(set) == 15);
43   assert(hwloc_bitmap_first(set) == 0);
44   assert(hwloc_bitmap_last(set) == 14);
45 
46   /* HWLOC_ALLOW_FLAG_ALL doesn't want any set */
47   err = hwloc_topology_allow(topology, new, new, HWLOC_ALLOW_FLAG_ALL);
48   assert(err == -1 && errno == EINVAL);
49   /* HWLOC_ALLOW_FLAG_LOCAL_RESTRICTIONS doesn't want any set */
50   err = hwloc_topology_allow(topology, new, new, HWLOC_ALLOW_FLAG_LOCAL_RESTRICTIONS);
51   assert(err == -1 && errno == EINVAL);
52   /* HWLOC_ALLOW_FLAG_LOCAL_RESTRICTIONS doesn't work without thissystem */
53   assert(!hwloc_topology_is_thissystem(topology));
54   err = hwloc_topology_allow(topology, NULL, NULL, HWLOC_ALLOW_FLAG_LOCAL_RESTRICTIONS);
55   assert(err == -1 && errno == EINVAL);
56   /* HWLOC_ALLOW_FLAG_CUSTOM doesn't like empty sets */
57   hwloc_bitmap_zero(new);
58   err = hwloc_topology_allow(topology, new, new, HWLOC_ALLOW_FLAG_CUSTOM);
59   assert(err == -1 && errno == EINVAL);
60   err = hwloc_topology_allow(topology, NULL, new, HWLOC_ALLOW_FLAG_CUSTOM);
61   assert(err == -1 && errno == EINVAL);
62   err = hwloc_topology_allow(topology, new, NULL, HWLOC_ALLOW_FLAG_CUSTOM);
63   assert(err == -1 && errno == EINVAL);
64   /* HWLOC_ALLOW_FLAG_CUSTOM doesn't like sets outside of the machine */
65   hwloc_bitmap_set_range(new, 500, 600);
66   err = hwloc_topology_allow(topology, new, new, HWLOC_ALLOW_FLAG_CUSTOM);
67   assert(err == -1 && errno == EINVAL);
68   err = hwloc_topology_allow(topology, NULL, new, HWLOC_ALLOW_FLAG_CUSTOM);
69   assert(err == -1 && errno == EINVAL);
70   err = hwloc_topology_allow(topology, new, NULL, HWLOC_ALLOW_FLAG_CUSTOM);
71   assert(err == -1 && errno == EINVAL);
72 
73   /* allow a single bit */
74   hwloc_bitmap_only(new, 10);
75   err = hwloc_topology_allow(topology, new, new, HWLOC_ALLOW_FLAG_CUSTOM);
76   assert(!err);
77   set = hwloc_topology_get_allowed_cpuset(topology);
78   assert(set);
79   assert(hwloc_bitmap_weight(set) == 1);
80   assert(hwloc_bitmap_first(set) == 10);
81   assert(hwloc_bitmap_last(set) == 10);
82   set = hwloc_topology_get_allowed_nodeset(topology);
83   assert(set);
84   assert(hwloc_bitmap_weight(set) == 1);
85   assert(hwloc_bitmap_first(set) == 10);
86   assert(hwloc_bitmap_last(set) == 10);
87 
88   /* restrict the topology outside of the allowed set */
89   hwloc_bitmap_zero(new);
90   hwloc_bitmap_set_range(new, 40, 60);
91   err = hwloc_topology_restrict(topology, new, 0);
92   assert(err == -1 && errno == EINVAL);
93   hwloc_bitmap_zero(new);
94   hwloc_bitmap_set_range(new, 12, 14);
95   err = hwloc_topology_restrict(topology, new, HWLOC_RESTRICT_FLAG_BYNODESET);
96   assert(err == -1 && errno == EINVAL);
97 
98   /* allow all NUMAs and more PUs */
99   hwloc_bitmap_zero(new);
100   hwloc_bitmap_set_range(new, 0, 20);
101   err = hwloc_topology_allow(topology, new, new, HWLOC_ALLOW_FLAG_CUSTOM);
102   assert(!err);
103   set = hwloc_topology_get_allowed_cpuset(topology);
104   assert(set);
105   assert(hwloc_bitmap_weight(set) == 21);
106   assert(hwloc_bitmap_first(set) == 0);
107   assert(hwloc_bitmap_last(set) == 20);
108   set = hwloc_topology_get_allowed_nodeset(topology);
109   assert(set);
110   assert(hwloc_bitmap_weight(set) == 15);
111   assert(hwloc_bitmap_first(set) == 0);
112   assert(hwloc_bitmap_last(set) == 14);
113 
114   /* allow less NUMAs */
115   hwloc_bitmap_zero(new);
116   hwloc_bitmap_set_range(new, 6, 10);
117   err = hwloc_topology_allow(topology, NULL, new, HWLOC_ALLOW_FLAG_CUSTOM);
118   assert(!err);
119   set = hwloc_topology_get_allowed_cpuset(topology);
120   assert(set);
121   assert(hwloc_bitmap_weight(set) == 21);
122   assert(hwloc_bitmap_first(set) == 0);
123   assert(hwloc_bitmap_last(set) == 20);
124   set = hwloc_topology_get_allowed_nodeset(topology);
125   assert(set);
126   assert(hwloc_bitmap_weight(set) == 5);
127   assert(hwloc_bitmap_first(set) == 6);
128   assert(hwloc_bitmap_last(set) == 10);
129 
130   /* restrict to PUs 10-30 and all NUMAs */
131   hwloc_bitmap_zero(new);
132   hwloc_bitmap_set_range(new, 10, 30);
133   err = hwloc_topology_restrict(topology, new, 0);
134   assert(!err);
135   set = hwloc_topology_get_allowed_cpuset(topology);
136   assert(set);
137   assert(hwloc_bitmap_weight(set) == 11);
138   assert(hwloc_bitmap_first(set) == 10);
139   assert(hwloc_bitmap_last(set) == 20);
140   set = hwloc_topology_get_allowed_nodeset(topology);
141   assert(set);
142   assert(hwloc_bitmap_weight(set) == 5);
143   assert(hwloc_bitmap_first(set) == 6);
144   assert(hwloc_bitmap_last(set) == 10);
145 
146   /* restrict to NUMA 4-7 */
147   hwloc_bitmap_zero(new);
148   hwloc_bitmap_set_range(new, 4, 7);
149   err = hwloc_topology_restrict(topology, new, HWLOC_RESTRICT_FLAG_BYNODESET);
150   assert(!err);
151   set = hwloc_topology_get_allowed_cpuset(topology);
152   assert(set);
153   assert(hwloc_bitmap_weight(set) == 11);
154   assert(hwloc_bitmap_first(set) == 10);
155   assert(hwloc_bitmap_last(set) == 20);
156   set = hwloc_topology_get_allowed_nodeset(topology);
157   assert(set);
158   assert(hwloc_bitmap_weight(set) == 2);
159   assert(hwloc_bitmap_first(set) == 6);
160   assert(hwloc_bitmap_last(set) == 7);
161 
162   /* allow all */
163   hwloc_bitmap_fill(new);
164   err = hwloc_topology_allow(topology, new, new, HWLOC_ALLOW_FLAG_CUSTOM);
165   assert(!err);
166   set = hwloc_topology_get_allowed_cpuset(topology);
167   assert(set);
168   assert(hwloc_bitmap_weight(set) == 21);
169   assert(hwloc_bitmap_first(set) == 10);
170   assert(hwloc_bitmap_last(set) == 30);
171   set = hwloc_topology_get_allowed_nodeset(topology);
172   assert(set);
173   assert(hwloc_bitmap_weight(set) == 4);
174   assert(hwloc_bitmap_first(set) == 4);
175   assert(hwloc_bitmap_last(set) == 7);
176 
177   hwloc_topology_destroy(topology);
178   hwloc_bitmap_free(new);
179 
180   return 0;
181 }
182