{-# LINE 1 "src/Foreign/CUDA/Driver/IPC/Marshal.chs" #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE EmptyDataDecls #-}
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE TemplateHaskell #-}
module Foreign.CUDA.Driver.IPC.Marshal (
IPCDevicePtr, IPCFlag(..),
export, open, close,
) where
import qualified Foreign.C.Types as C2HSImp
import qualified Foreign.Ptr as C2HSImp
{-# LINE 30 "src/Foreign/CUDA/Driver/IPC/Marshal.chs" #-}
import Foreign.CUDA.Ptr
import Foreign.CUDA.Driver.Error
import Foreign.CUDA.Internal.C2HS
import Foreign.CUDA.Driver.Marshal
import Control.Monad
import Prelude
import Foreign.C
import Foreign.Ptr
import Foreign.ForeignPtr
import Foreign.Marshal
newtype IPCDevicePtr a = IPCDevicePtr { useIPCDevicePtr :: IPCMemHandle }
deriving (Eq, Show)
data IPCFlag = LazyEnablePeerAccess
deriving (Eq,Show,Bounded)
instance Enum IPCFlag where
succ LazyEnablePeerAccess = error "IPCFlag.succ: LazyEnablePeerAccess has no successor"
pred LazyEnablePeerAccess = error "IPCFlag.pred: LazyEnablePeerAccess has no predecessor"
enumFromTo from to = go from
where
end = fromEnum to
go v = case compare (fromEnum v) end of
LT -> v : go (succ v)
EQ -> [v]
GT -> []
enumFrom from = enumFromTo from LazyEnablePeerAccess
fromEnum LazyEnablePeerAccess = 1
toEnum 1 = LazyEnablePeerAccess
toEnum unmatched = error ("IPCFlag.toEnum: Cannot match " ++ show unmatched)
{-# LINE 68 "src/Foreign/CUDA/Driver/IPC/Marshal.chs" #-}
{-# INLINEABLE export #-}
export :: DevicePtr a -> IO (IPCDevicePtr a)
export !dptr = do
h <- newIPCMemHandle
r <- cuIpcGetMemHandle h dptr
resultIfOk (r, IPCDevicePtr h)
{-# INLINE cuIpcGetMemHandle #-}
cuIpcGetMemHandle :: (IPCMemHandle) -> (DevicePtr a) -> IO ((Status))
cuIpcGetMemHandle a1 a2 =
withForeignPtr a1 $ \a1' ->
let {a2' = useDeviceHandle a2} in
cuIpcGetMemHandle'_ a1' a2' >>= \res ->
let {res' = cToEnum res} in
return (res')
{-# LINE 100 "src/Foreign/CUDA/Driver/IPC/Marshal.chs" #-}
{-# INLINEABLE open #-}
open :: IPCDevicePtr a -> [IPCFlag]-> IO (DevicePtr a)
open !hdl !flags = resultIfOk =<< cuIpcOpenMemHandle (useIPCDevicePtr hdl) flags
{-# INLINE cuIpcOpenMemHandle #-}
cuIpcOpenMemHandle :: (IPCMemHandle) -> ([IPCFlag]) -> IO ((Status), (DevicePtr a))
cuIpcOpenMemHandle a2 a3 =
alloca $ \a1' ->
withForeignPtr a2 $ \a2' ->
let {a3' = combineBitMasks a3} in
cuIpcOpenMemHandle'_ a1' a2' a3' >>= \res ->
let {res' = cToEnum res} in
peekDeviceHandle a1'>>= \a1'' ->
return (res', a1'')
{-# LINE 135 "src/Foreign/CUDA/Driver/IPC/Marshal.chs" #-}
{-# INLINEABLE close #-}
close :: DevicePtr a -> IO ()
close !dptr = nothingIfOk =<< cuIpcCloseMemHandle dptr
{-# INLINE cuIpcCloseMemHandle #-}
cuIpcCloseMemHandle :: (DevicePtr a) -> IO ((Status))
cuIpcCloseMemHandle a1 =
let {a1' = useDeviceHandle a1} in
cuIpcCloseMemHandle'_ a1' >>= \res ->
let {res' = cToEnum res} in
return (res')
{-# LINE 162 "src/Foreign/CUDA/Driver/IPC/Marshal.chs" #-}
type IPCMemHandle = ForeignPtr ()
newIPCMemHandle :: IO IPCMemHandle
newIPCMemHandle = mallocForeignPtrBytes 64
{-# LINE 176 "src/Foreign/CUDA/Driver/IPC/Marshal.chs" #-}
foreign import ccall unsafe "Foreign/CUDA/Driver/IPC/Marshal.chs.h cuIpcGetMemHandle"
cuIpcGetMemHandle'_ :: ((C2HSImp.Ptr ()) -> (C2HSImp.CULLong -> (IO C2HSImp.CInt)))
foreign import ccall unsafe "Foreign/CUDA/Driver/IPC/Marshal.chs.h cuIpcOpenMemHandle"
cuIpcOpenMemHandle'_ :: ((C2HSImp.Ptr C2HSImp.CULLong) -> ((C2HSImp.Ptr ()) -> (C2HSImp.CUInt -> (IO C2HSImp.CInt))))
foreign import ccall unsafe "Foreign/CUDA/Driver/IPC/Marshal.chs.h cuIpcCloseMemHandle"
cuIpcCloseMemHandle'_ :: (C2HSImp.CULLong -> (IO C2HSImp.CInt))