Using GHC with NVCC

As an alternative to accelerate , I am trying to invoke CUDA code via Haskell FFI.

Here is a simple program that does not compile:

cuda_code.cu:

void cuda_init() {
    cudaFree (0);
    cudaThreadSynchronize ();
}

Test.hs:

foreign import ccall unsafe "cuda_init" cuda_init :: IO ()
main = cuda_init

Compiled with

$> nvcc -c -o cuda_code.o cuda_code.cu
$> ghc Test cuda_code.o

and got some binding errors (undefined reference to cudaFree, etc.). This is not surprising, and the obvious solution for me is to communicate with NVCC using -pgml nvcc. (This worked when I used Intel CILK + in my C code: I just changed the linker to ICC, and it worked out just fine.)

Be that as it may, using NVCC to associate the results with a binding error:

ghc Test -pgml nvcc cuda_code.o
[1 of 1] Compiling Main             ( Test.hs, Test.o )
Linking Test ...
nvcc fatal   : Unknown option 'u'

Performance

strace -v -f -e execve ghc Test -pgml nvcc cuda_code.o

(is there an easier way?) I found that ghccalls nvccwith

nvcc ... -L ~ / ghc ... -L ... -l ... -l ... -u ghczmprim_GHC ... -u ghc ...

, -u gcc (, -, icc) undefined, - nvcc .

, GHC. , GHC CUDA?

-------- -----------------

- , GCC ( ), gcc, CUDA. - , , , , !

+4
2

, .

cudaTest.cu:

// the `extern "C"` is important! It tells nvcc to not 
// mangle the name, since nvcc assumes C++ code by default
extern "C" 
void cudafunc() {
  cudaFree(0);
  cudaThreadSynchronize();
}

Test.hs

foreign import ccall unsafe "cudafunc" cudaFunc :: IO ()
main = cudaFunc

:

>nvcc -c -o cudaTest.o cudaTest.cu
>ghc --make Test.hs -o Test cudaTest.o -optl-lcudart

GHC -pgmc g++ extern "C" ( ), CUDA. , , extern "C".

+2

GHC /usr/lib/ghc/settings , , /var/lib/ghc/package.conf.d/builtin_rts.conf, . ( ${GHC}/lib/ghc-${VERSION}/settings ${GHC}/lib/ghc-${VERSION}/package.conf.d .)

RTS:

ld-options: -u ghczmprim_GHCziTypes_Izh_static_info -u
            ghczmprim_GHCziTypes_Czh_static_info -u
            ghczmprim_GHCziTypes_Fzh_static_info -u
            ghczmprim_GHCziTypes_Dzh_static_info
            ...

man- ld -u undefined extern, - .

, , -u ld-options: package.conf.d.

, /, .

haskell-cafe@haskell.org. , - !

+3

All Articles