massiv 0.3.2.1 → 0.3.3.0
raw patch · 8 files changed
+95/−26 lines, 8 filesdep +randomdep ~schedulerPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
Dependencies added: random
Dependency ranges changed: scheduler
API changes (from Hackage documentation)
+ Data.Massiv.Array: randomArray :: Index ix => g -> (g -> (g, g)) -> (g -> (e, g)) -> Comp -> Sz ix -> Array DL ix e
- Data.Massiv.Array.Mutable: createArray :: forall r ix e a m. (Mutable r ix e, PrimMonad m, MonadUnliftIO m) => Comp -> Sz ix -> (Scheduler m a -> MArray (PrimState m) r ix e -> m [a]) -> m ([a], Array r ix e)
+ Data.Massiv.Array.Mutable: createArray :: forall r ix e a m b. (Mutable r ix e, PrimMonad m, MonadUnliftIO m) => Comp -> Sz ix -> (Scheduler m a -> MArray (PrimState m) r ix e -> m b) -> m ([a], Array r ix e)
Files
- CHANGELOG.md +6/−0
- massiv.cabal +2/−1
- src/Data/Massiv/Array/Mutable.hs +2/−2
- src/Data/Massiv/Array/Ops/Construct.hs +59/−1
- src/Data/Massiv/Array/Ops/Map.hs +2/−8
- src/Data/Massiv/Array/Ops/Sort.hs +14/−3
- src/Data/Massiv/Core/Common.hs +5/−2
- src/Data/Massiv/Core/Iterator.hs +5/−9
CHANGELOG.md view
@@ -1,3 +1,9 @@+# 0.3.3++* Fix type signature for `createArray`.+* Support for new version of `scheduler`+* Addition of `randomArray`+ # 0.3.2.1 * Fix `sqrtA` function: [#76](https://github.com/lehins/massiv/pull/76)
massiv.cabal view
@@ -1,5 +1,5 @@ name: massiv-version: 0.3.2.1+version: 0.3.3.0 synopsis: Massiv (Массив) is an Array Library. description: Multi-dimensional Arrays with fusion, stencils and parallel computation. homepage: https://github.com/lehins/massiv@@ -144,6 +144,7 @@ , QuickCheck , massiv , template-haskell+ , random default-language: Haskell2010 source-repository head
src/Data/Massiv/Array/Mutable.hs view
@@ -348,10 +348,10 @@ -- @since 0.3.0 -- createArray ::- forall r ix e a m. (Mutable r ix e, PrimMonad m, MonadUnliftIO m)+ forall r ix e a m b. (Mutable r ix e, PrimMonad m, MonadUnliftIO m) => Comp -- ^ Computation strategy to use after `MArray` gets frozen and onward. -> Sz ix -- ^ Size of the newly created array- -> (Scheduler m a -> MArray (PrimState m) r ix e -> m [a])+ -> (Scheduler m a -> MArray (PrimState m) r ix e -> m b) -- ^ An action that should fill all elements of the brand new mutable array -> m ([a], Array r ix e) createArray comp sz action = do
src/Data/Massiv/Array/Ops/Construct.hs view
@@ -26,12 +26,16 @@ , makeArrayR , makeArrayLinearR , makeVectorR+ -- *** Iterating , iterateN , iiterateN+ -- *** Unfolding , unfoldlS_ , iunfoldlS_ , unfoldrS_ , iunfoldrS_+ -- *** Random+ , randomArray -- *** Applicative , makeArrayA , makeArrayAR@@ -57,7 +61,7 @@ ) where import Control.Applicative hiding (empty)-import Control.Monad (void)+import Control.Monad (when, void) import Control.Monad.ST import Data.Massiv.Array.Delayed.Pull import Data.Massiv.Array.Delayed.Push@@ -239,6 +243,60 @@ pure acc' } {-# INLINE iunfoldlS_ #-}+++-- | Create an array with random values by using a pure splittable random number generator+-- such as the one provided by [random](https://www.stackage.org/package/random) or+-- [splitmix](https://www.stackage.org/package/splitmix) packages.+--+-- Because of the pure nature of the generator and its splitability we are not only able+-- to parallelize the random value generation, but also guarantee that it will be+-- deterministic, granted none of the arguments are changed.+--+-- ==== __Examples__+--+-- >>> import Data.Massiv.Array+-- >>> import System.Random as System+-- >>> gen = System.mkStdGen 217+-- >>> randomArray gen System.split System.random (ParN 2) (Sz2 2 3) :: Array DL Ix2 Double+-- Array DL (ParN 2) (Sz (2 :. 3))+-- [ [ 0.15191527341922206, 0.2045537167404079, 0.9635356052820256 ]+-- , [ 9.308278528094238e-2, 0.7200934018606843, 0.23173694193083583 ]+-- ]+--+-- @since 0.3.3+randomArray ::+ Index ix+ => g -- ^ Initial random value generator+ -> (g -> (g, g))+ -- ^ A function that can split a generator in two independent generators+ -> (g -> (e, g))+ -- ^ A function that produces a random value and the next generator+ -> Comp -- ^ Computation strategy.+ -> Sz ix -- ^ Resulting size of the array.+ -> Array DL ix e+randomArray gen splitGen nextRandom comp sz =+ unsafeMakeLoadArray comp sz Nothing $ \scheduler startAt writeAt ->+ splitLinearly (numWorkers scheduler) totalLength $ \chunkLength slackStart -> do+ let slackStartAt = slackStart + startAt+ writeRandom k genII = do+ let (e, genII') = nextRandom genII+ writeAt k e+ pure genII'+ genForSlack <-+ loopM startAt (< slackStartAt) (+ chunkLength) gen $ \start genI -> do+ let (genI0, genI1) = splitGen genI+ scheduleWork_ scheduler $+ void $ loopM start (< (start + chunkLength)) (+ 1) genI0 writeRandom+ pure genI1+ when (slackStartAt < totalLength + startAt) $+ scheduleWork_ scheduler $+ void $+ loopM slackStartAt (< totalLength + startAt) (+ 1) genForSlack writeRandom+ where+ !totalLength = totalElem sz+{-# INLINE randomArray #-}+ infix 4 ..., ..:
src/Data/Massiv/Array/Ops/Map.hs view
@@ -561,14 +561,8 @@ -- -- @since 0.2.6 imapIO_ :: (Source r ix e, MonadUnliftIO m) => (ix -> e -> m a) -> Array r ix e -> m ()-imapIO_ action arr = do- let sz = size arr- withScheduler_ (getComp arr) $ \scheduler ->- splitLinearlyWith_- scheduler- (totalElem sz)- (unsafeLinearIndex arr)- (\i -> void . action (fromLinearIndex sz i))+imapIO_ action arr =+ withScheduler_ (getComp arr) $ \scheduler -> imapSchedulerM_ scheduler action arr {-# INLINE imapIO_ #-} -- | Same as `imapM_`, but will use the supplied scheduler.
src/Data/Massiv/Array/Ops/Sort.hs view
@@ -62,10 +62,21 @@ -- @since 0.3.2 quicksort :: (Mutable r Ix1 e, Ord e) => Array r Ix1 e -> Array r Ix1 e-quicksort arr =- unsafePerformIO $- withMArray arr (\n s -> quicksortM_ (trivialScheduler_ {numWorkers = n, scheduleWork = s}))+quicksort arr = unsafePerformIO $ withMArray' arr quicksortM_ {-# INLINE quicksort #-}++-- TODO: switch to the regular withMArray, once it is fixed.+withMArray' ::+ (Mutable r ix e, MonadUnliftIO m)+ => Array r ix e+ -> (Scheduler m () -> MArray RealWorld r ix e -> m a)+ -> m (Array r ix e)+withMArray' arr action = do+ marr <- thaw arr+ withScheduler_ (getComp arr) $ \scheduler -> action scheduler marr+ liftIO $ unsafeFreeze (getComp arr) marr+{-# INLINE withMArray' #-}+ -- | Mutable version of `quicksort` --
src/Data/Massiv/Core/Common.hs view
@@ -29,7 +29,10 @@ , Manifest(..) , Mutable(..) , Comp(..)- , Scheduler(..)+ , Scheduler+ , numWorkers+ , scheduleWork+ , scheduleWork_ , unsafeRead , unsafeWrite , unsafeLinearModify@@ -78,7 +81,7 @@ import Control.Monad.Catch (MonadThrow(..)) import Control.Monad.IO.Unlift (MonadIO(liftIO), MonadUnliftIO) import Control.Monad.Primitive-import Control.Scheduler (Comp(..), Scheduler(..))+import Control.Scheduler (Comp(..), Scheduler, numWorkers, scheduleWork, scheduleWork_) import Data.Massiv.Core.Exception import Data.Massiv.Core.Index import Data.Typeable
src/Data/Massiv/Core/Iterator.hs view
@@ -19,7 +19,7 @@ , splitLinearlyWithStartAtM_ ) where -import Control.Scheduler (Scheduler(..))+import Control.Scheduler (Scheduler, numWorkers, scheduleWork_) -- | Efficient loop with an accumulator --@@ -127,9 +127,9 @@ splitLinearlyWithM_ scheduler totalLength make write = splitLinearly (numWorkers scheduler) totalLength $ \chunkLength slackStart -> do loopM_ 0 (< slackStart) (+ chunkLength) $ \ !start ->- scheduleWork scheduler $+ scheduleWork_ scheduler $ loopM_ start (< (start + chunkLength)) (+ 1) $ \ !k -> make k >>= write k- scheduleWork scheduler $ loopM_ slackStart (< totalLength) (+ 1) $ \ !k -> make k >>= write k+ scheduleWork_ scheduler $ loopM_ slackStart (< totalLength) (+ 1) $ \ !k -> make k >>= write k {-# INLINE splitLinearlyWithM_ #-} @@ -141,12 +141,8 @@ splitLinearlyWithStartAtM_ scheduler startAt totalLength make write = splitLinearly (numWorkers scheduler) totalLength $ \chunkLength slackStart -> do loopM_ startAt (< (slackStart + startAt)) (+ chunkLength) $ \ !start ->- scheduleWork scheduler $+ scheduleWork_ scheduler $ loopM_ start (< (start + chunkLength)) (+ 1) $ \ !k -> make k >>= write k- scheduleWork scheduler $+ scheduleWork_ scheduler $ loopM_ (slackStart + startAt) (< (totalLength + startAt)) (+ 1) $ \ !k -> make k >>= write k {-# INLINE splitLinearlyWithStartAtM_ #-}----