CARLsim  6.1.0
CARLsim: a GPU-accelerated SNN simulator
connection_monitor_core.h
Go to the documentation of this file.
1 /* * Copyright (c) 2016 Regents of the University of California. All rights reserved.
2 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions
5 * are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * 3. The names of its contributors may not be used to endorse or promote
15 * products derived from this software without specific prior written
16 * permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * *********************************************************************************************** *
31 * CARLsim
32 * created by: (MDR) Micah Richert, (JN) Jayram M. Nageswaran
33 * maintained by:
34 * (MA) Mike Avery <averym@uci.edu>
35 * (MB) Michael Beyeler <mbeyeler@uci.edu>,
36 * (KDC) Kristofor Carlson <kdcarlso@uci.edu>
37 * (TSC) Ting-Shuo Chou <tingshuc@uci.edu>
38 * (HK) Hirak J Kashyap <kashyaph@uci.edu>
39 *
40 * CARLsim v1.0: JM, MDR
41 * CARLsim v2.0/v2.1/v2.2: JM, MDR, MA, MB, KDC
42 * CARLsim3: MB, KDC, TSC
43 * CARLsim4: TSC, HK
44 * CARLsim5: HK, JX, KC
45 * CARLsim6: LN, JX, KC, KW
46 *
47 * CARLsim available from http://socsci.uci.edu/~jkrichma/CARLsim/
48 * Ver 12/31/2016
49 */
50 #ifndef _CONN_MON_CORE_H_
51 #define _CONN_MON_CORE_H_
52 
53 
54 #include <stdio.h> // FILE
55 #include <vector> // std::vector
56 #include <carlsim_definitions.h> // ALL
57 
58 class SNN; // forward declaration of SNN class
59 
60 
61 /*
62  * \brief ConnectionMonitor private core implementation
63  *
64  * Naming convention for methods:
65  * - getNum*: a number metric, returns an int
66  * - getTime*: a time metric, returns long int
67  * - getPercent*: a percentage metric, returns a double
68  * - get*: all the others
69  */
71 public:
73  ConnectionMonitorCore(SNN* snn, int monitorId, short int connId, int grpIdPre, int grpIdPost);
74 
77 
78 
79  // +++++ PUBLIC METHODS: +++++++++++++++++++++++++++++++++++++++++++++++//
80 
82  std::vector< std::vector<float> > getWeights();
83 
85  std::vector< std::vector<float> > getPrevWeights();
86 
88  std::vector< std::vector<float> > calcWeightChanges();
89 
91  short int getConnectId() { return connId_; }
92 
94  FILE* getConnectFileId() { return connFileId_; }
95 
97  int getFanIn(int neurPostId);
98 
100  int getFanOut(int neurPreId);
101 
103  float getMaxWeight(bool getCurrent=false);
104 
106  float getMinWeight(bool getCurrent=false);
107 
108 
110  int getMonitorId() { return monitorId_; }
111 
113  int getNumNeuronsPre() { return nNeurPre_; }
114 
116  int getNumNeuronsPost() { return nNeurPost_; }
117 
119  int getNumSynapses() { return nSynapses_; }
120 
122  int getNumWeightsChanged(double minAbsChanged=1e-5);
123 
125  int getNumWeightsInRange(double minVal, double maxVal);
126 
128  int getNumWeightsWithValue(double value);
129 
131  long int getTimeMsCurrentSnapshot() { return wtTime_; }
132 
134  long int getTimeMsLastSnapshot() { return wtTimeLast_; }
135 
137  long int getTimeMsSinceLastSnapshot() { return (wtTime_ - wtTimeLast_); }
138 
139  int getUpdateTimeIntervalSec() { return connFileTimeIntervalSec_; }
140 
142  double getTotalAbsWeightChange();
143 
145  void print();
146 
150  void printSparse(int neurPostId=ALL, int maxConn=100, int connPerLine=4, bool storeNewSnapshot=true);
151 
154  std::vector< std::vector<float> > takeSnapshot();
155 
156 
157  // +++++ PUBLIC METHODS THAT SHOULD NOT BE EXPOSED TO INTERFACE +++++++++//
158 
160  void clear();
161 
164  void init();
165 
167  void updateWeight(int preId, int postId, float wt);
168 
170  bool updateTime(int simTimeMs);
171 
173  void setConnectFileId(FILE* connFileId);
174 
176  void setUpdateTimeIntervalSec(int intervalSec);
177 
179  void writeConnectFileSnapshot(int simTimeMs, std::vector< std::vector<float> > wts);
180 
181 private:
183  bool needToWriteSnapshot();
184 
185  void updateStoredWeights();
186 
188  void writeConnectFileHeader();
189 
190  SNN* snn_;
191  int monitorId_;
192  short int connId_;
193  int grpIdPre_;
194  int grpIdPost_;
195  int nNeurPre_;
196  int nNeurPost_;
197  int nSynapses_;
198 
199  float minWt_;
200  float maxWt_;
201 
202  bool isPlastic_;
203 
204  std::vector< std::vector<float> > wtMat_;
205  std::vector< std::vector<float> > wtMatLast_;
206  long long wtTime_;
207  long long wtTimeLast_;
208  long long wtTimeWrite_;
209 
210  bool needToInit_;
211  bool needToWriteFileHeader_;
212 
213  FILE* connFileId_;
214  int connFileSignature_;
215  float connFileVersion_;
216  int connFileTimeIntervalSec_;
217 
218  const FILE* fpInf_;
219  const FILE* fpErr_;
220  const FILE* fpDeb_;
221  const FILE* fpLog_;
222 };
223 
224 #endif
FILE * getConnectFileId()
returns pointer to connection file
short int getConnectId()
returns connection ID
#define ALL
CARLsim common definitions.
long int getTimeMsSinceLastSnapshot()
returns the time passed between current and last snapshot
std::vector< std::vector< float > > getWeights()
LN20201118 returns calculates current and reports them in 2D weight matrix.
float getMinWeight(bool getCurrent=false)
returns min weight in the connection (getCurrent=false: RangeWeight.min, true: current smallest) ...
std::vector< std::vector< float > > takeSnapshot()
ConnectionMonitorCore(SNN *snn, int monitorId, short int connId, int grpIdPre, int grpIdPost)
constructor, created by CARLsim::setConnectionMonitor
float getMaxWeight(bool getCurrent=false)
returns max weight in the connection (getCurrent=false: RangeWeight.max, true: current largest) ...
~ConnectionMonitorCore()
destructor, cleans up all the memory upon object deletion
int getFanIn(int neurPostId)
returns number of incoming synapses to post-synaptic neuron
std::vector< std::vector< float > > getPrevWeights()
LN20201118 returns calculates previous weights and reports them in 2D weight matrix.
double getTotalAbsWeightChange()
returns absolute sum of all weight changes since last snapshot
bool updateTime(int simTimeMs)
updates timestamp of the snapshots, returns true if update was needed
void setConnectFileId(FILE *connFileId)
sets pointer to connection file
int getNumWeightsInRange(double minVal, double maxVal)
returns number of weights with values in range e[minVal,maxVal] (inclusive)
int getNumSynapses()
returns number of synapses that exist in the connection
void print()
prints current weight state as 2D matrix (non-existent synapses: NAN, existent but zero weigth: 0...
long int getTimeMsLastSnapshot()
returns the timestamp of the last snapshot
int getMonitorId()
returns ConnectionMonitor ID
void writeConnectFileSnapshot(int simTimeMs, std::vector< std::vector< float > > wts)
writes each snapshot to connect file
int getNumNeuronsPre()
returns number of neurons in pre-synaptic group
void updateWeight(int preId, int postId, float wt)
updates an entry in the current weight matrix (called by CARLsim::updateConnectionMonitor) ...
void printSparse(int neurPostId=ALL, int maxConn=100, int connPerLine=4, bool storeNewSnapshot=true)
Contains all of CARLsim&#39;s core functionality.
Definition: snn.h:138
int getNumWeightsChanged(double minAbsChanged=1e-5)
returns number of weights with >=minAbsChanged weight change since last snapshot
int getNumWeightsWithValue(double value)
returns number of weights that have a certain value
void setUpdateTimeIntervalSec(int intervalSec)
sets time update interval (seconds) for periodically storing weights to file
void clear()
deletes data from the 2D weight matrix
std::vector< std::vector< float > > calcWeightChanges()
calculates weight changes since last snapshot and reports them in 2D weight change matrix ...
long int getTimeMsCurrentSnapshot()
returns the timestamp of the current snapshot (not necessarily CARLsim::getSimTime) ...
int getNumNeuronsPost()
returns number of neurons in post-synaptic group
int getFanOut(int neurPreId)
returns number of outgoing synapses of pre-synaptic neuron