git.iko.soy/distributed-process
on master help sign in
Start a Demos package
f317db7Edsko de Vries
distributed-process-demos/LICENSEnew file+30
@@ -0,0 +1,30 @@
1+Copyright (c) 2012, Edsko de Vries
2+
3+All rights reserved.
4+
5+Redistribution and use in source and binary forms, with or without
6+modification, are permitted provided that the following conditions are met:
7+
8+ * Redistributions of source code must retain the above copyright
9+ notice, this list of conditions and the following disclaimer.
10+
11+ * Redistributions in binary form must reproduce the above
12+ copyright notice, this list of conditions and the following
13+ disclaimer in the documentation and/or other materials provided
14+ with the distribution.
15+
16+ * Neither the name of Edsko de Vries nor the names of other
17+ contributors may be used to endorse or promote products derived
18+ from this software without specific prior written permission.
19+
20+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
distributed-process-demos/Setup.hsnew file+2
@@ -0,0 +1,2 @@
1+import Distribution.Simple
2+main = defaultMain
distributed-process-demos/distributed-process-demos.cabalnew file+42
@@ -0,0 +1,42 @@
1+-- Initial distributed-process-demos.cabal generated by cabal init. For
2+-- further documentation, see http://haskell.org/cabal/users-guide/
3+
4+name: distributed-process-demos
5+version: 0.1.0.0
6+synopsis: Cloud Haskell Demo Applications
7+-- description:
8+homepage: http://github.com/haskell-distributed/distributed-process
9+license: BSD3
10+license-file: LICENSE
11+author: Edsko de Vries
12+maintainer: edsko@well-typed.com
13+copyright: Well-Typed LLP
14+category: Control
15+build-type: Simple
16+cabal-version: >=1.8
17+
18+flag use-simplelocalnet
19+ description: Use the SimpleLocalnet backend
20+ default: False
21+ manual: True
22+
23+flag use-azure
24+ description: Use the Azure backend
25+ default: False
26+ manual: True
27+
28+executable distributed-process-masterslave
29+ main-is: src/MasterSlave.hs
30+ build-depends: base ==4.5.*,
31+ distributed-process >= 0.3.2
32+ extensions: BangPatterns, CPP, TemplateHaskell
33+ ghc-options: -Wall
34+ if flag(use-simplelocalnet)
35+ build-depends: distributed-process-simplelocalnet
36+ cpp-options: -DUSE_SIMPLELOCALNET
37+ else
38+ if flag(use-azure)
39+ build-depends: distributed-process-azure
40+ cpp-options: -DUSE_AZURE
41+ else
42+ buildable: False
distributed-process-demos/src/MasterSlave.hsnew file+66
@@ -0,0 +1,66 @@
1+import System.Environment (getArgs)
2+import Control.Monad
3+import Control.Distributed.Process
4+import Control.Distributed.Process.Closure
5+
6+#ifdef USE_SIMPLELOCALNET
7+import Control.Distributed.Process.Node (initRemoteTable)
8+import Control.Distributed.Process.Backend.SimpleLocalnet
9+#endif
10+
11+--------------------------------------------------------------------------------
12+-- Backend agnostic code --
13+--------------------------------------------------------------------------------
14+
15+fib :: Integer -> Integer
16+fib = go (0, 1)
17+ where
18+ go (!a, !b) !n | n == 0 = a
19+ | otherwise = go (b, a + b) (n - 1)
20+
21+slave :: () -> Process ()
22+slave () = forever $ do
23+ (pid, n) <- expect
24+ send pid (fib n)
25+
26+remotable ['slave]
27+
28+master :: Integer -> [NodeId] -> Process Integer
29+master n slaves = do
30+ us <- getSelfPid
31+
32+ -- Start processes on the slaves that compute Fibonacci numbers
33+ slaveProcesses <- forM slaves $ flip spawn ($(mkClosure 'slave) ())
34+
35+ -- Distribute 1 .. n amongst the slave processes
36+ forM_ (zip [1 .. n] (cycle slaveProcesses)) $ \(m, them) ->
37+ send them (us, m)
38+
39+ -- Wait for the result
40+ partials <- replicateM (fromInteger n) (expect :: Process Integer)
41+
42+ -- And return the sum
43+ return (sum partials)
44+
45+rtable :: RemoteTable
46+rtable = __remoteTable initRemoteTable
47+
48+--------------------------------------------------------------------------------
49+-- Using the SimpleLocalnet backend --
50+--------------------------------------------------------------------------------
51+
52+#ifdef USE_SIMPLELOCALNET
53+main :: IO ()
54+main = do
55+ args <- getArgs
56+
57+ case args of
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
65+ startSlave backend
66+#endif -- USE_SIMPLELOCALNET