gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 06:40:44 25 4
gpt4 key购买 nike

我目前正在尝试了解如何在 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/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com