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_cache_covering_cpuset() */
17 
18 #define SYNTHETIC_TOPOLOGY_DESCRIPTION "numa:6 pack:5 l2:4 core:3 pu:2" /* 736bits wide topology */
19 
main(void)20 int main(void)
21 {
22   hwloc_topology_t topology;
23   hwloc_obj_t obj, cache;
24   hwloc_bitmap_t set;
25 
26   hwloc_topology_init(&topology);
27   hwloc_topology_set_synthetic(topology, SYNTHETIC_TOPOLOGY_DESCRIPTION);
28   hwloc_topology_load(topology);
29 
30   /* check the cache above a given cpu */
31 #define CPUINDEX 180
32   set = hwloc_bitmap_alloc();
33   obj = hwloc_get_obj_by_type(topology, HWLOC_OBJ_PU, CPUINDEX);
34   assert(obj);
35   hwloc_bitmap_or(set, set, obj->cpuset);
36   cache = hwloc_get_cache_covering_cpuset(topology, set);
37   assert(cache);
38   assert(hwloc_obj_type_is_dcache(cache->type));
39   assert(cache->logical_index == CPUINDEX/2/3);
40   assert(hwloc_obj_is_in_subtree(topology, obj, cache));
41   hwloc_bitmap_free(set);
42 
43   /* check the cache above two nearby cpus */
44 #define CPUINDEX1 180
45 #define CPUINDEX2 183
46   set = hwloc_bitmap_alloc();
47   obj = hwloc_get_obj_by_type(topology, HWLOC_OBJ_PU, CPUINDEX1);
48   assert(obj);
49   hwloc_bitmap_or(set, set, obj->cpuset);
50   obj = hwloc_get_obj_by_type(topology, HWLOC_OBJ_PU, CPUINDEX2);
51   assert(obj);
52   hwloc_bitmap_or(set, set, obj->cpuset);
53   cache = hwloc_get_cache_covering_cpuset(topology, set);
54   assert(cache);
55   assert(hwloc_obj_type_is_dcache(cache->type));
56   assert(cache->logical_index == CPUINDEX1/2/3);
57   assert(cache->logical_index == CPUINDEX2/2/3);
58   assert(hwloc_obj_is_in_subtree(topology, obj, cache));
59   hwloc_bitmap_free(set);
60 
61   /* check no cache above two distant cpus */
62 #undef CPUINDEX1
63 #define CPUINDEX1 300
64   set = hwloc_bitmap_alloc();
65   obj = hwloc_get_obj_by_type(topology, HWLOC_OBJ_PU, CPUINDEX1);
66   assert(obj);
67   hwloc_bitmap_or(set, set, obj->cpuset);
68   obj = hwloc_get_obj_by_type(topology, HWLOC_OBJ_PU, CPUINDEX2);
69   assert(obj);
70   hwloc_bitmap_or(set, set, obj->cpuset);
71   cache = hwloc_get_cache_covering_cpuset(topology, set);
72   assert(!cache);
73   hwloc_bitmap_free(set);
74 
75   /* check no cache above higher level */
76   set = hwloc_bitmap_alloc();
77   obj = hwloc_get_obj_by_type(topology, HWLOC_OBJ_PACKAGE, 0);
78   assert(obj);
79   hwloc_bitmap_or(set, set, obj->cpuset);
80   cache = hwloc_get_cache_covering_cpuset(topology, set);
81   assert(!cache);
82   hwloc_bitmap_free(set);
83 
84   hwloc_topology_destroy(topology);
85 
86   return EXIT_SUCCESS;
87 }
88