Passing a matrix from C to Haskell

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Passing a matrix from C to Haskell

Cyril Schmidt
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
 

 

_______________________________________________
Glasgow-haskell-users mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users
Reply | Threaded
Open this post in threaded view
|

Re: Passing a matrix from C to Haskell

haskell-2
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-Array.html

peekArray :: Storable a => Int -> Ptr a -> IO [a]
_______________________________________________
Glasgow-haskell-users mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users
Reply | Threaded
Open this post in threaded view
|

Re[2]: Passing a matrix from C to Haskell

Bulat Ziganshin
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-Array.html

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:[hidden email]



_______________________________________________
Glasgow-haskell-users mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users
Reply | Threaded
Open this post in threaded view
|

Re: Passing a matrix from C to Haskell

Simon Marlow-5
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-Array.html
>
> 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
_______________________________________________
Glasgow-haskell-users mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users
Reply | Threaded
Open this post in threaded view
|

Re: Passing a matrix from C to Haskell

Cyril Schmidt
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-Storable.html
)

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

_______________________________________________
Glasgow-haskell-users mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users
Reply | Threaded
Open this post in threaded view
|

Re: Passing a matrix from C to Haskell

Cyril Schmidt
In reply to this post by haskell-2
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-Array.html
>
> peekArray :: Storable a => Int -> Ptr a -> IO [a]
>
This works, but peekArray is very slow.

Cheers

Cyril



_______________________________________________
Glasgow-haskell-users mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users
Reply | Threaded
Open this post in threaded view
|

Re: Passing a matrix from C to Haskell

Cyril Schmidt
In reply to this post by Simon Marlow-5
> 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
_______________________________________________
Glasgow-haskell-users mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users
Reply | Threaded
Open this post in threaded view
|

Re: Passing a matrix from C to Haskell

Simon Marlow
In reply to this post by Cyril Schmidt
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-Storable.html
> )
>
> 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

_______________________________________________
Glasgow-haskell-users mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users
Reply | Threaded
Open this post in threaded view
|

Re[2]: Passing a matrix from C to Haskell

Bulat Ziganshin
In reply to this post by Cyril Schmidt
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:[hidden email]
_______________________________________________
Glasgow-haskell-users mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users

ForeignArray.hs (4K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Passing a matrix from C to Haskell

Ben Rudiak-Gould
In reply to this post by Cyril Schmidt
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

_______________________________________________
Glasgow-haskell-users mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users