CARLsim  5.0.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 *
46 * CARLsim available from http://socsci.uci.edu/~jkrichma/CARLsim/
47 * Ver 12/31/2016
48 */
49 #ifndef _CONN_MON_CORE_H_
50 #define _CONN_MON_CORE_H_
51 
52 #include <stdio.h> // FILE
53 #include <vector> // std::vector
54 #include <carlsim_definitions.h> // ALL
55 
56 class SNN; // forward declaration of SNN class
57 
58 
59 /*
60  * \brief ConnectionMonitor private core implementation
61  *
62  * Naming convention for methods:
63  * - getNum*: a number metric, returns an int
64  * - getTime*: a time metric, returns long int
65  * - getPercent*: a percentage metric, returns a double
66  * - get*: all the others
67  */
69 public:
71  ConnectionMonitorCore(SNN* snn, int monitorId, short int connId, int grpIdPre, int grpIdPost);
72 
75 
76 
77  // +++++ PUBLIC METHODS: +++++++++++++++++++++++++++++++++++++++++++++++//
78 
80  std::vector< std::vector<float> > calcWeightChanges();
81 
83  short int getConnectId() { return connId_; }
84 
86  FILE* getConnectFileId() { return connFileId_; }
87 
89  int getFanIn(int neurPostId);
90 
92  int getFanOut(int neurPreId);
93 
95  float getMaxWeight(bool getCurrent=false);
96 
98  float getMinWeight(bool getCurrent=false);
99 
100 
102  int getMonitorId() { return monitorId_; }
103 
105  int getNumNeuronsPre() { return nNeurPre_; }
106 
108  int getNumNeuronsPost() { return nNeurPost_; }
109 
111  int getNumSynapses() { return nSynapses_; }
112 
114  int getNumWeightsChanged(double minAbsChanged=1e-5);
115 
117  int getNumWeightsInRange(double minVal, double maxVal);
118 
120  int getNumWeightsWithValue(double value);
121 
123  long int getTimeMsCurrentSnapshot() { return wtTime_; }
124 
126  long int getTimeMsLastSnapshot() { return wtTimeLast_; }
127 
129  long int getTimeMsSinceLastSnapshot() { return (wtTime_ - wtTimeLast_); }
130 
131  int getUpdateTimeIntervalSec() { return connFileTimeIntervalSec_; }
132 
134  double getTotalAbsWeightChange();
135 
137  void print();
138 
142  void printSparse(int neurPostId=ALL, int maxConn=100, int connPerLine=4, bool storeNewSnapshot=true);
143 
146  std::vector< std::vector<float> > takeSnapshot();
147 
148 
149  // +++++ PUBLIC METHODS THAT SHOULD NOT BE EXPOSED TO INTERFACE +++++++++//
150 
152  void clear();
153 
156  void init();
157 
159  void updateWeight(int preId, int postId, float wt);
160 
162  bool updateTime(int simTimeMs);
163 
165  void setConnectFileId(FILE* connFileId);
166 
168  void setUpdateTimeIntervalSec(int intervalSec);
169 
171  void writeConnectFileSnapshot(int simTimeMs, std::vector< std::vector<float> > wts);
172 
173 private:
175  bool needToWriteSnapshot();
176 
177  void updateStoredWeights();
178 
180  void writeConnectFileHeader();
181 
182  SNN* snn_;
183  int monitorId_;
184  short int connId_;
185  int grpIdPre_;
186  int grpIdPost_;
187  int nNeurPre_;
188  int nNeurPost_;
189  int nSynapses_;
190 
191  float minWt_;
192  float maxWt_;
193 
194  bool isPlastic_;
195 
196  std::vector< std::vector<float> > wtMat_;
197  std::vector< std::vector<float> > wtMatLast_;
198  long long wtTime_;
199  long long wtTimeLast_;
200  long long wtTimeWrite_;
201 
202  bool needToInit_;
203  bool needToWriteFileHeader_;
204 
205  FILE* connFileId_;
206  int connFileSignature_;
207  float connFileVersion_;
208  int connFileTimeIntervalSec_;
209 
210  const FILE* fpInf_;
211  const FILE* fpErr_;
212  const FILE* fpDeb_;
213  const FILE* fpLog_;
214 };
215 
216 #endif
ConnectionMonitorCore::updateTime
bool updateTime(int simTimeMs)
updates timestamp of the snapshots, returns true if update was needed
ConnectionMonitorCore::setUpdateTimeIntervalSec
void setUpdateTimeIntervalSec(int intervalSec)
sets time update interval (seconds) for periodically storing weights to file
Definition: connection_monitor_core.cpp:432
ConnectionMonitorCore::getNumWeightsInRange
int getNumWeightsInRange(double minVal, double maxVal)
returns number of weights with values in range e[minVal,maxVal] (inclusive)
Definition: connection_monitor_core.cpp:261
ConnectionMonitorCore::calcWeightChanges
std::vector< std::vector< float > > calcWeightChanges()
calculates weight changes since last snapshot and reports them in 2D weight change matrix
Definition: connection_monitor_core.cpp:141
ConnectionMonitorCore::getMinWeight
float getMinWeight(bool getCurrent=false)
returns min weight in the connection (getCurrent=false: RangeWeight.min, true: current smallest)
Definition: connection_monitor_core.cpp:215
ConnectionMonitorCore
Definition: connection_monitor_core.h:68
ConnectionMonitorCore::getTimeMsCurrentSnapshot
long int getTimeMsCurrentSnapshot()
returns the timestamp of the current snapshot (not necessarily CARLsim::getSimTime)
Definition: connection_monitor_core.h:123
ConnectionMonitorCore::print
void print()
prints current weight state as 2D matrix (non-existent synapses: NAN, existent but zero weigth: 0....
Definition: connection_monitor_core.cpp:312
ConnectionMonitorCore::ConnectionMonitorCore
ConnectionMonitorCore(SNN *snn, int monitorId, short int connId, int grpIdPre, int grpIdPost)
constructor, created by CARLsim::setConnectionMonitor
Definition: connection_monitor_core.cpp:62
ConnectionMonitorCore::printSparse
void printSparse(int neurPostId=ALL, int maxConn=100, int connPerLine=4, bool storeNewSnapshot=true)
Definition: connection_monitor_core.cpp:342
ConnectionMonitorCore::getConnectId
short int getConnectId()
returns connection ID
Definition: connection_monitor_core.h:83
ConnectionMonitorCore::getNumWeightsWithValue
int getNumWeightsWithValue(double value)
returns number of weights that have a certain value
Definition: connection_monitor_core.cpp:288
ConnectionMonitorCore::updateWeight
void updateWeight(int preId, int postId, float wt)
updates an entry in the current weight matrix (called by CARLsim::updateConnectionMonitor)
ConnectionMonitorCore::getMaxWeight
float getMaxWeight(bool getCurrent=false)
returns max weight in the connection (getCurrent=false: RangeWeight.max, true: current largest)
Definition: connection_monitor_core.cpp:190
ConnectionMonitorCore::getMonitorId
int getMonitorId()
returns ConnectionMonitor ID
Definition: connection_monitor_core.h:102
ConnectionMonitorCore::getNumNeuronsPost
int getNumNeuronsPost()
returns number of neurons in post-synaptic group
Definition: connection_monitor_core.h:108
ConnectionMonitorCore::clear
void clear()
deletes data from the 2D weight matrix
Definition: connection_monitor_core.cpp:157
ALL
#define ALL
CARLsim common definitions.
Definition: carlsim_definitions.h:56
ConnectionMonitorCore::getConnectFileId
FILE * getConnectFileId()
returns pointer to connection file
Definition: connection_monitor_core.h:86
ConnectionMonitorCore::getNumWeightsChanged
int getNumWeightsChanged(double minAbsChanged=1e-5)
returns number of weights with >=minAbsChanged weight change since last snapshot
Definition: connection_monitor_core.cpp:241
ConnectionMonitorCore::~ConnectionMonitorCore
~ConnectionMonitorCore()
destructor, cleans up all the memory upon object deletion
Definition: connection_monitor_core.cpp:121
ConnectionMonitorCore::getFanOut
int getFanOut(int neurPreId)
returns number of outgoing synapses of pre-synaptic neuron
Definition: connection_monitor_core.cpp:179
ConnectionMonitorCore::getTimeMsLastSnapshot
long int getTimeMsLastSnapshot()
returns the timestamp of the last snapshot
Definition: connection_monitor_core.h:126
ConnectionMonitorCore::getNumSynapses
int getNumSynapses()
returns number of synapses that exist in the connection
Definition: connection_monitor_core.h:111
ConnectionMonitorCore::setConnectFileId
void setConnectFileId(FILE *connFileId)
sets pointer to connection file
Definition: connection_monitor_core.cpp:415
ConnectionMonitorCore::getNumNeuronsPre
int getNumNeuronsPre()
returns number of neurons in pre-synaptic group
Definition: connection_monitor_core.h:105
ConnectionMonitorCore::getTotalAbsWeightChange
double getTotalAbsWeightChange()
returns absolute sum of all weight changes since last snapshot
Definition: connection_monitor_core.cpp:298
ConnectionMonitorCore::writeConnectFileSnapshot
void writeConnectFileSnapshot(int simTimeMs, std::vector< std::vector< float > > wts)
writes each snapshot to connect file
Definition: connection_monitor_core.cpp:518
SNN
Contains all of CARLsim's core functionality.
Definition: snn.h:115
ConnectionMonitorCore::getTimeMsSinceLastSnapshot
long int getTimeMsSinceLastSnapshot()
returns the time passed between current and last snapshot
Definition: connection_monitor_core.h:129
ConnectionMonitorCore::init
void init()
Definition: connection_monitor_core.cpp:85
carlsim_definitions.h
ConnectionMonitorCore::getUpdateTimeIntervalSec
int getUpdateTimeIntervalSec()
Definition: connection_monitor_core.h:131
ConnectionMonitorCore::getFanIn
int getFanIn(int neurPostId)
returns number of incoming synapses to post-synaptic neuron
Definition: connection_monitor_core.cpp:167
ConnectionMonitorCore::takeSnapshot
std::vector< std::vector< float > > takeSnapshot()
Definition: connection_monitor_core.cpp:450