4

我一直在尝试并行化 Haskell 程序。令我惊讶的是,我的大多数尝试都使我的示例运行速度变慢,因此我决定深入研究 Threadscope 并开始遵循此处的教程。

第 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)

4

1 回答 1

2

我刚刚再次进行了测试,并且像预期的那样并行版本更快。

事实证明,在 CPU 100% 忙于转换视频时,衡量性能是个坏主意。

于 2015-02-20T21:10:47.553 回答