From 5d3e5c2d001e5724629b7d4e2d88f7bb8ebc6c59 Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Sun, 26 May 2024 14:59:06 +0200 Subject: Benchmark with haskell num ops via mutable vectors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Conclusions: - orthotope zipWith (+) -> zipWithT: 26.9 ms ± 1.6 ms - Data.Vector.Storable.zipWith (+): 10.1 ms ± 945 μs - hsaddDoubleVV (this commit): 1.52 ms ± 49 μs - cbits/arith.c: 1.29 ms ± 116 μs - hmatrix: 1.25 ms ± 49 μs --- src/Data/Array/Nested/Internal/Arith.hs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'src/Data/Array') diff --git a/src/Data/Array/Nested/Internal/Arith.hs b/src/Data/Array/Nested/Internal/Arith.hs index 07d5d8a..042c9d0 100644 --- a/src/Data/Array/Nested/Internal/Arith.hs +++ b/src/Data/Array/Nested/Internal/Arith.hs @@ -6,9 +6,10 @@ {-# LANGUAGE TypeOperators #-} {-# LANGUAGE ViewPatterns #-} {-# OPTIONS_GHC -fplugin GHC.TypeLits.KnownNat.Solver #-} +-- {-# OPTIONS_GHC -ddump-simpl -ddump-to-file -ddump-file-prefix=Arith #-} module Data.Array.Nested.Internal.Arith where -import Control.Monad (forM, guard) +import Control.Monad (forM, forM_, guard) import qualified Data.Array.Internal as OI import qualified Data.Array.Internal.RankedG as RG import qualified Data.Array.Internal.RankedS as RS @@ -309,8 +310,26 @@ instance NumElt Float where numEltSum1Inner = sum1VectorFloat numEltProduct1Inner = product1VectorFloat +hsaddDoubleSV :: Double -> VS.Vector Double -> VS.Vector Double +hsaddDoubleSV = error "unimplemented" + +{-# NOINLINE hsaddDoubleVV #-} +hsaddDoubleVV :: VS.Vector Double -> VS.Vector Double -> VS.Vector Double +-- hsaddDoubleVV = VS.zipWith (+) +hsaddDoubleVV v1 v2 = unsafePerformIO $ do + let n = min (VS.length v1) (VS.length v2) + dest <- VSM.unsafeNew n + forM_ [0 .. n - 1] $ \i -> do + VSM.write dest i (v1 VS.! i + v2 VS.! i) + VS.unsafeFreeze dest + instance NumElt Double where - numEltAdd = addVectorDouble + numEltAdd = \sn -> liftVEltwise2 sn $ \cases + (Left x) (Left y) -> VS.singleton (x + y) + (Left x) (Right vy) -> hsaddDoubleSV x vy + (Right vx) (Left y) -> hsaddDoubleSV y vx + (Right vx) (Right vy) -> hsaddDoubleVV vx vy + -- numEltAdd = addVectorDouble numEltSub = subVectorDouble numEltMul = mulVectorDouble numEltNeg = negVectorDouble -- cgit v1.2.3-70-g09d2