1 /*
2  * Copyright © 2011-2017 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   static hwloc_topology_t oldtopology, topology;
17   hwloc_bitmap_t cpuset = hwloc_bitmap_alloc();
18   struct hwloc_distances_s *distances;
19   hwloc_obj_t nodes[3], cores[6];
20   hwloc_uint64_t node_distances[9], core_distances[36];
21   unsigned i,j,nr;
22   int err;
23 
24   hwloc_topology_init(&oldtopology);
25   hwloc_topology_set_synthetic(oldtopology, "node:3 core:2 pu:4");
26   hwloc_topology_load(oldtopology);
27 
28   for(i=0; i<3; i++) {
29     nodes[i] = hwloc_get_obj_by_type(oldtopology, HWLOC_OBJ_NUMANODE, i);
30     for(j=0; j<3; j++)
31       node_distances[i*3+j] = (i == j ? 10 : 20);
32   }
33   err = hwloc_distances_add(oldtopology, 3, nodes, node_distances,
34 			    HWLOC_DISTANCES_KIND_MEANS_LATENCY|HWLOC_DISTANCES_KIND_FROM_USER,
35 			    HWLOC_DISTANCES_ADD_FLAG_GROUP);
36   assert(!err);
37 
38   for(i=0; i<6; i++) {
39     cores[i] = hwloc_get_obj_by_type(oldtopology, HWLOC_OBJ_CORE, i);
40     for(j=0; j<6; j++)
41       core_distances[i*6+j] = (i == j ? 4 : 8);
42   }
43   err = hwloc_distances_add(oldtopology, 6, cores, core_distances,
44 			    HWLOC_DISTANCES_KIND_MEANS_LATENCY|HWLOC_DISTANCES_KIND_FROM_USER,
45 			    HWLOC_DISTANCES_ADD_FLAG_GROUP);
46   assert(!err);
47 
48   printf("duplicating\n");
49   err = hwloc_topology_dup(&topology, oldtopology);
50   assert(!err);
51   printf("destroying the old topology\n");
52   hwloc_topology_destroy(oldtopology);
53 
54   /* remove the entire third node */
55   printf("removing one node\n");
56   hwloc_bitmap_fill(cpuset);
57   hwloc_bitmap_clr_range(cpuset, 16, 23);
58   err = hwloc_topology_restrict(topology, cpuset, HWLOC_RESTRICT_FLAG_REMOVE_CPULESS);
59   assert(!err);
60   printf("checking the result\n");
61   assert(hwloc_get_nbobjs_by_type(topology, HWLOC_OBJ_NUMANODE) == 2);
62 
63   nr = 1;
64   err = hwloc_distances_get_by_type(topology, HWLOC_OBJ_NUMANODE, &nr, &distances, 0, 0);
65   assert(!err);
66   assert(nr == 1);
67   assert(distances->nbobjs == 2);
68   assert(distances->kind == (HWLOC_DISTANCES_KIND_MEANS_LATENCY|HWLOC_DISTANCES_KIND_FROM_USER));
69   hwloc_distances_release(topology, distances);
70 
71   nr = 1;
72   err = hwloc_distances_get_by_type(topology, HWLOC_OBJ_CORE, &nr, &distances, 0, 0);
73   assert(!err);
74   assert(nr == 1);
75   assert(distances->nbobjs == 4);
76   assert(distances->kind == (HWLOC_DISTANCES_KIND_MEANS_LATENCY|HWLOC_DISTANCES_KIND_FROM_USER));
77   hwloc_distances_release(topology, distances);
78 
79   hwloc_topology_destroy(topology);
80 
81   hwloc_bitmap_free(cpuset);
82   return 0;
83 }
84