
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users- bounces@lists.boost.org] On Behalf Of Christian Henning Sent: 21 August 2009 15:05 To: boost-users@lists.boost.org Subject: Re: [Boost-users] [mapreduce] Prim Calculator
Hi Craig, what you're suggesting is exactly what I do. For reasons I don't understand I'm getting the following assertion:
Running Parallel Prime_Calculator MapReduce...Assertion failed: map_key != typename map_task_type::key_type(), file c:\chh\boost\boost\mapreduce\job.hpp, line 210
I don't understand why that happens? Can you help me out here.
Hi Christian
The assertion is an incorrect error condition which meant that the key could
not be a default constructed valued (in the case of your numeric key, zero).
I have updated the sandbox - thanks for finding this. Get the latest version
of job.hpp & in_memory.hpp from the sandbox.
There are a couple other problems in your implementation, too.
* number_source constructor should initialize _current to be start, not
zero.
* using a global to hold prime_numbers isn't thread-safe, and adding locking
will introduce contention. The MapReduce library provides mechanisms to
avoid this. In reduce_task::reduce, replace the line
copy( it, ite, back_inserter( prime_numbers ));
with
for_each(it, ite, boost::bind(&Runtime::emit, &runtime, _1, 0));
which will emit the final results.
To display the results:
for (prime_calculator::job::const_result_iterator
it=job.begin_results(); it!=job.end_results(); ++it)
std::cout << it->first << " ";
For performance, the datasource should return a range of integers so each
task tests many prime numbers rather than one at a time. This will reduce
lock contention in retrieving map keys.
Finally, 0 & 1 are not prime numbers - this is a bug in your is_prime
function.
Regards
-- Craig
#include <algorithm>
#include <cmath>
#include <numeric>
#include
job;
} // namespace prime_calculator
int main(int argc, char* argv[])
{
boost::mapreduce::specification spec;
boost::mapreduce::results result;
prime_calculator::job::datasource_type datasource( 0, 1000 );
spec.map_tasks = 0;
spec.reduce_tasks = std::max( 1U, boost::thread::hardware_concurrency()
);
std::cout << "\nRunning Parallel Prime_Calculator MapReduce...";
prime_calculator::job job( datasource, spec );
job.run<
boost::mapreduce::schedule_policy::cpu_parallel