Skip to content
Snippets Groups Projects
Commit 4376306e authored by Benjamin Cumming's avatar Benjamin Cumming
Browse files

Support for multi-threaded sort

* `nest::mc::threading::sort` is analogous to `std::sort`
    * when using TBB the `tbb::parallel_sort` is used
    * otherwise `std::sort` is used
* used to parallelise connection construction during setup
    * now all initialization and simulation is multi-threaded
* added a unit test
parent 99119e5f
No related branches found
No related tags found
No related merge requests found
...@@ -69,7 +69,7 @@ public: ...@@ -69,7 +69,7 @@ public:
/// must be called after all connections have been added /// must be called after all connections have been added
void construct() { void construct() {
if (!std::is_sorted(connections_.begin(), connections_.end())) { if (!std::is_sorted(connections_.begin(), connections_.end())) {
std::sort(connections_.begin(), connections_.end()); threading::sort(connections_);
} }
} }
......
...@@ -62,10 +62,24 @@ struct parallel_for { ...@@ -62,10 +62,24 @@ struct parallel_for {
} }
}; };
template <typename RandomIt>
void sort(RandomIt begin, RandomIt end) {
std::sort(begin, end);
}
template <typename RandomIt, typename Compare>
void sort(RandomIt begin, RandomIt end, Compare comp) {
std::sort(begin, end, comp);
}
template <typename Container>
void sort(Container& c) {
std::sort(c.begin(), c.end());
}
template <typename T> template <typename T>
using parallel_vector = std::vector<T>; using parallel_vector = std::vector<T>;
inline std::string description() { inline std::string description() {
return "serial"; return "serial";
} }
......
...@@ -51,6 +51,21 @@ using parallel_vector = tbb::concurrent_vector<T>; ...@@ -51,6 +51,21 @@ using parallel_vector = tbb::concurrent_vector<T>;
using task_group = tbb::task_group; using task_group = tbb::task_group;
template <typename RandomIt>
void sort(RandomIt begin, RandomIt end) {
tbb::parallel_sort(begin, end);
}
template <typename RandomIt, typename Compare>
void sort(RandomIt begin, RandomIt end, Compare comp) {
tbb::parallel_sort(begin, end, comp);
}
template <typename Container>
void sort(Container& c) {
tbb::parallel_sort(c.begin(), c.end());
}
} // threading } // threading
} // mc } // mc
} // nest } // nest
......
...@@ -6,6 +6,28 @@ ...@@ -6,6 +6,28 @@
#include "../test_util.hpp" #include "../test_util.hpp"
#include "util/debug.hpp" #include "util/debug.hpp"
/// tests the sort implementation in threading
/// is only parallel if TBB is being used
TEST(algorithms, parallel_sort)
{
auto n = 10000;
std::vector<int> v(n);
std::iota(v.begin(), v.end(), 1);
std::random_device rd;
std::shuffle(v.begin(), v.end(), std::mt19937(rd()));
// assert that the original vector has in fact been permuted
EXPECT_FALSE(std::is_sorted(v.begin(), v.end()));
nest::mc::threading::sort(v);
EXPECT_TRUE(std::is_sorted(v.begin(), v.end()));
for(auto i=0; i<n; ++i) {
EXPECT_EQ(i+1, v[i]);
}
}
TEST(algorithms, sum) TEST(algorithms, sum)
{ {
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment