Re: [boost] [Boost.Function] How about parallel functions?

Hi,
I required to call few functions is separate threads and use the results. However, the code for this looks really ugly, you have to create threads, then join the threads when ever you require the result of the >functions.
So I have created a boost.parallel_function library that does this seamlessly just like invoking a normal function and it only joins the thread when the value of the function result type is required. Is this library of any interest to any one?
Here is the a hello world example:
#include <cstdlib> #include <limits> #include <boost/parallel_function.hpp> using namespace boost;
int F0() { volatile int x = 0; for(int i=0; i<std::numeric_limits<int>::max()/1024; i++) x ^= std::rand(); return (int)x; }
int F2(int y) { volatile int x = 0; for(int i=0; i<std::numeric_limits<int>::max()/512; i++) x ^= std::rand(); return (int)x ^ y; }
int main() { typedef boost::parallel_function< void ( ) > f0_type; typedef boost::parallel_function< void (int) > f1_type;
f0_type f0( &::F0 ); f1_type f1( &::F1 );
f0_type::result_type r0 = f0(); f1_type::result_type r1 = f1(32);
// use r0 and r1 as if they are int r0 += 34; int value = r1 *= r0 + 452;
std::cout << "Hello world " << value << std::endl;
return 0; }
The Boost.Task library should provides the features you need.
int main() {
boost::task::handle<int> f0 = boost::task:async(boost:task(F0),boost::task::new_thread()); boost::task::handle<int> f1 = boost::task:async(boost:task(F1,32),boost::task::new_thread()); int r0=f0.get(); int r1=f1.get();
r0 += 34; int value = r1 *= r0 + 452;
std::cout << "Hello world " << value << std::endl;
return 0; }
This library is under the review schedule and will be reviewed soon. I'm >interested on the details of your implementation to compare with >Boost.Task. It will be also useful if you can take a look to Boost.task and >try so see it provides every thing you need.
Best, Vicente
I have taken a partial look at Boost.task, however, what I don't like about the syntax is the following:
int r0=f0.get(); int r1=f1.get();
Current implementation (hack) would make 'f0' and 'f1' the same type 'int' (in this example). So the .get() part could be omitted altogether; which seems a lot like thread.join() call! Which is accomplished by a simple implicit conversion operator to the reference of the type. A little out of topic discussion, why is there an atomic library within task? because boost.thread has one (I think). Atomic library should be written so it becomes more coherent and reduces the code size. With Best Regards Mr. Kasra Nassiri Department of Mathematics & Computer Science Imperial College London

Hi, ----- Original Message ----- From: "Kasra" <kasra_n500@yahoo.com> To: <boost@lists.boost.org> Sent: Monday, September 07, 2009 3:33 PM Subject: Re: [boost] [Boost.Function] How about parallel functions?
Hi,
I required to call few functions is separate threads and use the results. However, the code for this looks really ugly, you have to create threads, then join the threads when ever you require the result of the >functions.
So I have created a boost.parallel_function library that does this seamlessly just like invoking a normal function and it only joins the thread when the value of the function result type is required. Is this library of any interest to any one?
Here is the a hello world example:
#include <cstdlib> #include <limits> #include <boost/parallel_function.hpp> using namespace boost;
int F0() { volatile int x = 0; for(int i=0; i<std::numeric_limits<int>::max()/1024; i++) x ^= std::rand(); return (int)x; }
int F2(int y) { volatile int x = 0; for(int i=0; i<std::numeric_limits<int>::max()/512; i++) x ^= std::rand(); return (int)x ^ y; }
int main() { typedef boost::parallel_function< void ( ) > f0_type; typedef boost::parallel_function< void (int) > f1_type;
f0_type f0( &::F0 ); f1_type f1( &::F1 );
f0_type::result_type r0 = f0(); f1_type::result_type r1 = f1(32);
// use r0 and r1 as if they are int r0 += 34; int value = r1 *= r0 + 452;
std::cout << "Hello world " << value << std::endl;
return 0; }
The Boost.Task library should provides the features you need.
int main() {
boost::task::handle<int> f0 = boost::task:async(boost:task(F0),boost::task::new_thread()); boost::task::handle<int> f1 = boost::task:async(boost:task(F1,32),boost::task::new_thread()); int r0=f0.get(); int r1=f1.get();
r0 += 34; int value = r1 *= r0 + 452;
std::cout << "Hello world " << value << std::endl;
return 0; }
This library is under the review schedule and will be reviewed soon. I'm >interested on the details of your implementation to compare with >Boost.Task. It will be also useful if you can take a look to Boost.task and >try so see it provides every thing you need.
Best, Vicente
I have taken a partial look at Boost.task, however, what I don't like about the syntax is the following:
int r0=f0.get(); int r1=f1.get();
Current implementation (hack) would make 'f0' and 'f1' the same type 'int' (in this example). So the .get() part could be omitted altogether; which seems a lot like thread.join() call! Which is accomplished by a simple implicit conversion operator to the reference of the type.
As for futures it has been found safer to make explict the conversion in order to avoid unexpected implicit conversions. Maybe Anthony or Oliver can develop on this subject.
A little out of topic discussion, why is there an atomic library within task? because boost.thread has one (I think). Atomic library should be written so it becomes more coherent and reduces the code size.
I let the authors of these libraries to manage this issue. There are also other atmic implementations but there is not yet a partial implementation of the standard. BTW, did my example what you expected? Best, Vicente

[Please don't overquote: http://www.boost.org/community/policy.html#quoting] Kasra wrote:
I have taken a partial look at Boost.task, however, what I don't like about the syntax is the following:
int r0=f0.get(); int r1=f1.get();
Current implementation (hack) would make 'f0' and 'f1' the same type 'int' (in this example). So the .get() part could be omitted altogether; which seems a lot like thread.join() call! Which is accomplished by a simple implicit conversion operator to the reference of the type.
Implicit conversion operators are considered problematic in most cases as they can lead to ambiguities. _____ Rob Stewart robert.stewart@sig.com Software Engineer, Core Software using std::disclaimer; Susquehanna International Group, LLP http://www.sig.com IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.
participants (3)
-
Kasra
-
Stewart, Robert
-
vicente.botet