From 99353021c0ff0b0f087be9a1a5289e3f07a02fa8 Mon Sep 17 00:00:00 2001
From: Gregor Kleen <gkleen@yggdrasil.li>
Date: Tue, 8 Dec 2015 01:51:54 +0000
Subject: FFP - 08

---
 ws2015/ffp/blaetter/08/FFP_U08_Parallel.hs | 27 ++++++++++++++++++++++++---
 1 file changed, 24 insertions(+), 3 deletions(-)

diff --git a/ws2015/ffp/blaetter/08/FFP_U08_Parallel.hs b/ws2015/ffp/blaetter/08/FFP_U08_Parallel.hs
index 8615dad..34a77d2 100644
--- a/ws2015/ffp/blaetter/08/FFP_U08_Parallel.hs
+++ b/ws2015/ffp/blaetter/08/FFP_U08_Parallel.hs
@@ -114,7 +114,17 @@ main_A7_1seq = do
 
   
 main_A7_1par :: IO ()    -- parallele Version
-main_A7_1par = undefined -- !!! TODO !!!
+main_A7_1par = do
+  rets <- runParIO $ mapM (\(a, b) -> spawnP $ length $ hanoi difficulty a b) args >>= mapM get
+  putStrLn $ unlines $ zipWith (\a r -> show a ++ ": " ++ show r) args rets 
+  where
+    difficulty = 25
+    args = [ (1, 2)
+           , (1, 3)
+           , (2, 3)
+           , (3, 1)
+           ]
+
   
 
 -- Hilfsfunktionen, zur Erzeugung von Rechnenlast, bitte nicht verändern:
@@ -150,7 +160,15 @@ quicksortS  (x:xs)  = result
     hisort = quicksortS [y | y <- xs, y >= x]
 
 quicksortP :: [Integer] -> [Integer]
-quicksortP xs = undefined -- !!! TODO !!!
+quicksortP [] = []
+quicksortP (x:xs) = runEval $ do
+  lows <- strat $ quicksortP [y | y <- xs, y < x]
+  highs <- strat $ quicksortP [y | y <- xs, y >= x]
+  return $ lows ++ (x : highs)
+    where
+      strat = rparWith rdeepseq -- rpar, rseq
+      -- rparWith rdeepseq < rpar ~~ rseq
+      -- rpar macht nur minimale arbeit innerhalb des sparks (nur WHNF), der rest der arbeit wird am ende (bei return) in einem thread gemacht
             
 
 main_A7_2 = do
@@ -203,7 +221,10 @@ payL acc 0   coins = [acc]
 payL acc _   []    = []
 payL acc val ((c,qty):coins) 
   | c > val   = payL acc val coins 
-  | otherwise = left ++ right
+  | otherwise = let -- speedup by ~100% compared to (left ++ right)
+      l = right `par` left
+      r = right
+      in l ++ r
   where
     left  = payL (c:acc) (val - c) coins'
     right = payL    acc   val      coins
-- 
cgit v1.2.3