Essentially sequential array conversions in Repa

I wonder if there is an analogue (//) in repa?

It is necessary for transformations of arrays that cannot be parallelized. For example, if a function requires that the entire array modify one entry of the array, and then apply to the new array and so on (and it must be run sequentially).

+3
source share
2 answers

(//) can be implemented in terms of Data.Array.Repa.fromFunction:

import Data.Array.Repa

(//) :: Shape sh => Array sh a -> [(sh,a)] -> Array sh a
(//) arr us = fromFunction (extent arr) (\sh -> case lookup sh us of
                                                 Just a  -> a
                                                 Nothing -> index arr sh)

fromFunctiona function of the type can be passed Shape sh => s -> a, which itself can use the whole array.

The above implementation performs all updates in a single pass.

+5
source

(//) , , . , .

Data.Vector:

modify :: Vector v a => (forall s. Mutable v s a -> ST s ()) -> v a -> v a

, . -

import Data.Vector.Unboxed as V
import Data.Vector.Mutable.Unboxed as M
import Data.Array.Repa as R

(///) :: Shape sh => Array sh a -> [(sh,a)] -> Array sh a
(///) arr us = R.fromVector sh . modify f $ R.toVector arr
  where
  sh = extent arr
  f mv = forM_ us $ \(k,x) -> do
    M.write mv (R.toIndex sh k) x

1- DIM1, 100 : (//): 3,598973 (///): 2.0859999999999997e-3

+4

All Articles