{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# OPTIONS_GHC -fno-cse #-}
module Torch.Indef.Dynamic.Tensor.Copy
( copy
, copyByte
, copyChar
, copyShort
, copyInt
, copyLong
, copyFloat
, copyDouble
) where
import Foreign hiding (with)
import Foreign.C.Types
import Data.List (intercalate)
import Control.Exception.Safe (throwString)
import Control.Monad.Managed
import System.IO.Unsafe
import Torch.Types.TH (C'THState)
import qualified Torch.Types.TH as TH
import qualified Torch.Sig.Tensor as Sig
import qualified Torch.Sig.Types.Global as Sig
import qualified Torch.Sig.Tensor.Copy as Sig
import qualified Torch.FFI.TH.Byte.Tensor as B
import qualified Torch.FFI.TH.Short.Tensor as S
import qualified Torch.FFI.TH.Int.Tensor as I
import qualified Torch.FFI.TH.Long.Tensor as L
import qualified Torch.FFI.TH.Char.Tensor as C
import qualified Torch.FFI.TH.Float.Tensor as F
import qualified Torch.FFI.TH.Double.Tensor as D
import Torch.Indef.Types
copyType
:: (Ptr TH.C'THLongStorage -> Ptr TH.C'THLongStorage -> IO (Ptr a))
-> FinalizerPtr a
-> (ForeignPtr C'THState -> ForeignPtr a -> b)
-> (Ptr CState -> Ptr CTensor -> Ptr a -> IO ())
-> Dynamic -> b
copyType newWithSize_ fin builder cfun t
= unsafePerformIO
. flip with (pure . builder TH.torchstate) $ do
s' <- managedState
t' <- managedTensor t
liftIO $ do
sizes <- Sig.c_newSizeOf s' t'
strides <- Sig.c_newStrideOf s' t'
target <- newWithSize_ sizes strides
cfun s' t' target
newForeignPtr fin target
{-# NOINLINE copyType #-}
copy :: Dynamic -> Dynamic
copy t = unsafePerformIO . flip with pure $ do
s' <- managedState
t' <- managedTensor t
liftIO $ do
target <- Sig.c_new s'
Sig.c_resizeAs s' target t'
Sig.c_copy s' target t'
mkDynamic target
{-# NOINLINE copy #-}
copyByte :: Dynamic -> TH.ByteDynamic
copyByte = copyType B.c_newWithSize_ B.p_free TH.byteDynamic Sig.c_copyByte
copyChar :: Dynamic -> TH.CharDynamic
copyChar = copyType C.c_newWithSize_ C.p_free TH.charDynamic Sig.c_copyChar
copyShort :: Dynamic -> TH.ShortDynamic
copyShort = copyType S.c_newWithSize_ S.p_free TH.shortDynamic Sig.c_copyShort
copyInt :: Dynamic -> TH.IntDynamic
copyInt = copyType I.c_newWithSize_ I.p_free TH.intDynamic Sig.c_copyInt
copyLong :: Dynamic -> TH.LongDynamic
copyLong = copyType L.c_newWithSize_ L.p_free TH.longDynamic Sig.c_copyLong
copyFloat :: Dynamic -> TH.FloatDynamic
copyFloat = copyType F.c_newWithSize_ F.p_free TH.floatDynamic Sig.c_copyFloat
copyDouble :: Dynamic -> TH.DoubleDynamic
copyDouble = copyType D.c_newWithSize_ D.p_free TH.doubleDynamic Sig.c_copyDouble