mirror of
https://github.com/ekmett/speculation.git
synced 2024-07-14 16:50:23 +03:00
1.3 release candidate
This commit is contained in:
parent
3d0d9edb9a
commit
7b5ececdfc
@ -1,22 +1,39 @@
|
||||
/1.3/
|
||||
|
||||
* Removed old benchmark/test framework.
|
||||
* Make numSparks support mandatory.
|
||||
* Moved to Control.Concurrent from Data
|
||||
* Added MonadSpec, so we can make instances for Codensity, etc. in other packages
|
||||
* Removed the ContT r STM combinators
|
||||
* Cleaned out old issues
|
||||
* Made compatible with the removal of the Eq/Show superclasses of Num in GHC 7.3+
|
||||
|
||||
/1.2.0.2/
|
||||
|
||||
* Fixed name collision with the new Distribution.Simple.testHook in Setup.lhs
|
||||
|
||||
/1.2.0.1/
|
||||
|
||||
* Weakened dependencies
|
||||
|
||||
/1.2.0.0/:
|
||||
* Reorganized the module hierarchy into Data.Speculation.*
|
||||
|
||||
* Reorganized the module hierarchy into Data.Speculation
|
||||
|
||||
/1.1.0.0/:
|
||||
|
||||
* Added support for numSparks
|
||||
|
||||
/1.0.0.0/:
|
||||
|
||||
* Released
|
||||
|
||||
/0.9.0/:
|
||||
|
||||
* Removed interim boxing in the unsafeIsEvaluated and unsafeGetTagBits check
|
||||
|
||||
/0.8.1/:
|
||||
|
||||
* Added Data.List.Foldable
|
||||
* Added Data.Traversable.Foldable
|
||||
* Fixed an off-by-one error in the arguments to the speculative fold estimators
|
||||
@ -26,5 +43,5 @@
|
||||
* changed tests and benchmarks to not build by default to work around corruption in the hackage db
|
||||
|
||||
/0.8.0.1/:
|
||||
|
||||
|
||||
* test suite now forces build
|
||||
|
@ -1,5 +1,16 @@
|
||||
{-# LANGUAGE CPP, BangPatterns, DeriveDataTypeable, MagicHash #-}
|
||||
module Data.Speculation
|
||||
{-# LANGUAGE CPP #-}
|
||||
-----------------------------------------------------------------------------
|
||||
-- |
|
||||
-- Module : Control.Concurrent.Speculation
|
||||
-- Copyright : (C) 2008-2011 Edward Kmett,
|
||||
-- License : BSD-style (see the file LICENSE)
|
||||
--
|
||||
-- Maintainer : Edward Kmett <ekmett@gmail.com>
|
||||
-- Stability : provisional
|
||||
-- Portability : portable
|
||||
--
|
||||
----------------------------------------------------------------------------
|
||||
module Control.Concurrent.Speculation
|
||||
(
|
||||
-- * Speculative application
|
||||
spec
|
||||
@ -18,7 +29,7 @@ module Data.Speculation
|
||||
) where
|
||||
|
||||
import Control.Concurrent.STM
|
||||
import Data.Speculation.Internal (returning)
|
||||
import Control.Concurrent.Speculation.Internal (returning)
|
||||
import Data.TagBits (unsafeIsEvaluated)
|
||||
import Control.Monad (liftM2, unless)
|
||||
import Data.Function (on)
|
||||
@ -86,7 +97,7 @@ specBy cmp guess f a
|
||||
specBy' :: (a -> a -> Bool) -> a -> (a -> b) -> a -> b
|
||||
specBy' cmp guess f a
|
||||
| numCapabilities == 1 = f $! a
|
||||
| otherwise = speculation `par`
|
||||
| otherwise = speculation `par`
|
||||
if cmp guess a
|
||||
then speculation
|
||||
else f a
|
||||
@ -162,16 +173,11 @@ specBySTM cmp guess f a
|
||||
| otherwise = specBySTM' cmp guess f a
|
||||
{-# INLINE specBySTM #-}
|
||||
|
||||
#ifndef HAS_NUM_SPARKS
|
||||
numSparks :: IO Int
|
||||
numSparks = return 0
|
||||
#endif
|
||||
|
||||
-- | 'specSTM'' using a user defined comparison function
|
||||
specBySTM' :: (a -> a -> STM Bool) -> STM a -> (a -> STM b) -> a -> STM b
|
||||
specBySTM' cmp mguess f a = do
|
||||
sparks <- unsafeIOToSTM numSparks
|
||||
if sparks < numCapabilities
|
||||
if sparks < numCapabilities
|
||||
then a `par` do
|
||||
guess <- mguess
|
||||
result <- f guess
|
||||
@ -181,7 +187,7 @@ specBySTM' cmp mguess f a = do
|
||||
return result
|
||||
`orElse`
|
||||
f a
|
||||
else f $! a
|
||||
else f $! a
|
||||
{-# INLINE specBySTM' #-}
|
||||
|
||||
-- | @'specBySTM' . 'on' (==)@
|
52
Control/Concurrent/Speculation/Class.hs
Normal file
52
Control/Concurrent/Speculation/Class.hs
Normal file
@ -0,0 +1,52 @@
|
||||
-----------------------------------------------------------------------------
|
||||
-- |
|
||||
-- Module : Control.Concurrent.Speculation.Class
|
||||
-- Copyright : (C) 2011 Edward Kmett, Jake McArthur
|
||||
-- License : BSD-style (see the file LICENSE)
|
||||
--
|
||||
-- Maintainer : Edward Kmett <ekmett@gmail.com>
|
||||
-- Stability : provisional
|
||||
-- Portability : portable
|
||||
--
|
||||
-- Versions of the combinators from the 'speculation' package
|
||||
-- with the signature rearranged to enable them to be used
|
||||
-- directly as actions in the 'Cont' and 'ContT' monads
|
||||
-- or any other 'Codensity'-shaped monad.
|
||||
----------------------------------------------------------------------------
|
||||
module Control.Concurrent.Speculation.Class where
|
||||
|
||||
import Control.Monad.Trans.Cont
|
||||
import Control.Concurrent.Speculation
|
||||
|
||||
class MonadSpec m where
|
||||
-- | When a is unevaluated, @'spec' g a@ evaluates the current continuation
|
||||
-- with @g@ while testing if @g@ '==' @a@, if they differ, it re-evalutes the
|
||||
-- continuation with @a@. If @a@ was already evaluated, the continuation is
|
||||
-- just directly applied to @a@ instead.
|
||||
specM :: Eq a => a -> a -> m a
|
||||
|
||||
-- | As per 'spec', without the check for whether or not the second argument
|
||||
-- is already evaluated.
|
||||
specM' :: Eq a => a -> a -> m a
|
||||
|
||||
-- | @spec@ with a user supplied comparison function
|
||||
specByM :: (a -> a -> Bool) -> a -> a -> m a
|
||||
|
||||
-- | @spec'@ with a user supplied comparison function
|
||||
specByM' :: (a -> a -> Bool) -> a -> a -> m a
|
||||
|
||||
-- | @spec'@ with a user supplied comparison function
|
||||
specOnM :: Eq c => (a -> c) -> a -> a -> m a
|
||||
|
||||
-- | @spec'@ with a user supplied comparison function
|
||||
specOnM' :: Eq c => (a -> c) -> a -> a -> m a
|
||||
|
||||
-- * Basic speculation
|
||||
|
||||
instance Monad m => MonadSpec (ContT r m) where
|
||||
specM g a = ContT $ \k -> spec g k a
|
||||
specM' g a = ContT $ \k -> spec' g k a
|
||||
specByM f g a = ContT $ \k -> specBy f g k a
|
||||
specByM' f g a = ContT $ \k -> specBy' f g k a
|
||||
specOnM f g a = ContT $ \k -> specOn f g k a
|
||||
specOnM' f g a = ContT $ \k -> specOn' f g k a
|
@ -1,5 +1,5 @@
|
||||
{-# LANGUAGE BangPatterns #-}
|
||||
module Data.Speculation.Foldable
|
||||
module Control.Concurrent.Speculation.Foldable
|
||||
(
|
||||
-- * Speculative folds
|
||||
fold, foldBy
|
||||
@ -55,8 +55,8 @@ import Data.Function (on)
|
||||
import Data.Foldable (Foldable)
|
||||
import qualified Data.Foldable as Foldable
|
||||
import Control.Concurrent.STM
|
||||
import Data.Speculation
|
||||
import Data.Speculation.Internal
|
||||
import Control.Concurrent.Speculation
|
||||
import Control.Concurrent.Speculation.Internal
|
||||
import Control.Applicative
|
||||
import Control.Monad hiding (mapM_, msum, forM_, sequence_)
|
||||
|
||||
@ -349,7 +349,7 @@ any :: Foldable t => (Int -> Bool) -> (a -> Bool) -> t a -> Bool
|
||||
any g p = getAny . foldMap (Any . g) (Any . p)
|
||||
{-# INLINE any #-}
|
||||
|
||||
sum :: (Foldable t, Num a) => (Int -> a) -> t a -> a
|
||||
sum :: (Foldable t, Eq a, Num a) => (Int -> a) -> t a -> a
|
||||
sum = sumBy (==)
|
||||
{-# INLINE sum #-}
|
||||
|
||||
@ -357,7 +357,7 @@ sumBy :: (Foldable t, Num a) => (a -> a -> Bool) -> (Int -> a) -> t a -> a
|
||||
sumBy cmp g = getSum . foldMapBy (on cmp getSum) (Sum . g) Sum
|
||||
{-# INLINE sumBy #-}
|
||||
|
||||
product :: (Foldable t, Num a) => (Int -> a) -> t a -> a
|
||||
product :: (Foldable t, Eq a, Num a) => (Int -> a) -> t a -> a
|
||||
product = productBy (==)
|
||||
{-# INLINE product #-}
|
||||
|
@ -1,4 +1,15 @@
|
||||
module Data.Speculation.Internal
|
||||
-----------------------------------------------------------------------------
|
||||
-- |
|
||||
-- Module : Control.Concurrent.Speculation.Internal
|
||||
-- Copyright : (C) 2010-2011 Edward Kmett
|
||||
-- License : BSD-style (see the file LICENSE)
|
||||
--
|
||||
-- Maintainer : Edward Kmett <ekmett@gmail.com>
|
||||
-- Stability : provisional
|
||||
-- Portability : portable
|
||||
--
|
||||
----------------------------------------------------------------------------
|
||||
module Control.Concurrent.Speculation.Internal
|
||||
( Acc(..)
|
||||
, extractAcc
|
||||
, MaybeAcc(..)
|
@ -1,6 +1,16 @@
|
||||
{-# LANGUAGE BangPatterns #-}
|
||||
module Data.Speculation.List
|
||||
(
|
||||
-----------------------------------------------------------------------------
|
||||
-- |
|
||||
-- Module : Control.Concurrent.Speculation.List
|
||||
-- Copyright : (C) 2010-2011 Edward Kmett,
|
||||
-- License : BSD-style (see the file LICENSE)
|
||||
--
|
||||
-- Maintainer : Edward Kmett <ekmett@gmail.com>
|
||||
-- Stability : provisional
|
||||
-- Portability : portable
|
||||
--
|
||||
----------------------------------------------------------------------------
|
||||
module Control.Concurrent.Speculation.List
|
||||
(
|
||||
-- * Speculative scans
|
||||
scan, scanBy
|
||||
, scanMap, scanMapBy
|
||||
@ -19,7 +29,7 @@ module Data.Speculation.List
|
||||
) where
|
||||
|
||||
|
||||
import Prelude hiding
|
||||
import Prelude hiding
|
||||
(foldl, foldl1, foldr, foldr1
|
||||
, any, all, and, or, mapM_, sequence_
|
||||
, elem, notElem, sum, product
|
||||
@ -29,13 +39,13 @@ import Prelude hiding
|
||||
|
||||
import Data.Monoid
|
||||
import qualified Data.List as List
|
||||
import Data.Speculation
|
||||
import Data.Speculation.Internal
|
||||
import Control.Concurrent.Speculation
|
||||
import Control.Concurrent.Speculation.Internal
|
||||
|
||||
-- | Given a valid estimator @g@, @'scan' g xs@ converts @xs@ into a list of the prefix sums.
|
||||
--
|
||||
--
|
||||
-- @g n@ should supply an estimate of the value of the monoidal summation over the first @n@ elements of the container.
|
||||
--
|
||||
--
|
||||
-- If @g n@ is accurate a reasonable percentage of the time and faster to compute than the prefix sum, then this can
|
||||
-- provide increased opportunities for parallelism.
|
||||
|
||||
@ -49,9 +59,9 @@ scanBy cmp g = scanrBy cmp g mappend mempty
|
||||
{-# INLINE scanBy #-}
|
||||
|
||||
-- | Given a valid estimator @g@, @'scanMap' g f xs@ converts @xs@ into a list of the prefix sums.
|
||||
--
|
||||
--
|
||||
-- @g n@ should supply an estimate of the value of the monoidal summation over the first @n@ elements of the container.
|
||||
--
|
||||
--
|
||||
-- If @g n@ is accurate a reasonable percentage of the time and faster to compute than the scan, then this can
|
||||
-- provide increased opportunities for parallelism.
|
||||
--
|
||||
@ -79,13 +89,13 @@ scanr = scanrBy (==)
|
||||
|
||||
scanrBy :: (b -> b -> Bool) -> (Int -> b) -> (a -> b -> b) -> b -> [a] -> [b]
|
||||
scanrBy cmp g f z = map extractAcc . List.scanr mf (Acc 0 z)
|
||||
where
|
||||
where
|
||||
mf a (Acc n b) = let n' = n + 1 in Acc n' (specBy' cmp (g n') (f a) b)
|
||||
{-# INLINE scanrBy #-}
|
||||
|
||||
|
||||
scanl :: Eq b => (Int -> b) -> (b -> a -> b) -> b -> [a] -> [b]
|
||||
scanl = scanlBy (==)
|
||||
scanl = scanlBy (==)
|
||||
{-# INLINE scanl #-}
|
||||
|
||||
scanlBy :: (b -> b -> Bool) -> (Int -> b) -> (b -> a -> b) -> b -> [a] -> [b]
|
||||
@ -95,7 +105,7 @@ scanlBy cmp g f z = map extractAcc . List.scanl mf (Acc 0 z)
|
||||
{-# INLINE scanlBy #-}
|
||||
|
||||
scanr1 :: Eq a => (Int -> a) -> (a -> a -> a) -> [a] -> [a]
|
||||
scanr1 = scanr1By (==)
|
||||
scanr1 = scanr1By (==)
|
||||
{-# INLINE scanr1 #-}
|
||||
|
||||
scanr1By :: (a -> a -> Bool) -> (Int -> a) -> (a -> a -> a) -> [a] -> [a]
|
||||
@ -115,67 +125,3 @@ scanl1By cmp g f xs = map (fromMaybeAcc undefined) $ List.scanl mf NothingAcc xs
|
||||
mf (JustAcc n a) b = let n' = n + 1 in JustAcc n' (specBy' cmp (g n') (`f` b) a)
|
||||
mf NothingAcc b = JustAcc 1 b
|
||||
{-# INLINE scanl1By #-}
|
||||
|
||||
{-
|
||||
scanlM :: (Monad m, Eq b) => (Int -> b) -> (b -> a -> m b) -> b -> [a] -> m [b]
|
||||
scanlM = scanlByM (==)
|
||||
{-# INLINE scanlM #-}
|
||||
|
||||
scanlByM :: Monad m => (b -> b -> Bool) -> (Int -> b) -> (b -> a -> m b) -> b -> [a] -> m [b]
|
||||
scanlByM cmp g f mz = liftM (map extractAcc) . List.scanl go (liftM (map (Acc 0)) mz)
|
||||
where
|
||||
go mia b = do
|
||||
Acc n a <- mia
|
||||
a' <- specBy' cmp (g n) (`f` b) a
|
||||
return (Acc (n + 1) a')
|
||||
{-# INLINE scanlByM #-}
|
||||
|
||||
scanrM :: (Monad m, Eq (m b)) => (Int -> m b) -> (a -> b -> m b) -> m b -> [a] -> m [b]
|
||||
scanrM = scanrByM (==)
|
||||
{-# INLINE scanrM #-}
|
||||
|
||||
scanrByM :: Monad m => (m b -> m b -> Bool) -> (Int -> m b) -> (a -> b -> m b) -> m b -> [a] -> m [b]
|
||||
scanrByM cmp g f mz = liftM (map extractAcc) . List.scanr go (liftM (map (Acc 0)) mz)
|
||||
where
|
||||
go a mib = do
|
||||
Acc n b <- mib
|
||||
let !n' = n + 1
|
||||
b' <- specBy' cmp (g n') (>>= f a) (return b)
|
||||
return (Acc n' b')
|
||||
{-# INLINE scanrByM #-}
|
||||
|
||||
scanlSTM :: Eq a => (Int -> STM a) -> (a -> b -> STM a) -> STM a -> [b] -> STM [a]
|
||||
scanlSTM = scanlBySTM (returning (==))
|
||||
{-# INLINE scanlSTM #-}
|
||||
|
||||
scanlBySTM :: (a -> a -> STM Bool) -> (Int -> STM a) -> (a -> b -> STM a) -> STM a -> [b] -> STM [a]
|
||||
scanlBySTM cmp g f mz = liftM (map extractAcc) . List.scanl go (liftM (Acc 0) mz)
|
||||
where
|
||||
go mia b = do
|
||||
Acc n a <- mia
|
||||
let !n' = n + 1
|
||||
a' <- specBySTM' cmp (g n') (`f` b) a
|
||||
return (Acc n' a')
|
||||
{-# INLINE scanlBySTM #-}
|
||||
|
||||
scanrSTM :: Eq b => (Int -> STM b) -> (a -> b -> STM b) -> STM b -> [a] -> STM [b]
|
||||
scanrSTM = scanrBySTM (returning (==))
|
||||
{-# INLINE scanrSTM #-}
|
||||
|
||||
scanrBySTM :: (b -> b -> STM Bool) -> (Int -> STM b) -> (a -> b -> STM b) -> STM b -> [a] -> STM [b]
|
||||
scanrBySTM cmp g f mz = liftM (map extractAcc) . List.scanr go (liftM (Acc 0) mz)
|
||||
where
|
||||
go a mib = do
|
||||
Acc n b <- mib
|
||||
let !n' = n + 1
|
||||
b' <- specBySTM' cmp (g n') (f a) b
|
||||
return (Acc n' b')
|
||||
{-# INLINE scanrBySTM #-}
|
||||
|
||||
-- | Given a valid estimator @g@, @'scanl' g f z xs@ yields the same answer as @'scanl'' f z xs@.
|
||||
--
|
||||
-- @g n@ should supply an estimate of the value returned from scaning over the first @n@ elements of the container.
|
||||
--
|
||||
-- If @g n@ is accurate a reasonable percentage of the time and faster to compute than the scan, then this can
|
||||
-- provide increased opportunities for parallelism.
|
||||
-}
|
@ -1,5 +1,16 @@
|
||||
{-# LANGUAGE MagicHash, Rank2Types, UnboxedTuples, BangPatterns #-}
|
||||
module Data.Speculation.Traversable
|
||||
-----------------------------------------------------------------------------
|
||||
-- |
|
||||
-- Module : Control.Concurrent.Speculation.Traversable
|
||||
-- Copyright : (C) 2010-2011 Edward Kmett,
|
||||
-- License : BSD-style (see the file LICENSE)
|
||||
--
|
||||
-- Maintainer : Edward Kmett <ekmett@gmail.com>
|
||||
-- Stability : provisional
|
||||
-- Portability : non-portable (UnboxedTuples, Rank2Types)
|
||||
--
|
||||
----------------------------------------------------------------------------
|
||||
module Control.Concurrent.Speculation.Traversable
|
||||
(
|
||||
-- * Traversable
|
||||
-- ** Applicative Traversals
|
||||
@ -13,7 +24,6 @@ module Data.Speculation.Traversable
|
||||
-- ** STM-based traversals with transactional rollback
|
||||
, mapSTM, mapBySTM
|
||||
, forSTM, forBySTM
|
||||
-- , sequenceSTM, sequenceBySTM
|
||||
-- * Accumulating parameters
|
||||
, mapAccumL, mapAccumLBy
|
||||
, mapAccumR, mapAccumRBy
|
||||
@ -26,8 +36,8 @@ import Data.Traversable (Traversable)
|
||||
import qualified Data.Traversable as Traversable
|
||||
import Control.Applicative
|
||||
import Control.Concurrent.STM
|
||||
import Data.Speculation
|
||||
import Data.Speculation.Internal
|
||||
import Control.Concurrent.Speculation
|
||||
import Control.Concurrent.Speculation.Internal
|
||||
|
||||
mapAccumL :: (Traversable t, Eq a) => (Int -> a) -> (a -> b -> (a, c)) -> a -> t b -> (a, t c)
|
||||
mapAccumL = mapAccumLBy (==)
|
||||
@ -36,7 +46,7 @@ mapAccumL = mapAccumLBy (==)
|
||||
mapAccumLBy :: Traversable t => (a -> a -> Bool) -> (Int -> a) -> (a -> b -> (a, c)) -> a -> t b -> (a, t c)
|
||||
mapAccumLBy cmp g f z xs = runIntAccumL (Traversable.traverse go xs) 0 z
|
||||
where
|
||||
go b = IntAccumL (\n a ->
|
||||
go b = IntAccumL (\n a ->
|
||||
let ~(a', c) = specBy' cmp (g (I# n)) (`f` b) a
|
||||
in (# n +# 1#, a', c #))
|
||||
{-# INLINE mapAccumLBy #-}
|
||||
@ -48,7 +58,7 @@ mapAccumR = mapAccumRBy (==)
|
||||
mapAccumRBy :: Traversable t => (a -> a -> Bool) -> (Int -> a) -> (a -> b -> (a, c)) -> a -> t b -> (a, t c)
|
||||
mapAccumRBy cmp g f z xs = runIntAccumR (Traversable.traverse go xs) 0 z
|
||||
where
|
||||
go b = IntAccumR (\n a ->
|
||||
go b = IntAccumR (\n a ->
|
||||
let ~(a', c) = specBy' cmp (g (I# n)) (`f` b) a
|
||||
in (# n +# 1#, a', c #))
|
||||
{-# INLINE mapAccumRBy #-}
|
||||
@ -82,7 +92,7 @@ mapBySTM cmp g f xs = unwrapMonad (runAccT (Traversable.traverse go xs) 0)
|
||||
go a = AccT $ \i -> acc (i +# 1#) $ WrapMonad $ specBySTM cmp (g (I# i)) f a
|
||||
{-# INLINE mapBySTM #-}
|
||||
|
||||
|
||||
|
||||
sequenceA :: (Traversable t, Applicative f, Eq (f a)) => (Int -> f a) -> t (f a) -> f (t a)
|
||||
sequenceA g = traverse g id
|
||||
{-# INLINE sequenceA #-}
|
||||
@ -153,9 +163,9 @@ instance Functor (IntAccumL s) where
|
||||
instance Applicative (IntAccumL s) where
|
||||
pure a = IntAccumL (\i s -> (# i, s, a #))
|
||||
IntAccumL mf <*> IntAccumL ma = IntAccumL (\i s ->
|
||||
case mf i s of
|
||||
case mf i s of
|
||||
(# i1, s1, f #) ->
|
||||
case ma i1 s1 of
|
||||
case ma i1 s1 of
|
||||
(# i2, s2, a #) -> (# i2, s2, f a #))
|
||||
|
||||
data IntAccumR s a = IntAccumR (Int# -> s -> (# Int#, s, a #))
|
||||
@ -172,9 +182,9 @@ instance Functor (IntAccumR s) where
|
||||
instance Applicative (IntAccumR s) where
|
||||
pure a = IntAccumR (\i s -> (# i, s, a #))
|
||||
IntAccumR mf <*> IntAccumR ma = IntAccumR (\i s ->
|
||||
case ma i s of
|
||||
case ma i s of
|
||||
(# i1, s1, a #) ->
|
||||
case mf i1 s1 of
|
||||
case mf i1 s1 of
|
||||
(# i2, s2, f #) -> (# i2, s2, f a #))
|
||||
|
||||
-- applicative composition with a strict integer state applicative
|
||||
@ -189,7 +199,7 @@ instance Functor f => Functor (AccT f) where
|
||||
|
||||
instance Applicative f => Applicative (AccT f) where
|
||||
pure a = AccT (\i -> Acc (I# i) (pure a))
|
||||
AccT mf <*> AccT ma = AccT (\i0# ->
|
||||
AccT mf <*> AccT ma = AccT (\i0# ->
|
||||
let !(Acc !(I# i1#) f) = mf i0#
|
||||
!(Acc i2 a) = ma i1#
|
||||
in Acc i2 (f <*> a))
|
@ -1,70 +0,0 @@
|
||||
-----------------------------------------------------------------------------
|
||||
-- |
|
||||
-- Module : Data.Speculation.Cont
|
||||
-- Copyright : (C) 2011 Edward Kmett, Jake McArthur
|
||||
-- License : BSD-style (see the file LICENSE)
|
||||
--
|
||||
-- Maintainer : Edward Kmett <ekmett@gmail.com>
|
||||
-- Stability : provisional
|
||||
-- Portability : portable
|
||||
--
|
||||
-- Versions of the combinators from the 'speculation' package
|
||||
-- with the signature rearranged to enable them to be used
|
||||
-- directly as actions in the 'Cont' and 'ContT' monads.
|
||||
----------------------------------------------------------------------------
|
||||
module Data.Speculation.Cont where
|
||||
|
||||
import Control.Monad.Trans.Cont
|
||||
import qualified Data.Speculation as Prim
|
||||
import Control.Concurrent.STM
|
||||
|
||||
-- * Basic speculation
|
||||
|
||||
-- | When a is unevaluated, @'spec' g a@ evaluates the current continuation
|
||||
-- with @g@ while testing if @g@ '==' @a@, if they differ, it re-evalutes the
|
||||
-- continuation with @a@. If @a@ was already evaluated, the continuation is
|
||||
-- just directly applied to @a@ instead.
|
||||
spec :: Eq a => a -> a -> ContT r m a
|
||||
spec g a = ContT $ \k -> Prim.spec g k a
|
||||
|
||||
-- | As per 'spec', without the check for whether or not the second argument
|
||||
-- is already evaluated.
|
||||
spec' :: Eq a => a -> a -> ContT r m a
|
||||
spec' g a = ContT $ \k -> Prim.spec' g k a
|
||||
|
||||
-- | @spec@ with a user supplied comparison function
|
||||
specBy :: (a -> a -> Bool) -> a -> a -> ContT r m a
|
||||
specBy f g a = ContT $ \k -> Prim.specBy f g k a
|
||||
|
||||
-- | @spec'@ with a user supplied comparison function
|
||||
specBy' :: (a -> a -> Bool) -> a -> a -> ContT r m a
|
||||
specBy' f g a = ContT $ \k -> Prim.specBy' f g k a
|
||||
|
||||
-- | @spec'@ with a user supplied comparison function
|
||||
specOn :: Eq c => (a -> c) -> a -> a -> ContT r m a
|
||||
specOn f g a = ContT $ \k -> Prim.specOn f g k a
|
||||
|
||||
-- | @spec'@ with a user supplied comparison function
|
||||
specOn' :: Eq c => (a -> c) -> a -> a -> ContT r m a
|
||||
specOn' f g a = ContT $ \k -> Prim.specOn' f g k a
|
||||
|
||||
-- * STM-based speculation
|
||||
|
||||
specSTM :: Eq a => STM a -> a -> ContT r STM a
|
||||
specSTM g a = ContT $ \k -> Prim.specSTM g k a
|
||||
|
||||
specSTM' :: Eq a => STM a -> a -> ContT r STM a
|
||||
specSTM' g a = ContT $ \k -> Prim.specSTM' g k a
|
||||
|
||||
specOnSTM :: Eq c => (a -> STM c) -> STM a -> a -> ContT r STM a
|
||||
specOnSTM f g a = ContT $ \k -> Prim.specOnSTM f g k a
|
||||
|
||||
specOnSTM' :: Eq c => (a -> STM c) -> STM a -> a -> ContT r STM a
|
||||
specOnSTM' f g a = ContT $ \k -> Prim.specOnSTM' f g k a
|
||||
|
||||
specBySTM :: (a -> a -> STM Bool) -> STM a -> a -> ContT r STM a
|
||||
specBySTM f g a = ContT $ \k -> Prim.specBySTM f g k a
|
||||
|
||||
specBySTM' :: (a -> a -> STM Bool) -> STM a -> a -> ContT r STM a
|
||||
specBySTM' f g a = ContT $ \k -> Prim.specBySTM' f g k a
|
||||
|
@ -1,31 +0,0 @@
|
||||
{-# LANGUAGE BangPatterns, MagicHash #-}
|
||||
module Data.Speculation.Morphism
|
||||
( hylo
|
||||
) where
|
||||
|
||||
import GHC.Prim
|
||||
import GHC.Types
|
||||
|
||||
import Data.Speculation
|
||||
|
||||
{-
|
||||
newtype Mu f = In { out :: f (Mu f) }
|
||||
|
||||
ana :: (Functor f, Eq a) => (Int -> a) -> (a -> f a) -> a -> Mu f
|
||||
ana g psi = go 0#
|
||||
where
|
||||
go n = In . fmap (go (n +# 1#)) . spec (g (I# n)) psi
|
||||
|
||||
apo :: (Functor f, Eq a) => (Int -> a) -> (a -> f (Either (Mu f) a)) -> a -> Mu f
|
||||
apo g psi = go 0#
|
||||
where
|
||||
go n = In . fmap (either id (go (n +# 1#))) . spec (g (I# n)) psi
|
||||
-}
|
||||
|
||||
-- | @'hylo' g phi psi@ is a hylomorphism using a speculative anamorphism, where
|
||||
-- @g n@ estimates the seed after n iterations of 'psi'.
|
||||
|
||||
hylo :: (Functor f, Eq a) => (Int -> a) -> (f b -> b) -> (a -> f a) -> a -> b
|
||||
hylo g phi psi = go 0#
|
||||
where
|
||||
go n = phi . fmap (go (n +# 1#)) . spec (g (I# n)) psi
|
@ -1,31 +1,3 @@
|
||||
sparking the right computation
|
||||
------------------------------
|
||||
|
||||
Currently the computation of 'spec g f a' does exactly what I want
|
||||
the computation of 'f g' happens in the spark queue through 'par'
|
||||
while the computation of 'a' happens in the foreground, as well as
|
||||
any (lazy) evaluation of 'f a'.
|
||||
|
||||
(TODO: flop the other of g and a in the cmp to force them in reverse order, to make g even lazier?)
|
||||
|
||||
However specSTM lacks this property. the 'par' has to launch the
|
||||
computation of 'a' because 'f g' is in STM. Can we play games with par
|
||||
to retain ownership of the STM transaction and move it into the par block,
|
||||
and rendezvous with it upon the completion of calculating 'a' ? Per Marlow,
|
||||
apparently not. This leads to a fallback plan:
|
||||
|
||||
sparking only when it makes sense
|
||||
---------------------------------
|
||||
|
||||
Another option (from talking with Simon Marlow) is to add a new primop:
|
||||
|
||||
numSparks# :: State# s -> (# Int#, State# s #)
|
||||
|
||||
which computes dequeElements(cap->spark) and to not speculate when that is too high.
|
||||
|
||||
Sadly, the cap and spark machinery is marked private by GHC, so this needs to become
|
||||
a ticket.
|
||||
|
||||
inconsistent use of the estimator function
|
||||
------------------------------------------
|
||||
|
||||
|
@ -29,7 +29,7 @@ If `g` is a good guess at the value of `a`, this is one way to induce parallelis
|
||||
|
||||
However, if `g` isn\'t available more cheaply than `a`, then this saves no work, and if `g` is wrong, you risk evaluating the function twice.
|
||||
spec a f a = f $! a
|
||||
|
||||
|
||||
The best-case timeline looks like:
|
||||
[---- f g ----]
|
||||
[----- a -----]
|
||||
@ -40,7 +40,7 @@ The worst-case timeline looks like:
|
||||
[----- a -----]
|
||||
[---- f a ----]
|
||||
[------- spec g f a -----------]
|
||||
|
||||
|
||||
Compare these to the timeline of `f $! a`:
|
||||
[---- a -----]
|
||||
[---- f a ----]
|
||||
@ -53,7 +53,7 @@ Speculative Folds (Data.Foldable.Speculation)
|
||||
---------------------------------------------
|
||||
|
||||
A speculative version of the combinators from `Data.Foldable` is provided as `Data.Foldable.Speculation`.
|
||||
|
||||
|
||||
Each combinator therein takes an extra argument that is used to speculate on the value of the list.
|
||||
|
||||
#### foldr
|
||||
|
@ -1,5 +1,5 @@
|
||||
name: speculation
|
||||
version: 1.2.0.2
|
||||
version: 1.3
|
||||
license: BSD3
|
||||
license-file: LICENSE
|
||||
author: Edward A. Kmett
|
||||
@ -10,7 +10,7 @@ category: Concurrency
|
||||
copyright: (c) 2010 Edward A. Kmett
|
||||
build-type: Custom
|
||||
cabal-version: >=1.6
|
||||
tested-with: GHC==6.12.1
|
||||
tested-with: GHC==6.12.1, GHC==7.3.20111017
|
||||
synopsis: A framework for safe, programmable, speculative parallelism
|
||||
description:
|
||||
A framework for safe, programmable, speculative parallelism, loosely based on:
|
||||
@ -23,7 +23,7 @@ description:
|
||||
of the transactional rollback machinery from the paper.
|
||||
.
|
||||
For example:
|
||||
.
|
||||
.
|
||||
@'spec' g f a@ evaluates @f g@ while forcing @a@, if @g == a@ then @f g@ is returned, otherwise @f a@ is evaluated and returned. Furthermore, if the argument has already been evaluated, we skip the @f g@ computation entirely. If a good guess at the value of @a@ is available, this is one way to induce parallelism in an otherwise sequential task. However, if the guess isn\'t available more cheaply than the actual answer, then this saves no work and if the guess is wrong, you risk evaluating the function twice. Under high load, since 'f g' is computed via the spark queue, the speculation will be skipped and you will obtain the same answer as 'f $! a'.
|
||||
.
|
||||
The best-case timeline looks like:
|
||||
@ -64,46 +64,26 @@ description:
|
||||
.
|
||||
'specSTM' provides a similar time table for STM actions, but also rolls back side-effects. The one unfortunate operational distinction is that it is forced to compute 'a' in the background thread and therefore degrades slightly less gracefully under load, although we mitigate this effect by only enqueuing if the number of sparks for the current capability is lower than the total number of capabilities, to try to avoid wasting time when all computational resources are in use.
|
||||
|
||||
extra-source-files:
|
||||
README.markdown
|
||||
CHANGELOG.markdown
|
||||
ISSUES.markdown
|
||||
extra-source-files:
|
||||
README.markdown
|
||||
CHANGELOG.markdown
|
||||
ISSUES.markdown
|
||||
|
||||
source-repository head
|
||||
type: git
|
||||
location: http://github.com/ekmett/speculation.git
|
||||
branch: master
|
||||
|
||||
flag HasNumSparks
|
||||
description: RTS provides GHC.Conc.numSparks
|
||||
default: True
|
||||
manual: False
|
||||
|
||||
flag tests
|
||||
description: Build the tests
|
||||
default: False
|
||||
|
||||
flag benchmarks
|
||||
description: Build the benchmarks
|
||||
default: False
|
||||
|
||||
flag optimize
|
||||
description: Enable optimizations for the library and benchmarks
|
||||
default: True
|
||||
|
||||
flag hpc
|
||||
description: Use HPC for tests
|
||||
default: True
|
||||
|
||||
library
|
||||
ghc-options: -Wall
|
||||
if flag(optimize)
|
||||
ghc-options: -funbox-strict-fields -O2 -fspec-constr -fdicts-cheap
|
||||
if flag(HasNumSparks)
|
||||
cpp-options: -DHAS_NUM_SPARKS
|
||||
build-depends: base >= 4.3 && < 6
|
||||
else
|
||||
build-depends: base >= 4 && < 4.3
|
||||
|
||||
build-depends: base >= 4.3 && < 6
|
||||
|
||||
build-depends:
|
||||
ghc-prim >= 0.2 && < 0.4,
|
||||
@ -112,74 +92,11 @@ library
|
||||
stm >= 2.1 && < 2.3
|
||||
|
||||
exposed-modules:
|
||||
Data.Speculation
|
||||
Data.Speculation.Cont
|
||||
Data.Speculation.Morphism
|
||||
Data.Speculation.Foldable
|
||||
Data.Speculation.Traversable
|
||||
Data.Speculation.List
|
||||
Control.Concurrent.Speculation
|
||||
Control.Concurrent.Speculation.Class
|
||||
Control.Concurrent.Speculation.Foldable
|
||||
Control.Concurrent.Speculation.Traversable
|
||||
Control.Concurrent.Speculation.List
|
||||
other-modules:
|
||||
Data.Speculation.Internal
|
||||
Control.Concurrent.Speculation.Internal
|
||||
|
||||
executable test-speculation
|
||||
main-is: Test.hs
|
||||
if !flag(tests)
|
||||
buildable: False
|
||||
else
|
||||
if flag(hpc)
|
||||
ghc-options: -fhpc
|
||||
x-hpc: true
|
||||
ghc-options: -Wall
|
||||
if flag(HasNumSparks)
|
||||
cpp-options: -DHAS_NUM_SPARKS
|
||||
build-depends: base >= 4.3 && < 6
|
||||
else
|
||||
build-depends: base >= 4 && < 4.3
|
||||
build-depends:
|
||||
ghc-prim >= 0.2 && < 0.4,
|
||||
tag-bits >= 0.1 && < 0.2,
|
||||
stm >= 2.1 && < 2.3,
|
||||
transformers >= 0.2.2.0 && < 0.3,
|
||||
containers >= 0.3.0 && < 0.5,
|
||||
test-framework >= 0.2.4 && < 0.3,
|
||||
test-framework-quickcheck >= 0.2.4 && < 0.3,
|
||||
test-framework-hunit >= 0.2.4 && < 0.3,
|
||||
QuickCheck >= 1.2.0.0 && < 1.3,
|
||||
HUnit >= 1.2.2.1 && < 1.3
|
||||
other-modules:
|
||||
Data.Speculation
|
||||
Data.Speculation.Cont
|
||||
Data.Speculation.Morphism
|
||||
Data.Speculation.Foldable
|
||||
Data.Speculation.Traversable
|
||||
Data.Speculation.List
|
||||
Data.Speculation.Internal
|
||||
|
||||
executable benchmark-speculation
|
||||
main-is: Benchmark.hs
|
||||
if !flag(benchmarks)
|
||||
buildable: False
|
||||
else
|
||||
ghc-options: -Wall -threaded
|
||||
if flag(optimize)
|
||||
ghc-options: -O2 -fspec-constr -funbox-strict-fields -fdicts-cheap
|
||||
if flag(HasNumSparks)
|
||||
cpp-options: -DHAS_NUM_SPARKS
|
||||
build-depends: base >= 4.3 && < 6
|
||||
else
|
||||
build-depends: base >= 4 && < 4.3
|
||||
build-depends:
|
||||
ghc-prim >= 0.2 && < 0.4,
|
||||
transformers >= 0.2.2.0 && < 0.3,
|
||||
tag-bits >= 0.1 && < 0.2,
|
||||
stm >= 2.1 && < 2.3,
|
||||
containers >= 0.3.0 && < 0.5,
|
||||
criterion >= 0.5 && < 0.6
|
||||
other-modules:
|
||||
Data.Speculation
|
||||
Data.Speculation.Cont
|
||||
Data.Speculation.Morphism
|
||||
Data.Speculation.Foldable
|
||||
Data.Speculation.Traversable
|
||||
Data.Speculation.List
|
||||
Data.Speculation.Internal
|
||||
|
Loading…
Reference in New Issue
Block a user