Auryn simulator  v0.8.1-206-gb56e451
Plastic Spiking Neural Network Simulator
SparseConnection.h
Go to the documentation of this file.
1 /*
2 * Copyright 2014-2018 Friedemann Zenke
3 *
4 * This file is part of Auryn, a simulation package for plastic
5 * spiking neural networks.
6 *
7 * Auryn is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * Auryn is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Auryn. If not, see <http://www.gnu.org/licenses/>.
19 *
20 * If you are using Auryn or parts of it for your work please cite:
21 * Zenke, F. and Gerstner, W., 2014. Limits to high-speed simulations
22 * of spiking neural networks using general-purpose computers.
23 * Front Neuroinform 8, 76. doi: 10.3389/fninf.2014.00076
24 */
25 
26 #ifndef SPARSECONNECTION_H_
27 #define SPARSECONNECTION_H_
28 
29 #include "auryn_definitions.h"
30 #include "AurynVector.h"
31 #include "Connection.h"
32 #include "System.h"
33 #include "ComplexMatrix.h"
34 
35 #include <sstream>
36 #include <fstream>
37 #include <stdio.h>
38 #include <algorithm>
39 
40 #include <boost/random/mersenne_twister.hpp>
41 #include <boost/random/uniform_int.hpp>
42 #include <boost/random/uniform_real.hpp>
43 #include <boost/random/variate_generator.hpp>
44 #include <boost/random/exponential_distribution.hpp>
45 #include <boost/random/normal_distribution.hpp>
46 #include <boost/random/lognormal_distribution.hpp>
47 
48 #define WARN_FILL_LEVEL 0.8
49 
50 namespace auryn {
51 
53 
54 
67 {
68 private:
69  SpikeContainer * spikes;
70  static bool has_been_seeded;
71  bool has_been_allocated;
72  void init();
73  bool init_from_file(const char * filename);
75  AurynLong dryrun_from_file(string filename);
76 
77 protected:
78  void virtual_serialize(boost::archive::binary_oarchive & ar, const unsigned int version )
79  {
81  ar & *w;
82  }
83 
84  void virtual_serialize(boost::archive::binary_iarchive & ar, const unsigned int version )
85  {
87  ar & *w;
88  }
89 
91  static boost::mt19937 sparse_connection_gen;
92 
98 
104 
108 
109  void free();
110 
112  void allocate(AurynLong bufsize);
113 
121  std::vector<type_pattern> load_pattern_file( string filename, int nb_max_patterns);
122 
123 public:
127 
133 
139 
144 
147  ForwardMatrix * w;
148 
151 
153  SparseConnection(const char * filename);
154 
156  SparseConnection(NeuronID rows, NeuronID cols);
157 
159  SparseConnection(SpikingGroup * source, NeuronGroup * destination, const char * filename, TransmitterType transmitter=GLUT);
160 
162  SparseConnection(SpikingGroup * source,
163  NeuronGroup * destination,
164  TransmitterType transmitter=GLUT,
165  string name="SparseConnection");
166 
176  SpikingGroup * source,
177  NeuronGroup * destination,
178  AurynWeight weight,
179  AurynDouble sparseness=0.05,
180  TransmitterType transmitter=GLUT,
181  string name="SparseConnection"
182  );
183 
186  SparseConnection(SpikingGroup * source, NeuronGroup * destination, SparseConnection * con, string name="SparseConnection");
187 
192  SparseConnection(SpikingGroup * source, NeuronGroup * destination, AurynWeight weight, AurynDouble sparseness, NeuronID lo_row, NeuronID hi_row, NeuronID lo_col, NeuronID hi_col, TransmitterType transmitter=GLUT);
193 
195  virtual ~SparseConnection();
196 
198  void allocate_manually(AurynLong expected_size);
199 
203  AurynLong estimate_required_nonzero_entires( AurynLong nonzero , double sigma = 5.);
204 
206  void seed(NeuronID randomseed);
207 
209  virtual AurynWeight get(NeuronID i, NeuronID j);
210 
212  virtual AurynWeight * get_ptr(NeuronID i, NeuronID j);
213 
215  virtual AurynWeight get_data(NeuronID i);
216 
218  virtual void set_data(NeuronID i, AurynWeight value);
219 
221  virtual void set(NeuronID i, NeuronID j, AurynWeight value);
222 
224  virtual void set(std::vector<neuron_pair> element_list, AurynWeight value);
225 
227  void random_data(AurynWeight mean, AurynWeight sigma);
228 
230  void random_data_normal(AurynWeight mean, AurynWeight sigma);
231 
234 
239  void init_random_binary(AurynFloat prob=0.5, AurynWeight wlo=0.0, AurynWeight whi=1.0);
240 
242  void random_col_data(AurynWeight mean, AurynWeight sigma);
243 
245  void set_block(NeuronID lo_row, NeuronID hi_row, NeuronID lo_col, NeuronID hi_col, AurynWeight weight);
246 
248  void scale_block(NeuronID lo_row, NeuronID hi_row, NeuronID lo_col, NeuronID hi_col, AurynWeight alpha);
249 
251  virtual void set_all(AurynWeight weight);
252 
254  virtual void scale_all(AurynFloat value);
255 
257  virtual void clip(AurynWeight lo, AurynWeight hi);
258 
260  void set_upper_triangular(AurynWeight weight);
261 
263  virtual void sparse_set_data(AurynDouble sparseness, AurynWeight value);
264 
268  void connect_random(AurynWeight weight=1.0, AurynDouble sparseness=0.05, bool skip_diag=false);
269 
276  void connect_block_random(AurynWeight weight,
277  AurynDouble sparseness,
278  NeuronID lo_row,
279  NeuronID hi_row,
280  NeuronID lo_col,
281  NeuronID hi_col,
282  bool skip_diag=false );
283 
288  virtual void finalize();
289 
299  bool push_back(NeuronID i, NeuronID j, AurynWeight weight);
300 
303 
307  void put_pattern(type_pattern * pattern, AurynWeight strength, bool overwrite );
308 
312  void put_pattern(type_pattern * pattern1, type_pattern * pattern2, AurynWeight strength, bool overwrite );
313 
314 
327  void load_patterns( string filename, AurynWeight strength, int nb_max_patterns=10000, bool overwrite = false, bool chainmode = false);
328 
341  void load_pre_post_patterns( std::string pre_file, std::string post_file, AurynWeight strength, int nb_max_patterns=10000, bool overwrite = false);
342 
346  virtual void propagate();
347 
349  void sanity_check();
350 
352  virtual AurynDouble sum();
353 
358  virtual void stats(AurynDouble &mean, AurynDouble &std);
359 
362  virtual void stats(AurynDouble &mean, AurynDouble &std, NeuronID zid);
363 
364 
371  bool write_to_file(ForwardMatrix * m, string filename );
372 
378  virtual bool write_to_file(string filename);
379 
388  virtual bool load_from_complete_file(string filename);
389 
398  virtual bool load_from_file(string filename);
399 
406  bool load_from_file(ForwardMatrix * m, string filename, AurynLong data_size = 0 );
407 
409  virtual void set_min_weight(AurynWeight minimum_weight);
410 
413  return wmin;
414  };
415 
417  virtual void set_max_weight(AurynWeight maximum_weight);
418 
421  return wmax;
422  };
423 
425  std::vector<neuron_pair> get_block(NeuronID lo_row, NeuronID hi_row, NeuronID lo_col, NeuronID hi_col);
426 
428  std::vector<neuron_pair> get_post_partners(NeuronID i);
429 
431  std::vector<neuron_pair> get_pre_partners(NeuronID j);
432 };
433 
434 } // namespace
435 
436 #endif /*SPARSECONNECTION_H_*/
AurynWeight get_max_weight()
Gets maximum weight (for plastic connections).
Definition: SparseConnection.h:420
AurynWeight wmax
Definition: SparseConnection.h:103
SparseConnection()
Empty constructor which should not be used – TODO should be deprecated at some point.
void set_block(NeuronID lo_row, NeuronID hi_row, NeuronID lo_col, NeuronID hi_col, AurynWeight weight)
Sets all weights of existing connections in a block spanned by the first 4 parameters to the value gi...
Definition: SparseConnection.cpp:307
void random_col_data(AurynWeight mean, AurynWeight sigma)
Sets weights in cols to the same value drewn from a Gaussian distribution.
Definition: SparseConnection.cpp:289
void load_pre_post_patterns(std::string pre_file, std::string post_file, AurynWeight strength, int nb_max_patterns=10000, bool overwrite=false)
Reads patterns from two files and connects them.
Definition: SparseConnection.cpp:1079
void set_upper_triangular(AurynWeight weight)
Sets weights in a upper triangular matrix.
Definition: SparseConnection.cpp:345
Standard Glutamatergic (excitatory) transmission.
Definition: auryn_definitions.h:139
void scale_block(NeuronID lo_row, NeuronID hi_row, NeuronID lo_col, NeuronID hi_col, AurynWeight alpha)
Scale all weights of existing connections in a block spanned by the first 4 parameters to the value g...
Definition: SparseConnection.cpp:321
bool skip_diagonal
Definition: SparseConnection.h:107
virtual void set_data(NeuronID i, AurynWeight value)
Sets weight value of a given element referenced by its index in the data array.
Definition: SparseConnection.cpp:661
The abstract base class for all Connection objects in Auryn.
Definition: Connection.h:53
std::vector< NeuronID > SpikeContainer
Spike container type. Used for storing spikes.
Definition: auryn_definitions.h:161
ForwardMatrix * w
A pointer that points per default to the ComplexMatrix that stores the connectinos.
Definition: SparseConnection.h:147
STL namespace.
void seed(NeuronID randomseed)
This function seeds the pseudo random number generator for all random fill operatios.
Definition: SparseConnection.cpp:155
Abstract base class of all objects producing spikes.
Definition: SpikingGroup.h:67
void random_data_normal(AurynWeight mean, AurynWeight sigma)
Set weights of all existing connections randomly using a normal distrubtion.
Definition: SparseConnection.cpp:213
double AurynDouble
Higher precision floating point datatype.
Definition: auryn_definitions.h:158
virtual AurynWeight get_data(NeuronID i)
Returns weight value of a given element referenced by index in the data array.
Definition: SparseConnection.cpp:656
AurynFloat AurynWeight
Unit of synaptic weights.
Definition: auryn_definitions.h:159
bool patterns_ignore_gamma
Switch that toggles for the load_patterns function whether or not to use the intensity (gamma) value...
Definition: SparseConnection.h:126
AurynWeight wmin
Definition: SparseConnection.h:97
NeuronID patterns_every_post
The every_post parameter allows to skip postsynaptically over pattern IDs when loading patterns...
Definition: SparseConnection.h:138
virtual bool load_from_complete_file(string filename)
Loads weight matrix from a single file.
Definition: SparseConnection.cpp:921
The base class to create sparse random connections.
Definition: SparseConnection.h:66
bool push_back(NeuronID i, NeuronID j, AurynWeight weight)
Pushes a single element to the ComplexMatrix.
Definition: SparseConnection.cpp:515
std::vector< pattern_member > type_pattern
Definition: auryn_definitions.h:177
bool wrap_patterns
Switch that toggles the behavior when loading a pattern to wrap neuron IDs back onto existing cells v...
Definition: SparseConnection.h:143
Template for a sparse matrix with row major ordering and fast access of rows and capability to store ...
Definition: ComplexMatrix.h:75
virtual void clip(AurynWeight lo, AurynWeight hi)
Clip weights.
Definition: SparseConnection.cpp:1147
virtual void propagate()
Internally used propagate method.
Definition: SparseConnection.cpp:524
void connect_block_random(AurynWeight weight, AurynDouble sparseness, NeuronID lo_row, NeuronID hi_row, NeuronID lo_col, NeuronID hi_col, bool skip_diag=false)
Underlying sparse fill method.
Definition: SparseConnection.cpp:360
virtual void scale_all(AurynFloat value)
Scales all weights in the weight matrix with the given value.
Definition: SparseConnection.cpp:340
AurynLong estimate_required_nonzero_entires(AurynLong nonzero, double sigma=5.)
This function estimates the required size of the nonzero entry buffer.
Definition: SparseConnection.cpp:164
void sanity_check()
Quick an dirty function that checks if all units on the local rank are connected. ...
Definition: SparseConnection.cpp:537
Definition: ABSConnection.h:38
virtual void sparse_set_data(AurynDouble sparseness, AurynWeight value)
Sets a sparse random subset of connection elements wight the given value.
Definition: SparseConnection.cpp:271
unsigned long AurynLong
An unsigned long type used to count synapses or similar.
Definition: auryn_definitions.h:154
void init_random_binary(AurynFloat prob=0.5, AurynWeight wlo=0.0, AurynWeight whi=1.0)
Initialize with random binary at wlo and whi.
Definition: SparseConnection.cpp:250
virtual void virtual_serialize(boost::archive::binary_oarchive &ar, const unsigned int version)
Definition: Connection.h:71
virtual ~SparseConnection()
The default destructor.
Definition: SparseConnection.cpp:123
std::vector< neuron_pair > get_post_partners(NeuronID i)
Returns a vector of ConnectionsID of postsynaptic parterns of neuron i.
Definition: SparseConnection.cpp:1118
virtual void set_max_weight(AurynWeight maximum_weight)
Sets maximum weight (for plastic connections).
Definition: SparseConnection.cpp:203
virtual void set_all(AurynWeight weight)
Sets all weights of existing connections to the given value.
Definition: SparseConnection.cpp:335
void virtual_serialize(boost::archive::binary_oarchive &ar, const unsigned int version)
Definition: SparseConnection.h:78
AurynWeight get_min_weight()
Gets minimum weight (for plastic connections).
Definition: SparseConnection.h:412
virtual void stats(AurynDouble &mean, AurynDouble &std)
Computes mean and variance of weights in default weight matrix.
Definition: SparseConnection.cpp:618
void random_data(AurynWeight mean, AurynWeight sigma)
Synonym for random_data.
Definition: SparseConnection.cpp:208
void allocate_manually(AurynLong expected_size)
Is used whenever memory has to be allocated manually. Automatically adjusts for number of ranks and f...
Definition: SparseConnection.cpp:192
TransmitterType
Specifies the different transmitter types that Auryn knows.
Definition: auryn_definitions.h:138
float AurynFloat
Low precision floating point datatype.
Definition: auryn_definitions.h:157
void virtual_serialize(boost::archive::binary_iarchive &ar, const unsigned int version)
Definition: SparseConnection.h:84
NeuronID patterns_every_pre
The every_pre parameter allows to skip presynaptically over pattern IDs when loading patterns...
Definition: SparseConnection.h:132
virtual void set_min_weight(AurynWeight minimum_weight)
Sets minimum weight (for plastic connections).
Definition: SparseConnection.cpp:198
void allocate(AurynLong bufsize)
Definition: SparseConnection.cpp:175
void free()
Definition: SparseConnection.cpp:169
virtual bool load_from_file(string filename)
Loads weight matrix from Matrix Market (wmat) file.
Definition: SparseConnection.cpp:935
void load_patterns(string filename, AurynWeight strength, int nb_max_patterns=10000, bool overwrite=false, bool chainmode=false)
Reads first n patterns from a .pat file and adds them as Hebbian assemblies onto an existing weight m...
Definition: SparseConnection.cpp:1057
std::vector< neuron_pair > get_block(NeuronID lo_row, NeuronID hi_row, NeuronID lo_col, NeuronID hi_col)
Returns a vector of ConnectionsID of a block specified by the arguments.
Definition: SparseConnection.cpp:1100
AurynLong get_nonzero()
Returns number of nonzero elements in this SparseConnection.
Definition: SparseConnection.cpp:949
Abstract base class for all neuron groups.
Definition: NeuronGroup.h:45
void random_data_lognormal(AurynWeight m, AurynWeight s)
Set weights of all existing connections randomly using a lognormal distribution.
Definition: SparseConnection.cpp:231
static boost::mt19937 sparse_connection_gen
Definition: SparseConnection.h:91
virtual void finalize()
Finalizes connection after random or manual initialization of the weights.
Definition: SparseConnection.cpp:499
bool write_to_file(ForwardMatrix *m, string filename)
Writes rank specific weight matrix on the same rank to a file.
Definition: SparseConnection.cpp:690
void put_pattern(type_pattern *pattern, AurynWeight strength, bool overwrite)
Puts cell assembly to existing sparse weights.
Definition: SparseConnection.cpp:954
void connect_random(AurynWeight weight=1.0, AurynDouble sparseness=0.05, bool skip_diag=false)
Connect src and dst SpikingGroup and NeuronGroup randomly with given sparseness.
Definition: SparseConnection.cpp:487
virtual AurynWeight * get_ptr(NeuronID i, NeuronID j)
Returns pointer to given weight element if it exists. Returns NULL if element does not exist...
Definition: SparseConnection.cpp:671
virtual AurynDouble sum()
Computes sum of all weight elements in the Connection.
Definition: SparseConnection.cpp:645
std::string string
Standard library string type which is imported into Auryn namespace.
Definition: auryn_definitions.h:156
unsigned int NeuronID
NeuronID is an unsigned integeger type used to index neurons in Auryn.
Definition: auryn_definitions.h:151
std::vector< neuron_pair > get_pre_partners(NeuronID j)
Returns a vector of ConnectionsID of presynaptic parterns of neuron i.
Definition: SparseConnection.cpp:1131
std::vector< type_pattern > load_pattern_file(string filename, int nb_max_patterns)
Reads patterns from a .pat file and returns a vector with the patterns.
Definition: SparseConnection.cpp:992
ComplexMatrix< AurynWeight > ForwardMatrix
Definition: SparseConnection.h:52