Skip to content

Commit

Permalink
del ThreadPool from cpp (to h)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpn-- committed Feb 3, 2017
1 parent cb11fe4 commit 0e79279
Showing 1 changed file with 63 additions and 63 deletions.
126 changes: 63 additions & 63 deletions src/etk/etk_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -302,69 +302,69 @@ namespace etk {


////////////////////////////////////////////////////////////////////////////////
#include <thread>
#include <vector>
#include <cmath>
////////////////////////////////////////////////////////////////////////////////

class ThreadPool {

public:

template<typename ResourceBucket, typename ResourceBucketInitializer, typename Index, typename Callable>
static void ParallelFor(Index start, Index end, Callable func, ResourceBucketInitializer& bucketparent ) {
// Estimate number of threads in the pool
const static unsigned nb_threads_hint = std::thread::hardware_concurrency();
const static unsigned nb_threads = (nb_threads_hint == 0u ? 8u : nb_threads_hint);

// Size of a slice for the range functions
Index n = end - start + 1;
Index slice = (Index) std::round(n / static_cast<double> (nb_threads));
slice = std::max(slice, Index(1));

// [Helper] Inner loop
auto launchRange = [&func] (int k1, int k2, ResourceBucket& bucket) {
for (Index k = k1; k < k2; k++) {
func(k, bucket);
}
};

// Create pool and launch jobs
std::vector<std::thread> pool;
std::vector<ResourceBucket> pool_resources;
pool.reserve(nb_threads);
pool_resources.reserve(nb_threads);
Index i1 = start;
Index i2 = std::min(start + slice, end);
for (unsigned i = 0; i + 1 < nb_threads && i1 < end; ++i) {
pool_resources.emplace_back(bucketparent);
pool.emplace_back(launchRange, i1, i2, std::ref(pool_resources.back()));
i1 = i2;
i2 = std::min(i2 + slice, end);
}
if (i1 < end) {
pool_resources.emplace_back(bucketparent);
pool.emplace_back(launchRange, i1, end, std::ref(pool_resources.back()));
}

// Wait for jobs to finish
for (std::thread &t : pool) {
if (t.joinable()) {
t.join();
}
}
}

// Serial version for easy comparison
template<typename Index, typename Callable>
static void SequentialFor(Index start, Index end, Callable func) {
for (Index i = start; i < end; i++) {
func(i);
}
}

};

//#include <thread>
//#include <vector>
//#include <cmath>
//////////////////////////////////////////////////////////////////////////////////
//
//class ThreadPool {
//
//public:
//
// template<typename ResourceBucket, typename ResourceBucketInitializer, typename Index, typename Callable>
// static void ParallelFor(Index start, Index end, Callable func, ResourceBucketInitializer& bucketparent ) {
// // Estimate number of threads in the pool
// const static unsigned nb_threads_hint = std::thread::hardware_concurrency();
// const static unsigned nb_threads = (nb_threads_hint == 0u ? 8u : nb_threads_hint);
//
// // Size of a slice for the range functions
// Index n = end - start + 1;
// Index slice = (Index) std::round(n / static_cast<double> (nb_threads));
// slice = std::max(slice, Index(1));
//
// // [Helper] Inner loop
// auto launchRange = [&func] (int k1, int k2, ResourceBucket& bucket) {
// for (Index k = k1; k < k2; k++) {
// func(k, bucket);
// }
// };
//
// // Create pool and launch jobs
// std::vector<std::thread> pool;
// std::vector<ResourceBucket> pool_resources;
// pool.reserve(nb_threads);
// pool_resources.reserve(nb_threads);
// Index i1 = start;
// Index i2 = std::min(start + slice, end);
// for (unsigned i = 0; i + 1 < nb_threads && i1 < end; ++i) {
// pool_resources.emplace_back(bucketparent);
// pool.emplace_back(launchRange, i1, i2, std::ref(pool_resources.back()));
// i1 = i2;
// i2 = std::min(i2 + slice, end);
// }
// if (i1 < end) {
// pool_resources.emplace_back(bucketparent);
// pool.emplace_back(launchRange, i1, end, std::ref(pool_resources.back()));
// }
//
// // Wait for jobs to finish
// for (std::thread &t : pool) {
// if (t.joinable()) {
// t.join();
// }
// }
// }
//
// // Serial version for easy comparison
// template<typename Index, typename Callable>
// static void SequentialFor(Index start, Index end, Callable func) {
// for (Index i = start; i < end; i++) {
// func(i);
// }
// }
//
//};
//


#endif // NOTHREAD
Expand Down

0 comments on commit 0e79279

Please sign in to comment.