我一直在尝试并行化
Haskell程序.令我惊讶的是,我的大多数尝试都使我的示例运行得更慢,因此我决定深入研究Threadscope并开始学习
here教程.
在part 5中引入了示例程序sudoku3,其被假定为良好并行化的Haskell程序的示例.所以我按照教程中描述的那样编译它
ghc -O2 sudoku3.hs -threaded -rtsopts -eventlog
和(不像在教程中描述的那样)测量它有多快和没有并行性.结果令人惊讶:
$./sudoku3 sudoku17.1000.txt +RTS -s -N1
1,181,490,684 bytes allocated in the heap
13,247,408 bytes copied during GC
1,094,432 bytes maximum residency (5 sample(s))
35,556 bytes maximum slop
3 MB total memory in use (0 MB lost due to fragmentation)
Tot time (elapsed) Avg pause Max pause
Gen 0 2282 colls, 0 par 0.07s 0.08s 0.0000s 0.0061s
Gen 1 5 colls, 0 par 0.01s 0.01s 0.0012s 0.0044s
TASKS: 3 (1 bound, 2 peak workers (2 total), using -N1)
SPARKS: 1000 (0 converted, 0 overflowed, 0 dud, 0 GC'd, 1000 fizzled)
INIT time 0.00s ( 0.00s elapsed)
MUT time 2.51s ( 2.90s elapsed)
GC time 0.08s ( 0.08s elapsed)
EXIT time 0.00s ( 0.00s elapsed)
Total time 2.59s ( 2.98s elapsed)
Alloc rate 469,886,714 bytes per MUT second
Productivity 97.0% of total user, 84.3% of total elapsed
gc_alloc_block_sync: 0
whitehole_spin: 0
gen[0].sync: 0
gen[1].sync: 0
这里有两个核心的运行:
$./sudoku3 sudoku17.1000.txt +RTS -s -N2
1,207,033,704 bytes allocated in the heap
23,422,808 bytes copied during GC
1,066,716 bytes maximum residency (22 sample(s))
47,524 bytes maximum slop
5 MB total memory in use (0 MB lost due to fragmentation)
Tot time (elapsed) Avg pause Max pause
Gen 0 1488 colls, 1488 par 0.40s 0.40s 0.0003s 0.0147s
Gen 1 22 colls, 21 par 0.07s 0.06s 0.0026s 0.0087s
Parallel GC work balance: 39.57% (serial 0%, perfect 100%)
TASKS: 4 (1 bound, 3 peak workers (3 total), using -N2)
SPARKS: 1000 (968 converted, 0 overflowed, 0 dud, 0 GC'd, 32 fizzled)
INIT time 0.00s ( 0.00s elapsed)
MUT time 3.45s ( 2.96s elapsed)
GC time 0.47s ( 0.45s elapsed)
EXIT time 0.00s ( 0.00s elapsed)
Total time 3.93s ( 3.41s elapsed)
Alloc rate 349,389,354 bytes per MUT second
Productivity 88.0% of total user, 101.4% of total elapsed
gc_alloc_block_sync: 913
whitehole_spin: 0
gen[0].sync: 25
gen[1].sync: 0
令人惊讶的是,两个核心的运行速度较慢.为什么是这样?
我的GHC版本是7.6.3
操作系统:Debian Jessie Linux / GNU i386(i686)
最佳答案 我刚刚再次进行测试,并且预期并行版本更快.
事实证明,衡量性能是一个坏主意,而CPU则100%忙于转换视频.