如何在Haskell上使用FFI调用CUDA应用程序?

6
我已经将一个Haskell应用程序移植到CUDA上以加速它。现在,我有一个.cu文件,想将其作为API从Haskell中使用。我已经成功地按照教程用FFI连接了C文件,但我不确定如何将其应用于CUDA/nvcc。我该怎么做?
补充一下,这是我尝试将.cu文件视为普通的.c文件时得到的结果:
vh:CUDA apple1$ nvcc hello.cu -c -o hello.o
vh:CUDA apple1$ ghc test.hs -o test hello.o
Linking test ...
Undefined symbols for architecture x86_64:
  "___cudaRegisterFatBinary", referenced from:
      __sti____cudaRegisterAll_40_tmpxft_00002168_00000000_7_hello_cpp1_ii_f33df8d2() in hello.o
  "___cudaRegisterFunction", referenced from:
      __nv_cudaEntityRegisterCallback(void**) in hello.o
  "___cudaUnregisterFatBinary", referenced from:
      __cudaUnregisterBinaryUtil() in hello.o
  "_cudaConfigureCall", referenced from:
      render(Renderer_*) in hello.o
  "_cudaFree", referenced from:
      renderer_free(Renderer_*) in hello.o
  "_cudaLaunch", referenced from:
      cudaError cudaLaunch<char>(char*) in hello.o
  "_cudaMalloc", referenced from:
      renderer_init(Renderer_*, float, float, float, float, float) in hello.o
  "_cudaMemcpy", referenced from:
      renderer_init(Renderer_*, float, float, float, float, float) in hello.o
      render(Renderer_*) in hello.o
  "_cudaSetupArgument", referenced from:
      __device_stub__Z4walk6float3PiS_S_S_S_S0_(float3&, int*, float3&, float3&, float3&, float3&, int*) in hello.o
  "_hello", referenced from:
      _r3yw_info in test.o
      _c3Ib_info in test.o
      _c3Il_info in test.o
     (maybe you meant: _Main_hello_closure, _Main_hello_info )
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

以下是我的Haskell文件:

{-# LANGUAGE ForeignFunctionInterface #-}

module Main where

import Foreign.C
import Foreign.Ptr (Ptr,nullPtr)

foreign import ccall "hello" hello :: IO ()

main = hello

2
你可能需要链接CUDA运行库。 - Robert Crovella
1个回答

5

我通过在 hello.cu 中的所有函数上添加 extern "C" 来解决了这个问题:

-- hello.cu
extern "C" 
void hello();

使用以下命令编译CUDA文件:

nvcc -c hello.cu

以及附带 Haskell 文件:

ghc --make test.hs -o test hello.o -L/usr/local/cuda/lib -optl-lcudart

请告诉我是否有更好的答案。 - MaiaVictor
1
我认为没有更好的答案,你只需要使用适当且可移植的ABI(使用“extern”C”)并与CUDA库链接即可。 - m0nhawk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接