seams-core v2.14.0
libyodaLib, the C++ engine of d-SEAMS
Loading...
Searching...
No Matches
seams::TeamWidth Struct Reference

#include <src/include/internal/thread_team.hpp>

Public Member Functions

 TeamWidth (int n)
 ~TeamWidth ()
 TeamWidth (const TeamWidth &)=delete
TeamWidth & operator= (const TeamWidth &)=delete

Public Attributes

int saved = 1
bool on = false

Detailed Description

Definition at line 139 of file thread_team.hpp.

Constructor & Destructor Documentation

◆ TeamWidth() [1/2]

seams::TeamWidth::TeamWidth ( int n)
inlineexplicit

Definition at line 145 of file thread_team.hpp.

145 {
146#ifdef SEAMS_HAS_OPENMP
147 if (!omp_in_parallel()) {
148 saved = std::max(1, omp_get_max_threads());
149 const int want = teamFor(n);
150 if (want != saved) {
151 omp_set_num_threads(want);
152 on = true;
153 }
154#ifdef __linux__
155 // A four-thread team on an eight-core mask still migrates across
156 // all eight. Bind it to the first cores the process is allowed,
157 // before the workers are created, so they inherit that mask.
158 if (want > 0 && want < saved &&
159 sched_getaffinity(0, sizeof(oldMask), &oldMask) == 0) {
160 // CPU_ISSET and CPU_SET take the cpu number as a size_t.
161 std::size_t cpus[256];
162 int ncpu = 0;
163 for (std::size_t cpu = 0; cpu < CPU_SETSIZE && ncpu < want && ncpu < 256; ++cpu) {
164 if (CPU_ISSET(cpu, &oldMask)) {
165 cpus[ncpu++] = cpu;
166 }
167 }
168 if (ncpu > 0) {
169 // Once per process. Repeating the bind on a call of a few
170 // hundred microseconds was the whole gap between four threads
171 // requested directly and eight narrowed to four. OpenMP's own
172 // bind would put the workers back on the whole allowed set,
173 // so it is turned off before the team is created.
174 // Thread 0 is the caller and stays on its own mask: a pinned
175 // caller hands its one cpu to every OpenMP worker and Rayon
176 // thread it creates later, and the next eight-thread region
177 // then runs five threads on that cpu.
178 static int bound = 0;
179 static std::mutex boundMu;
180 const std::lock_guard<std::mutex> lock(boundMu);
181 if (bound != want) {
182 setenv("OMP_PROC_BIND", "false", 1);
183#pragma omp parallel num_threads(want)
184 {
185 const int tid = omp_get_thread_num();
186 if (tid != 0) {
187 cpu_set_t mask;
188 CPU_ZERO(&mask);
189 CPU_SET(cpus[tid < ncpu ? tid : 0], &mask);
190 sched_setaffinity(0, sizeof(mask), &mask);
191 }
192 }
193 bound = want;
194 }
195 }
196 }
197#endif
198 }
199#else
200 (void)n;
201#endif
202 }
int teamFor(int n)

◆ ~TeamWidth()

seams::TeamWidth::~TeamWidth ( )
inline

Definition at line 203 of file thread_team.hpp.

203 {
204#ifdef SEAMS_HAS_OPENMP
205 if (on) {
206 omp_set_num_threads(saved);
207 }
208#endif
209 }

◆ TeamWidth() [2/2]

seams::TeamWidth::TeamWidth ( const TeamWidth & )
delete

Member Function Documentation

◆ operator=()

TeamWidth & seams::TeamWidth::operator= ( const TeamWidth & )
delete

Member Data Documentation

◆ on

bool seams::TeamWidth::on = false

Definition at line 141 of file thread_team.hpp.

◆ saved

int seams::TeamWidth::saved = 1

Definition at line 140 of file thread_team.hpp.


The documentation for this struct was generated from the following file: