Skip to content

Commit

Permalink
oops backwards
Browse files Browse the repository at this point in the history
  • Loading branch information
jpn-- committed Feb 3, 2017
1 parent 0e79279 commit ec7053d
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 120 deletions.
114 changes: 57 additions & 57 deletions src/etk/etk_thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,65 +172,65 @@ void etk::threadtester()


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

class ThreadPool {

public:

template<typename Index, typename Callable>
static void ParallelFor(Index start, Index end, Callable func) {
// 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) {
for (Index k = k1; k < k2; k++) {
func(k);
}
};

// Create pool and launch jobs
std::vector<std::thread> pool;
pool.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.emplace_back(launchRange, i1, i2);
i1 = i2;
i2 = std::min(i2 + slice, end);
}
if (i1 < end) {
pool.emplace_back(launchRange, i1, end);
}

// 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);
}
}

};
//class ThreadPool {
//
//public:
//
// template<typename Index, typename Callable>
// static void ParallelFor(Index start, Index end, Callable func) {
// // 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) {
// for (Index k = k1; k < k2; k++) {
// func(k);
// }
// };
//
// // Create pool and launch jobs
// std::vector<std::thread> pool;
// pool.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.emplace_back(launchRange, i1, i2);
// i1 = i2;
// i2 = std::min(i2 + slice, end);
// }
// if (i1 < end) {
// pool.emplace_back(launchRange, i1, end);
// }
//
// // 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 <iostream>
//#include <mutex>
Expand Down
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 ec7053d

Please sign in to comment.