@@ -20,16 +20,30 @@ sumIntegers = go 0
2020 m <- expect
2121 go (acc + m) (n - 1)
2222
23−master :: Integer -> [NodeId] -> Process Integer
24−master n slaves = do
23+data SpawnStrategy = SpawnSyncWithReconnect
24+ | SpawnSyncNoReconnect
25+ | SpawnAsync
26+ deriving (Show, Read)
27+
28+master :: Integer -> SpawnStrategy -> [NodeId] -> Process Integer
29+master n spawnStrategy slaves = do
2530 us <- getSelfPid
2631
2732 -- Distribute 1 .. n amongst the slave processes
28− spawnLocal $
29− forM_ (zip [1 .. n] (cycle slaves)) $ \(m, there) -> do
30− spawnAsync there ($(mkClosure 'slave) (us, m))
31− _ <- expectTimeout 0 :: Process (Maybe DidSpawn)
32− return ()
33+ spawnLocal $ case spawnStrategy of
34+ SpawnSyncWithReconnect ->
35+ forM_ (zip [1 .. n] (cycle slaves)) $ \(m, there) -> do
36+ them <- spawn there ($(mkClosure 'slave) (us, m))
37+ reconnect them
38+ SpawnSyncNoReconnect ->
39+ forM_ (zip [1 .. n] (cycle slaves)) $ \(m, there) -> do
40+ _them <- spawn there ($(mkClosure 'slave) (us, m))
41+ return ()
42+ SpawnAsync ->
43+ forM_ (zip [1 .. n] (cycle slaves)) $ \(m, there) -> do
44+ spawnAsync there ($(mkClosure 'slave) (us, m))
45+ _ <- expectTimeout 0 :: Process (Maybe DidSpawn)
46+ return ()
3347
3448 -- Wait for the result
3549 sumIntegers (fromIntegral n)