ADS
Algorithms and Data Structures
Toggle main menu visibility
Loading...
Searching...
No Matches
Affinity.h
Go to the documentation of this file.
1
/*--------------------------------------------------------------------------*/
2
/*---------------------------- File Affinity.h -----------------------------*/
3
/*--------------------------------------------------------------------------*/
52
/*--------------------------------------------------------------------------*/
53
/*----------------------------- DEFINITIONS --------------------------------*/
54
/*--------------------------------------------------------------------------*/
55
56
#ifndef __ADS_Affinity
57
#define __ADS_Affinity
58
/* self-identification: #endif constant */
59
60
/*--------------------------------------------------------------------------*/
61
/*------------------------------ INCLUDES ----------------------------------*/
62
/*--------------------------------------------------------------------------*/
63
64
#include <thread>
65
#include <vector>
66
67
#if defined( ADS_WITH_HWLOC )
68
#include <hwloc.h>
69
#endif
70
71
/*--------------------------------------------------------------------------*/
72
/*--------------------------- NAMESPACE ------------------------------------*/
73
/*--------------------------------------------------------------------------*/
74
75
namespace
ADS
76
{
77
79
namespace
detail
80
{
81
82
#if defined( ADS_WITH_HWLOC )
83
86
inline
hwloc_topology_t hwlocTopology(
void
)
87
{
88
struct
Topology
89
{
90
hwloc_topology_t t =
nullptr
;
91
Topology(
void
) {
92
if
( hwloc_topology_init( &t ) != 0 )
93
t =
nullptr
;
94
else
if
( hwloc_topology_load( t ) != 0 ) {
95
hwloc_topology_destroy( t );
96
t =
nullptr
;
97
}
98
}
99
~Topology() {
100
if
( t )
101
hwloc_topology_destroy( t );
102
}
103
};
104
static
const
Topology T;
105
return
( T.t );
106
}
107
109
struct
HwlocSet
110
{
111
hwloc_bitmap_t s = hwloc_bitmap_alloc();
112
HwlocSet(
void
) =
default
;
113
HwlocSet(
const
HwlocSet & ) =
delete
;
114
HwlocSet & operator=(
const
HwlocSet & ) =
delete
;
115
~HwlocSet() { hwloc_bitmap_free( s ); }
116
};
117
118
#endif
119
120
}
// end( namespace detail )
122
127
inline
const
std::vector< int > &
affinityCpus
(
void
)
128
{
129
static
const
std::vector< int > cpus = [] {
130
std::vector< int > c;
131
#if defined( ADS_WITH_HWLOC )
132
const
hwloc_topology_t t = detail::hwlocTopology();
133
detail::HwlocSet S;
134
if
( t && S.s &&
135
( ( hwloc_get_cpubind( t , S.s , HWLOC_CPUBIND_THREAD ) == 0 ) ||
136
( hwloc_bitmap_copy( S.s , hwloc_topology_get_allowed_cpuset( t ) ) ==
137
0 ) ) )
138
for
(
int
i = hwloc_bitmap_first( S.s ) ; i >= 0 ;
139
i = hwloc_bitmap_next( S.s , i ) )
140
c.push_back( i );
141
#endif
142
return
( c );
143
}();
144
return
( cpus );
145
}
146
150
inline
unsigned
availableCpus
(
void
)
151
{
152
const
std::vector< int > & c =
affinityCpus
();
153
return
( c.empty() ? std::thread::hardware_concurrency()
154
:
unsigned
( c.size() ) );
155
}
156
172
inline
bool
pinThread
(
unsigned
i )
173
{
174
const
std::vector< int > & c =
affinityCpus
();
175
if
( c.empty() )
176
return
(
false
);
177
#if defined( ADS_WITH_HWLOC )
178
detail::HwlocSet S;
179
if
( ! S.s )
180
return
(
false
);
181
hwloc_bitmap_only( S.s ,
unsigned
( c[ i % c.size() ] ) );
182
return
( hwloc_set_cpubind( detail::hwlocTopology() , S.s ,
183
HWLOC_CPUBIND_THREAD ) == 0 );
184
#else
185
( void )i;
186
return
(
false
);
187
#endif
188
}
189
192
inline
int
currentCpu
(
void
)
193
{
194
#if defined( ADS_WITH_HWLOC )
195
const
hwloc_topology_t t = detail::hwlocTopology();
196
detail::HwlocSet S;
197
if
( t && S.s &&
198
( hwloc_get_last_cpu_location( t , S.s , HWLOC_CPUBIND_THREAD ) == 0 ) )
199
return
( hwloc_bitmap_first( S.s ) );
200
#endif
201
return
( -1 );
202
}
203
204
/*--------------------------------------------------------------------------*/
205
206
}
// end( namespace ADS )
207
208
/*--------------------------------------------------------------------------*/
209
210
#endif
/* Affinity.h included */
211
212
/*--------------------------------------------------------------------------*/
213
/*--------------------------- End File Affinity.h --------------------------*/
214
/*--------------------------------------------------------------------------*/
ADS::affinityCpus
const std::vector< int > & affinityCpus(void)
the processors on which the process may run, in increasing order, as the first call found them: those...
Definition
Affinity.h:127
ADS::availableCpus
unsigned availableCpus(void)
the number of processors on which the process may run, that of affinityCpus() where it is not empty a...
Definition
Affinity.h:150
ADS::pinThread
bool pinThread(unsigned i)
binds the calling thread to the processor of affinityCpus(), their number; true if the thread is bo...
Definition
Affinity.h:172
ADS::currentCpu
int currentCpu(void)
the processor on which the calling thread runs now, where it is not known
Definition
Affinity.h:192
ADS
Definition
ADT.h:96
ADS
parallel
Affinity.h
Generated by
1.17.0