Subversion Repositories Games.Chess Giants

Rev

Rev 169 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 169 Rev 185
Line 4... Line 4...
4
  Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
4
  Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
5
  Copyright (C) 2015-2018 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
5
  Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
6
 
6
 
7
  Stockfish is free software: you can redistribute it and/or modify
7
  Stockfish is free software: you can redistribute it and/or modify
8
  it under the terms of the GNU General Public License as published by
8
  it under the terms of the GNU General Public License as published by
9
  the Free Software Foundation, either version 3 of the License, or
9
  the Free Software Foundation, either version 3 of the License, or
10
  (at your option) any later version.
10
  (at your option) any later version.
Line 18... Line 18...
18
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
*/
19
*/
20
 
20
 
21
#include <cstring>   // For std::memset
21
#include <cstring>   // For std::memset
22
#include <iostream>
22
#include <iostream>
-
 
23
#include <thread>
23
 
24
 
24
#include "bitboard.h"
25
#include "bitboard.h"
-
 
26
#include "misc.h"
25
#include "tt.h"
27
#include "tt.h"
-
 
28
#include "uci.h"
26
 
29
 
27
TranspositionTable TT; // Our global transposition table
30
TranspositionTable TT; // Our global transposition table
-
 
31
 
-
 
32
/// TTEntry::save saves a TTEntry
-
 
33
void TTEntry::save(Key k, Value v, Bound b, Depth d, Move m, Value ev) {
-
 
34
 
-
 
35
  assert(d / ONE_PLY * ONE_PLY == d);
-
 
36
 
-
 
37
  // Preserve any existing move for the same position
-
 
38
  if (m || (k >> 48) != key16)
-
 
39
      move16 = (uint16_t)m;
-
 
40
 
-
 
41
  // Overwrite less valuable entries
-
 
42
  if (  (k >> 48) != key16
-
 
43
      || d / ONE_PLY > depth8 - 4
-
 
44
      || b == BOUND_EXACT)
-
 
45
  {
-
 
46
      key16     = (uint16_t)(k >> 48);
-
 
47
      value16   = (int16_t)v;
-
 
48
      eval16    = (int16_t)ev;
-
 
49
      genBound8 = (uint8_t)(TT.generation8 | b);
-
 
50
      depth8    = (int8_t)(d / ONE_PLY);
-
 
51
  }
-
 
52
}
28
 
53
 
29
 
54
 
30
/// TranspositionTable::resize() sets the size of the transposition table,
55
/// TranspositionTable::resize() sets the size of the transposition table,
31
/// measured in megabytes. Transposition table consists of a power of 2 number
56
/// measured in megabytes. Transposition table consists of a power of 2 number
32
/// of clusters and each cluster consists of ClusterSize number of TTEntry.
57
/// of clusters and each cluster consists of ClusterSize number of TTEntry.
33
 
58
 
34
void TranspositionTable::resize(size_t mbSize) {
59
void TranspositionTable::resize(size_t mbSize) {
35
 
60
 
36
  size_t newClusterCount = mbSize * 1024 * 1024 / sizeof(Cluster);
61
  clusterCount = mbSize * 1024 * 1024 / sizeof(Cluster);
37
 
-
 
38
  if (newClusterCount == clusterCount)
-
 
39
      return;
-
 
40
 
-
 
41
  clusterCount = newClusterCount;
-
 
42
 
62
 
43
  free(mem);
63
  free(mem);
44
  mem = malloc(clusterCount * sizeof(Cluster) + CacheLineSize - 1);
64
  mem = malloc(clusterCount * sizeof(Cluster) + CacheLineSize - 1);
45
 
65
 
46
  if (!mem)
66
  if (!mem)
Line 53... Line 73...
53
  table = (Cluster*)((uintptr_t(mem) + CacheLineSize - 1) & ~(CacheLineSize - 1));
73
  table = (Cluster*)((uintptr_t(mem) + CacheLineSize - 1) & ~(CacheLineSize - 1));
54
  clear();
74
  clear();
55
}
75
}
56
 
76
 
57
 
77
 
58
/// TranspositionTable::clear() overwrites the entire transposition table
78
/// TranspositionTable::clear() initializes the entire transposition table to zero,
59
/// with zeros. It is called whenever the table is resized, or when the
79
//  in a multi-threaded way.
60
/// user asks the program to clear the table (from the UCI interface).
-
 
61
 
80
 
62
void TranspositionTable::clear() {
81
void TranspositionTable::clear() {
63
 
82
 
64
  std::memset(table, 0, clusterCount * sizeof(Cluster));
83
  std::vector<std::thread> threads;
65
}
-
 
66
 
84
 
-
 
85
  for (size_t idx = 0; idx < Options["Threads"]; idx++)
-
 
86
  {
-
 
87
      threads.emplace_back([this, idx]() {
-
 
88
 
-
 
89
          // Thread binding gives faster search on systems with a first-touch policy
-
 
90
          if (Options["Threads"] > 8)
-
 
91
              WinProcGroup::bindThisThread(idx);
-
 
92
 
-
 
93
          // Each thread will zero its part of the hash table
-
 
94
          const size_t stride = clusterCount / Options["Threads"],
-
 
95
                       start  = stride * idx,
-
 
96
                       len    = idx != Options["Threads"] - 1 ?
-
 
97
                                stride : clusterCount - start;
-
 
98
 
-
 
99
          std::memset(&table[start], 0, len * sizeof(Cluster));
-
 
100
      });
-
 
101
  }
-
 
102
 
-
 
103
  for (std::thread& th: threads)
-
 
104
      th.join();
-
 
105
}
67
 
106
 
68
/// TranspositionTable::probe() looks up the current position in the transposition
107
/// TranspositionTable::probe() looks up the current position in the transposition
69
/// table. It returns true and a pointer to the TTEntry if the position is found.
108
/// table. It returns true and a pointer to the TTEntry if the position is found.
70
/// Otherwise, it returns false and a pointer to an empty or least valuable TTEntry
109
/// Otherwise, it returns false and a pointer to an empty or least valuable TTEntry
71
/// to be replaced later. The replace value of an entry is calculated as its depth
110
/// to be replaced later. The replace value of an entry is calculated as its depth
Line 78... Line 117...
78
  const uint16_t key16 = key >> 48;  // Use the high 16 bits as key inside the cluster
117
  const uint16_t key16 = key >> 48;  // Use the high 16 bits as key inside the cluster
79
 
118
 
80
  for (int i = 0; i < ClusterSize; ++i)
119
  for (int i = 0; i < ClusterSize; ++i)
81
      if (!tte[i].key16 || tte[i].key16 == key16)
120
      if (!tte[i].key16 || tte[i].key16 == key16)
82
      {
121
      {
83
          if ((tte[i].genBound8 & 0xFC) != generation8 && tte[i].key16)
-
 
84
              tte[i].genBound8 = uint8_t(generation8 | tte[i].bound()); // Refresh
122
          tte[i].genBound8 = uint8_t(generation8 | tte[i].bound()); // Refresh
85
 
123
 
86
          return found = (bool)tte[i].key16, &tte[i];
124
          return found = (bool)tte[i].key16, &tte[i];
87
      }
125
      }
88
 
126
 
89
  // Find an entry to be replaced according to the replacement strategy
127
  // Find an entry to be replaced according to the replacement strategy