Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
ThreadBindingMng.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* ThreadBindingMng.cc (C) 2000-2025 */
9/* */
10/* Thread manager for threads. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/impl/internal/ThreadBindingMng.h"
15
16#include "arcane/utils/ITraceMng.h"
18#include "arcane/utils/PlatformUtils.h"
19#include "arcane/utils/IProcessorAffinityService.h"
20#include "arccore/concurrency/internal/TaskFactoryInternal.h"
21
22/*---------------------------------------------------------------------------*/
23/*---------------------------------------------------------------------------*/
24
25namespace Arcane
26{
27
28/*---------------------------------------------------------------------------*/
29/*---------------------------------------------------------------------------*/
30
31ThreadBindingMng::
32ThreadBindingMng()
33: m_thread_created_callback(new ObserverT<ThreadBindingMng>(this, &ThreadBindingMng::_createThreadCallback))
34{
35}
36
37/*---------------------------------------------------------------------------*/
38/*---------------------------------------------------------------------------*/
39
40ThreadBindingMng::
41~ThreadBindingMng()
42{
43 finalize();
44 delete m_thread_created_callback;
45}
46
47/*---------------------------------------------------------------------------*/
48/*---------------------------------------------------------------------------*/
49
50void ThreadBindingMng::
51initialize(ITraceMng* tm, const String& strategy)
52{
53 m_trace_mng = tm;
54 m_bind_strategy = strategy;
55 // If the strategy is not null, it attaches to the TaskFactory observable
56 // to be notified of thread creation.
57 if (!m_bind_strategy.null()) {
58 if (m_bind_strategy != "Simple")
59 ARCANE_FATAL("Invalid strategy '{0}'. Valid values are : 'Simple'", m_bind_strategy);
60 m_max_thread = TaskFactory::nbAllowedThread();
61 if (tm)
62 tm->info() << "Thread binding strategy is '" << m_bind_strategy << "'";
63 TaskFactoryInternal::addThreadCreateObserver(m_thread_created_callback);
64 m_has_callback = true;
65 }
66}
67
68/*---------------------------------------------------------------------------*/
69/*---------------------------------------------------------------------------*/
70
71void ThreadBindingMng::
72finalize()
73{
74 if (m_has_callback) {
75 TaskFactoryInternal::removeThreadCreateObserver(m_thread_created_callback);
76 m_has_callback = false;
77 }
78}
79
80/*---------------------------------------------------------------------------*/
81/*---------------------------------------------------------------------------*/
82
83void ThreadBindingMng::
84_createThreadCallback()
85{
86 // TODO: do not exceed the maximum number of allocated threads, otherwise display a
87 // warning message
88 ITraceMng* tm = m_trace_mng;
89 Int32 thread_index = m_current_thread_index;
90 ++m_current_thread_index;
91
92 IProcessorAffinityService* pas = platform::getProcessorAffinityService();
93 if (!pas) {
94 if (tm)
95 tm->info() << "WARNING: Can not bind thread because there is no 'IProcessorAffinityService'";
96 return;
97 }
98
99 if (tm) {
100 if (thread_index < m_max_thread) {
101 pas->bindThread(thread_index);
102 tm->info() << "Binding thread index=" << thread_index << " cpuset=" << pas->cpuSetString();
103 }
104 else
105 tm->info() << "WARNING: thread index is greater than maximum number of allowed thread. No binding done";
106 }
107}
108
109/*---------------------------------------------------------------------------*/
110/*---------------------------------------------------------------------------*/
111
112} // End namespace Arcane
113
114/*---------------------------------------------------------------------------*/
115/*---------------------------------------------------------------------------*/
#define ARCANE_FATAL(...)
Macro throwing a FatalErrorException.
Classes, Types, and macros for managing concurrency.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --