packages feed

halide-arrayfire (empty) → 0.0.2.0

raw patch · 5 files changed

+150/−0 lines, 5 filesdep +arrayfiredep +basedep +halide-arrayfire

Dependencies added: arrayfire, base, halide-arrayfire, halide-haskell, hspec

Files

+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2023, twesterhout++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++    * Redistributions of source code must retain the above copyright+      notice, this list of conditions and the following disclaimer.++    * Redistributions in binary form must reproduce the above+      copyright notice, this list of conditions and the following+      disclaimer in the documentation and/or other materials provided+      with the distribution.++    * Neither the name of twesterhout nor the names of other+      contributors may be used to endorse or promote products derived+      from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,22 @@+<h1 align="center">+halide-arrayfire+</h1>++<div align="center">+<br />++[![license](https://img.shields.io/github/license/twesterhout/halide-haskell.svg?style=flat-square)](LICENSE)++[![build](https://img.shields.io/github/actions/workflow/status/twesterhout/halide-haskell/ci.yml?style=flat-square)](https://github.com/twesterhout/halide-haskell/actions/workflows/ci.yml)+[![Hackage](https://img.shields.io/hackage/v/halide-arrayfire?style=flat-square)](https://hackage.haskell.org/package/halide-arrayfire)++</div>++This package integrates+[halide-haskell](https://github.com/twesterhout/halide-haskell/) with+[arrayfire](https://github.com/arrayfire/arrayfire-haskell) by implementing+an instance of `IsHalideBuffer` for the `Array` data type.++  - [X] CPU+  - [ ] CUDA (know how to do it, just need a bit of time)+  - [ ] OpenCL (no idea how, contributions are welcome!)
+ halide-arrayfire.cabal view
@@ -0,0 +1,47 @@+cabal-version:   3.0+name:            halide-arrayfire+version:         0.0.2.0+synopsis:        Integration between Halide and ArrayFire+description:+  This package provides instances of [Language.Halide.IsHalideBuffer](https://hackage.haskell.org/package/halide-haskell/docs/Language-Halide.html#t:IsHalideBuffer)+  for arrays from the [arrayfire](https://hackage.haskell.org/package/arrayfire) library.++homepage:        https://github.com/twesterhout/halide-haskell+license:         BSD-3-Clause+license-file:    LICENSE+author:          Tom Westerhout+maintainer:+  Tom Westerhout <14264576+twesterhout@users.noreply.github.com>++category:        Language+copyright:       2022-2023 Tom Westerhout+build-type:      Simple+extra-doc-files: README.md+tested-with:     GHC ==9.2.7 || ==9.4.4 || ==9.4.5++common warnings+  ghc-options:      -Wall+  default-language: GHC2021++library+  import:          warnings+  exposed-modules: Language.Halide.ArrayFire+  build-depends:+    , arrayfire       >=0.7.0.0  && <0.8+    , base            >=4.16.0.0 && <5+    , halide-haskell  >=0.0.2.0  && <0.1++  ghc-options:     -Wno-orphans+  hs-source-dirs:  src++test-suite halide-arrayfire-test+  import:         warnings+  type:           exitcode-stdio-1.0+  hs-source-dirs: test+  main-is:        Spec.hs+  build-depends:+    , arrayfire+    , base+    , halide-arrayfire+    , halide-haskell+    , hspec             >=2.9.7 && <3
+ src/Language/Halide/ArrayFire.hs view
@@ -0,0 +1,33 @@+{-# LANGUAGE DataKinds #-}+{-# LANGUAGE TypeFamilies #-}+{-# LANGUAGE UndecidableInstances #-}++module Language.Halide.ArrayFire () where++import ArrayFire (AFType, Array)+import ArrayFire qualified as AF+import Data.Proxy+import GHC.TypeLits+import Language.Halide++forceNumDims :: AFType a => Array a -> Int -> [Int]+forceNumDims arr n+  | AF.getNumDims arr <= n = take n shape+  | otherwise =+      error $+        "cannot treat a "+          <> show (AF.getNumDims arr)+          <> "-dimensional array as a "+          <> show n+          <> "-dimensional buffer"+  where+    shape = let (d0, d1, d2, d3) = AF.getDims arr in [d0, d1, d2, d3]++instance (IsHalideType a, AFType a, KnownNat n, n <= 4) => IsHalideBuffer (Array a) n a where+  withHalideBufferImpl arr action = case AF.getBackend arr of+    AF.CPU -> AF.withDevicePtr arr $ \ptr -> bufferFromPtrShape ptr shape action+    AF.CUDA -> undefined+    AF.OpenCL -> undefined+    AF.Default -> error "do not know how to handle 'Default' backend"+    where+      shape = forceNumDims arr . fromIntegral $ natVal (Proxy @n)
+ test/Spec.hs view
@@ -0,0 +1,18 @@+{-# LANGUAGE DataKinds #-}++module Main (main) where++import ArrayFire qualified as AF+import Language.Halide+import Language.Halide.ArrayFire+import Test.Hspec++main :: IO ()+main =+  hspec $+    describe "IsHalideBuffer" $ do+      it "supports CPU arrays" $ do+        let arr = 1 + AF.moddims (AF.range @Double [6] (-1)) [2, 3]+        -- print arr+        -- print =<< AF.getManualEvalFlag+        withHalideBuffer @2 @Double arr peekToList `shouldReturn` [[1, 3, 5], [2, 4, 6]]