将 Haskell 中数组的指针传递给 C 函数

发布于 2024-07-25 04:18:06 字数 1399 浏览 9 评论 0原文

我有以下 C 代码:

#include <sys/times.h>
#include <time.h>


float etime_( float *tarray )
{   struct tms buf;

    times( &buf );

    tarray[0] = 1.0 * buf.tms_utime / CLOCKS_PER_SEC;
    tarray[1] = 1.0 * buf.tms_stime / CLOCKS_PER_SEC;

    return tarray[0] + tarray[1];
}

尝试将此 Fortran 代码移植到 Haskell:

  PROGRAM Test
    IMPLICIT NONE

    REAL t, ta(2), etime

    INTEGER i
    DOUBLE PRECISION x

    do i = 1, 10000
       x = sin( cos( i * 1.0 d0 ) )
       print *, x 
    enddo

    ta(1) = 0.0d0
    ta(2) = 0.0d0

    t = etime( ta )       

    PRINT *, 'user time:    ', ta(1)
    PRINT *, 'system time:  ', ta(2)
    PRINT *, 'process time: ', t

  END

如何定义数组和! 或者 !!! 为了让下面的代码工作?

module Main where

import GHC.Ptr
import GHC.Prim
import System.IO.Unsafe
import Control.Monad

foreign import ccall etime_ :: Ptr Double → IO Double
etime = etime_

main :: IO Int
main = do
  mapM_ (print . sin . cos . (* (1.0 :: Double)) . fromIntegral) [1..10000 :: Int]
  ta ← array 2
  t ← etime ta
  putStrLn $ "user time:    " ++ show (ta !!! 0)
  putStrLn $ "system time:  " ++ show (ta !!! 1)
  putStrLn $ "process time: " ++ show t
  return 0

array :: Int → IO (Ptr a)
array size = undefined

(!) :: Ptr a → Int → IO a
(!) = undefined

(!!!) :: Ptr a → Int → a
(!!!) = undefined

I have the following C code:

#include <sys/times.h>
#include <time.h>


float etime_( float *tarray )
{   struct tms buf;

    times( &buf );

    tarray[0] = 1.0 * buf.tms_utime / CLOCKS_PER_SEC;
    tarray[1] = 1.0 * buf.tms_stime / CLOCKS_PER_SEC;

    return tarray[0] + tarray[1];
}

Trying to port this Fortran code to Haskell:

  PROGRAM Test
    IMPLICIT NONE

    REAL t, ta(2), etime

    INTEGER i
    DOUBLE PRECISION x

    do i = 1, 10000
       x = sin( cos( i * 1.0 d0 ) )
       print *, x 
    enddo

    ta(1) = 0.0d0
    ta(2) = 0.0d0

    t = etime( ta )       

    PRINT *, 'user time:    ', ta(1)
    PRINT *, 'system time:  ', ta(2)
    PRINT *, 'process time: ', t

  END

How can I define array and ! or !!! for the below code to work?

module Main where

import GHC.Ptr
import GHC.Prim
import System.IO.Unsafe
import Control.Monad

foreign import ccall etime_ :: Ptr Double → IO Double
etime = etime_

main :: IO Int
main = do
  mapM_ (print . sin . cos . (* (1.0 :: Double)) . fromIntegral) [1..10000 :: Int]
  ta ← array 2
  t ← etime ta
  putStrLn $ "user time:    " ++ show (ta !!! 0)
  putStrLn $ "system time:  " ++ show (ta !!! 1)
  putStrLn $ "process time: " ++ show t
  return 0

array :: Int → IO (Ptr a)
array size = undefined

(!) :: Ptr a → Int → IO a
(!) = undefined

(!!!) :: Ptr a → Int → a
(!!!) = undefined

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千笙结 2024-08-01 04:18:07

我建议这样:

{-# LANGUAGE ForeignFunctionInterface #-}

import Foreign.Marshal.Array (allocaArray, peekArray)
import Foreign.Ptr (Ptr)

foreign import ccall etime_ :: Ptr Float -> IO Float

main :: IO ()
main = do
  mapM_ (print . sin . cos . (* (1.0 :: Double)) . fromIntegral) [1..10000 :: Int]
  allocaArray 2 $ \ta -> do
    ptime <- etime_ ta
    [utime, stime] <- peekArray 2 ta
    putStrLn $ "user time:    " ++ show utime
    putStrLn $ "system time:  " ++ show stime
    putStrLn $ "process time: " ++ show ptime

但是回答你的具体问题:

array :: Storable a => Int -> IO (Ptr a)
array = mallocArray

(!) :: Storable a => Ptr a -> Int -> IO a
(!) = peekElemOff

(!!!) :: Storable a => Ptr a -> Int -> a
(!!!) ptr = unsafePerformIO . peekElemOff ptr

我建议使用hoogle来找到你需要的功能。

I suggest this:

{-# LANGUAGE ForeignFunctionInterface #-}

import Foreign.Marshal.Array (allocaArray, peekArray)
import Foreign.Ptr (Ptr)

foreign import ccall etime_ :: Ptr Float -> IO Float

main :: IO ()
main = do
  mapM_ (print . sin . cos . (* (1.0 :: Double)) . fromIntegral) [1..10000 :: Int]
  allocaArray 2 $ \ta -> do
    ptime <- etime_ ta
    [utime, stime] <- peekArray 2 ta
    putStrLn $ "user time:    " ++ show utime
    putStrLn $ "system time:  " ++ show stime
    putStrLn $ "process time: " ++ show ptime

but to answer your specific questions:

array :: Storable a => Int -> IO (Ptr a)
array = mallocArray

(!) :: Storable a => Ptr a -> Int -> IO a
(!) = peekElemOff

(!!!) :: Storable a => Ptr a -> Int -> a
(!!!) ptr = unsafePerformIO . peekElemOff ptr

I suggest to use hoogle to find the functions you need.

ぃ弥猫深巷。 2024-08-01 04:18:07

以元组形式返回总和、用户时间和系统时间。 请注意,etime 采用的是 float 指针,而不是 double 指针,因此请确保您的类型一致:

{-# LANGUAGE ForeignFunctionInterface #-}

module Main where

import Foreign (Ptr)
import Foreign.Marshal.Array (allocaArray,peekArray)
import Control.Monad (mapM_)

foreign import ccall etime_ :: Ptr Float -> IO Float

etime :: IO (Float, Float, Float)
etime = do
  allocaArray 2 $ \ta -> do
    t <- etime_ ta
    [user,sys] <- peekArray 2 ta
    return (t,user,sys)

main :: IO Int
main = do
  mapM_ (print . sin . cos . (* (1.0 :: Double)) . fromIntegral) [1..10000 :: Int]
  (t,user,sys) <- etime
  putStrLn $ "user time:    " ++ show user
  putStrLn $ "system time:  " ++ show sys
  putStrLn $ "process time: " ++ show t
  return 0

Return the sum, user time, and system time as a tuple. Note that etime takes a pointer to float, not double, so make your types agree:

{-# LANGUAGE ForeignFunctionInterface #-}

module Main where

import Foreign (Ptr)
import Foreign.Marshal.Array (allocaArray,peekArray)
import Control.Monad (mapM_)

foreign import ccall etime_ :: Ptr Float -> IO Float

etime :: IO (Float, Float, Float)
etime = do
  allocaArray 2 $ \ta -> do
    t <- etime_ ta
    [user,sys] <- peekArray 2 ta
    return (t,user,sys)

main :: IO Int
main = do
  mapM_ (print . sin . cos . (* (1.0 :: Double)) . fromIntegral) [1..10000 :: Int]
  (t,user,sys) <- etime
  putStrLn $ "user time:    " ++ show user
  putStrLn $ "system time:  " ++ show sys
  putStrLn $ "process time: " ++ show t
  return 0
要走干脆点 2024-08-01 04:18:07

给定以下附加 Haskell 模块:

module AllocSymbols (

  (↑≣), -- mallocArray
  (≣⊠), -- free

  (≣→), -- peekElemOff
  (≣←), -- pokeElemOff

  (⥱), -- peekElemOff unsafe

  (⥅), -- advancePtr

) where

import GHC.Ptr
import System.IO.Unsafe
import Foreign.Storable
import Foreign.Marshal.Alloc (free)
import Foreign.Marshal.Array

(↑≣) :: Storable a ⇒ Int → IO (Ptr a)
(↑≣) = mallocArray

(⥱) :: Storable a ⇒ Ptr a → Int → a
a ⥱ i = unsafePerformIO $ peekElemOff a i

(≣→) :: Storable a ⇒ Ptr a → Int → IO a
(≣→) = peekElemOff

(≣←) :: Storable a ⇒ Ptr a → Int → a → IO ()
(≣←) = pokeElemOff

(⥅) :: Storable a ⇒ Ptr a → Int → Ptr a
(⥅) = advancePtr

(≣⊠) :: Ptr a → IO ()
(≣⊠) = Foreign.Marshal.Alloc.free

现在我们可以将这段 Fortran 代码

  PROGRAM Test
    IMPLICIT NONE

    REAL t, ta(2), etime

    INTEGER i
    DOUBLE PRECISION x

    do i = 1, 10000
       x = sin( cos( i * 1.0 d0 ) )
       print *, x 
    enddo

    ta(1) = 0.0d0
    ta(2) = 0.0d0

    t = etime( ta )       

    PRINT *, 'user time:    ', ta(1)
    PRINT *, 'system time:  ', ta(2)
    PRINT *, 'process time: ', t

  END

逐字移植到 Haskell:

module Main where

import GHC.Ptr
import AllocSymbols
import Control.Monad

foreign import ccall etime_ :: Ptr Float → IO Float

main :: IO ()
main = do
  mapM_ (print . sin . cos . (* (1.0 :: Double)) . fromIntegral) [1 .. 10000 :: Int]

  ta ← (2 ↑≣) -- ta :: Ptr Float

  (ta ≣← 0) 0.0
  (ta ≣← 1) 0.0

  t ← etime_ ta

  putStrLn $ " user time:    " ++ show (ta ⥱ 0)
  putStrLn $ " system time:  " ++ show (ta ⥱ 1)
  putStrLn $ " process time: " ++ show t

  (ta ≣⊠) -- optional in this case

Given the following additional Haskell module:

module AllocSymbols (

  (↑≣), -- mallocArray
  (≣⊠), -- free

  (≣→), -- peekElemOff
  (≣←), -- pokeElemOff

  (⥱), -- peekElemOff unsafe

  (⥅), -- advancePtr

) where

import GHC.Ptr
import System.IO.Unsafe
import Foreign.Storable
import Foreign.Marshal.Alloc (free)
import Foreign.Marshal.Array

(↑≣) :: Storable a ⇒ Int → IO (Ptr a)
(↑≣) = mallocArray

(⥱) :: Storable a ⇒ Ptr a → Int → a
a ⥱ i = unsafePerformIO $ peekElemOff a i

(≣→) :: Storable a ⇒ Ptr a → Int → IO a
(≣→) = peekElemOff

(≣←) :: Storable a ⇒ Ptr a → Int → a → IO ()
(≣←) = pokeElemOff

(⥅) :: Storable a ⇒ Ptr a → Int → Ptr a
(⥅) = advancePtr

(≣⊠) :: Ptr a → IO ()
(≣⊠) = Foreign.Marshal.Alloc.free

And now we can port this piece of Fortran code:

  PROGRAM Test
    IMPLICIT NONE

    REAL t, ta(2), etime

    INTEGER i
    DOUBLE PRECISION x

    do i = 1, 10000
       x = sin( cos( i * 1.0 d0 ) )
       print *, x 
    enddo

    ta(1) = 0.0d0
    ta(2) = 0.0d0

    t = etime( ta )       

    PRINT *, 'user time:    ', ta(1)
    PRINT *, 'system time:  ', ta(2)
    PRINT *, 'process time: ', t

  END

word for word to Haskell:

module Main where

import GHC.Ptr
import AllocSymbols
import Control.Monad

foreign import ccall etime_ :: Ptr Float → IO Float

main :: IO ()
main = do
  mapM_ (print . sin . cos . (* (1.0 :: Double)) . fromIntegral) [1 .. 10000 :: Int]

  ta ← (2 ↑≣) -- ta :: Ptr Float

  (ta ≣← 0) 0.0
  (ta ≣← 1) 0.0

  t ← etime_ ta

  putStrLn $ " user time:    " ++ show (ta ⥱ 0)
  putStrLn $ " system time:  " ++ show (ta ⥱ 1)
  putStrLn $ " process time: " ++ show t

  (ta ≣⊠) -- optional in this case
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文