Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup exceptions #742

Merged
merged 12 commits into from
Nov 9, 2019
2 changes: 1 addition & 1 deletion src/htm/engine/Input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ void Input::uninitialize() {

namespace htm {
std::ostream &operator<<(std::ostream &f, const Input &d) {
f << "Input: " << d.getRegion()->getName() << "." << d.getName() << " " << d.getData();
f << "Input: " << d.getRegion()->getName() << "." << d.getName() << " " << d.getData();
return f;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/htm/engine/Link.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include <htm/ntypes/BasicType.hpp>
#include <htm/utils/Log.hpp>

// By calling NTA_LOG_LEVEL = LogLevel::LogLevel_Verbose
// By calling Network::setLogLevel(LogLevel_Verbose)
// you can enable the NTA_DEBUG macros below.

namespace htm {
Expand Down Expand Up @@ -187,7 +187,7 @@ void Link::compute() {
const Array &src = propagationDelay_ ? propagationDelayBuffer_.front() : src_->getData();
Array &dest = dest_->getData();

NTA_DEBUG << "Link::compute: " << getMoniker() << "; copying to dest input"
NTA_DEBUG << "compute Link: copying " << getMoniker()
<< "; delay=" << propagationDelay_ << "; size=" << src.getCount()
<< " type=" << BasicType::getName(src.getType())
<< " --> " << BasicType::getName(dest.getType()) << std::endl;
Expand Down
7 changes: 5 additions & 2 deletions src/htm/engine/Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,22 @@ namespace htm {

class RegisteredRegionImpl;

thread_local LogLevel NTA_LOG_LEVEL;


Network::Network() {
commonInit();
}

// move constructor
Network::Network(Network && n) {
Network::Network(Network &&n) noexcept {
regions_ = std::move(n.regions_);
minEnabledPhase_ = n.minEnabledPhase_;
maxEnabledPhase_ = n.maxEnabledPhase_;
phaseInfo_ = std::move(n.phaseInfo_);
callbacks_ = n.callbacks_;
iteration_ = n.iteration_;
}
}

Network::Network(const std::string& filename) {
commonInit();
Expand Down
6 changes: 4 additions & 2 deletions src/htm/engine/Network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class Link;
/**
* Cannot copy or assign a Network object. But can be moved.
*/
Network(Network&&); // move is allowed
Network(Network &&) noexcept; // move is allowed
Network(const Network&) = delete;
void operator=(const Network&) = delete;

Expand Down Expand Up @@ -378,8 +378,10 @@ class Link;
/**
* Set one of the debug levels: LogLevel_None = 0, LogLevel_Minimal, LogLevel_Normal, LogLevel_Verbose
*/
static void setLogLevel(LogLevel level) {
static LogLevel setLogLevel(LogLevel level) {
LogLevel prev = NTA_LOG_LEVEL;
NTA_LOG_LEVEL = level;
return prev;
}


Expand Down
2 changes: 1 addition & 1 deletion src/htm/engine/Output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ void Output::removeLink(const std::shared_ptr<Link>& link) {

namespace htm {
std::ostream &operator<<(std::ostream &f, const Output &d) {
f << "Output:" << d.getRegion()->getName() << "." << d.getName() << " " << d.getData();
f << "Output: " << d.getRegion()->getName() << "." << d.getName() << " " << d.getData();
return f;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/htm/regions/SPRegion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ void SPRegion::compute() {
// Call SpatialPooler compute
sp_->compute(inputBuffer.getSDR(), args_.learningMode, outputBuffer.getSDR());


// trace facility
NTA_DEBUG << "compute " << *getOutput("bottomUpOut") << "\n";

}
Expand Down
3 changes: 3 additions & 0 deletions src/htm/regions/ScalarSensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ void ScalarSensor::compute()
{
SDR &output = getOutput("encoded")->getData().getSDR();
encoder_->encode((Real64)sensedValue_, output);

// trace facility
NTA_DEBUG << "compute " << getOutput("encoded") << std::endl;
}

ScalarSensor::~ScalarSensor() {}
Expand Down
17 changes: 8 additions & 9 deletions src/htm/regions/TMRegion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ void TMRegion::compute() {
Array &externalPredictiveInputsWinners = getInput("externalPredictiveInputsWinners")->getData();
SDR& externalPredictiveInputsWinnerCells = (args_.externalPredictiveInputs) ? (externalPredictiveInputsWinners.getSDR()) : nullSDR;

// Trace facility
NTA_DEBUG << "compute " << *in << std::endl;

// Perform Bottom up compute()
Expand All @@ -231,34 +232,32 @@ void TMRegion::compute() {
//
std::shared_ptr<Output> out;
out = getOutput("bottomUpOut");
//set
NTA_LOG_LEVEL = htm::LogLevel::LogLevel_Verbose;
NTA_CHECK(NTA_LOG_LEVEL == LogLevel::LogLevel_Verbose) << "setting Verbose failed, man";
//to output the NTA_DEBUG statements below
//call Network::setLogLevel(LogLevel::LogLevel_Verbose);
// to output the NTA_DEBUG statements below
SDR& sdr = out->getData().getSDR();
tm_->getActiveCells(sdr); //active cells
if (args_.orColumnOutputs) { //output as columns
sdr = tm_->cellsToColumns(sdr);
}
NTA_DEBUG << "bottomUpOut " << *out << std::endl;
NTA_DEBUG << "compute "<< *out << std::endl;

out = getOutput("activeCells");
tm_->getActiveCells(out->getData().getSDR());
NTA_DEBUG << "active " << *out << std::endl;
NTA_DEBUG << "compute "<< *out << std::endl;

out = getOutput("predictedActiveCells");
tm_->activateDendrites();
tm_->getWinnerCells(out->getData().getSDR());
NTA_DEBUG << "winners " << *out << std::endl;
NTA_DEBUG << "compute "<< *out << std::endl;

out = getOutput("anomaly");
Real32* buffer = reinterpret_cast<Real32*>(out->getData().getBuffer());
buffer[0] = tm_->anomaly; //only the first field is valid
NTA_DEBUG << "anomaly " << *out << std::endl;
NTA_DEBUG << "compute "<< *out << std::endl;

out = getOutput("predictiveCells");
out->getData().getSDR() = tm_->getPredictiveCells();
NTA_DEBUG << "predictive " << *out << std::endl;
NTA_DEBUG << "compute " << *out << std::endl;
}


Expand Down
2 changes: 1 addition & 1 deletion src/htm/regions/TMRegion.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ class TMRegion : public RegionImpl, Serializable {
Size iter;
} args_;

bool isConnected_(std::string name) const;


computeCallbackFunc computeCallback_;
std::unique_ptr<TemporalMemory> tm_;
Expand Down
8 changes: 8 additions & 0 deletions src/htm/regions/TestNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ void TestNode::compute() {
Array &inputArray = bottomUpIn_->getData();
Real64* inputBuffer = (Real64*)inputArray.getBuffer();
size_t count = inputArray.getCount();

// trace facility
NTA_DEBUG << "compute " << bottomUpIn_ << std::endl;


// See TestNode.hpp for description of the computation

Expand All @@ -198,6 +202,10 @@ void TestNode::compute() {
}
}

// trace facility
NTA_DEBUG << "compute " << bottomUpOut_ << "\n";


iter_++;
}

Expand Down
3 changes: 2 additions & 1 deletion src/htm/regions/VectorFileEffector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ void VectorFileEffector::initialize() {
}

void VectorFileEffector::compute() {
NTA_DEBUG << "VectorFileEffector compute() input: " << *region_->getInput("dataIn") << "\n";
// trace facility
NTA_DEBUG << "compute " << *region_->getInput("dataIn") << "\n";
dataIn_ = region_->getInput("dataIn")->getData();
// It's not necessarily an error to have no inputs. In this case we just
// return
Expand Down
12 changes: 9 additions & 3 deletions src/htm/regions/VectorFileSensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,19 +127,25 @@ void VectorFileSensor::compute() {
Real *categoryOut = reinterpret_cast<Real *>(categoryOut_.getBuffer());
vectorFile_.getRawVector((htm::UInt)curVector_, categoryOut, offset, 1);
offset++;
NTA_DEBUG << "VectorFileSensor compute() CategoryOut= " << *region_->getOutput("categoryOut") << "\n";

// trace facility
NTA_DEBUG << "compute " << region_->getOutput("categoryOut") << std::endl;
}

if (hasResetOut_) {
resetOut_ = region_->getOutput("resetOut")->getData();
Real *resetOut = reinterpret_cast<Real *>(resetOut_.getBuffer());
vectorFile_.getRawVector((htm::UInt)curVector_, resetOut, offset, 1);
offset++;
NTA_DEBUG << "VectorFileSensor compute() reset= " << *region_->getOutput("reset") << "\n";

// trace facility
NTA_DEBUG << "compute " << *region_->getOutput("reset") << std::endl;
}

vectorFile_.getScaledVector((htm::UInt)curVector_, out, offset, count);
NTA_DEBUG << "VectorFileSensor compute() dataOut= " << *region_->getOutput("dataOut") << "\n";

// trace facility
NTA_DEBUG << "compute " << *region_->getOutput("dataOut") << std::endl;
iterations_++;
}

Expand Down
16 changes: 10 additions & 6 deletions src/htm/utils/Log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,36 @@

namespace htm {
enum class LogLevel { LogLevel_None = 0, LogLevel_Minimal=1, LogLevel_Normal=2, LogLevel_Verbose=3 };
static LogLevel NTA_LOG_LEVEL = LogLevel::LogLevel_Minimal; // change this in your class to set log level
// change this in your class to set log level using Network.setLogLevel(level);
extern thread_local LogLevel NTA_LOG_LEVEL;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess non NetworkAPI classes can also use the static Network.setLogLevel(), but writing directly to this variable should work too, right?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but writing directly to this variable should work too, right?

yes. you can just assign to it. NTA_LOG_LEVEL = LogLevel::LogLevel_Verbose;

We could use some other class to put the setLogLevel( ) function on that is accessible by the algorithms but I did not find anything general enough. Anyway, the python binding interface has it on the Network class.


//this code intentionally uses "if() dosomething" instead of "if() { dosomething }"
// as the macro expects another "<< "my clever message";
// so it eventually becomes: `if() std::cout << "DEBUG:\t" << "users message";`
//
//Expected usage:
//<your class>:
//NTA_LOG_LEVEL = LogLevel::LogLevel_Normal;
//Network::setLogLevel(LogLevel::LogLevel_Verbose);
//NTA_WARN << "Hello World!" << std::endl; //shows
//NTA_DEBUG << "more details how cool this is"; //not showing under "Normal" log level
//NTA_ERR << "You'll always see this, HAHA!";
//NTA_THROW << "crashing for a good cause";

#define NTA_DEBUG \
if (NTA_LOG_LEVEL >= LogLevel::LogLevel_Verbose ) std::cout << "DEBUG:\t" << __FILE__ << ":" << __LINE__ << ":"
#define NTA_DEBUG \
if (NTA_LOG_LEVEL >= LogLevel::LogLevel_Verbose) \
std::cout << "DEBUG:\t" << Path::getBasename(__FILE__) << ":" << __LINE__ << ": "

// For informational messages that report status but do not indicate that
// anything is wrong
#define NTA_INFO \
if (NTA_LOG_LEVEL >= LogLevel::LogLevel_Normal ) std::cout << "INFO:\t" << __FILE__ << ":" << __LINE__ << ":"
if (NTA_LOG_LEVEL >= LogLevel::LogLevel_Normal) \
std::cout << "INFO:\t" << Path::getBasename(__FILE__) << ":" << __LINE__ << ": "

// For messages that indicate a recoverable error or something else that it may
// be important for the end user to know about.
#define NTA_WARN \
if (NTA_LOG_LEVEL >= LogLevel::LogLevel_Normal ) std::cout << "WARN:\t" << __FILE__ << ":" << __LINE__ << ":"
if (NTA_LOG_LEVEL >= LogLevel::LogLevel_Normal) \
std::cout << "WARN:\t" << Path::getBasename(__FILE__) << ":" << __LINE__ << ": "

// To throw an exception and make sure the exception message is logged
// appropriately
Expand Down
10 changes: 10 additions & 0 deletions src/test/unit/regions/TMRegionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,18 @@ TEST(TMRegionTest, testLinking) {
ASSERT_EQ(region3->getParameterUInt32("inputWidth"), (UInt32)dataWidth);

VERBOSE << "Execute once." << std::endl;

// turn on trace...for one iteration
LogLevel prev;
VERBOSE << "Turning on Trace =========\n";
if (verbose) { prev = net.setLogLevel(LogLevel::LogLevel_Verbose); }

net.run(1);

// turn off trace
if (verbose) { net.setLogLevel(prev); }
VERBOSE << "Turned off Trace =========\n";

VERBOSE << "Checking data after first iteration..." << std::endl;
VERBOSE << " VectorFileSensor Output" << std::endl;
Array r1OutputArray = region1->getOutputData("dataOut");
Expand Down