Connecting Tech Pros Worldwide Forums | Help | Site Map

fixed type allocator - Intel IPP

Joshua Kolden
Guest
 
Posts: n/a
#1: Jul 23 '05
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

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Maxim Yegorushkin
Guest
 
Posts: n/a
#2: Jul 23 '05

re: fixed type allocator - Intel IPP


On Sat, 28 May 2005 15:03:23 +0400, Joshua Kolden
<joshua@koldnhostile.com> wrote:
[color=blue]
> 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.[/color]

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.
[color=blue]
> 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[/color]

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> {};
// ...

--
Maxim Yegorushkin

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

James Daughtry
Guest
 
Posts: n/a
#3: Jul 23 '05

re: fixed type allocator - Intel IPP


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; };
// ...
};


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Closed Thread