Skip to content

Commit b7fac0f

Browse files
committed
Added batch suite of functions.
- Additional tests - Added doctests - Added docs + examples - Added .ghci - Updated .gitignore - More bash functions in shell.nix
1 parent c5fe0e4 commit b7fac0f

27 files changed

+892
-423
lines changed

.ghci

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
:set prompt "\x03BB> "
2+
:seti -XTypeApplications
3+
:load ArrayFire
4+
:set -laf

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ result/
44
/result
55
*~
66
/ctags
7+
cabal.project.local
8+
tags

README.md

+35-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
## <a href="http://arrayfire.com/"><img src="http://arrayfire.com/logos/arrayfire_logo_whitebkgnd.png" width="300"></a>
22
`ArrayFire` is a general-purpose library that simplifies the process of developing software that targets parallel and massively-parallel architectures including CPUs, GPUs, and other hardware acceleration devices.
33

4-
`arrayfire-haskell` is a [Haskell](https://haskell.org) binding for [ArrayFire](https://arrayfire.com).
4+
`arrayfire-haskell` is a [Haskell](https://haskell.org) binding to [ArrayFire](https://arrayfire.com).
55

66
## Table of Contents
77
- [Installation](#Installation)
@@ -25,31 +25,59 @@ To hack on this library locally, complete the installation step above. We recomm
2525

2626
After the above tools are installed, clone the source from Github.
2727

28-
``bash
28+
```bash
2929
git clone [email protected]:arrayfire/arrayfire-haskell.git
3030
cd arrayfire-haskell
3131
```
3232

33-
To build and run all tests in response to file changes.
33+
To build and run all tests in response to file changes
3434

3535
```bash
3636
nix-shell --run test-runner
3737
```
3838

39-
To perform interactive development w/ `ghcid`.
39+
To perform interactive development w/ `ghcid`
4040

4141
```bash
4242
nix-shell --run ghcid
4343
```
4444

45+
To interactively evaluate code in the `repl`
46+
47+
```bash
48+
nix-shell --run repl
49+
```
50+
51+
To run the doctests on each file change
52+
53+
```bash
54+
nix-shell --run doctest-runner
55+
```
56+
57+
To produce the haddocks and open them in a browser
58+
59+
```bash
60+
nix-shell --run docs
61+
```
62+
63+
4564
## Example
4665
```haskell
4766
module Main where
4867

4968
import qualified ArrayFire as A
5069

5170
main :: IO ()
52-
main = A.printArray action `catch` (\(e :: A.AFException) -> print e)
71+
main = print newArray `catch` (\(e :: A.AFException) -> print e)
5372
where
54-
action = A.matrix @Double (3,3) [1.0 ..] `A.mul` A.matrix @Double (3,3) [1.0 ..]
55-
```
73+
newArray = A.matrix @Double (2,2) [ [1..], [1..] ] * A.matrix @Double (2,2) [ [2..], [2..] ]
74+
75+
{-|
76+
77+
ArrayFire Array
78+
[2 2 1 1]
79+
2.0000 6.0000
80+
2.0000 6.0000
81+
82+
-}
83+
```

Setup.hs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1-
import Distribution.Simple
2-
main = defaultMain
1+
module Main where
2+
3+
import Distribution.Extra.Doctest (defaultMainWithDoctests)
4+
5+
main :: IO ()
6+
main = defaultMainWithDoctests "doctests"

arrayfire.cabal

+24-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ author: David Johnson
88
maintainer: [email protected]
99
copyright: David Johnson (c) 2018-2020
1010
category: Math
11-
build-type: Simple
11+
build-type: Custom
1212
extra-source-files: CHANGELOG.md
1313
cabal-version: >=1.10
1414
description: High-level Haskell bindings to the ArrayFire General-purpose GPU library
@@ -21,6 +21,12 @@ flag disable-default-paths
2121
default: False
2222
manual: True
2323

24+
custom-setup
25+
setup-depends:
26+
base <5,
27+
Cabal,
28+
cabal-doctest >=1 && <1.1
29+
2430
library
2531
exposed-modules:
2632
ArrayFire
@@ -80,7 +86,7 @@ library
8086
hs-source-dirs:
8187
src
8288
ghc-options:
83-
-Wall
89+
-Wall -Wno-missing-home-modules
8490
default-language:
8591
Haskell2010
8692

@@ -90,9 +96,7 @@ library
9096
extra-lib-dirs:
9197
/opt/arrayfire/lib
9298
ld-options:
93-
-rpath /opt/arrayfire/lib
94-
cc-options:
95-
-fPIC
99+
-Wl -rpath /opt/arrayfire/lib
96100

97101
executable main
98102
hs-source-dirs:
@@ -157,3 +161,18 @@ test-suite test
157161
ArrayFire.StatisticsSpec
158162
ArrayFire.UtilSpec
159163
ArrayFire.VisionSpec
164+
165+
test-suite doctests
166+
type:
167+
exitcode-stdio-1.0
168+
ghc-options:
169+
-threaded
170+
main-is:
171+
Main.hs
172+
hs-source-dirs:
173+
doctest
174+
build-depends:
175+
base, doctest >= 0.8
176+
default-language:
177+
Haskell2010
178+

default.nix

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
{ pkgs ? import <nixpkgs> {} }:
2-
pkgs.haskellPackages.callPackage ./pkg.nix {}
2+
# Latest arrayfire is not yet procured w/ nix.
3+
pkgs.haskellPackages.callCabal2nix "arrayfire" ./. { af = null; }

doctest/Main.hs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module Main (main) where
2+
3+
import Build_doctests (flags, pkgs, module_sources)
4+
import Test.DocTest (doctest)
5+
6+
main :: IO ()
7+
main = doctest (flags ++ pkgs ++ module_sources)

exe/Main.hs

+16-15
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
module Main where
66

77
import ArrayFire
8-
import Prelude hiding (sum, product)
8+
import Control.Concurrent
9+
import Prelude hiding (sum, product)
910
-- import GHC.RTS
1011

1112
foreign import ccall safe "test_bool"
@@ -16,11 +17,11 @@ foreign import ccall safe "test_window"
1617

1718
main :: IO ()
1819
main = do
19-
testBool >> testWindow
20-
-- ks <- randn @'(100,100) @Double
21-
-- saveArray 0 "key" ks "array.txt" False
22-
-- !ks' <- readArrayKey "array.txt" "key"
23-
-- print ks'
20+
-- testWindow
21+
-- ks <- randn @Double [100,100]
22+
-- saveArray "key" ks "array.txt" False
23+
-- !ks' <- readArrayKey "array.txt" "key"
24+
-- print ks'
2425

2526
-- info >> putStrLn "ok" >> afInit
2627
-- -- Info things
@@ -70,13 +71,13 @@ main = do
7071
-- putStrLn "got one"
7172
-- print e)
7273

73-
-- putStrLn "create window"
74-
-- window <- createWindow 200 200 "hey"
75-
-- putStrLn "set visibility"
76-
-- setVisibility window True
77-
-- putStrLn "show window"
78-
-- showWindow window
79-
74+
putStrLn "create window"
75+
window <- createWindow 200 200 "hey"
76+
putStrLn "set visibility"
77+
setVisibility window True
78+
putStrLn "show window"
79+
showWindow window
80+
threadDelay (secs 10)
8081

8182
-- -- print =<< getActiveBackend
8283
-- -- print =<< getDeviceCount
@@ -88,5 +89,5 @@ main = do
8889
-- -- print =<< getVersion
8990

9091

91-
-- secs :: Int -> Int
92-
-- secs = (*1000000)
92+
secs :: Int -> Int
93+
secs = (*1000000)

shell.nix

+14
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ let
44
in
55
pkgs.lib.overrideDerivation pkg (drv: {
66
shellHook = ''
7+
export PATH=$PATH:${pkgs.haskellPackages.doctest}/bin
78
function ghcid () {
89
${pkgs.haskellPackages.ghcid.bin}/bin/ghcid -c 'cabal v1-repl lib:arrayfire'
910
};
@@ -13,5 +14,18 @@ in
1314
'cabal v1-configure --enable-tests && \
1415
cabal v1-build test && dist/build/test/test'
1516
}
17+
function doctest-runner () {
18+
${pkgs.ag}/bin/ag -l | \
19+
${pkgs.entr}/bin/entr sh -c \
20+
'cabal v1-configure --enable-tests && \
21+
cabal v1-build doctests && dist/build/doctests/doctests'
22+
}
23+
function repl () {
24+
${pkgs.cabal-install}/bin/cabal v1-repl lib:arrayfire
25+
}
26+
function docs () {
27+
${pkgs.cabal-install}/bin/cabal haddock
28+
open ./dist-newstyle/*/*/*/*/doc/html/arrayfire/index.html
29+
}
1630
'';
1731
})

src/ArrayFire.hs

+5-3
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ import Data.Word
7171
-- import qualified ArrayFire as A
7272
--
7373
-- main :: IO ()
74-
-- main = A.printArray $ A.matrix @Double (3,3) [1.0 .. ]
74+
-- main = print $ A.matrix @Double (2,2) [[1,2],[3,4]]
7575
-- @
7676
--
7777
-- * Exception Handling
@@ -84,9 +84,11 @@ import Data.Word
8484
-- import Control.Exception ( catch )
8585
--
8686
-- main :: IO ()
87-
-- main = A.printArray (action shouldBatchOperation) \`catch\` (\\(e :: A.AFException) -> print e)
87+
-- main = A.printArray action \`catch\` (\\(e :: A.AFException) -> print e)
8888
-- where
89-
-- action = A.matrix \@Double (3,3) [1.0 .. ] \`A.mul\` A.matrix \@Double (2,2) [1.0 .. ]
89+
-- action =
90+
-- A.matrix \@Double (3,3) [[[1..],[1..],[1..]]]
91+
-- \`A.mul\` A.matrix \@Double (2,2) [[1..],[1..]]
9092
-- @
9193
--
9294
-- The above operation is invalid since the matrix multiply has improper dimensions. The caught exception produces the following error:

0 commit comments

Comments
 (0)