module Data.Repa.Eval.Generic.Par.Interleaved
(fillInterleaved)
where
import Data.Repa.Eval.Gang
import GHC.Exts
fillInterleaved
:: Gang
-> (Int# -> a -> IO ())
-> (Int# -> a)
-> Int#
-> IO ()
fillInterleaved gang write getElem len
= gangIO gang
$ \thread ->
let !step = threads
!start = thread
!count = elemsForThread thread
in fill step start count
where
!threads = gangSize gang
!chunkLenBase = len `quotInt#` threads
!chunkLenSlack = len `remInt#` threads
elemsForThread thread
| 1# <- thread <# chunkLenSlack = chunkLenBase +# 1#
| otherwise = chunkLenBase
fill !step !ix0 !count0
= go ix0 count0
where
go !ix !count
| 1# <- count <=# 0# = return ()
| otherwise
= do write ix (getElem ix)
go (ix +# step) (count -# 1#)