Skip to content

Commit 0ab2109

Browse files
committed
Merge branch 'master' into probCutUpd
2 parents 2a68f9d + 1d3efff commit 0ab2109

File tree

5 files changed

+49
-21
lines changed

5 files changed

+49
-21
lines changed

src/evaluate.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ namespace {
168168
template<Color Us> Score passed() const;
169169
template<Color Us> Score space() const;
170170
ScaleFactor scale_factor(Value eg) const;
171-
Score initiative(Score score, Score materialScore) const;
171+
Score initiative(Score score) const;
172172

173173
const Position& pos;
174174
Material::Entry* me;
@@ -696,7 +696,7 @@ namespace {
696696
// known attacking/defending status of the players.
697697

698698
template<Tracing T>
699-
Score Evaluation<T>::initiative(Score score, Score materialScore) const {
699+
Score Evaluation<T>::initiative(Score score) const {
700700

701701
int outflanking = distance<File>(pos.square<KING>(WHITE), pos.square<KING>(BLACK))
702702
- distance<Rank>(pos.square<KING>(WHITE), pos.square<KING>(BLACK));
@@ -722,7 +722,7 @@ namespace {
722722
- 100 ;
723723

724724
// Give more importance to non-material score
725-
score = (score * 2 - materialScore) / 2;
725+
score = score - pos.psq_score() / 2;
726726
Value mg = mg_value(score);
727727
Value eg = eg_value(score);
728728

@@ -794,9 +794,6 @@ namespace {
794794
if (abs(v) > LazyThreshold + pos.non_pawn_material() / 64)
795795
return pos.side_to_move() == WHITE ? v : -v;
796796

797-
// Remember this score
798-
Score materialScore = score;
799-
800797
// Main evaluation begins here
801798

802799
initialize<WHITE>();
@@ -815,7 +812,7 @@ namespace {
815812
+ passed< WHITE>() - passed< BLACK>()
816813
+ space< WHITE>() - space< BLACK>();
817814

818-
score += initiative(score, materialScore);
815+
score += initiative(score);
819816

820817
// Interpolate between a middlegame and a (scaled by 'sf') endgame score
821818
ScaleFactor sf = scale_factor(eg_value(score));

src/misc.cpp

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ typedef bool(*fun3_t)(HANDLE, CONST GROUP_AFFINITY*, PGROUP_AFFINITY);
4747
#include <sstream>
4848
#include <vector>
4949

50+
#ifdef __linux__
51+
#include <stdlib.h>
52+
#include <sys/mman.h>
53+
#endif
54+
5055
#include "misc.h"
5156
#include "thread.h"
5257

@@ -190,7 +195,7 @@ const std::string compiler_info() {
190195
compiler += "(unknown version)";
191196
#endif
192197

193-
#if defined(__APPLE__)
198+
#if defined(__APPLE__)
194199
compiler += " on Apple";
195200
#elif defined(__CYGWIN__)
196201
compiler += " on Cygwin";
@@ -288,6 +293,35 @@ void prefetch(void* addr) {
288293

289294
#endif
290295

296+
297+
/// aligned_ttmem_alloc will return suitably aligned memory, and if possible use large pages.
298+
/// The returned pointer is the aligned one, while the mem argument is the one that needs to be passed to free.
299+
/// With c++17 some of this functionality can be simplified.
300+
#ifdef __linux__
301+
302+
void* aligned_ttmem_alloc(size_t allocSize, void** mem) {
303+
304+
constexpr size_t alignment = 2 * 1024 * 1024; // assumed 2MB page sizes
305+
size_t size = ((allocSize + alignment - 1) / alignment) * alignment; // multiple of alignment
306+
*mem = aligned_alloc(alignment, size);
307+
madvise(*mem, allocSize, MADV_HUGEPAGE);
308+
return *mem;
309+
}
310+
311+
#else
312+
313+
void* aligned_ttmem_alloc(size_t allocSize, void** mem) {
314+
315+
constexpr size_t alignment = 64; // assumed cache line size
316+
size_t size = allocSize + alignment - 1; // allocate some extra space
317+
*mem = malloc(size);
318+
void* ret = reinterpret_cast<void*>((uintptr_t(*mem) + alignment - 1) & ~uintptr_t(alignment - 1));
319+
return ret;
320+
}
321+
322+
#endif
323+
324+
291325
namespace WinProcGroup {
292326

293327
#ifndef _WIN32

src/misc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const std::string engine_info(bool to_uci = false);
3333
const std::string compiler_info();
3434
void prefetch(void* addr);
3535
void start_logger(const std::string& fname);
36+
void* aligned_ttmem_alloc(size_t size, void** mem);
3637

3738
void dbg_hit_on(bool b);
3839
void dbg_hit_on(bool c, bool b);

src/tt.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,17 @@ void TranspositionTable::resize(size_t mbSize) {
6363

6464
Threads.main()->wait_for_search_finished();
6565

66-
clusterCount = mbSize * 1024 * 1024 / sizeof(Cluster);
67-
6866
free(mem);
69-
mem = malloc(clusterCount * sizeof(Cluster) + CacheLineSize - 1);
7067

68+
clusterCount = mbSize * 1024 * 1024 / sizeof(Cluster);
69+
table = static_cast<Cluster*>(aligned_ttmem_alloc(clusterCount * sizeof(Cluster), &mem));
7170
if (!mem)
7271
{
7372
std::cerr << "Failed to allocate " << mbSize
7473
<< "MB for transposition table." << std::endl;
7574
exit(EXIT_FAILURE);
7675
}
7776

78-
table = (Cluster*)((uintptr_t(mem) + CacheLineSize - 1) & ~(CacheLineSize - 1));
7977
clear();
8078
}
8179

src/tt.h

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,24 +57,22 @@ struct TTEntry {
5757
};
5858

5959

60-
/// A TranspositionTable consists of a power of 2 number of clusters and each
61-
/// cluster consists of ClusterSize number of TTEntry. Each non-empty entry
62-
/// contains information of exactly one position. The size of a cluster should
63-
/// divide the size of a cache line size, to ensure that clusters never cross
64-
/// cache lines. This ensures best cache performance, as the cacheline is
65-
/// prefetched, as soon as possible.
60+
/// A TranspositionTable is an array of Cluster, of size clusterCount. Each
61+
/// cluster consists of ClusterSize number of TTEntry. Each non-empty TTEntry
62+
/// contains information on exactly one position. The size of a Cluster should
63+
/// divide the size of a cache line for best performance,
64+
/// as the cacheline is prefetched when possible.
6665

6766
class TranspositionTable {
6867

69-
static constexpr int CacheLineSize = 64;
7068
static constexpr int ClusterSize = 3;
7169

7270
struct Cluster {
7371
TTEntry entry[ClusterSize];
74-
char padding[2]; // Align to a divisor of the cache line size
72+
char padding[2]; // Pad to 32 bytes
7573
};
7674

77-
static_assert(CacheLineSize % sizeof(Cluster) == 0, "Cluster size incorrect");
75+
static_assert(sizeof(Cluster) == 32, "Unexpected Cluster size");
7876

7977
public:
8078
~TranspositionTable() { free(mem); }

0 commit comments

Comments
 (0)