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

Parallel algorithms POC #559

Draft
wants to merge 12 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions CommonCompilerConfig.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ set(boost_required ON)
if(NOT FORCE_CPP11)
if(${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU")
if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL "9")
set(extra_lib_for_filesystem "tbb") #TODO hijacked for parallel TS, link with tbb for g++-9
set(CMAKE_CXX_STANDARD 17)
set(boost_required OFF)
elseif(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL "8")
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ set(regions_files

set(types_files
htm/types/Exception.hpp
htm/types/Parallelizable.hpp
htm/types/Types.hpp
htm/types/Serializable.hpp
htm/types/Sdr.hpp
Expand Down
9 changes: 8 additions & 1 deletion src/htm/algorithms/SpatialPooler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <htm/algorithms/SpatialPooler.hpp>
#include <htm/utils/Topology.hpp>
#include <htm/utils/VectorHelpers.hpp>
#include <htm/types/Parallelizable.hpp>

using namespace std;
using namespace htm;
Expand Down Expand Up @@ -844,19 +845,25 @@ void SpatialPooler::inhibitColumnsGlobal_(const vector<Real> &overlaps,
// faster than a regular sort because it stops after it partitions the
// elements about the Nth element, with all elements on their correct side of
// the Nth element.
std::nth_element(
tNth.start();
std::nth_element(htm::parallel::mode,
Copy link
Member Author

Choose a reason for hiding this comment

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

inhibition identified as one of the slowest methods in SP.
nth_element as the most significant in global inh.

activeColumns.begin(),
activeColumns.begin() + numDesired,
activeColumns.end(),
compare);
// Remove the columns which lost the competition.
activeColumns.resize(numDesired);
tNth.stop();
// Finish sorting the winner columns by their overlap.
tSort.start();
std::sort(activeColumns.begin(), activeColumns.end(), compare);
tSort.stop();
// Remove sub-threshold winners
tWhile.start();
while( !activeColumns.empty() &&
overlaps[activeColumns.back()] < stimulusThreshold_)
activeColumns.pop_back();
tWhile.stop();
}


Expand Down
3 changes: 2 additions & 1 deletion src/htm/algorithms/SpatialPooler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include <htm/types/Types.hpp>
#include <htm/types/Serializable.hpp>
#include <htm/types/Sdr.hpp>

#include <htm/os/Timer.hpp>

namespace htm {

Expand Down Expand Up @@ -1210,6 +1210,7 @@ class SpatialPooler : public Serializable

public:
const Connections &connections = connections_;
mutable Timer tSort, tNth, tWhile;
};

std::ostream & operator<<(std::ostream & out, const SpatialPooler &sp);
Expand Down
29 changes: 29 additions & 0 deletions src/htm/types/Parallelizable.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Parallelizable.hpp
*
* include this header to files where you want to run blocks of code in parallel.
* We use [C++17 standard Parallel TS](https://en.cppreference.com/w/cpp/experimental/parallelism).
*
* Requirements:
* - c++17
* - The Building Blocks (tbb) linked to the library
* - [supported compiler](https://en.cppreference.com/w/cpp/compiler_support#cpp17): currently GCC-9+, MSVC 2019
* //TODO: switch to c++17 by default, or implement `transform()` temporarily as a custom method?
*
* Functionality:
* - include all needed headers for given platform, compiler, ...
* - handle define `NUM_PARALLEL=n`
* - to run in single thread, set NUM_PARALLEL=1
*
*/

//includes for TS Parallel
#include <execution> //std::execution::par, seq, par_unseq
// #include <tbb/parallel_for.h>
#include <mutex>

namespace htm {
namespace parallel {
const constexpr auto mode = std::execution::par_unseq; //TODO ifdef NUM_PARALLEL=1 -> seq
}
}