multithreading - 为什么我的程序用一个核心而不是两个核心更快?

标签 multithreading haskell parallel-processing

我目前正在尝试了解如何在 Haskell 中并行编程。我正在关注 Simon Peyton Jones 和 Satnam Singh 的论文“A Tutorial on Parallel and Concurrent Programming in Haskell”。源代码如下:

module Main where
import Control.Parallel
import System.Time

main :: IO ()
main = do
      putStrLn "Starting computation....."
      t0 <- getClockTime
      pseq r1 (return())
      t1 <- getClockTime
      putStrLn ("sum: " ++ show r1)
      putStrLn ("time: " ++ show (secDiff t0 t1) ++ " seconds")
      putStrLn "Finish."

r1 :: Int
r1 = parSumFibEuler 38 5300

-- This is the Fibonacci number generator
fib :: Int -> Int
fib 0 = 0
fib 1 = 1
fib n = fib (n-1) + fib (n-2)

-- Gets the euler sum
mkList :: Int -> [Int]
mkList n = [1..n-1]

relprime :: Int -> Int -> Bool
relprime x y = gcd x y == 1

euler :: Int -> Int
euler n = length $ filter (relprime n) (mkList n)

sumEuler :: Int -> Int
sumEuler = sum.(map euler).mkList

-- Gets the sum of Euler and Fibonacci (NORMAL)
sumFibEuler :: Int -> Int -> Int
sumFibEuler a b = fib a + sumEuler b

-- Gets the sum of Euler and Fibonacci (PARALLEL)
parSumFibEuler :: Int -> Int -> Int
parSumFibEuler a b =
  f `par` (e `pseq`(f+e))
    where
    f = fib a
    e = sumEuler b

-- Measure time
secDiff :: ClockTime -> ClockTime -> Float
secDiff (TOD secs1 psecs1) (TOD secs2 psecs2)
  = fromInteger (psecs2 -psecs1) / 1e12 + fromInteger (secs2- secs1)

我使用以下命令编译它:
ghc --make -threaded Main.hs

a) 使用 1 个核心运行它:
./Main +RTS -N1

b) 使用 2 个核心运行它:
./Main +RTS -N2

但是,一个核心运行了 53.556 秒。然而,两个核心运行了 73.401 秒。我不明白 2 核实际上如何运行得比 1 核慢。也许这个小程序的消息传递开销太大了?与地雷相比,该论文具有完全不同的结果。以下是输出详细信息。

对于 1 个核心:
Starting computation.....
sum: 47625790
time: 53.556335 seconds
Finish.
  17,961,210,216 bytes allocated in the heap
      12,595,880 bytes copied during GC
         176,536 bytes maximum residency (3 sample(s))
          23,904 bytes maximum slop
               2 MB total memory in use (0 MB lost due to fragmentation)

                                    Tot time (elapsed)  Avg pause  Max pause
  Gen  0     34389 colls,     0 par    2.54s    2.57s     0.0001s    0.0123s
  Gen  1         3 colls,     0 par    0.00s    0.00s     0.0007s    0.0010s

  Parallel GC work balance: -nan (0 / 0, ideal 1)

                        MUT time (elapsed)       GC time  (elapsed)
  Task  0 (worker) :    0.00s    (  0.00s)       0.00s    (  0.00s)
  Task  1 (worker) :    0.00s    ( 53.56s)       0.00s    (  0.00s)
  Task  2 (bound)  :   50.49s    ( 50.99s)       2.52s    (  2.57s)

  SPARKS: 0 (0 converted, 0 overflowed, 0 dud, 0 GC'd, 0 fizzled)

  INIT    time    0.00s  (  0.00s elapsed)
  MUT     time   50.47s  ( 50.99s elapsed)
  GC      time    2.54s  (  2.57s elapsed)
  EXIT    time    0.00s  (  0.00s elapsed)
  Total   time   53.02s  ( 53.56s elapsed)

  Alloc rate    355,810,305 bytes per MUT second

  Productivity  95.2% of total user, 94.2% of total elapsed

gc_alloc_block_sync: 0
whitehole_spin: 0
gen[0].sync: 0
gen[1].sync: 0

对于 2 核:
Starting computation.....
sum: 47625790
time: 73.401146 seconds
Finish.
  17,961,210,256 bytes allocated in the heap
      12,558,088 bytes copied during GC
         176,536 bytes maximum residency (3 sample(s))
         195,936 bytes maximum slop
               3 MB total memory in use (0 MB lost due to fragmentation)

                                    Tot time (elapsed)  Avg pause  Max pause
  Gen  0     34389 colls, 34388 par    7.42s    4.73s     0.0001s    0.0205s
  Gen  1         3 colls,     3 par    0.01s    0.00s     0.0011s    0.0017s

  Parallel GC work balance: 1.00 (1432193 / 1429197, ideal 2)

                        MUT time (elapsed)       GC time  (elapsed)
  Task  0 (worker) :    1.19s    ( 40.26s)      16.95s    ( 33.15s)
  Task  1 (worker) :    0.00s    ( 73.40s)       0.00s    (  0.00s)
  Task  2 (bound)  :   54.50s    ( 68.67s)       3.66s    (  4.73s)
  Task  3 (worker) :    0.00s    ( 73.41s)       0.00s    (  0.00s)

  SPARKS: 0 (0 converted, 0 overflowed, 0 dud, 0 GC'd, 0 fizzled)

  INIT    time    0.00s  (  0.00s elapsed)
  MUT     time   68.87s  ( 68.67s elapsed)
  GC      time    7.43s  (  4.73s elapsed)
  EXIT    time    0.00s  (  0.00s elapsed)
  Total   time   76.31s  ( 73.41s elapsed)

  Alloc rate    260,751,318 bytes per MUT second

  Productivity  90.3% of total user, 93.8% of total elapsed

gc_alloc_block_sync: 12254
whitehole_spin: 0
gen[0].sync: 0
gen[1].sync: 0

最佳答案

r1 = sumFibEuler 38 5300

我相信你的意思是
r1 = parSumFibEuler 38 5300

在我的配置中(使用 parSumFibEuler 45 8000 并且只运行一次):
  • 当 N1 = 126.83s
  • 当 N2 = 115.46s

  • 我怀疑 fibsumEuler 消耗更多 CPU 的函数.这可以解释 -N2 的低改进。在你的情况下不会有一些偷工减料。

    有了内存,你的斐波那契函数会好得多,但我认为这不是你想要尝试的。

    编辑:正如评论中提到的,我认为使用 -N2 你有很多中断,因为你有两个可用的内核。
    我的配置示例(4 核)与 sum $ parMap rdeepseq (fib) [1..40]
  • 使用 -N1 大约需要 26 秒
  • 使用 -N2 大约需要 16 秒
  • 使用 -N3 大约需要 13 秒
  • 使用 -N4 大约需要 30 秒(嗯,Haskell 程序并不孤单)

  • 来自 here :

    Be careful when using all the processors in your machine: if some of your processors are in use by other programs, this can actually harm performance rather than improve it.

    关于multithreading - 为什么我的程序用一个核心而不是两个核心更快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13188747/

    相关文章:

    multithreading - 多个线程在 C++ 中等待同一个事件句柄

    multithreading - 如何通过ctrl + c停止多线程进程

    java - 如何创建一个监听端口并接受多个客户端的 Java 服务器

    haskell - 为什么我不能使用 (cnt <- hGetContents h) 表达式而不是 cnt?

    list - Haskell- 在列表中查找元素并返回其位置

    c# - 如何取消在 C# Winforms 应用程序中执行长时间运行的异步任务

    haskell - 使用 RankNTypes 和 TypeFamilies 的非法多态或限定类型

    c++ - OpenMP - 主指令中的并行区域

    c#-4.0 - 并行执行每个有序执行

    java - Weld CDI 环境中的并行 Web 服务访问