Skip to content

Commit a49f9b3

Browse files
Merge pull request #564 from htm-community/sp_opt
Connections: microoptimizations
2 parents bb4f7a6 + 942293f commit a49f9b3

File tree

6 files changed

+33
-17
lines changed

6 files changed

+33
-17
lines changed

src/examples/hotgym/HelloSPTP.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ Real64 BenchmarkHotgym::run(UInt EPOCHS, bool useSPlocal, bool useSPglobal, bool
158158
cout << "Init:\t" << tInit.getElapsed() << endl;
159159
cout << "Random:\t" << tRng.getElapsed() << endl;
160160
cout << "Encode:\t" << tEnc.getElapsed() << endl;
161+
cout << "Inh " << spGlobal.tInh.getElapsed() << endl;
162+
cout << "Overlaps " << spGlobal.tOverlap.getElapsed() << endl;
163+
161164
if(useSPlocal) cout << "SP (l):\t" << tSPloc.getElapsed()*1.0f << endl;
162165
if(useSPglobal) cout << "SP (g):\t" << tSPglob.getElapsed() << endl;
163166
if(useTM) cout << "TM:\t" << tTM.getElapsed() << endl;

src/examples/mnist/MNIST_SP.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,9 @@ void train(const bool skipSP=false) {
155155
}
156156

157157
tTrain.stop();
158-
cout << "MNIST train time: " << tTrain.getElapsed() << endl;
158+
cout << "MNIST train time: " << tTrain.getElapsed() << endl;
159+
cout << "inh " << sp.tInh.getElapsed() << endl;
160+
cout << "over " << sp.tOverlap.getElapsed() << endl;
159161

160162
// Save the connections to file for postmortem analysis.
161163
ofstream dump("mnist_sp_learned.connections", ofstream::binary | ofstream::trunc | ofstream::out);

src/htm/algorithms/Connections.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,11 @@ void Connections::updateSynapsePermanence(Synapse synapse,
268268

269269
const bool before = synData.permanence >= connectedThreshold_;
270270
const bool after = permanence >= connectedThreshold_;
271+
272+
// update the permanence
271273
synData.permanence = permanence;
272274

273-
if( before == after ) { //no change
275+
if( before == after ) { //no change in dis/connected status
274276
return;
275277
}
276278
const auto &presyn = synData.presynapticCell;
@@ -371,17 +373,17 @@ bool Connections::compareSegments(const Segment a, const Segment b) const {
371373
}
372374
}
373375

374-
vector<Synapse>
375-
Connections::synapsesForPresynapticCell(CellIdx presynapticCell) const {
376-
vector<Synapse> all(
377-
potentialSynapsesForPresynapticCell_.at(presynapticCell).begin(),
378-
potentialSynapsesForPresynapticCell_.at(presynapticCell).end());
379-
all.insert( all.end(),
380-
connectedSynapsesForPresynapticCell_.at(presynapticCell).begin(),
381-
connectedSynapsesForPresynapticCell_.at(presynapticCell).end());
376+
377+
vector<Synapse> Connections::synapsesForPresynapticCell(const CellIdx presynapticCell) const {
378+
const auto& potential = potentialSynapsesForPresynapticCell_.at(presynapticCell);
379+
const auto& connected = connectedSynapsesForPresynapticCell_.at(presynapticCell);
380+
381+
vector<Synapse> all( potential.cbegin(), potential.cend());
382+
all.insert( all.cend(), connected.cbegin(), connected.cend());
382383
return all;
383384
}
384385

386+
385387
void Connections::reset()
386388
{
387389
if( not timeseries_ ) {

src/htm/algorithms/Connections.hpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ using SynapseIdx= UInt16; /** Index of synapse in segment. */
4242
using Segment = UInt32; /** Index of segment's data. */
4343
using Synapse = UInt32; /** Index of synapse's data. */
4444
using Permanence= Real32; //TODO experiment with half aka float16
45-
const Permanence minPermanence = 0.0f;
46-
const Permanence maxPermanence = 1.0f;
45+
constexpr const Permanence minPermanence = 0.0f;
46+
constexpr const Permanence maxPermanence = 1.0f;
4747

4848

4949
/**
@@ -651,10 +651,13 @@ class Connections : public Serializable
651651
Permanence connectedThreshold_; //TODO make const
652652

653653
// Extra bookkeeping for faster computing of segment activity.
654-
std::unordered_map<CellIdx, std::vector<Synapse>> potentialSynapsesForPresynapticCell_;
655-
std::unordered_map<CellIdx, std::vector<Synapse>> connectedSynapsesForPresynapticCell_;
656-
std::map<CellIdx, std::vector<Segment>> potentialSegmentsForPresynapticCell_;
657-
std::map<CellIdx, std::vector<Segment>> connectedSegmentsForPresynapticCell_;
654+
655+
struct identity { constexpr size_t operator()( const CellIdx t ) const noexcept { return t; }; }; //TODO in c++20 use std::identity
656+
657+
std::unordered_map<CellIdx, std::vector<Synapse>, identity> potentialSynapsesForPresynapticCell_;
658+
std::unordered_map<CellIdx, std::vector<Synapse>, identity> connectedSynapsesForPresynapticCell_;
659+
std::unordered_map<CellIdx, std::vector<Segment>, identity> potentialSegmentsForPresynapticCell_;
660+
std::unordered_map<CellIdx, std::vector<Segment>, identity> connectedSegmentsForPresynapticCell_;
658661

659662
std::vector<Segment> segmentOrdinals_;
660663
std::vector<Synapse> synapseOrdinals_;

src/htm/algorithms/SpatialPooler.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,17 +463,22 @@ void SpatialPooler::compute(const SDR &input, const bool learn, SDR &active) {
463463
input.reshape( inputDimensions_ );
464464
active.reshape( columnDimensions_ );
465465
updateBookeepingVars_(learn);
466+
467+
tOverlap.start();
466468
calculateOverlap_(input, overlaps_);
469+
tOverlap.stop();
467470

468471
boostOverlaps_(overlaps_, boostedOverlaps_);
469472

473+
tInh.start();
470474
auto &activeVector = active.getSparse();
471475
inhibitColumns_(boostedOverlaps_, activeVector);
472476
// Notify the active SDR that its internal data vector has changed. Always
473477
// call SDR's setter methods even if when modifying the SDR's own data
474478
// inplace.
475479
sort( activeVector.begin(), activeVector.end() );
476480
active.setSparse( activeVector );
481+
tInh.stop();
477482

478483
if (learn) {
479484
adaptSynapses_(input, active);

src/htm/algorithms/SpatialPooler.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#include <htm/types/Types.hpp>
3030
#include <htm/types/Serializable.hpp>
3131
#include <htm/types/Sdr.hpp>
32-
32+
#include <htm/os/Timer.hpp>
3333

3434
namespace htm {
3535

@@ -61,6 +61,7 @@ using namespace std;
6161
class SpatialPooler : public Serializable
6262
{
6363
public:
64+
Timer tInh, tOverlap;
6465

6566
const Real MAX_LOCALAREADENSITY = 0.5f; //require atleast 2 areas
6667

0 commit comments

Comments
 (0)