small_vector alignment

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

small_vector alignment

Tobias Loew
Hi,

I wanted to use a boost::container::small_vector for 64-bit aligned doubles (to be used with SSE):

    typedef boost::container::small_vector<double, 16, aligned_allocator<double, sizeof(__m128)> > sse_double_vector;
But it seems that the "small-buffer" has a fixed calculated alignment which only depends on the value-type and does not consider the alignment of the (dynamic) allocator.
Is this intended or is it a bug in the implementation?

best regards

Tobias
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: small_vector alignment

Glen Fernandes
> I wanted to use a boost::container::small_vector for 64-bit aligned
> doubles (to be used with SSE):
>
>    typedef boost::container::small_vector<double, 16,
>        aligned_allocator<double, sizeof(__m128)> > sse_double_vector;
>
> But it seems that the "small-buffer" has a fixed calculated alignment
> which only depends on the value-type and does not consider the alignment
> of the (dynamic) allocator.

Hi Tobias,

Yes, it looks like small_vector does not expose any mechanism for you to control the alignment of its internal "small" storage. So you can only expect that for 'double' the storage is at least alignof(double) aligned. (The aligned_allocator will only apply after that 'small' storage is exhausted of course.)

One solution could be to use std::vector instead with aligned_allocator_adaptor with in conjunction with another custom allocator for a kind of small-size optimization e.g.

    sso_storage<N> s;
    std::vector<double, aligned_allocator_adaptor<sso_allocator<double, N>, alignof(__m128)> > sse_double_vector(sso_allocator<double, N>(s));

(i.e. The 'sso_allocator' and 'sso_storage' is what you would have to implement.)

I would suggest also opening an feature request to Ion to allow providing a mechanism to specify the alignment of the small_vector storage type.

Glen
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: small_vector alignment

Boost - Dev mailing list
On 21 March 2017 at 07:55, Glen Fernandes via Boost <[hidden email]>
wrote:

> I would suggest also opening an feature request to Ion to allow providing a
> mechanism to specify the alignment of the small_vector storage type.


This (the mechanisme) is very usefull (and I can stop to r-m-o due to this
issue).

+1

degski

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: small_vector alignment

Tobias Loew
In reply to this post by Glen Fernandes
Hi Glen,

thanks for the tip with the allocator. In the meantime, I came up with another solution: I made a new double type (not just a typedef) and specialized its allignment:

BOOST_STRONG_TYPEDEF(double, double_m128);

namespace boost {
    namespace move_detail {
        template<>
        struct alignment_of<double_m128>
        {
            static const std::size_t value = sizeof(__m128);
        };
    }  //namespace move_detail {
}  //namespace boost {

sizeof(double_m128) == sizeof(double), "???");

typedef boost::container::small_vector<double_m128, 16, aligned_allocator<double_m128, sizeof(__m128)> > sse_double_vector;

Its only drawback is that accessing the "data()" member returns a double_m128* which has to be reinterpreted as a double*.

Tobias
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: small_vector alignment

Tobias Loew
This post was updated on .
I've made a pull-request for the container-module. Comments are welcome.

Tobias
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: small_vector alignment

Boost - Dev mailing list
On 27/03/2017 12:42, Tobias Loew via Boost wrote:
> I've made a  pull-request <https://github.com/boostorg/container/pull/47>
> for the container-module. Comments are wellcome.
>
> Tobias

The alignment could be part of the allocator's compile-time properties.
At least, this does not require passing the alignment as an extra
template parameter.

Best,

Ion


_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Loading...