|
Boost Users : |
From: Kim Kuen tang (kuentang_at_[hidden])
Date: 2008-05-27 16:30:36
Hi all,
i've written a small code for estimating the mean from a series of
random numbers.
The random numbers are generated with and without boost::thread.
The simulation shows me that with boost::thread you only get a small
performance.
So my question is, how much performance can you get for simulation with
boost::thread?
with best regards,
Kim Tang
My code is shown below:
#include <iostream>
#include <fstream>
#include <ctime> // std::time
#include <climits>
#include <boost/timer.hpp>
#include <boost/random/linear_congruential.hpp>
#include <boost/random/uniform_int.hpp>
#include <boost/random/uniform_real.hpp>
#include <boost/random/variate_generator.hpp>
#include <boost/random/normal_distribution.hpp>
#include <boost/thread/thread.hpp>
// Sun CC doesn't handle boost::iterator_adaptor yet
#if !defined(__SUNPRO_CC) || (__SUNPRO_CC > 0x530)
#include <boost/generator_iterator.hpp>
#endif
#ifdef BOOST_NO_STDC_NAMESPACE
namespace std {
using ::time;
}
#endif
typedef boost::minstd_rand base_generator_type;
typedef boost::variate_generator<base_generator_type&,
boost::normal_distribution<> > NormalRandomNumber;
class SimClass
{
public: SimClass(double& data,NormalRandomNumber & generator,
std::size_t number)
: data_(data),
generator_(generator),
number_(number)
{};
void operator()()
{
data_=0.0;
for(std::size_t i = 0; i < number_; i++)
data_+=generator_();
};
private:
double& data_;
NormalRandomNumber generator_;
std::size_t number_;
};
int main()
{
double mean=0.0;
const std::size_t NUM_OF_THREADS=4;
const std::size_t NUM_CALCS=10000000;
const std::size_t NSIMULATION=NUM_CALCS*NUM_OF_THREADS;
base_generator_type generator(42u);
std::cout <<NSIMULATION <<" samples of a normal distribution :\n";
boost::normal_distribution<> norm_dist(0,1);
NormalRandomNumber normalRN(generator, norm_dist);
std::cout.setf(std::ios::fixed);
// You can now retrieve random numbers from that distribution by means
// of a STL Generator interface, i.e. calling the generator as a zero-
// argument function.
boost::timer cTime;
for(int i = 0; i < NSIMULATION; i++)
mean+=normalRN();
std::cout <<"estimated mean is: " <<mean/NSIMULATION << '\n';
std::cout<<"time to elapsed:"<<cTime.elapsed()<<"\n";
std::cout <<NSIMULATION <<" samples of a normal distribution with
"<<NUM_OF_THREADS<<" threads:\n";
cTime.restart();
double results[NUM_OF_THREADS];
boost::thread_group thrds;
for (int i=0; i < NUM_OF_THREADS; ++i)
thrds.create_thread(SimClass(results[i],normalRN,NUM_CALCS));
thrds.join_all();
double result=0.0;
for(std::size_t i=0;i<NUM_OF_THREADS;++i)
result+=results[i];
std::cout<<"estimated mean is: "<<result/NSIMULATION<<"\n";
std::cout<<"time to elapsed:"<<cTime.elapsed()<<"\n";
return 0;
}
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net