]> git.sesse.net Git - stockfish/commitdiff
Cache line aligned TT
authorMarco Costalba <mcostalba@gmail.com>
Fri, 26 Apr 2013 16:45:54 +0000 (18:45 +0200)
committerMarco Costalba <mcostalba@gmail.com>
Fri, 26 Apr 2013 17:38:11 +0000 (19:38 +0200)
Let TT clusters (16*4=64 bytes) to hold on a singe cache line.
This avoids the need for the double prefetch.

Original patches by Lucas and Jean-Francois that has also tested
on his AMD FX:

BIG HASHTABLE

./stockfish bench 1024 1 18 > /dev/null

Before:
1437642 nps
1426519 nps
1438493 nps

After:
1474482 nps
1476375 nps
1475877 nps

SMALL HASHTABLE

./stockfish bench 128 1 18 > /dev/null

Before:
1435207 nps
1435586 nps
1433741 nps

After:
1479143 nps
1471042 nps
1472286 nps

No functional change.

src/misc.cpp
src/tt.cpp
src/tt.h
src/types.h

index c0c00b009fa1ab347ebb97599a2a1e687d4346ca..477fb392c6add636f01a46c7e0bf1b7ca51113c4 100644 (file)
@@ -237,10 +237,8 @@ void prefetch(char* addr) {
 
 #  if defined(__INTEL_COMPILER) || defined(_MSC_VER)
   _mm_prefetch(addr, _MM_HINT_T0);
-  _mm_prefetch(addr+64, _MM_HINT_T0); // 64 bytes ahead
 #  else
   __builtin_prefetch(addr);
-  __builtin_prefetch(addr+64);
 #  endif
 }
 
index 80ea493bd9c68e3593936bc7bee7a101c195a0de..998d73783be003e8deb2567695e51527a7657bf3 100644 (file)
@@ -39,18 +39,18 @@ void TranspositionTable::set_size(size_t mbSize) {
   if (hashMask == size - ClusterSize)
       return;
 
-  hashMask = size - ClusterSize;
-  delete [] table;
-  table = new (std::nothrow) TTEntry[size];
-
-  if (!table)
+  free(mem);
+  mem = malloc(size * sizeof(TTEntry) + (CACHE_LINE_SIZE - 1));
+  if (!mem)
   {
       std::cerr << "Failed to allocate " << mbSize
                 << "MB for transposition table." << std::endl;
       exit(EXIT_FAILURE);
   }
 
-  clear(); // Operator new is not guaranteed to initialize memory to zero
+  table = (TTEntry*)((size_t(mem) + CACHE_LINE_SIZE - 1) & ~(CACHE_LINE_SIZE - 1));
+  hashMask = size - ClusterSize;
+  clear(); // Newly allocated block of memory is not initialized
 }
 
 
index c7c39e90cc9918d3cddb8611a12d8e66ffbf63a8..1caa277e821ac0463110fe0df6487b74f7e20f79 100644 (file)
--- a/src/tt.h
+++ b/src/tt.h
@@ -85,7 +85,7 @@ class TranspositionTable {
   static const unsigned ClusterSize = 4; // A cluster is 64 Bytes
 
 public:
- ~TranspositionTable() { delete [] table; }
+ ~TranspositionTable() { free(mem); }
   void new_search() { generation++; }
 
   TTEntry* probe(const Key key) const;
@@ -98,6 +98,7 @@ public:
 private:
   uint32_t hashMask;
   TTEntry* table;
+  void* mem;
   uint8_t generation; // Size must be not bigger then TTEntry::generation8
 };
 
index 1def7d00aeb8f200230c7b50911e71c2acfdeb91..dae86db310d6d80bc715e99c7aafd9ca482225e4 100644 (file)
 #   include <xmmintrin.h> // Intel and Microsoft header for _mm_prefetch()
 #  endif
 
+#define CACHE_LINE_SIZE 64
 #if defined(_MSC_VER) || defined(__INTEL_COMPILER)
-#  define CACHE_LINE_ALIGNMENT __declspec(align(64))
+#  define CACHE_LINE_ALIGNMENT __declspec(align(CACHE_LINE_SIZE))
 #else
-#  define CACHE_LINE_ALIGNMENT  __attribute__ ((aligned(64)))
+#  define CACHE_LINE_ALIGNMENT  __attribute__ ((aligned(CACHE_LINE_SIZE)))
 #endif
 
 #if defined(_MSC_VER)