我如何在Haskell上FFI一个CUDA应用程序?

时间:2015-05-23 15:13:57

标签: haskell cuda ffi

我已经将一个Haskell应用程序移植到CUDA以加速它。现在,我有一个.cu文件,我想在Haskell中使用它作为API。我已经按照教程轻松设法了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

1 个答案:

答案 0 :(得分:2)

我设法通过将extern "C"添加到hello.cu上的所有函数来解决它:

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