/
vp_manager.cpp
188 lines (171 loc) · 5.9 KB
/
vp_manager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* vp_manager.cpp
*
* This file is part of NEST.
*
* Copyright (C) 2004 The NEST Initiative
*
* NEST is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* NEST is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with NEST. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "vp_manager.h"
// Includes from libnestutil:
#include "logging.h"
// Includes from nestkernel:
#include "kernel_manager.h"
#include "mpi_manager.h"
#include "mpi_manager_impl.h"
#include "vp_manager_impl.h"
// Includes from sli:
#include "dictutils.h"
nest::VPManager::VPManager()
#ifdef _OPENMP
: force_singlethreading_( false )
#else
: force_singlethreading_( true )
#endif
, n_threads_( 1 )
{
}
void
nest::VPManager::initialize()
{
// When the VPManager is initialized, you will have 1 thread again.
// Setting more threads will be done via nest::set_kernel_status
#ifdef _OPENMP
/* The next line is required because we use the OpenMP
threadprivate() directive in the allocator, see OpenMP
API Specifications v 3.1, Ch 2.9.2, p 89, l 14f.
It keeps OpenMP from automagically changing the number
of threads used for parallel regions.
*/
omp_set_dynamic( false );
#endif
set_num_threads( 1 );
}
void
nest::VPManager::finalize()
{
}
void
nest::VPManager::set_status( const DictionaryDatum& d )
{
long n_threads = get_num_threads();
bool n_threads_updated =
updateValue< long >( d, "local_num_threads", n_threads );
if ( n_threads_updated )
{
if ( kernel().node_manager.size() > 1 )
throw KernelException(
"Nodes exist: Thread/process number cannot be changed." );
if ( kernel().model_manager.has_user_models() )
throw KernelException(
"Custom neuron models exist: Thread/process number cannot be "
"changed." );
if ( kernel().model_manager.has_user_prototypes() )
throw KernelException(
"Custom synapse types exist: Thread/process number cannot be "
"changed." );
if ( kernel().connection_manager.get_user_set_delay_extrema() )
throw KernelException(
"Delay extrema have been set: Thread/process number cannot be "
"changed." );
if ( kernel().simulation_manager.has_been_simulated() )
throw KernelException(
"The network has been simulated: Thread/process number cannot be "
"changed." );
if ( not Time::resolution_is_default() )
throw KernelException(
"The resolution has been set: Thread/process number cannot be "
"changed." );
if ( kernel().model_manager.are_model_defaults_modified() )
throw KernelException(
"Model defaults have been modified: Thread/process number cannot be "
"changed." );
if ( n_threads > 1 && force_singlethreading_ )
{
LOG( M_WARNING,
"VPManager::set_status",
"No multithreading available, using single threading" );
n_threads = 1;
}
// it is essential to call reset() here to adapt memory pools and more
// to the new number of threads and VPs.
set_num_threads( n_threads );
kernel().num_threads_changed_reset();
}
long n_vps = get_num_virtual_processes();
bool n_vps_updated =
updateValue< long >( d, "total_num_virtual_procs", n_vps );
if ( n_vps_updated )
{
if ( kernel().node_manager.size() > 1 )
throw KernelException(
"Nodes exist: Thread/process number cannot be changed." );
if ( kernel().model_manager.has_user_models() )
throw KernelException(
"Custom neuron models exist: Thread/process number cannot be "
"changed." );
if ( kernel().model_manager.has_user_prototypes() )
throw KernelException(
"Custom synapse types exist: Thread/process number cannot be "
"changed." );
if ( kernel().connection_manager.get_user_set_delay_extrema() )
throw KernelException(
"Delay extrema have been set: Thread/process number cannot be "
"changed." );
if ( kernel().simulation_manager.has_been_simulated() )
throw KernelException(
"The network has been simulated: Thread/process number cannot be "
"changed." );
if ( not Time::resolution_is_default() )
throw KernelException(
"The resolution has been set: Thread/process number cannot be "
"changed." );
if ( kernel().model_manager.are_model_defaults_modified() )
throw KernelException(
"Model defaults have been modified: Thread/process number cannot be "
"changed." );
if ( n_vps % kernel().mpi_manager.get_num_processes() != 0 )
throw BadProperty(
"Number of virtual processes (threads*processes) must be an integer "
"multiple of the number of processes. Value unchanged." );
long n_threads = n_vps / kernel().mpi_manager.get_num_processes();
if ( ( n_threads > 1 ) && ( force_singlethreading_ ) )
{
LOG( M_WARNING,
"VPManager::set_status",
"No multithreading available, using single threading" );
n_threads = 1;
}
// it is essential to call reset() here to adapt memory pools and more
// to the new number of threads and VPs
set_num_threads( n_threads );
kernel().num_threads_changed_reset();
}
}
void
nest::VPManager::get_status( DictionaryDatum& d )
{
def< long >( d, "local_num_threads", get_num_threads() );
def< long >( d, "total_num_virtual_procs", get_num_virtual_processes() );
}
void
nest::VPManager::set_num_threads( nest::thread n_threads )
{
n_threads_ = n_threads;
#ifdef _OPENMP
omp_set_num_threads( n_threads_ );
#endif
}