Skip to content

Commit 074fd1d

Browse files
committed
Correctly handle situations where GetNodes does not have local nodes to share
1 parent 3d31721 commit 074fd1d

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

nestkernel/mpi_manager.cpp

+9-2
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,12 @@ nest::MPIManager::communicate( std::vector< long >& local_nodes, std::vector< lo
326326
num_nodes_per_rank[ get_rank() ] = local_nodes.size();
327327
communicate( num_nodes_per_rank );
328328

329-
size_t num_globals = std::accumulate( num_nodes_per_rank.begin(), num_nodes_per_rank.end(), 0 );
329+
const size_t num_globals = std::accumulate( num_nodes_per_rank.begin(), num_nodes_per_rank.end(), 0 );
330+
if ( num_globals == 0 )
331+
{
332+
return; // must return here to avoid passing address to empty global_nodes below
333+
}
334+
330335
global_nodes.resize( num_globals, 0L );
331336

332337
// Set up displacements vector. Entry i specifies the displacement (relative
@@ -337,7 +342,9 @@ nest::MPIManager::communicate( std::vector< long >& local_nodes, std::vector< lo
337342
displacements.at( i ) = displacements.at( i - 1 ) + num_nodes_per_rank.at( i - 1 );
338343
}
339344

340-
MPI_Allgatherv( &( *local_nodes.begin() ),
345+
// avoid dereferencing empty vector
346+
const auto send_ptr = local_nodes.empty() ? nullptr : &local_nodes[ 0 ];
347+
MPI_Allgatherv( send_ptr,
341348
local_nodes.size(),
342349
MPI_Type< long >::type,
343350
&global_nodes[ 0 ],

testsuite/pytests/test_getnodes.py

+10
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ def test_GetNodes_with_params(self):
6666

6767
self.assertEqual(nodes_exp_ref, nodes_exp)
6868

69+
def test_GetNodes_no_match(self):
70+
"""
71+
Ensure we get an empty result if nothing matches.
72+
73+
This would lead to crashes in MPI-parallel code before #3460.
74+
"""
75+
76+
nodes = nest.GetNodes({"V_m": 100.0})
77+
self.assertEqual(len(nodes), 0)
78+
6979

7080
def suite():
7181
suite = unittest.makeSuite(GetNodesTestCase, "test")

0 commit comments

Comments
 (0)