-- |
-- Module:     Data.Vector.Algorithms.Quicksort.Predefined.PIntParallelMedian3IO
-- Copyright:  (c) Sergey Vinokurov 2023
-- License:    Apache-2.0 (see LICENSE)
-- Maintainer: serg.foo@gmail.com

module Data.Vector.Algorithms.Quicksort.Predefined.PIntParallelMedian3IO
  ( sortPIntParallelMedian3IO
  ) where

import Control.Concurrent (getNumCapabilities)
import Control.Monad.ST
import Data.Int
import Data.Vector.Primitive qualified as P

import Data.Vector.Algorithms.Quicksort.Parameterised

import Data.Vector.Algorithms.Quicksort.Predefined.PIntSequentialMedian3IO ()

{-# NOINLINE sortPIntParallelMedian3IO #-}
sortPIntParallelMedian3IO :: P.MVector RealWorld Int64 -> IO ()
sortPIntParallelMedian3IO :: MVector RealWorld Int64 -> IO ()
sortPIntParallelMedian3IO MVector RealWorld Int64
xs = do
  Parallel
p <- Int -> IO Parallel
mkParallel (Int -> IO Parallel) -> IO Int -> IO Parallel
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< IO Int
getNumCapabilities
  Parallel -> Median3 Int64 -> MVector (PrimState IO) Int64 -> IO ()
forall p med x (m :: * -> *) a (v :: * -> * -> *).
(Fork2 p x m, Median med a m (PrimState m), PrimMonad m, Ord a,
 MVector v a) =>
p -> med -> v (PrimState m) a -> m ()
sortInplaceFM Parallel
p (forall a. Median3 a
forall {k} (a :: k). Median3 a
Median3 @Int64) MVector RealWorld Int64
MVector (PrimState IO) Int64
xs
  Parallel -> IO ()
waitParallel Parallel
p