About unsafeThaw and unsafeFreeze efficiency ....

heron_carvalho at bol.com.br heron_carvalho at bol.com.br
Wed Nov 5 03:17:11 EST 2003


     in GHC documentation, it is said :

"unsafeThaw :: (Ix i, IArray a e, MArray b e m) => a i e -
> m (b i e)
Converts an immutable array into a mutable array without 
taking a copy. This function is "unsafe" because any 
subsequent modifications made to the mutable version of 
the array will be shared with the immutable version. It
is safe to use, therefore, if the immutable version is 
never referenced
again. "

    I am trying to use unsafeThaw in a parallel program 
written in a parallel language that I am developing. A 
process have to transmit a UArray Int Double to another 
process. I used unsafeThaw to translate the UArray into a 
StorableArray. Thus, using the function 
withStorableArray, it is possible to access the array 
elements (double's) in a contiguos buffer that could be 
sent directly through MPI. The receiving process fetch 
the message (MPI_Recv) in a buffer and copy their 
elements (including array bounds information) in a 
StorableArray (using withStorableArray and copyArray).
Then, using unsafeFreeze applied to the StorableArray, 
the original UArray Int Double sent by the sender is 

     But this is efficient only if the conversion from 
UArray to StorableArray, using unsafe versions of thaw 
and freeze, does not take a copy, as GHC documentation 
suggests to the reader. However, my experiments
(profiling, measurements, etc.) have detected a source of 
ineficiency in the application of unsafeThaw and 
unsafreFreeze.  It appears to be more efficient to 
translate the array to a list (using elems) and then copy 
the list in a "Ptr" array using pokeArray.

     unsafeThaw is really "free of copy" ?? Any 
exception ?? I need a way to access an unboxed immutable 
array in a contiguos buffer in constant time, in
order to minimize marshalling overheads for preparing a 
message to be sent through a network, using MPI. This is 
important for parallel scientific code, that makes 
extensive use of arrays and have to transmitt them 
between processes.

     Any suggestion ?


Acabe com aquelas janelinhas que pulam na sua tela.
AntiPop-up UOL - É grátis!

More information about the Glasgow-haskell-users mailing list