Skip to content

Commit 6d4f454

Browse files
Add CI for inline-c-cuda
1 parent de3b85d commit 6d4f454

File tree

7 files changed

+100
-13
lines changed

7 files changed

+100
-13
lines changed

.github/workflows/ci.yaml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,19 @@ jobs:
3737
- name: Build
3838
run: |
3939
if [ ${{ matrix.os }} == "ubuntu-latest" ] ; then
40-
stack build --stack-yaml stack-${{ matrix.stackage }}.yaml --flag inline-c:gsl-example --flag inline-c-cpp:std-vector-example
40+
stack build --stack-yaml stack-${{ matrix.stackage }}.yaml --flag inline-c:gsl-example --flag inline-c-cpp:std-vector-example --flag inline-c-cuda:test-without-cuda
4141
else
42-
stack build --stack-yaml stack-${{ matrix.stackage }}.yaml --flag inline-c-cpp:std-vector-example
42+
stack build --stack-yaml stack-${{ matrix.stackage }}.yaml --flag inline-c-cpp:std-vector-example --flag inline-c-cuda:test-without-cuda
4343
fi
4444
- name: Test
4545
run: |
4646
if [ ${{ matrix.os }} == "ubuntu-latest" ] ; then
47-
stack test --stack-yaml stack-${{ matrix.stackage }}.yaml --flag inline-c:gsl-example --flag inline-c-cpp:std-vector-example
47+
stack test --stack-yaml stack-${{ matrix.stackage }}.yaml --flag inline-c:gsl-example --flag inline-c-cpp:std-vector-example --flag inline-c-cuda:test-without-cuda
4848
./inline-c-cpp/test-error-message-line-numbers.sh --stack-yaml stack-${{ matrix.stackage }}.yaml
4949
else
50-
stack test --stack-yaml stack-${{ matrix.stackage }}.yaml --flag inline-c-cpp:std-vector-example
50+
stack test --stack-yaml stack-${{ matrix.stackage }}.yaml --flag inline-c-cpp:std-vector-example --flag inline-c-cuda:test-without-cuda
5151
./inline-c-cpp/test-error-message-line-numbers.sh --stack-yaml stack-${{ matrix.stackage }}.yaml
5252
fi
53+
env:
54+
INLINE_C_CUDA_SUFFIX: cc
55+
INLINE_C_CUDA_COMPILER: g++

inline-c-cuda/inline-c-cuda.cabal

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,21 @@ description: Utilities to inline CUDA code into Haskell using inline-c.
66
tests for example on how to build.
77
license: MIT
88
license-file: LICENSE
9-
author: Francesco Mazzoli
10-
maintainer: [email protected]
11-
copyright: (c) 2015-2016 FP Complete Corporation, (c) 2017-2019 Francesco Mazzoli
9+
author: Junji Hashimoto
10+
maintainer: [email protected]
11+
copyright: (c) 2015-2016 FP Complete Corporation, (c) 2023 Junji Hashimoto
1212
category: FFI
13-
tested-with: GHC == 9.2.2
13+
tested-with: GHC == 9.2.8, GHC == 9.4.7, GHC == 9.6.2
1414
build-type: Simple
1515

1616
source-repository head
1717
type: git
1818
location: https://github.com/fpco/inline-c
1919

20+
flag test-without-cuda
21+
description: Test without cuda
22+
default: False
23+
2024
library
2125
exposed-modules: Language.C.Inline.Cuda
2226
build-depends: base >=4.7 && <5
@@ -31,7 +35,11 @@ library
3135
hs-source-dirs: src
3236
default-language: Haskell2010
3337
ghc-options: -Wall
34-
extra-libraries: cudart
38+
if flag(test-without-cuda)
39+
cpp-options: -DTEST_WITHOUT_CUDA
40+
else
41+
extra-libraries: cudart
42+
3543

3644
test-suite tests
3745
type: exitcode-stdio-1.0
@@ -48,3 +56,5 @@ test-suite tests
4856
, template-haskell
4957
, vector
5058
default-language: Haskell2010
59+
if flag(test-without-cuda)
60+
cpp-options: -DTEST_WITHOUT_CUDA

inline-c-cuda/src/Language/C/Inline/Cuda.hs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE CPP #-}
12
{-# LANGUAGE QuasiQuotes #-}
23
{-# LANGUAGE TemplateHaskell #-}
34
{-# LANGUAGE OverloadedStrings #-}
@@ -25,12 +26,21 @@ import qualified Data.Map as Map
2526
import Control.Monad.IO.Class (liftIO)
2627
import System.Exit (ExitCode(..))
2728
import System.Process (readProcessWithExitCode)
29+
import System.Environment (lookupEnv)
30+
import Data.Maybe (fromMaybe)
2831

2932
compileCuda :: String -> TH.Q FilePath
3033
compileCuda src = do
31-
cuFile <- TH.addTempFile "cu"
34+
#ifdef TEST_WITHOUT_CUDA
35+
nvcc <- fromMaybe "g++" <$> TH.runIO (lookupEnv "INLINE_C_CUDA_COMPILER")
36+
cu <- fromMaybe "cc" <$> TH.runIO (lookupEnv "INLINE_C_CUDA_SUFFIX")
37+
#else
38+
nvcc <- fromMaybe "nvcc" <$> TH.runIO (lookupEnv "INLINE_C_CUDA_COMPILER")
39+
cu <- fromMaybe "cu" <$> TH.runIO (lookupEnv "INLINE_C_CUDA_SUFFIX")
40+
#endif
3241
oFile <- TH.addTempFile "o"
33-
let (cmd,args) = ("nvcc", ["-c","-o",oFile, cuFile])
42+
cuFile <- TH.addTempFile cu
43+
let (cmd,args) = (nvcc, ["-c", "-o", oFile, cuFile])
3444
(code, stdout, stderr) <- liftIO $ do
3545
writeFile cuFile src
3646
readProcessWithExitCode cmd args ""

inline-c-cuda/test/tests.hs

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,57 @@ C.context $ C.cudaCtx
3737

3838
C.include "<iostream>"
3939
C.include "<stdexcept>"
40+
C.include "<cstring>"
41+
42+
#ifdef TEST_WITHOUT_CUDA
43+
44+
[C.emitBlock|
45+
46+
void
47+
vectorAdd(int blocksPerGrid, int threadsPerBlock, const float *A, const float *B, float *C, int numElements)
48+
{
49+
for(int blockIdx = 0; blockIdx < blocksPerGrid ; blockIdx++){
50+
int blockDim = threadsPerBlock;
51+
for(int threadIdx = 0; threadIdx < threadsPerBlock ; threadIdx++){
52+
int i = blockDim * blockIdx + threadIdx;
53+
54+
if (i < numElements)
55+
{
56+
C[i] = A[i] + B[i];
57+
}
58+
}
59+
}
60+
}
61+
62+
63+
typedef int cudaError_t;
64+
const int cudaSuccess = 1;
65+
66+
cudaError_t cudaMalloc(void** dst, size_t size){
67+
*dst = malloc(size);
68+
return cudaSuccess;
69+
}
70+
71+
cudaError_t cudaFree(void* dst){
72+
free(dst);
73+
return cudaSuccess;
74+
}
75+
76+
const int cudaMemcpyHostToDevice = 0;
77+
const int cudaMemcpyDeviceToHost = 1;
78+
79+
cudaError_t cudaMemcpy(void *dst, void *src, size_t nbytes, int direction){
80+
memcpy(dst, src, nbytes);
81+
return cudaSuccess;
82+
}
83+
84+
char* cudaGetErrorString(cudaError_t err){
85+
return "";
86+
}
87+
88+
|]
89+
90+
#else
4091

4192
[C.emitBlock|
4293
__global__ void
@@ -51,6 +102,8 @@ vectorAdd(const float *A, const float *B, float *C, int numElements)
51102
}
52103
|]
53104

105+
#endif
106+
54107
cudaAllocaArray :: forall b. Int -> (Ptr C.CFloat -> IO b) -> IO b
55108
cudaAllocaArray size func = do
56109
let csize = fromIntegral size
@@ -121,11 +174,19 @@ main = Hspec.hspec $ do
121174
} |]
122175
cudaMemcpyHostToDevice numElements h_A d_A
123176
cudaMemcpyHostToDevice numElements h_B d_B
177+
#ifdef TEST_WITHOUT_CUDA
178+
[C.block| void {
179+
const int threadsPerBlock = 256;
180+
const int blocksPerGrid =($(int cNumElements) + threadsPerBlock - 1) / threadsPerBlock;
181+
vectorAdd(blocksPerGrid, threadsPerBlock, $(float* d_A), $(float* d_B), $(float* d_C), $(int cNumElements));
182+
} |]
183+
#else
124184
[C.block| void {
125-
int threadsPerBlock = 256;
126-
int blocksPerGrid =($(int cNumElements) + threadsPerBlock - 1) / threadsPerBlock;
185+
const int threadsPerBlock = 256;
186+
const int blocksPerGrid =($(int cNumElements) + threadsPerBlock - 1) / threadsPerBlock;
127187
vectorAdd<<<blocksPerGrid, threadsPerBlock>>>($(float* d_A), $(float* d_B), $(float* d_C), $(int cNumElements));
128188
} |]
189+
#endif
129190
cudaMemcpyDeviceToHost numElements d_C h_C
130191
lA <- peekArray numElements h_A
131192
lB <- peekArray numElements h_B

stack-lts-20.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ packages:
33
- inline-c
44
- inline-c-cpp
55
- inline-c-objc
6+
- inline-c-cuda
67
- sample-cabal-project

stack-lts-21.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ packages:
33
- inline-c
44
- inline-c-cpp
55
- inline-c-objc
6+
- inline-c-cuda
67
- sample-cabal-project

stack-nightly.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ packages:
33
- inline-c
44
- inline-c-cpp
55
- inline-c-objc
6+
- inline-c-cuda
67
- sample-cabal-project

0 commit comments

Comments
 (0)