UArray question....

Simon Marlow simonmar at
Mon Nov 3 13:31:52 EST 2003

>    In GHC/FFI, is there some way (is it possible ?) to 
> access DIRECTLY an array of UArray type (immutable) in a 
> sequential contiguous memory buffer (in C side, for 
> example) without to need to copy the array elements one 
> by one ? GHC hackers and implementors are wellcome... :-) 
> My interest is to transmit the array over a network using 
> MPI, but avoiding marshalling overheads. 
>     It is easy to do this with StorableArray's in the IO 
> monad, of course, but I need to use UArray.

I've been pondering this issue for a while.  In fact, I have a
replacement for StorableArray which addresses this issue, amongst
others.  I provide the following function:

  unsafeStorableArrayToIOUArray :: StorableArray i e -> IOUArray i e

This function works in O(1) time in GHC's implementation, because it
uses the same representation type for StorableArray and IOUArray (well,
in my hacked version of StorableArray this is true).  The function has
an 'unsafe' prefix, because it only works when the representations of
the two arrays are the same (currently true for all 'e' except Bool).

>From an IOUArray, you can get a UArray by using unsafeFreeze.

A better way might be to have an immutable StorableArray type, that you
can get by freezing a StorableArray, and that also supports passing to
C.  This involves a lot of extra code, though.


More information about the Glasgow-haskell-users mailing list