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