|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r55720 - sandbox/libs/mapreduce/examples/prime
From: cdm.henderson_at_[hidden]
Date: 2009-08-22 15:03:55
Author: chenderson
Date: 2009-08-22 15:03:54 EDT (Sat, 22 Aug 2009)
New Revision: 55720
URL: http://svn.boost.org/trac/boost/changeset/55720
Log:
Prime Number Calculator
Added:
sandbox/libs/mapreduce/examples/prime/
sandbox/libs/mapreduce/examples/prime/prime.cpp (contents, props changed)
sandbox/libs/mapreduce/examples/prime/prime.vcproj (contents, props changed)
Added: sandbox/libs/mapreduce/examples/prime/prime.cpp
==============================================================================
--- (empty file)
+++ sandbox/libs/mapreduce/examples/prime/prime.cpp 2009-08-22 15:03:54 EDT (Sat, 22 Aug 2009)
@@ -0,0 +1,119 @@
+//#include <algorithm>
+//#include <cmath>
+#//include <numeric>
+
+#include <boost/mapreduce.hpp>
+
+namespace prime_calculator {
+
+bool const is_prime(long const number)
+{
+ if (number == 0 || number == 1)
+ return false;
+ else if (number == 2)
+ return true;
+ else if (number%2 == 0)
+ return false;
+
+ long n = std::abs(number);
+ long const sqrt_number = static_cast<long>(std::sqrt(static_cast<double>(n)));
+
+ for (long i = 3; i < sqrt_number; ++i)
+ {
+ if (n % i == 0)
+ return false;
+ }
+
+ return true;
+}
+
+template<typename MapTask>
+class number_source : boost::noncopyable
+{
+ public:
+ number_source(long first, long last, long step)
+ : current_(first), last_(last), step_(step)
+ {
+ }
+
+ const bool setup_key(typename MapTask::key_type &key)
+ {
+ if (current_ > last_)
+ return false;
+
+ key = current_;
+ current_ = std::min(current_ + step_, last_+1);
+ return true;
+ }
+
+ const bool get_data(typename MapTask::key_type &key, typename MapTask::value_type &value) const
+ {
+ value = std::min(key + step_ - 1, last_);
+ return true;
+ }
+
+ private:
+ long step_;
+ long last_;
+ long current_;
+};
+
+struct map_task : public boost::mapreduce::map_task<long, long>
+{
+ template<typename Runtime>
+ static void map(Runtime &runtime, key_type const &key, value_type const &value)
+ {
+ BOOST_STATIC_ASSERT((boost::is_same<key_type, value_type>::value));
+ for (typename key_type loop=key; loop<=value; ++loop)
+ runtime.emit_intermediate(is_prime(loop), loop);
+ }
+};
+
+struct reduce_task : public boost::mapreduce::reduce_task<bool, long>
+{
+ template<typename Runtime, typename It>
+ static void reduce(Runtime &runtime, key_type const &key, It it, It ite)
+ {
+ if (key)
+ for_each(it, ite, boost::bind(&Runtime::emit, &runtime, true, _1));
+ }
+};
+
+typedef
+boost::mapreduce::job<prime_calculator::map_task
+ , prime_calculator::reduce_task
+ , boost::mapreduce::null_combiner
+ , prime_calculator::number_source<prime_calculator::map_task>
+> 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, 60000, 1000);
+
+ spec.map_tasks = 0;
+ spec.reduce_tasks = std::max(1U, boost::thread::hardware_concurrency());
+
+ std::cout <<"\nRunning Parallel Prime_Calculator MapReduce..." <<std::endl;
+ prime_calculator::job job(datasource, spec);
+#ifdef _DEBUG
+ job.run<boost::mapreduce::schedule_policy::sequential<prime_calculator::job> >(result);
+#else
+ job.run<boost::mapreduce::schedule_policy::cpu_parallel<prime_calculator::job> >(result);
+#endif
+ std::cout <<"\nMapReduce Finished." <<std::endl;
+
+ for (prime_calculator::job::const_result_iterator it = job.begin_results()
+ ; it!=job.end_results()
+ ; ++it
+ )
+ {
+ std::cout <<it->first <<" ";
+ }
+
+ return 0;
+}
Added: sandbox/libs/mapreduce/examples/prime/prime.vcproj
==============================================================================
--- (empty file)
+++ sandbox/libs/mapreduce/examples/prime/prime.vcproj 2009-08-22 15:03:54 EDT (Sat, 22 Aug 2009)
@@ -0,0 +1,197 @@
+<?xml version="1.0" encoding="Windows-1252"?>
+<VisualStudioProject
+ ProjectType="Visual C++"
+ Version="8.00"
+ Name="prime"
+ ProjectGUID="{FD001B34-1464-4D19-B92E-F33A1BD9E97C}"
+ RootNamespace="prime"
+ Keyword="Win32Proj"
+ >
+ <Platforms>
+ <Platform
+ Name="Win32"
+ />
+ </Platforms>
+ <ToolFiles>
+ </ToolFiles>
+ <Configurations>
+ <Configuration
+ Name="Debug|Win32"
+ OutputDirectory="$(ConfigurationName)"
+ IntermediateDirectory="$(ConfigurationName)\compiler"
+ ConfigurationType="1"
+ CharacterSet="1"
+ >
+ <Tool
+ Name="VCPreBuildEventTool"
+ />
+ <Tool
+ Name="VCCustomBuildTool"
+ />
+ <Tool
+ Name="VCXMLDataGeneratorTool"
+ />
+ <Tool
+ Name="VCWebServiceProxyGeneratorTool"
+ />
+ <Tool
+ Name="VCMIDLTool"
+ />
+ <Tool
+ Name="VCCLCompilerTool"
+ Optimization="0"
+ PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE"
+ MinimalRebuild="true"
+ BasicRuntimeChecks="3"
+ RuntimeLibrary="3"
+ UsePrecompiledHeader="0"
+ WarningLevel="3"
+ Detect64BitPortabilityProblems="true"
+ DebugInformationFormat="3"
+ />
+ <Tool
+ Name="VCManagedResourceCompilerTool"
+ />
+ <Tool
+ Name="VCResourceCompilerTool"
+ />
+ <Tool
+ Name="VCPreLinkEventTool"
+ />
+ <Tool
+ Name="VCLinkerTool"
+ LinkIncremental="2"
+ GenerateDebugInformation="true"
+ SubSystem="1"
+ TargetMachine="1"
+ />
+ <Tool
+ Name="VCALinkTool"
+ />
+ <Tool
+ Name="VCManifestTool"
+ />
+ <Tool
+ Name="VCXDCMakeTool"
+ />
+ <Tool
+ Name="VCBscMakeTool"
+ />
+ <Tool
+ Name="VCFxCopTool"
+ />
+ <Tool
+ Name="VCAppVerifierTool"
+ />
+ <Tool
+ Name="VCWebDeploymentTool"
+ />
+ <Tool
+ Name="VCPostBuildEventTool"
+ />
+ </Configuration>
+ <Configuration
+ Name="Release|Win32"
+ OutputDirectory="$(ConfigurationName)"
+ IntermediateDirectory="$(ConfigurationName)\compiler"
+ ConfigurationType="1"
+ CharacterSet="1"
+ WholeProgramOptimization="1"
+ >
+ <Tool
+ Name="VCPreBuildEventTool"
+ />
+ <Tool
+ Name="VCCustomBuildTool"
+ />
+ <Tool
+ Name="VCXMLDataGeneratorTool"
+ />
+ <Tool
+ Name="VCWebServiceProxyGeneratorTool"
+ />
+ <Tool
+ Name="VCMIDLTool"
+ />
+ <Tool
+ Name="VCCLCompilerTool"
+ PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE"
+ RuntimeLibrary="2"
+ UsePrecompiledHeader="0"
+ WarningLevel="3"
+ Detect64BitPortabilityProblems="true"
+ DebugInformationFormat="3"
+ />
+ <Tool
+ Name="VCManagedResourceCompilerTool"
+ />
+ <Tool
+ Name="VCResourceCompilerTool"
+ />
+ <Tool
+ Name="VCPreLinkEventTool"
+ />
+ <Tool
+ Name="VCLinkerTool"
+ LinkIncremental="1"
+ GenerateDebugInformation="true"
+ SubSystem="1"
+ OptimizeReferences="2"
+ EnableCOMDATFolding="2"
+ TargetMachine="1"
+ />
+ <Tool
+ Name="VCALinkTool"
+ />
+ <Tool
+ Name="VCManifestTool"
+ />
+ <Tool
+ Name="VCXDCMakeTool"
+ />
+ <Tool
+ Name="VCBscMakeTool"
+ />
+ <Tool
+ Name="VCFxCopTool"
+ />
+ <Tool
+ Name="VCAppVerifierTool"
+ />
+ <Tool
+ Name="VCWebDeploymentTool"
+ />
+ <Tool
+ Name="VCPostBuildEventTool"
+ />
+ </Configuration>
+ </Configurations>
+ <References>
+ </References>
+ <Files>
+ <Filter
+ Name="Source Files"
+ Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
+ UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
+ >
+ <File
+ RelativePath=".\prime.cpp"
+ >
+ </File>
+ </Filter>
+ <Filter
+ Name="Header Files"
+ Filter="h;hpp;hxx;hm;inl;inc;xsd"
+ UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
+ >
+ </Filter>
+ <Filter
+ Name="Resource Files"
+ Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
+ UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
+ >
+ </Filter>
+ </Files>
+ <Globals>
+ </Globals>
+</VisualStudioProject>
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