diff --git a/CommonCompilerConfig.cmake b/CommonCompilerConfig.cmake index 2360307875..901f410b62 100644 --- a/CommonCompilerConfig.cmake +++ b/CommonCompilerConfig.cmake @@ -100,8 +100,9 @@ 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(CMAKE_CXX_STANDARD 17) - set(boost_required OFF) + 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") set(CMAKE_CXX_STANDARD 17) set(extra_lib_for_filesystem "stdc++fs") diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6b05da3e11..af4bf2fe1a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -171,6 +171,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 diff --git a/src/htm/algorithms/SpatialPooler.cpp b/src/htm/algorithms/SpatialPooler.cpp index 9f4795d5c2..d6dd3c120d 100644 --- a/src/htm/algorithms/SpatialPooler.cpp +++ b/src/htm/algorithms/SpatialPooler.cpp @@ -27,6 +27,7 @@ #include #include #include +#include using namespace std; using namespace htm; @@ -863,19 +864,25 @@ void SpatialPooler::inhibitColumnsGlobal_(const vector &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, 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(); } diff --git a/src/htm/algorithms/SpatialPooler.hpp b/src/htm/algorithms/SpatialPooler.hpp index f1d662d69e..3a83502d6c 100644 --- a/src/htm/algorithms/SpatialPooler.hpp +++ b/src/htm/algorithms/SpatialPooler.hpp @@ -29,7 +29,7 @@ #include #include #include - +#include namespace htm { @@ -1212,6 +1212,7 @@ class SpatialPooler : public Serializable Random rng_; public: + mutable Timer tSort, tNth, tWhile; const Connections& connections = connections_; //for inspection of details in connections. Const, so users cannot break the SP internals. const Connections& getConnections() const { return connections_; } // as above, but for use in pybind11 }; diff --git a/src/htm/types/Parallelizable.hpp b/src/htm/types/Parallelizable.hpp new file mode 100644 index 0000000000..6957c85913 --- /dev/null +++ b/src/htm/types/Parallelizable.hpp @@ -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 //std::execution::par, seq, par_unseq +// #include +#include + +namespace htm { +namespace parallel { + const constexpr auto mode = std::execution::par_unseq; //TODO ifdef NUM_PARALLEL=1 -> seq +} +}