-
Notifications
You must be signed in to change notification settings - Fork 141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Alignment isn't respected in Data.Vector.Storable. #75
Comments
What does this mean exactly? Is it aligning to 8 and overwriting things? |
It means the items aren't aligned in memory as they should be, which means peeking and pokeing may produce undefined behaviour.
|
Could you share your storable instance code? On Fri, Feb 6, 2015, 1:56 PM Daniel Waterworth notifications@github.com
|
I guess it could happen if |
Here you go:
|
https://ghc.haskell.org/trac/ghc/ticket/9806 is a (possibly) related ghc ticket. |
looks like storable vector doesnt do the right thing {-# INLINE mallocVector #-}
mallocVector :: Storable a => Int -> IO (ForeignPtr a)
mallocVector =
#if __GLASGOW_HASKELL__ >= 605
doMalloc undefined
where
doMalloc :: Storable b => b -> Int -> IO (ForeignPtr b)
doMalloc dummy size = mallocPlainForeignPtrBytes (size * sizeOf dummy)
#else
mallocForeignPtrArray
#endif |
ok, i think the solution is that we need to use
which is defined in Next question: what range of ghc versions support this? |
@dolio whatever fix we do for this in master, we should /probably also do a patchlevel bug fix for |
https://github.com/haskell/vector/blob/master/Data/Vector/Storable/Mutable.hs is where the allocation for storable vectors is defined |
@hvr has pointed me to https://ghc.haskell.org/trac/ghc/ticket/7067 so we can only directly use the provided operation in GHC >= 7.6, |
ok, we've had |
ok, 6.12 too. I dont think 6.10 had it. but >=6.12 should be a large enough supported range :) |
Wow @cartazio, eight consecutive comments. I've got to say, I'm impressed. |
@danielwaterworth combo-breaker! ;-) |
ok, i think given some of the other changes slated for vector 0.11, newAlignedPinnedByteArray# should be on the table for storable vectors in 0.11 release |
Hi, I was trying to fix this issue but couldn't find a backwards-compatible way to construct ForeignPtr from MutableByteArray# type returned by newAlignedPinnedByteArray#. There's a way to obtain Addr# from the mutable array, but it cannot be straightforwardly converted into ForeignPtr. The reason is that only starting from 7.6.3 the Ghc.ForeignPtr module started exporting ForeignPtrContents datatype required to apply ForeignPtr constructor - see https://downloads.haskell.org/~ghc/7.6.3/docs/html/libraries/base-4.6.0.1/src/GHC-ForeignPtr.html (compare with ghc 7.4.2 where it's not exported https://downloads.haskell.org/~ghc/7.4.2/docs/html/libraries/base-4.5.1.0/src/GHC-ForeignPtr.html). Another possibility is to convert Addr# to Ptr and then wrap Ptr into ForeignPtr. But the only way to obtain Ptr from Addr# seems to be casting via integers. I'm not sure whether this is a good idea. I should mention in passing, maybe it's ok to use mallocPlainForeignPtrAlignedBytes on newer ghcs and keep old behavior on older ones? |
so to convert an Addr# to Ptr you just need the Ptr constructor which is available in |
did we fix this with pr #96 ? |
I'm going to close this. I think it should be fixed by #96. For reference, in case this comes back up, I'm somewhat skeptical of the example given. Of course, it wasn't even enforcing 8-byte alignment before, which was wrong. |
There is. It means that the whole thing fits on one cache line. |
Actually, I do have a use for it, which is the same one that @danielwaterworth mentions. I have code that needs stuff to align to cache line sizes to avoid cache-line contention a lot of places. This is the same behavior as the cache_aligned_allocator in intel's thread building blocks in C++. Otherwise I have to double the size of everything yet again to avoid false sharing. |
This sort of alignment is only possible currently for storable vectors On Thursday, August 11, 2016, Edward Kmett notifications@github.com wrote:
|
Yes, as they are the only thing using, you know, |
I have a type where the alignment of the type is greater than 8 bytes. When I create a vector, using
fromList
for example, I find that the alignment isn't respected.I'm using version 0.10.12.2.
The text was updated successfully, but these errors were encountered: