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

Mai*_*tor 6 linker haskell cuda ffi ghc

我已将 Haskell 应用程序移植到 CUDA 以加速它。现在,我有一个.cu文件想要从 Haskell 作为 API 调用。我已经通过遵循教程轻松地管理了 FFI C 文件,但我不确定这如何应用于 CUDA/nvcc。如何通过 FFI 访问 CUDA 源文件中定义的符号?

为了完成,这就是我尝试将其视为.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)
Run Code Online (Sandbox Code Playgroud)

这是我的 Haskell 文件:

{-# LANGUAGE ForeignFunctionInterface #-}

module Main where

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

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

main = hello
Run Code Online (Sandbox Code Playgroud)

Mai*_*tor 5

我设法通过添加extern "C"以下所有功能来解决这个问题hello.cu

-- hello.cu
extern "C" 
void hello();
Run Code Online (Sandbox Code Playgroud)

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

nvcc -c hello.cu
Run Code Online (Sandbox Code Playgroud)

Haskell 文件包含:

ghc --make test.hs -o test hello.o -L/usr/local/cuda/lib -optl-lcudart
Run Code Online (Sandbox Code Playgroud)