NEST  2.6.0,not_revisioned_source_dir@0
communicator.h
Go to the documentation of this file.
1 /*
2  * communicator.h
3  *
4  * This file is part of NEST.
5  *
6  * Copyright (C) 2004 The NEST Initiative
7  *
8  * NEST is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * NEST is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with NEST. If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 
23 #ifndef COMMUNICATOR_H
24 #define COMMUNICATOR_H
25 
26 #include <vector>
27 #include <cassert>
28 #include <numeric>
29 
30 #include "config.h"
31 #include "nest.h"
32 #include <iostream>
33 #include <unistd.h>
34 #include <limits>
35 
36 #include "dictdatum.h"
37 #include "nodelist.h"
38 
39 #ifdef HAVE_MPI
40 // Do NOT include mpi.h in this header file, otherwise we get into
41 // trouble on the Blue Gene/L. mpi.h is included in communicator_impl.h
42 
43 #ifdef HAVE_MUSIC
44 #include <music.hh>
45 #include "music_event_handler.h"
46 #endif
47 
48 
49 namespace nest
50 {
51 class Network;
52 
54 {
55  friend class Network;
56 
57 public:
60 
61 #ifdef HAVE_MUSIC
62  static MUSIC::Setup *music_setup;
63  static MUSIC::Runtime *music_runtime;
64 #endif
65 
75  class OffGridSpike {
76  public:
79 
81  gid_(0), offset_(0.0) {}
83  gid_(gidv), offset_(offsetv) {}
84 
85  uint_t get_gid() const { return static_cast<gid_external_type>(gid_ ); }
86  void set_gid(gid_external_type gid) { gid_ = static_cast<double_t>(gid ); }
87  double_t get_offset() const { return offset_; }
88 
89  private:
90 
91  friend class Communicator; //void Communicator::init(int*, char**);
92 
95 
98  {
99  assert(std::numeric_limits<double_t >::digits >
100  std::numeric_limits<gid_external_type>::digits);
101 
102  // the next one is doubling up, better be safe than sorry
104  OffGridSpike ogs(maxgid, 0.0);
105  assert(maxgid == ogs.get_gid());
106  }
107 
108  };
109 
111  public:
113  gid_(0), parent_gid_(0), vp_(0) {}
115  gid_(gid), parent_gid_(parent_gid), vp_(vp) {}
116 
117  uint_t get_gid() const {return gid_;}
119  uint_t get_vp() const {return vp_;}
120  bool operator< (const NodeAddressingData &other) const {return this->gid_ < other.gid_;}
121  bool operator== (const NodeAddressingData &other) const {return this->gid_ == other.gid_;}
122  private:
123  friend class Communicator;
127  };
128 
129 #ifdef HAVE_MUSIC
130 
135  static void enter_runtime(double_t h_min_delay);
136 
137  static MUSIC::Setup* get_music_setup();
138  static MUSIC::Runtime* get_music_runtime();
139 
145  static void advance_music_time(long_t num_steps);
146 
152  static void register_music_event_in_proxy(std::string portname, int channel, nest::Node *mp);
153 #endif
154 
155  static void init(int* argc, char** argv[]);
156  static void finalize();
157  static void mpi_abort(int exitcode);
158 
159  static void communicate(std::vector<uint_t>& send_buffer,
160  std::vector<uint_t>& recv_buffer,
161  std::vector<int>& displacements);
162  static void communicate(std::vector<OffGridSpike>& send_buffer,
163  std::vector<OffGridSpike>& recv_buffer,
164  std::vector<int>& displacements);
165  static void communicate(std::vector<double_t>& send_buffer,
166  std::vector<double_t>& recv_buffer,
167  std::vector<int>& displacements);
168  static void communicate(double_t, std::vector<double_t>&);
169  static void communicate(std::vector<int_t>&);
170  static void communicate(std::vector<long_t>&);
171 
176  template <typename NodeListType>
177  static void communicate(const NodeListType& local_nodes,
178  std::vector<NodeAddressingData>& all_nodes,
179  bool remote = false);
180  template <typename NodeListType>
181  static void communicate(const NodeListType& local_nodes,
182  std::vector<NodeAddressingData>& all_nodes,
183  Network& net,
184  DictionaryDatum params,
185  bool remote = false);
186 
188 
189  static void synchronize();
190  static void test_link(int, int);
191  static void test_links();
192 
193  static bool grng_synchrony(unsigned long);
194  static double_t time_communicate(int num_bytes, int samples=1000);
195  static double_t time_communicatev(int num_bytes, int samples=1000);
196  static double_t time_communicate_offgrid(int num_bytes, int samples=1000);
197  static double_t time_communicate_alltoall(int num_bytes, int samples=1000);
198  static double_t time_communicate_alltoallv(int num_bytes, int samples=1000);
199 
200  static std::string get_processor_name();
201 
202  static int get_rank();
203  static int get_num_processes();
204  static void set_num_processes(int);
205  static int get_num_virtual_processes();
206  static int get_send_buffer_size();
207  static int get_recv_buffer_size();
208  static bool get_use_Allgather();
209  static bool get_initialized();
210 
211  static void set_num_threads(thread num_threads);
212  static void set_buffer_sizes(int send_buffer_size, int recv_buffer_size);
213  static void set_use_Allgather(bool use_Allgather);
214 
215 private:
216 
217  static Network* net_;
218 
219  static int rank_;
220  static int num_processes_;
221  static int n_vps_;
222  static int send_buffer_size_;
223  static int recv_buffer_size_;
224  static bool initialized_;
225  static bool use_Allgather_;
226 
227  static std::vector<int> comm_step_;
229 
230  static void init_communication();
231 
232  static void communicate_Allgather(std::vector<uint_t>& send_buffer,
233  std::vector<uint_t>& recv_buffer,
234  std::vector<int>& displacements);
235  static void communicate_Allgather(std::vector<OffGridSpike>& send_buffer,
236  std::vector<OffGridSpike>& recv_buffer,
237  std::vector<int>& displacements);
238  static void communicate_Allgather(std::vector<int_t>&);
239  static void communicate_Allgather(std::vector<long_t>&);
240 
241  template <typename T>
242  static void communicate_Allgatherv(std::vector<T>& send_buffer,
243  std::vector<T>& recv_buffer,
244  std::vector<int>& displacements,
245  std::vector<int>& recv_counts);
246 
247  template <typename T>
248  static void communicate_Allgather(std::vector<T>& send_buffer,
249  std::vector<T>& recv_buffer,
250  std::vector<int>& displacements);
251 
252  static void communicate_CPEX(std::vector<uint_t>& send_buffer,
253  std::vector<uint_t>& recv_buffer,
254  std::vector<int>& displacements);
255  static void communicate_CPEX(std::vector<OffGridSpike>& send_buffer,
256  std::vector<OffGridSpike>& recv_buffer,
257  std::vector<int>& displacements);
258  static void communicate_CPEX(std::vector<int_t>&);
259  static void communicate_CPEX(std::vector<long_t>&);
260 };
261 
262 inline void Communicator::set_use_Allgather(bool use_Allgather)
263 {
264  use_Allgather_ = use_Allgather;
265  if (!use_Allgather)
267 }
268 
269 }
270 
271 #else /* #ifdef HAVE_MPI */
272 
273 namespace nest
274 {
275 class Network;
276 
277 class Communicator
278 {
279  friend class Network;
280 
281 public:
284 
285  class OffGridSpike {
286  public:
288  gid_(0), offset_(0.0) {}
289  OffGridSpike(uint_t gidv, double_t offsetv) :
290  gid_(gidv), offset_(offsetv) {}
291 
292  uint_t get_gid() const { return static_cast<uint_t >(gid_ ); }
293  void set_gid(uint_t gid) { gid_ = static_cast<double_t>(gid ); }
294  double_t get_offset() const { return offset_; }
295 
296  private:
297 
298  friend class Communicator; //void Communicator::init(int*, char**);
299 
300  double_t gid_;
301  double_t offset_;
302  };
303 
304  class NodeAddressingData {
305  public:
307  gid_(0), parent_gid_(0), vp_(0) {}
309  gid_(gid), parent_gid_(parent_gid), vp_(vp) {}
310 
311  uint_t get_gid() const {return gid_;}
313  uint_t get_vp() const {return vp_;}
314  bool operator< (const NodeAddressingData &other) const {return this->gid_ < other.gid_;}
315  bool operator== (const NodeAddressingData &other) const {return this->gid_ == other.gid_;}
316  private:
317  friend class Communicator;
318  uint_t gid_;
320  uint_t vp_;
321  };
322 
323  static void communicate(std::vector<uint_t>& send_buffer, std::vector<uint_t>& recv_buffer,
324  std::vector<int>& displacements);
325  static void communicate(std::vector<OffGridSpike>& send_buffer,
326  std::vector<OffGridSpike>& recv_buffer,
327  std::vector<int>& displacements);
328  static void communicate(std::vector<double_t>& send_buffer,
329  std::vector<double_t>& recv_buffer,
330  std::vector<int>& displacements);
331  static void communicate(double_t, std::vector<double_t>&);
332  static void communicate(std::vector<int_t>&) {}
333  static void communicate(std::vector<long_t>&) {}
334 
343  template <typename NodeListType>
344  static void communicate(const NodeListType& local_nodes,
345  std::vector<NodeAddressingData>& all_nodes,
346  bool remote = false);
347  template <typename NodeListType>
348  static void communicate(const NodeListType& local_nodes,
349  std::vector<NodeAddressingData>& all_nodes,
350  Network& net, DictionaryDatum params,
351  bool remote = false);
352 
354 
355  static void synchronize() {}
356 
357  /* replaced u_long with unsigned long since u_long is not known when
358  mpi.h is not available. This is a rather ugly fix.
359  HEP 2007-03-09
360  */
361  static bool grng_synchrony(unsigned long) {return true;}
362  static double_t time_communicate(int, int){return 0.0;}
363  static double_t time_communicatev(int, int){return 0.0;}
364  static double_t time_communicate_offgrid(int, int){return 0.0;}
365  static double_t time_communicate_alltoall(int, int){return 0.0;}
366  static double_t time_communicate_alltoallv(int, int){return 0.0;}
367 
368  static std::string get_processor_name();
369 
370  static int get_rank();
371  static int get_num_processes();
372  static void set_num_processes(int);
373  static int get_num_virtual_processes();
374  static int get_send_buffer_size();
375  static int get_recv_buffer_size();
376  static bool get_use_Allgather();
377  static bool get_initialized();
378 
379  static void set_num_threads(thread num_threads);
380  static void set_buffer_sizes(int send_buffer_size, int recv_buffer_size);
381  static void set_use_Allgather(bool use_Allgather);
382 
383 private:
384 
385  static Network* net_;
386 
387  static int rank_;
388  static int num_processes_;
389  static int n_vps_;
390  static int send_buffer_size_;
391  static int recv_buffer_size_;
392  static bool initialized_;
393  static bool use_Allgather_;
394 };
395 
396 inline void Communicator::set_use_Allgather(bool use_Allgather)
397 {
398  use_Allgather_ = use_Allgather;
399 }
400 
401 
402 inline std::string Communicator::get_processor_name()
403 {
404  char name[1024];
405  name[1023] = '\0';
406  gethostname(name, 1023);
407  return name;
408 }
409 
410 }
411 #endif /* #ifdef HAVE_MPI */
412 
413 namespace nest
414 {
415 
417 {
418  return rank_;
419 }
420 
422 {
423  return num_processes_;
424 }
425 
427 {
428  num_processes_ = np;
429 }
430 
432 {
433  return n_vps_;
434 }
435 
437 {
438  return send_buffer_size_;
439 }
440 
442 {
443  return recv_buffer_size_;
444 }
445 
447 {
448  return use_Allgather_;
449 }
450 
452 {
453  return initialized_;
454 }
455 
456 inline void Communicator::set_num_threads(thread num_threads)
457 {
458  n_vps_ = num_processes_ * num_threads;
459 }
460 
461 inline void Communicator::set_buffer_sizes(int send_buffer_size, int recv_buffer_size)
462 {
463  send_buffer_size_ = send_buffer_size;
464  recv_buffer_size_ = recv_buffer_size;
465 }
466 
467 } // namespace nest
468 
469 #endif /* #ifndef COMMUNICATOR_H */
static double_t time_communicate(int, int)
Definition: communicator.h:362
static double_t time_communicate_alltoall(int, int)
Definition: communicator.h:365
bool operator<(const NodeAddressingData &other) const
Definition: communicator.h:120
uint_t get_gid() const
Definition: communicator.h:85
static bool grng_synchrony(unsigned long)
Definition: communicator.h:361
static void communicate_connector_properties(DictionaryDatum &)
Definition: communicator.h:353
void set_gid(gid_external_type gid)
Definition: communicator.h:86
Communicator()
Definition: communicator.h:58
double_t offset_
offset of spike from grid
Definition: communicator.h:94
static int num_processes_
the number of mpi-processes
Definition: communicator.h:220
static void set_buffer_sizes(int send_buffer_size, int recv_buffer_size)
Definition: communicator.h:461
Definition: communicator.h:110
static int get_recv_buffer_size()
Definition: communicator.h:441
NodeAddressingData(uint_t gid, uint_t parent_gid, uint_t vp)
Definition: communicator.h:114
static void test_link(int, int)
Definition: communicator.cpp:775
static double_t time_communicate_offgrid(int, int)
Definition: communicator.h:364
static int rank_
the rank of the machine
Definition: communicator.h:219
static void enter_runtime(double_t h_min_delay)
Enter the runtime mode.
Definition: communicator.cpp:1028
static bool get_initialized()
Definition: communicator.h:451
static void mpi_abort(int exitcode)
Definition: communicator.cpp:191
static bool grng_synchrony(unsigned long)
Definition: communicator.cpp:804
static bool initialized_
whether MPI is initialized
Definition: communicator.h:224
static bool use_Allgather_
using Allgather communication
Definition: communicator.h:225
uint_t get_gid() const
Definition: communicator.h:117
static MUSIC::Runtime * get_music_runtime()
Definition: communicator.cpp:1023
assert(pNet!=0)
static std::string get_processor_name()
Definition: communicator.cpp:197
static void communicate_Allgather(std::vector< uint_t > &send_buffer, std::vector< uint_t > &recv_buffer, std::vector< int > &displacements)
Definition: communicator.cpp:259
static MUSIC::Runtime * music_runtime
pointer to a MUSIC runtime object
Definition: communicator.h:63
static void communicate_Allgatherv(std::vector< T > &send_buffer, std::vector< T > &recv_buffer, std::vector< int > &displacements, std::vector< int > &recv_counts)
Definition: communicator_impl.h:59
uint_t get_parent_gid() const
Definition: communicator.h:118
uint_t gid_
GID of neuron.
Definition: communicator.h:124
static double_t time_communicate(int num_bytes, int samples=1000)
Definition: communicator.cpp:823
static int get_send_buffer_size()
Definition: communicator.h:436
static void test_links()
Definition: communicator.cpp:794
static void set_use_Allgather(bool use_Allgather)
Definition: communicator.h:262
static bool get_use_Allgather()
Definition: communicator.h:446
static void communicate(std::vector< int_t > &)
Definition: communicator.h:332
static void communicate(std::vector< uint_t > &send_buffer, std::vector< uint_t > &recv_buffer, std::vector< int > &displacements)
communicate (on-grid) if compiled without MPI
Definition: communicator.cpp:239
static int get_num_virtual_processes()
Definition: communicator.h:431
static void finalize()
Finish off MPI routines.
Definition: communicator.cpp:154
static void set_num_processes(int)
Definition: communicator.h:426
uint_t parent_gid_
GID of neuron's parent.
Definition: communicator.h:125
double_t get_offset() const
Definition: communicator.h:87
const Name other("other")
Node type.
Definition: nest_names.h:216
static MUSIC::Setup * get_music_setup()
Definition: communicator.cpp:1018
static double_t time_communicate_alltoall(int num_bytes, int samples=1000)
Definition: communicator.cpp:892
static Network * net_
Pointer to the Network class.
Definition: communicator.h:217
uint_t get_vp() const
Definition: communicator.h:119
OffGridSpike(uint_t gidv, double_t offsetv)
Definition: communicator.h:289
OffGridSpike()
Definition: communicator.h:80
static uint_t COMM_OVERFLOW_ERROR
Definition: communicator.h:228
uint_t gid_external_type
We defined this type explicitly, so that the assert function below always tests the correct type...
Definition: communicator.h:78
static void communicate_connector_properties(DictionaryDatum &dict)
Definition: communicator.cpp:946
static std::vector< int > comm_step_
array containing communication partner for each step.
Definition: communicator.h:227
static void init_communication()
Perform the CPEX algorithm to determine the communication partner of the process for each communicati...
Definition: communicator.cpp:214
static double_t time_communicate_alltoallv(int num_bytes, int samples=1000)
Definition: communicator.cpp:916
static double_t time_communicate_offgrid(int num_bytes, int samples=1000)
Definition: communicator.cpp:871
NodeAddressingData()
Definition: communicator.h:112
const Name vp("vp")
Node parameter.
Definition: nest_names.h:341
Main administrative interface to the network.
Definition: network.h:135
OffGridSpike(gid_external_type gidv, double_t offsetv)
Definition: communicator.h:82
static int send_buffer_size_
expected size of send buffer
Definition: communicator.h:222
static void synchronize()
Ensure all processes have reached the same stage by waiting until all processes have sent a dummy mes...
Definition: communicator.cpp:770
friend class Network
Definition: communicator.h:55
static int get_num_processes()
Definition: communicator.h:421
static void set_num_threads(thread num_threads)
Definition: communicator.h:456
static void synchronize()
Definition: communicator.h:355
int char ** argv
Definition: neststartup.cpp:61
static void register_music_event_in_proxy(std::string portname, int channel, nest::Node *mp)
Register a music_event_in_proxy for a given port and a given channel.
static MUSIC::Setup * music_setup
pointer to a MUSIC setup object
Definition: communicator.h:62
static double_t time_communicatev(int, int)
Definition: communicator.h:363
static int recv_buffer_size_
size of receive buffer
Definition: communicator.h:223
double double_t
Double precision floating point numbers.
Definition: nest.h:93
Combined storage of GID and offset information for off-grid spikes.
Definition: communicator.h:75
static double_t time_communicatev(int num_bytes, int samples=1000)
Definition: communicator.cpp:844
uint_t vp_
virtual process of neuron
Definition: communicator.h:126
static void communicate(std::vector< long_t > &)
Definition: communicator.h:333
int char SLIInterpreter nest::Network std::string modulepath nest::Communicator::init & argc
Definition: neststartup.cpp:66
unsigned int uint_t
Unsigned int_t.
Definition: nest.h:97
const Name max("max")
Definition: topology_names.h:75
static int n_vps_
the number of virtual processes
Definition: communicator.h:221
Definition: communicator.h:53
Default types used by the NEST kernel.
bool operator==(const NodeAddressingData &other) const
Definition: communicator.h:121
Base class for all NEST network objects.
Definition: node.h:96
~Communicator()
Definition: communicator.h:59
void set_gid(uint_t gid)
Definition: communicator.h:293
static double_t time_communicate_alltoallv(int, int)
Definition: communicator.h:366
static void init(int *argc, char **argv[])
Set up MPI and establish number of processes and rank.
Definition: communicator.cpp:81
static int get_rank()
Definition: communicator.h:416
static void assert_datatype_compatibility()
This function asserts that doubles can hold GIDs without loss.
Definition: communicator.h:97
int_t thread
Thread index type.
Definition: nest.h:133
long long_t
Integer number with at least 32 bit.
Definition: nest.h:96
static void advance_music_time(long_t num_steps)
Advance the time of music by num_steps simulation steps.
Definition: communicator.cpp:1038
double_t gid_
GID of neuron that spiked.
Definition: communicator.h:93
static void communicate_CPEX(std::vector< uint_t > &send_buffer, std::vector< uint_t > &recv_buffer, std::vector< int > &displacements)
Definition: communicator.cpp:307