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