フォルス・シェアリングを回避するメモリー割り当てテンプレート・クラス。
template<typename T> class cache_aligned_allocator;
#include "tbb/cache_aligned_allocator.h"
cache_aligned_allocator は、フォルス・シェアリングを回避するため、キャッシュライン境界でメモリーを割り当てます。フォルス・シェアリングは、論理的に別のアイテムが同じキャッシュラインをアクセスするときに発生します。複数のスレッドが異なるアイテムに同時にアクセスすると、パフォーマンスが低下します。アイテムが論理的には別であっても、プロセッサーのハードウェアはそれらのアイテムが 1 つの場所を共有しているかのようにプロセッサー間のキャッシュラインを転送しなければならないことがあります。最終的には、論理的に別のアイテムが異なるキャッシュライン上にある場合よりも、さらに多くのメモリー・トラフィックが発生することがあります。
cache_aligned_allocator は、Allocator コンセプトをモデル化します。std::allocator を置換するために使用できます。注意深く cache_aligned_allocator を使用することで、フォルス・シェアリングが減り、パフォーマンスが改善されます。しかし、キャッシュライン上の割り当ての恩恵は cache_aligned_allocator が暗黙的に追加するパディングメモリーの代償によるものであるため、置換が適切でない場合もあります。パディングは通常 128 バイトです。このため、cache_aligned_allocator で多くの小さなオブジェクトを割り当てると、メモリーの使用量が増加します。
namespace tbb { template<typename T> class cache_aligned_allocator { public: typedef T* pointer; typedef const T* const_pointer; typedef T& reference; typedef const T& const_reference; typedef T value_type; typedef size_t size_type; typedef ptrdiff_t difference_type; template<typename U> struct rebind { typedef cache_aligned_allocator<U> other; }; #if _WIN64 char* _Charalloc( size_type size ); #endif /* _WIN64 */ cache_aligned_allocator() throw(); cache_aligned_allocator( const cache_aligned_allocator& ) throw(); template<typename U> cache_aligned_allocator( const cache_aligned_allocator<U>& ) throw(); ~cache_aligned_allocator(); pointer address(reference x) const; const_pointer address(const_reference x) const; pointer allocate( size_type n, const void* hint=0 ); void deallocate( pointer p, size_type ); size_type max_size() const throw(); void construct( pointer p, const T& value ); void destroy( pointer p ); }; template<> class cache_aligned_allocator<void> { public: typedef void* pointer; typedef const void* const_pointer; typedef void value_type; template<typename U> struct rebind { typedef cache_aligned_allocator<U> other; }; }; template<typename T, typename U> bool operator==( const cache_aligned_allocator<T>&, const cache_aligned_allocator<U>& ); template<typename T, typename U> bool operator!=( const cache_aligned_allocator<T>&, const cache_aligned_allocator<U>& ); }
簡潔に説明するため、次の表では対応する std::allocator のメソッドと大幅に異なるメソッドについてのみ説明します。
メンバー | 説明 |
---|---|
pointer allocate( size_type n, const void* hint=0 ) | キャッシュライン境界上で size バイトのメモリーを割り当てます。境界配置のため割り当てにはパディングが含まれます。 戻り値: 割り当てたメモリーへのポインター。 |
void deallocate( pointer p, size_type n ) | 要件: ポインター p が、allocate(n) メソッドの結果であること。メモリーがすでに割り当て解除されていないこと。 効果: p が指しているメモリーの割り当てを解除し、パディングの割り当ても解除します。 |
char* _Charalloc( size_type size ) | 注このメソッドは 64 ビット Windows* プラットフォーム上でのみ提供されます。このメソッドは、このメソッドを必要とする Windows* のコンテナーのバージョンとの後方互換性のために存在する非 ISO メソッドです。直接使用しないでください。 |