vivace
vivace

Reputation: 59

Haskell FFI with CUDA using stack build tool set up?

I have a project which is working quite nicely with haskell, stack, and some C code via FFI. I would like to add some CUDA code to offload some computations to the GPU. Kind of not sure how this needs to be configured?

in my .cabal file I simply have:

c-sources: src/ccode/blah.c, src/ccode/blah.h, src/ccode/blah2.c, etc.. cc-options: -std=c99

when i run stack build it nicely builds both the C and the haskell. How can I add CUDA to the mix?

Upvotes: 1

Views: 145

Answers (1)

zbw
zbw

Reputation: 962

You could manually compile the CUDA code before using cabal:

nvcc -c cudacode.c

Then build with cabal. Example .cabal file:

name:                package
version:             0.1
build-type:          Simple

executable main
  main-is:             Main.hs
  build-depends:       base
  extra-libraries:     stdc++
  ghc-options:         -pgmlg++ cudacode.o
  C-sources:           just_c.c, some_cpp.cpp
  Includes:            just_c.h, cudacode.h, some_cpp.h

Without C++, you can omit the extra-libraries field and the first ghc-option.

Alternatively, you can specify nvcc as the compiler for everything, and not have to compile it separately:

cabal install --with-gcc=/path/to/nvcc

Example .cabal file:

name:                package
version:             0.1
build-type:          Simple

executable main
  main-is:             Main.hs
  build-depends:       base
  extra-libraries:     stdc++
  ghc-options:         -pgmlg++
  C-sources:           just_c.c, cudacode.c, some_cpp.cpp
  Includes:            just_c.h, cudacode.h, some_cpp.h

Again, the extra-libraries and ghc-options are only necessary with C++.

Upvotes: 1

Related Questions