fixed type allocator - Intel IPP

J

Joshua Kolden

STL allocators are templates so that when you write one you are obliged
to make it work with any type. However, the Intel IPP library that we
use has memory aligned allocators for each of 15 different types. For
example an 8 bit unsigned array is allocated with ippsMalloc_8u(size).

So I want to create memory aligned allocators for use with the STL (in
particular the vector container) that is fast (due to alignment), and
pointer compatible with other IPP library functions.

Is there any way to create an allocator of a fixed type? Right now it
seems the only way it can work is:

vector<Ipp8u, myAllocIpp8u<Ipp8u>> ipp8uVector;

But A) its redundant and liable to lead to allocator mismatch, and B)
leaves the internal rebind typecast undefined.

I'd prefer either:

vector<Ipp8u, myAllocIpp8u> ipp8uVector; // fixed type allocator

or

vector<Ipp8u, myAlloc<Ipp8u>> ipp8uVector; // allocator that can
// switch by type

But I don't see how either of these are possible.

Thanks,
j
 
M

Maxim Yegorushkin

STL allocators are templates so that when you write one you are obliged
to make it work with any type. However, the Intel IPP library that we
use has memory aligned allocators for each of 15 different types. For
example an 8 bit unsigned array is allocated with ippsMalloc_8u(size).

So I want to create memory aligned allocators for use with the STL (in
particular the vector container) that is fast (due to alignment), and
pointer compatible with other IPP library functions.

Please note, that std::vector<> stores its elements in an array. Elements
in arrays in C and C++ are stored contiguously with no padding between
them, which means that if you force the alignment of an array to be more
than that of element type's natural one, you only align so the first
element of the array, all other array elements remain aligned with their
natural alignment.

Also, no matter what alignment, std::vector<> may use or may not use
standard algorithms in its implementation, which means that you'd probably
have to specialize algorithms like std::copy, std::uninitialized_copy,
etc.., to take advantage of IPP library functions.

Not sure I understand "pointer compatible" issue.
Is there any way to create an allocator of a fixed type? Right now it
seems the only way it can work is:

vector<Ipp8u, myAllocIpp8u<Ipp8u>> ipp8uVector;

But A) its redundant and liable to lead to allocator mismatch, and B)
leaves the internal rebind typecast undefined.

I'd prefer either:

vector<Ipp8u, myAllocIpp8u> ipp8uVector; // fixed type allocator

or

vector<Ipp8u, myAlloc<Ipp8u>> ipp8uVector; // allocator that can
// switch by type

Create your own allocator which kicks in for your types and uses stock
std::allocator implementation for all other type. Something along these
lines:

template<class T = void>
struct my_alloc;

template<class T>
struct my_alloc_impl
{
template<class U>
struct rebind { typedef my_alloc<U> other; };

// std::allocator interface implementation
};

template<class T>
struct my_alloc : std::allocator<T>
{
// override std::allocator<T>'s rebind
template<class U>
struct rebind { typedef my_alloc<U> other; };
};

// specialize it for your types
template<> struct my_alloc<whatever> : my_alloc_impl<whatever> {};
// ...
 
J

James Daughtry

You can create an allocator that switches by type with template
specialization:

template <typename T>
class myAlloc {
// Generic allocator
};

template <>
class myAlloc<Ipp8u> {
// Specialized for Ipp8u
};

You can also use fixed type allocators provided you define them with
the same operations as are expected. For example, rebind is expected to
be a template class with a nested type 'other'. You can effectively
make that a noop without breaking the interface by saying:

class myAllocIpp8u {
public:
// ...
template <typename T> struct rebind { typedef myAllocIpp8u other; };
// ...
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top