Cypress  1.0
C++ Spiking Neural Network Simulation Framework
data.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_CORE_DATA_HPP
22 #define CYPRESS_CORE_DATA_HPP
23 
24 #include <cstdint>
25 #include <string>
26 #include <vector>
27 
28 #include <cypress/core/types.hpp>
29 #include <cypress/util/matrix.hpp>
30 
31 namespace cypress {
32 /*
33  * Forward declarations
34  */
35 class NeuronType;
36 
43 template <typename Iterator>
44 struct IterableRange {
45 private:
49  Iterator m_begin;
50 
54  Iterator m_end;
55 
56 public:
64  IterableRange(Iterator begin, Iterator end) : m_begin(begin), m_end(end) {}
65 
69  Iterator begin() { return m_begin; }
70 
74  Iterator end() { return m_end; }
75 };
76 
84 template <typename Iterator>
86 {
88 }
89 
95 public:
99  using ParameterType = std::vector<Real>;
100 
104  using RecordType = std::vector<uint8_t>;
105 
110  using DataType = std::vector<std::shared_ptr<Matrix<Real>>>;
111 
112 private:
116  size_t m_size;
117 
121  NeuronType const *m_type;
122 
126  std::string m_name;
127 
135  std::vector<ParameterType> m_parameters;
136 
144  std::vector<RecordType> m_record;
145 
149  std::vector<DataType> m_data;
150 
151 public:
156  size_t size = 1, NeuronType const *type = nullptr,
157  const std::string &name = std::string(),
158  const std::vector<ParameterType> &parameters =
159  std::vector<ParameterType>(),
160  const std::vector<RecordType> &record = std::vector<RecordType>(),
161  const std::vector<DataType> &data = std::vector<DataType>())
162  : m_size(size),
163  m_type(type),
164  m_name(name),
165  m_parameters(parameters),
166  m_record(record),
167  m_data(data)
168  {
169  }
170 
174  size_t size() const { return m_size; }
175 
179  NeuronType const *type() const { return m_type; }
180 
184  std::string &name() { return m_name; }
185 
189  const std::string &name() const { return m_name; }
190 
196  const std::vector<ParameterType> &parameters() const
197  {
198  return m_parameters;
199  }
200 
205  std::vector<ParameterType> &parameters() { return m_parameters; }
206 
212  const std::vector<RecordType> &record() const { return m_record; }
213 
218  std::vector<RecordType> &record() { return m_record; }
219 
225  const std::vector<DataType> &data() const { return m_data; }
226 
231  std::vector<DataType> &data() { return m_data; }
232 
243  size_t get_parameters_size(NeuronIndex nid0, NeuronIndex nid1) const;
244 
255  size_t get_record_size(NeuronIndex nid0, NeuronIndex nid1) const;
256 
267  size_t get_data_size(NeuronIndex nid0, NeuronIndex nid1) const;
268 
280  const ParameterType &read_parameters(NeuronIndex nid0,
281  NeuronIndex nid1) const;
282 
295  const RecordType &read_record(NeuronIndex nid0, NeuronIndex nid1) const;
296 
308  const DataType &read_data(NeuronIndex nid0, NeuronIndex nid1) const;
309 
325  NeuronIndex nid0, NeuronIndex nid1, bool partial = true);
326 
342  NeuronIndex nid0, NeuronIndex nid1, bool partial = true);
343 
359  NeuronIndex nid0, NeuronIndex nid1, bool partial = true);
360 
364  bool homogeneous_parameters() const { return m_parameters.size() <= 1; }
365 
370  bool homogeneous_record() const { return m_record.size() <= 1; }
371 
376  bool homogeneous_data() const { return m_data.size() <= 1; }
377 };
378 
384 private:
388  std::shared_ptr<PopulationData> m_data;
389 
393  NeuronIndex m_nid0;
394 
398  NeuronIndex m_nid1;
399 
403  bool m_own_parameters : 1;
404 
408  bool m_own_record : 1;
409 
413  bool m_own_data : 1;
414 
415 protected:
422  template <typename Seq>
423  static typename Seq::value_type from_sequence(Seq list)
424  {
425  using Impl = typename Seq::value_type;
426 
427  // Create containers for the compund object.
428  const size_t size = list.size();
429  std::vector<PopulationData::ParameterType> parameters(size);
430  std::vector<PopulationData::RecordType> record(size);
431  std::vector<PopulationData::DataType> data(size);
432 
433  // Fill the containers
434  size_t idx = 0;
435  for (const Impl &i : list) {
436  const PopulationData &id = i.population_data();
437  if (id.parameters().size() == 1 && i.m_own_parameters) {
438  parameters[idx] = id.parameters()[0];
439  }
440  if (id.record().size() == 1 && i.m_own_record) {
441  record[idx] = id.record()[0];
442  }
443  if (id.data().size() == 1 && i.m_own_data) {
444  data[idx] = id.data()[0];
445  }
446  idx++;
447  }
448  return Impl(std::make_shared<PopulationData>(
449  size, nullptr, std::string(), parameters, record, data),
450  0, size);
451  }
452 
456  const PopulationData &population_data() const { return *m_data; }
457 
461  PopulationData &population_data() { return *m_data; }
462 
469  size_t get_parameters_size() const
470  {
471  return population_data().get_parameters_size(m_nid0, m_nid1);
472  }
473 
480  size_t get_record_size() const
481  {
482  return population_data().get_record_size(m_nid0, m_nid1);
483  }
484 
491  size_t get_data_size() const
492  {
493  return population_data().get_data_size(m_nid0, m_nid1);
494  }
495 
503  const auto &read_parameters() const
504  {
505  return population_data().read_parameters(m_nid0, m_nid1);
506  }
507 
516  const auto &read_record() const
517  {
518  return population_data().read_record(m_nid0, m_nid1);
519  }
520 
528  const auto &read_data() const
529  {
530  return population_data().read_data(m_nid0, m_nid1);
531  }
532 
543  auto write_parameters(bool partial = true)
544  {
545  return population_data().write_parameters(m_nid0, m_nid1, partial);
546  }
547 
558  auto write_record(bool partial = true)
559  {
560  return population_data().write_record(m_nid0, m_nid1, partial);
561  }
562 
574  auto write_data(bool partial = true)
575  {
576  return population_data().write_data(m_nid0, m_nid1, partial);
577  }
578 
579 public:
585  : m_data(std::make_shared<PopulationData>(1)),
586  m_nid0(0),
587  m_nid1(1),
588  m_own_parameters(true),
589  m_own_record(true),
590  m_own_data(true)
591  {
592  }
593 
600 
604  PopulationDataView(PopulationDataView &&) noexcept = default;
605 
622  PopulationDataView(std::shared_ptr<PopulationData> data, NeuronIndex nid0,
623  NeuronIndex nid1, bool own_parameters = true,
624  bool own_record = true, bool own_data = true)
625  : m_data(data),
626  m_nid0(nid0),
627  m_nid1(nid1),
628  m_own_parameters(own_parameters),
629  m_own_record(own_record),
630  m_own_data(own_data)
631  {
632  }
633 
638  PopulationDataView &operator=(const PopulationDataView &other);
639 };
640 }
641 
642 #endif /* CYPRESS_CORE_DATA_HPP */
Definition: neurons_base.hpp:54
IterableRange< Iterator > make_iterable_range(Iterator begin, Iterator end)
Definition: data.hpp:85
std::vector< std::shared_ptr< Matrix< Real >>> DataType
Definition: data.hpp:110
Iterator end()
Definition: data.hpp:74
std::vector< RecordType > & record()
Definition: data.hpp:218
bool homogeneous_record() const
Definition: data.hpp:370
bool homogeneous_parameters() const
Definition: data.hpp:364
PopulationData & population_data()
Definition: data.hpp:461
const std::vector< DataType > & data() const
Definition: data.hpp:225
int32_t NeuronIndex
Definition: types.hpp:74
Definition: data.hpp:94
const auto & read_parameters() const
Definition: data.hpp:503
NeuronType const * type() const
Definition: data.hpp:179
size_t get_parameters_size() const
Definition: data.hpp:469
std::vector< Real > ParameterType
Definition: data.hpp:99
Iterator begin()
Definition: data.hpp:69
STL namespace.
PopulationData(size_t size=1, NeuronType const *type=nullptr, const std::string &name=std::string(), const std::vector< ParameterType > &parameters=std::vector< ParameterType >(), const std::vector< RecordType > &record=std::vector< RecordType >(), const std::vector< DataType > &data=std::vector< DataType >())
Definition: data.hpp:155
static Seq::value_type from_sequence(Seq list)
Definition: data.hpp:423
const PopulationData & population_data() const
Definition: data.hpp:456
std::string & name()
Definition: data.hpp:184
auto write_record(bool partial=true)
Definition: data.hpp:558
const std::string & name() const
Definition: data.hpp:189
PopulationDataView()
Definition: data.hpp:584
const auto & read_data() const
Definition: data.hpp:528
size_t size() const
Definition: data.hpp:174
auto write_data(bool partial=true)
Definition: data.hpp:574
std::vector< ParameterType > & parameters()
Definition: data.hpp:205
Definition: data.hpp:383
Definition: brainscales_lib.hpp:39
size_t get_record_size() const
Definition: data.hpp:480
PopulationDataView(std::shared_ptr< PopulationData > data, NeuronIndex nid0, NeuronIndex nid1, bool own_parameters=true, bool own_record=true, bool own_data=true)
Definition: data.hpp:622
IterableRange(Iterator begin, Iterator end)
Definition: data.hpp:64
const auto & read_record() const
Definition: data.hpp:516
size_t get_data_size() const
Definition: data.hpp:491
const std::vector< RecordType > & record() const
Definition: data.hpp:212
auto write_parameters(bool partial=true)
Definition: data.hpp:543
const std::vector< ParameterType > & parameters() const
Definition: data.hpp:196
Definition: data.hpp:44
bool homogeneous_data() const
Definition: data.hpp:376
std::vector< DataType > & data()
Definition: data.hpp:231
std::vector< uint8_t > RecordType
Definition: data.hpp:104