Passing a matrix from C to Haskell

I need to pass a big 2-dimensional array of doubles from a legacy C code to a Haskell module. Of this huge array, the Haskell code will actually use only a few elements (say, 10 out of 100x20 matrix). Unfortunately, the C code does not know which data are actually needed in the Haskell module. What would be a good way to pass the array to Haskell and, once the data are handed over, what is the fastest way to fetch these elements? I was thinking of something like passing the array as Ptr Int#, but how do I fetch the elements that I am interested in? Cheers, Cyril

Cyril Schmidt wrote:
I need to pass a big 2-dimensional array of doubles from a legacy C code to a Haskell module. Of this huge array, the Haskell code will actually use only a few elements (say, 10 out of 100x20 matrix). Unfortunately, the C code does not know which data are actually needed in the Haskell module.
What would be a good way to pass the array to Haskell and, once the data are handed over, what is the fastest way to fetch these elements?
I was thinking of something like passing the array as Ptr Int#, but how do I fetch the elements that I am interested in?
Cheers,
Cyril
You probably want Foreign.Marshal.Array http://www.haskell.org/ghc/docs/6.4.1/html/libraries/base/Foreign-Marshal-Ar... peekArray :: Storable a => Int -> Ptr a -> IO [a]

Hello Chris, Wednesday, February 08, 2006, 2:35:39 AM, you wrote: CK> You probably want Foreign.Marshal.Array CK> http://www.haskell.org/ghc/docs/6.4.1/html/libraries/base/Foreign-Marshal-Ar... CK> peekArray :: Storable a => Int -> Ptr a -> IO [a] i think we should define "secret door" to construct StorableArray from a pointer to allow to use full power of MArray interface on foreign arrays -- Best regards, Bulat mailto:bulatz@HotPOP.com

Bulat Ziganshin wrote:
Hello Chris,
Wednesday, February 08, 2006, 2:35:39 AM, you wrote:
CK> You probably want Foreign.Marshal.Array
CK> http://www.haskell.org/ghc/docs/6.4.1/html/libraries/base/Foreign-Marshal-Ar...
CK> peekArray :: Storable a => Int -> Ptr a -> IO [a]
i think we should define "secret door" to construct StorableArray from a pointer to allow to use full power of MArray interface on foreign arrays
You mean this? -- |Construct a 'StorableArray' from an arbitrary 'ForeignPtr'. It is -- the caller's responsibility to ensure that the 'ForeignPtr' points to -- an area of memory sufficient for the specified bounds. unsafeForeignPtrToStorableArray :: ForeignPtr e -> (i,i) -> IO (StorableArray i e) Cheers, Simon

Simon Marlow wrote:
Bulat Ziganshin wrote: [...]
i think we should define "secret door" to construct StorableArray from a pointer to allow to use full power of MArray interface on foreign arrays
You mean this?
-- |Construct a 'StorableArray' from an arbitrary 'ForeignPtr'. It is -- the caller's responsibility to ensure that the 'ForeignPtr' points to -- an area of memory sufficient for the specified bounds. unsafeForeignPtrToStorableArray :: ForeignPtr e -> (i,i) -> IO (StorableArray i e)
It looks like this function is not available in GHC 6.4.1 (see http://www.haskell.org/ghc/docs/6.4.1/html/libraries/base/Data-Array-Storabl... ) It is, however, documented at http://www.haskell.org/HOpenGL/newAPI/base/Foreign-Storable.html but I don't know which version of GHC it applies to. Cheers, Cyril

Cyril Schmidt wrote:
Simon Marlow wrote:
Bulat Ziganshin wrote:
[...]
i think we should define "secret door" to construct StorableArray from a pointer to allow to use full power of MArray interface on foreign arrays
You mean this?
-- |Construct a 'StorableArray' from an arbitrary 'ForeignPtr'. It is -- the caller's responsibility to ensure that the 'ForeignPtr' points to -- an area of memory sufficient for the specified bounds. unsafeForeignPtrToStorableArray :: ForeignPtr e -> (i,i) -> IO (StorableArray i e)
It looks like this function is not available in GHC 6.4.1 (see http://www.haskell.org/ghc/docs/6.4.1/html/libraries/base/Data-Array-Storabl... )
It is, however, documented at http://www.haskell.org/HOpenGL/newAPI/base/Foreign-Storable.html but I don't know which version of GHC it applies to.
Yes, it's new since 6.4.x. It will be in 6.6. Cheers, Simon

It is, however, documented at http://www.haskell.org/HOpenGL/newAPI/base/Foreign-Storable.html but I don't know which version of GHC it applies to.
Sorry, I meant at http://www.haskell.org/HOpenGL/newAPI/base/Data-Array-Storable.html Cyril

Chris Kuklewicz wrote:
Cyril Schmidt wrote:
I need to pass a big 2-dimensional array of doubles from a legacy C code to a Haskell module. Of this huge array, the Haskell code will actually use only a few elements (say, 10 out of 100x20 matrix). Unfortunately, the C code does not know which data are actually needed in the Haskell module.
You probably want Foreign.Marshal.Array
http://www.haskell.org/ghc/docs/6.4.1/html/libraries/base/Foreign-Marshal-Ar...
peekArray :: Storable a => Int -> Ptr a -> IO [a]
This works, but peekArray is very slow. Cheers Cyril

Hello Cyril, Wednesday, February 08, 2006, 7:39:51 PM, you wrote:
peekArray :: Storable a => Int -> Ptr a -> IO [a]
CS> This works, but peekArray is very slow.
use the attached module. this will allow you to use usafeRead/unsafeWrite i written it while i'm online so it can contain any mumber of bugs ;) ps: btw, if you know russian - i know it too ;) -- Best regards, Bulat mailto:bulatz@HotPOP.com

Cyril Schmidt wrote:
I was thinking of something like passing the array as Ptr Int#, but how do I fetch the elements that I am interested in?
I think you want Ptr CInt and peekElemOff. If the array is declared in C as int a[100][20] and p :: Ptr CInt is a Haskell pointer to it, then a[y][x] can be accessed as peekElemOff p (y * 20 + x). This should be 100% portable. -- Ben
participants (6)
-
Ben Rudiak-Gould
-
Bulat Ziganshin
-
Chris Kuklewicz
-
Cyril Schmidt
-
Simon Marlow
-
Simon Marlow