Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

loadP has no need to be in IO #41

Closed
lehins opened this issue Aug 5, 2018 · 1 comment
Closed

loadP has no need to be in IO #41

lehins opened this issue Aug 5, 2018 · 1 comment
Assignees
Milestone

Comments

@lehins
Copy link
Owner

lehins commented Aug 5, 2018

There is a way to abstract parallel loading as far as Load class is concerned to be an arbitrary Monad for loadP, just as it is already with loadS. This would enforce safer instances for Loadable arrays. Here is the proposed function to be added to Load as a replacement for loadP:

  loadWithWorkers
    :: Monad m =>
       Int -- ^ Total number of workers
    -> (m () -> m ()) -- ^ A monadic action that will schedule work for the workers
    -> Array r ix e -- ^ Array that is being loaded
    -> (Int -> m e) -- ^ Function that reads an element from target array
    -> (Int -> e -> m ()) -- ^ Function that writes an element into target array
    -> m ()

And the function that does the actual loading:

loadMutableWithScheduler :: (Load r' ix e, Mutable r ix e) =>
                            [Int] -> Array r' ix e -> IO (Array r ix e)
loadMutableWithScheduler wIds !arr = do
  mArr <- unsafeNew (size arr)
  withScheduler_ wIds $ \scheduler ->
    loadWithWorkers
      (numWorkers scheduler)
      (scheduleWork scheduler)
      arr
      (unsafeLinearRead mArr)
      (unsafeLinearWrite mArr)
  unsafeFreeze (ParOn wIds) mArr

This is a pure safety and clarity improvement, as such it is not urgent. Moreover it would be considered a breaking change, therefore this implementation will be postponed to v0.3, whenever that will happen :)

@lehins
Copy link
Owner Author

lehins commented Feb 26, 2019

Argument function that could elements of the target array was removed as it turned out to be useless. This is now done:

  -- | Load an array into memory.
  loadArrayM
    :: Monad m =>
       Int -- ^ Total number of workers (for `Seq` it's always 1)
    -> (m () -> m ())
    -- ^ A monadic action that will schedule work for the workers (for `Seq` it's always `id`)
    -> Array r ix e -- ^ Array that is being loaded
    -> (Int -> e -> m ()) -- ^ Function that writes an element into target array
    -> m ()

@lehins lehins closed this as completed Feb 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant