|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r51523 - in sandbox/interthreads/libs/interthreads: example test
From: vicente.botet_at_[hidden]
Date: 2009-03-01 18:16:43
Author: viboes
Date: 2009-03-01 18:16:43 EST (Sun, 01 Mar 2009)
New Revision: 51523
URL: http://svn.boost.org/trac/boost/changeset/51523
Log:
0.4.1 : Adaptation to the Boost.ThreadPoold Version 0.21 + Scoped forking + Parallel sort
Text files modified:
sandbox/interthreads/libs/interthreads/example/parallel_sort.cpp | 162 ++++++++++++++++++++++++---------------
sandbox/interthreads/libs/interthreads/example/parallel_sort2.cpp | 116 ++++++++++++++++++++-------
sandbox/interthreads/libs/interthreads/test/test_ae.hpp | 4
sandbox/interthreads/libs/interthreads/test/test_thread_pool.cpp | 12 +-
4 files changed, 194 insertions(+), 100 deletions(-)
Modified: sandbox/interthreads/libs/interthreads/example/parallel_sort.cpp
==============================================================================
--- sandbox/interthreads/libs/interthreads/example/parallel_sort.cpp (original)
+++ sandbox/interthreads/libs/interthreads/example/parallel_sort.cpp 2009-03-01 18:16:43 EST (Sun, 01 Mar 2009)
@@ -29,23 +29,37 @@
#include <assert.h>
+#define BOOST_PARTS 2
+#define NN 400000
+
+class scoped_timer {
+ boost::posix_time::ptime start_;
+public:
+ scoped_timer()
+ : start_(boost::posix_time::microsec_clock::universal_time())
+ {}
+ ~scoped_timer() {
+ boost::posix_time::ptime stop( boost::posix_time::microsec_clock::universal_time() );
+ std::cout << " " << ( stop - start_).total_milliseconds() << " milli seconds" << std::endl;
+ }
+};
-template <typename Range, std::size_t Parts>
+template <typename Range>
class partition
{
public:
- boost::array<boost::sub_range<Range>,Parts> parts_;
- partition(boost::sub_range<Range>& range)
- {
- std::size_t size = boost::size(range);
- parts_[0]=boost::sub_range<Range>(boost::begin(range), boost::begin(range)+(size/Parts));
- for (std::size_t i=1; i< Parts-1; ++i) {
- parts_[i]=boost::sub_range<Range>(boost::begin(range)+i*(size/Parts), boost::begin(range)+(i+1)*(size/Parts));
- }
- parts_[Parts-1]=boost::sub_range<Range>(boost::begin(range)+(Parts-1)*(size/Parts), boost::end(range));
- }
- boost::sub_range<Range> operator[](unsigned i) {
- return parts_[i];
+ boost::iterator_range<typename boost::range_iterator<Range>::type> range_;
+ std::size_t parts_;
+ partition(boost::iterator_range<typename boost::range_iterator<Range>::type>& range, std::size_t parts):
+ range_(range),
+ parts_(parts)
+ {}
+ boost::iterator_range<typename boost::range_iterator<Range>::type> operator[](unsigned i) {
+ unsigned size = boost::size(range_);
+ if (i<(parts_-1))
+ return boost::make_sliced_range(range_, i*(size/parts_), ((i+1)*(size/parts_)));
+ else
+ return boost::make_sliced_range(range_, (parts_-1)*(size/parts_), size);
}
};
@@ -53,12 +67,7 @@
boost::tp::unbounded_channel< boost::tp::fifo >
> pool_type;
-#ifdef TASK_POOL
- typedef boost::tp::task< pool_type, void > task_type;
-#else
- typedef boost::tp::task< void > task_type;
-#endif
-
+typedef boost::tp::task< void > task_type;
template <
typename DirectSolver,
@@ -77,11 +86,9 @@
boost::sub_range<Range> range,
unsigned cutoff) {
unsigned size = boost::size(range);
- //std::cout << "<<par_ " << size << std::endl;
if ( size <= cutoff) DirectSolver()(range);
else {
-#define BOOST_PARTS 2
- partition<Range, BOOST_PARTS> parts(range);
+ partition<Range> parts(range, BOOST_PARTS);
task_type tasks[BOOST_PARTS];
for (unsigned i=0;i < BOOST_PARTS-1; ++i) {
task_type tmp(ae.submit(
@@ -99,9 +106,7 @@
tasks[i].wait();
};
- //std::cout << "par_inplace_merge_fct " << size << ">>"<< std::endl;
Composer()(range);
- //std::cout << "par_ " << size << ">>"<< std::endl;
}
}
@@ -123,12 +128,11 @@
template <typename Range>
void parallel_sort(Range& range, unsigned cutoff=10000) {
pool_type pool( boost::tp::poolsize( 2) );
+// pool_type& pool(boost::this_task::get_thread_pool<pool_type>() );
boost::sub_range<Range> rng(boost::begin(range), boost::end(range));
inplace_solve<sort_fct,inplace_merge_fct,pool_type,Range>( pool, rng, cutoff);
- //std::cout << "parallel_sort " << ">>"<< std::endl;
}
-#define NN 100000
int sorted[NN];
int values1[NN];
int values2[NN];
@@ -138,70 +142,106 @@
int values6[NN];
int main() {
- //pool_type ae(boost::tp::poolsize(2));
for (unsigned i=0; i<NN; ++i) sorted[i]=i;
for (unsigned i=0; i<NN; ++i) values1[i]=NN-i-1;
{
- std::cout << "std::sort: reverse 0.." << NN << std::endl;
- boost::progress_timer t; // start timing
+ std::cout << "std::sort: reverse 0.." << NN;
+ scoped_timer t; // start timing
std::sort(boost::begin(values1), boost::end(values1));
}
assert(boost::equal(values1, sorted));
+ {
+ std::cout << "std::sort: 0.." << NN;
+ scoped_timer t; // start timing
+ std::sort(boost::begin(values1), boost::end(values1));
+ }
for (unsigned i=0; i<NN; ++i) values2[i]=NN-i-1;
{
- std::cout << "boost::sort: reverse 0.."<<NN << std::endl;
- boost::progress_timer t; // start timing
+ std::cout << "boost::sort: reverse 0.."<<NN;
+ scoped_timer t; // start timing
boost::sort(values2);
}
assert(boost::equal(values2, sorted));
+ {
+ std::cout << "boost::sort: 0.."<<NN;
+ scoped_timer t; // start timing
+ boost::sort(values2);
+ }
// creates a threadpool with two worker-threads
- pool_type pool( boost::tp::poolsize( 2) );
-
+ //pool_type pool( boost::tp::poolsize( 2) );
- for (unsigned i=0; i<NN; ++i) values3[i]=NN-i-1;
- std::cout << "parallel_sort "<<NN/16<<": reverse 0.."<<NN << std::endl;
+
+ for (unsigned i=0; i<NN; ++i) values6[i]=NN-i-1;
{
- boost::progress_timer tmr; // start timing
- parallel_sort(values3, NN/16);
- }
+ std::cout << "parallel_sort "<<NN/2<<": reverse 0.."<<NN;
+ scoped_timer tmr; // start timing
+ parallel_sort(values6, NN/2);
+ }
+ assert(boost::equal(values6, sorted));
+ {
+ std::cout << "parallel_sort "<<NN/2<<": 0.."<<NN;
+ scoped_timer tmr; // start timing
+ parallel_sort(values6, NN/2);
+ }
- //for (unsigned i=0; i<NN; ++i) std::cout << sorted[i] << " " <<values3[i] << std::endl;
- assert(boost::equal(values3, sorted));
-
- for (unsigned i=0; i<NN; ++i) values4[i]=NN-i-1;
+ for (unsigned i=0; i<NN; ++i) values6[i]=NN-i-1;
{
- std::cout << "std::sort: reverse 0.." << NN << std::endl;
- boost::progress_timer t; // start timing
- std::sort(boost::begin(values4), boost::end(values4));
- }
- assert(boost::equal(values4, sorted));
+ std::cout << "parallel_sort "<<NN/4<<": reverse 0.."<<NN;
+ scoped_timer tmr; // start timing
+ parallel_sort(values6, NN/4);
+ }
+ assert(boost::equal(values6, sorted));
+ {
+ std::cout << "parallel_sort "<<NN/4<<": 0.."<<NN;
+ scoped_timer tmr; // start timing
+ parallel_sort(values6, NN/4);
+ }
+
+ for (unsigned i=0; i<NN; ++i) values6[i]=NN-i-1;
+ {
+ std::cout << "parallel_sort "<<NN/8<<": reverse 0.."<<NN;
+ scoped_timer tmr; // start timing
+ parallel_sort(values6, NN/8);
+ }
+ assert(boost::equal(values6, sorted));
+ {
+ std::cout << "parallel_sort "<<NN/8<<": 0.."<<NN;
+ scoped_timer tmr; // start timing
+ parallel_sort(values6, NN/8);
+ }
for (unsigned i=0; i<NN; ++i) values5[i]=NN-i-1;
- std::cout << "parallel_sort "<<NN/16<<": reverse 0.."<<NN << std::endl;
{
- boost::progress_timer tmr; // start timing
+ std::cout << "parallel_sort "<<NN/16<<": reverse 0.."<<NN;
+ scoped_timer tmr; // start timing
parallel_sort(values5, NN/16);
- }
-
- //for (unsigned i=0; i<NN; ++i) std::cout << sorted[i] << " " <<values3[i] << std::endl;
+ }
assert(boost::equal(values5, sorted));
+ {
+ std::cout << "parallel_sort "<<NN/16<<": 0.."<<NN;
+ scoped_timer tmr; // start timing
+ parallel_sort(values5, NN/16);
+ }
for (unsigned i=0; i<NN; ++i) values6[i]=NN-i-1;
- std::cout << "parallel_sort "<<NN/16<<": reverse 0.."<<NN << std::endl;
{
- boost::progress_timer tmr; // start timing
- parallel_sort(values6, NN/16);
- }
-
- //for (unsigned i=0; i<NN; ++i) std::cout << sorted[i] << " " <<values3[i] << std::endl;
+ std::cout << "parallel_sort "<<NN/32<<": reverse 0.."<<NN;
+ scoped_timer tmr; // start timing
+ parallel_sort(values6, NN/32);
+ }
assert(boost::equal(values6, sorted));
+ {
+ std::cout << "parallel_sort "<<NN/32<<": 0.."<<NN;
+ scoped_timer tmr; // start timing
+ parallel_sort(values6, NN/32);
+ }
- //boost::this_thread::sleep(boost::posix_time::milliseconds(5000));
- std::cout << "shutdown"<< std::endl;
- pool.shutdown();
+
+ //std::cout << "shutdown"<< std::endl;
+ //pool.shutdown();
std::cout << "end"<< std::endl;
return 0;
}
Modified: sandbox/interthreads/libs/interthreads/example/parallel_sort2.cpp
==============================================================================
--- sandbox/interthreads/libs/interthreads/example/parallel_sort2.cpp (original)
+++ sandbox/interthreads/libs/interthreads/example/parallel_sort2.cpp 2009-03-01 18:16:43 EST (Sun, 01 Mar 2009)
@@ -35,6 +35,19 @@
#include <assert.h>
#define BOOST_PARTS 2
+#define NN 400000
+
+class scoped_timer {
+ boost::posix_time::ptime start_;
+public:
+ scoped_timer()
+ : start_(boost::posix_time::microsec_clock::universal_time())
+ {}
+ ~scoped_timer() {
+ boost::posix_time::ptime stop( boost::posix_time::microsec_clock::universal_time() );
+ std::cout << " " << ( stop - start_).total_milliseconds() << " milli seconds" << std::endl;
+ }
+};
template <typename Range>
class partition
@@ -87,14 +100,14 @@
unsigned cutoff )
{
unsigned size = boost::size(range);
- //std::cout << "<<par_ " << size << std::endl;
+ //std::cout << "<<par_ " << size;
if ( size <= cutoff) DirectSolver()(range);
else {
partition<Range> parts(range, BOOST_PARTS);
std::list<task_type> tasks;
- #if 1 // this code do not compiles with gcc 3.4.4 cygwin
+ #if 0 // this code do not compiles with gcc 3.4.4 cygwin
boost::transform(parts, boost::begin(tasks),
- boost::bind(AE::submit, boost::ref(ae),
+ boost::bind(&AE::submit, boost::ref(ae),
//boost::bind(&boost::interthreads::fork<AE>, boost::ref(ae),
boost::bind(&inplace_solve<DirectSolver,Composer,AE,Range>, boost::ref(ae),_1,cutoff)));
#else
@@ -137,10 +150,8 @@
void parallel_sort(AE& ae, Range& range, unsigned cutoff=10000) {
boost::iterator_range<typename boost::range_iterator<Range>::type> rng(range);
inplace_solve<sort_fct,inplace_merge_fct,pool_type,Range>( ae, rng, cutoff);
- //std::cout << "parallel_sort " << ">>"<< std::endl;
}
-#define NN 100000
int sorted[NN];
int values1[NN];
int values2[NN];
@@ -154,58 +165,99 @@
for (unsigned i=0; i<NN; ++i) values1[i]=NN-i-1;
{
- std::cout << "std::sort: reverse 0.." << NN << std::endl;
- boost::progress_timer t; // start timing
+ std::cout << "std::sort: reverse 0.." << NN;
+ scoped_timer t; // start timing
std::sort(boost::begin(values1), boost::end(values1));
}
assert(boost::equal(values1, sorted));
+ {
+ std::cout << "std::sort: 0.." << NN;
+ scoped_timer t; // start timing
+ std::sort(boost::begin(values1), boost::end(values1));
+ }
for (unsigned i=0; i<NN; ++i) values2[i]=NN-i-1;
{
- std::cout << "boost::sort: reverse 0.."<<NN << std::endl;
- boost::progress_timer t; // start timing
+ std::cout << "boost::sort: reverse 0.."<<NN;
+ scoped_timer t; // start timing
boost::sort(values2);
}
assert(boost::equal(values2, sorted));
+ {
+ std::cout << "boost::sort: 0.."<<NN;
+ scoped_timer t; // start timing
+ boost::sort(values2);
+ }
// creates a threadpool with two worker-threads
pool_type pool( boost::tp::poolsize( 2) );
+ for (unsigned i=0; i<NN; ++i) values5[i]=NN-i-1;
+ {
+ std::cout << "parallel_sort "<<NN/2<<": reverse 0.."<<NN;
+ scoped_timer tmr; // start timing
+ parallel_sort(pool, values5, NN/2);
+ }
+ assert(boost::equal(values5, sorted));
+ {
+ std::cout << "parallel_sort "<<NN/2<<": 0.."<<NN;
+ scoped_timer tmr; // start timing
+ parallel_sort(pool, values5, NN/2);
+ }
- for (unsigned i=0; i<NN; ++i) values3[i]=NN-i-1;
- std::cout << "parallel_sort "<<NN/16<<": reverse 0.."<<NN << std::endl;
+ for (unsigned i=0; i<NN; ++i) values5[i]=NN-i-1;
+ {
+ std::cout << "parallel_sort "<<NN/4<<": reverse 0.."<<NN;
+ scoped_timer tmr; // start timing
+ parallel_sort(pool, values5, NN/4);
+ }
+ assert(boost::equal(values5, sorted));
{
- boost::progress_timer tmr; // start timing
- parallel_sort(pool, values3, NN/16);
+ std::cout << "parallel_sort "<<NN/4<<": 0.."<<NN;
+ scoped_timer tmr; // start timing
+ parallel_sort(pool, values5, NN/4);
}
- assert(boost::equal(values3, sorted));
- for (unsigned i=0; i<NN; ++i) values4[i]=NN-i-1;
+ for (unsigned i=0; i<NN; ++i) values5[i]=NN-i-1;
{
- std::cout << "std::sort: reverse 0.." << NN << std::endl;
- boost::progress_timer t; // start timing
- std::sort(boost::begin(values4), boost::end(values4));
- }
- assert(boost::equal(values4, sorted));
-
+ std::cout << "parallel_sort "<<NN/8<<": reverse 0.."<<NN;
+ scoped_timer tmr; // start timing
+ parallel_sort(pool, values5, NN/8);
+ }
+ assert(boost::equal(values5, sorted));
+ {
+ std::cout << "parallel_sort "<<NN/8<<": 0.."<<NN;
+ scoped_timer tmr; // start timing
+ parallel_sort(pool, values5, NN/8);
+ }
+
for (unsigned i=0; i<NN; ++i) values5[i]=NN-i-1;
- std::cout << "parallel_sort "<<NN/16<<": reverse 0.."<<NN << std::endl;
{
- boost::progress_timer tmr; // start timing
+ std::cout << "parallel_sort "<<NN/16<<": reverse 0.."<<NN;
+ scoped_timer tmr; // start timing
parallel_sort(pool, values5, NN/16);
}
- //for (unsigned i=0; i<NN; ++i) std::cout << sorted[i] << " " <<values3[i] << std::endl;
assert(boost::equal(values5, sorted));
+ {
+ std::cout << "parallel_sort "<<NN/16<<": 0.."<<NN;
+ scoped_timer tmr; // start timing
+ parallel_sort(pool, values5, NN/16);
+ }
-#if 0
- for (unsigned i=0; i<NN; ++i) values6[i]=NN-i-1;
- std::cout << "parallel_sort "<<NN/16<<": reverse 0.."<<NN << std::endl;
+ for (unsigned i=0; i<NN; ++i) values5[i]=NN-i-1;
{
- boost::progress_timer tmr; // start timing
- parallel_sort(pool, values6, NN/16);
- }
- assert(boost::equal(values6, sorted));
-#endif
+ std::cout << "parallel_sort "<<NN/32<<": reverse 0.."<<NN;
+ scoped_timer tmr; // start timing
+ parallel_sort(pool, values5, NN/32);
+ }
+ assert(boost::equal(values5, sorted));
+ {
+ std::cout << "parallel_sort "<<NN/32<<": 0.."<<NN;
+ scoped_timer tmr; // start timing
+ parallel_sort(pool, values5, NN/32);
+ }
+
+
std::cout << "shutdown"<< std::endl;
pool.shutdown();
std::cout << "end"<< std::endl;
Modified: sandbox/interthreads/libs/interthreads/test/test_ae.hpp
==============================================================================
--- sandbox/interthreads/libs/interthreads/test/test_ae.hpp (original)
+++ sandbox/interthreads/libs/interthreads/test/test_ae.hpp 2009-03-01 18:16:43 EST (Sun, 01 Mar 2009)
@@ -196,7 +196,7 @@
template <typename AE>
void do_test_fork_1(AE& ae) {
test_value=0;
- BOOST_AUTO(act, bith::fork(ae, simple_thread_1, 2));
+ BOOST_AUTO(act, bith::fork<AE>(ae, simple_thread_1, 2));
int res_value = bith::get(act);
BOOST_CHECK_EQUAL(test_value, 2);
BOOST_CHECK_EQUAL(res_value, 2);
@@ -500,7 +500,7 @@
boost::mutex::scoped_lock lk(m);
BOOST_AUTO(act,bith::fork(ae, interruption_point_thread, &m,&failed));
act.interrupt();
- BOOST_CHECK_EQUAL(act.interruption_requested(), true);
+ BOOST_CHECK_EQUAL(bith::interruption_requested(act), true);
lk.unlock();
bith::wait(act);
BOOST_CHECK(!failed);
Modified: sandbox/interthreads/libs/interthreads/test/test_thread_pool.cpp
==============================================================================
--- sandbox/interthreads/libs/interthreads/test/test_thread_pool.cpp (original)
+++ sandbox/interthreads/libs/interthreads/test/test_thread_pool.cpp 2009-03-01 18:16:43 EST (Sun, 01 Mar 2009)
@@ -37,6 +37,7 @@
> pool_type;
#endif
+#if 0
void do_test_member_fork_detach() {
pool_type ae(boost::tp::poolsize(2));
aetst::do_test_member_fork_detach(ae);
@@ -51,12 +52,13 @@
pool_type ae(boost::tp::poolsize(2));
aetst::do_test_member_fork_bind(ae);
}
+#endif
void do_test_fork() {
pool_type ae(boost::tp::poolsize(2));
aetst::do_test_fork(ae);
}
-void do_test_fork_1() {
+void do_test_fork_1() {
pool_type ae(boost::tp::poolsize(2));
aetst::do_test_fork_1(ae);
}
@@ -162,7 +164,7 @@
pool_type ae(boost::tp::poolsize(2));
aetst::do_test_wait_for_all(ae);
}
-#if 0
+#if 1
void do_test_fork_after_wait() {
pool_type ae(boost::tp::poolsize(2));
aetst::do_test_fork_after_wait(ae);
@@ -177,11 +179,11 @@
{
test_suite* test = BOOST_TEST_SUITE("scheduler");
-#if 0 // DO NOT WORK YET
+#if 0 // DO NOT WORK UNTIL tp::task has a fork member
test->add(BOOST_TEST_CASE(&do_test_member_fork_detach));
-#endif
test->add(BOOST_TEST_CASE(&do_test_member_fork));
test->add(BOOST_TEST_CASE(&do_test_member_fork_bind));
+#endif
test->add(BOOST_TEST_CASE(&do_test_fork));
test->add(BOOST_TEST_CASE(&do_test_fork_1));
test->add(BOOST_TEST_CASE(&do_test_thread_interrupts_at_interruption_point));
@@ -209,7 +211,7 @@
test->add(BOOST_TEST_CASE(&do_test_wait_for_all));
-#if 0 // DO NOT WORK YET
+#if 1 // DO NOT WORK YET
test->add(BOOST_TEST_CASE(&do_test_wait_for_any));
test->add(BOOST_TEST_CASE(&do_test_fork_after_wait));
test->add(BOOST_TEST_CASE(&do_test_fork_after_get));
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk