CARLsim  4.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 *
45 * CARLsim available from http://socsci.uci.edu/~jkrichma/CARLsim/
46 * Ver 12/31/2016
47 */
48 #ifndef _CONN_MON_CORE_H_
49 #define _CONN_MON_CORE_H_
50 
51 #include <stdio.h> // FILE
52 #include <vector> // std::vector
53 #include <carlsim_definitions.h> // ALL
54 
55 class SNN; // forward declaration of SNN class
56 
57 
58 /*
59  * \brief ConnectionMonitor private core implementation
60  *
61  * Naming convention for methods:
62  * - getNum*: a number metric, returns an int
63  * - getTime*: a time metric, returns long int
64  * - getPercent*: a percentage metric, returns a double
65  * - get*: all the others
66  */
68 public:
70  ConnectionMonitorCore(SNN* snn, int monitorId, short int connId, int grpIdPre, int grpIdPost);
71 
74 
75 
76  // +++++ PUBLIC METHODS: +++++++++++++++++++++++++++++++++++++++++++++++//
77 
79  std::vector< std::vector<float> > calcWeightChanges();
80 
82  short int getConnectId() { return connId_; }
83 
85  FILE* getConnectFileId() { return connFileId_; }
86 
88  int getFanIn(int neurPostId);
89 
91  int getFanOut(int neurPreId);
92 
94  float getMaxWeight(bool getCurrent=false);
95 
97  float getMinWeight(bool getCurrent=false);
98 
99 
101  int getMonitorId() { return monitorId_; }
102 
104  int getNumNeuronsPre() { return nNeurPre_; }
105 
107  int getNumNeuronsPost() { return nNeurPost_; }
108 
110  int getNumSynapses() { return nSynapses_; }
111 
113  int getNumWeightsChanged(double minAbsChanged=1e-5);
114 
116  int getNumWeightsInRange(double minVal, double maxVal);
117 
119  int getNumWeightsWithValue(double value);
120 
122  long int getTimeMsCurrentSnapshot() { return wtTime_; }
123 
125  long int getTimeMsLastSnapshot() { return wtTimeLast_; }
126 
128  long int getTimeMsSinceLastSnapshot() { return (wtTime_ - wtTimeLast_); }
129 
130  int getUpdateTimeIntervalSec() { return connFileTimeIntervalSec_; }
131 
133  double getTotalAbsWeightChange();
134 
136  void print();
137 
141  void printSparse(int neurPostId=ALL, int maxConn=100, int connPerLine=4, bool storeNewSnapshot=true);
142 
145  std::vector< std::vector<float> > takeSnapshot();
146 
147 
148  // +++++ PUBLIC METHODS THAT SHOULD NOT BE EXPOSED TO INTERFACE +++++++++//
149 
151  void clear();
152 
155  void init();
156 
158  void updateWeight(int preId, int postId, float wt);
159 
161  bool updateTime(int simTimeMs);
162 
164  void setConnectFileId(FILE* connFileId);
165 
167  void setUpdateTimeIntervalSec(int intervalSec);
168 
170  void writeConnectFileSnapshot(int simTimeMs, std::vector< std::vector<float> > wts);
171 
172 private:
174  bool needToWriteSnapshot();
175 
176  void updateStoredWeights();
177 
179  void writeConnectFileHeader();
180 
181  SNN* snn_;
182  int monitorId_;
183  short int connId_;
184  int grpIdPre_;
185  int grpIdPost_;
186  int nNeurPre_;
187  int nNeurPost_;
188  int nSynapses_;
189 
190  float minWt_;
191  float maxWt_;
192 
193  bool isPlastic_;
194 
195  std::vector< std::vector<float> > wtMat_;
196  std::vector< std::vector<float> > wtMatLast_;
197  long long wtTime_;
198  long long wtTimeLast_;
199  long long wtTimeWrite_;
200 
201  bool needToInit_;
202  bool needToWriteFileHeader_;
203 
204  FILE* connFileId_;
205  int connFileSignature_;
206  float connFileVersion_;
207  int connFileTimeIntervalSec_;
208 
209  const FILE* fpInf_;
210  const FILE* fpErr_;
211  const FILE* fpDeb_;
212  const FILE* fpLog_;
213 };
214 
215 #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:431
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:260
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:140
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:214
ConnectionMonitorCore
Definition: connection_monitor_core.h:67
ConnectionMonitorCore::getTimeMsCurrentSnapshot
long int getTimeMsCurrentSnapshot()
returns the timestamp of the current snapshot (not necessarily CARLsim::getSimTime)
Definition: connection_monitor_core.h:122
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:311
ConnectionMonitorCore::ConnectionMonitorCore
ConnectionMonitorCore(SNN *snn, int monitorId, short int connId, int grpIdPre, int grpIdPost)
constructor, created by CARLsim::setConnectionMonitor
Definition: connection_monitor_core.cpp:61
ConnectionMonitorCore::printSparse
void printSparse(int neurPostId=ALL, int maxConn=100, int connPerLine=4, bool storeNewSnapshot=true)
Definition: connection_monitor_core.cpp:341
ConnectionMonitorCore::getConnectId
short int getConnectId()
returns connection ID
Definition: connection_monitor_core.h:82
ConnectionMonitorCore::getNumWeightsWithValue
int getNumWeightsWithValue(double value)
returns number of weights that have a certain value
Definition: connection_monitor_core.cpp:287
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:189
ConnectionMonitorCore::getMonitorId
int getMonitorId()
returns ConnectionMonitor ID
Definition: connection_monitor_core.h:101
ConnectionMonitorCore::getNumNeuronsPost
int getNumNeuronsPost()
returns number of neurons in post-synaptic group
Definition: connection_monitor_core.h:107
ConnectionMonitorCore::clear
void clear()
deletes data from the 2D weight matrix
Definition: connection_monitor_core.cpp:156
ALL
#define ALL
CARLsim common definitions.
Definition: carlsim_definitions.h:55
ConnectionMonitorCore::getConnectFileId
FILE * getConnectFileId()
returns pointer to connection file
Definition: connection_monitor_core.h:85
ConnectionMonitorCore::getNumWeightsChanged
int getNumWeightsChanged(double minAbsChanged=1e-5)
returns number of weights with >=minAbsChanged weight change since last snapshot
Definition: connection_monitor_core.cpp:240
ConnectionMonitorCore::~ConnectionMonitorCore
~ConnectionMonitorCore()
destructor, cleans up all the memory upon object deletion
Definition: connection_monitor_core.cpp:120
ConnectionMonitorCore::getFanOut
int getFanOut(int neurPreId)
returns number of outgoing synapses of pre-synaptic neuron
Definition: connection_monitor_core.cpp:178
ConnectionMonitorCore::getTimeMsLastSnapshot
long int getTimeMsLastSnapshot()
returns the timestamp of the last snapshot
Definition: connection_monitor_core.h:125
ConnectionMonitorCore::getNumSynapses
int getNumSynapses()
returns number of synapses that exist in the connection
Definition: connection_monitor_core.h:110
ConnectionMonitorCore::setConnectFileId
void setConnectFileId(FILE *connFileId)
sets pointer to connection file
Definition: connection_monitor_core.cpp:414
ConnectionMonitorCore::getNumNeuronsPre
int getNumNeuronsPre()
returns number of neurons in pre-synaptic group
Definition: connection_monitor_core.h:104
ConnectionMonitorCore::getTotalAbsWeightChange
double getTotalAbsWeightChange()
returns absolute sum of all weight changes since last snapshot
Definition: connection_monitor_core.cpp:297
ConnectionMonitorCore::writeConnectFileSnapshot
void writeConnectFileSnapshot(int simTimeMs, std::vector< std::vector< float > > wts)
writes each snapshot to connect file
Definition: connection_monitor_core.cpp:517
SNN
Contains all of CARLsim's core functionality.
Definition: snn.h:114
ConnectionMonitorCore::getTimeMsSinceLastSnapshot
long int getTimeMsSinceLastSnapshot()
returns the time passed between current and last snapshot
Definition: connection_monitor_core.h:128
ConnectionMonitorCore::init
void init()
Definition: connection_monitor_core.cpp:84
carlsim_definitions.h
ConnectionMonitorCore::getUpdateTimeIntervalSec
int getUpdateTimeIntervalSec()
Definition: connection_monitor_core.h:130
ConnectionMonitorCore::getFanIn
int getFanIn(int neurPostId)
returns number of incoming synapses to post-synaptic neuron
Definition: connection_monitor_core.cpp:166
ConnectionMonitorCore::takeSnapshot
std::vector< std::vector< float > > takeSnapshot()
Definition: connection_monitor_core.cpp:449