module Mischief.ECS.World.Par
  ( par,
    par_,
    parFor,
    parFor_,
    parIterList,
  )
where

import Control.Concurrent.Async (async, wait)
import Control.Monad
import Control.Monad.IO.Class
import Control.Monad.Reader
import Data.Foldable
import Data.IORef (modifyIORef', newIORef, readIORef)
import Data.Traversable
import GHC.Conc (numCapabilities)
import Mischief.ECS.Hidden
import Mischief.ECS.World

-- | Receives a list of @ParSystems@ and runs them in parallel.
par :: (MonadSystem w m) => [ParSystem a] -> m [a]
par :: forall w (m :: * -> *) a. MonadSystem w m => [ParSystem a] -> m [a]
par ![ParSystem a]
parSystems = do
  !world <- m World
forall w (m :: * -> *). MonadSystem w m => m World
unsafeGetWorld

  !x <- forM parSystems $ \(ParSystem !ReaderT ParWorld IO a
p) -> do
    !systems <- IO (IORef [System ()]) -> m (IORef [System ()])
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (IORef [System ()]) -> m (IORef [System ()]))
-> IO (IORef [System ()]) -> m (IORef [System ()])
forall a b. (a -> b) -> a -> b
$ [System ()] -> IO (IORef [System ()])
forall a. a -> IO (IORef a)
newIORef []
    !id <- liftIO $ async $ runReaderT p ParWorld {world = Hidden world, parDeferred = systems}
    pure (id, systems)

  for x $ \(!Async a
id, !IORef [System ()]
systems) -> do
    a <- IO a -> m a
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO a -> m a) -> IO a -> m a
forall a b. (a -> b) -> a -> b
$ Async a -> IO a
forall a. Async a -> IO a
wait Async a
id
    !systems <- liftIO $ readIORef systems
    liftIO $ modifyIORef' world.deferred (++ systems)
    pure a

-- | Receives a list of @ParSystems@ and runs them in parallel. Discards the results.
par_ :: (MonadSystem w m) => [ParSystem a] -> m ()
par_ :: forall w (m :: * -> *) a. MonadSystem w m => [ParSystem a] -> m ()
par_ ![ParSystem a]
a = m [a] -> m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (m [a] -> m ()) -> m [a] -> m ()
forall a b. (a -> b) -> a -> b
$ [ParSystem a] -> m [a]
forall w (m :: * -> *) a. MonadSystem w m => [ParSystem a] -> m [a]
par [ParSystem a]
a

parIterList :: (MonadSystem w m, Foldable t) => t a -> ([a] -> ParSystem b) -> m [b]
parIterList :: forall w (m :: * -> *) (t :: * -> *) a b.
(MonadSystem w m, Foldable t) =>
t a -> ([a] -> ParSystem b) -> m [b]
parIterList !t a
list ![a] -> ParSystem b
s = do
  !world <- m World
forall w (m :: * -> *). MonadSystem w m => m World
unsafeGetWorld

  let !n = Int
numCapabilities
  let !len = t a -> Int
forall a. t a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length t a
list

  let !chunks = Int -> [a] -> [[a]]
forall a. Int -> [a] -> [[a]]
group (Int
len Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
n) (t a -> [a]
forall a. t a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList t a
list)

  !x <- forM chunks $ \(![a]
chunk) -> do
    !systems <- IO (IORef [System ()]) -> m (IORef [System ()])
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (IORef [System ()]) -> m (IORef [System ()]))
-> IO (IORef [System ()]) -> m (IORef [System ()])
forall a b. (a -> b) -> a -> b
$ [System ()] -> IO (IORef [System ()])
forall a. a -> IO (IORef a)
newIORef []
    let ParSystem !p = s chunk
    !id <- liftIO $ async $ runReaderT p ParWorld {world = Hidden world, parDeferred = systems}
    pure (id, systems)

  for x $ \(!Async b
id, !IORef [System ()]
systems) -> do
    !a <- IO b -> m b
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO b -> m b) -> IO b -> m b
forall a b. (a -> b) -> a -> b
$ Async b -> IO b
forall a. Async a -> IO a
wait Async b
id
    !systems <- liftIO $ readIORef systems
    liftIO $ modifyIORef' world.deferred (++ systems)
    pure a

group :: Int -> [a] -> [[a]]
group :: forall a. Int -> [a] -> [[a]]
group Int
_ [] = []
group Int
0 ![a]
l = [[a]
l]
group !Int
n ![a]
l = Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
take Int
n [a]
l [a] -> [[a]] -> [[a]]
forall a. a -> [a] -> [a]
: Int -> [a] -> [[a]]
forall a. Int -> [a] -> [[a]]
group Int
n (Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
drop Int
n [a]
l)

-- | Iterates over a list in parallel.
parFor :: (MonadSystem w m, Foldable t) => t a -> (a -> ParSystem b) -> m [b]
parFor :: forall w (m :: * -> *) (t :: * -> *) a b.
(MonadSystem w m, Foldable t) =>
t a -> (a -> ParSystem b) -> m [b]
parFor !t a
x !a -> ParSystem b
s = [[b]] -> [b]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[b]] -> [b]) -> m [[b]] -> m [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> t a -> ([a] -> ParSystem [b]) -> m [[b]]
forall w (m :: * -> *) (t :: * -> *) a b.
(MonadSystem w m, Foldable t) =>
t a -> ([a] -> ParSystem b) -> m [b]
parIterList t a
x ([a] -> (a -> ParSystem b) -> ParSystem [b]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
t a -> (a -> f b) -> f (t b)
`for` a -> ParSystem b
s)

-- | Iterates over a list in parallel and discards the
parFor_ :: (MonadSystem w m, Foldable t) => t a -> (a -> ParSystem b) -> m ()
parFor_ :: forall w (m :: * -> *) (t :: * -> *) a b.
(MonadSystem w m, Foldable t) =>
t a -> (a -> ParSystem b) -> m ()
parFor_ !t a
x !a -> ParSystem b
s = m [()] -> m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (t a -> ([a] -> ParSystem ()) -> m [()]
forall w (m :: * -> *) (t :: * -> *) a b.
(MonadSystem w m, Foldable t) =>
t a -> ([a] -> ParSystem b) -> m [b]
parIterList t a
x ([a] -> (a -> ParSystem b) -> ParSystem ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
`for_` a -> ParSystem b
s))