[Haskell-cafe] vector, alignment and SIMD through FFI

Nicolas Trangez nicolas at incubaid.com
Sat Jul 7 00:25:36 CEST 2012

On Fri, 2012-07-06 at 13:43 -0700, Thomas DuBuisson wrote:
> On Fri, Jul 6, 2012 at 1:06 PM, Nicolas Trangez <nicolas at incubaid.com> wrote:
> >     -- This fails:
> >     -- Ambiguous type variable `a0' in the constraint:
> >     --   (Storable a0) arising from a use of `sizeOf'
> Here you can either tie a type knot using proxy types or you can use
> the scoped type variable language extension.

Guess I'll have to do some reading ;-) Thanks.

> Perhaps I'm missing something specific to your use, but for the
> alignment issue you should be OK just calling allocBytes or one of its
> variants.  I made some noise about this a bit ago and it resulted in
> some extra words in the report under mallocBytes:
> """
> The block of memory is sufficiently aligned for any of the basic
> foreign types that fits into a memory block of the allocated size.
> """
> Which I'm pretty sure GHC did, and still does, follow.

Hmh... as far as I could find, mallocBytes basically does what malloc(3)
does, which is 8-byte alignment if I'm not mistaken on my x86-64 Linux
system. I could use those and the over-allocate-and-offset tricks,
but... that's ugly unless strictly necessary ;-)

Normally posix_memalign or memalign or valloc or _mm_malloc should
provide what I need as-is.

Except, when using those and vector's unsafeFromForeignPtr0, all I get
is a "Vector a", which no longer has any alignment information in the
type, so I can't write a function which only accepts N-aligned vectors.
As a result, I'd need to be very careful only to pass aligned vectors to
it (checking manually), add code to handle pre/post-alignment bytes in
my SIMD functions (slow and stupid), or live with it and let my
application crash at random.

I found some work by Oleg Kiselyov and Chung-chieh Shan at [1] which
might be related, yet as of now I feel like that's too general for my
purpose (e.g. I don't see how to integrate it with vector).



[1] http://okmij.org/ftp/Haskell/types.html#ls-resources

More information about the Haskell-Cafe mailing list