Cypress  1.0
C++ Spiking Neural Network Simulation Framework
pynn.hpp
Go to the documentation of this file.
1 /*
2  * Cypress -- C++ Spiking Neural Network Simulation Framework
3  * Copyright (C) 2016 Andreas Stöckel
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #pragma once
20 
21 #ifndef CYPRESS_BACKEND_PYNN2_HPP
22 #define CYPRESS_BACKEND_PYNN2_HPP
23 
24 // Include first to avoid "_POSIX_C_SOURCE redefined" warning
25 #include <pybind11/pybind11.h>
26 
27 #include <pybind11/embed.h>
28 
29 #include <memory>
30 #include <stdexcept>
31 #include <string>
32 #include <vector>
33 
35 #include <cypress/core/backend.hpp>
37 #include <cypress/util/json.hpp>
38 
39 namespace cypress {
40 namespace py = pybind11;
41 
58 private:
60 
61 public:
62  PythonInstance(PythonInstance const &) = delete;
63  void operator=(PythonInstance const &) = delete;
65  {
66  static PythonInstance instance;
67  return instance;
68  }
69 
70 private:
71  ~PythonInstance();
72 };
73 
78 class PyNN : public Backend {
79 protected:
80  std::string m_simulator;
82  std::vector<std::string> m_imports;
83  bool m_keep_log;
85  void do_run(NetworkBase &network, Real duration) const override;
86 
87 public:
91  class PyNNSimulatorNotFound : public std::runtime_error {
92  public:
93  using std::runtime_error::runtime_error;
94  };
104  PyNN(const std::string &simulator, const Json &setup = Json());
105 
109  ~PyNN() override;
110 
118  Real timestep();
119 
128  std::unordered_set<const NeuronType *> supported_neuron_types()
129  const override;
130 
134  std::string name() const override { return m_normalised_simulator; }
135 
139  const std::string &simulator() const { return m_simulator; }
140 
144  const std::string &normalised_simulator() const
145  {
146  return m_normalised_simulator;
147  }
148 
153  const std::vector<std::string> &imports() const { return m_imports; }
154 
159  std::string nmpi_platform() const;
160 
164  static std::vector<std::string> simulators();
165 
172  static std::string get_import(const std::vector<std::string> &imports,
173  const std::string &simulator);
177  Json setup() const { return m_setup; }
178 
184  static int get_pynn_version();
185 
191  static int get_neo_version();
192 
200  static py::dict json_to_dict(Json json);
201 
209  static std::string get_neuron_class(const NeuronType &neuron_type);
210 
215  static void init_logger();
216 
224  static py::object create_source_population(const PopulationBase &pop,
225  py::module &pynn);
234  static py::object create_homogeneous_pop(const PopulationBase &pop,
235  py::module &pynn,
236  bool &init_available);
237 
245  static void set_inhomogeneous_parameters(const PopulationBase &pop,
246  py::object &pypop,
247  bool init_available = false);
248 
255  static void set_homogeneous_rec(const PopulationBase &pop,
256  py::object &pypop);
257 
264  static void set_inhomogenous_rec(const PopulationBase &pop,
265  py::object &pypop, py::module &pynn);
266 
277  static py::object get_pop_view(const py::module &pynn,
278  const py::object &py_pop,
279  const PopulationBase &c_pop,
280  const size_t &start, const size_t &end);
281 
290  static py::object get_connector7(const ConnectionDescriptor &conn,
291  const py::module &pynn);
292 
304  static py::object get_connector(const std::string &connector_name,
305  const py::module &pynn,
306  const Real &additional_parameter,
307  const bool allow_self_connections);
308 
321  static py::object group_connect(
322  const std::vector<PopulationBase> &populations,
323  const std::vector<py::object> &pypopulations,
324  const ConnectionDescriptor &conn, const py::module &pynn,
325  const bool nest_flag, const Real timestep = 0.0);
326 
327  static py::object group_connect(
328  const std::vector<PopulationBase> &populations,
329  const std::vector<py::object> &pypopulations,
330  const ConnectionDescriptor &conn, const py::module &pynn,
331  const Real nest_flag) = delete;
341  static py::object group_connect7(
342  const std::vector<PopulationBase> &populations,
343  const std::vector<py::object> &pypopulations,
344  const ConnectionDescriptor &conn, const py::module &pynn);
345 
357  static std::tuple<py::object, py::object> list_connect(
358  const std::vector<py::object> &pypopulations,
359  const ConnectionDescriptor conn, const py::module &pynn,
360  const bool current_based, const Real timestep = 0.0);
361 
362  static std::tuple<py::object, py::object> list_connect(
363  const std::vector<py::object> &pypopulations,
364  const ConnectionDescriptor conn, const py::module &pynn,
365  const Real current_based) = delete;
366 
376  static std::tuple<py::object, py::object> list_connect7(
377  const std::vector<py::object> &pypopulations,
378  const ConnectionDescriptor conn, const py::module &pynn);
379 
380  template <typename T>
392  static Matrix<T> matrix_from_numpy(const py::object &object,
393  bool transposed = false);
394 
401  static void fetch_data_nest(const std::vector<PopulationBase> &populations,
402  const std::vector<py::object> &pypopulations);
403 
411  static void fetch_data_spinnaker(
412  const std::vector<PopulationBase> &populations,
413  const std::vector<py::object> &pypopulations);
414 
421  static void fetch_data_neo(const std::vector<PopulationBase> &populations,
422  const std::vector<py::object> &pypopulations);
423 
424  // ______________ Spikey related part _______________________
425 
433  static void spikey_run(NetworkBase &source, Real duration, py::module &pynn,
434  py::dict dict);
435 
443  static py::object spikey_create_source_population(const PopulationBase &pop,
444  py::module &pynn);
445 
455  static py::object spikey_create_homogeneous_pop(const PopulationBase &pop,
456  py::module &pynn);
457 
465  static void spikey_set_homogeneous_rec(const PopulationBase &pop,
466  py::object &pypop, py::module &pynn);
467 
474  static void spikey_set_inhomogeneous_rec(const PopulationBase &pop,
475  py::object &pypop,
476  py::module pynn);
477 
484  static void spikey_set_inhomogeneous_parameters(const PopulationBase &pop,
485  py::object &pypop);
486 
494  static void spikey_get_spikes(PopulationBase pop, py::object &pypop);
501  static void spikey_get_voltage(NeuronBase neuron, py::module &pynn);
502 };
503 } // namespace cypress
504 
505 #endif /* CYPRESS_BACKEND_PYNN_HPP */
Definition: neurons_base.hpp:54
std::vector< std::string > m_imports
Definition: pynn.hpp:82
Definition: backend.hpp:50
double Real
Definition: types.hpp:56
nlohmann::json Json
Definition: json.hpp:27
std::string m_normalised_simulator
Definition: pynn.hpp:81
const std::string & simulator() const
Definition: pynn.hpp:139
std::string m_simulator
Definition: pynn.hpp:80
Definition: matrix.hpp:51
Definition: network_base_objects.hpp:169
bool m_keep_log
Definition: pynn.hpp:83
Definition: network_base.hpp:116
Definition: network_base_objects.hpp:53
std::string name() const override
Definition: pynn.hpp:134
void operator=(PythonInstance const &)=delete
Definition: brainscales_lib.hpp:39
Definition: pynn.hpp:57
Definition: pynn.hpp:78
Json m_setup
Definition: pynn.hpp:84
static PythonInstance & instance()
Definition: pynn.hpp:64
const std::string & normalised_simulator() const
Definition: pynn.hpp:144
const std::vector< std::string > & imports() const
Definition: pynn.hpp:153
Definition: connector.hpp:705
Json setup() const
Definition: pynn.hpp:177