Skip to content

Commit

Permalink
Added a task_region_handle_gen that referes to an existing executor, …
Browse files Browse the repository at this point in the history
…so that we don't create one each time.
  • Loading branch information
viboes committed Oct 26, 2014
1 parent 69f2a1d commit f1370b1
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 15 deletions.
37 changes: 36 additions & 1 deletion example/fib_task_region.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#endif

#define BOOST_THREAD_VERSION 4
//#define BOOST_THREAD_PROVIDES_EXECUTORS
#define BOOST_THREAD_PROVIDES_EXECUTORS

#include <boost/thread/experimental/task_region.hpp>
#include <iostream>
Expand Down Expand Up @@ -40,12 +40,47 @@ int fib_task_region(int n)
return n1 + n2;
}

#if defined BOOST_THREAD_PROVIDES_EXECUTORS
template <class Ex>
int fib_task_region_gen( Ex& ex, int n)
{
using boost::experimental::parallel::task_region;
using boost::experimental::parallel::task_region_handle_gen;

if (n == 0) return 0;
if (n == 1) return 1;

int n1;
int n2;

task_region(ex, [&](task_region_handle_gen<Ex>& trh)
{
trh.run([&]
{
n1 = fib_task_region(n - 1);
});

n2 = fib_task_region(n - 2);
});

return n1 + n2;
}
#endif

int main()
{
for (int i = 0; i<10; ++i) {
std::cout << fib_task_region(i) << " ";
}
std::cout << std::endl;

#if defined BOOST_THREAD_PROVIDES_EXECUTORS
boost::basic_thread_pool tp;
for (int i = 0; i<10; ++i) {
std::cout << fib_task_region_gen(tp,i) << " ";
}
std::cout << std::endl;
#endif
return 0;
}
#else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ BOOST_THREAD_INLINE_NAMESPACE(v1)
{
return list_.end();
}
const char* what() const BOOST_NOEXCEPT
const char* what() const BOOST_NOEXCEPT_OR_NOTHROW
{
return "exception_list";
}
Expand Down
108 changes: 95 additions & 13 deletions include/boost/thread/experimental/parallel/v2/task_region.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ BOOST_THREAD_INLINE_NAMESPACE(v2)
//task_canceled_exception() BOOST_NOEXCEPT {}
//task_canceled_exception(const task_canceled_exception&) BOOST_NOEXCEPT {}
//task_canceled_exception& operator=(const task_canceled_exception&) BOOST_NOEXCEPT {}
virtual const char* what() const BOOST_NOEXCEPT
virtual const char* what() const BOOST_NOEXCEPT_OR_NOTHROW
{ return "task_canceled_exception";}
};
class task_region_handle;

template <class Executor>
class task_region_handle_gen;

namespace detail
{
Expand Down Expand Up @@ -97,7 +99,8 @@ BOOST_THREAD_INLINE_NAMESPACE(v2)
#endif
}

class task_region_handle
template <class Executor>
class task_region_handle_gen
{
private:
// Private members and friends
Expand All @@ -109,6 +112,10 @@ BOOST_THREAD_INLINE_NAMESPACE(v2)
friend void task_region(F&& f);
template<typename F>
friend void task_region_final(F&& f);
template <class Ex, typename F>
friend void task_region(Ex&, F&& f);
template<class Ex, typename F>
friend void task_region_final(Ex&, F&& f);

void wait_all()
{
Expand Down Expand Up @@ -137,14 +144,41 @@ BOOST_THREAD_INLINE_NAMESPACE(v2)
//throw exs;
}
}
protected:
#if ! defined BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED && ! defined BOOST_THREAD_PROVIDES_EXECUTORS
task_region_handle_gen()
{}
#endif

task_region_handle()
#if defined BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED
#if defined BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED && defined BOOST_THREAD_PROVIDES_EXECUTORS
task_region_handle_gen()
: canceled(false)
, ex(0)
{}
task_region_handle_gen(Executor& ex)
: canceled(false)
, ex(&ex)
{}

#endif

#if ! defined BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED && defined BOOST_THREAD_PROVIDES_EXECUTORS
task_region_handle_gen()
: ex(0)
{}
task_region_handle_gen(Executor& ex)
: ex(&ex)
{}
#endif

#if defined BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED && ! defined BOOST_THREAD_PROVIDES_EXECUTORS
task_region_handle_gen()
: canceled(false)
{
}
~task_region_handle()
#endif

~task_region_handle_gen()
{
//wait_all();
}
Expand All @@ -153,17 +187,17 @@ BOOST_THREAD_INLINE_NAMESPACE(v2)
bool canceled;
#endif
#if defined BOOST_THREAD_PROVIDES_EXECUTORS
basic_thread_pool tp;
Executor* ex;
#endif
exception_list exs;
csbl::vector<future<void>> group;
mutable mutex mtx;


public:
task_region_handle(const task_region_handle&) = delete;
task_region_handle& operator=(const task_region_handle&) = delete;
task_region_handle* operator&() const = delete;
task_region_handle_gen(const task_region_handle_gen&) = delete;
task_region_handle_gen& operator=(const task_region_handle_gen&) = delete;
task_region_handle_gen* operator&() const = delete;

template<typename F>
void run(F&& f)
Expand All @@ -175,13 +209,13 @@ BOOST_THREAD_INLINE_NAMESPACE(v2)
//throw task_canceled_exception();
}
#if defined BOOST_THREAD_PROVIDES_EXECUTORS
group.push_back(async(tp, detail::wrapped<task_region_handle, F>(*this, forward<F>(f))));
group.push_back(async(*ex, detail::wrapped<task_region_handle_gen<Executor>, F>(*this, forward<F>(f))));
#else
group.push_back(async(detail::wrapped<task_region_handle, F>(*this, forward<F>(f))));
group.push_back(async(detail::wrapped<task_region_handle_gen<Executor>, F>(*this, forward<F>(f))));
#endif
#else
#if defined BOOST_THREAD_PROVIDES_EXECUTORS
group.push_back(async(tp, forward<F>(f)));
group.push_back(async(*ex, forward<F>(f)));
#else
group.push_back(async(forward<F>(f)));
#endif
Expand All @@ -200,6 +234,54 @@ BOOST_THREAD_INLINE_NAMESPACE(v2)
wait_all();
}
};
#if defined BOOST_THREAD_PROVIDES_EXECUTORS
typedef basic_thread_pool default_executor;
#else
typedef int default_executor;
#endif
class task_region_handle :
public task_region_handle_gen<default_executor>
{
default_executor tp;
template <typename F>
friend void task_region(F&& f);
template<typename F>
friend void task_region_final(F&& f);

protected:
task_region_handle() : task_region_handle_gen<default_executor>()
{
#if defined BOOST_THREAD_PROVIDES_EXECUTORS
ex = &tp;
#endif
}
task_region_handle(const task_region_handle&) = delete;
task_region_handle& operator=(const task_region_handle&) = delete;
task_region_handle* operator&() const = delete;

};

template <typename Executor, typename F>
void task_region_final(Executor& ex, F&& f)
{
task_region_handle_gen<Executor> tr(ex);
try
{
f(tr);
}
catch (...)
{
lock_guard<mutex> lk(tr.mtx);
detail::handle_task_region_exceptions(tr.exs);
}
tr.wait_all();
}

template <typename Executor, typename F>
void task_region(Executor& ex, F&& f)
{
task_region_final(ex, forward<F>(f));
}

template <typename F>
void task_region_final(F&& f)
Expand Down

0 comments on commit f1370b1

Please sign in to comment.