1 /*
2  * Copyright © 2016-2017 Inria.  All rights reserved.
3  * See COPYING in top-level directory.
4  */
5 
6 #include "hwloc.h"
7 
8 #include <stdio.h>
9 #include <assert.h>
10 
11 #define LEN 1048576
12 
main(void)13 int main(void)
14 {
15   const struct hwloc_topology_support *support;
16   char *buffer;
17   hwloc_topology_t topology;
18   hwloc_bitmap_t set = hwloc_bitmap_alloc();
19   hwloc_bitmap_t total = hwloc_bitmap_alloc();
20   hwloc_obj_t node;
21   char *s;
22   int err;
23 
24   err = hwloc_topology_init(&topology);
25   assert(!err);
26   err = hwloc_topology_load(topology);
27   assert(!err);
28 
29   support = hwloc_topology_get_support(topology);
30   if (!support->membind->get_area_memlocation)
31     goto out;
32 
33   buffer = hwloc_alloc(topology, LEN);
34   assert(buffer);
35   printf("buffer %p length %d\n", buffer, LEN);
36 
37   err = hwloc_get_area_memlocation(topology, buffer, LEN, set, HWLOC_MEMBIND_BYNODESET);
38   if (err < 0 && errno == ENOSYS) {
39     fprintf(stderr, "hwloc_get_area_memlocation() failed with ENOSYS, aborting\n");
40     goto out_with_buffer;
41   }
42   assert(!err);
43   hwloc_bitmap_asprintf(&s, set);
44   printf("address %p length %d allocated in nodeset %s\n", buffer, LEN, s);
45   free(s);
46   hwloc_bitmap_copy(total, set);
47 
48   node = NULL;
49  next1:
50   node = hwloc_get_next_obj_by_type(topology, HWLOC_OBJ_NUMANODE, node);
51   if (!node)
52     goto out_with_buffer;
53   if (!node->attr->numanode.local_memory)
54     goto next1;
55   printf("binding to 1st node and touching 1st quarter\n");
56   err = hwloc_set_area_membind(topology, buffer, LEN, node->nodeset, HWLOC_MEMBIND_BIND, HWLOC_MEMBIND_BYNODESET);
57   if (err < 0 && errno == ENOSYS) {
58     fprintf(stderr, "hwloc_set_area_membind() failed with ENOSYS, aborting\n");
59     goto out_with_buffer;
60   }
61   assert(!err);
62 
63   memset(buffer, 0, LEN/4);
64   err = hwloc_get_area_memlocation(topology, buffer, 1, set, HWLOC_MEMBIND_BYNODESET);
65   assert(!err);
66   hwloc_bitmap_asprintf(&s, set);
67   printf("address %p length %d allocated in nodeset %s\n", buffer, LEN/4, s);
68   free(s);
69   hwloc_bitmap_or(total, total, set);
70 
71  next2:
72   node = hwloc_get_next_obj_by_type(topology, HWLOC_OBJ_NUMANODE, node);
73   if (!node)
74     goto out_with_nomorenodes;
75   if (!node->attr->numanode.local_memory)
76     goto next2;
77   printf("binding to 2nd node and touching 2nd quarter\n");
78   err = hwloc_set_area_membind(topology, buffer, LEN, node->nodeset, HWLOC_MEMBIND_BIND, HWLOC_MEMBIND_BYNODESET);
79   assert(!err);
80 
81   memset(buffer+LEN/4, 0, LEN/4);
82   err = hwloc_get_area_memlocation(topology, buffer+LEN/4, LEN/4, set, HWLOC_MEMBIND_BYNODESET);
83   assert(!err);
84   hwloc_bitmap_asprintf(&s, set);
85   printf("address %p length %d allocated in nodeset %s\n", buffer+LEN/4, LEN/4, s);
86   free(s);
87   hwloc_bitmap_or(total, total, set);
88 
89  next3:
90   node = hwloc_get_next_obj_by_type(topology, HWLOC_OBJ_NUMANODE, node);
91   if (!node)
92     goto out_with_nomorenodes;
93   if (!node->attr->numanode.local_memory)
94     goto next3;
95   printf("binding to 3rd node and touching 3rd quarter\n");
96   err = hwloc_set_area_membind(topology, buffer, LEN, node->nodeset, HWLOC_MEMBIND_BIND, HWLOC_MEMBIND_BYNODESET);
97   assert(!err);
98 
99   memset(buffer+LEN/2, 0, LEN/4);
100   err = hwloc_get_area_memlocation(topology, buffer+LEN/2, LEN/4, set, HWLOC_MEMBIND_BYNODESET);
101   assert(!err);
102   hwloc_bitmap_asprintf(&s, set);
103   printf("address %p length %d allocated in nodeset %s\n", buffer+LEN/2, LEN/4, s);
104   free(s);
105   hwloc_bitmap_or(total, total, set);
106 
107  next4:
108   node = hwloc_get_next_obj_by_type(topology, HWLOC_OBJ_NUMANODE, node);
109   if (!node)
110     goto out_with_nomorenodes;
111   if (!node->attr->numanode.local_memory)
112     goto next4;
113   printf("binding to 4th node and touching 4th quarter\n");
114   err = hwloc_set_area_membind(topology, buffer, LEN, node->nodeset, HWLOC_MEMBIND_BIND, HWLOC_MEMBIND_BYNODESET);
115   assert(!err);
116 
117   memset(buffer+3*LEN/4, 0, LEN/4);
118   err = hwloc_get_area_memlocation(topology, buffer+3*LEN/4, LEN/4, set, HWLOC_MEMBIND_BYNODESET);
119   assert(!err);
120   hwloc_bitmap_asprintf(&s, set);
121   printf("address %p length %d allocated in nodeset %s\n", buffer+3*LEN/4, LEN/4, s);
122   free(s);
123   hwloc_bitmap_or(total, total, set);
124 
125  out_with_nomorenodes:
126   err = hwloc_get_area_memlocation(topology, buffer, LEN, set, HWLOC_MEMBIND_BYNODESET);
127   assert(!err);
128   hwloc_bitmap_asprintf(&s, set);
129   printf("address %p length %d located on %s\n", buffer, LEN, s);
130   free(s);
131   assert(hwloc_bitmap_isincluded(total, set));
132 
133  out_with_buffer:
134   hwloc_free(topology, buffer, LEN);
135 
136  out:
137   hwloc_topology_destroy(topology);
138   hwloc_bitmap_free(set);
139   hwloc_bitmap_free(total);
140   return 0;
141 }
142