この記事は、インテル® デベロッパー・ゾーンに公開されている『Intel® Threading Building Blocks Documentation』の「cache_aligned_allocator Template Class」(https://software.intel.com/en-us/node/506260) の日本語参考訳です。
概要
フォルス・シェアリングを回避するメモリー割り当てテンプレート・クラス。
構文
1 | template < typename T> class cache_aligned_allocator; |
ヘッダー
1 | #include "tbb/cache_aligned_allocator.h" |
説明
cache_aligned_allocator は、フォルス・シェアリングを回避するため、キャッシュライン境界でメモリーを割り当てます。フォルス・シェアリングは、論理的に異なる項目が同じキャッシュラインを占有する場合に発生します。複数のスレッドが異なる項目に同時にアクセスすると、パフォーマンスが低下します。項目が論理的には別であっても、プロセッサーのハードウェアはそれらの項目が 1 つの場所を共有しているとプロセッサー間でキャッシュラインを転送しなければならないことがあります。最終的には、論理的に異なる項目が異なるキャッシュライン上にある場合よりも、多くのメモリー・トラフィックが発生することがあります。
cache_aligned_allocator は、アロケーターの概念をモデル化します。これは、std::allocator を置き換えることもできます。注意深く使用することで、cache_aligned_allocator はフォルス・シェアリングを回避してパフォーマンスを改善できます。しかし、キャッシュラインに割り当てる利点には、cache_aligned_allocator が暗黙的に追加するパディングメモリーの代償を伴うため、置換は適切でない場合もあります。パディングは通常 128 バイトです。したがって、cache_aligned_allocator を使用して多数の小さなオブジェクトを割り当てると、メモリー使用量が増加することがあります。
メンバー
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | 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 ) | キャッシュライン境界で n バイトのメモリーを割り当てます。境界配置のため割り当てにはパディングが含まれます。 戻り値: 割り当てたメモリーへのポインター。 |
void deallocate( pointer p, size_type n ) | 必要条件: ポインター p は、allocate(n) メソッドの結果である必要があります。メモリーがすでに割り当て解除されていてはなりません。 効果: ポインター p が指すメモリーの割り当てを解除します。パディング割り当ても同様に解除します。 |
char* _Charalloc( size_type size ) |
注このメソッドは 64 ビット Windows* プラットフォーム上でのみ提供されます。このメソッドは、このメソッドを必要とする Windows* のコンテナーのバージョンとの後方互換性のために存在する非 ISO メソッドです。直接使用しないでください。 |
上位トピック: Allocator の概念
https://software.intel.com/node/4983ab72-5b5a-4965-ab69-6f9fd67db776
コンパイラーの最適化に関する詳細は、最適化に関する注意事項を参照してください。