Skip to content

Commit 83cb86e

Browse files
committed
parallelize
1 parent b8fc003 commit 83cb86e

File tree

3 files changed

+30
-18
lines changed

3 files changed

+30
-18
lines changed

egison.cabal

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ source-repository head
6868
location: https://github.com/egison/egison.git
6969

7070
Library
71-
Build-Depends: base >= 4.0 && < 5, array, random, containers, unordered-containers, haskeline, transformers, mtl, parsec >= 3.0, directory, ghc, ghc-paths, text, regex-tdfa, process, vector, monad-parallel
71+
Build-Depends: base >= 4.0 && < 5, array, random, containers, unordered-containers, haskeline, transformers, mtl, parsec >= 3.0, directory, ghc, ghc-paths, text, regex-tdfa, process, vector, parallel
7272
Hs-Source-Dirs: hs-src
7373
Exposed-Modules:
7474
Language.Egison
@@ -79,7 +79,7 @@ Library
7979
Language.Egison.Primitives
8080
Language.Egison.Util
8181
Other-modules: Paths_egison
82-
ghc-options: -O3
82+
ghc-options: -O3 -threaded
8383

8484
Test-Suite test
8585
Type: exitcode-stdio-1.0

hs-src/Language/Egison/Core.hs

+4-5
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ import Control.Applicative
4646
import Control.Monad.Except hiding (mapM)
4747
import Control.Monad.State hiding (mapM, state)
4848
import Control.Monad.Trans.Maybe
49-
import qualified Control.Monad.Parallel as MP
5049

5150
import Data.Sequence (Seq, ViewL(..), ViewR(..), (><))
5251
import qualified Data.Sequence as Sq
@@ -205,7 +204,7 @@ evalExpr env (ArrayExpr exprs) = do
205204
return . Intermediate . IArray $ Array.listArray (1, toInteger (length exprs)) refs'
206205

207206
evalExpr env (VectorExpr exprs) = do
208-
whnfs <- MP.mapM (evalExpr env) exprs
207+
whnfs <- parallelMapM (evalExpr env) exprs
209208
case whnfs of
210209
((Intermediate (ITensor (Tensor _ _ _))):_) -> do
211210
ret <- mapM toTensor whnfs >>= tConcat' >>= fromTensor
@@ -217,7 +216,7 @@ evalExpr env (TensorExpr nsExpr xsExpr supExpr subExpr) = do
217216
nsWhnf <- evalExpr env nsExpr
218217
ns <- ((fromCollection nsWhnf >>= fromMList >>= mapM evalRef >>= mapM fromWHNF) :: EgisonM [Integer])
219218
xsWhnf <- evalExpr env xsExpr
220-
xs <- fromCollection xsWhnf >>= fromMList >>= MP.mapM evalRef
219+
xs <- fromCollection xsWhnf >>= fromMList >>= parallelMapM evalRef
221220
supWhnf <- evalExpr env supExpr
222221
sup <- fromCollection supWhnf >>= fromMList >>= mapM evalRefDeep -- >>= mapM extractScalar'
223222
subWhnf <- evalExpr env subExpr
@@ -560,7 +559,7 @@ evalExpr env (GenerateTensorExpr fnExpr sizeExpr) = do
560559
size'' <- collectionToList size'
561560
ns <- (mapM fromEgison size'') :: EgisonM [Integer]
562561
fn <- evalExpr env fnExpr
563-
xs <- MP.mapM (\ms -> applyFunc env fn (Value (makeTuple ms))) (map (\ms -> map toEgison ms) (enumTensorIndices ns))
562+
xs <- parallelMapM (\ms -> applyFunc env fn (Value (makeTuple ms))) (map (\ms -> map toEgison ms) (enumTensorIndices ns))
564563
case (ns, xs) of
565564
([1], x:[]) -> return $ x
566565
_ -> fromTensor (Tensor ns (V.fromList xs) [])
@@ -683,7 +682,7 @@ evalWHNF (Intermediate (IStrHash refs)) = do
683682
evalWHNF (Intermediate (ITuple [ref])) = evalRefDeep ref
684683
evalWHNF (Intermediate (ITuple refs)) = Tuple <$> mapM evalRefDeep refs
685684
evalWHNF (Intermediate (ITensor (Tensor ns whnfs js))) = do
686-
vals <- MP.mapM evalWHNF (V.toList whnfs)
685+
vals <- parallelMapM evalWHNF (V.toList whnfs)
687686
return $ TensorData $ Tensor ns (V.fromList vals) js
688687
-- vals <- mapM evalWHNF whnfs
689688
-- return $ TensorData $ Tensor ns vals js

hs-src/Language/Egison/Types.hs

+24-11
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ module Language.Egison.Types
101101
, liftError
102102
-- * Monads
103103
, EgisonM (..)
104+
, parallelMapM
104105
, runEgisonM
105106
, liftEgisonM
106107
, fromEgisonM
@@ -146,6 +147,7 @@ module Language.Egison.Types
146147
import Prelude hiding (foldr, mappend, mconcat)
147148

148149
import Control.Exception
150+
import Control.Parallel
149151
import Data.Typeable
150152

151153
import Control.Applicative
@@ -155,7 +157,6 @@ import Control.Monad.Reader (ReaderT)
155157
import Control.Monad.Writer (WriterT)
156158
import Control.Monad.Identity
157159
import Control.Monad.Trans.Maybe
158-
import qualified Control.Monad.Parallel as MP
159160

160161
import Data.Monoid (Monoid)
161162
import qualified Data.HashMap.Lazy as HL
@@ -837,7 +838,7 @@ tTranspose' is t@(Tensor ns xs js) = do
837838

838839
tMap :: HasTensor a => (a -> EgisonM a) -> (Tensor a) -> EgisonM (Tensor a)
839840
tMap f (Tensor ns xs js) = do
840-
xs' <- MP.mapM f (V.toList xs) >>= return . V.fromList
841+
xs' <- parallelMapM f (V.toList xs) >>= return . V.fromList
841842
t <- toTensor (V.head xs')
842843
case t of
843844
(Tensor ns1 _ js1) ->
@@ -847,9 +848,9 @@ tMap f (Scalar x) = f x >>= return . Scalar
847848

848849
tMapN :: HasTensor a => ([a] -> EgisonM a) -> [Tensor a] -> EgisonM (Tensor a)
849850
tMapN f ts@((Tensor ns xs js):_) = do
850-
xs' <- MP.mapM (\is -> mapM (tIntRef is) ts >>= mapM fromTensor >>= f) (enumTensorIndices ns)
851+
xs' <- parallelMapM (\is -> mapM (tIntRef is) ts >>= mapM fromTensor >>= f) (enumTensorIndices ns)
851852
return $ Tensor ns (V.fromList xs') js
852-
tMapN f xs = MP.mapM fromTensor xs >>= f >>= return . Scalar
853+
tMapN f xs = parallelMapM fromTensor xs >>= f >>= return . Scalar
853854

854855
tMap2 :: HasTensor a => (a -> a -> EgisonM a) -> Tensor a -> Tensor a -> EgisonM (Tensor a)
855856
tMap2 f t1@(Tensor ns1 xs1 js1) t2@(Tensor ns2 xs2 js2) = do
@@ -859,7 +860,7 @@ tMap2 f t1@(Tensor ns1 xs1 js1) t2@(Tensor ns2 xs2 js2) = do
859860
let cns = take (length cjs) (tSize t1')
860861
rts1 <- mapM (flip tIntRef t1') (enumTensorIndices cns)
861862
rts2 <- mapM (flip tIntRef t2') (enumTensorIndices cns)
862-
rts' <- MP.mapM (\(t1, t2) -> tProduct f t1 t2) (zip rts1 rts2)
863+
rts' <- parallelMapM (\(t1, t2) -> tProduct f t1 t2) (zip rts1 rts2)
863864
let ret = Tensor (cns ++ (tSize (head rts'))) (V.concat (map tToVector rts')) (cjs ++ tIndex (head rts'))
864865
tTranspose (uniq (tDiagIndex (js1 ++ js2))) ret
865866
where
@@ -1554,8 +1555,23 @@ liftError = either throwError return
15541555

15551556
newtype EgisonM a = EgisonM {
15561557
unEgisonM :: (ExceptT EgisonError (FreshT IO) a)
1557-
} deriving (Functor, Applicative, Monad, MonadIO, MonadError EgisonError, MonadFresh, MP.MonadParallel)
1558-
-- } deriving (Functor, Applicative, Monad, MonadIO, MonadError EgisonError, MonadFresh)
1558+
} deriving (Functor, Applicative, Monad, MonadIO, MonadError EgisonError, MonadFresh)
1559+
1560+
parallelMapM :: (a -> EgisonM b) -> [a] -> EgisonM [b]
1561+
parallelMapM f [] = return []
1562+
parallelMapM f (x:xs) = do
1563+
let y = unsafePerformEgison (0,1) $ f x
1564+
let ys = unsafePerformEgison (0,1) $ parallelMapM f xs
1565+
y `par` (ys `pseq` return (y:ys))
1566+
1567+
unsafePerformEgison :: (Int, Int) -> EgisonM a -> a
1568+
unsafePerformEgison (x, y) ma =
1569+
let ((Right ret), _) = unsafePerformIO $ runFreshT (x, y + 1) $ runEgisonM ma in
1570+
ret
1571+
-- f' :: (Either EgisonError a) -> (Either EgisonError b) -> EgisonM c
1572+
-- f' (Right x) (Right y) = f x y
1573+
-- f' (Left e) _ = liftError (Left e)
1574+
-- f' _ (Left e) = liftError (Left e)
15591575

15601576
runEgisonM :: EgisonM a -> FreshT IO (Either EgisonError a)
15611577
runEgisonM = runExceptT . unEgisonM
@@ -1587,8 +1603,7 @@ modifyCounter m = do
15871603
return result
15881604

15891605
newtype FreshT m a = FreshT { unFreshT :: StateT (Int, Int) m a }
1590-
deriving (Functor, Applicative, Monad, MonadState (Int, Int), MonadTrans, MP.MonadParallel)
1591-
-- deriving (Functor, Applicative, Monad, MonadState Int, MonadTrans)
1606+
deriving (Functor, Applicative, Monad, MonadState (Int, Int), MonadTrans)
15921607

15931608
type Fresh = FreshT Identity
15941609

@@ -1622,8 +1637,6 @@ instance (MonadFresh m, Monoid e) => MonadFresh (WriterT e m) where
16221637
instance MonadIO (FreshT IO) where
16231638
liftIO = lift
16241639

1625-
instance (MP.MonadParallel m) => MP.MonadParallel (StateT s m)
1626-
16271640
runFreshT :: Monad m => (Int, Int) -> FreshT m a -> m (a, (Int, Int))
16281641
runFreshT seed = flip (runStateT . unFreshT) seed
16291642

0 commit comments

Comments
 (0)