@@ -10,16 +10,26 @@ slave (pid, n) = send pid (numPrimeFactors n)
11remotable ['slave]
11remotable ['slave]
13+-- | Wait for n integers and sum them all up
14+sumIntegers :: Int -> Process Integer
17+ go :: Integer -> Int -> Process Integer
18+ go !acc 0 = return acc
13master :: Integer -> [NodeId] -> Process Integer
23master :: Integer -> [NodeId] -> Process Integer
14master n slaves = do
24master n slaves = do
15 us <- getSelfPid
25 us <- getSelfPid
17 -- Distribute 1 .. n amongst the slave processes
27 -- Distribute 1 .. n amongst the slave processes
18− spawnLocal $ forM_ (zip [1 .. n] (cycle slaves)) $ \(m, them) ->
29+ forM_ (zip [1 .. n] (cycle slaves)) $ \(m, there) -> do
19− spawn them ($(mkClosure 'slave) (us, m))
30+ spawnAsync there ($(mkClosure 'slave) (us, m))
31+ _ <- expectTimeout 0 :: Process (Maybe DidSpawn)
21 -- Wait for the result
34 -- Wait for the result
22− partials <- replicateM (fromIntegral n) (expect :: Process Integer)
35+ sumIntegers (fromIntegral n)
24− -- And return the sum
25− return (sum partials)