问题
你好,我正在使用加速库来创建一个应用程序,允许用户交互调用处理图像的函数,这就是我使用 ghc api 基于和扩展 ghci 的原因。
问题是,当从 shell 运行编译后的可执行文件时,计算在 100 毫秒(略小于 80 毫秒)内完成,而在 ghci 中运行相同的编译代码则需要 100 毫秒以上(平均多于 140 毫秒)才能完成。
资源
示例代码+执行日志: https ://gist.github.com/zgredzik/15a437c87d3d8d03b8fc
描述
首先:测试是在编译 CUDA 内核之后运行的(编译本身增加了 2 秒,但事实并非如此)。
从 shell 运行编译后的可执行文件时,计算在 10 毫秒内完成。(shell first run
并second shell run
传递不同的参数以确保数据没有缓存在任何地方)。
当尝试从 ghci 运行相同的代码并摆弄输入数据时,计算需要超过 100 毫秒。我知道解释代码比编译代码慢,但我在 ghci 会话中加载相同的编译代码并调用相同的顶级绑定 ( packedFunction
)。我已明确键入它以确保它是专用的(与使用 SPECIALIZED pragma 的结果相同)。
main
但是,如果我在 ghci 中运行该函数(即使:set args
在连续调用之间更改输入数据),计算时间确实不到 10 毫秒。
编译Main.hs
与ghc -o main Main.hs -O2 -dynamic -threaded
我想知道开销来自哪里。有人对为什么会发生这种情况有任何建议吗?
remdezx发布的示例的简化版本:
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Data.Array.Accelerate as A
import Data.Array.Accelerate.CUDA as C
import Data.Time.Clock (diffUTCTime, getCurrentTime)
main :: IO ()
main = do
start <- getCurrentTime
print $ C.run $ A.maximum $ A.map (+1) $ A.use (fromList (Z:.1000000) [1..1000000] :: Vector Double)
end <- getCurrentTime
print $ diffUTCTime end start
当我编译并执行它需要0,09 秒才能完成。
$ ghc -O2 Main.hs -o main -threaded
[1 of 1] Compiling Main ( Main.hs, Main.o )
Linking main ...
$ ./main
Array (Z) [1000001.0]
0.092906s
但是当我预编译它并在解释器中运行它需要0,25s
$ ghc -O2 Main.hs -c -dynamic
$ ghci Main
ghci> main
Array (Z) [1000001.0]
0.258224s