git.iko.soy/distributed-process
on master help sign in
Use numPrimeFactors instead of fib
commitd6649fa1542e353676379a807a4e05c019c4ffa5 authorEdsko de Vries <edsko@edsko.net> dateFri 28 Sep 2012, 14:13 (14 years ago) parent8f51d80
5 files changed+40−30
distributed-process-demos/distributed-process-demos.cabal+5−5 distributed-process-demos/src/Common/PrimeFactors.hs+25 distributed-process-demos/src/MasterSlave/MasterSlave.hs+2−7 distributed-process-demos/src/WorkPushing/WorkPushing.hs+3−8 distributed-process-demos/src/WorkStealing/WorkStealing.hs+5−10
distributed-process-demos/distributed-process-demos.cabal+5−5
@@ -25,7 +25,7 @@ flag use-azure
2525
2626executable distributed-process-simplelocalnet-masterslave
2727 main-is: SimpleLocalnet.hs
28− hs-source-dirs: src/MasterSlave
28+ hs-source-dirs: src/MasterSlave src/Common
2929 build-depends: base ==4.5.*,
3030 distributed-process >= 0.3.2
3131 extensions: BangPatterns, CPP, TemplateHaskell
@@ -38,7 +38,7 @@ executable distributed-process-simplelocalnet-masterslave
3838
3939executable distributed-process-azure-masterslave
4040 main-is: Azure.hs
41− hs-source-dirs: src/MasterSlave
41+ hs-source-dirs: src/MasterSlave src/Common
4242 build-depends: base ==4.5.*,
4343 distributed-process >= 0.3.2
4444 extensions: BangPatterns, CPP, TemplateHaskell
@@ -51,7 +51,7 @@ executable distributed-process-azure-masterslave
5151
5252executable distributed-process-simplelocalnet-workpushing
5353 main-is: SimpleLocalnet.hs
54− hs-source-dirs: src/WorkPushing
54+ hs-source-dirs: src/WorkPushing src/Common
5555 build-depends: base ==4.5.*,
5656 distributed-process >= 0.3.2
5757 extensions: BangPatterns, CPP, TemplateHaskell
@@ -64,7 +64,7 @@ executable distributed-process-simplelocalnet-workpushing
6464
6565executable distributed-process-azure-workpushing
6666 main-is: Azure.hs
67− hs-source-dirs: src/WorkPushing
67+ hs-source-dirs: src/WorkPushing src/Common
6868 build-depends: base ==4.5.*,
6969 distributed-process >= 0.3.2
7070 extensions: BangPatterns, CPP, TemplateHaskell
@@ -77,7 +77,7 @@ executable distributed-process-azure-workpushing
7777
7878executable distributed-process-simplelocalnet-workstealing
7979 main-is: SimpleLocalnet.hs
80− hs-source-dirs: src/WorkStealing
80+ hs-source-dirs: src/WorkStealing src/Common
8181 build-depends: base ==4.5.*,
8282 distributed-process >= 0.3.2
8383 extensions: BangPatterns, CPP, TemplateHaskell
distributed-process-demos/src/Common/PrimeFactors.hsnew file+25
@@ -0,0 +1,25 @@
1+-- | Prime factorization
2+--
3+-- Written by Dan Weston
4+-- <http://westondan.blogspot.ie/2007/07/simple-prime-factorization-code.html>
5+module PrimeFactors where
6+
7+primes :: [Integer]
8+primes = primes' (2:[3,5..])
9+ where
10+ primes' (x:xs) = x : primes' (filter (notDivisorOf x) xs)
11+ notDivisorOf d n = n `mod` d /= 0
12+
13+factors :: [Integer] -> Integer -> [Integer]
14+factors qs@(p:ps) n
15+ | n <= 1 = []
16+ | m == 0 = p : factors qs d
17+ | otherwise = factors ps n
18+ where
19+ (d,m) = n `divMod` p
20+
21+primeFactors :: Integer -> [Integer]
22+primeFactors = factors primes
23+
24+numPrimeFactors :: Integer -> Integer
25+numPrimeFactors = fromIntegral . length . primeFactors
distributed-process-demos/src/MasterSlave/MasterSlave.hs+2−7
@@ -3,15 +3,10 @@ module MasterSlave where
33import Control.Monad
44import Control.Distributed.Process
55import Control.Distributed.Process.Closure
6−
7−fib :: Integer -> Integer
8−fib = go (0, 1)
9− where
10− go (!a, !b) !n | n == 0 = a
11− | otherwise = go (b, a + b) (n - 1)
6+import PrimeFactors
127
138slave :: (ProcessId, Integer) -> Process ()
14−slave (pid, n) = send pid (fib n)
9+slave (pid, n) = send pid (numPrimeFactors n)
1510
1611remotable ['slave]
1712
distributed-process-demos/src/WorkPushing/WorkPushing.hs+3−8
@@ -3,17 +3,12 @@ module WorkPushing where
33import Control.Monad
44import Control.Distributed.Process
55import Control.Distributed.Process.Closure
6−
7−fib :: Integer -> Integer
8−fib = go (0, 1)
9− where
10− go (!a, !b) !n | n == 0 = a
11− | otherwise = go (b, a + b) (n - 1)
6+import PrimeFactors
127
138slave :: ProcessId -> Process ()
149slave them = forever $ do
1510 n <- expect
16− send them (fib n)
11+ send them (numPrimeFactors n)
1712
1813remotable ['slave]
1914
@@ -21,7 +16,7 @@ master :: Integer -> [NodeId] -> Process Integer
2116master n slaves = do
2217 us <- getSelfPid
2318
24− -- Start processes on the slaves that compute Fibonacci numbers
19+ -- Start slave processes
2520 slaveProcesses <- forM slaves $ \nid -> spawn nid ($(mkClosure 'slave) us)
2621
2722 -- Distribute 1 .. n amongst the slave processes
distributed-process-demos/src/WorkStealing/WorkStealing.hs+5−10
@@ -3,12 +3,7 @@ module WorkStealing where
33import Control.Monad
44import Control.Distributed.Process
55import Control.Distributed.Process.Closure
6−
7−fib :: Integer -> Integer
8−fib = go (0, 1)
9− where
10− go (!a, !b) !n | n == 0 = a
11− | otherwise = go (b, a + b) (n - 1)
6+import PrimeFactors
127
138slave :: (ProcessId, ProcessId) -> Process ()
149slave (master, workQueue) = do
@@ -21,7 +16,7 @@ slave (master, workQueue) = do
2116
2217 -- If there is work, do it, otherwise terminate
2318 receiveWait
24− [ match $ \n -> send master (fib n) >> go us
19+ [ match $ \n -> send master (numPrimeFactors n) >> go us
2520 , match $ \() -> return ()
2621 ]
2722
@@ -32,17 +27,17 @@ master n slaves = do
3227 us <- getSelfPid
3328
3429 workQueue <- spawnLocal $ do
35− -- As long as there is work, return the next Fib to compute
30+ -- Reply with the next bit of work to be done
3631 forM_ [1 .. n] $ \m -> do
3732 them <- expect
3833 send them m
3934
40− -- After that, just report that the work is done
35+ -- Once all the work is done, tell the slaves to terminate
4136 forever $ do
4237 pid <- expect
4338 send pid ()
4439
45− -- Start processes on the slaves that compute Fibonacci numbers
40+ -- Start slave processes
4641 forM_ slaves $ \nid -> spawn nid ($(mkClosure 'slave) (us, workQueue))
4742
4843 -- Wait for the result