NEST main@caf0ae8
 
Loading...
Searching...
No Matches
stimulation_backend_mpi.h
Go to the documentation of this file.
1/*
2 * stimulation_backend_mpi.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 STIMULATION_BACKEND_MPI_H
24#define STIMULATION_BACKEND_MPI_H
25
26#include "nest_time.h"
27#include "nest_types.h"
28#include "stimulation_backend.h"
29#include <arpa/inet.h>
30#include <mpi.h>
31#include <netinet/in.h>
32#include <set>
33#include <sys/socket.h>
34#include <unistd.h>
35
36/* BeginUserDocs: stimulation backend
37
38Short description
39+++++++++++++++++
40
41Stimulation backend `mpi` - Receive stimulation parameters via MPI
42
43Description
44+++++++++++
45
46.. admonition:: Availability
47
48 This stimulation backend is only available if NEST was compiled with
49 :ref:`support for MPI <compile-with-mpi>`.
50
51The `mpi` stimulation backend collects data from MPI channels and
52updates stimulation devices just before each run. This is useful for
53co-simulation or for receiving stimuli from external software.
54
55There are two ways to set the MPI port. If both are set, option A has precedence
56
571. The address is supplied via the recording backends "mpi_address" status property.
58
592. The name of the MPI port to send data to is read from a file for each
60 device configured to use this backend. The file needs to be named
61 according to the following pattern:
62
63::
64
65 {data_path}/{data_prefix}{label}/{node_id}.txt
66
67The ``data_path`` and ``data_prefix`` are global kernel properties,
68while `label` is a property of the device in question and `node_id`
69its node ID. This path can only be set outside of a ``Run`` context
70(i.e. after ``Prepare()`` has been called, but ``Cleanup()`` has
71not).
72
73Communication Protocol
74++++++++++++++++++++++
75
76The following protocol is used to exchange information between both
77MPI processes. The protocol is described using the following format
78for the MPI messages: (value, number, type, source/destination, tag)
79
801) ``Prepare`` : Connection of MPI port include in one file (see below)
812) ``Run`` begin : Send start run (true, 1, CXX_BOOL, 0, 0)
823) ``Run`` begin : Send the id of the device to update (node_id, 1, INT, 0, 0)
834) ``Run`` begin : Receive shape of the data (shape, 1, INT, 0, 0)
845) ``Run`` begin : Receive the data for updating the device (data, shape, DOUBLE, 0, 0)
856) ``Run`` end : Send at each ending of the run (true, 1, CXX_BOOL, 0, 1)
867) ``Cleanup`` : Send at this en of the simulation (true, 1, CXX_BOOL, 0, 2)
87
88Data format
89+++++++++++
90
91The format of the data depends on the exact type of stimulation
92device.
93
94EndUserDocs */
95
96namespace nest
97{
98
110{
111public:
118
124 ~StimulationBackendMPI() noexcept override;
125
126 void initialize() override;
127
128 void finalize() override;
129
130 void enroll( StimulationDevice& device, const Dictionary& params ) override;
131
132 void disenroll( StimulationDevice& device ) override;
133
134 void cleanup() override;
135
136 void prepare() override;
137
138 void pre_run_hook() override;
139
140 void post_run_hook() override;
141
142private:
152 using device_map = std::vector< std::map< size_t, std::pair< const MPI_Comm*, StimulationDevice* > > >;
159 typedef std::map< std::string, std::tuple< MPI_Comm*, std::vector< int >*, int* > > comm_map;
161
162 std::string mpi_address_;
169 void get_port( StimulationDevice* device, std::string* port_name );
170 void get_port( size_t index_node, const std::string& label, std::string* port_name );
171
181 static std::pair< int*, double* > receive_spike_train( const MPI_Comm& comm, std::vector< int >& device_id );
189 void update_device( int* array_index, std::vector< int >& devices_id, std::pair< int*, double* > data );
195 void clean_memory_input_data( std::vector< std::pair< int*, double* > >& data );
196};
197
198} // namespace
199
200#endif /* #ifndef STIMULATION_BACKEND_MPI_H */
Dictionary class for interface to Python and C++ API.
Definition dictionary.h:213
A simple input backend MPI implementation.
Definition stimulation_backend_mpi.h:110
void post_run_hook() override
Clean up the backend at the end of a Run.
Definition stimulation_backend_mpi.cpp:249
std::string mpi_address_
Definition stimulation_backend_mpi.h:162
bool prepared_
Definition stimulation_backend_mpi.h:144
void cleanup() override
Clean up the backend at the end of a user level call to the NEST Simulate function.
Definition stimulation_backend_mpi.cpp:264
static std::pair< int *, double * > receive_spike_train(const MPI_Comm &comm, std::vector< int > &device_id)
MPI communication for receiving the data before each run.
Definition stimulation_backend_mpi.cpp:351
void finalize() override
Definition stimulation_backend_mpi.cpp:57
~StimulationBackendMPI() noexcept override
InputBackend destructor.
Definition stimulation_backend_mpi.cpp:43
void disenroll(StimulationDevice &device) override
Disenroll a StimulationDevice from the StimulationBackend.
Definition stimulation_backend_mpi.cpp:92
bool enrolled_
Definition stimulation_backend_mpi.h:143
void get_port(StimulationDevice *device, std::string *port_name)
Getting the port name for the MPI connection.
Definition stimulation_backend_mpi.cpp:292
void clean_memory_input_data(std::vector< std::pair< int *, double * > > &data)
clean all the memory allocated for the updating device.
Definition stimulation_backend_mpi.cpp:421
void update_device(int *array_index, std::vector< int > &devices_id, std::pair< int *, double * > data)
Update all the devices with the data received.
Definition stimulation_backend_mpi.cpp:376
void initialize() override
Definition stimulation_backend_mpi.cpp:49
std::map< std::string, std::tuple< MPI_Comm *, std::vector< int > *, int * > > comm_map
A map of MPI communicators used by the master thread for the MPI communication.
Definition stimulation_backend_mpi.h:159
std::vector< std::map< size_t, std::pair< const MPI_Comm *, StimulationDevice * > > > device_map
A map for the enrolled devices.
Definition stimulation_backend_mpi.h:152
void pre_run_hook() override
Initialize global backend-specific data structures.
Definition stimulation_backend_mpi.cpp:211
void enroll(StimulationDevice &device, const Dictionary &params) override
Enroll a StimulationDevice with the StimulationBackend.
Definition stimulation_backend_mpi.cpp:69
comm_map commMap_
Definition stimulation_backend_mpi.h:160
void prepare() override
Prepare the backend at the beginning of the NEST Simulate function.
Definition stimulation_backend_mpi.cpp:106
StimulationBackendMPI()
InputBackend constructor.
Definition stimulation_backend_mpi.cpp:37
device_map devices_
Definition stimulation_backend_mpi.h:153
Abstract base class for all NESTio stimulation backends.
Definition stimulation_backend.h:68
Base class for common properties of StimulationDevices.
Definition stimulation_device.h:154
Namespace for the NEST simulation kernel.
Definition beta_normalization_factor.h:33