KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I have an application that spends about 80% of its time computing the centroid of a large list (10^7) of high dimensional vectors (dim=100) using the Kahan summation algorithm . I have done my best at optimizing the summation, but it is still 20x slower than an equivalent C implementation. Profiling indicates that the culprits are the unsafeRead and unsafeWrite functions from Data.Vector.Unboxed.Mutable . My question is: are these functions really this slow or am I misunderstanding the profiling statistics? Here are the two implementations. The Haskell one is compiled with ghc-7.0.3 using the llvm backend. The C one is compiled with llvm-gcc. Kahan summation in Haskell: {-# LANGUAGE BangPatterns #-} module Test where import Control.Monad ( mapM_ ) import Data.Vector.Unboxed ( Vector, Unbox ) import Data.Vector.Unboxed.Mutable ( MVector ) import qualified Data.Vector.Unboxed as U import qualified Data.Vector.Unboxed.Mutable as UM import Data.Word ( Word ) import Data.Bits ( shiftL, shiftR, xor ) prng :: Word -> Word prng w = w' where !w1 = w `xor` (w `shiftL` 13) !w2 = w1 `xor` (w1 `shiftR` 7) !w' = w2 `xor` (w2 `shiftL` 17) mkVect :: Word -> Vector Double mkVect = U.force . U.map fromIntegral . U.fromList . take 100 . iterate prng foldV :: (Unbox a, Unbox b) => (a -> b -> a) -- componentwise function to fold -> Vector a -- initial accumulator value -> [Vector b] -- data vectors -> Vector a -- final accumulator value foldV fn accum vs = U.modify (\x -> mapM_ (liftV fn x) vs) accum where liftV f acc = fV where fV v = go 0 where n = min (U.length v) (UM.length acc) go i | i < n = step >> go (i + 1) | otherwise = return () where
Tags (comma-separated)
Save Edits
Cancel