{-# LANGUAGE GADTs #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
module Data.Array.Accelerate.IO.Foreign.Ptr
where
import Data.Array.Accelerate.Array.Data ( ArrayData, GArrayDataR )
import Data.Array.Accelerate.Array.Unique
import Data.Array.Accelerate.Sugar.Array
import Data.Array.Accelerate.Sugar.Elt
import Data.Array.Accelerate.Sugar.Shape
import Data.Array.Accelerate.Lifetime
import Data.Array.Accelerate.Representation.Type
import qualified Data.Array.Accelerate.Representation.Array as R
import Data.Array.Accelerate.IO.Foreign.Internal
import Foreign.Ptr
import Foreign.ForeignPtr
import Foreign.ForeignPtr.Unsafe
import System.IO.Unsafe
type Ptrs e = GArrayDataR Ptr e
{-# INLINE fromPtrs #-}
fromPtrs :: forall sh e. (Shape sh, Elt e) => sh -> Ptrs (EltR e) -> Array sh e
fromPtrs sh ps = Array (R.Array (fromElt sh) (go (eltR @e) ps))
where
go :: TypeR a -> Ptrs a -> ArrayData a
go TupRunit () = ()
go (TupRpair aR1 aR2) (a1, a2) = (go aR1 a1, go aR2 a2)
go (TupRsingle t) p
| ScalarArrayDict{} <- scalarArrayDict t
= unsafePerformIO $ newUniqueArray =<< newForeignPtr_ p
{-# INLINE toPtrs #-}
toPtrs :: forall sh e. (Shape sh, Elt e) => Array sh e -> Ptrs (EltR e)
toPtrs (Array (R.Array _ adata)) = go (eltR @e) adata
where
go :: TypeR a -> ArrayData a -> Ptrs a
go TupRunit () = ()
go (TupRpair aR1 aR2) (a1, a2) = (go aR1 a1, go aR2 a2)
go (TupRsingle t) a
| ScalarArrayDict{} <- scalarArrayDict t
= unsafeForeignPtrToPtr (unsafeGetValue (uniqueArrayData a))