I have this mutable array in an ST monad. And I have this loop function.
runST $ do
myarray <- newMArray (Sz 10) 0
loopM_ 0 (<10) (+1) (\j ->
loopM_ 0 (<10) (+1) (\i ->
when (mytruthcheck j i)
(modifyM_ myarray (pure . (+1)) ((funcofji j i) :: Int)
)))
I want to use forkST_ to run the outer loop in parallel like this.
runST $ do
myarray <- newMArray (Sz 10) 0
loopM_ 0 (<10) (+1) (\j ->
void (forkST_ (loopM_ 0 (<10) (+1) (\i ->
when (mytruthcheck j i)
(Data.Massiv.Array.Mutable.modifyM_
myarray (pure . (+1)) ((funcofji j i) :: Int)
))))
But I’m guessing this will cause thread collisions but I don’t really know although I do know it is possible that funcofji can output the same value for different values of j and therefore the loop can modify the same index of myarray for different j s. Is there a way to ensure this is done atomically or is that already the case?
Btw here’s the loopM_ function
loopM_ :: Monad m => Int -> (Int -> Bool) -> (Int -> Int) -> (Int -> m a) -> m ()
loopM_ !init' condition increment f = go init'
where
go !step
| condition step = f step >> go (increment step)
| otherwise = pure ()