QUARK  0.9.0
QUARK-QUeuingAndRuntimeforKernels
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
quarkos-hwloc.c
Go to the documentation of this file.
1 
18 #include <hwloc.h>
19 #include <stdlib.h>
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 #ifdef QUARK_HWLOC
26 
27 static hwloc_topology_t quark_topology = NULL; /* Topology object */
28 static volatile int quark_nbr = 0;
29 
30 void quark_topology_init(){
31 
32  pthread_mutex_lock(&mutextopo);
33  if (!topo_initialized) {
34 
35  /* Allocate and initialize topology object. */
36  hwloc_topology_init(&quark_topology);
37 
38  /* Perform the topology detection. */
39  hwloc_topology_load(quark_topology);
40 
41  /* Get the number of cores (We don't want to use HyperThreading */
42  sys_corenbr = hwloc_get_nbobjs_by_type(quark_topology, HWLOC_OBJ_CORE);
43 
44  topo_initialized = 1;
45  }
46  quark_nbr++;
47  pthread_mutex_unlock(&mutextopo);
48 }
49 
51 
52  pthread_mutex_lock(&mutextopo);
53  quark_nbr--;
54  if ((topo_initialized ==1) && (quark_nbr == 0)) {
55  /* Destroy tpology */
56  hwloc_topology_destroy(quark_topology);
57 
58  topo_initialized = 0;
59  }
60  pthread_mutex_unlock(&mutextopo);
61 }
62 
72 int quark_setaffinity(int rank) {
73  hwloc_obj_t obj; /* Hwloc object */
74  hwloc_cpuset_t cpuset; /* HwLoc cpuset */
75 
76  if (!topo_initialized) {
77  /* quark_error("quark_setaffinity", "Topology not initialized"); */
78  return -1;
79  }
80 
81  /* Get last one. */
82  obj = hwloc_get_obj_by_type(quark_topology, HWLOC_OBJ_CORE, rank);
83  if (!obj)
84  return -1;
85 
86  /* Get a copy of its cpuset that we may modify. */
87  /* Get only one logical processor (in case the core is SMT/hyperthreaded). */
88 #if !defined(HWLOC_BITMAP_H)
89  cpuset = hwloc_cpuset_dup(obj->cpuset);
90  hwloc_cpuset_singlify(cpuset);
91 #else
92  cpuset = hwloc_bitmap_dup(obj->cpuset);
93  hwloc_bitmap_singlify(cpuset);
94 #endif
95 
96  /* And try to bind ourself there. */
97  if (hwloc_set_cpubind(quark_topology, cpuset, HWLOC_CPUBIND_THREAD)) {
98  char *str = NULL;
99 #if !defined(HWLOC_BITMAP_H)
100  hwloc_cpuset_asprintf(&str, obj->cpuset);
101 #else
102  hwloc_bitmap_asprintf(&str, obj->cpuset);
103 #endif
104  printf("Couldn't bind to cpuset %s\n", str);
105  free(str);
106  return -1;
107  }
108 
109  /* Get the number at Proc level ( We don't want to use HyperThreading ) */
110  rank = obj->children[0]->os_index;
111 
112  /* Free our cpuset copy */
113 #if !defined(HWLOC_BITMAP_H)
114  hwloc_cpuset_free(cpuset);
115 #else
116  hwloc_bitmap_free(cpuset);
117 #endif
118 
119  return 0;
120 }
121 
122 #ifdef __cplusplus
123 }
124 #endif
125 
126 #endif /* QUARK_HAS_COMPLEX */