1.3 release candidate

This commit is contained in:
Edward Kmett 2011-11-09 00:26:35 -05:00
parent 3d0d9edb9a
commit 7b5ececdfc
12 changed files with 168 additions and 338 deletions

View File

@ -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/ /1.2.0.2/
* Fixed name collision with the new Distribution.Simple.testHook in Setup.lhs * Fixed name collision with the new Distribution.Simple.testHook in Setup.lhs
/1.2.0.1/ /1.2.0.1/
* Weakened dependencies * Weakened dependencies
/1.2.0.0/: /1.2.0.0/:
* Reorganized the module hierarchy into Data.Speculation.*
* Reorganized the module hierarchy into Data.Speculation
/1.1.0.0/: /1.1.0.0/:
* Added support for numSparks * Added support for numSparks
/1.0.0.0/: /1.0.0.0/:
* Released * Released
/0.9.0/: /0.9.0/:
* Removed interim boxing in the unsafeIsEvaluated and unsafeGetTagBits check * Removed interim boxing in the unsafeIsEvaluated and unsafeGetTagBits check
/0.8.1/: /0.8.1/:
* Added Data.List.Foldable * Added Data.List.Foldable
* Added Data.Traversable.Foldable * Added Data.Traversable.Foldable
* Fixed an off-by-one error in the arguments to the speculative fold estimators * 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 * changed tests and benchmarks to not build by default to work around corruption in the hackage db
/0.8.0.1/: /0.8.0.1/:
* test suite now forces build * test suite now forces build

View File

@ -1,5 +1,16 @@
{-# LANGUAGE CPP, BangPatterns, DeriveDataTypeable, MagicHash #-} {-# LANGUAGE CPP #-}
module Data.Speculation -----------------------------------------------------------------------------
-- |
-- 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 -- * Speculative application
spec spec
@ -18,7 +29,7 @@ module Data.Speculation
) where ) where
import Control.Concurrent.STM import Control.Concurrent.STM
import Data.Speculation.Internal (returning) import Control.Concurrent.Speculation.Internal (returning)
import Data.TagBits (unsafeIsEvaluated) import Data.TagBits (unsafeIsEvaluated)
import Control.Monad (liftM2, unless) import Control.Monad (liftM2, unless)
import Data.Function (on) import Data.Function (on)
@ -86,7 +97,7 @@ specBy cmp guess f a
specBy' :: (a -> a -> Bool) -> a -> (a -> b) -> a -> b specBy' :: (a -> a -> Bool) -> a -> (a -> b) -> a -> b
specBy' cmp guess f a specBy' cmp guess f a
| numCapabilities == 1 = f $! a | numCapabilities == 1 = f $! a
| otherwise = speculation `par` | otherwise = speculation `par`
if cmp guess a if cmp guess a
then speculation then speculation
else f a else f a
@ -162,16 +173,11 @@ specBySTM cmp guess f a
| otherwise = specBySTM' cmp guess f a | otherwise = specBySTM' cmp guess f a
{-# INLINE specBySTM #-} {-# INLINE specBySTM #-}
#ifndef HAS_NUM_SPARKS
numSparks :: IO Int
numSparks = return 0
#endif
-- | 'specSTM'' using a user defined comparison function -- | 'specSTM'' using a user defined comparison function
specBySTM' :: (a -> a -> STM Bool) -> STM a -> (a -> STM b) -> a -> STM b specBySTM' :: (a -> a -> STM Bool) -> STM a -> (a -> STM b) -> a -> STM b
specBySTM' cmp mguess f a = do specBySTM' cmp mguess f a = do
sparks <- unsafeIOToSTM numSparks sparks <- unsafeIOToSTM numSparks
if sparks < numCapabilities if sparks < numCapabilities
then a `par` do then a `par` do
guess <- mguess guess <- mguess
result <- f guess result <- f guess
@ -181,7 +187,7 @@ specBySTM' cmp mguess f a = do
return result return result
`orElse` `orElse`
f a f a
else f $! a else f $! a
{-# INLINE specBySTM' #-} {-# INLINE specBySTM' #-}
-- | @'specBySTM' . 'on' (==)@ -- | @'specBySTM' . 'on' (==)@

View 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

View File

@ -1,5 +1,5 @@
{-# LANGUAGE BangPatterns #-} {-# LANGUAGE BangPatterns #-}
module Data.Speculation.Foldable module Control.Concurrent.Speculation.Foldable
( (
-- * Speculative folds -- * Speculative folds
fold, foldBy fold, foldBy
@ -55,8 +55,8 @@ import Data.Function (on)
import Data.Foldable (Foldable) import Data.Foldable (Foldable)
import qualified Data.Foldable as Foldable import qualified Data.Foldable as Foldable
import Control.Concurrent.STM import Control.Concurrent.STM
import Data.Speculation import Control.Concurrent.Speculation
import Data.Speculation.Internal import Control.Concurrent.Speculation.Internal
import Control.Applicative import Control.Applicative
import Control.Monad hiding (mapM_, msum, forM_, sequence_) 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) any g p = getAny . foldMap (Any . g) (Any . p)
{-# INLINE any #-} {-# 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 (==) sum = sumBy (==)
{-# INLINE sum #-} {-# 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 sumBy cmp g = getSum . foldMapBy (on cmp getSum) (Sum . g) Sum
{-# INLINE sumBy #-} {-# 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 (==) product = productBy (==)
{-# INLINE product #-} {-# INLINE product #-}

View File

@ -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(..) ( Acc(..)
, extractAcc , extractAcc
, MaybeAcc(..) , MaybeAcc(..)

View File

@ -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 -- * Speculative scans
scan, scanBy scan, scanBy
, scanMap, scanMapBy , scanMap, scanMapBy
@ -19,7 +29,7 @@ module Data.Speculation.List
) where ) where
import Prelude hiding import Prelude hiding
(foldl, foldl1, foldr, foldr1 (foldl, foldl1, foldr, foldr1
, any, all, and, or, mapM_, sequence_ , any, all, and, or, mapM_, sequence_
, elem, notElem, sum, product , elem, notElem, sum, product
@ -29,13 +39,13 @@ import Prelude hiding
import Data.Monoid import Data.Monoid
import qualified Data.List as List import qualified Data.List as List
import Data.Speculation import Control.Concurrent.Speculation
import Data.Speculation.Internal import Control.Concurrent.Speculation.Internal
-- | Given a valid estimator @g@, @'scan' g xs@ converts @xs@ into a list of the prefix sums. -- | 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. -- @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 -- 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. -- provide increased opportunities for parallelism.
@ -49,9 +59,9 @@ scanBy cmp g = scanrBy cmp g mappend mempty
{-# INLINE scanBy #-} {-# INLINE scanBy #-}
-- | Given a valid estimator @g@, @'scanMap' g f xs@ converts @xs@ into a list of the prefix sums. -- | 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. -- @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 -- 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. -- provide increased opportunities for parallelism.
-- --
@ -79,13 +89,13 @@ scanr = scanrBy (==)
scanrBy :: (b -> b -> Bool) -> (Int -> b) -> (a -> b -> b) -> b -> [a] -> [b] 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) 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) mf a (Acc n b) = let n' = n + 1 in Acc n' (specBy' cmp (g n') (f a) b)
{-# INLINE scanrBy #-} {-# INLINE scanrBy #-}
scanl :: Eq b => (Int -> b) -> (b -> a -> b) -> b -> [a] -> [b] scanl :: Eq b => (Int -> b) -> (b -> a -> b) -> b -> [a] -> [b]
scanl = scanlBy (==) scanl = scanlBy (==)
{-# INLINE scanl #-} {-# INLINE scanl #-}
scanlBy :: (b -> b -> Bool) -> (Int -> b) -> (b -> a -> b) -> b -> [a] -> [b] 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 #-} {-# INLINE scanlBy #-}
scanr1 :: Eq a => (Int -> a) -> (a -> a -> a) -> [a] -> [a] scanr1 :: Eq a => (Int -> a) -> (a -> a -> a) -> [a] -> [a]
scanr1 = scanr1By (==) scanr1 = scanr1By (==)
{-# INLINE scanr1 #-} {-# INLINE scanr1 #-}
scanr1By :: (a -> a -> Bool) -> (Int -> a) -> (a -> a -> a) -> [a] -> [a] 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 (JustAcc n a) b = let n' = n + 1 in JustAcc n' (specBy' cmp (g n') (`f` b) a)
mf NothingAcc b = JustAcc 1 b mf NothingAcc b = JustAcc 1 b
{-# INLINE scanl1By #-} {-# 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.
-}

View File

@ -1,5 +1,16 @@
{-# LANGUAGE MagicHash, Rank2Types, UnboxedTuples, BangPatterns #-} {-# 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 -- * Traversable
-- ** Applicative Traversals -- ** Applicative Traversals
@ -13,7 +24,6 @@ module Data.Speculation.Traversable
-- ** STM-based traversals with transactional rollback -- ** STM-based traversals with transactional rollback
, mapSTM, mapBySTM , mapSTM, mapBySTM
, forSTM, forBySTM , forSTM, forBySTM
-- , sequenceSTM, sequenceBySTM
-- * Accumulating parameters -- * Accumulating parameters
, mapAccumL, mapAccumLBy , mapAccumL, mapAccumLBy
, mapAccumR, mapAccumRBy , mapAccumR, mapAccumRBy
@ -26,8 +36,8 @@ import Data.Traversable (Traversable)
import qualified Data.Traversable as Traversable import qualified Data.Traversable as Traversable
import Control.Applicative import Control.Applicative
import Control.Concurrent.STM import Control.Concurrent.STM
import Data.Speculation import Control.Concurrent.Speculation
import Data.Speculation.Internal import Control.Concurrent.Speculation.Internal
mapAccumL :: (Traversable t, Eq a) => (Int -> a) -> (a -> b -> (a, c)) -> a -> t b -> (a, t c) mapAccumL :: (Traversable t, Eq a) => (Int -> a) -> (a -> b -> (a, c)) -> a -> t b -> (a, t c)
mapAccumL = mapAccumLBy (==) 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 :: 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 mapAccumLBy cmp g f z xs = runIntAccumL (Traversable.traverse go xs) 0 z
where where
go b = IntAccumL (\n a -> go b = IntAccumL (\n a ->
let ~(a', c) = specBy' cmp (g (I# n)) (`f` b) a let ~(a', c) = specBy' cmp (g (I# n)) (`f` b) a
in (# n +# 1#, a', c #)) in (# n +# 1#, a', c #))
{-# INLINE mapAccumLBy #-} {-# 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 :: 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 mapAccumRBy cmp g f z xs = runIntAccumR (Traversable.traverse go xs) 0 z
where where
go b = IntAccumR (\n a -> go b = IntAccumR (\n a ->
let ~(a', c) = specBy' cmp (g (I# n)) (`f` b) a let ~(a', c) = specBy' cmp (g (I# n)) (`f` b) a
in (# n +# 1#, a', c #)) in (# n +# 1#, a', c #))
{-# INLINE mapAccumRBy #-} {-# 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 go a = AccT $ \i -> acc (i +# 1#) $ WrapMonad $ specBySTM cmp (g (I# i)) f a
{-# INLINE mapBySTM #-} {-# INLINE mapBySTM #-}
sequenceA :: (Traversable t, Applicative f, Eq (f a)) => (Int -> f a) -> t (f a) -> f (t a) sequenceA :: (Traversable t, Applicative f, Eq (f a)) => (Int -> f a) -> t (f a) -> f (t a)
sequenceA g = traverse g id sequenceA g = traverse g id
{-# INLINE sequenceA #-} {-# INLINE sequenceA #-}
@ -153,9 +163,9 @@ instance Functor (IntAccumL s) where
instance Applicative (IntAccumL s) where instance Applicative (IntAccumL s) where
pure a = IntAccumL (\i s -> (# i, s, a #)) pure a = IntAccumL (\i s -> (# i, s, a #))
IntAccumL mf <*> IntAccumL ma = IntAccumL (\i s -> IntAccumL mf <*> IntAccumL ma = IntAccumL (\i s ->
case mf i s of case mf i s of
(# i1, s1, f #) -> (# i1, s1, f #) ->
case ma i1 s1 of case ma i1 s1 of
(# i2, s2, a #) -> (# i2, s2, f a #)) (# i2, s2, a #) -> (# i2, s2, f a #))
data IntAccumR s a = IntAccumR (Int# -> s -> (# Int#, s, 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 instance Applicative (IntAccumR s) where
pure a = IntAccumR (\i s -> (# i, s, a #)) pure a = IntAccumR (\i s -> (# i, s, a #))
IntAccumR mf <*> IntAccumR ma = IntAccumR (\i s -> IntAccumR mf <*> IntAccumR ma = IntAccumR (\i s ->
case ma i s of case ma i s of
(# i1, s1, a #) -> (# i1, s1, a #) ->
case mf i1 s1 of case mf i1 s1 of
(# i2, s2, f #) -> (# i2, s2, f a #)) (# i2, s2, f #) -> (# i2, s2, f a #))
-- applicative composition with a strict integer state applicative -- 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 instance Applicative f => Applicative (AccT f) where
pure a = AccT (\i -> Acc (I# i) (pure a)) 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# let !(Acc !(I# i1#) f) = mf i0#
!(Acc i2 a) = ma i1# !(Acc i2 a) = ma i1#
in Acc i2 (f <*> a)) in Acc i2 (f <*> a))

View File

@ -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

View File

@ -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

View File

@ -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 inconsistent use of the estimator function
------------------------------------------ ------------------------------------------

View File

@ -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. 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 spec a f a = f $! a
The best-case timeline looks like: The best-case timeline looks like:
[---- f g ----] [---- f g ----]
[----- a -----] [----- a -----]
@ -40,7 +40,7 @@ The worst-case timeline looks like:
[----- a -----] [----- a -----]
[---- f a ----] [---- f a ----]
[------- spec g f a -----------] [------- spec g f a -----------]
Compare these to the timeline of `f $! a`: Compare these to the timeline of `f $! a`:
[---- a -----] [---- a -----]
[---- f 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`. 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. Each combinator therein takes an extra argument that is used to speculate on the value of the list.
#### foldr #### foldr

View File

@ -1,5 +1,5 @@
name: speculation name: speculation
version: 1.2.0.2 version: 1.3
license: BSD3 license: BSD3
license-file: LICENSE license-file: LICENSE
author: Edward A. Kmett author: Edward A. Kmett
@ -10,7 +10,7 @@ category: Concurrency
copyright: (c) 2010 Edward A. Kmett copyright: (c) 2010 Edward A. Kmett
build-type: Custom build-type: Custom
cabal-version: >=1.6 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 synopsis: A framework for safe, programmable, speculative parallelism
description: description:
A framework for safe, programmable, speculative parallelism, loosely based on: A framework for safe, programmable, speculative parallelism, loosely based on:
@ -23,7 +23,7 @@ description:
of the transactional rollback machinery from the paper. of the transactional rollback machinery from the paper.
. .
For example: 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'. @'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: 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. '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: extra-source-files:
README.markdown README.markdown
CHANGELOG.markdown CHANGELOG.markdown
ISSUES.markdown ISSUES.markdown
source-repository head source-repository head
type: git type: git
location: http://github.com/ekmett/speculation.git location: http://github.com/ekmett/speculation.git
branch: master 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 flag optimize
description: Enable optimizations for the library and benchmarks description: Enable optimizations for the library and benchmarks
default: True default: True
flag hpc
description: Use HPC for tests
default: True
library library
ghc-options: -Wall ghc-options: -Wall
if flag(optimize) if flag(optimize)
ghc-options: -funbox-strict-fields -O2 -fspec-constr -fdicts-cheap ghc-options: -funbox-strict-fields -O2 -fspec-constr -fdicts-cheap
if flag(HasNumSparks)
cpp-options: -DHAS_NUM_SPARKS build-depends: base >= 4.3 && < 6
build-depends: base >= 4.3 && < 6
else
build-depends: base >= 4 && < 4.3
build-depends: build-depends:
ghc-prim >= 0.2 && < 0.4, ghc-prim >= 0.2 && < 0.4,
@ -112,74 +92,11 @@ library
stm >= 2.1 && < 2.3 stm >= 2.1 && < 2.3
exposed-modules: exposed-modules:
Data.Speculation Control.Concurrent.Speculation
Data.Speculation.Cont Control.Concurrent.Speculation.Class
Data.Speculation.Morphism Control.Concurrent.Speculation.Foldable
Data.Speculation.Foldable Control.Concurrent.Speculation.Traversable
Data.Speculation.Traversable Control.Concurrent.Speculation.List
Data.Speculation.List
other-modules: 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