@@ -0,0 +1,66 @@
1+import System.Environment (getArgs)
3+import Control.Distributed.Process
4+import Control.Distributed.Process.Closure
6+#ifdef USE_SIMPLELOCALNET
7+import Control.Distributed.Process.Node (initRemoteTable)
8+import Control.Distributed.Process.Backend.SimpleLocalnet
11+--------------------------------------------------------------------------------
12+-- Backend agnostic code --
13+--------------------------------------------------------------------------------
15+fib :: Integer -> Integer
18+ go (!a, !b) !n | n == 0 = a
19+ | otherwise = go (b, a + b) (n - 1)
21+slave :: () -> Process ()
22+slave () = forever $ do
28+master :: Integer -> [NodeId] -> Process Integer
32+ -- Start processes on the slaves that compute Fibonacci numbers
33+ slaveProcesses <- forM slaves $ flip spawn ($(mkClosure 'slave) ())
35+ -- Distribute 1 .. n amongst the slave processes
36+ forM_ (zip [1 .. n] (cycle slaveProcesses)) $ \(m, them) ->
39+ -- Wait for the result
40+ partials <- replicateM (fromInteger n) (expect :: Process Integer)
42+ -- And return the sum
43+ return (sum partials)
46+rtable = __remoteTable initRemoteTable
48+--------------------------------------------------------------------------------
49+-- Using the SimpleLocalnet backend --
50+--------------------------------------------------------------------------------
52+#ifdef USE_SIMPLELOCALNET
58+ ["master", host, port, n] -> do
59+ backend <- initializeBackend host port rtable
60+ startMaster backend $ \slaves -> do
61+ result <- master (read n) slaves
62+ liftIO $ print result
63+ ["slave", host, port] -> do
64+ backend <- initializeBackend host port rtable
66+#endif -- USE_SIMPLELOCALNET