FIAS . Impressum . Privacy

Segmentation fault when using Vc::Memory as a member of a heap-allocated object

Matthias Kretz [please enable javascript to see the address]
Thu Feb 20 16:39:36 CET 2014


Hi Georgios,

welcome to our little community of Vc users (and devs) :)

On Thursday 20 February 2014 14:42:09 Georgios Bitzes wrote:
> I would like to report an issue I came across while using Vc. Here is a
> short example that reproduces it:
> 
> #include <Vc/Vc>
> 
> class A {
>     private:
>         Vc::Memory<Vc::double_v, 3> internal;
> };
> 
> int main() {
>     std::cout << "size: " << Vc::double_v::Size << std::endl;
>     A *a = new A();
>     return 0;
> }
> 
> 
> The example compiles but segfaults during the creation of object a.

It doesn't surprise me, and it's really frustrating that this is to be 
expected.

> On the contrary, if I replace "A *a = new A()" with "A a()" it runs fine.
> This appears to happen only when the object is allocated on the heap, not
> on the stack.

Also expected.

> Is this a bug on the part of Vc or am I doing something wrong?

This is a shortcoming in C++. The new operator ignores the alignment 
requirement of the type it allocates. Vc::Memory<Vc::double_v, 3> has an 
alignment of 32 Bytes when compiled for AVX. But on 64-bit Linux "new" simply 
aligns to 16 Bytes. So you may be lucky and it works, or well it segfaults. A 
misaligned SIMD load/store is a segfault. You have a 50% chance.

There is no way for Vc to fix this. It will hopefully be fixed in C++17. Till 
then you have to manually help the compiler:

class A : public Vc::VectorAlignedBase {
    private:
        Vc::Memory<Vc::double_v, 3> internal;
};

should work already.

See here for more on alignment issues:
http://code.compeng.uni-frankfurt.de/docs/Vc-0.7/intro.html#intro_alignment

BTW, if you compile your code for SSE it won't crash, unless you compile for 
32-bit Linux SSE - there the standard alignment is 8...

Cheers,
  Matthias

-- 
─────────────────────────────────────────────────────────────
 Dipl.-Phys. Matthias Kretz

 Web:   http://compeng.uni-frankfurt.de/?mkretz

 SIMD easy and portable: http://compeng.uni-frankfurt.de/?vc
─────────────────────────────────────────────────────────────
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part.
URL: <http://compeng.uni-frankfurt.de/pipermail/vc/attachments/20140220/4c9d8ea5/attachment.pgp>


More information about the Vc mailing list
FIAS . Impressum . Privacy