By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,590 Members | 1,145 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,590 IT Pros & Developers. It's quick & easy.

fixed type allocator - Intel IPP

P: n/a
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! ]

Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Sat, 28 May 2005 15:03:23 +0400, Joshua Kolden
<jo****@koldnhostile.com> wrote:
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> {};
// ...

--
Maxim Yegorushkin

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

Jul 23 '05 #2

P: n/a
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! ]

Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.