[Fwd: Memory corruption issues when using newAlignedPinnedByteArray, GC kicking in?]
marlowsd at gmail.com
Wed Jul 11 10:50:01 CEST 2012
On 10/07/2012 23:03, Nicolas Trangez wrote:
> I sent this mail to Haskell Cafe earlier today, and was pointed  at
> this list. As such...
> Any help/advice would be greatly appreciated!
It looks like you're making a ForeignPtr from the Addr# or Ptr that
points to the contents of the ByteArray#. Since this ForeignPtr doesn't
keep the original ByteArray# alive, the GC will collect it. You need to
keep a reference to the ByteArray# too.
Basically you need a version of mallocForeignPtrBytes that has supports
alignment. Unfortunately it's not possible to write one because the
internals of ForeignPtrContents are not exported - we had a recent
ticket about that (http://hackage.haskell.org/trac/ghc/ticket/7012) and
in 7.6.1 we will export the necessary internals. If you want we could
also add mallocForeignPtrAlignedBytes - please send a patch.
>  http://www.haskell.org/pipermail/haskell-cafe/2012-July/102242.html
> -------- Forwarded Message --------
>> From: Nicolas Trangez <nicolas at incubaid.com>
>> To: haskell-cafe at haskell.org
>> Cc: Roman Leshchinskiy <rl at cse.unsw.edu.au>
>> Subject: Memory corruption issues when using
>> newAlignedPinnedByteArray, GC kicking in?
>> Date: Tue, 10 Jul 2012 19:20:01 +0200
>> While working on my vector-simd library, I noticed somehow memory I'm
>> using gets corrupted/overwritten. I reworked this into a test case, and
>> would love to get some help on how to fix this.
>> Previously I used some custom FFI calls to C to allocate aligned memory,
>> which yields correct results, but this has a significant (+- 10x)
>> performance impact on my benchmarks. Later on I discovered the
>> newAlignedPinnedByteArray# function, and wrote some code using this.
>> Here's what I did in the test case: I created an MVector instance, with
>> the exact same implementation as vector's
>> Data.Vector.Storable.Mutable.MVector instance, except for basicUnsafeNew
>> where I pass one more argument to mallocVector .
>> I also use 3 different versions of mallocVector (depending on
>> compile-time flags):
>> mallocVectorOrig : This is the upstream version, discarding the
>> integer argument I added.
>> Then here's my first attempt, very similar to the implementation of
>> mallocPlainForeignPtrBytes  at  using GHC.* libraries.
>> Finally there's something similar at  which uses the 'primitive'
>> The test case creates vectors of increasing size, then checks whether
>> they contain the expected values. For the default implementation this
>> works correctly. For both others it fails at some random size, and the
>> values stored in the vector are not exactly what they should be.
>> I don't understand what's going on here. I suspect I lack a reference
>> (or something along those lines) so GC kicks in, or maybe the buffer
>> gets relocated, whilst it shouldn't.
>> Basically I'd need something like
>> GHC.ForeignPtr.mallocPlainAlignedForeignPtrBytes :: Int -> Int -> IO
>> (ForeignPtr a)
>>  https://gist.github.com/3084806#LC37
>>  https://gist.github.com/3084806#LC119
>>  https://gist.github.com/3084806#LC100
>>  https://gist.github.com/3084806#LC81
> Glasgow-haskell-users mailing list
> Glasgow-haskell-users at haskell.org
More information about the Glasgow-haskell-users