CARLsim  5.0.0
CARLsim: a GPU-accelerated SNN simulator
connection_monitor.cpp
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 #include <connection_monitor.h>
50 #include <connection_monitor_core.h> // ConnectionMonitor private implementation
51 
52 #include <user_errors.h> // fancy user error messages
53 #include <sstream> // std::stringstream
54 
55 
57  connMonCorePtr_ = connMonCorePtr;
58 }
59 
61  delete connMonCorePtr_;
62 }
63 
64 
65 // +++++ PUBLIC METHODS: +++++++++++++++++++++++++++++++++++++++++++++++//
66 
67 std::vector< std::vector<float> > ConnectionMonitor::calcWeightChanges() {
68  return connMonCorePtr_->calcWeightChanges();
69 }
70 
72  return connMonCorePtr_->getConnectId();
73 }
74 
75 int ConnectionMonitor::getFanIn(int neurPostId) {
76  std::string funcName = "getFanIn()";
77  UserErrors::assertTrue(neurPostId<getNumNeuronsPost(), UserErrors::MUST_BE_SMALLER, funcName, "neurPostId",
78  "getNumNeuronsPost()");
79  return connMonCorePtr_->getFanIn(neurPostId);
80 }
81 
82 int ConnectionMonitor::getFanOut(int neurPreId) {
83  std::string funcName = "getFanOut()";
84  UserErrors::assertTrue(neurPreId<getNumNeuronsPre(), UserErrors::MUST_BE_SMALLER, funcName, "neurPreId",
85  "getNumNeuronsPre()");
86  return connMonCorePtr_->getFanOut(neurPreId);
87 }
88 
90  return connMonCorePtr_->getNumNeuronsPre();
91 }
92 
94  return connMonCorePtr_->getNumNeuronsPost();
95 }
96 
97 double ConnectionMonitor::getMaxWeight(bool getCurrent) {
98  return (double) (connMonCorePtr_->getMaxWeight(getCurrent));
99 }
100 
101 double ConnectionMonitor::getMinWeight(bool getCurrent) {
102  return (double) (connMonCorePtr_->getMinWeight(getCurrent));
103 }
104 
106  return connMonCorePtr_->getNumSynapses();
107 }
108 
109 int ConnectionMonitor::getNumWeightsChanged(double minAbsChanged) {
110  std::string funcName = "getNumWeightsChanged()";
111  UserErrors::assertTrue(minAbsChanged>=0.0, UserErrors::CANNOT_BE_NEGATIVE, funcName, "minAbsChanged");
112  return connMonCorePtr_->getNumWeightsChanged(minAbsChanged);
113 }
114 
115 int ConnectionMonitor::getNumWeightsInRange(double minValue, double maxValue) {
116  std::string funcName = "getNumWeightsInRange()";
117  UserErrors::assertTrue(maxValue >= minValue, UserErrors::CANNOT_BE_SMALLER, funcName, "maxValue", "minValue");
118  return connMonCorePtr_->getNumWeightsInRange(minValue,maxValue);
119 }
120 
122  std::string funcName = "getNumWeightsWithValue()";
123  return connMonCorePtr_->getNumWeightsWithValue(value);
124 }
125 
126 double ConnectionMonitor::getPercentWeightsChanged(double minAbsChanged) {
127  return getNumWeightsChanged(minAbsChanged)*100.0/getNumSynapses();
128 }
129 
130 double ConnectionMonitor::getPercentWeightsInRange(double minVal, double maxVal) {
131  return getNumWeightsInRange(minVal,maxVal)*100.0/getNumSynapses();
132 }
133 
135  return getNumWeightsWithValue(value)*100.0/getNumSynapses();
136 }
137 
139  return connMonCorePtr_->getTimeMsCurrentSnapshot();
140 }
141 
143  return connMonCorePtr_->getTimeMsLastSnapshot();
144 }
145 
147  return connMonCorePtr_->getTimeMsSinceLastSnapshot();
148 }
149 
151  return connMonCorePtr_->getTotalAbsWeightChange();
152 }
153 
155  connMonCorePtr_->print();
156 }
157 
158 void ConnectionMonitor::printSparse(int neurPostId, int maxConn, int connPerLine) {
159  std::string funcName = "printSparse()";
160  UserErrors::assertTrue(neurPostId<getNumNeuronsPost(), UserErrors::MUST_BE_SMALLER, funcName, "neurPostId",
161  "getNumNeuronsPost()");
162  UserErrors::assertTrue(maxConn>0, UserErrors::MUST_BE_POSITIVE, funcName, "maxConn");
163  UserErrors::assertTrue(connPerLine>0, UserErrors::MUST_BE_POSITIVE, funcName, "connPerLine");
164  connMonCorePtr_->printSparse(neurPostId,maxConn,connPerLine);
165 }
166 
168  std::string funcName = "setUpdateTimeIntervalSec()";
169  UserErrors::assertTrue(intervalSec==-1 || intervalSec>=1, UserErrors::MUST_BE_SET_TO, funcName, "intervalSec",
170  "-1 or >= 1.");
171  connMonCorePtr_->setUpdateTimeIntervalSec(intervalSec);
172 }
173 
174 std::vector< std::vector<float> > ConnectionMonitor::takeSnapshot() {
175  return connMonCorePtr_->takeSnapshot();
176 }
ConnectionMonitor::getPercentWeightsWithValue
double getPercentWeightsWithValue(double value)
Returns the percentage of weights in the connection with a particular value.
Definition: connection_monitor.cpp:134
ConnectionMonitorCore::setUpdateTimeIntervalSec
void setUpdateTimeIntervalSec(int intervalSec)
sets time update interval (seconds) for periodically storing weights to file
Definition: connection_monitor_core.cpp:432
connection_monitor_core.h
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
ConnectionMonitor::setUpdateTimeIntervalSec
void setUpdateTimeIntervalSec(int intervalSec)
Sets the time interval (seconds) for writing snapshots to file.
Definition: connection_monitor.cpp:167
UserErrors::MUST_BE_SMALLER
@ MUST_BE_SMALLER
parameter must be smaller than
Definition: user_errors.h:56
ConnectionMonitor::getPercentWeightsChanged
double getPercentWeightsChanged(double minAbsChanged=1e-5)
Returns the percentage of weights that have changed since the last snapshot.
Definition: connection_monitor.cpp:126
ConnectionMonitor::~ConnectionMonitor
~ConnectionMonitor()
ConnectionMonitor destructor.
Definition: connection_monitor.cpp:60
user_errors.h
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
ConnectionMonitor::getNumWeightsInRange
int getNumWeightsInRange(double minValue, double maxValue)
Returns the number of weights in the connection whose values are within some range (inclusive)
Definition: connection_monitor.cpp:115
ConnectionMonitor::getTimeMsSinceLastSnapshot
long int getTimeMsSinceLastSnapshot()
Returns the timestamp difference of the current and last snapshot.
Definition: connection_monitor.cpp:146
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
ConnectionMonitor::getNumNeuronsPost
int getNumNeuronsPost()
Returns the number of post-synaptic neurons.
Definition: connection_monitor.cpp:93
ConnectionMonitorCore
Definition: connection_monitor_core.h:68
UserErrors::MUST_BE_POSITIVE
@ MUST_BE_POSITIVE
parameter must have positive value
Definition: user_errors.h:53
ConnectionMonitor::getTimeMsCurrentSnapshot
long int getTimeMsCurrentSnapshot()
Returns the timestamp of the current snapshot (ms since beginning of simulation)
Definition: connection_monitor.cpp:138
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
ConnectionMonitor::print
void print()
Prints the current weight state as a 2D matrix (pre x post)
Definition: connection_monitor.cpp:154
ConnectionMonitor::getNumWeightsWithValue
int getNumWeightsWithValue(double value)
Returns the number of weights in the connection with a particular value.
Definition: connection_monitor.cpp:121
ConnectionMonitorCore::printSparse
void printSparse(int neurPostId=ALL, int maxConn=100, int connPerLine=4, bool storeNewSnapshot=true)
Definition: connection_monitor_core.cpp:342
ConnectionMonitor::getTimeMsLastSnapshot
long int getTimeMsLastSnapshot()
Returns the timestamp of the last snapshot (ms since beginning of simulation)
Definition: connection_monitor.cpp:142
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::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
ConnectionMonitor::getFanIn
int getFanIn(int neurPostId)
Returns the number of incoming synapses for a specific post-synaptic neuron.
Definition: connection_monitor.cpp:75
ConnectionMonitor::getConnectId
short int getConnectId()
Returns the connection ID that this ConnectionMonitor is managing.
Definition: connection_monitor.cpp:71
ConnectionMonitor::printSparse
void printSparse(int neurPostId=ALL, int maxConn=100, int connPerLine=4)
Prints the current weight state as a sparse list of weights.
Definition: connection_monitor.cpp:158
ConnectionMonitorCore::getNumNeuronsPost
int getNumNeuronsPost()
returns number of neurons in post-synaptic group
Definition: connection_monitor_core.h:108
connection_monitor.h
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::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
ConnectionMonitor::getPercentWeightsInRange
double getPercentWeightsInRange(double minValue, double maxValue)
Returns the percentage of weights whose values are within some range (inclusive)
Definition: connection_monitor.cpp:130
ConnectionMonitor::calcWeightChanges
std::vector< std::vector< float > > calcWeightChanges()
Reports the weight changes since the last snapshot in a 2D weight matrix (pre x post)
Definition: connection_monitor.cpp:67
ConnectionMonitorCore::getNumNeuronsPre
int getNumNeuronsPre()
returns number of neurons in pre-synaptic group
Definition: connection_monitor_core.h:105
ConnectionMonitor::getTotalAbsWeightChange
double getTotalAbsWeightChange()
Returns the absolute sum of all the weight changes since the last snapshot.
Definition: connection_monitor.cpp:150
ConnectionMonitor::getNumSynapses
int getNumSynapses()
Returns the number of allocated synapses.
Definition: connection_monitor.cpp:105
ConnectionMonitor::getNumNeuronsPre
int getNumNeuronsPre()
Returns the number of pre-synaptic neurons.
Definition: connection_monitor.cpp:89
ConnectionMonitor::takeSnapshot
std::vector< std::vector< float > > takeSnapshot()
Takes a snapshot of the current weight state.
Definition: connection_monitor.cpp:174
ConnectionMonitorCore::getTotalAbsWeightChange
double getTotalAbsWeightChange()
returns absolute sum of all weight changes since last snapshot
Definition: connection_monitor_core.cpp:298
ConnectionMonitor::getMaxWeight
double getMaxWeight(bool getCurrent=false)
Returns the max weight in the connection.
Definition: connection_monitor.cpp:97
ConnectionMonitor::getNumWeightsChanged
int getNumWeightsChanged(double minAbsChanged=1e-5)
Returns the number of weights that have changed since the last snapshot.
Definition: connection_monitor.cpp:109
UserErrors::MUST_BE_SET_TO
@ MUST_BE_SET_TO
parameter must be set to
Definition: user_errors.h:54
ConnectionMonitor::getMinWeight
double getMinWeight(bool getCurrent=false)
Returns the min weight in the connection.
Definition: connection_monitor.cpp:101
UserErrors::assertTrue
static void assertTrue(bool statement, errorType errorIfAssertionFails, std::string errorFunc, std::string errorMsgPrefix="", std::string errorMsgSuffix="")
simple wrapper for assert statement
Definition: user_errors.cpp:15
UserErrors::CANNOT_BE_NEGATIVE
@ CANNOT_BE_NEGATIVE
parameter cannot have negative value (opposite to "must be", but includes zero)
Definition: user_errors.h:33
ConnectionMonitorCore::getTimeMsSinceLastSnapshot
long int getTimeMsSinceLastSnapshot()
returns the time passed between current and last snapshot
Definition: connection_monitor_core.h:129
UserErrors::CANNOT_BE_SMALLER
@ CANNOT_BE_SMALLER
parameter cannot have smaller vaule than some vaule
Definition: user_errors.h:36
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
ConnectionMonitor::ConnectionMonitor
ConnectionMonitor(ConnectionMonitorCore *connMonCorePtr)
ConnectionMonitor constructor.
Definition: connection_monitor.cpp:56
ConnectionMonitor::getFanOut
int getFanOut(int neurPreId)
Returns the number of outgoing synapses for a specific pre-synaptic neuron.
Definition: connection_monitor.cpp:82