Compare commits
166
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2aaa560dd3 | ||
|
|
134af1abde | ||
|
|
1e41671002 | ||
|
|
dd831d14bf | ||
|
|
ae3a39cf04 | ||
|
|
aa120b14f9 | ||
|
|
4c67267c8c | ||
|
|
2c4292c485 | ||
|
|
f77c4c1fbd | ||
|
|
e64c6c60b9 | ||
|
|
de1d0e22ff | ||
|
|
d593833e9e | ||
|
|
a2297ed04e | ||
|
|
ecd57607f3 | ||
|
|
be9982eb75 | ||
|
|
babe3d0db0 | ||
|
|
30e54c2b71 | ||
|
|
1470ad34a8 | ||
|
|
f23a59730b | ||
|
|
dbd56e0c21 | ||
|
|
390ee23e75 | ||
|
|
4ebdeb54f6 | ||
|
|
4e437d52fc | ||
|
|
a3255581fb | ||
|
|
24a36414f8 | ||
|
|
f0b4e42f17 | ||
|
|
4e95413c4d | ||
|
|
19ea29b902 | ||
|
|
9fcef9c3c8 | ||
|
|
7ab07c8c71 | ||
|
|
7e3287be71 | ||
|
|
9642d606dc | ||
|
|
dc52b7846a | ||
|
|
0b18805f00 | ||
|
|
469ff7237c | ||
|
|
0f410353d1 | ||
|
|
b2eb9ca81a | ||
|
|
e4d114a763 | ||
|
|
68d7dcbccb | ||
|
|
7db4fcf59c | ||
|
|
00f0d06381 | ||
|
|
0f849c1b7f | ||
|
|
4c4e6d15c8 | ||
|
|
d1bcec29ce | ||
|
|
c4886db1fe | ||
|
|
caac437db7 | ||
|
|
de6b96bbf8 | ||
|
|
34018414df | ||
|
|
d4ed1a3f40 | ||
|
|
f9160084a7 | ||
|
|
04b1187963 | ||
|
|
f69da36c7c | ||
|
|
e3184752c0 | ||
|
|
cfcd24318b | ||
|
|
13cb768c67 | ||
|
|
372ab43e8b | ||
|
|
65b809d115 | ||
|
|
b239ce6bf0 | ||
|
|
e69294dfa3 | ||
|
|
454d9e61c1 | ||
|
|
1f2bb52418 | ||
|
|
e6ec17090b | ||
|
|
67c9e59939 | ||
|
|
f1df87a22d | ||
|
|
9c5335b4ef | ||
|
|
e98241cc7f | ||
|
|
740be80922 | ||
|
|
eb133efba1 | ||
|
|
cfe58552f2 | ||
|
|
36640bfe7e | ||
|
|
a6549e256a | ||
|
|
21a64294cf | ||
|
|
ba4ffca56f | ||
|
|
081d54dc55 | ||
|
|
64981dad3e | ||
|
|
a6472893a8 | ||
|
|
81a9a89ac8 | ||
|
|
f9e0f5db1a | ||
|
|
aafc6af697 | ||
|
|
82737f243f | ||
|
|
0250f9e954 | ||
|
|
b3ffc1f32f | ||
|
|
5f8e143cf8 | ||
|
|
2e84a1e3ab | ||
|
|
9d7567dfea | ||
|
|
c9f6ac3849 | ||
|
|
feb441c380 | ||
|
|
0a7be07d0f | ||
|
|
61da1c4d58 | ||
|
|
2bde715482 | ||
|
|
41b421ded5 | ||
|
|
baf0741e0d | ||
|
|
d99a35fdf0 | ||
|
|
2e952b0f92 | ||
|
|
b3c3182fec | ||
|
|
20196cb545 | ||
|
|
e33b88277b | ||
|
|
9885596770 | ||
|
|
83c9a2c51c | ||
|
|
ad7041c0a4 | ||
|
|
73d2945e95 | ||
|
|
b1b7b8234d | ||
|
|
8a2ada7165 | ||
|
|
506487f396 | ||
|
|
05ce50e647 | ||
|
|
c2f2730114 | ||
|
|
e06e1cdac1 | ||
|
|
94eced67f6 | ||
|
|
b5a34eda5e | ||
|
|
8b2c55eae4 | ||
|
|
aea6985817 | ||
|
|
e7731361cd | ||
|
|
7586608aaa | ||
|
|
1814cbc755 | ||
|
|
020132ea3c | ||
|
|
1bdedebf96 | ||
|
|
692ec62aac | ||
|
|
ce3ed3b6d1 | ||
|
|
345c6d21d9 | ||
|
|
0d126aa6a7 | ||
|
|
5ca125e336 | ||
|
|
855e5c2377 | ||
|
|
7e621311e6 | ||
|
|
b2a475ff5f | ||
|
|
4b3e8020fa | ||
|
|
194c3d3bc4 | ||
|
|
b929963f91 | ||
|
|
079959e6d4 | ||
|
|
64b2e3d9b1 | ||
|
|
eac21b424a | ||
|
|
cce6f5dd0c | ||
|
|
7e4d5889a8 | ||
|
|
71d3b3309a | ||
|
|
d662594814 | ||
|
|
9be74da127 | ||
|
|
85cdb7e077 | ||
|
|
a3d2f352d3 | ||
|
|
4dcf0d04a0 | ||
|
|
91b5062549 | ||
|
|
a7ef4ee4a9 | ||
|
|
2e802d7dfe | ||
|
|
81bd4648e3 | ||
|
|
48bbfa7114 | ||
|
|
320c43cdd3 | ||
|
|
a9b51e384f | ||
|
|
1fb235fb3f | ||
|
|
0f748da56c | ||
|
|
fd2168d529 | ||
|
|
41dabc2150 | ||
|
|
f114a6d0bb | ||
|
|
7cf8b381ce | ||
|
|
1f0b36764d | ||
|
|
2fd3cf0d2a | ||
|
|
931b6fa999 | ||
|
|
ba3505afc8 | ||
|
|
6ed9e882f8 | ||
|
|
0df0921c47 | ||
|
|
865eac5782 | ||
|
|
9421d254f7 | ||
|
|
d71e264bb9 | ||
|
|
031b5285ed | ||
|
|
9e5f131105 | ||
|
|
f4bf94eb98 | ||
|
|
75022da950 | ||
|
|
5bc4459111 | ||
|
|
65f7b3f38a |
No files matched your search
@@ -1,14 +1,15 @@
|
|||||||
# Define directories
|
# Define directories
|
||||||
SRC_DIR := source
|
SRC_DIR := source
|
||||||
OBJ_DIR := obj
|
OBJ_DIR := obj
|
||||||
FMT_DIR := external/fmt
|
|
||||||
|
|
||||||
CXX := clang++
|
|
||||||
EXE ?= Eleanor
|
EXE ?= Eleanor
|
||||||
EVALFILE := ./nnue.bin
|
EVALFILE := ./nnue.bin
|
||||||
|
|
||||||
|
CXX := clang++
|
||||||
|
|
||||||
ifeq ($(OS),Windows_NT)
|
ifeq ($(OS),Windows_NT)
|
||||||
UNAME_S := Windows
|
UNAME_S := Windows
|
||||||
|
ARCH := x86_64
|
||||||
EXE_EXT := .exe
|
EXE_EXT := .exe
|
||||||
MKDIR := mkdir
|
MKDIR := mkdir
|
||||||
RM := rmdir /s /q
|
RM := rmdir /s /q
|
||||||
@@ -28,19 +29,25 @@ else
|
|||||||
endif
|
endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
CXX_BASE := $(notdir $(firstword $(CXX)))
|
||||||
|
CXX_DRIVER_FLAGS :=
|
||||||
CXXFLAGS := -std=c++20 -O3 -flto -Wall
|
CXXFLAGS := -std=c++20 -O3 -flto -Wall
|
||||||
LDFLAGS := -O3 -flto
|
LDFLAGS := -O3 -flto
|
||||||
|
|
||||||
ifeq ($(PLATFORM),mac)
|
ifeq ($(PLATFORM),mac)
|
||||||
ifeq ($(ARCH),arm64)
|
ifeq ($(ARCH),arm64)
|
||||||
# Apple Silicon
|
|
||||||
ARCH_FLAGS := -mcpu=apple-m1
|
ARCH_FLAGS := -mcpu=apple-m1
|
||||||
else
|
else
|
||||||
ARCH_FLAGS := -march=native
|
ARCH_FLAGS := -march=native
|
||||||
endif
|
endif
|
||||||
|
else ifeq ($(PLATFORM),windows)
|
||||||
|
ARCH_FLAGS := -march=native
|
||||||
|
CXX_DRIVER_FLAGS += --target=$(ARCH)-w64-windows-gnu
|
||||||
|
CXXFLAGS += -pthread
|
||||||
|
LDFLAGS += -pthread
|
||||||
|
LDFLAGS += -static -Wl,--stack,8388608
|
||||||
else
|
else
|
||||||
ifeq ($(ARCH),aarch64)
|
ifeq ($(ARCH),aarch64)
|
||||||
# Generic Linux ARM64
|
|
||||||
ARCH_FLAGS := -mcpu=native
|
ARCH_FLAGS := -mcpu=native
|
||||||
else
|
else
|
||||||
ARCH_FLAGS := -march=native
|
ARCH_FLAGS := -march=native
|
||||||
@@ -56,17 +63,11 @@ MAKEFLAGS += -j
|
|||||||
SRCS := $(wildcard $(SRC_DIR)/*.cpp)
|
SRCS := $(wildcard $(SRC_DIR)/*.cpp)
|
||||||
OBJS := $(patsubst $(SRC_DIR)/%.cpp, $(OBJ_DIR)/%.o, $(SRCS))
|
OBJS := $(patsubst $(SRC_DIR)/%.cpp, $(OBJ_DIR)/%.o, $(SRCS))
|
||||||
|
|
||||||
FMT_SRC := $(FMT_DIR)/format.cc
|
$(EXE)$(EXE_EXT): $(OBJS)
|
||||||
FMT_OBJ := $(OBJ_DIR)/format.o
|
$(CXX) $(CXX_DRIVER_FLAGS) $(CXXFLAGS) -DEVALFILE=\"$(EVALFILE)\" $^ -o $@ $(LDFLAGS)
|
||||||
|
|
||||||
$(EXE)$(EXE_EXT): $(OBJS) $(FMT_OBJ)
|
|
||||||
$(CXX) $(CXXFLAGS) -DEVALFILE=\"$(EVALFILE)\" $^ -o $@ $(LDFLAGS)
|
|
||||||
|
|
||||||
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp | $(OBJ_DIR)
|
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp | $(OBJ_DIR)
|
||||||
$(CXX) $(CXXFLAGS) -c -o $@ $<
|
$(CXX) $(CXX_DRIVER_FLAGS) $(CXXFLAGS) -c -o $@ $<
|
||||||
|
|
||||||
$(FMT_OBJ): $(FMT_SRC) | $(OBJ_DIR)
|
|
||||||
$(CXX) $(CXXFLAGS) -c -o $@ $<
|
|
||||||
|
|
||||||
$(OBJ_DIR):
|
$(OBJ_DIR):
|
||||||
$(MKDIR) $(OBJ_DIR)
|
$(MKDIR) $(OBJ_DIR)
|
||||||
@@ -80,4 +81,3 @@ else
|
|||||||
@$(RM) $(OBJ_DIR)
|
@$(RM) $(OBJ_DIR)
|
||||||
@$(DEL) $(EXE)$(EXE_EXT)
|
@$(DEL) $(EXE)$(EXE_EXT)
|
||||||
endif
|
endif
|
||||||
$(MAKE)
|
|
||||||
@@ -12,11 +12,12 @@
|
|||||||

|

|
||||||
|
|
||||||
|
|
||||||
| Version | Release Date | [CCRL 40/15](https://www.computerchess.org.uk/ccrl/4040/cgi/compare_engines.cgi?family=Eleanor&print=Rating+list&print=Results+table&print=LOS+table&print=Ponder+hit+table&print=Eval+difference+table&print=Comopp+gamenum+table&print=Overlap+table&print=Score+with+common+opponents) | [CCRL Blitz](https://www.computerchess.org.uk/ccrl/404/cgi/compare_engines.cgi?family=Eleanor&print=Rating+list&print=Results+table&print=LOS+table&print=Ponder+hit+table&print=Eval+difference+table&print=Comopp+gamenum+table&print=Overlap+table&print=Score+with+common+opponents) |
|
| Version | Release Date | [CCRL 40/15](https://www.computerchess.org.uk/ccrl/4040/cgi/compare_engines.cgi?family=Eleanor&print=Rating+list&print=Results+table&print=LOS+table&print=Ponder+hit+table&print=Eval+difference+table&print=Comopp+gamenum+table&print=Overlap+table&print=Score+with+common+opponents) | [CCRL Blitz](https://www.computerchess.org.uk/ccrl/404/cgi/compare_engines.cgi?family=Eleanor&print=Rating+list&print=Results+table&print=LOS+table&print=Ponder+hit+table&print=Eval+difference+table&print=Comopp+gamenum+table&print=Overlap+table&print=Score+with+common+opponents) | [CEGT 40/20](http://www.cegt.net/40_40%20Rating%20List/40_40%20SingleVersion/rangliste.html)
|
||||||
| --- | --- | --- | --- |
|
| --- | --- | --- | --- | --- |
|
||||||
| 3.0 | 2025-12-05 | ? | ?
|
| 4.1 | 2026-04-22 | 3528 (#60-61) | 3590 (#64) | 3443 (#59)
|
||||||
| 2.0 | 2025-08-23 | 3312 (#115) | 3392 (#105)
|
| 3.0 | 2025-12-05 | 3405 (#84) | 3495 (#79-80) | -
|
||||||
| 1.0 | 2025-06-02 | 2856 (#244) | 2759 (#256)
|
| 2.0 | 2025-08-23 | 3312 (#115) | 3392 (#105) | -
|
||||||
|
| 1.0 | 2025-06-02 | 2856 (#244) | 2759 (#256) | -
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -25,13 +26,14 @@
|
|||||||
|
|
||||||
Eleanor uses a **fully neural network-based evaluation function**, entirely trained on self-play data from an initially simple HCE using the [bullet](https://github.com/jw1912/bullet) trainer.
|
Eleanor uses a **fully neural network-based evaluation function**, entirely trained on self-play data from an initially simple HCE using the [bullet](https://github.com/jw1912/bullet) trainer.
|
||||||
|
|
||||||
**Architecture:** `(768hm -> 1536)x2 -> 1x8`
|
**Architecture:** `(768x8hm -> 1536)x2 -> 1x8`
|
||||||
A 1536-hidden-layer *perspective net* with 8 output buckets and horizontal mirroring.
|
A 1536-hidden-layer *perspective net* with 8 input buckets, 8 output buckets and horizontal mirroring.
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
> **Prerequisites:**
|
> **Prerequisites:**
|
||||||
> - `clang++` compiler
|
> - `clang++` compiler
|
||||||
|
> - On Windows, `clang++` must have access to a MinGW GNU toolchain/runtime because the build targets `x86_64-w64-windows-gnu`
|
||||||
> - Access to `make` command
|
> - Access to `make` command
|
||||||
|
|
||||||
1. Clone the repository
|
1. Clone the repository
|
||||||
@@ -70,4 +72,4 @@ The engine can be used in any GUI with **UCI support**.
|
|||||||
|
|
||||||
**The project is a work in progress and actively developed. Contributions are highly appreciated.**
|
**The project is a work in progress and actively developed. Contributions are highly appreciated.**
|
||||||
|
|
||||||
###### Last updated: 2025-12-05
|
###### Last updated: 2026-04-18
|
||||||
@@ -0,0 +1,96 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import re
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
SCRIPT_DIR = Path(__file__).resolve().parent
|
||||||
|
DEFAULT_INPUT = SCRIPT_DIR / "spsa_output.txt"
|
||||||
|
DEFAULT_TARGET = SCRIPT_DIR.parent / "source" / "tunables.h"
|
||||||
|
|
||||||
|
VALUE_RE = re.compile(r"^\s*([A-Za-z0-9_]+)\s*,\s*([^\s,]+)\s*$")
|
||||||
|
LINE_RE = re.compile(
|
||||||
|
r"^(?P<indent>\s*)X_(?P<kind>INT|DOUBLE)\("
|
||||||
|
r"(?P<name>[A-Za-z0-9_]+),\s*"
|
||||||
|
r"(?P<default>[^,]+),\s*"
|
||||||
|
r"(?P<rest>.+)\)\s*(?P<continuation>\\)?$"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def update_line(line: str, updated_values: dict[str, str]) -> tuple[str, bool]:
|
||||||
|
match = LINE_RE.match(line)
|
||||||
|
if not match:
|
||||||
|
return line, False
|
||||||
|
|
||||||
|
name = match.group("name")
|
||||||
|
if name not in updated_values:
|
||||||
|
return line, False
|
||||||
|
|
||||||
|
continuation = " \\" if match.group("continuation") else ""
|
||||||
|
new_line = (
|
||||||
|
f"{match.group('indent')}X_{match.group('kind')}("
|
||||||
|
f"{name}, {updated_values[name]}, {match.group('rest')}){continuation}\n"
|
||||||
|
)
|
||||||
|
return new_line, True
|
||||||
|
|
||||||
|
|
||||||
|
def load_values(path: Path) -> dict[str, str]:
|
||||||
|
values = {}
|
||||||
|
|
||||||
|
for line in path.read_text().splitlines():
|
||||||
|
match = VALUE_RE.match(line)
|
||||||
|
if not match:
|
||||||
|
continue
|
||||||
|
values[match.group(1)] = match.group(2)
|
||||||
|
|
||||||
|
return values
|
||||||
|
|
||||||
|
|
||||||
|
def apply_updates(text: str, updated_values: dict[str, str]) -> tuple[str, set[str]]:
|
||||||
|
updated_lines: list[str] = []
|
||||||
|
seen: set[str] = set()
|
||||||
|
|
||||||
|
for line in text.splitlines(keepends=True):
|
||||||
|
new_line, changed = update_line(line, updated_values)
|
||||||
|
if changed:
|
||||||
|
match = LINE_RE.match(line)
|
||||||
|
if match:
|
||||||
|
seen.add(match.group("name"))
|
||||||
|
updated_lines.append(new_line)
|
||||||
|
|
||||||
|
return "".join(updated_lines), seen
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> int:
|
||||||
|
input_path = Path(sys.argv[1]) if len(sys.argv) > 1 else DEFAULT_INPUT
|
||||||
|
target = Path(sys.argv[2]) if len(sys.argv) > 2 else DEFAULT_TARGET
|
||||||
|
|
||||||
|
if not input_path.exists():
|
||||||
|
print(f"Input file not found: {input_path}", file=sys.stderr)
|
||||||
|
return 1
|
||||||
|
|
||||||
|
updated_values = load_values(input_path)
|
||||||
|
if not updated_values:
|
||||||
|
print(f"No tunable values found in {input_path}", file=sys.stderr)
|
||||||
|
return 1
|
||||||
|
|
||||||
|
if not target.exists():
|
||||||
|
print(f"Target file not found: {target}", file=sys.stderr)
|
||||||
|
return 1
|
||||||
|
|
||||||
|
text = target.read_text()
|
||||||
|
updated_text, seen = apply_updates(text, updated_values)
|
||||||
|
|
||||||
|
missing = sorted(set(updated_values) - seen)
|
||||||
|
if missing:
|
||||||
|
print("Missing tunables:", ", ".join(missing), file=sys.stderr)
|
||||||
|
return 1
|
||||||
|
|
||||||
|
target.write_text(updated_text)
|
||||||
|
print(f"Updated {len(seen)} tunables in {target} from {input_path}")
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
raise SystemExit(main())
|
||||||
+68
-53
@@ -1,59 +1,74 @@
|
|||||||
[Event "?"]
|
[Event "?"]
|
||||||
[Site "?"]
|
[Site "?"]
|
||||||
[Date "2025.09.04"]
|
[Date "2026.03.26"]
|
||||||
[Round "1"]
|
[Round "1"]
|
||||||
[White "Eleanor-base"]
|
[White "Eleanor-dev"]
|
||||||
[Black "Eleanor-dev"]
|
[Black "Eleanor-base"]
|
||||||
[Result "1-0"]
|
[Result "0-1"]
|
||||||
[FEN "rn1qk1nr/ppp2pRp/3bp3/3p4/3P4/2P2P1P/PP2PP2/RNBQKB2 b Qkq - 0 1"]
|
[FEN "rn1qk1nr/pbp1ppbp/1p1p2p1/3P4/5P2/2N1P3/PPPB2PP/R2QKBNR b KQkq - 0 1"]
|
||||||
[GameDuration "00:01:31"]
|
[GameDuration "00:00:14"]
|
||||||
[GameEndTime "2025-09-04T22:39:24.808 CEST"]
|
[GameEndTime "2026-03-26T20:42:15.476 CET"]
|
||||||
[GameStartTime "2025-09-04T22:37:53.089 CEST"]
|
[GameStartTime "2026-03-26T20:42:00.864 CET"]
|
||||||
[PlyCount "84"]
|
[PlyCount "115"]
|
||||||
[SetUp "1"]
|
[SetUp "1"]
|
||||||
[Termination "abandoned"]
|
[Termination "abandoned"]
|
||||||
[TimeControl "37.56+0.38"]
|
[TimeControl "5.17+0.05"]
|
||||||
|
|
||||||
1... Ne7 {-1.65 14/26 1861 1544983} 2. Rg1 {+1.64 12/23 2087 934687}
|
1... Nf6 {+0.25 16/24 256 274740} 2. Bb5+ {-0.31 14/28 286 222469}
|
||||||
c5 {-1.74 15/32 1565 1167061} 3. f4 {+1.61 13/27 2002 772197}
|
c6 {+0.30 14/30 154 175813} 3. dxc6 {-0.31 15/29 214 240246}
|
||||||
Nd7 {-1.59 15/31 1455 1090717} 4. e3 {+1.84 14/31 1753 1387350}
|
Nxc6 {+0.29 15/25 271 56301} 4. Nf3 {-0.25 16/25 174 198800}
|
||||||
Qb6 {-1.65 14/31 1385 1273463} 5. a4 {+1.98 14/28 1505 1375155}
|
O-O {+0.25 14/29 260 157287} 5. O-O {-0.28 15/26 245 287541}
|
||||||
Qc7 {-2.11 14/29 1849 830961} 6. dxc5 {+1.98 15/30 1571 1101192}
|
a6 {+0.24 17/35 224 246704} 6. Bd3 {-0.22 14/27 136 153559}
|
||||||
Nxc5 {-2.16 15/34 1778 545375} 7. a5 {+1.90 14/29 1555 1021903}
|
b5 {+0.24 17/31 241 178951} 7. Qe1 {-0.24 14/28 245 274939}
|
||||||
a6 {-2.22 15/33 1706 667700} 8. Nd2 {+2.38 14/29 1267 922229}
|
Qb6 {+0.25 16/25 230 190364} 8. a3 {-0.25 14/25 207 226160}
|
||||||
O-O-O {-2.34 14/31 1641 580655} 9. b4 {+2.62 14/31 1463 975771}
|
e5 {+0.26 14/27 221 86623} 9. fxe5 {-0.10 14/27 158 172124}
|
||||||
Ne4 {-2.37 15/39 1336 932258} 10. Nxe4 {+2.55 13/31 1579 228776}
|
Nxe5 {+0.28 13/27 214 129429} 10. Nxe5 {-0.39 13/24 170 177476}
|
||||||
dxe4 {-2.85 15/31 1004 694847} 11. Bd2 {+3.02 13/30 1247 753167}
|
dxe5 {+0.26 15/27 100 113069} 11. Kh1 {-0.18 13/23 121 136389}
|
||||||
Ng6 {-2.40 14/35 1118 862794} 12. Qb1 {+2.40 12/26 1475 339681}
|
e4 {+0.32 13/18 202 26644} 12. Be2 {-0.19 15/28 182 213316}
|
||||||
f5 {-1.97 13/31 1465 304169} 13. Qb3 {+2.25 12/34 957 597544}
|
Rfc8 {+0.19 13/23 194 212538} 13. a4 {-0.12 13/24 207 126167}
|
||||||
Nh4 {-2.21 14/38 1409 432849} 14. Be2 {+2.30 13/35 1343 843064}
|
b4 {+0.22 13/25 187 192683} 14. a5 {-0.18 14/26 101 112261}
|
||||||
Nf3+ {-2.18 14/40 1355 318067} 15. Bxf3 {+2.49 14/39 1346 645681}
|
Qd8 {+0.15 14/31 111 123537} 15. Na4 {-0.19 14/34 196 210792}
|
||||||
exf3 {-2.59 15/30 1115 864898} 16. O-O-O {+2.85 14/35 1297 425564}
|
Nd5 {+0.23 15/29 157 177113} 16. Qf2 {-0.22 14/25 188 135679}
|
||||||
Rhe8 {-2.11 13/34 1269 275858} 17. c4 {+2.35 13/30 1249 86929}
|
f5 {+0.22 15/28 130 130850} 17. c4 {-0.23 13/25 182 139155}
|
||||||
e5 {-1.53 14/40 1188 1007892} 18. Bc3 {+2.71 12/37 1207 154226}
|
bxc3 {+0.24 15/25 98 105292} 18. bxc3 {-0.24 13/25 175 94316}
|
||||||
exf4 {-0.71 13/45 1186 280646} 19. c5 {+0.71 13/34 883 618110}
|
Bc6 {+0.23 14/17 166 18894} 19. Bc4 {-0.20 14/29 97 108937}
|
||||||
Be5 {-0.52 13/65 1145 440206} 20. Rxd8+ {+0.56 13/36 861 757974}
|
Bxa4 {+0.20 13/24 81 86007} 20. Rxa4 {-0.21 16/30 85 101741}
|
||||||
Qxd8 {-0.55 15/56 1106 373775} 21. Bxe5 {+0.98 14/49 845 689690}
|
h5 {+0.25 13/20 159 95557} 21. Qe2 {-0.25 12/30 165 163762}
|
||||||
Rxe5 {-0.87 14/83 799 511973} 22. exf4 {+0.98 13/36 1092 370730}
|
Kh7 {+0.36 12/21 98 103093} 22. Rxf5 {-0.39 12/27 159 75820}
|
||||||
Re2 {-0.87 13/89 751 367978} 23. Rg8 {+1.05 13/43 1056 334030}
|
gxf5 {+0.34 12/26 101 110999} 23. Qxh5+ {-0.45 13/38 107 119594}
|
||||||
Re8 {-0.87 13/79 1030 242199} 24. Rxe8 {+1.13 12/34 1023 141620}
|
Kg8 {+0.33 13/23 149 16142} 24. Qxf5 {-0.39 14/26 74 84084}
|
||||||
Qxe8 {-0.87 13/41 998 22345} 25. Qd1 {+1.46 11/36 991 227470}
|
Rc5 {+0.40 13/23 144 140578} 25. Qe6+ {-0.37 12/36 150 138869}
|
||||||
Qe4 {-0.89 12/59 968 192246} 26. Qd2 {+1.17 13/42 856 592181}
|
Kf8 {+0.53 14/27 138 120201} 26. Bc1 {-0.45 14/30 144 132855}
|
||||||
Qc4+ {-0.90 11/95 891 510827} 27. Kb2 {+1.46 12/38 937 154015}
|
Rb8 {+0.59 14/33 74 90173} 27. Ra1 {-0.55 14/36 130 123759}
|
||||||
h5 {-1.01 11/66 913 404234} 28. Qc3 {+1.38 13/37 909 437481}
|
Qf6 {+0.63 14/35 82 98374} 28. Qxf6+ {-0.21 14/39 132 146058}
|
||||||
Qe2+ {-0.73 10/76 886 55851} 29. Qc2 {+1.42 14/34 882 272787}
|
Bxf6 {+0.61 15/35 100 125452} 29. Ba3 {-0.59 14/29 86 103103}
|
||||||
Qe4 {-1.19 10/49 860 32225} 30. Kb3 {+1.27 12/37 824 598253}
|
Nb4 {+0.57 16/29 129 51381} 30. Bxb4 {-0.57 15/25 64 80650}
|
||||||
Qxf4 {-1.13 10/98 838 184366} 31. c6 {+1.24 12/51 734 627843}
|
Rxb4 {+0.53 14/29 91 125806} 31. cxb4 {-0.49 15/23 129 48919}
|
||||||
Qe4 {-0.96 10/103 814 271816} 32. cxb7+ {+1.24 13/50 817 324130}
|
Rxc4 {+0.47 15/27 122 47840} 32. Rb1 {-0.45 15/30 67 91596}
|
||||||
Kxb7 {-0.96 9/16 792 1604} 33. Qc3 {+1.24 14/30 796 171391}
|
Be7 {+0.46 14/24 92 126553} 33. g4 {-0.44 15/26 124 72397}
|
||||||
Qd5+ {-1.20 8/35 772 15385} 34. Ka3 {+1.23 11/40 525 374116}
|
Rxb4 {+0.49 14/25 73 87941} 34. Rf1+ {-0.49 14/28 121 72810}
|
||||||
Qe4 {-1.25 8/85 752 9999} 35. Qd2 {+1.19 12/37 767 281732}
|
Ke8 {+0.45 15/36 89 113698} 35. Rf5 {-0.45 13/22 117 70248}
|
||||||
Kc7 {-1.09 10/65 733 175302} 36. Qb2 {+1.21 12/40 748 316739}
|
Kd7 {+0.45 14/30 115 74784} 36. Re5 {-0.45 13/22 82 101380}
|
||||||
h4 {-1.09 7/48 715 39908} 37. Qc1+ {+1.19 11/38 529 523703}
|
Bd6 {+0.50 14/33 87 110879} 37. Rh5 {-0.45 12/34 113 72228}
|
||||||
Kd8 {-0.65 10/83 700 298167} 38. Qc3 {+1.26 12/49 723 322911}
|
Rb1+ {+0.50 15/32 56 64801} 38. Kg2 {-0.46 14/29 74 94136}
|
||||||
Kd7 {-0.64 9/44 684 18906} 39. Qg7+ {+1.23 11/37 537 395773}
|
Rb2+ {+0.50 14/17 108 4246} 39. Kh1 {-0.48 15/25 108 48523}
|
||||||
Kc8 {-1.04 8/14 667 847} 40. Qf7 {+1.23 11/45 572 501521}
|
Re2 {+0.53 14/27 78 96242} 40. Rh7+ {-0.56 13/28 105 70703}
|
||||||
Qd3+ {-1.04 7/81 653 28141} 41. Qb3 {+1.23 11/36 689 130089}
|
Ke6 {+0.59 13/25 67 86840} 41. Rh6+ {-0.61 14/29 87 110057}
|
||||||
Qe2 {-1.28 8/54 639 10579} 42. Qc3+ {+1.08 11/38 581 477548}
|
Kd5 {+0.59 15/27 91 103932} 42. Rh5+ {-0.65 14/28 100 108417}
|
||||||
Kd7 {-0.67 8/40 627 10552} 43. Qb2 {+1.08 11/33 663 104343, Black disconnects}
|
Be5 {+0.70 14/33 94 110182} 43. Rh3 {-0.67 14/30 86 100550}
|
||||||
1-0
|
Kc4 {+0.83 13/27 100 123700} 44. Rh5 {-0.75 12/24 96 56083}
|
||||||
|
Bc3 {+0.88 12/26 47 63602} 45. Rh6 {-0.73 12/23 95 70966}
|
||||||
|
Rxe3 {+0.92 12/31 96 115497} 46. h4 {-0.83 10/24 91 46076}
|
||||||
|
Re1+ {+0.98 12/25 94 124212} 47. Kg2 {-0.92 13/29 45 57555}
|
||||||
|
Bd4 {+0.94 14/28 86 116606} 48. Rxa6 {-0.93 13/26 90 42707}
|
||||||
|
e3 {+0.95 13/23 90 44547} 49. Re6 {-0.92 12/40 88 86679}
|
||||||
|
e2 {+0.99 13/23 88 36470} 50. a6 {-0.96 13/40 86 89628}
|
||||||
|
Kd5 {+0.97 12/27 86 80236} 51. a7 {-0.97 12/30 84 17470}
|
||||||
|
Bxa7 {+1.08 13/30 84 33077} 52. Re7 {-1.12 12/28 82 71784}
|
||||||
|
Rg1+ {+1.17 13/28 53 70853} 53. Kf3 {-1.21 13/30 81 51340}
|
||||||
|
e1=Q {+1.21 13/30 83 51082} 54. Rxe1 {-1.51 14/31 59 80154}
|
||||||
|
Rxe1 {+1.25 13/29 41 59171} 55. h5 {-1.31 14/29 40 55864}
|
||||||
|
Bd4 {+1.29 12/29 81 48179} 56. g5 {-1.23 11/26 80 30276}
|
||||||
|
Rf1+ {+1.33 12/20 80 18531} 57. Kg2 {-3.99 12/39 78 76309}
|
||||||
|
Rf5 {+1.56 12/26 79 58979} 58. g6 {-M18 12/38 76 76733}
|
||||||
|
Ke4 {+M19 13/35 46 55731, White disconnects} 0-1
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
lmrBaseQuiet, 1.1029723689888005
|
||||||
|
lmrDivisorQuiet, 2.3063513397139945
|
||||||
|
lmrBaseNoisy, 0.451805556082857
|
||||||
|
lmrDivisorNoisy, 3.1355330830404338
|
||||||
|
lmrCutnode, 2005
|
||||||
|
lmrIsPV, 982
|
||||||
|
lmrTTPV, 936
|
||||||
|
lmrHistoryDivisor, 8377
|
||||||
|
lmrImproving, 1131
|
||||||
|
lmrCorrplexity, 830
|
||||||
|
lmrTTPVFailLow, 913
|
||||||
|
lmrDeeperBase, 46
|
||||||
|
rfpBase, 21
|
||||||
|
rfpMargin, 97
|
||||||
|
fpMargin, 86
|
||||||
|
probcutBetaMargin, 190
|
||||||
|
razoringScalar, 195
|
||||||
|
doubleExtMargin, 32
|
||||||
|
seeQuietThreshold, -81
|
||||||
|
seeNoisyThreshold, -32
|
||||||
|
aspInitialDelta, 26
|
||||||
|
nmpBetaMargin, 25
|
||||||
|
seePawnValue, 98
|
||||||
|
seeKnightValue, 301
|
||||||
|
seeBishopValue, 346
|
||||||
|
seeRookValue, 507
|
||||||
|
seeQueenValue, 924
|
||||||
|
seeQsThreshold, -13
|
||||||
|
seeOrderingThreshold, -102
|
||||||
|
ldseMargin, 26
|
||||||
|
historyBonusMultiplier, 306
|
||||||
|
historyBonusSub, 252
|
||||||
|
historyMalusMultiplier, 301
|
||||||
|
historyMalusSub, 250
|
||||||
|
contHistoryBonusMultiplier, 300
|
||||||
|
contHistoryBonusSub, 252
|
||||||
|
contHistoryMalusMultiplier, 300
|
||||||
|
contHistoryMalusSub, 249
|
||||||
|
captHistoryBonusMultiplier, 302
|
||||||
|
captHistoryBonusSub, 249
|
||||||
|
captHistoryMalusMultiplier, 300
|
||||||
|
captHistoryMalusSub, 249
|
||||||
Vendored
-2985
File diff suppressed because it is too large.
Load diff
Vendored
-1949
File diff suppressed because it is too large.
Load diff
Vendored
-4244
File diff suppressed because it is too large.
Load diff
Vendored
-46
@@ -1,46 +0,0 @@
|
|||||||
// Formatting library for C++
|
|
||||||
//
|
|
||||||
// Copyright (c) 2012 - 2016, Victor Zverovich
|
|
||||||
// All rights reserved.
|
|
||||||
//
|
|
||||||
// For the license information refer to format.h.
|
|
||||||
|
|
||||||
#include "fmt/format-inl.h"
|
|
||||||
|
|
||||||
FMT_BEGIN_NAMESPACE
|
|
||||||
namespace detail {
|
|
||||||
|
|
||||||
template FMT_API auto dragonbox::to_decimal(float x) noexcept
|
|
||||||
-> dragonbox::decimal_fp<float>;
|
|
||||||
template FMT_API auto dragonbox::to_decimal(double x) noexcept
|
|
||||||
-> dragonbox::decimal_fp<double>;
|
|
||||||
|
|
||||||
#if FMT_USE_LOCALE
|
|
||||||
// DEPRECATED! locale_ref in the detail namespace
|
|
||||||
template FMT_API locale_ref::locale_ref(const std::locale& loc);
|
|
||||||
template FMT_API auto locale_ref::get<std::locale>() const -> std::locale;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Explicit instantiations for char.
|
|
||||||
|
|
||||||
template FMT_API auto thousands_sep_impl(locale_ref)
|
|
||||||
-> thousands_sep_result<char>;
|
|
||||||
template FMT_API auto decimal_point_impl(locale_ref) -> char;
|
|
||||||
|
|
||||||
// DEPRECATED!
|
|
||||||
template FMT_API void buffer<char>::append(const char*, const char*);
|
|
||||||
|
|
||||||
// DEPRECATED!
|
|
||||||
template FMT_API void vformat_to(buffer<char>&, string_view,
|
|
||||||
typename vformat_args<>::type, locale_ref);
|
|
||||||
|
|
||||||
// Explicit instantiations for wchar_t.
|
|
||||||
|
|
||||||
template FMT_API auto thousands_sep_impl(locale_ref)
|
|
||||||
-> thousands_sep_result<wchar_t>;
|
|
||||||
template FMT_API auto decimal_point_impl(locale_ref) -> wchar_t;
|
|
||||||
|
|
||||||
template FMT_API void buffer<wchar_t>::append(const wchar_t*, const wchar_t*);
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
FMT_END_NAMESPACE
|
|
||||||
+150
-50
@@ -7,8 +7,8 @@ namespace ACC {
|
|||||||
int CalculateIndex(bool perspective, bool side, int pieceType, int square, bool mirrored) {
|
int CalculateIndex(bool perspective, bool side, int pieceType, int square, bool mirrored) {
|
||||||
if (perspective == Black)
|
if (perspective == Black)
|
||||||
{
|
{
|
||||||
side = 1 - side; // Flip side
|
side = 1 - side;
|
||||||
square = square ^ 0b111000; // Mirror square
|
square = square ^ 0b111000;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mirrored)
|
if (mirrored)
|
||||||
@@ -17,70 +17,170 @@ int CalculateIndex(bool perspective, bool side, int pieceType, int square, bool
|
|||||||
return side * 64 * 6 + pieceType * 64 + square;
|
return side * 64 * 6 + pieceType * 64 + square;
|
||||||
}
|
}
|
||||||
|
|
||||||
// All friendly, for quiets
|
const int16_t* Row(int bucket, int index) {
|
||||||
void AccumulatorPair::addSub(bool stm, int add, int addPT, int sub, int subPT) {
|
return &NNUE::net.accumulator_weights[bucket][static_cast<size_t>(index) * NNUE::HL_SIZE];
|
||||||
int addW = CalculateIndex(White, stm, addPT, add, mirroredWhite);
|
}
|
||||||
int addB = CalculateIndex(Black, stm, addPT, add, mirroredBlack);
|
|
||||||
|
|
||||||
int subW = CalculateIndex(White, stm, subPT, sub, mirroredWhite);
|
void FinnyTable::Reset() {
|
||||||
int subB = CalculateIndex(Black, stm, subPT, sub, mirroredBlack);
|
for (auto& perspective : entries) {
|
||||||
|
for (auto& mirror : perspective) {
|
||||||
for (int i = 0; i < NNUE::HL_SIZE; i++) {
|
for (auto& entry : mirror) {
|
||||||
white[i] += NNUE::net.accumulator_weights[addW * NNUE::HL_SIZE + i]
|
entry.acc = NNUE::net.accumulator_biases;
|
||||||
- NNUE::net.accumulator_weights[subW * NNUE::HL_SIZE + i];
|
for (auto& color : entry.bb)
|
||||||
|
for (auto& bb : color)
|
||||||
black[i] += NNUE::net.accumulator_weights[addB * NNUE::HL_SIZE + i]
|
bb = 0;
|
||||||
- NNUE::net.accumulator_weights[subB * NNUE::HL_SIZE + i];
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Captures
|
#if defined(_MSC_VER) && !defined(__clang__)
|
||||||
void AccumulatorPair::addSubSub(bool stm, int add, int addPT, int sub1, int subPT1, int sub2, int subPT2) {
|
#define ACC_NOINLINE __declspec(noinline)
|
||||||
int addW = CalculateIndex(White, stm, addPT, add, mirroredWhite);
|
#else
|
||||||
int addB = CalculateIndex(Black, stm, addPT, add, mirroredBlack);
|
#define ACC_NOINLINE __attribute__((noinline))
|
||||||
|
#endif
|
||||||
|
|
||||||
int subW1 = CalculateIndex(White, stm, subPT1, sub1, mirroredWhite);
|
#if defined(__AVX512F__) && defined(__AVX512BW__)
|
||||||
int subB1 = CalculateIndex(Black, stm, subPT1, sub1, mirroredBlack);
|
#include <immintrin.h>
|
||||||
|
using Vec = __m512i;
|
||||||
|
static inline Vec vLoad(const int16_t* p) { return _mm512_loadu_si512(p); }
|
||||||
|
static inline void vStore(int16_t* p, Vec v) { _mm512_storeu_si512(p, v); }
|
||||||
|
static inline Vec vAdd(Vec a, Vec b) { return _mm512_add_epi16(a, b); }
|
||||||
|
static inline Vec vSub(Vec a, Vec b) { return _mm512_sub_epi16(a, b); }
|
||||||
|
#define ACC_SIMD 1
|
||||||
|
#elif defined(__AVX2__)
|
||||||
|
#include <immintrin.h>
|
||||||
|
using Vec = __m256i;
|
||||||
|
static inline Vec vLoad(const int16_t* p) { return _mm256_loadu_si256(reinterpret_cast<const __m256i*>(p)); }
|
||||||
|
static inline void vStore(int16_t* p, Vec v) { _mm256_storeu_si256(reinterpret_cast<__m256i*>(p), v); }
|
||||||
|
static inline Vec vAdd(Vec a, Vec b) { return _mm256_add_epi16(a, b); }
|
||||||
|
static inline Vec vSub(Vec a, Vec b) { return _mm256_sub_epi16(a, b); }
|
||||||
|
#define ACC_SIMD 1
|
||||||
|
#elif defined(__SSE2__) || defined(_M_X64) || defined(_M_AMD64)
|
||||||
|
#include <immintrin.h>
|
||||||
|
using Vec = __m128i;
|
||||||
|
static inline Vec vLoad(const int16_t* p) { return _mm_loadu_si128(reinterpret_cast<const __m128i*>(p)); }
|
||||||
|
static inline void vStore(int16_t* p, Vec v) { _mm_storeu_si128(reinterpret_cast<__m128i*>(p), v); }
|
||||||
|
static inline Vec vAdd(Vec a, Vec b) { return _mm_add_epi16(a, b); }
|
||||||
|
static inline Vec vSub(Vec a, Vec b) { return _mm_sub_epi16(a, b); }
|
||||||
|
#define ACC_SIMD 1
|
||||||
|
#elif defined(__aarch64__)
|
||||||
|
#include <arm_neon.h>
|
||||||
|
using Vec = int16x8_t;
|
||||||
|
static inline Vec vLoad(const int16_t* p) { return vld1q_s16(p); }
|
||||||
|
static inline void vStore(int16_t* p, Vec v) { vst1q_s16(p, v); }
|
||||||
|
static inline Vec vAdd(Vec a, Vec b) { return vaddq_s16(a, b); }
|
||||||
|
static inline Vec vSub(Vec a, Vec b) { return vsubq_s16(a, b); }
|
||||||
|
#define ACC_SIMD 1
|
||||||
|
#endif
|
||||||
|
|
||||||
int subW2 = CalculateIndex(White, !stm, subPT2, sub2, mirroredWhite);
|
#ifdef ACC_SIMD
|
||||||
int subB2 = CalculateIndex(Black, !stm, subPT2, sub2, mirroredBlack);
|
|
||||||
|
|
||||||
for (int i = 0; i < NNUE::HL_SIZE; i++) {
|
constexpr size_t LANES = sizeof(Vec) / sizeof(int16_t);
|
||||||
white[i] += NNUE::net.accumulator_weights[addW * NNUE::HL_SIZE + i]
|
constexpr size_t TILE = 4;
|
||||||
- NNUE::net.accumulator_weights[subW1 * NNUE::HL_SIZE + i]
|
constexpr size_t STEP = LANES * TILE;
|
||||||
- NNUE::net.accumulator_weights[subW2 * NNUE::HL_SIZE + i];
|
static_assert(NNUE::HL_SIZE % STEP == 0, "HL size must be divisible by the SIMD tile size");
|
||||||
|
|
||||||
black[i] += NNUE::net.accumulator_weights[addB * NNUE::HL_SIZE + i]
|
template <int NA, int NS>
|
||||||
- NNUE::net.accumulator_weights[subB1 * NNUE::HL_SIZE + i]
|
ACC_NOINLINE static void ApplyKernel(int16_t* dst, const int16_t* src,
|
||||||
- NNUE::net.accumulator_weights[subB2 * NNUE::HL_SIZE + i];
|
const int16_t* a1, const int16_t* a2,
|
||||||
|
const int16_t* s1, const int16_t* s2) {
|
||||||
|
for (size_t i = 0; i < NNUE::HL_SIZE; i += STEP) {
|
||||||
|
Vec r[TILE];
|
||||||
|
|
||||||
|
for (size_t u = 0; u < TILE; u++) r[u] = vLoad(src + i + u * LANES);
|
||||||
|
for (size_t u = 0; u < TILE; u++) r[u] = vAdd(r[u], vLoad(a1 + i + u * LANES));
|
||||||
|
if constexpr (NA > 1)
|
||||||
|
for (size_t u = 0; u < TILE; u++) r[u] = vAdd(r[u], vLoad(a2 + i + u * LANES));
|
||||||
|
for (size_t u = 0; u < TILE; u++) r[u] = vSub(r[u], vLoad(s1 + i + u * LANES));
|
||||||
|
if constexpr (NS > 1)
|
||||||
|
for (size_t u = 0; u < TILE; u++) r[u] = vSub(r[u], vLoad(s2 + i + u * LANES));
|
||||||
|
|
||||||
|
for (size_t u = 0; u < TILE; u++) vStore(dst + i + u * LANES, r[u]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Castling
|
ACC_NOINLINE static void InPlaceKernel(int16_t* acc, const int16_t* const* adds, int nAdds,
|
||||||
void AccumulatorPair::addAddSubSub(bool stm, int add1, int addPT1, int add2, int addPT2, int sub1, int subPT1, int sub2, int subPT2) {
|
const int16_t* const* subs, int nSubs) {
|
||||||
int addW1 = CalculateIndex(White, stm, addPT1, add1, mirroredWhite);
|
for (size_t i = 0; i < NNUE::HL_SIZE; i += STEP) {
|
||||||
int addB1 = CalculateIndex(Black, stm, addPT1, add1, mirroredBlack);
|
Vec r[TILE];
|
||||||
|
|
||||||
int addW2 = CalculateIndex(White, stm, addPT2, add2, mirroredWhite);
|
for (size_t u = 0; u < TILE; u++) r[u] = vLoad(acc + i + u * LANES);
|
||||||
int addB2 = CalculateIndex(Black, stm, addPT2, add2, mirroredBlack);
|
|
||||||
|
|
||||||
int subW1 = CalculateIndex(White, stm, subPT1, sub1, mirroredWhite);
|
for (int j = 0; j < nAdds; j++)
|
||||||
int subB1 = CalculateIndex(Black, stm, subPT1, sub1, mirroredBlack);
|
for (size_t u = 0; u < TILE; u++) r[u] = vAdd(r[u], vLoad(adds[j] + i + u * LANES));
|
||||||
|
|
||||||
int subW2 = CalculateIndex(White, stm, subPT2, sub2, mirroredWhite);
|
for (int j = 0; j < nSubs; j++)
|
||||||
int subB2 = CalculateIndex(Black, stm, subPT2, sub2, mirroredBlack);
|
for (size_t u = 0; u < TILE; u++) r[u] = vSub(r[u], vLoad(subs[j] + i + u * LANES));
|
||||||
|
|
||||||
for (int i = 0; i < NNUE::HL_SIZE; i++) {
|
for (size_t u = 0; u < TILE; u++) vStore(acc + i + u * LANES, r[u]);
|
||||||
white[i] += NNUE::net.accumulator_weights[addW1 * NNUE::HL_SIZE + i]
|
|
||||||
+ NNUE::net.accumulator_weights[addW2 * NNUE::HL_SIZE + i]
|
|
||||||
- NNUE::net.accumulator_weights[subW1 * NNUE::HL_SIZE + i]
|
|
||||||
- NNUE::net.accumulator_weights[subW2 * NNUE::HL_SIZE + i];
|
|
||||||
|
|
||||||
black[i] += NNUE::net.accumulator_weights[addB1 * NNUE::HL_SIZE + i]
|
|
||||||
+ NNUE::net.accumulator_weights[addB2 * NNUE::HL_SIZE + i]
|
|
||||||
- NNUE::net.accumulator_weights[subB1 * NNUE::HL_SIZE + i]
|
|
||||||
- NNUE::net.accumulator_weights[subB2 * NNUE::HL_SIZE + i];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Apply(Accumulator& dst, const Accumulator& src, const int16_t* a1, const int16_t* a2, const int16_t* s1, const int16_t* s2) {
|
||||||
|
if (a2)
|
||||||
|
ApplyKernel<2, 2>(dst.data(), src.data(), a1, a2, s1, s2);
|
||||||
|
else if (s2)
|
||||||
|
ApplyKernel<1, 2>(dst.data(), src.data(), a1, nullptr, s1, s2);
|
||||||
|
else
|
||||||
|
ApplyKernel<1, 1>(dst.data(), src.data(), a1, nullptr, s1, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ApplyInPlace(Accumulator& acc, const int16_t* const* adds, int nAdds, const int16_t* const* subs, int nSubs) {
|
||||||
|
InPlaceKernel(acc.data(), adds, nAdds, subs, nSubs);
|
||||||
|
}
|
||||||
|
|
||||||
|
void AddRow(Accumulator& acc, const int16_t* row) {
|
||||||
|
const int16_t* rows[1] = {row};
|
||||||
|
InPlaceKernel(acc.data(), rows, 1, nullptr, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
using Ptr = int16_t* __restrict;
|
||||||
|
using CPtr = const int16_t* __restrict;
|
||||||
|
|
||||||
|
ACC_NOINLINE static void Kernel1p1m(Ptr dst, CPtr src, CPtr a, CPtr s) {
|
||||||
|
for (size_t i = 0; i < NNUE::HL_SIZE; i++)
|
||||||
|
dst[i] = static_cast<int16_t>(src[i] + a[i] - s[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
ACC_NOINLINE static void Kernel1p2m(Ptr dst, CPtr src, CPtr a, CPtr s1, CPtr s2) {
|
||||||
|
for (size_t i = 0; i < NNUE::HL_SIZE; i++)
|
||||||
|
dst[i] = static_cast<int16_t>(src[i] + a[i] - s1[i] - s2[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
ACC_NOINLINE static void Kernel2p2m(Ptr dst, CPtr src, CPtr a1, CPtr a2, CPtr s1, CPtr s2) {
|
||||||
|
for (size_t i = 0; i < NNUE::HL_SIZE; i++)
|
||||||
|
dst[i] = static_cast<int16_t>(src[i] + a1[i] + a2[i] - s1[i] - s2[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Apply(Accumulator& dst, const Accumulator& src, const int16_t* a1, const int16_t* a2, const int16_t* s1, const int16_t* s2) {
|
||||||
|
if (a2)
|
||||||
|
Kernel2p2m(dst.data(), src.data(), a1, a2, s1, s2);
|
||||||
|
else if (s2)
|
||||||
|
Kernel1p2m(dst.data(), src.data(), a1, s1, s2);
|
||||||
|
else
|
||||||
|
Kernel1p1m(dst.data(), src.data(), a1, s1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ApplyInPlace(Accumulator& acc, const int16_t* const* adds, int nAdds, const int16_t* const* subs, int nSubs) {
|
||||||
|
int16_t* p = acc.data();
|
||||||
|
|
||||||
|
for (int j = 0; j < nAdds; j++)
|
||||||
|
for (size_t i = 0; i < NNUE::HL_SIZE; i++)
|
||||||
|
p[i] = static_cast<int16_t>(p[i] + adds[j][i]);
|
||||||
|
|
||||||
|
for (int j = 0; j < nSubs; j++)
|
||||||
|
for (size_t i = 0; i < NNUE::HL_SIZE; i++)
|
||||||
|
p[i] = static_cast<int16_t>(p[i] - subs[j][i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
void AddRow(Accumulator& acc, const int16_t* row) {
|
||||||
|
const int16_t* rows[1] = {row};
|
||||||
|
ApplyInPlace(acc, rows, 1, nullptr, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
}
|
}
|
||||||
+83
-8
@@ -1,23 +1,98 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include <array>
|
||||||
|
#include <vector>
|
||||||
|
#include <cstdint>
|
||||||
#include "nnue.h"
|
#include "nnue.h"
|
||||||
|
|
||||||
namespace ACC {
|
namespace ACC {
|
||||||
|
|
||||||
|
struct BucketPair {
|
||||||
|
int white;
|
||||||
|
int black;
|
||||||
|
};
|
||||||
|
|
||||||
using Accumulator = std::array<int16_t, NNUE::HL_SIZE>;
|
using Accumulator = std::array<int16_t, NNUE::HL_SIZE>;
|
||||||
|
|
||||||
struct AccumulatorPair {
|
struct AccumulatorPair {
|
||||||
alignas(32) Accumulator white;
|
alignas(ALIGNMENT) Accumulator white;
|
||||||
alignas(32) Accumulator black;
|
alignas(ALIGNMENT) Accumulator black;
|
||||||
|
|
||||||
bool mirroredWhite = false;
|
Accumulator& get(bool perspective) { return perspective ? black : white; }
|
||||||
bool mirroredBlack = false;
|
const Accumulator& get(bool perspective) const { return perspective ? black : white; }
|
||||||
|
};
|
||||||
|
|
||||||
// yoinked from Quinniboi - Prelude
|
enum class UpdateType : uint8_t {
|
||||||
void addSub(bool stm, int add, int addPT, int sub, int subPT);
|
None,
|
||||||
void addSubSub(bool stm, int add, int addPT, int sub1, int subPT1, int sub2, int subPT2);
|
Refresh,
|
||||||
void addAddSubSub(bool stm, int add1, int addPT1, int add2, int addPT2, int sub1, int subPT1, int sub2, int subPT2);
|
Copy,
|
||||||
|
Delta
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Update {
|
||||||
|
UpdateType type = UpdateType::None;
|
||||||
|
uint8_t refreshMask = 0;
|
||||||
|
bool stm = false;
|
||||||
|
bool sub2Opp = false;
|
||||||
|
uint8_t nAdd = 0, nSub = 0;
|
||||||
|
int8_t add1 = 0, addPT1 = 0, add2 = 0, addPT2 = 0;
|
||||||
|
int8_t sub1 = 0, subPT1 = 0, sub2 = 0, subPT2 = 0;
|
||||||
|
|
||||||
|
void setAddSub(bool s, int add, int addPT, int sub, int subPT) {
|
||||||
|
type = UpdateType::Delta;
|
||||||
|
stm = s; sub2Opp = false; nAdd = 1; nSub = 1;
|
||||||
|
add1 = add; addPT1 = addPT; sub1 = sub; subPT1 = subPT;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setAddSubSub(bool s, int add, int addPT, int sub1_, int subPT1_, int sub2_, int subPT2_) {
|
||||||
|
type = UpdateType::Delta;
|
||||||
|
stm = s; sub2Opp = true; nAdd = 1; nSub = 2;
|
||||||
|
add1 = add; addPT1 = addPT;
|
||||||
|
sub1 = sub1_; subPT1 = subPT1_; sub2 = sub2_; subPT2 = subPT2_;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setAddAddSubSub(bool s, int add1_, int addPT1_, int add2_, int addPT2_, int sub1_, int subPT1_, int sub2_, int subPT2_) {
|
||||||
|
type = UpdateType::Delta;
|
||||||
|
stm = s; sub2Opp = false; nAdd = 2; nSub = 2;
|
||||||
|
add1 = add1_; addPT1 = addPT1_; add2 = add2_; addPT2 = addPT2_;
|
||||||
|
sub1 = sub1_; subPT1 = subPT1_; sub2 = sub2_; subPT2 = subPT2_;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setRefresh() {
|
||||||
|
*this = Update();
|
||||||
|
type = UpdateType::Refresh;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct FinnyEntry {
|
||||||
|
alignas(ALIGNMENT) Accumulator acc;
|
||||||
|
uint64_t bb[2][6];
|
||||||
|
};
|
||||||
|
|
||||||
|
struct FinnyTable {
|
||||||
|
FinnyEntry entries[2][2][NNUE::INPUT_BUCKETS];
|
||||||
|
|
||||||
|
void Reset();
|
||||||
|
};
|
||||||
|
|
||||||
|
struct AccStack {
|
||||||
|
std::vector<AccumulatorPair> slots;
|
||||||
|
std::vector<const AccumulatorPair*> cur;
|
||||||
|
FinnyTable finny;
|
||||||
|
|
||||||
|
explicit AccStack(size_t n) : slots(n), cur(n, nullptr) { finny.Reset(); }
|
||||||
|
|
||||||
|
AccStack(const AccStack& other) : AccStack(other.slots.size()) {}
|
||||||
|
AccStack& operator=(const AccStack&) { return *this; }
|
||||||
};
|
};
|
||||||
|
|
||||||
int CalculateIndex(bool perspective, bool side, int pieceType, int square, bool mirrored);
|
int CalculateIndex(bool perspective, bool side, int pieceType, int square, bool mirrored);
|
||||||
|
|
||||||
|
const int16_t* Row(int bucket, int index);
|
||||||
|
|
||||||
|
void Apply(Accumulator& dst, const Accumulator& src, const int16_t* a1, const int16_t* a2, const int16_t* s1, const int16_t* s2);
|
||||||
|
|
||||||
|
void ApplyInPlace(Accumulator& acc, const int16_t* const* adds, int nAdds, const int16_t* const* subs, int nSubs);
|
||||||
|
|
||||||
|
void AddRow(Accumulator& acc, const int16_t* row);
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -64,7 +64,9 @@ constexpr std::array<std::string_view, 50> fenPositions = {
|
|||||||
void RunBenchmark() {
|
void RunBenchmark() {
|
||||||
Board board;
|
Board board;
|
||||||
|
|
||||||
|
TTable BenchTT;
|
||||||
auto ctx = std::make_unique<SearchContext>();
|
auto ctx = std::make_unique<SearchContext>();
|
||||||
|
ctx->TT = &BenchTT;
|
||||||
|
|
||||||
Stopwatch sw;
|
Stopwatch sw;
|
||||||
for (int i = 0; i < fenPositions.size(); i++) {
|
for (int i = 0; i < fenPositions.size(); i++) {
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
constexpr int BENCH_DEPTH = 13;
|
constexpr int BENCH_DEPTH = 11;
|
||||||
|
|
||||||
void RunBenchmark();
|
void RunBenchmark();
|
||||||
+550
-90
@@ -1,9 +1,14 @@
|
|||||||
|
#include <bit>
|
||||||
#include "board.h"
|
#include "board.h"
|
||||||
|
#include "accumulator.h"
|
||||||
#include "movegen.h"
|
#include "movegen.h"
|
||||||
|
#include "nnue.h"
|
||||||
#include "tt.h"
|
#include "tt.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <ranges>
|
#include <ranges>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
|
#include <cassert>
|
||||||
|
#include "types.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
void Board::Reset() {
|
void Board::Reset() {
|
||||||
@@ -16,10 +21,19 @@ void Board::Reset() {
|
|||||||
sideToMove = White;
|
sideToMove = White;
|
||||||
occupied = 0ULL;
|
occupied = 0ULL;
|
||||||
|
|
||||||
|
checkers = 0ULL;
|
||||||
|
|
||||||
|
mailbox = std::array<int, 64>();
|
||||||
|
mailbox.fill(nullPieceType);
|
||||||
|
|
||||||
|
pinned = std::array<Bitboard, 2>();
|
||||||
|
|
||||||
pieces = std::array<Bitboard, 6>();
|
pieces = std::array<Bitboard, 6>();
|
||||||
colors = std::array<Bitboard, 2>();
|
colors = std::array<Bitboard, 2>();
|
||||||
|
|
||||||
accPair = ACC::AccumulatorPair();
|
accUpdate.setRefresh();
|
||||||
|
mirroredWhite = false;
|
||||||
|
mirroredBlack = false;
|
||||||
|
|
||||||
pieceThreats = std::array<Bitboard, 6>();
|
pieceThreats = std::array<Bitboard, 6>();
|
||||||
colorThreats = std::array<Bitboard, 2>();
|
colorThreats = std::array<Bitboard, 2>();
|
||||||
@@ -33,12 +47,14 @@ void Board::Reset() {
|
|||||||
hashKey = 0ULL;
|
hashKey = 0ULL;
|
||||||
pawnKey = 0ULL;
|
pawnKey = 0ULL;
|
||||||
nonPawnKey = 0ULL;
|
nonPawnKey = 0ULL;
|
||||||
|
majorKey = 0ULL;
|
||||||
|
|
||||||
|
checkZones = std::array<Bitboard, 4>();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Board::SetByFen(std::string_view fen) {
|
void Board::SetByFen(std::string_view fen) {
|
||||||
Reset();
|
Reset();
|
||||||
|
|
||||||
// Starting from top left
|
|
||||||
int currSquare = a8;
|
int currSquare = a8;
|
||||||
|
|
||||||
std::vector<std::string> tokens = UTILS::split(fen, ' ');
|
std::vector<std::string> tokens = UTILS::split(fen, ' ');
|
||||||
@@ -56,8 +72,7 @@ void Board::SetByFen(std::string_view fen) {
|
|||||||
bool side = std::islower(piece);
|
bool side = std::islower(piece);
|
||||||
int pieceType = pieceTypes.find(std::tolower(piece));
|
int pieceType = pieceTypes.find(std::tolower(piece));
|
||||||
|
|
||||||
colors[side].SetBit(currSquare);
|
SetPiece(pieceType, currSquare, side);
|
||||||
pieces[pieceType].SetBit(currSquare);
|
|
||||||
|
|
||||||
currSquare++;
|
currSquare++;
|
||||||
}
|
}
|
||||||
@@ -75,14 +90,20 @@ void Board::SetByFen(std::string_view fen) {
|
|||||||
|
|
||||||
if (tokens[3] != "-") enPassantTarget = UTILS::parseSquare(tokens[3]);
|
if (tokens[3] != "-") enPassantTarget = UTILS::parseSquare(tokens[3]);
|
||||||
|
|
||||||
// full and half move
|
|
||||||
if (tokens.size() > 4) {
|
if (tokens.size() > 4) {
|
||||||
halfMoves = std::stoi(tokens[4]);
|
halfMoves = std::stoi(tokens[4]);
|
||||||
fullMoves = std::stoi(tokens[5]);
|
fullMoves = std::stoi(tokens[5]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pawnKey = 0ULL;
|
||||||
|
nonPawnKey = 0ULL;
|
||||||
|
majorKey = 0ULL;
|
||||||
|
|
||||||
occupied = colors[White] | colors[Black];
|
occupied = colors[White] | colors[Black];
|
||||||
hashKey = UTILS::GetHashKey(*this);
|
hashKey = UTILS::GetHashKey(*this);
|
||||||
|
checkers = CalcCheckers();
|
||||||
|
pinned = {CalcPinned(White), CalcPinned(Black)};
|
||||||
|
CalcCheckers();
|
||||||
MOVEGEN::GenThreatMaps(*this);
|
MOVEGEN::GenThreatMaps(*this);
|
||||||
MOVEGEN::GenerateMoves<All>(*this, true);
|
MOVEGEN::GenerateMoves<All>(*this, true);
|
||||||
|
|
||||||
@@ -90,13 +111,13 @@ void Board::SetByFen(std::string_view fen) {
|
|||||||
int blackKingFile = (pieces[King] & colors[Black]).getLS1BIndex() % 8;
|
int blackKingFile = (pieces[King] & colors[Black]).getLS1BIndex() % 8;
|
||||||
|
|
||||||
if (whiteKingFile > 3) {
|
if (whiteKingFile > 3) {
|
||||||
accPair.mirroredWhite = true;
|
mirroredWhite = true;
|
||||||
}
|
}
|
||||||
if (blackKingFile > 3) {
|
if (blackKingFile > 3) {
|
||||||
accPair.mirroredBlack = true;
|
mirroredBlack = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
ResetAccPair();
|
accUpdate.setRefresh();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Board::GetFen() {
|
std::string Board::GetFen() {
|
||||||
@@ -246,11 +267,11 @@ void Board::ResetMoves() {
|
|||||||
void Board::ListMoves() {
|
void Board::ListMoves() {
|
||||||
int moveCount = 1;
|
int moveCount = 1;
|
||||||
for (int i = 0; i < currentMoveIndex; i++) {
|
for (int i = 0; i < currentMoveIndex; i++) {
|
||||||
Board copy = *this;
|
if (!IsLegal(moveList[i])) continue;
|
||||||
bool isLegal = copy.MakeMove(moveList[i]);
|
|
||||||
|
Board copy = *this;
|
||||||
|
copy.MakeMove(moveList[i]);
|
||||||
|
|
||||||
if (!isLegal)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
std::cout << moveCount << ". ";
|
std::cout << moveCount << ". ";
|
||||||
moveList[i].PrintMove();
|
moveList[i].PrintMove();
|
||||||
@@ -261,11 +282,7 @@ void Board::ListMoves() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int Board::GetPieceType(int square) {
|
int Board::GetPieceType(int square) {
|
||||||
for (int piece = Pawn; piece <= King; piece++) {
|
return mailbox[square];
|
||||||
if (pieces[piece].IsSet(square)) return piece;
|
|
||||||
}
|
|
||||||
|
|
||||||
return nullPieceType;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int Board::GetPieceColor(int square) {
|
int Board::GetPieceColor(int square) {
|
||||||
@@ -283,12 +300,18 @@ void Board::SetPiece(int piece, int square, bool color) {
|
|||||||
colors[color].SetBit(square);
|
colors[color].SetBit(square);
|
||||||
occupied.SetBit(square);
|
occupied.SetBit(square);
|
||||||
|
|
||||||
|
mailbox[square] = piece;
|
||||||
|
|
||||||
hashKey ^= UTILS::zKeys[color][piece][square];
|
hashKey ^= UTILS::zKeys[color][piece][square];
|
||||||
|
|
||||||
if (piece == Pawn) {
|
if (piece == Pawn) {
|
||||||
pawnKey ^= UTILS::zKeys[color][Pawn][square];
|
pawnKey ^= UTILS::zKeys[color][Pawn][square];
|
||||||
} else {
|
} else {
|
||||||
nonPawnKey ^= UTILS::zKeys[color][piece][square];
|
nonPawnKey ^= UTILS::zKeys[color][piece][square];
|
||||||
|
|
||||||
|
if (piece == King || piece == Rook || piece == Queen) {
|
||||||
|
majorKey ^= UTILS::zKeys[color][piece][square];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -297,19 +320,23 @@ void Board::RemovePiece(int piece, int square, bool color) {
|
|||||||
colors[color].PopBit(square);
|
colors[color].PopBit(square);
|
||||||
occupied.PopBit(square);
|
occupied.PopBit(square);
|
||||||
|
|
||||||
|
mailbox[square] = nullPieceType;
|
||||||
|
|
||||||
hashKey ^= UTILS::zKeys[color][piece][square];
|
hashKey ^= UTILS::zKeys[color][piece][square];
|
||||||
|
|
||||||
if (piece == Pawn) {
|
if (piece == Pawn) {
|
||||||
pawnKey ^= UTILS::zKeys[color][Pawn][square];
|
pawnKey ^= UTILS::zKeys[color][Pawn][square];
|
||||||
} else {
|
} else {
|
||||||
nonPawnKey ^= UTILS::zKeys[color][piece][square];
|
nonPawnKey ^= UTILS::zKeys[color][piece][square];
|
||||||
|
|
||||||
|
if (piece == King || piece == Rook || piece == Queen) {
|
||||||
|
majorKey ^= UTILS::zKeys[color][piece][square];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Updates castling rights
|
|
||||||
static void UpdateCastlingRights(Board &board, int square, int type, int color) {
|
static void UpdateCastlingRights(Board &board, int square, int type, int color) {
|
||||||
if (type == Rook) {
|
if (type == Rook) {
|
||||||
// Removing old rights
|
|
||||||
board.hashKey ^= UTILS::zCastle[board.castlingRights];
|
board.hashKey ^= UTILS::zCastle[board.castlingRights];
|
||||||
int queenSideRook = color ? a8 : a1;
|
int queenSideRook = color ? a8 : a1;
|
||||||
int kingSideRook = color ? h8 : h1;
|
int kingSideRook = color ? h8 : h1;
|
||||||
@@ -322,7 +349,6 @@ static void UpdateCastlingRights(Board &board, int square, int type, int color)
|
|||||||
|
|
||||||
board.hashKey ^= UTILS::zCastle[board.castlingRights];
|
board.hashKey ^= UTILS::zCastle[board.castlingRights];
|
||||||
} else if (type == King) {
|
} else if (type == King) {
|
||||||
// Removing old rights
|
|
||||||
board.hashKey ^= UTILS::zCastle[board.castlingRights];
|
board.hashKey ^= UTILS::zCastle[board.castlingRights];
|
||||||
|
|
||||||
board.castlingRights &= color ? ~blackKingRight : ~whiteKingRight;
|
board.castlingRights &= color ? ~blackKingRight : ~whiteKingRight;
|
||||||
@@ -342,10 +368,14 @@ void Board::Promote(int square, int pieceType, int color, bool isCapture) {
|
|||||||
SetPiece(pieceType, square, color);
|
SetPiece(pieceType, square, color);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Board::MakeMove(Move move) {
|
void Board::MakeMove(Move move) {
|
||||||
Board save = *this;
|
const bool accParentClean = accUpdate.type == ACC::UpdateType::None;
|
||||||
// Null Move
|
accUpdate = ACC::Update();
|
||||||
|
|
||||||
if (!move) {
|
if (!move) {
|
||||||
|
if (accParentClean) accUpdate.type = ACC::UpdateType::Copy;
|
||||||
|
else accUpdate.setRefresh();
|
||||||
|
|
||||||
int newEpTarget = noEPTarget;
|
int newEpTarget = noEPTarget;
|
||||||
|
|
||||||
sideToMove = !sideToMove;
|
sideToMove = !sideToMove;
|
||||||
@@ -357,7 +387,7 @@ bool Board::MakeMove(Move move) {
|
|||||||
|
|
||||||
enPassantTarget = newEpTarget;
|
enPassantTarget = newEpTarget;
|
||||||
|
|
||||||
return true;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int newEpTarget = noEPTarget;
|
int newEpTarget = noEPTarget;
|
||||||
@@ -370,28 +400,50 @@ bool Board::MakeMove(Move move) {
|
|||||||
|
|
||||||
int endPiece = attackerPiece;
|
int endPiece = attackerPiece;
|
||||||
|
|
||||||
// Removing attacker piece from old position
|
uint8_t refreshMask = 0;
|
||||||
|
|
||||||
|
if (attackerPiece == King) {
|
||||||
|
const int toFile = move.MoveTo() % 8;
|
||||||
|
|
||||||
|
if (attackerColor == White) {
|
||||||
|
if ((toFile > 3) != mirroredWhite) {
|
||||||
|
mirroredWhite = !mirroredWhite;
|
||||||
|
refreshMask |= 1 << White;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if ((toFile > 3) != mirroredBlack) {
|
||||||
|
mirroredBlack = !mirroredBlack;
|
||||||
|
refreshMask |= 1 << Black;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (NNUE::kingBuckets[attackerColor][move.MoveFrom()] != NNUE::kingBuckets[attackerColor][move.MoveTo()]) {
|
||||||
|
refreshMask |= 1 << attackerColor;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
RemovePiece(attackerPiece, move.MoveFrom(), attackerColor);
|
RemovePiece(attackerPiece, move.MoveFrom(), attackerColor);
|
||||||
|
|
||||||
if (move.IsPromo()) {
|
if (move.IsPromo()) {
|
||||||
|
endPiece = move.GetPromoPiece();
|
||||||
if (move.IsCapture()) {
|
if (move.IsCapture()) {
|
||||||
endPiece = move.GetFlags() - 9;
|
|
||||||
Promote(move.MoveTo(), endPiece, sideToMove, true);
|
Promote(move.MoveTo(), endPiece, sideToMove, true);
|
||||||
} else {
|
} else {
|
||||||
endPiece = move.GetFlags() - 5;
|
|
||||||
Promote(move.MoveTo(), endPiece, sideToMove, false);
|
Promote(move.MoveTo(), endPiece, sideToMove, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
if (move.IsCapture()) {
|
if (move.IsCapture()) {
|
||||||
if (move.GetFlags() != epCapture) {
|
if (move.GetFlags() != epCapture) {
|
||||||
accPair.addSubSub(sideToMove, move.MoveTo(), endPiece, move.MoveFrom(), attackerPiece, move.MoveTo(), targetPiece);
|
accUpdate.setAddSubSub(sideToMove, move.MoveTo(), endPiece, move.MoveFrom(), attackerPiece, move.MoveTo(), targetPiece);
|
||||||
} else {
|
} else {
|
||||||
accPair.addSubSub(sideToMove, enPassantTarget, endPiece, move.MoveFrom(), attackerPiece, move.MoveTo() - direction, Pawn);
|
accUpdate.setAddSubSub(sideToMove, enPassantTarget, endPiece, move.MoveFrom(), attackerPiece, move.MoveTo() - direction, Pawn);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (move.GetFlags() != kingCastle && move.GetFlags() != queenCastle) {
|
if (move.GetFlags() != kingCastle && move.GetFlags() != queenCastle) {
|
||||||
accPair.addSub(sideToMove, move.MoveTo(), endPiece, move.MoveFrom(), attackerPiece);
|
accUpdate.setAddSub(sideToMove, move.MoveTo(), endPiece, move.MoveFrom(), attackerPiece);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -419,52 +471,44 @@ bool Board::MakeMove(Move move) {
|
|||||||
{
|
{
|
||||||
int rookSquare = attackerColor ? h8 : h1;
|
int rookSquare = attackerColor ? h8 : h1;
|
||||||
|
|
||||||
// Removing rook from old position
|
|
||||||
RemovePiece(Rook, rookSquare, attackerColor);
|
RemovePiece(Rook, rookSquare, attackerColor);
|
||||||
|
|
||||||
// Setting rook on new position
|
|
||||||
SetPiece(Rook, rookSquare - 2, attackerColor);
|
SetPiece(Rook, rookSquare - 2, attackerColor);
|
||||||
|
|
||||||
SetPiece(attackerPiece, move.MoveTo(), attackerColor);
|
SetPiece(attackerPiece, move.MoveTo(), attackerColor);
|
||||||
|
|
||||||
accPair.addAddSubSub(sideToMove, move.MoveTo(), King, rookSquare - 2, Rook, move.MoveFrom(), King, rookSquare, Rook);
|
{
|
||||||
|
accUpdate.setAddAddSubSub(sideToMove, move.MoveTo(), King, rookSquare - 2, Rook, move.MoveFrom(), King, rookSquare, Rook);
|
||||||
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case queenCastle:
|
case queenCastle:
|
||||||
{
|
{
|
||||||
int rookSquare = attackerColor ? a8 : a1;
|
int rookSquare = attackerColor ? a8 : a1;
|
||||||
|
|
||||||
// Removing rook from old position
|
|
||||||
RemovePiece(Rook, rookSquare, attackerColor);
|
RemovePiece(Rook, rookSquare, attackerColor);
|
||||||
|
|
||||||
// Setting rook on new position
|
|
||||||
SetPiece(Rook, rookSquare + 3, attackerColor);
|
SetPiece(Rook, rookSquare + 3, attackerColor);
|
||||||
|
|
||||||
SetPiece(attackerPiece, move.MoveTo(), attackerColor);
|
SetPiece(attackerPiece, move.MoveTo(), attackerColor);
|
||||||
accPair.addAddSubSub(sideToMove, move.MoveTo(), King, rookSquare + 3, Rook, move.MoveFrom(), King, rookSquare, Rook);
|
|
||||||
|
{
|
||||||
|
accUpdate.setAddAddSubSub(sideToMove, move.MoveTo(), King, rookSquare + 3, Rook, move.MoveFrom(), King, rookSquare, Rook);
|
||||||
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (attackerPiece == King) {
|
if (!accParentClean || accUpdate.type == ACC::UpdateType::None) {
|
||||||
const int toFile = move.MoveTo() % 8;
|
accUpdate.setRefresh();
|
||||||
|
|
||||||
if (attackerColor == White) {
|
|
||||||
if ((toFile > 3) != accPair.mirroredWhite) {
|
|
||||||
accPair.mirroredWhite = !accPair.mirroredWhite;
|
|
||||||
ResetAccPair();
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if ((toFile > 3) != accPair.mirroredBlack) {
|
accUpdate.refreshMask = refreshMask;
|
||||||
accPair.mirroredBlack = !accPair.mirroredBlack;
|
|
||||||
ResetAccPair();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Removing the right to castle on king and rook movement
|
|
||||||
UpdateCastlingRights(*this, move.MoveFrom(), attackerPiece, attackerColor);
|
UpdateCastlingRights(*this, move.MoveFrom(), attackerPiece, attackerColor);
|
||||||
|
|
||||||
sideToMove = !attackerColor;
|
sideToMove = !attackerColor;
|
||||||
@@ -481,13 +525,10 @@ bool Board::MakeMove(Move move) {
|
|||||||
enPassantTarget = newEpTarget;
|
enPassantTarget = newEpTarget;
|
||||||
|
|
||||||
MOVEGEN::GenThreatMaps(*this);
|
MOVEGEN::GenThreatMaps(*this);
|
||||||
|
pinned = {CalcPinned(White), CalcPinned(Black)};
|
||||||
|
checkers = CalcCheckers();
|
||||||
|
CalcCheckZones();
|
||||||
|
|
||||||
sideToMove = !sideToMove;
|
|
||||||
if (InCheck()) {
|
|
||||||
*this = save;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
sideToMove = !sideToMove;
|
|
||||||
|
|
||||||
if (attackerColor == Black) fullMoves++;
|
if (attackerColor == Black) fullMoves++;
|
||||||
if (attackerPiece == Pawn || move.IsCapture()) {
|
if (attackerPiece == Pawn || move.IsCapture()) {
|
||||||
@@ -497,14 +538,11 @@ bool Board::MakeMove(Move move) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
positionIndex++;
|
positionIndex++;
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Board::InPossibleZug() {
|
bool Board::InPossibleZug() {
|
||||||
Bitboard toCheck;
|
Bitboard toCheck;
|
||||||
|
|
||||||
// For all pieces other than pawns and king
|
|
||||||
for (int piece = Knight; piece <= Queen; piece++) {
|
for (int piece = Knight; piece <= Queen; piece++) {
|
||||||
toCheck |= (pieces[piece] & colors[sideToMove]);
|
toCheck |= (pieces[piece] & colors[sideToMove]);
|
||||||
}
|
}
|
||||||
@@ -512,71 +550,493 @@ bool Board::InPossibleZug() {
|
|||||||
return !toCheck;
|
return !toCheck;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Board::ResetAccPair() {
|
ACC::BucketPair Board::GetBuckets() {
|
||||||
Bitboard whitePieces = colors[White];
|
int wKingSq = (pieces[King] & colors[White]).getLS1BIndex();
|
||||||
Bitboard blackPieces = colors[Black];
|
int bKingSq = (pieces[King] & colors[Black]).getLS1BIndex();
|
||||||
|
|
||||||
accPair.white = NNUE::net.accumulator_biases;
|
return {NNUE::kingBuckets[White][wKingSq],NNUE::kingBuckets[Black][bKingSq]};
|
||||||
accPair.black = NNUE::net.accumulator_biases;
|
|
||||||
|
|
||||||
while (whitePieces) {
|
|
||||||
int square = whitePieces.getLS1BIndex();
|
|
||||||
|
|
||||||
int wInput = ACC::CalculateIndex(White, White, GetPieceType(square), square, accPair.mirroredWhite);
|
|
||||||
int bInput = ACC::CalculateIndex(Black, White, GetPieceType(square), square, accPair.mirroredBlack);
|
|
||||||
|
|
||||||
for (int i = 0; i < NNUE::HL_SIZE; i++) {
|
|
||||||
accPair.white[i] += NNUE::net.accumulator_weights[wInput * NNUE::HL_SIZE + i];
|
|
||||||
accPair.black[i] += NNUE::net.accumulator_weights[bInput * NNUE::HL_SIZE + i];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
whitePieces.PopBit(square);
|
void Board::RefreshAccumulator(ACC::AccumulatorPair& out) const {
|
||||||
|
const int wKingSq = (pieces[King] & colors[White]).getLS1BIndex();
|
||||||
|
const int bKingSq = (pieces[King] & colors[Black]).getLS1BIndex();
|
||||||
|
const ACC::BucketPair bucketPair = {NNUE::kingBuckets[White][wKingSq], NNUE::kingBuckets[Black][bKingSq]};
|
||||||
|
|
||||||
|
out.white = NNUE::net.accumulator_biases;
|
||||||
|
out.black = NNUE::net.accumulator_biases;
|
||||||
|
|
||||||
|
for (int color = White; color <= Black; color++) {
|
||||||
|
Bitboard bb = colors[color];
|
||||||
|
|
||||||
|
while (bb) {
|
||||||
|
int square = bb.getLS1BIndex();
|
||||||
|
|
||||||
|
int wInput = ACC::CalculateIndex(White, color, mailbox[square], square, mirroredWhite);
|
||||||
|
int bInput = ACC::CalculateIndex(Black, color, mailbox[square], square, mirroredBlack);
|
||||||
|
|
||||||
|
ACC::AddRow(out.white, &NNUE::net.accumulator_weights[bucketPair.white][wInput * NNUE::HL_SIZE]);
|
||||||
|
ACC::AddRow(out.black, &NNUE::net.accumulator_weights[bucketPair.black][bInput * NNUE::HL_SIZE]);
|
||||||
|
|
||||||
|
bb.PopBit(square);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
while (blackPieces) {
|
void Board::RefreshPerspective(ACC::FinnyTable& finny, bool perspective, ACC::Accumulator& out) const {
|
||||||
int square = blackPieces.getLS1BIndex();
|
const bool mirrored = perspective == White ? mirroredWhite : mirroredBlack;
|
||||||
|
const int kingSq = (pieces[King] & colors[perspective]).getLS1BIndex();
|
||||||
|
const int bucket = NNUE::kingBuckets[perspective][kingSq];
|
||||||
|
|
||||||
int wInput = ACC::CalculateIndex(White, Black, GetPieceType(square), square, accPair.mirroredWhite);
|
ACC::FinnyEntry& entry = finny.entries[perspective][mirrored][bucket];
|
||||||
int bInput = ACC::CalculateIndex(Black, Black, GetPieceType(square), square, accPair.mirroredBlack);
|
|
||||||
|
|
||||||
for (int i = 0; i < NNUE::HL_SIZE; i++) {
|
const int16_t* adds[32];
|
||||||
accPair.white[i] += NNUE::net.accumulator_weights[wInput * NNUE::HL_SIZE + i];
|
const int16_t* subs[32];
|
||||||
accPair.black[i] += NNUE::net.accumulator_weights[bInput * NNUE::HL_SIZE + i];
|
int nAdds = 0;
|
||||||
|
int nSubs = 0;
|
||||||
|
|
||||||
|
for (int color = White; color <= Black; color++) {
|
||||||
|
for (int pt = Pawn; pt <= King; pt++) {
|
||||||
|
Bitboard tmp = pieces[pt] & colors[color];
|
||||||
|
const uint64_t now = tmp;
|
||||||
|
const uint64_t old = entry.bb[color][pt];
|
||||||
|
|
||||||
|
uint64_t added = now & ~old;
|
||||||
|
uint64_t removed = old & ~now;
|
||||||
|
|
||||||
|
while (added) {
|
||||||
|
const int square = std::countr_zero(added);
|
||||||
|
added &= added - 1;
|
||||||
|
adds[nAdds++] = ACC::Row(bucket, ACC::CalculateIndex(perspective, color, pt, square, mirrored));
|
||||||
}
|
}
|
||||||
|
|
||||||
blackPieces.PopBit(square);
|
while (removed) {
|
||||||
|
const int square = std::countr_zero(removed);
|
||||||
|
removed &= removed - 1;
|
||||||
|
subs[nSubs++] = ACC::Row(bucket, ACC::CalculateIndex(perspective, color, pt, square, mirrored));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
entry.bb[color][pt] = now;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ACC::ApplyInPlace(entry.acc, adds, nAdds, subs, nSubs);
|
||||||
|
out = entry.acc;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Board::ApplyDeltaPerspective(bool perspective, ACC::Accumulator& dst, const ACC::Accumulator& src) const {
|
||||||
|
const bool mirrored = perspective == White ? mirroredWhite : mirroredBlack;
|
||||||
|
const int kingSq = (pieces[King] & colors[perspective]).getLS1BIndex();
|
||||||
|
const int bucket = NNUE::kingBuckets[perspective][kingSq];
|
||||||
|
|
||||||
|
const bool stm = accUpdate.stm;
|
||||||
|
|
||||||
|
const int16_t* a1 = ACC::Row(bucket, ACC::CalculateIndex(perspective, stm, accUpdate.addPT1, accUpdate.add1, mirrored));
|
||||||
|
const int16_t* a2 = nullptr;
|
||||||
|
const int16_t* s1 = ACC::Row(bucket, ACC::CalculateIndex(perspective, stm, accUpdate.subPT1, accUpdate.sub1, mirrored));
|
||||||
|
const int16_t* s2 = nullptr;
|
||||||
|
|
||||||
|
if (accUpdate.nAdd == 2)
|
||||||
|
a2 = ACC::Row(bucket, ACC::CalculateIndex(perspective, stm, accUpdate.addPT2, accUpdate.add2, mirrored));
|
||||||
|
|
||||||
|
if (accUpdate.nSub == 2)
|
||||||
|
s2 = ACC::Row(bucket, ACC::CalculateIndex(perspective, accUpdate.sub2Opp ? !stm : stm, accUpdate.subPT2, accUpdate.sub2, mirrored));
|
||||||
|
|
||||||
|
ACC::Apply(dst, src, a1, a2, s1, s2);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Board::UpdateAccumulator(ACC::AccStack& stack, int ply) {
|
||||||
|
using ACC::UpdateType;
|
||||||
|
|
||||||
|
if (accUpdate.type == UpdateType::None) return;
|
||||||
|
|
||||||
|
if (accUpdate.type == UpdateType::Copy && ply > 0) {
|
||||||
|
stack.cur[ply] = stack.cur[ply - 1];
|
||||||
|
accUpdate.type = UpdateType::None;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ACC::AccumulatorPair& dst = stack.slots[ply];
|
||||||
|
|
||||||
|
if (ply == 0 || accUpdate.type != UpdateType::Delta) {
|
||||||
|
RefreshPerspective(stack.finny, White, dst.white);
|
||||||
|
RefreshPerspective(stack.finny, Black, dst.black);
|
||||||
|
} else {
|
||||||
|
const ACC::AccumulatorPair& prev = *stack.cur[ply - 1];
|
||||||
|
|
||||||
|
for (int perspective = White; perspective <= Black; perspective++) {
|
||||||
|
if (accUpdate.refreshMask & (1 << perspective))
|
||||||
|
RefreshPerspective(stack.finny, perspective, dst.get(perspective));
|
||||||
|
else
|
||||||
|
ApplyDeltaPerspective(perspective, dst.get(perspective), prev.get(perspective));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
stack.cur[ply] = &dst;
|
||||||
|
accUpdate.type = UpdateType::None;
|
||||||
}
|
}
|
||||||
|
|
||||||
Bitboard Board::AttacksTo(int square, Bitboard occupancy) {
|
Bitboard Board::AttacksTo(int square, Bitboard occupancy) {
|
||||||
Bitboard attacks;
|
Bitboard attacks;
|
||||||
|
|
||||||
// Pawn attacks
|
|
||||||
|
|
||||||
// Opponent attacks
|
|
||||||
attacks |= (MOVEGEN::pawnAttacks[sideToMove][square] & colors[!sideToMove] & pieces[Pawn]);
|
attacks |= (MOVEGEN::pawnAttacks[sideToMove][square] & colors[!sideToMove] & pieces[Pawn]);
|
||||||
|
|
||||||
// Own piece attacks
|
|
||||||
attacks |= (MOVEGEN::pawnAttacks[!sideToMove][square] & colors[sideToMove] & pieces[Pawn]);
|
attacks |= (MOVEGEN::pawnAttacks[!sideToMove][square] & colors[sideToMove] & pieces[Pawn]);
|
||||||
|
|
||||||
// Knight
|
|
||||||
attacks |= (MOVEGEN::knightAttacks[square] & pieces[Knight]);
|
attacks |= (MOVEGEN::knightAttacks[square] & pieces[Knight]);
|
||||||
|
|
||||||
// King
|
|
||||||
attacks |= (MOVEGEN::kingAttacks[square] & pieces[King]);
|
attacks |= (MOVEGEN::kingAttacks[square] & pieces[King]);
|
||||||
|
|
||||||
Bitboard bishopAttacks = MOVEGEN::getBishopAttack(square, occupancy);
|
Bitboard bishopAttacks = MOVEGEN::getBishopAttack(square, occupancy);
|
||||||
Bitboard rookAttacks = MOVEGEN::getRookAttack(square, occupancy);
|
Bitboard rookAttacks = MOVEGEN::getRookAttack(square, occupancy);
|
||||||
|
|
||||||
// Rook or queen
|
|
||||||
attacks |= (rookAttacks & (pieces[Rook] | pieces[Queen]));
|
attacks |= (rookAttacks & (pieces[Rook] | pieces[Queen]));
|
||||||
|
|
||||||
// Bishop or queen
|
|
||||||
attacks |= (bishopAttacks & (pieces[Bishop] | pieces[Queen]));
|
attacks |= (bishopAttacks & (pieces[Bishop] | pieces[Queen]));
|
||||||
|
|
||||||
return attacks;
|
return attacks;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static Bitboard rayBetween(int sq1, int sq2) {
|
||||||
|
if (sq1 == sq2) return 0ULL;
|
||||||
|
|
||||||
|
int f1 = sq1 % 8, r1 = sq1 / 8;
|
||||||
|
int f2 = sq2 % 8, r2 = sq2 / 8;
|
||||||
|
|
||||||
|
int df = (f2 > f1) - (f2 < f1);
|
||||||
|
int dr = (r2 > r1) - (r2 < r1);
|
||||||
|
|
||||||
|
if (df != 0 && dr != 0 && std::abs(f2 - f1) != std::abs(r2 - r1))
|
||||||
|
return 0ULL;
|
||||||
|
|
||||||
|
Bitboard ray = 0ULL;
|
||||||
|
|
||||||
|
int f = f1 + df;
|
||||||
|
int r = r1 + dr;
|
||||||
|
|
||||||
|
while (f != f2 || r != r2) {
|
||||||
|
ray |= 1ULL << (r * 8 + f);
|
||||||
|
f += df;
|
||||||
|
r += dr;
|
||||||
|
}
|
||||||
|
|
||||||
|
ray &= ~((1ULL << sq1) | (1ULL << sq2));
|
||||||
|
|
||||||
|
return ray;
|
||||||
|
}
|
||||||
|
|
||||||
|
static Bitboard fullRay(int from, int to) {
|
||||||
|
int df = (to % 8) - (from % 8);
|
||||||
|
int dr = (to / 8) - (from / 8);
|
||||||
|
|
||||||
|
if (df != 0) df /= std::abs(df);
|
||||||
|
if (dr != 0) dr /= std::abs(dr);
|
||||||
|
|
||||||
|
Bitboard ray = 0ULL;
|
||||||
|
|
||||||
|
int f = from % 8 + df;
|
||||||
|
int r = from / 8 + dr;
|
||||||
|
while (f >= 0 && f < 8 && r >= 0 && r < 8) {
|
||||||
|
ray |= 1ULL << (r * 8 + f);
|
||||||
|
f += df;
|
||||||
|
r += dr;
|
||||||
|
}
|
||||||
|
|
||||||
|
f = from % 8 - df;
|
||||||
|
r = from / 8 - dr;
|
||||||
|
while (f >= 0 && f < 8 && r >= 0 && r < 8) {
|
||||||
|
ray |= 1ULL << (r * 8 + f);
|
||||||
|
f -= df;
|
||||||
|
r -= dr;
|
||||||
|
}
|
||||||
|
|
||||||
|
ray |= 1ULL << from;
|
||||||
|
return ray;
|
||||||
|
}
|
||||||
|
|
||||||
bool Board::IsSquareThreatened(bool side, int square) {
|
bool Board::IsSquareThreatened(bool side, int square) {
|
||||||
return colorThreats[!side].IsSet(square);
|
return colorThreats[!side].IsSet(square);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Bitboard Board::CalcCheckers() {
|
||||||
|
return AttacksTo((colors[sideToMove] & pieces[King]).getLS1BIndex(), occupied) & colors[!sideToMove];
|
||||||
|
}
|
||||||
|
|
||||||
|
Bitboard Board::CalcPinned(bool color) {
|
||||||
|
Bitboard pinned;
|
||||||
|
|
||||||
|
int kingSquare = (pieces[King] & colors[color]).getLS1BIndex();
|
||||||
|
|
||||||
|
assert(kingSquare != -1);
|
||||||
|
|
||||||
|
Bitboard oppQueens = pieces[Queen] & colors[!color];
|
||||||
|
|
||||||
|
Bitboard potentialAttackers = MOVEGEN::getBishopAttack(kingSquare, colors[!color]) & (oppQueens | (pieces[Bishop] & colors[!color]))
|
||||||
|
| MOVEGEN::getRookAttack(kingSquare, colors[!color]) & (oppQueens | (pieces[Rook] & colors[!color]));
|
||||||
|
|
||||||
|
while (potentialAttackers) {
|
||||||
|
int attackerSquare = potentialAttackers.getLS1BIndex();
|
||||||
|
|
||||||
|
Bitboard isPinned = colors[color] & rayBetween(attackerSquare, kingSquare);
|
||||||
|
|
||||||
|
if (isPinned.PopCount() == 1)
|
||||||
|
pinned |= isPinned;
|
||||||
|
|
||||||
|
potentialAttackers.PopBit(attackerSquare);
|
||||||
|
}
|
||||||
|
|
||||||
|
return pinned;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Board::IsLegal(Move &move) {
|
||||||
|
assert(move != 0);
|
||||||
|
|
||||||
|
int us = sideToMove;
|
||||||
|
int them = !us;
|
||||||
|
|
||||||
|
int from = move.MoveFrom();
|
||||||
|
int to = move.MoveTo();
|
||||||
|
|
||||||
|
Bitboard king = pieces[King] & colors[us];
|
||||||
|
int kingSquare = king.getLS1BIndex();
|
||||||
|
|
||||||
|
int moveType = move.GetFlags();
|
||||||
|
|
||||||
|
if (moveType == epCapture) {
|
||||||
|
int epCapturedSquare = to + (us == White ? -8 : 8);
|
||||||
|
Bitboard occAfterEP = occupied ^ Bitboard::GetSquare(from) ^ Bitboard::GetSquare(to)
|
||||||
|
^ Bitboard::GetSquare(epCapturedSquare);
|
||||||
|
|
||||||
|
Bitboard theirQueens = pieces[Queen] & colors[them];
|
||||||
|
|
||||||
|
return (MOVEGEN::getBishopAttack(kingSquare, occAfterEP) & (theirQueens | (pieces[Bishop] & colors[them]))).PopCount() < 1
|
||||||
|
&& (MOVEGEN::getRookAttack(kingSquare, occAfterEP) & (theirQueens | (pieces[Rook] & colors[them]))).PopCount() < 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int movingPiece = GetPieceType(from);
|
||||||
|
|
||||||
|
if (movingPiece == King) {
|
||||||
|
Bitboard kinglessOcc = occupied ^ king;
|
||||||
|
Bitboard theirQueens = pieces[Queen] & colors[them];
|
||||||
|
|
||||||
|
return !colorThreats[them].IsSet(to)
|
||||||
|
&& (MOVEGEN::getBishopAttack(to, kinglessOcc) & (theirQueens | (pieces[Bishop] & colors[them]))).PopCount() == 0
|
||||||
|
&& (MOVEGEN::getRookAttack(to, kinglessOcc) & (theirQueens | (pieces[Rook] & colors[them]))).PopCount() == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (checkers.PopCount() > 1) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pinned[us].IsSet(from) && !fullRay(kingSquare, from).IsSet(to))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (checkers.PopCount() < 1)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
int checkerSquare = checkers.getLS1BIndex();
|
||||||
|
|
||||||
|
return (rayBetween(kingSquare, checkerSquare) | checkers).IsSet(to);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Board::IsPseudoLegal(Move &move) {
|
||||||
|
if (!move) return true;
|
||||||
|
|
||||||
|
const int from = move.MoveFrom();
|
||||||
|
const int to = move.MoveTo();
|
||||||
|
const int flag = move.GetFlags();
|
||||||
|
|
||||||
|
if ((unsigned)from >= 64u || (unsigned)to >= 64u) return false;
|
||||||
|
|
||||||
|
const int us = sideToMove;
|
||||||
|
const int them = !sideToMove;
|
||||||
|
|
||||||
|
const int movingPiece = GetPieceType(from);
|
||||||
|
if (movingPiece == nullPieceType) return false;
|
||||||
|
if (!colors[us].IsSet(from)) return false;
|
||||||
|
if (colors[us].IsSet(to)) return false;
|
||||||
|
|
||||||
|
const bool isCapture = move.IsCapture();
|
||||||
|
const int targetPiece = GetPieceType(to);
|
||||||
|
|
||||||
|
if (flag == capture) {
|
||||||
|
if (targetPiece == nullPieceType) return false;
|
||||||
|
if (!colors[them].IsSet(to)) return false;
|
||||||
|
} else if (isCapture && flag != epCapture) {
|
||||||
|
if (targetPiece == nullPieceType) return false;
|
||||||
|
if (!colors[them].IsSet(to)) return false;
|
||||||
|
} else if (!isCapture) {
|
||||||
|
if (occupied.IsSet(to)) {
|
||||||
|
if (flag != kingCastle && flag != queenCastle) return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const int fromFile = from & 7;
|
||||||
|
const int toFile = to & 7;
|
||||||
|
const int fromRank = from >> 3;
|
||||||
|
const int toRank = to >> 3;
|
||||||
|
|
||||||
|
const int dir = us ? south : north;
|
||||||
|
|
||||||
|
auto isOneStepPawnPush = [&] {
|
||||||
|
return to == from + dir && !occupied.IsSet(to);
|
||||||
|
};
|
||||||
|
|
||||||
|
auto isPawnCaptureTo = [&] {
|
||||||
|
return (to == from + dir - 1 && toFile == fromFile - 1)
|
||||||
|
|| (to == from + dir + 1 && toFile == fromFile + 1);
|
||||||
|
};
|
||||||
|
|
||||||
|
switch (flag) {
|
||||||
|
case quiet:
|
||||||
|
case capture:
|
||||||
|
break;
|
||||||
|
|
||||||
|
case doublePawnPush: {
|
||||||
|
if (movingPiece != Pawn) return false;
|
||||||
|
if (!us) {
|
||||||
|
if (fromRank != 1) return false;
|
||||||
|
} else {
|
||||||
|
if (fromRank != 6) return false;
|
||||||
|
}
|
||||||
|
const int mid = from + dir;
|
||||||
|
if (to != from + 2 * dir) return false;
|
||||||
|
if (occupied.IsSet(mid) || occupied.IsSet(to)) return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
case epCapture: {
|
||||||
|
if (movingPiece != Pawn) return false;
|
||||||
|
if (enPassantTarget == noEPTarget) return false;
|
||||||
|
if (to != enPassantTarget) return false;
|
||||||
|
if (!isPawnCaptureTo()) return false;
|
||||||
|
|
||||||
|
const int capSq = to - dir;
|
||||||
|
if (!colors[them].IsSet(capSq)) return false;
|
||||||
|
if (GetPieceType(capSq) != Pawn) return false;
|
||||||
|
if (occupied.IsSet(to)) return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
case kingCastle:
|
||||||
|
case queenCastle: {
|
||||||
|
if (movingPiece != King) return false;
|
||||||
|
|
||||||
|
const int kingSquare = (colors[us] & pieces[King]).getLS1BIndex();
|
||||||
|
if (from != kingSquare) return false;
|
||||||
|
|
||||||
|
const int kingRight = us ? blackKingRight : whiteKingRight;
|
||||||
|
const int queenRight = us ? blackQueenRight : whiteQueenRight;
|
||||||
|
|
||||||
|
if (flag == kingCastle) {
|
||||||
|
if (!(castlingRights & kingRight)) return false;
|
||||||
|
const U64 KingSide = us ? 0xf000000000000000ULL : 0xf0ULL;
|
||||||
|
const U64 mask = us ? 0x7000000000000000ULL : 0x70ULL;
|
||||||
|
const int targetSq = us ? g8 : g1;
|
||||||
|
if (to != targetSq) return false;
|
||||||
|
|
||||||
|
if ((Bitboard(KingSide) & occupied).PopCount() != 2) return false;
|
||||||
|
if (mask & colorThreats[them]) return false;
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
if (!(castlingRights & queenRight)) return false;
|
||||||
|
const U64 QueenSide = us ? 0x1f00000000000000ULL : 0x1fULL;
|
||||||
|
const U64 mask = us ? 0x1c00000000000000ULL : 0x1cULL;
|
||||||
|
const int targetSq = us ? c8 : c1;
|
||||||
|
if (to != targetSq) return false;
|
||||||
|
|
||||||
|
if ((Bitboard(QueenSide) & occupied).PopCount() != 2) return false;
|
||||||
|
if (mask & colorThreats[them]) return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
if (!move.IsPromo()) return false;
|
||||||
|
if (movingPiece != Pawn) return false;
|
||||||
|
|
||||||
|
if (!us) {
|
||||||
|
if (toRank != 7) return false;
|
||||||
|
} else {
|
||||||
|
if (toRank != 0) return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (move.IsCapture()) {
|
||||||
|
if (!isPawnCaptureTo()) return false;
|
||||||
|
if (!colors[them].IsSet(to)) return false;
|
||||||
|
} else {
|
||||||
|
if (!isOneStepPawnPush()) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (movingPiece) {
|
||||||
|
case Pawn: {
|
||||||
|
if (!us) { if (toRank == 7) return false; }
|
||||||
|
else { if (toRank == 0) return false; }
|
||||||
|
|
||||||
|
if (move.IsCapture()) {
|
||||||
|
if (!isPawnCaptureTo()) return false;
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return isOneStepPawnPush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
case Knight:
|
||||||
|
return (MOVEGEN::knightAttacks[from] & Bitboard::GetSquare(to)) != 0;
|
||||||
|
|
||||||
|
case Bishop: {
|
||||||
|
Bitboard att = MOVEGEN::getBishopAttack(from, occupied);
|
||||||
|
return (att & Bitboard::GetSquare(to)) != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
case Rook: {
|
||||||
|
Bitboard att = MOVEGEN::getRookAttack(from, occupied);
|
||||||
|
return (att & Bitboard::GetSquare(to)) != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
case Queen: {
|
||||||
|
Bitboard att = MOVEGEN::getBishopAttack(from, occupied) | MOVEGEN::getRookAttack(from, occupied);
|
||||||
|
return (att & Bitboard::GetSquare(to)) != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
case King:
|
||||||
|
return (MOVEGEN::kingAttacks[from] & Bitboard::GetSquare(to)) != 0;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Board::CalcCheckZones() {
|
||||||
|
int oppKingSquare = (colors[!sideToMove] & pieces[King]).getLS1BIndex();
|
||||||
|
|
||||||
|
checkZones[0] = MOVEGEN::pawnAttacks[!sideToMove][oppKingSquare];
|
||||||
|
checkZones[1] = MOVEGEN::knightAttacks[oppKingSquare];
|
||||||
|
checkZones[2] = MOVEGEN::getBishopAttack(oppKingSquare, occupied);
|
||||||
|
checkZones[3] = MOVEGEN::getRookAttack(oppKingSquare, occupied);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Board::GivesDirectCheck(Move &move) {
|
||||||
|
int attackerType = nullPieceType;
|
||||||
|
|
||||||
|
if (move.IsPromo()) {
|
||||||
|
attackerType = move.GetPromoPiece();
|
||||||
|
} else {
|
||||||
|
attackerType = GetPieceType(move.MoveFrom());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (attackerType == King)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
Bitboard checkZone = 0ULL;
|
||||||
|
|
||||||
|
if (attackerType == Queen) {
|
||||||
|
checkZone = checkZones[Bishop] | checkZones[Rook];
|
||||||
|
} else {
|
||||||
|
checkZone = checkZones[attackerType];
|
||||||
|
}
|
||||||
|
|
||||||
|
return checkZone.IsSet(move.MoveTo());
|
||||||
|
}
|
||||||
+27
-3
@@ -16,12 +16,19 @@ public:
|
|||||||
SetByFen(StartingFen);
|
SetByFen(StartingFen);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::array<int, 64> mailbox;
|
||||||
|
|
||||||
std::array<Bitboard, 6> pieces;
|
std::array<Bitboard, 6> pieces;
|
||||||
std::array<Bitboard, 2> colors;
|
std::array<Bitboard, 2> colors;
|
||||||
|
|
||||||
Bitboard occupied;
|
Bitboard occupied;
|
||||||
|
|
||||||
ACC::AccumulatorPair accPair;
|
std::array<Bitboard, 2> pinned;
|
||||||
|
Bitboard checkers;
|
||||||
|
|
||||||
|
ACC::Update accUpdate;
|
||||||
|
bool mirroredWhite = false;
|
||||||
|
bool mirroredBlack = false;
|
||||||
|
|
||||||
std::array<Bitboard, 6> pieceThreats;
|
std::array<Bitboard, 6> pieceThreats;
|
||||||
std::array<Bitboard, 2> colorThreats;
|
std::array<Bitboard, 2> colorThreats;
|
||||||
@@ -42,6 +49,9 @@ public:
|
|||||||
U64 hashKey = 0ULL;
|
U64 hashKey = 0ULL;
|
||||||
U64 pawnKey = 0ULL;
|
U64 pawnKey = 0ULL;
|
||||||
U64 nonPawnKey = 0ULL;
|
U64 nonPawnKey = 0ULL;
|
||||||
|
U64 majorKey = 0ULL;
|
||||||
|
|
||||||
|
std::array<Bitboard, 4> checkZones;
|
||||||
|
|
||||||
void Reset();
|
void Reset();
|
||||||
void SetByFen(std::string_view fen);
|
void SetByFen(std::string_view fen);
|
||||||
@@ -54,7 +64,12 @@ public:
|
|||||||
void ResetMoves();
|
void ResetMoves();
|
||||||
void ListMoves();
|
void ListMoves();
|
||||||
|
|
||||||
void ResetAccPair();
|
ACC::BucketPair GetBuckets();
|
||||||
|
void RefreshAccumulator(ACC::AccumulatorPair& out) const;
|
||||||
|
void UpdateAccumulator(ACC::AccStack& stack, int ply);
|
||||||
|
void RefreshPerspective(ACC::FinnyTable& finny, bool perspective, ACC::Accumulator& out) const;
|
||||||
|
void ApplyDeltaPerspective(bool perspective, ACC::Accumulator& dst, const ACC::Accumulator& src) const;
|
||||||
|
void MarkAccumulatorDirty() { accUpdate.setRefresh(); }
|
||||||
|
|
||||||
int GetPieceType(int square);
|
int GetPieceType(int square);
|
||||||
int GetPieceColor(int square);
|
int GetPieceColor(int square);
|
||||||
@@ -65,11 +80,20 @@ public:
|
|||||||
void RemovePiece(int piece, int square, bool color);
|
void RemovePiece(int piece, int square, bool color);
|
||||||
|
|
||||||
void Promote(int square, int pieceType, int color, bool isCapture);
|
void Promote(int square, int pieceType, int color, bool isCapture);
|
||||||
bool MakeMove(Move move);
|
void MakeMove(Move move);
|
||||||
|
|
||||||
bool InPossibleZug();
|
bool InPossibleZug();
|
||||||
|
|
||||||
Bitboard AttacksTo(int square, Bitboard occupancy);
|
Bitboard AttacksTo(int square, Bitboard occupancy);
|
||||||
|
|
||||||
bool IsSquareThreatened(bool side, int square);
|
bool IsSquareThreatened(bool side, int square);
|
||||||
|
|
||||||
|
Bitboard CalcCheckers();
|
||||||
|
Bitboard CalcPinned(bool color);
|
||||||
|
|
||||||
|
void CalcCheckZones();
|
||||||
|
bool GivesDirectCheck(Move &move);
|
||||||
|
|
||||||
|
bool IsLegal(Move &move);
|
||||||
|
bool IsPseudoLegal(Move &move);
|
||||||
};
|
};
|
||||||
+187
-106
@@ -6,29 +6,61 @@
|
|||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <fstream>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <iomanip>
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <csignal>
|
#include <csignal>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
#include <cerrno>
|
||||||
|
|
||||||
#include "datagen.h"
|
#include "datagen.h"
|
||||||
#include "movegen.h"
|
#include "movegen.h"
|
||||||
#include "search.h"
|
#include "search.h"
|
||||||
#include "movegen.h"
|
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
// OS-dependent threading includes
|
// OS-dependent threading includes
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#include <io.h> // for _commit
|
#include <io.h>
|
||||||
|
#include <process.h>
|
||||||
#else
|
#else
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <unistd.h> // for sync
|
#include <unistd.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
static std::filesystem::path MakeTempPath(const std::string& prefix, const std::string& ext) {
|
||||||
|
namespace fs = std::filesystem;
|
||||||
|
fs::path dir = fs::temp_directory_path();
|
||||||
|
|
||||||
|
std::random_device rd;
|
||||||
|
std::mt19937_64 gen(rd());
|
||||||
|
std::uniform_int_distribution<uint64_t> dist;
|
||||||
|
auto name = prefix + std::to_string(dist(gen)) + ext;
|
||||||
|
|
||||||
|
return dir / name;
|
||||||
|
}
|
||||||
|
|
||||||
|
static std::string ShellQuote(const std::string& s) {
|
||||||
|
#ifdef _WIN32
|
||||||
|
std::string out = "\"";
|
||||||
|
for (char c : s) {
|
||||||
|
if (c == '"') out += "\\\"";
|
||||||
|
else out += c;
|
||||||
|
}
|
||||||
|
out += "\"";
|
||||||
|
return out;
|
||||||
|
#else
|
||||||
|
std::string out = "'";
|
||||||
|
for (char c : s) {
|
||||||
|
if (c == '\'') out += "'\\''";
|
||||||
|
else out += c;
|
||||||
|
}
|
||||||
|
out += "'";
|
||||||
|
return out;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
static void HideCursor() {
|
static void HideCursor() {
|
||||||
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
|
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||||
@@ -46,38 +78,39 @@ static void ShowCursor() {
|
|||||||
SetConsoleCursorInfo(hConsole, &cursorInfo);
|
SetConsoleCursorInfo(hConsole, &cursorInfo);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
static void HideCursor() {
|
static void HideCursor() { std::cout << "\033[?25l"; }
|
||||||
std::cout << "\033[?25l";
|
static void ShowCursor() { std::cout << "\033[?25h"; }
|
||||||
}
|
|
||||||
|
|
||||||
static void ShowCursor() {
|
|
||||||
std::cout << "\033[?25h";
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace DATAGEN {
|
namespace DATAGEN {
|
||||||
|
|
||||||
// ============================================================================
|
|
||||||
// HTTP CLIENT FUNCTIONS (using system curl commands)
|
|
||||||
// ============================================================================
|
|
||||||
|
|
||||||
static bool PostJSON(const std::string& url, const std::string& jsonBody, std::string& response) {
|
static bool PostJSON(const std::string& url, const std::string& jsonBody, std::string& response) {
|
||||||
std::string tempFile = "/tmp/datagen_request.json";
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
|
fs::path tempFile = MakeTempPath("datagen_request_", ".json");
|
||||||
|
fs::path responseFile = MakeTempPath("datagen_response_", ".txt");
|
||||||
|
|
||||||
std::ofstream out(tempFile);
|
std::ofstream out(tempFile);
|
||||||
if (!out) {
|
if (!out) {
|
||||||
std::cerr << "Failed to create temp file for request" << std::endl;
|
std::cerr << "Failed to create temp file for request: " << tempFile.string() << "\n";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
out << jsonBody;
|
out << jsonBody;
|
||||||
out.close();
|
out.close();
|
||||||
|
|
||||||
std::string responseFile = "/tmp/datagen_response.txt";
|
|
||||||
std::ostringstream cmd;
|
std::ostringstream cmd;
|
||||||
cmd << "curl -s -X POST \"" << url << "\" "
|
cmd << "curl -s -X POST "
|
||||||
<< "-H \"Content-Type: application/json\" "
|
<< ShellQuote(url) << " "
|
||||||
<< "-d @" << tempFile << " "
|
<< "-H " << ShellQuote("Content-Type: application/json") << " "
|
||||||
<< "-o " << responseFile << " "
|
<< "-d " << ShellQuote("@" + tempFile.string()) << " "
|
||||||
<< "2>/dev/null";
|
<< "-o " << ShellQuote(responseFile.string()) << " ";
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
cmd << "2>nul";
|
||||||
|
#else
|
||||||
|
cmd << "2>/dev/null";
|
||||||
|
#endif
|
||||||
|
|
||||||
int result = system(cmd.str().c_str());
|
int result = system(cmd.str().c_str());
|
||||||
|
|
||||||
@@ -86,23 +119,34 @@ static bool PostJSON(const std::string& url, const std::string& jsonBody, std::s
|
|||||||
std::stringstream buffer;
|
std::stringstream buffer;
|
||||||
buffer << responseIn.rdbuf();
|
buffer << responseIn.rdbuf();
|
||||||
response = buffer.str();
|
response = buffer.str();
|
||||||
responseIn.close();
|
} else {
|
||||||
|
response.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::remove(tempFile.c_str());
|
std::error_code ec;
|
||||||
std::remove(responseFile.c_str());
|
fs::remove(tempFile, ec);
|
||||||
|
fs::remove(responseFile, ec);
|
||||||
|
|
||||||
return (result == 0);
|
return (result == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool UploadFile(const std::string& url, const std::string& filepath,
|
static bool UploadFile(const std::string& url,
|
||||||
const std::string& username, int positions) {
|
const std::string& filepath,
|
||||||
|
const std::string& username,
|
||||||
|
int positions) {
|
||||||
std::ostringstream cmd;
|
std::ostringstream cmd;
|
||||||
cmd << "curl -s -X POST \"" << url << "\" "
|
|
||||||
<< "-F \"file=@" << filepath << "\" "
|
cmd << "curl -s -X POST "
|
||||||
<< "-F \"user=" << username << "\" "
|
<< ShellQuote(url) << " "
|
||||||
<< "-F \"positions=" << positions << "\" "
|
<< "-F " << ShellQuote("file=@" + filepath) << " "
|
||||||
<< "2>/dev/null";
|
<< "-F " << ShellQuote("user=" + username) << " "
|
||||||
|
<< "-F " << ShellQuote("positions=" + std::to_string(positions)) << " ";
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
cmd << "2>nul";
|
||||||
|
#else
|
||||||
|
cmd << "2>/dev/null";
|
||||||
|
#endif
|
||||||
|
|
||||||
int result = system(cmd.str().c_str());
|
int result = system(cmd.str().c_str());
|
||||||
return (result == 0);
|
return (result == 0);
|
||||||
@@ -126,30 +170,24 @@ static std::string BuildHeartbeatJSON(const std::string& username, int threads)
|
|||||||
return json.str();
|
return json.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Simple JSON value extractor
|
|
||||||
static std::string ExtractJSONValue(const std::string& json, const std::string& key) {
|
static std::string ExtractJSONValue(const std::string& json, const std::string& key) {
|
||||||
// Look for "key":"value" or "key":value pattern
|
|
||||||
std::string searchKey = "\"" + key + "\":";
|
std::string searchKey = "\"" + key + "\":";
|
||||||
size_t pos = json.find(searchKey);
|
size_t pos = json.find(searchKey);
|
||||||
if (pos == std::string::npos) return "";
|
if (pos == std::string::npos) return "";
|
||||||
|
|
||||||
pos += searchKey.length();
|
pos += searchKey.length();
|
||||||
|
|
||||||
// Skip whitespace
|
|
||||||
while (pos < json.length() && (json[pos] == ' ' || json[pos] == '\t' || json[pos] == '\n')) {
|
while (pos < json.length() && (json[pos] == ' ' || json[pos] == '\t' || json[pos] == '\n')) {
|
||||||
pos++;
|
pos++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pos >= json.length()) return "";
|
if (pos >= json.length()) return "";
|
||||||
|
|
||||||
// Check if value is quoted
|
|
||||||
if (json[pos] == '"') {
|
if (json[pos] == '"') {
|
||||||
pos++; // Skip opening quote
|
pos++;
|
||||||
size_t endPos = json.find("\"", pos);
|
size_t endPos = json.find("\"", pos);
|
||||||
if (endPos == std::string::npos) return "";
|
if (endPos == std::string::npos) return "";
|
||||||
return json.substr(pos, endPos - pos);
|
return json.substr(pos, endPos - pos);
|
||||||
} else {
|
} else {
|
||||||
// Unquoted value (number, boolean, etc.)
|
|
||||||
size_t endPos = pos;
|
size_t endPos = pos;
|
||||||
while (endPos < json.length() && json[endPos] != ',' && json[endPos] != '}' && json[endPos] != ']') {
|
while (endPos < json.length() && json[endPos] != ',' && json[endPos] != '}' && json[endPos] != ']') {
|
||||||
endPos++;
|
endPos++;
|
||||||
@@ -158,9 +196,6 @@ static std::string ExtractJSONValue(const std::string& json, const std::string&
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ============================================================================
|
|
||||||
// HEARTBEAT THREAD
|
|
||||||
// ============================================================================
|
|
||||||
|
|
||||||
static void HeartbeatThread(const std::string& username, int threads,
|
static void HeartbeatThread(const std::string& username, int threads,
|
||||||
std::atomic<bool>& stopFlag, const std::string& serverUrl) {
|
std::atomic<bool>& stopFlag, const std::string& serverUrl) {
|
||||||
@@ -177,10 +212,6 @@ static void HeartbeatThread(const std::string& username, int threads,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ============================================================================
|
|
||||||
// EXISTING DATAGEN FUNCTIONS
|
|
||||||
// ============================================================================
|
|
||||||
|
|
||||||
// Signal handling globals
|
// Signal handling globals
|
||||||
static std::atomic<bool> g_shutdownRequested(false);
|
static std::atomic<bool> g_shutdownRequested(false);
|
||||||
|
|
||||||
@@ -206,10 +237,9 @@ static bool IsGameOver(Board &board, SEARCH::SearchContext* ctx) {
|
|||||||
int moveSeen = 0;
|
int moveSeen = 0;
|
||||||
|
|
||||||
for (int i = 0; i < board.currentMoveIndex; i++) {
|
for (int i = 0; i < board.currentMoveIndex; i++) {
|
||||||
|
if (!board.IsLegal(board.moveList[i])) continue;
|
||||||
Board copy = board;
|
Board copy = board;
|
||||||
bool isLegal = copy.MakeMove(board.moveList[i]);
|
copy.MakeMove(board.moveList[i]);
|
||||||
|
|
||||||
if (!isLegal) continue;
|
|
||||||
moveSeen++;
|
moveSeen++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -226,14 +256,22 @@ static void PlayRandMoves(Board &board, SEARCH::SearchContext* ctx) {
|
|||||||
for (int count = 0; count < RAND_MOVES + plusOne; count++) {
|
for (int count = 0; count < RAND_MOVES + plusOne; count++) {
|
||||||
MOVEGEN::GenerateMoves<All>(board, true);
|
MOVEGEN::GenerateMoves<All>(board, true);
|
||||||
|
|
||||||
std::uniform_int_distribution<int> dist(0, board.currentMoveIndex - 1);
|
if (board.currentMoveIndex <= 0) {
|
||||||
bool isLegal = board.MakeMove(board.moveList[dist(rng)]);
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
if (!isLegal) {
|
std::uniform_int_distribution<int> dist(0, board.currentMoveIndex - 1);
|
||||||
|
|
||||||
|
Move currMove = board.moveList[dist(rng)];
|
||||||
|
if (!board.IsLegal(currMove)) {
|
||||||
count--;
|
count--;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
board.MakeMove(currMove);
|
||||||
|
|
||||||
ctx->positionHistory[board.positionIndex] = board.hashKey;
|
ctx->positionHistory[board.positionIndex] = board.hashKey;
|
||||||
|
|
||||||
MOVEGEN::GenerateMoves<All>(board, true);
|
MOVEGEN::GenerateMoves<All>(board, true);
|
||||||
if (IsGameOver(board, ctx)) break;
|
if (IsGameOver(board, ctx)) break;
|
||||||
}
|
}
|
||||||
@@ -371,7 +409,9 @@ static void PlayGames(int id, std::atomic<int>& positions, std::atomic<bool>& st
|
|||||||
try {
|
try {
|
||||||
while (!stopFlag && !g_shutdownRequested) {
|
while (!stopFlag && !g_shutdownRequested) {
|
||||||
Board board;
|
Board board;
|
||||||
|
TTable DatagenTT;
|
||||||
auto ctx = std::make_unique<SEARCH::SearchContext>();
|
auto ctx = std::make_unique<SEARCH::SearchContext>();
|
||||||
|
ctx->TT = &DatagenTT;
|
||||||
|
|
||||||
PlayRandMoves(board, ctx.get());
|
PlayRandMoves(board, ctx.get());
|
||||||
if (IsGameOver(board, ctx.get())) continue;
|
if (IsGameOver(board, ctx.get())) continue;
|
||||||
@@ -387,7 +427,7 @@ static void PlayGames(int id, std::atomic<int>& positions, std::atomic<bool>& st
|
|||||||
|
|
||||||
SearchResults safeResults;
|
SearchResults safeResults;
|
||||||
|
|
||||||
int staticEval = UTILS::ConvertToWhiteRelative(board, NNUE::net.Evaluate(board));
|
int staticEval = UTILS::ConvertToWhiteRelative(board, NNUE::net.Evaluate(board, true));
|
||||||
int wdl = 1;
|
int wdl = 1;
|
||||||
|
|
||||||
while (!IsGameOver(board, ctx.get())) {
|
while (!IsGameOver(board, ctx.get())) {
|
||||||
@@ -428,28 +468,80 @@ static void PlayGames(int id, std::atomic<int>& positions, std::atomic<bool>& st
|
|||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (gamesBuffer.size() > 0) {
|
if (!gamesBuffer.empty()) {
|
||||||
WriteToFile(gamesBuffer, basePath, fileCounter);
|
WriteToFile(gamesBuffer, basePath, fileCounter);
|
||||||
gamesBuffer.clear();
|
gamesBuffer.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!stopFlag.load() && !g_shutdownRequested.load()) {
|
||||||
|
std::abort();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
static void CreateWorkerThread(int id, std::atomic<int>& positions, std::atomic<bool>& stopFlag, std::vector<std::thread>& threads) {
|
|
||||||
threads.emplace_back(PlayGames, id, std::ref(positions), std::ref(stopFlag));
|
struct WinThreadArgs {
|
||||||
|
int id;
|
||||||
|
std::atomic<int>* positions;
|
||||||
|
std::atomic<bool>* stopFlag;
|
||||||
|
};
|
||||||
|
|
||||||
|
static unsigned __stdcall WinThreadEntry(void* p) {
|
||||||
|
std::unique_ptr<WinThreadArgs> args(static_cast<WinThreadArgs*>(p));
|
||||||
|
PlayGames(args->id, *args->positions, *args->stopFlag);
|
||||||
|
_endthreadex(0);
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool CreateWorkerThread(int id,
|
||||||
|
std::atomic<int>& positions,
|
||||||
|
std::atomic<bool>& stopFlag,
|
||||||
|
std::vector<HANDLE>& threads,
|
||||||
|
size_t stackSizeBytes) {
|
||||||
|
auto* args = new WinThreadArgs{ id, &positions, &stopFlag };
|
||||||
|
|
||||||
|
unsigned tid = 0;
|
||||||
|
HANDLE h = reinterpret_cast<HANDLE>(
|
||||||
|
_beginthreadex(
|
||||||
|
nullptr,
|
||||||
|
static_cast<unsigned>(stackSizeBytes),
|
||||||
|
&WinThreadEntry,
|
||||||
|
args,
|
||||||
|
0,
|
||||||
|
&tid
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!h) {
|
||||||
|
std::cerr << "Failed to create thread " << id << " (errno=" << errno << ")\n";
|
||||||
|
delete args;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
threads.push_back(h);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void JoinWorkerThreads(std::vector<HANDLE>& threads) {
|
||||||
|
for (HANDLE h : threads) {
|
||||||
|
if (h) {
|
||||||
|
WaitForSingleObject(h, INFINITE);
|
||||||
|
CloseHandle(h);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
threads.clear();
|
||||||
|
}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
static void* ThreadFunc(void* arg) {
|
static void* ThreadFunc(void* arg) {
|
||||||
auto* tup = static_cast<std::tuple<int, std::atomic<int>*, std::atomic<bool>*>*>(arg);
|
auto* tup = static_cast<std::tuple<int, std::atomic<int>*, std::atomic<bool>*>*>(arg);
|
||||||
PlayGames(std::get<0>(*tup), *std::get<1>(*tup), *std::get<2>(*tup));
|
PlayGames(std::get<0>(*tup), *std::get<1>(*tup), *std::get<2>(*tup));
|
||||||
delete tup;
|
delete tup;
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
// ============================================================================
|
#endif
|
||||||
// MAIN RUN FUNCTIONS
|
|
||||||
// ============================================================================
|
|
||||||
|
|
||||||
void Run(int targetPositions, int threads) {
|
void Run(int targetPositions, int threads) {
|
||||||
SetupSignalHandlers();
|
SetupSignalHandlers();
|
||||||
@@ -466,10 +558,13 @@ void Run(int targetPositions, int threads) {
|
|||||||
std::atomic<bool> stopFlag = false;
|
std::atomic<bool> stopFlag = false;
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
std::vector<std::thread> workerThreads;
|
std::vector<HANDLE> workerThreads;
|
||||||
|
workerThreads.reserve((threads > 1) ? (threads - 1) : 0);
|
||||||
|
|
||||||
|
const size_t stackSizeBytes = 8ull * 1024ull * 1024ull;
|
||||||
|
|
||||||
for (int i = 0; i < threads - 1; ++i) {
|
for (int i = 0; i < threads - 1; ++i) {
|
||||||
CreateWorkerThread(i, positions, stopFlag, workerThreads);
|
CreateWorkerThread(i, positions, stopFlag, workerThreads, stackSizeBytes);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
std::vector<pthread_t> workerThreads;
|
std::vector<pthread_t> workerThreads;
|
||||||
@@ -506,11 +601,7 @@ void Run(int targetPositions, int threads) {
|
|||||||
std::cout << "\n\nWaiting for threads to finish writing..." << std::endl;
|
std::cout << "\n\nWaiting for threads to finish writing..." << std::endl;
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
for (std::thread& thread : workerThreads) {
|
JoinWorkerThreads(workerThreads);
|
||||||
if (thread.joinable()) {
|
|
||||||
thread.join();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#else
|
#else
|
||||||
for (pthread_t& thread : workerThreads) {
|
for (pthread_t& thread : workerThreads) {
|
||||||
pthread_join(thread, nullptr);
|
pthread_join(thread, nullptr);
|
||||||
@@ -541,7 +632,6 @@ void RunOnline(const std::string& username, int targetPositions, int threads) {
|
|||||||
std::cout << "===================" << std::endl << std::endl;
|
std::cout << "===================" << std::endl << std::endl;
|
||||||
|
|
||||||
while (!g_shutdownRequested) {
|
while (!g_shutdownRequested) {
|
||||||
// Send initial heartbeat
|
|
||||||
std::cout << "Sending heartbeat..." << std::endl;
|
std::cout << "Sending heartbeat..." << std::endl;
|
||||||
std::string heartbeatResponse;
|
std::string heartbeatResponse;
|
||||||
std::string heartbeatJSON = BuildHeartbeatJSON(username, threads);
|
std::string heartbeatJSON = BuildHeartbeatJSON(username, threads);
|
||||||
@@ -554,7 +644,6 @@ void RunOnline(const std::string& username, int targetPositions, int threads) {
|
|||||||
|
|
||||||
std::cout << "Heartbeat response: " << heartbeatResponse << std::endl;
|
std::cout << "Heartbeat response: " << heartbeatResponse << std::endl;
|
||||||
|
|
||||||
// Start session
|
|
||||||
std::string startResponse;
|
std::string startResponse;
|
||||||
std::string startJSON = BuildStartJSON(username, targetPositions);
|
std::string startJSON = BuildStartJSON(username, targetPositions);
|
||||||
|
|
||||||
@@ -576,11 +665,9 @@ void RunOnline(const std::string& username, int targetPositions, int threads) {
|
|||||||
|
|
||||||
std::cout << "Session ID: " << sessionId << std::endl;
|
std::cout << "Session ID: " << sessionId << std::endl;
|
||||||
|
|
||||||
// Start heartbeat thread
|
|
||||||
std::atomic<bool> heartbeatStop(false);
|
std::atomic<bool> heartbeatStop(false);
|
||||||
std::thread heartbeat(HeartbeatThread, username, threads, std::ref(heartbeatStop), SERVER_URL);
|
std::thread heartbeat(HeartbeatThread, username, threads, std::ref(heartbeatStop), SERVER_URL);
|
||||||
|
|
||||||
// Run datagen
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
system("cls");
|
system("cls");
|
||||||
HideCursor();
|
HideCursor();
|
||||||
@@ -593,10 +680,13 @@ void RunOnline(const std::string& username, int targetPositions, int threads) {
|
|||||||
std::atomic<bool> stopFlag = false;
|
std::atomic<bool> stopFlag = false;
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
std::vector<std::thread> workerThreads;
|
std::vector<HANDLE> workerThreads;
|
||||||
|
workerThreads.reserve((threads > 1) ? (threads - 1) : 0);
|
||||||
|
|
||||||
|
const size_t stackSizeBytes = 8ull * 1024ull * 1024ull;
|
||||||
|
|
||||||
for (int i = 0; i < threads - 1; ++i) {
|
for (int i = 0; i < threads - 1; ++i) {
|
||||||
CreateWorkerThread(i, positions, stopFlag, workerThreads);
|
CreateWorkerThread(i, positions, stopFlag, workerThreads, stackSizeBytes);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
std::vector<pthread_t> workerThreads;
|
std::vector<pthread_t> workerThreads;
|
||||||
@@ -633,18 +723,13 @@ void RunOnline(const std::string& username, int targetPositions, int threads) {
|
|||||||
std::cout << "\n\nWaiting for threads to finish writing..." << std::endl;
|
std::cout << "\n\nWaiting for threads to finish writing..." << std::endl;
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
for (std::thread& thread : workerThreads) {
|
JoinWorkerThreads(workerThreads);
|
||||||
if (thread.joinable()) {
|
|
||||||
thread.join();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#else
|
#else
|
||||||
for (pthread_t& thread : workerThreads) {
|
for (pthread_t& thread : workerThreads) {
|
||||||
pthread_join(thread, nullptr);
|
pthread_join(thread, nullptr);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Stop heartbeat
|
|
||||||
heartbeatStop = true;
|
heartbeatStop = true;
|
||||||
if (heartbeat.joinable()) {
|
if (heartbeat.joinable()) {
|
||||||
heartbeat.join();
|
heartbeat.join();
|
||||||
@@ -668,7 +753,6 @@ void RunOnline(const std::string& username, int targetPositions, int threads) {
|
|||||||
if (UploadFile(uploadUrl, mergedFile, username, positions.load())) {
|
if (UploadFile(uploadUrl, mergedFile, username, positions.load())) {
|
||||||
std::cout << "Upload successful!" << std::endl;
|
std::cout << "Upload successful!" << std::endl;
|
||||||
|
|
||||||
// Delete the file after successful upload
|
|
||||||
std::error_code ec;
|
std::error_code ec;
|
||||||
std::filesystem::remove(mergedFile, ec);
|
std::filesystem::remove(mergedFile, ec);
|
||||||
if (!ec) {
|
if (!ec) {
|
||||||
@@ -713,11 +797,10 @@ void PrintProgress(int positions, int targetPositions, Stopwatch &stopwatch, int
|
|||||||
int width = 60;
|
int width = 60;
|
||||||
|
|
||||||
std::string title = "Eleanor - Datagen";
|
std::string title = "Eleanor - Datagen";
|
||||||
int titlePadding = (width - title.length()) / 2;
|
int titlePadding = (width - static_cast<int>(title.length())) / 2;
|
||||||
std::cout << COLOR_TITLE << std::setw(titlePadding + title.length()) << title << COLOR_RESET << std::endl;
|
std::cout << COLOR_TITLE << std::setw(titlePadding + static_cast<int>(title.length())) << title << COLOR_RESET << std::endl;
|
||||||
|
|
||||||
std::cout << std::string(width, '=') << std::endl;
|
std::cout << std::string(width, '=') << std::endl;
|
||||||
|
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
|
|
||||||
std::cout << std::fixed << std::setprecision(0);
|
std::cout << std::fixed << std::setprecision(0);
|
||||||
@@ -726,31 +809,31 @@ void PrintProgress(int positions, int targetPositions, Stopwatch &stopwatch, int
|
|||||||
datagenTextStream << std::fixed << std::setprecision(2)
|
datagenTextStream << std::fixed << std::setprecision(2)
|
||||||
<< "Positions processed: " << positions / 1000.0 << "K | Target: " << targetPositions / 1000.0 << "K";
|
<< "Positions processed: " << positions / 1000.0 << "K | Target: " << targetPositions / 1000.0 << "K";
|
||||||
std::string datagenText = datagenTextStream.str();
|
std::string datagenText = datagenTextStream.str();
|
||||||
int datagenPadding = (width - datagenText.length()) / 2;
|
int datagenPadding = (width - static_cast<int>(datagenText.length())) / 2;
|
||||||
std::cout << COLOR_SECTION << std::setw(datagenPadding + datagenText.length()) << datagenText << COLOR_RESET << std::endl;
|
std::cout << COLOR_SECTION << std::setw(datagenPadding + static_cast<int>(datagenText.length())) << datagenText << COLOR_RESET << std::endl;
|
||||||
|
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
|
|
||||||
std::string elapsedText = "Elapsed Time: " + std::to_string(static_cast<int>(round(elapsed))) + " sec";
|
std::string elapsedText = "Elapsed Time: " + std::to_string(static_cast<int>(round(elapsed))) + " sec";
|
||||||
int elapsedPadding = (width - elapsedText.length()) / 2;
|
int elapsedPadding = (width - static_cast<int>(elapsedText.length())) / 2;
|
||||||
std::cout << COLOR_SECTION << std::setw(elapsedPadding + elapsedText.length()) << elapsedText << COLOR_RESET << std::endl;
|
std::cout << COLOR_SECTION << std::setw(elapsedPadding + static_cast<int>(elapsedText.length())) << elapsedText << COLOR_RESET << std::endl;
|
||||||
|
|
||||||
std::string positionsPerSecText = "Positions/sec: " + std::to_string(static_cast<int>(round(positionsPerSec)));
|
std::string positionsPerSecText = "Positions/sec: " + std::to_string(static_cast<int>(round(positionsPerSec)));
|
||||||
int positionsPerSecPadding = (width - positionsPerSecText.length()) / 2;
|
int positionsPerSecPadding = (width - static_cast<int>(positionsPerSecText.length())) / 2;
|
||||||
std::cout << COLOR_VALUE << std::setw(positionsPerSecPadding + positionsPerSecText.length()) << positionsPerSecText << COLOR_RESET << std::endl;
|
std::cout << COLOR_VALUE << std::setw(positionsPerSecPadding + static_cast<int>(positionsPerSecText.length())) << positionsPerSecText << COLOR_RESET << std::endl;
|
||||||
|
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
|
|
||||||
std::string threadsText = "Threads: " + std::to_string(static_cast<int>(threads));
|
std::string threadsText = "Threads: " + std::to_string(static_cast<int>(threads));
|
||||||
int threadsTextPadding = (width - threadsText.length()) / 2;
|
int threadsTextPadding = (width - static_cast<int>(threadsText.length())) / 2;
|
||||||
std::cout << COLOR_SECTION << std::setw(threadsTextPadding + threadsText.length()) << threadsText << COLOR_RESET << std::endl;
|
std::cout << COLOR_SECTION << std::setw(threadsTextPadding + static_cast<int>(threadsText.length())) << threadsText << COLOR_RESET << std::endl;
|
||||||
|
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
|
|
||||||
double remainingTime = 0.0;
|
double remainingTime = 0.0;
|
||||||
if (positions > 0 && elapsed > 0.0) {
|
if (positions > 0 && elapsed > 0.0) {
|
||||||
double positionsPerSec = positions / elapsed;
|
double pps = positions / elapsed;
|
||||||
remainingTime = (targetPositions - positions) / positionsPerSec;
|
remainingTime = (targetPositions - positions) / pps;
|
||||||
}
|
}
|
||||||
|
|
||||||
remainingTime = (std::max)(0.0, remainingTime);
|
remainingTime = (std::max)(0.0, remainingTime);
|
||||||
@@ -766,8 +849,9 @@ void PrintProgress(int positions, int targetPositions, Stopwatch &stopwatch, int
|
|||||||
<< std::setw(2) << remainingSeconds << "s";
|
<< std::setw(2) << remainingSeconds << "s";
|
||||||
|
|
||||||
std::string remainingTimeText = remainingTimeStream.str();
|
std::string remainingTimeText = remainingTimeStream.str();
|
||||||
int remainingPadding = (width - remainingTimeText.length()) / 2;
|
int remainingPadding = (width - static_cast<int>(remainingTimeText.length())) / 2;
|
||||||
std::cout << COLOR_SECTION << std::setw(remainingPadding + remainingTimeText.length())
|
std::cout << COLOR_SECTION
|
||||||
|
<< std::setw(remainingPadding + static_cast<int>(remainingTimeText.length()))
|
||||||
<< remainingTimeText << COLOR_RESET << std::endl;
|
<< remainingTimeText << COLOR_RESET << std::endl;
|
||||||
|
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
@@ -779,21 +863,18 @@ void PrintProgress(int positions, int targetPositions, Stopwatch &stopwatch, int
|
|||||||
std::string progressBar = "[";
|
std::string progressBar = "[";
|
||||||
|
|
||||||
for (int i = 0; i < barWidth; ++i) {
|
for (int i = 0; i < barWidth; ++i) {
|
||||||
if (i < pos) {
|
if (i < pos) progressBar += "=";
|
||||||
progressBar += "=";
|
else if (i == pos) progressBar += ">";
|
||||||
} else if (i == pos) {
|
else progressBar += " ";
|
||||||
progressBar += ">";
|
|
||||||
} else {
|
|
||||||
progressBar += " ";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
progressBar += "] " + std::to_string(static_cast<int>(round(progress * 100.0))) + " %";
|
progressBar += "] " + std::to_string(static_cast<int>(round(progress * 100.0))) + " %";
|
||||||
|
|
||||||
int progressPadding = (width - progressBar.length()) / 2;
|
int progressPadding = (width - static_cast<int>(progressBar.length())) / 2;
|
||||||
std::cout << COLOR_BAR << std::setw(progressPadding + progressBar.length()) << progressBar << COLOR_RESET << std::endl;
|
std::cout << COLOR_BAR << std::setw(progressPadding + static_cast<int>(progressBar.length()))
|
||||||
|
<< progressBar << COLOR_RESET << std::endl;
|
||||||
|
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
} // namespace DATAGEN
|
||||||
@@ -47,7 +47,11 @@ int main(int argc, char* argv[]) {
|
|||||||
MOVEGEN::initLeaperAttacks();
|
MOVEGEN::initLeaperAttacks();
|
||||||
MOVEGEN::initSliderAttacks();
|
MOVEGEN::initSliderAttacks();
|
||||||
UTILS::InitZobrist();
|
UTILS::InitZobrist();
|
||||||
|
#ifdef TUNING
|
||||||
|
SEARCH::RefreshTunableCaches();
|
||||||
|
#else
|
||||||
SEARCH::InitLMRTable();
|
SEARCH::InitLMRTable();
|
||||||
|
#endif
|
||||||
|
|
||||||
Board board;
|
Board board;
|
||||||
|
|
||||||
|
|||||||
@@ -64,6 +64,10 @@ public:
|
|||||||
return (GetFlags() >= knightPromotion);
|
return (GetFlags() >= knightPromotion);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool IsCastle() {
|
||||||
|
return (GetFlags() == kingCastle || GetFlags() == queenCastle);
|
||||||
|
}
|
||||||
|
|
||||||
void PrintMove() {
|
void PrintMove() {
|
||||||
std::cout << squareCoords[MoveFrom()] << squareCoords[MoveTo()];
|
std::cout << squareCoords[MoveFrom()] << squareCoords[MoveTo()];
|
||||||
|
|
||||||
@@ -146,6 +150,14 @@ public:
|
|||||||
return viriMove;
|
return viriMove;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int GetPromoPiece() {
|
||||||
|
if (IsCapture()) {
|
||||||
|
return GetFlags() - 9;
|
||||||
|
} else {
|
||||||
|
return GetFlags() - 5;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
operator uint16_t() {
|
operator uint16_t() {
|
||||||
return m_move;
|
return m_move;
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -38,7 +38,7 @@ template <MovegenMode mode>
|
|||||||
void GenKingMoves(Board &board);
|
void GenKingMoves(Board &board);
|
||||||
|
|
||||||
template <MovegenMode mode>
|
template <MovegenMode mode>
|
||||||
void GenerateMoves(Board &board, bool clear);
|
void GenerateMoves(Board &board, bool clear, bool recalcThreats = true);
|
||||||
|
|
||||||
void GenThreatMaps(Board &board);
|
void GenThreatMaps(Board &board);
|
||||||
|
|
||||||
|
|||||||
+3
-3
@@ -270,14 +270,14 @@ void GenKingMoves(Board &board) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <MovegenMode mode>
|
template <MovegenMode mode>
|
||||||
void GenerateMoves(Board &board, bool clear) {
|
void GenerateMoves(Board &board, bool clear, bool recalcThreats) {
|
||||||
if (clear) {
|
if (clear) {
|
||||||
board.ResetMoves();
|
board.ResetMoves();
|
||||||
board.pieceThreats = {};
|
|
||||||
board.colorThreats = {};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (recalcThreats) {
|
||||||
GenThreatMaps(board);
|
GenThreatMaps(board);
|
||||||
|
}
|
||||||
|
|
||||||
GenPawnMoves<mode>(board);
|
GenPawnMoves<mode>(board);
|
||||||
GenKnightMoves<mode>(board);
|
GenKnightMoves<mode>(board);
|
||||||
|
|||||||
@@ -0,0 +1,209 @@
|
|||||||
|
#include "movepicker.h"
|
||||||
|
#include "search.h"
|
||||||
|
#include "tunables.h"
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
namespace SEARCH {
|
||||||
|
|
||||||
|
template <MovegenMode mode>
|
||||||
|
MovePicker<mode>::MovePicker(Board &b, SearchContext *c, int p, Move tt)
|
||||||
|
: board(b), ctx(c), ply(p), ttMove(tt), killerMove(), stage(PickStage::TTMove) {
|
||||||
|
|
||||||
|
if (!ttMove || !board.IsPseudoLegal(ttMove)) {
|
||||||
|
ttMove = Move();
|
||||||
|
}
|
||||||
|
|
||||||
|
if constexpr (mode == Noisy) {
|
||||||
|
if (ttMove && !(ttMove.IsCapture() || ttMove.IsPromo())) {
|
||||||
|
ttMove = Move();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Move killer = ctx->killerMoves[ply];
|
||||||
|
|
||||||
|
if (killer && killer != ttMove && !killer.IsCapture() && !killer.IsPromo()
|
||||||
|
&& board.IsPseudoLegal(killer)) {
|
||||||
|
killerMove = killer;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <MovegenMode mode>
|
||||||
|
int MovePicker<mode>::ScoreNoisyMove(Move move) {
|
||||||
|
int score = MoveEstimatedValue(board, move) * noisyScoreScale;
|
||||||
|
|
||||||
|
if (move.IsCapture()) {
|
||||||
|
int attackerType = board.GetPieceType(move.MoveFrom());
|
||||||
|
int targetType = (move.GetFlags() == epCapture) ? Pawn : board.GetPieceType(move.MoveTo());
|
||||||
|
|
||||||
|
score += ctx->capthist[board.sideToMove][attackerType][targetType][move.MoveTo()];
|
||||||
|
}
|
||||||
|
|
||||||
|
return score;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <MovegenMode mode>
|
||||||
|
int MovePicker<mode>::ScoreQuietMove(Move move) {
|
||||||
|
bool sourceThreatened = board.IsSquareThreatened(board.sideToMove, move.MoveFrom());
|
||||||
|
bool targetThreatened = board.IsSquareThreatened(board.sideToMove, move.MoveTo());
|
||||||
|
|
||||||
|
int score = ctx->history[board.sideToMove][move.MoveFrom()][move.MoveTo()][sourceThreatened][targetThreatened];
|
||||||
|
|
||||||
|
if (ply > 0) score += ctx->conthist.GetNPly(board, move, ctx, ply, 1);
|
||||||
|
if (ply > 1) score += ctx->conthist.GetNPly(board, move, ctx, ply, 2);
|
||||||
|
|
||||||
|
return score;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <MovegenMode mode>
|
||||||
|
void MovePicker<mode>::ScoreNoisyRange() {
|
||||||
|
noisyStart = 0;
|
||||||
|
|
||||||
|
MOVEGEN::GenerateMoves<Noisy>(board, true, false);
|
||||||
|
|
||||||
|
int rawEnd = board.currentMoveIndex;
|
||||||
|
int write = noisyStart;
|
||||||
|
|
||||||
|
for (int read = noisyStart; read < rawEnd; read++) {
|
||||||
|
Move m = board.moveList[read];
|
||||||
|
if (m == ttMove || m == killerMove) continue;
|
||||||
|
|
||||||
|
board.moveList[write] = m;
|
||||||
|
scores[write] = ScoreNoisyMove(m);
|
||||||
|
write++;
|
||||||
|
}
|
||||||
|
|
||||||
|
noisyEnd = write;
|
||||||
|
board.currentMoveIndex = noisyEnd;
|
||||||
|
|
||||||
|
int goodWrite = noisyStart;
|
||||||
|
|
||||||
|
for (int i = noisyStart; i < noisyEnd; i++) {
|
||||||
|
Move m = board.moveList[i];
|
||||||
|
|
||||||
|
if (SEE(board, m, seeOrderingThreshold)) {
|
||||||
|
if (i != goodWrite) {
|
||||||
|
std::swap(board.moveList[i], board.moveList[goodWrite]);
|
||||||
|
std::swap(scores[i], scores[goodWrite]);
|
||||||
|
}
|
||||||
|
goodWrite++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
goodNoisyEnd = goodWrite;
|
||||||
|
noisyCursor = noisyStart;
|
||||||
|
badNoisyCursor = goodNoisyEnd;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <MovegenMode mode>
|
||||||
|
void MovePicker<mode>::ScoreQuietRange() {
|
||||||
|
quietStart = noisyEnd;
|
||||||
|
|
||||||
|
MOVEGEN::GenerateMoves<Quiet>(board, false, false);
|
||||||
|
|
||||||
|
int rawEnd = board.currentMoveIndex;
|
||||||
|
int write = quietStart;
|
||||||
|
|
||||||
|
for (int read = quietStart; read < rawEnd; read++) {
|
||||||
|
Move m = board.moveList[read];
|
||||||
|
if (m == ttMove || m == killerMove) continue;
|
||||||
|
|
||||||
|
board.moveList[write] = m;
|
||||||
|
scores[write] = ScoreQuietMove(m);
|
||||||
|
write++;
|
||||||
|
}
|
||||||
|
|
||||||
|
quietEnd = write;
|
||||||
|
board.currentMoveIndex = quietEnd;
|
||||||
|
quietCursor = quietStart;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <MovegenMode mode>
|
||||||
|
Move MovePicker<mode>::PopBest(int &cursor, int end) {
|
||||||
|
if (cursor >= end) return Move();
|
||||||
|
|
||||||
|
int bestIdx = cursor;
|
||||||
|
|
||||||
|
for (int i = cursor + 1; i < end; i++) {
|
||||||
|
if (scores[i] > scores[bestIdx]) bestIdx = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (bestIdx != cursor) {
|
||||||
|
std::swap(board.moveList[bestIdx], board.moveList[cursor]);
|
||||||
|
std::swap(scores[bestIdx], scores[cursor]);
|
||||||
|
}
|
||||||
|
|
||||||
|
Move best = board.moveList[cursor];
|
||||||
|
cursor++;
|
||||||
|
|
||||||
|
return best;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <MovegenMode mode>
|
||||||
|
Move MovePicker<mode>::Next() {
|
||||||
|
while (true) {
|
||||||
|
switch (stage) {
|
||||||
|
case PickStage::TTMove: {
|
||||||
|
stage = PickStage::GenNoisy;
|
||||||
|
if (ttMove) return ttMove;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
case PickStage::GenNoisy: {
|
||||||
|
ScoreNoisyRange();
|
||||||
|
stage = PickStage::GoodNoisy;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
case PickStage::GoodNoisy: {
|
||||||
|
Move m = PopBest(noisyCursor, goodNoisyEnd);
|
||||||
|
if (m) return m;
|
||||||
|
|
||||||
|
if constexpr (mode == Noisy) {
|
||||||
|
stage = PickStage::BadNoisy;
|
||||||
|
} else {
|
||||||
|
stage = PickStage::Killer;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
case PickStage::Killer: {
|
||||||
|
stage = PickStage::GenQuiet;
|
||||||
|
if (killerMove) return killerMove;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
case PickStage::GenQuiet: {
|
||||||
|
ScoreQuietRange();
|
||||||
|
stage = PickStage::Quiet;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
case PickStage::Quiet: {
|
||||||
|
Move m = PopBest(quietCursor, quietEnd);
|
||||||
|
if (m) return m;
|
||||||
|
|
||||||
|
stage = PickStage::BadNoisy;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
case PickStage::BadNoisy: {
|
||||||
|
Move m = PopBest(badNoisyCursor, noisyEnd);
|
||||||
|
if (m) return m;
|
||||||
|
|
||||||
|
stage = PickStage::Finished;
|
||||||
|
return Move();
|
||||||
|
}
|
||||||
|
|
||||||
|
case PickStage::Finished:
|
||||||
|
return Move();
|
||||||
|
|
||||||
|
default:
|
||||||
|
return Move();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template class MovePicker<Noisy>;
|
||||||
|
template class MovePicker<All>;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <array>
|
||||||
|
#include "board.h"
|
||||||
|
#include "movegen.h"
|
||||||
|
|
||||||
|
namespace SEARCH {
|
||||||
|
|
||||||
|
class SearchContext;
|
||||||
|
|
||||||
|
enum class PickStage {
|
||||||
|
TTMove,
|
||||||
|
GenNoisy,
|
||||||
|
GoodNoisy,
|
||||||
|
Killer,
|
||||||
|
GenQuiet,
|
||||||
|
Quiet,
|
||||||
|
BadNoisy,
|
||||||
|
Finished
|
||||||
|
};
|
||||||
|
|
||||||
|
template <MovegenMode mode>
|
||||||
|
class MovePicker {
|
||||||
|
public:
|
||||||
|
MovePicker(Board &board, SearchContext *ctx, int ply, Move ttMove);
|
||||||
|
|
||||||
|
Move Next();
|
||||||
|
|
||||||
|
private:
|
||||||
|
Board &board;
|
||||||
|
SearchContext *ctx;
|
||||||
|
int ply;
|
||||||
|
|
||||||
|
Move ttMove;
|
||||||
|
Move killerMove;
|
||||||
|
|
||||||
|
PickStage stage;
|
||||||
|
|
||||||
|
int noisyStart = 0;
|
||||||
|
int noisyEnd = 0;
|
||||||
|
int goodNoisyEnd = 0;
|
||||||
|
|
||||||
|
int quietStart = 0;
|
||||||
|
int quietEnd = 0;
|
||||||
|
|
||||||
|
int noisyCursor = 0;
|
||||||
|
int badNoisyCursor = 0;
|
||||||
|
int quietCursor = 0;
|
||||||
|
|
||||||
|
std::array<int, MAX_MOVES> scores{};
|
||||||
|
|
||||||
|
void ScoreNoisyRange();
|
||||||
|
|
||||||
|
void ScoreQuietRange();
|
||||||
|
|
||||||
|
int ScoreNoisyMove(Move move);
|
||||||
|
int ScoreQuietMove(Move move);
|
||||||
|
|
||||||
|
Move PopBest(int &cursor, int end);
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
+22
-16
@@ -15,24 +15,22 @@ void Network::Load(const std::string& path) {
|
|||||||
std::cerr << "Failed to open file " + path << std::endl;
|
std::cerr << "Failed to open file " + path << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
// HL weights
|
|
||||||
for (size_t i = 0; i < accumulator_weights.size(); i++) {
|
for (size_t i = 0; i < accumulator_weights.size(); i++) {
|
||||||
file.read(reinterpret_cast<char*>(&accumulator_weights[i]), sizeof(int16_t));
|
for (size_t j = 0; j < accumulator_weights[i].size(); j++) {
|
||||||
|
file.read(reinterpret_cast<char*>(&accumulator_weights[i][j]), sizeof(int16_t));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// HL biases
|
|
||||||
for (size_t i = 0; i < accumulator_biases.size(); i++) {
|
for (size_t i = 0; i < accumulator_biases.size(); i++) {
|
||||||
file.read(reinterpret_cast<char*>(&accumulator_biases[i]), sizeof(int16_t));
|
file.read(reinterpret_cast<char*>(&accumulator_biases[i]), sizeof(int16_t));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Output weights
|
|
||||||
for (size_t i = 0; i < output_weights.size(); i++) {
|
for (size_t i = 0; i < output_weights.size(); i++) {
|
||||||
for (size_t j = 0; j < output_weights[i].size(); j++) {
|
for (size_t j = 0; j < output_weights[i].size(); j++) {
|
||||||
file.read(reinterpret_cast<char*>(&output_weights[i][j]), sizeof(int16_t));
|
file.read(reinterpret_cast<char*>(&output_weights[i][j]), sizeof(int16_t));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Output biases
|
|
||||||
for (size_t i = 0; i < output_bias.size(); i++) {
|
for (size_t i = 0; i < output_bias.size(); i++) {
|
||||||
file.read(reinterpret_cast<char*>(&output_bias[i]), sizeof(int16_t));
|
file.read(reinterpret_cast<char*>(&output_bias[i]), sizeof(int16_t));
|
||||||
}
|
}
|
||||||
@@ -133,12 +131,12 @@ using nativeVector = void*;
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
static int32_t VectorizedSCReLU(const Board& board, const Network& net, size_t outputBucket) {
|
static int32_t VectorizedSCReLU(const Board& board, const ACC::AccumulatorPair& acc, const Network& net, size_t outputBucket) {
|
||||||
const size_t VECTOR_SIZE = sizeof(nativeVector) / sizeof(int16_t);
|
const size_t VECTOR_SIZE = sizeof(nativeVector) / sizeof(int16_t);
|
||||||
static_assert(HL_SIZE % VECTOR_SIZE == 0, "HL size must be divisible by the native register size of your CPU for vectorization to work");
|
static_assert(HL_SIZE % VECTOR_SIZE == 0, "HL size must be divisible by the native register size of your CPU for vectorization to work");
|
||||||
|
|
||||||
const ACC::Accumulator& stmAcc = board.sideToMove == White ? board.accPair.white : board.accPair.black;
|
const ACC::Accumulator& stmAcc = board.sideToMove == White ? acc.white : acc.black;
|
||||||
const ACC::Accumulator& nstmAcc = !board.sideToMove == White ? board.accPair.white : board.accPair.black;
|
const ACC::Accumulator& nstmAcc = !board.sideToMove == White ? acc.white : acc.black;
|
||||||
|
|
||||||
const nativeVector VEC_QA = set1_epi16(QA);
|
const nativeVector VEC_QA = set1_epi16(QA);
|
||||||
const nativeVector VEC_ZERO = set1_epi16(0);
|
const nativeVector VEC_ZERO = set1_epi16(0);
|
||||||
@@ -146,19 +144,15 @@ static int32_t VectorizedSCReLU(const Board& board, const Network& net, size_t o
|
|||||||
nativeVector accumulator{};
|
nativeVector accumulator{};
|
||||||
|
|
||||||
for (int i = 0; i < HL_SIZE; i += VECTOR_SIZE) {
|
for (int i = 0; i < HL_SIZE; i += VECTOR_SIZE) {
|
||||||
// Load accumulators
|
|
||||||
const nativeVector stmAccumValues = load_epi16(reinterpret_cast<const nativeVector*>(&stmAcc[i]));
|
const nativeVector stmAccumValues = load_epi16(reinterpret_cast<const nativeVector*>(&stmAcc[i]));
|
||||||
const nativeVector nstmAccumValues = load_epi16(reinterpret_cast<const nativeVector*>(&nstmAcc[i]));
|
const nativeVector nstmAccumValues = load_epi16(reinterpret_cast<const nativeVector*>(&nstmAcc[i]));
|
||||||
|
|
||||||
// Clamp values
|
|
||||||
const nativeVector stmClamped = min_epi16(VEC_QA, max_epi16(stmAccumValues, VEC_ZERO));
|
const nativeVector stmClamped = min_epi16(VEC_QA, max_epi16(stmAccumValues, VEC_ZERO));
|
||||||
const nativeVector nstmClamped = min_epi16(VEC_QA, max_epi16(nstmAccumValues, VEC_ZERO));
|
const nativeVector nstmClamped = min_epi16(VEC_QA, max_epi16(nstmAccumValues, VEC_ZERO));
|
||||||
|
|
||||||
// Load weights
|
|
||||||
const nativeVector stmWeights = load_epi16(reinterpret_cast<const nativeVector*>(&net.output_weights[outputBucket][i]));
|
const nativeVector stmWeights = load_epi16(reinterpret_cast<const nativeVector*>(&net.output_weights[outputBucket][i]));
|
||||||
const nativeVector nstmWeights = load_epi16(reinterpret_cast<const nativeVector*>(&net.output_weights[outputBucket][i + HL_SIZE]));
|
const nativeVector nstmWeights = load_epi16(reinterpret_cast<const nativeVector*>(&net.output_weights[outputBucket][i + HL_SIZE]));
|
||||||
|
|
||||||
// SCReLU activation
|
|
||||||
const nativeVector stmActivated = madd_epi16(stmClamped, mullo_epi16(stmClamped, stmWeights));
|
const nativeVector stmActivated = madd_epi16(stmClamped, mullo_epi16(stmClamped, stmWeights));
|
||||||
const nativeVector nstmActivated = madd_epi16(nstmClamped, mullo_epi16(nstmClamped, nstmWeights));
|
const nativeVector nstmActivated = madd_epi16(nstmClamped, mullo_epi16(nstmClamped, nstmWeights));
|
||||||
|
|
||||||
@@ -169,13 +163,13 @@ static int32_t VectorizedSCReLU(const Board& board, const Network& net, size_t o
|
|||||||
return reduce_epi32(accumulator);
|
return reduce_epi32(accumulator);
|
||||||
}
|
}
|
||||||
|
|
||||||
int Forward(const Board& board, const Network& net) {
|
static int Forward(const Board& board, const ACC::AccumulatorPair& acc, const Network& net) {
|
||||||
const size_t divisor = 32 / OUTPUT_BUCKETS;
|
const size_t divisor = 32 / OUTPUT_BUCKETS;
|
||||||
const size_t outputBucket = (board.occupied.PopCount() - 2) / divisor;
|
const size_t outputBucket = (board.occupied.PopCount() - 2) / divisor;
|
||||||
|
|
||||||
int64_t eval = 0;
|
int64_t eval = 0;
|
||||||
|
|
||||||
eval = VectorizedSCReLU(board, net, outputBucket);
|
eval = VectorizedSCReLU(board, acc, net, outputBucket);
|
||||||
|
|
||||||
eval /= QA;
|
eval /= QA;
|
||||||
|
|
||||||
@@ -185,9 +179,21 @@ int Forward(const Board& board, const Network& net) {
|
|||||||
return (eval * SCALE) / (QA * QB);
|
return (eval * SCALE) / (QA * QB);
|
||||||
}
|
}
|
||||||
|
|
||||||
int16_t Network::Evaluate(const Board& board) {
|
int16_t Network::Evaluate(const Board& board, const ACC::AccumulatorPair& acc, bool datagen) {
|
||||||
return std::clamp(Forward(board, *this), (-SEARCH::MATE_SCORE + SEARCH::MAX_DEPTH),
|
const int materialScale = datagen ? 4096 : 2048
|
||||||
|
+ 90 * board.pieces[Knight].PopCount()
|
||||||
|
+ 90 * board.pieces[Bishop].PopCount()
|
||||||
|
+ 180 * board.pieces[Rook].PopCount()
|
||||||
|
+ 360 * board.pieces[Queen].PopCount();
|
||||||
|
|
||||||
|
return std::clamp(Forward(board, acc, *this) * materialScale / 4096, (-SEARCH::MATE_SCORE + SEARCH::MAX_DEPTH),
|
||||||
(SEARCH::MATE_SCORE - SEARCH::MAX_DEPTH));
|
(SEARCH::MATE_SCORE - SEARCH::MAX_DEPTH));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int16_t Network::Evaluate(const Board& board, bool datagen) {
|
||||||
|
ACC::AccumulatorPair acc;
|
||||||
|
board.RefreshAccumulator(acc);
|
||||||
|
return Evaluate(board, acc, datagen);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
+33
-4
@@ -3,10 +3,12 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <array>
|
#include <array>
|
||||||
|
|
||||||
#include "../external/fmt/fmt/format.h"
|
|
||||||
|
|
||||||
class Board;
|
class Board;
|
||||||
|
|
||||||
|
namespace ACC {
|
||||||
|
struct AccumulatorPair;
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef __AVX512F__
|
#ifdef __AVX512F__
|
||||||
constexpr size_t ALIGNMENT = 64;
|
constexpr size_t ALIGNMENT = 64;
|
||||||
#else
|
#else
|
||||||
@@ -17,21 +19,48 @@ namespace NNUE {
|
|||||||
|
|
||||||
constexpr size_t INPUT_SIZE = 768;
|
constexpr size_t INPUT_SIZE = 768;
|
||||||
constexpr size_t HL_SIZE = 1536;
|
constexpr size_t HL_SIZE = 1536;
|
||||||
|
constexpr size_t INPUT_BUCKETS = 8;
|
||||||
constexpr size_t OUTPUT_BUCKETS = 8;
|
constexpr size_t OUTPUT_BUCKETS = 8;
|
||||||
|
|
||||||
constexpr int16_t SCALE = 400;
|
constexpr int16_t SCALE = 400;
|
||||||
constexpr int16_t QA = 255;
|
constexpr int16_t QA = 255;
|
||||||
constexpr int16_t QB = 64;
|
constexpr int16_t QB = 64;
|
||||||
|
|
||||||
|
constexpr int kingBuckets[2][64] = {
|
||||||
|
{
|
||||||
|
0, 1, 2, 3, 3, 2, 1, 0,
|
||||||
|
4, 4, 5, 5, 5, 5, 4, 4,
|
||||||
|
6, 6, 6, 6, 6, 6, 6, 6,
|
||||||
|
6, 6, 6, 6, 6, 6, 6, 6,
|
||||||
|
6, 6, 6, 6, 6, 6, 6, 6,
|
||||||
|
7, 7, 7, 7, 7, 7, 7, 7,
|
||||||
|
7, 7, 7, 7, 7, 7, 7, 7,
|
||||||
|
7, 7, 7, 7, 7, 7, 7, 7
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
7, 7, 7, 7, 7, 7, 7, 7,
|
||||||
|
7, 7, 7, 7, 7, 7, 7, 7,
|
||||||
|
7, 7, 7, 7, 7, 7, 7, 7,
|
||||||
|
6, 6, 6, 6, 6, 6, 6, 6,
|
||||||
|
6, 6, 6, 6, 6, 6, 6, 6,
|
||||||
|
6, 6, 6, 6, 6, 6, 6, 6,
|
||||||
|
4, 4, 5, 5, 5, 5, 4, 4,
|
||||||
|
0, 1, 2, 3, 3, 2, 1, 0,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
struct Network {
|
struct Network {
|
||||||
alignas(ALIGNMENT) std::array<int16_t, HL_SIZE * INPUT_SIZE> accumulator_weights;
|
alignas(ALIGNMENT) std::array<std::array<int16_t, HL_SIZE * INPUT_SIZE>, INPUT_BUCKETS> accumulator_weights;
|
||||||
alignas(ALIGNMENT) std::array<int16_t, HL_SIZE> accumulator_biases;
|
alignas(ALIGNMENT) std::array<int16_t, HL_SIZE> accumulator_biases;
|
||||||
alignas(ALIGNMENT) std::array<std::array<int16_t, 2 * HL_SIZE>, OUTPUT_BUCKETS> output_weights;
|
alignas(ALIGNMENT) std::array<std::array<int16_t, 2 * HL_SIZE>, OUTPUT_BUCKETS> output_weights;
|
||||||
std::array<int16_t, OUTPUT_BUCKETS> output_bias;
|
std::array<int16_t, OUTPUT_BUCKETS> output_bias;
|
||||||
|
|
||||||
void Load(const std::string& path);
|
void Load(const std::string& path);
|
||||||
|
|
||||||
int16_t Evaluate(const Board& board);
|
int16_t Evaluate(const Board& board, const ACC::AccumulatorPair& acc, bool datagen = false);
|
||||||
|
|
||||||
|
int16_t Evaluate(const Board& board, bool datagen = false);
|
||||||
};
|
};
|
||||||
|
|
||||||
inline Network net;
|
inline Network net;
|
||||||
|
|||||||
+9
-5
@@ -1,8 +1,9 @@
|
|||||||
#include "perft.h"
|
#include "perft.h"
|
||||||
#include "stopwatch.h"
|
#include "stopwatch.h"
|
||||||
#include "movegen.h"
|
#include "movegen.h"
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
static U64 HelperPerft(Board board, int depth) {
|
static U64 HelperPerft(Board &board, int depth) {
|
||||||
MOVEGEN::GenerateMoves<All>(board, true);
|
MOVEGEN::GenerateMoves<All>(board, true);
|
||||||
|
|
||||||
if (depth == 0) return 1ULL;
|
if (depth == 0) return 1ULL;
|
||||||
@@ -10,24 +11,27 @@ static U64 HelperPerft(Board board, int depth) {
|
|||||||
U64 nodes = 0;
|
U64 nodes = 0;
|
||||||
|
|
||||||
for (int i = 0; i < board.currentMoveIndex; i++) {
|
for (int i = 0; i < board.currentMoveIndex; i++) {
|
||||||
|
if (!board.IsLegal(board.moveList[i])) continue;
|
||||||
|
|
||||||
Board copy = board;
|
Board copy = board;
|
||||||
bool isLegal = copy.MakeMove(board.moveList[i]);
|
copy.MakeMove(board.moveList[i]);
|
||||||
if (!isLegal) continue;
|
|
||||||
nodes += HelperPerft(copy, depth - 1);
|
nodes += HelperPerft(copy, depth - 1);
|
||||||
}
|
}
|
||||||
return nodes;
|
return nodes;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Perft(Board &board, int depth) {
|
void Perft(Board &board, int depth) {
|
||||||
|
//board.SetByFen(StartingFen);
|
||||||
MOVEGEN::GenerateMoves<All>(board, true);
|
MOVEGEN::GenerateMoves<All>(board, true);
|
||||||
|
|
||||||
U64 totalNodes = 0;
|
U64 totalNodes = 0;
|
||||||
|
|
||||||
Stopwatch sw;
|
Stopwatch sw;
|
||||||
for (int i = 0; i < board.currentMoveIndex; i++) {
|
for (int i = 0; i < board.currentMoveIndex; i++) {
|
||||||
|
if (!board.IsLegal(board.moveList[i])) continue;
|
||||||
Board copy = board;
|
Board copy = board;
|
||||||
bool isLegal = copy.MakeMove(board.moveList[i]);
|
copy.MakeMove(board.moveList[i]);
|
||||||
if (!isLegal) continue;
|
|
||||||
U64 nodeCount = HelperPerft(copy, depth - 1);
|
U64 nodeCount = HelperPerft(copy, depth - 1);
|
||||||
totalNodes += nodeCount;
|
totalNodes += nodeCount;
|
||||||
board.moveList[i].PrintMove();
|
board.moveList[i].PrintMove();
|
||||||
|
|||||||
+373
-337
File diff suppressed because it is too large.
Load diff
+42
-13
@@ -23,6 +23,7 @@ enum searchMode {
|
|||||||
constexpr int MATE_SCORE = 32767;
|
constexpr int MATE_SCORE = 32767;
|
||||||
constexpr int MAX_DEPTH = 256;
|
constexpr int MAX_DEPTH = 256;
|
||||||
constexpr int MAX_HISTORY = 16384;
|
constexpr int MAX_HISTORY = 16384;
|
||||||
|
constexpr int WIN_SCORE = 30000;
|
||||||
|
|
||||||
constexpr int CORRHIST_WEIGHT_SCALE = 256;
|
constexpr int CORRHIST_WEIGHT_SCALE = 256;
|
||||||
constexpr int CORRHIST_GRAIN = 256;
|
constexpr int CORRHIST_GRAIN = 256;
|
||||||
@@ -33,7 +34,6 @@ constexpr int CORRHIST_MAX = 16384;
|
|||||||
constexpr int32_t ScoreNone = -255000;
|
constexpr int32_t ScoreNone = -255000;
|
||||||
constexpr int inf = 100000;
|
constexpr int inf = 100000;
|
||||||
|
|
||||||
// first index: [0] noisy, [1] quiet
|
|
||||||
inline int lmrTable[2][MAX_DEPTH+1][MAX_MOVES];
|
inline int lmrTable[2][MAX_DEPTH+1][MAX_MOVES];
|
||||||
|
|
||||||
#ifdef TUNING
|
#ifdef TUNING
|
||||||
@@ -47,13 +47,38 @@ constexpr std::array<int, 6> SEEPieceValues = {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
void InitLMRTable();
|
void InitLMRTable();
|
||||||
|
|
||||||
|
#ifdef TUNING
|
||||||
|
void InitSEEPieceValues();
|
||||||
|
void RefreshTunableCaches();
|
||||||
|
#endif
|
||||||
|
|
||||||
class SearchContext;
|
class SearchContext;
|
||||||
|
|
||||||
|
class CaptHistory {
|
||||||
|
private:
|
||||||
|
MultiArray<int, 2, 6, 6, 64> historyMoves;
|
||||||
|
public:
|
||||||
|
void Update(bool stm, int moving, int capture, int to, int bonus) {
|
||||||
|
int clampedBonus = std::clamp(bonus, -MAX_HISTORY, MAX_HISTORY);
|
||||||
|
historyMoves[stm][moving][capture][to] +=
|
||||||
|
clampedBonus - historyMoves[stm][moving][capture][to] * std::abs(clampedBonus) / MAX_HISTORY;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Clear() {
|
||||||
|
std::fill(&historyMoves[0][0][0][0], &historyMoves[0][0][0][0] + sizeof(historyMoves) / sizeof(int), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto& operator[](int index) {
|
||||||
|
return historyMoves[index];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
class CorrHist {
|
class CorrHist {
|
||||||
private:
|
private:
|
||||||
// indexed by [stm][key]
|
|
||||||
MultiArray<int, 2, CORRHIST_SIZE> pawnHist;
|
MultiArray<int, 2, CORRHIST_SIZE> pawnHist;
|
||||||
MultiArray<int, 2, CORRHIST_SIZE> nonPawnHist;
|
MultiArray<int, 2, CORRHIST_SIZE> nonPawnHist;
|
||||||
|
MultiArray<int, 2, CORRHIST_SIZE> majorHist;
|
||||||
public:
|
public:
|
||||||
void Update(Board& board, int depth, int diff, int* entry) {
|
void Update(Board& board, int depth, int diff, int* entry) {
|
||||||
const int scaledDiff = diff * CORRHIST_GRAIN;
|
const int scaledDiff = diff * CORRHIST_GRAIN;
|
||||||
@@ -66,22 +91,24 @@ public:
|
|||||||
void UpdateAll(Board& board, int depth, int diff) {
|
void UpdateAll(Board& board, int depth, int diff) {
|
||||||
Update(board, depth, diff, &pawnHist[board.sideToMove][board.pawnKey % CORRHIST_SIZE]);
|
Update(board, depth, diff, &pawnHist[board.sideToMove][board.pawnKey % CORRHIST_SIZE]);
|
||||||
Update(board, depth, diff, &nonPawnHist[board.sideToMove][board.nonPawnKey % CORRHIST_SIZE]);
|
Update(board, depth, diff, &nonPawnHist[board.sideToMove][board.nonPawnKey % CORRHIST_SIZE]);
|
||||||
|
Update(board, depth, diff, &majorHist[board.sideToMove][board.majorKey % CORRHIST_SIZE]);
|
||||||
}
|
}
|
||||||
|
|
||||||
int GetAllHist(Board& board) {
|
int GetAllHist(Board& board) {
|
||||||
return pawnHist[board.sideToMove][board.pawnKey % CORRHIST_SIZE]
|
return pawnHist[board.sideToMove][board.pawnKey % CORRHIST_SIZE]
|
||||||
+ nonPawnHist[board.sideToMove][board.nonPawnKey % CORRHIST_SIZE];
|
+ nonPawnHist[board.sideToMove][board.nonPawnKey % CORRHIST_SIZE]
|
||||||
|
+ majorHist[board.sideToMove][board.majorKey % CORRHIST_SIZE];
|
||||||
}
|
}
|
||||||
|
|
||||||
void Clear() {
|
void Clear() {
|
||||||
std::fill(&pawnHist[0][0], &pawnHist[0][0] + sizeof(pawnHist) / sizeof(int), 0);
|
std::fill(&pawnHist[0][0], &pawnHist[0][0] + sizeof(pawnHist) / sizeof(int), 0);
|
||||||
std::fill(&nonPawnHist[0][0], &nonPawnHist[0][0] + sizeof(nonPawnHist) / sizeof(int), 0);
|
std::fill(&nonPawnHist[0][0], &nonPawnHist[0][0] + sizeof(nonPawnHist) / sizeof(int), 0);
|
||||||
|
std::fill(&majorHist[0][0], &majorHist[0][0] + sizeof(majorHist) / sizeof(int), 0);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class ContHistory {
|
class ContHistory {
|
||||||
private:
|
private:
|
||||||
// indexed by [other color][prev move piece][stm][prev move to][piece][to]
|
|
||||||
MultiArray<int16_t, 2, 6, 64, 2, 6, 64> contHistMoves;
|
MultiArray<int16_t, 2, 6, 64, 2, 6, 64> contHistMoves;
|
||||||
public:
|
public:
|
||||||
void Update(bool stm, bool otherColor, int prevType, int prevTo, int type, int to, int bonus) {
|
void Update(bool stm, bool otherColor, int prevType, int prevTo, int type, int to, int bonus) {
|
||||||
@@ -104,7 +131,6 @@ public:
|
|||||||
|
|
||||||
class History {
|
class History {
|
||||||
private:
|
private:
|
||||||
// indexed by [stm][from][to][threatenedSource][threatenedTarget]
|
|
||||||
MultiArray<int, 2, 64, 64, 2, 2> historyMoves;
|
MultiArray<int, 2, 64, 64, 2, 2> historyMoves;
|
||||||
public:
|
public:
|
||||||
void Update(bool stm, Move move, bool source, bool target, int bonus) {
|
void Update(bool stm, Move move, bool source, bool target, int bonus) {
|
||||||
@@ -187,6 +213,8 @@ public:
|
|||||||
|
|
||||||
class SearchContext {
|
class SearchContext {
|
||||||
public:
|
public:
|
||||||
|
bool doPrint = false;
|
||||||
|
|
||||||
U64 nodes = 0;
|
U64 nodes = 0;
|
||||||
U64 nodesToGo = 0;
|
U64 nodesToGo = 0;
|
||||||
int timeToSearch = 0;
|
int timeToSearch = 0;
|
||||||
@@ -194,25 +222,28 @@ public:
|
|||||||
std::array<U64, 4096> nodesTable{};
|
std::array<U64, 4096> nodesTable{};
|
||||||
|
|
||||||
int seldepth = 0;
|
int seldepth = 0;
|
||||||
|
int rootDepth = 0;
|
||||||
|
|
||||||
Move excluded = Move();
|
Move excluded = Move();
|
||||||
|
|
||||||
bool doingNullMove = false;
|
int minNmpPly = 0;
|
||||||
bool searchStopped = false;
|
|
||||||
|
|
||||||
PVLine pvLine;
|
PVLine pvLine;
|
||||||
History history;
|
History history;
|
||||||
ContHistory conthist;
|
ContHistory conthist;
|
||||||
CorrHist corrhist;
|
CorrHist corrhist;
|
||||||
|
CaptHistory capthist;
|
||||||
|
|
||||||
std::array<std::array<int, MAX_DEPTH>, 2> killerMoves{};
|
std::array<Move, MAX_DEPTH> killerMoves{};
|
||||||
|
|
||||||
std::vector<U64> positionHistory;
|
std::vector<U64> positionHistory;
|
||||||
|
|
||||||
TTable TT;
|
TTable* TT = &SharedTT;
|
||||||
|
|
||||||
std::array<Stack, MAX_DEPTH> ss{};
|
std::array<Stack, MAX_DEPTH> ss{};
|
||||||
|
|
||||||
|
ACC::AccStack accStack{MAX_DEPTH + 2};
|
||||||
|
|
||||||
Stopwatch sw;
|
Stopwatch sw;
|
||||||
|
|
||||||
SearchContext(){
|
SearchContext(){
|
||||||
@@ -220,7 +251,7 @@ public:
|
|||||||
history.Clear();
|
history.Clear();
|
||||||
conthist.Clear();
|
conthist.Clear();
|
||||||
corrhist.Clear();
|
corrhist.Clear();
|
||||||
TT.Clear();
|
capthist.Clear();
|
||||||
sw.Restart();
|
sw.Restart();
|
||||||
killerMoves = {};
|
killerMoves = {};
|
||||||
ss = {};
|
ss = {};
|
||||||
@@ -238,9 +269,7 @@ SearchResults SearchPosition(Board &board, SearchParams params, SearchContext* c
|
|||||||
|
|
||||||
bool IsDraw(Board &board, SearchContext* ctx);
|
bool IsDraw(Board &board, SearchContext* ctx);
|
||||||
|
|
||||||
void ListScores(Board &board, SearchContext* ctx);
|
void PrintSearchInfo(Board& board, SearchContext* ctx, SearchResults& results, int depth, int elapsed);
|
||||||
|
|
||||||
void PrintSearchInfo(SearchContext* ctx, SearchResults& results, int depth, int elapsed);
|
|
||||||
|
|
||||||
int MoveEstimatedValue(Board& board, Move& move);
|
int MoveEstimatedValue(Board& board, Move& move);
|
||||||
}
|
}
|
||||||
+41
-2
@@ -1,11 +1,50 @@
|
|||||||
#include "tt.h"
|
#include "tt.h"
|
||||||
|
|
||||||
void TTable::WriteEntry(U64 &hashKey, int depth, int score, int nodeType, Move bestMove) {
|
TTable SharedTT;
|
||||||
TTEntry *current = &table[hashKey % table.size()];
|
|
||||||
|
void TTable::WriteEntry(U64 &hashKey, int depth, int score, int nodeType, Move bestMove, bool ttpv) {
|
||||||
|
TTBucket *bucket = &table[hashKey % table.size()];
|
||||||
|
TTEntry *current = nullptr;
|
||||||
|
|
||||||
|
if (bucket->depthPreferred && bucket->depthPreferred.hashKey == hashKey) {
|
||||||
|
current = &bucket->depthPreferred;
|
||||||
|
} else if (bucket->alwaysReplace && bucket->alwaysReplace.hashKey == hashKey) {
|
||||||
|
current = &bucket->alwaysReplace;
|
||||||
|
} else if (!bucket->alwaysReplace) {
|
||||||
|
current = &bucket->alwaysReplace;
|
||||||
|
} else if (!bucket->depthPreferred) {
|
||||||
|
current = &bucket->depthPreferred;
|
||||||
|
} else if (ttpv) {
|
||||||
|
current = &bucket->depthPreferred;
|
||||||
|
} else {
|
||||||
|
const int depthPreferredValue = GetReplacementValue(bucket->depthPreferred);
|
||||||
|
const int alwaysReplaceValue = GetReplacementValue(bucket->alwaysReplace);
|
||||||
|
|
||||||
|
if (depthPreferredValue <= alwaysReplaceValue) {
|
||||||
|
current = &bucket->depthPreferred;
|
||||||
|
} else {
|
||||||
|
current = &bucket->alwaysReplace;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const bool samePosition = current->hashKey == hashKey;
|
||||||
|
const bool exactBound = nodeType == PV;
|
||||||
|
const bool entryFromCurrentAge = current->age == age;
|
||||||
|
const int writeDepth = depth + 4 + ttpv * 2;
|
||||||
|
|
||||||
|
if (samePosition && !exactBound && entryFromCurrentAge && writeDepth <= current->depth) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
current->hashKey = hashKey;
|
current->hashKey = hashKey;
|
||||||
current->nodeType = nodeType;
|
current->nodeType = nodeType;
|
||||||
current->score = score;
|
current->score = score;
|
||||||
current->depth = depth;
|
current->depth = depth;
|
||||||
|
|
||||||
|
if (bestMove || !samePosition) {
|
||||||
current->bestMove = bestMove;
|
current->bestMove = bestMove;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
current->age = age;
|
||||||
|
current->ttpv = ttpv;
|
||||||
|
}
|
||||||
+37
-14
@@ -26,6 +26,8 @@ public:
|
|||||||
Move bestMove = Move();
|
Move bestMove = Move();
|
||||||
uint8_t depth = 0;
|
uint8_t depth = 0;
|
||||||
uint8_t nodeType = 0;
|
uint8_t nodeType = 0;
|
||||||
|
uint8_t age = 0;
|
||||||
|
bool ttpv = false;
|
||||||
|
|
||||||
TTEntry() {}
|
TTEntry() {}
|
||||||
|
|
||||||
@@ -38,36 +40,57 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class TTBucket {
|
||||||
|
public:
|
||||||
|
TTEntry depthPreferred;
|
||||||
|
TTEntry alwaysReplace;
|
||||||
|
};
|
||||||
|
|
||||||
// 1024 MB
|
// 1024 MB
|
||||||
constexpr U64 maxHash = (1024 * 1000000) / sizeof(TTEntry);
|
constexpr U64 maxHash = (1024 * 1000000) / sizeof(TTBucket);
|
||||||
// 8 MB
|
// 8 MB
|
||||||
constexpr U64 defaultHash = (8 * 1000000) / sizeof(TTEntry);
|
constexpr U64 defaultHash = (8 * 1000000) / sizeof(TTBucket);
|
||||||
|
|
||||||
constexpr int16_t invalidEntry = 11111;
|
constexpr int16_t invalidEntry = 11111;
|
||||||
|
|
||||||
class TTable {
|
class TTable {
|
||||||
private:
|
private:
|
||||||
std::vector<TTEntry> table;
|
std::vector<TTBucket> table;
|
||||||
|
|
||||||
|
uint8_t age = 0;
|
||||||
|
|
||||||
|
int GetReplacementValue(const TTEntry& entry) const {
|
||||||
|
const int ageDelta = uint8_t(age - entry.age);
|
||||||
|
return entry.depth - ageDelta * 4 + entry.ttpv * 2;
|
||||||
|
}
|
||||||
public:
|
public:
|
||||||
TTable() : table(defaultHash) {};
|
TTable() : table(defaultHash) {};
|
||||||
|
|
||||||
void Resize(U64 size) {
|
void Resize(U64 size) {
|
||||||
table.resize(size);
|
table.assign(size, {});
|
||||||
}
|
}
|
||||||
|
|
||||||
void Clear() {
|
void Clear() {
|
||||||
U64 size = table.size();
|
std::fill(table.begin(), table.end(), TTBucket());
|
||||||
table.clear();
|
|
||||||
table.resize(size);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void PrefetchEntry(U64 &hashKey) {
|
void PrefetchEntry(U64 &hashKey) {
|
||||||
__builtin_prefetch(&table[hashKey % table.size()]);
|
__builtin_prefetch(&table[hashKey % table.size()]);
|
||||||
}
|
}
|
||||||
|
|
||||||
TTEntry GetRawEntry(U64 &hashKey) {
|
void IncreaseAge() {
|
||||||
TTEntry *current = &table[hashKey % table.size()];
|
age++;
|
||||||
return *current;
|
}
|
||||||
|
|
||||||
|
TTEntry GetEntry(U64 &hashKey) {
|
||||||
|
TTBucket *current = &table[hashKey % table.size()];
|
||||||
|
if (current->depthPreferred.hashKey == hashKey) {
|
||||||
|
return current->depthPreferred;
|
||||||
|
} else if (current->alwaysReplace.hashKey == hashKey) {
|
||||||
|
return current->alwaysReplace;
|
||||||
|
}
|
||||||
|
|
||||||
|
return TTEntry();
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns used space per mill
|
// returns used space per mill
|
||||||
@@ -75,13 +98,13 @@ public:
|
|||||||
int count = 0;
|
int count = 0;
|
||||||
|
|
||||||
for (int i = 0; i < 1000; i++) {
|
for (int i = 0; i < 1000; i++) {
|
||||||
if (table[i]) count++;
|
if (table[i].depthPreferred || table[i].alwaysReplace) count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
TTEntry ReadEntry(U64 &hashKey, int depth, int alpha, int beta);
|
void WriteEntry(U64 &hashKey, int depth, int score, int nodeType, Move bestMove, bool ttpv);
|
||||||
|
|
||||||
void WriteEntry(U64 &hashKey, int depth, int score, int nodeType, Move besteMove);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
extern TTable SharedTT;
|
||||||
+51
-29
@@ -1,38 +1,60 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include <algorithm>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
//#define TUNING
|
//#define TUNING
|
||||||
|
|
||||||
#define AUTO_STEP(min_val, max_val) ((max_val - min_val) / 20.0)
|
#define AUTO_STEP(min_val, max_val) ((max_val - min_val) / 20.0)
|
||||||
|
#define AUTO_R_END_INT(min_val, max_val) \
|
||||||
|
(0.002 / (std::min(0.5, static_cast<double>(AUTO_STEP(min_val, max_val))) / 0.5))
|
||||||
|
#define AUTO_R_END_FLOAT 0.002
|
||||||
|
|
||||||
#define TUNABLE_LIST \
|
#define TUNABLE_LIST \
|
||||||
X_DOUBLE(lmrBaseQuiet, 1.1, 0.5, 1.5) \
|
X_DOUBLE(lmrBaseQuiet, 1.1029723689888005, 0.6, 1.6) \
|
||||||
X_DOUBLE(lmrDivisorQuiet, 2.33, 1.5, 3.5) \
|
X_DOUBLE(lmrDivisorQuiet, 2.3063513397139945, 1.6, 3.6) \
|
||||||
X_DOUBLE(lmrBaseNoisy, 0.41, 0.5, 1.5) \
|
X_DOUBLE(lmrBaseNoisy, 0.451805556082857, 0.1, 1.1) \
|
||||||
X_DOUBLE(lmrDivisorNoisy, 3.20, 1.5, 3.5) \
|
X_DOUBLE(lmrDivisorNoisy, 3.1355330830404338, 2.0, 4.5) \
|
||||||
X_INT(lmrCutnode, 2048, 1024, 3072) \
|
X_INT(lmrCutnode, 2005, 1024, 3072) \
|
||||||
X_INT(lmrIsPV, 1024, 512, 2048) \
|
X_INT(lmrIsPV, 982, 256, 2048) \
|
||||||
X_INT(lmrHistoryDivisor, 8192, 6000, 10000) \
|
X_INT(lmrTTPV, 936, 256, 2048) \
|
||||||
X_INT(lmrImproving, 1024, 6000, 10000) \
|
X_INT(lmrHistoryDivisor, 8377, 4096, 16384) \
|
||||||
X_INT(lmrCorrplexity, 1024, 6000, 10000) \
|
X_INT(lmrImproving, 1131, 256, 2048) \
|
||||||
X_INT(rfpBase, 25, 50, 200) \
|
X_INT(lmrCorrplexity, 830, 256, 2048) \
|
||||||
X_INT(rfpMargin, 100, 50, 200) \
|
X_INT(lmrTTPVFailLow, 913, 256, 2048) \
|
||||||
X_INT(fpMargin, 100, 50, 200) \
|
X_INT(lmrDeeperBase, 46, 0, 96) \
|
||||||
X_INT(razoringScalar, 200, 50, 200) \
|
X_INT(rfpBase, 21, 0, 100) \
|
||||||
X_INT(doubleExtMargin, 30, 10, 60) \
|
X_INT(rfpMargin, 97, 50, 200) \
|
||||||
X_INT(seeQuietThreshold, -80, -150, -20) \
|
X_INT(fpMargin, 86, 50, 200) \
|
||||||
X_INT(seeNoisyThreshold, -30, -80, 20) \
|
X_INT(qsFPMargin, 100, 50, 200) \
|
||||||
X_INT(aspInitialDelta, 30, 20, 80) \
|
X_INT(probcutBetaMargin, 190, 100, 300) \
|
||||||
X_INT(nmpBetaMargin, 30, 20, 80) \
|
X_INT(razoringScalar, 195, 80, 320) \
|
||||||
X_INT(seePawnValue, 100, 80, 120) \
|
X_INT(doubleExtMargin, 32, 8, 64) \
|
||||||
X_INT(seeKnightValue, 300, 250, 350) \
|
X_INT(seeQuietThreshold, -81, -160, 0) \
|
||||||
X_INT(seeBishopValue, 350, 300, 400) \
|
X_INT(seeNoisyThreshold, -32, -120, 40) \
|
||||||
X_INT(seeRookValue, 500, 400, 600) \
|
X_INT(aspInitialDelta, 26, 8, 96) \
|
||||||
X_INT(seeQueenValue, 900, 750, 1100) \
|
X_INT(nmpBetaMargin, 25, 8, 96) \
|
||||||
X_INT(seeQsThreshold, 0, -50, 50) \
|
X_INT(seePawnValue, 98, 80, 140) \
|
||||||
X_INT(seeOrderingThreshold, -100, -120, -80) \
|
X_INT(seeKnightValue, 301, 240, 420) \
|
||||||
X_INT(historyBonusMultiplier, 300, 270, 330) \
|
X_INT(seeBishopValue, 346, 250, 450) \
|
||||||
X_INT(historyBonusSub, 250, 230, 280)
|
X_INT(seeRookValue, 507, 400, 700) \
|
||||||
|
X_INT(seeQueenValue, 924, 700, 1300) \
|
||||||
|
X_INT(seeQsThreshold, -13, -120, 120) \
|
||||||
|
X_INT(seeOrderingThreshold, -102, -200, 0) \
|
||||||
|
X_INT(ldseMargin, 26, 0, 96) \
|
||||||
|
X_INT(noisyScoreScale, 32, 0, 96) \
|
||||||
|
X_INT(historyBonusMultiplier, 306, 280, 320) \
|
||||||
|
X_INT(historyBonusSub, 252, 235, 265) \
|
||||||
|
X_INT(historyMalusMultiplier, 301, 280, 320) \
|
||||||
|
X_INT(historyMalusSub, 250, 235, 265) \
|
||||||
|
X_INT(contHistoryBonusMultiplier, 300, 280, 320) \
|
||||||
|
X_INT(contHistoryBonusSub, 252, 235, 265) \
|
||||||
|
X_INT(contHistoryMalusMultiplier, 300, 280, 320) \
|
||||||
|
X_INT(contHistoryMalusSub, 249, 235, 265) \
|
||||||
|
X_INT(captHistoryBonusMultiplier, 302, 280, 320) \
|
||||||
|
X_INT(captHistoryBonusSub, 249, 235, 265) \
|
||||||
|
X_INT(captHistoryMalusMultiplier, 300, 280, 320) \
|
||||||
|
X_INT(captHistoryMalusSub, 249, 235, 265) \
|
||||||
|
X_INT(fiftyMoveScaleBase, 200, 100, 400)
|
||||||
|
|
||||||
#ifdef TUNING
|
#ifdef TUNING
|
||||||
#define X_DOUBLE(name, default_val, min_val, max_val) \
|
#define X_DOUBLE(name, default_val, min_val, max_val) \
|
||||||
@@ -61,14 +83,14 @@ inline void PrintTunables() {
|
|||||||
<< ", " << min_val \
|
<< ", " << min_val \
|
||||||
<< ", " << max_val \
|
<< ", " << max_val \
|
||||||
<< ", " << name##_step \
|
<< ", " << name##_step \
|
||||||
<< ", 0.002" << std::endl;
|
<< ", " << AUTO_R_END_FLOAT << std::endl;
|
||||||
|
|
||||||
#define X_INT(name, default_val, min_val, max_val) \
|
#define X_INT(name, default_val, min_val, max_val) \
|
||||||
std::cout << #name << ", int, " << name \
|
std::cout << #name << ", int, " << name \
|
||||||
<< ", " << min_val \
|
<< ", " << min_val \
|
||||||
<< ", " << max_val \
|
<< ", " << max_val \
|
||||||
<< ", " << name##_step \
|
<< ", " << name##_step \
|
||||||
<< ", 0.002" << std::endl;
|
<< ", " << AUTO_R_END_INT(min_val, max_val) << std::endl;
|
||||||
|
|
||||||
TUNABLE_LIST
|
TUNABLE_LIST
|
||||||
|
|
||||||
|
|||||||
+121
-24
@@ -4,6 +4,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <algorithm>
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
#include "movegen.h"
|
#include "movegen.h"
|
||||||
#include "search.h"
|
#include "search.h"
|
||||||
@@ -20,9 +21,55 @@
|
|||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
constexpr size_t DefaultPositionHistorySize = 1000;
|
||||||
|
|
||||||
|
static void EnsurePositionHistory(SEARCH::SearchContext* ctx, int positionIndex) {
|
||||||
|
if (positionIndex >= static_cast<int>(ctx->positionHistory.size())) {
|
||||||
|
ctx->positionHistory.resize(positionIndex + 100);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ResetPositionHistory(SEARCH::SearchContext* ctx, const Board& board) {
|
||||||
|
const size_t historySize = std::max(DefaultPositionHistorySize, static_cast<size_t>(board.positionIndex + 100));
|
||||||
|
ctx->positionHistory.assign(historySize, 0);
|
||||||
|
ctx->positionHistory[board.positionIndex] = board.hashKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
static std::vector<std::thread> searchThreads;
|
||||||
|
#else
|
||||||
|
static std::vector<pthread_t> searchThreads;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static void JoinSearchThreads() {
|
||||||
|
#ifdef _WIN32
|
||||||
|
for (auto& thread : searchThreads) {
|
||||||
|
if (thread.joinable()) {
|
||||||
|
thread.join();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
for (pthread_t& thread : searchThreads) {
|
||||||
|
pthread_join(thread, nullptr);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
searchThreads.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void StopSearchThreads() {
|
||||||
|
searchStopped.store(true, std::memory_order_relaxed);
|
||||||
|
JoinSearchThreads();
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
static void ParsePosition(Board &board, std::string_view command, SEARCH::SearchContext* ctx) {
|
static void ParsePosition(Board &board, std::string_view command, SEARCH::SearchContext* ctx) {
|
||||||
if (command.find("startpos") != std::string::npos) {
|
if (command.find("startpos") != std::string::npos) {
|
||||||
board.SetByFen(StartingFen);
|
board.SetByFen(StartingFen);
|
||||||
|
ResetPositionHistory(ctx, board);
|
||||||
}
|
}
|
||||||
|
|
||||||
int fenIndex = command.find("fen");
|
int fenIndex = command.find("fen");
|
||||||
@@ -34,6 +81,8 @@ static void ParsePosition(Board &board, std::string_view command, SEARCH::Search
|
|||||||
} else {
|
} else {
|
||||||
board.SetByFen(command.substr(fenIndex + 4, command.length() - (fenIndex + 3)));
|
board.SetByFen(command.substr(fenIndex + 4, command.length() - (fenIndex + 3)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ResetPositionHistory(ctx, board);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (movesIndex != std::string::npos) {
|
if (movesIndex != std::string::npos) {
|
||||||
@@ -41,6 +90,7 @@ static void ParsePosition(Board &board, std::string_view command, SEARCH::Search
|
|||||||
|
|
||||||
for (std::string_view move : moves) {
|
for (std::string_view move : moves) {
|
||||||
board.MakeMove(UTILS::parseMove(board, move));
|
board.MakeMove(UTILS::parseMove(board, move));
|
||||||
|
EnsurePositionHistory(ctx, board.positionIndex);
|
||||||
ctx->positionHistory[board.positionIndex] = board.hashKey;
|
ctx->positionHistory[board.positionIndex] = board.hashKey;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -89,53 +139,80 @@ static double ReadParam(const std::string& param, const std::string &command) {
|
|||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
// Windows implementation using std::thread
|
// Windows implementation using std::thread
|
||||||
template <SEARCH::searchMode mode>
|
template <SEARCH::searchMode mode>
|
||||||
static void ThreadFunc(Board* board, SearchParams params, SEARCH::SearchContext* ctx) {
|
static void ThreadFunc(Board board, SearchParams params, SEARCH::SearchContext* ctx) {
|
||||||
SEARCH::SearchPosition<mode>(*board, params, ctx);
|
SEARCH::SearchPosition<mode>(board, params, ctx);
|
||||||
|
delete ctx;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void StartSearchThread(Board& board, SearchParams params, SEARCH::SearchContext* ctx) {
|
static void StartSearchThread(Board& board, SearchParams params, SEARCH::SearchContext* ctx, int id) {
|
||||||
std::thread searchThread;
|
SEARCH::SearchContext* ctxCopy = new SEARCH::SearchContext(*ctx);
|
||||||
|
if (id == 0)
|
||||||
|
ctxCopy->doPrint = true;
|
||||||
|
|
||||||
if (params.nodes) {
|
if (params.nodes) {
|
||||||
searchThread = std::thread(ThreadFunc<SEARCH::nodesMode>, &board, params, ctx);
|
searchThreads.emplace_back(ThreadFunc<SEARCH::nodesMode>, board, params, ctxCopy);
|
||||||
} else {
|
} else {
|
||||||
searchThread = std::thread(ThreadFunc<SEARCH::normal>, &board, params, ctx);
|
searchThreads.emplace_back(ThreadFunc<SEARCH::normal>, board, params, ctxCopy);
|
||||||
}
|
}
|
||||||
|
|
||||||
searchThread.detach();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
// Unix/Linux implementation using pthread
|
// Unix/Linux implementation using pthread
|
||||||
template <SEARCH::searchMode mode>
|
template <SEARCH::searchMode mode>
|
||||||
static void* ThreadFunc(void* arg) {
|
static void* ThreadFunc(void* arg) {
|
||||||
auto* tup = static_cast<std::tuple<Board*, SearchParams, SEARCH::SearchContext*>*>(arg);
|
auto* tup = static_cast<std::tuple<Board, SearchParams, SEARCH::SearchContext*>*>(arg);
|
||||||
SEARCH::SearchPosition<mode>(*std::get<0>(*tup), std::get<1>(*tup), std::get<2>(*tup));
|
SEARCH::SearchContext* ctx = std::get<2>(*tup);
|
||||||
|
SEARCH::SearchPosition<mode>(std::get<0>(*tup), std::get<1>(*tup), ctx);
|
||||||
|
delete ctx;
|
||||||
delete tup;
|
delete tup;
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void StartSearchThread(Board& board, SearchParams params, SEARCH::SearchContext* ctx) {
|
static void StartSearchThread(Board& board, SearchParams params, SEARCH::SearchContext* ctx, int id) {
|
||||||
pthread_attr_t attr;
|
pthread_attr_t attr;
|
||||||
pthread_attr_init(&attr);
|
pthread_attr_init(&attr);
|
||||||
pthread_attr_setstacksize(&attr, 8 * 1024 * 1024); // 8 MB stack
|
pthread_attr_setstacksize(&attr, 8 * 1024 * 1024); // 8 MB stack
|
||||||
|
|
||||||
|
SEARCH::SearchContext* ctxCopy = new SEARCH::SearchContext(*ctx);
|
||||||
|
if (id == 0)
|
||||||
|
ctxCopy->doPrint = true;
|
||||||
|
|
||||||
pthread_t thread;
|
pthread_t thread;
|
||||||
|
int rc = 0;
|
||||||
|
|
||||||
if (params.nodes) {
|
if (params.nodes) {
|
||||||
auto* arg = new std::tuple<Board*, SearchParams, SEARCH::SearchContext*>(&board, params, ctx);
|
auto* arg = new std::tuple<Board, SearchParams, SEARCH::SearchContext*>(board, params, ctxCopy);
|
||||||
pthread_create(&thread, &attr, ThreadFunc<SEARCH::nodesMode>, arg);
|
rc = pthread_create(&thread, &attr, ThreadFunc<SEARCH::nodesMode>, arg);
|
||||||
|
if (rc != 0) {
|
||||||
|
delete std::get<2>(*arg);
|
||||||
|
delete arg;
|
||||||
|
ctxCopy = nullptr;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
auto* arg = new std::tuple<Board*, SearchParams, SEARCH::SearchContext*>(&board, params, ctx);
|
auto* arg = new std::tuple<Board, SearchParams, SEARCH::SearchContext*>(board, params, ctxCopy);
|
||||||
pthread_create(&thread, &attr, ThreadFunc<SEARCH::normal>, arg);
|
rc = pthread_create(&thread, &attr, ThreadFunc<SEARCH::normal>, arg);
|
||||||
|
if (rc != 0) {
|
||||||
|
delete std::get<2>(*arg);
|
||||||
|
delete arg;
|
||||||
|
ctxCopy = nullptr;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pthread_attr_destroy(&attr);
|
pthread_attr_destroy(&attr);
|
||||||
pthread_detach(thread);
|
|
||||||
|
if (rc == 0) {
|
||||||
|
searchThreads.push_back(thread);
|
||||||
|
} else {
|
||||||
|
delete ctxCopy;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static void ParseGo(Board &board, std::string &command, SEARCH::SearchContext* ctx) {
|
static void ParseGo(Board &board, std::string &command, SEARCH::SearchContext* ctx) {
|
||||||
|
StopSearchThreads();
|
||||||
|
searchStopped.store(false, std::memory_order_relaxed);
|
||||||
|
ctx->TT->IncreaseAge();
|
||||||
|
|
||||||
SearchParams params;
|
SearchParams params;
|
||||||
|
|
||||||
params.wtime = ReadParam("wtime", command);
|
params.wtime = ReadParam("wtime", command);
|
||||||
@@ -153,13 +230,29 @@ static void ParseGo(Board &board, std::string &command, SEARCH::SearchContext* c
|
|||||||
params.btime = 99999999;
|
params.btime = 99999999;
|
||||||
}
|
}
|
||||||
|
|
||||||
StartSearchThread(board, params, ctx);
|
searchThreads.reserve(threads);
|
||||||
|
for (int i = 0; i < threads; i++) {
|
||||||
|
StartSearchThread(board, params, ctx, i);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void SetOption(std::string& command, SEARCH::SearchContext* ctx) {
|
static void SetOption(std::string& command, SEARCH::SearchContext* ctx) {
|
||||||
|
StopSearchThreads();
|
||||||
|
|
||||||
if (command.find("Hash") != std::string::npos) {
|
if (command.find("Hash") != std::string::npos) {
|
||||||
U64 hashSize = (ReadParam("value", command) * 1000000ULL) / sizeof(TTEntry);
|
U64 hashSize = (ReadParam("value", command) * 1000000ULL) / sizeof(TTEntry);
|
||||||
ctx->TT.Resize(hashSize);
|
ctx->TT->Resize(hashSize);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (command.find("Threads") != std::string::npos) {
|
||||||
|
threads = ReadParam("value", command);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (command.find("UCI_ShowWDL") != std::string::npos) {
|
||||||
|
UCIShowWDL = command.find("value true") != std::string::npos
|
||||||
|
|| command.find("value 1") != std::string::npos;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -186,6 +279,7 @@ static void SetOption(std::string& command, SEARCH::SearchContext* ctx) {
|
|||||||
if (v < min_val) v = min_val; \
|
if (v < min_val) v = min_val; \
|
||||||
if (v > max_val) v = max_val; \
|
if (v > max_val) v = max_val; \
|
||||||
paramName = v; \
|
paramName = v; \
|
||||||
|
SEARCH::RefreshTunableCaches(); \
|
||||||
return; \
|
return; \
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -195,6 +289,7 @@ static void SetOption(std::string& command, SEARCH::SearchContext* ctx) {
|
|||||||
if (v < min_val) v = min_val; \
|
if (v < min_val) v = min_val; \
|
||||||
if (v > max_val) v = max_val; \
|
if (v > max_val) v = max_val; \
|
||||||
paramName = v; \
|
paramName = v; \
|
||||||
|
SEARCH::RefreshTunableCaches(); \
|
||||||
return; \
|
return; \
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -206,10 +301,11 @@ static void SetOption(std::string& command, SEARCH::SearchContext* ctx) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void PrintEngineInfo() {
|
static void PrintEngineInfo() {
|
||||||
std::cout << "id name Eleanor v3.0" << std::endl;
|
std::cout << "id name Eleanor v4.1" << std::endl;
|
||||||
std::cout << "id author rektdie" << std::endl;
|
std::cout << "id author rektdie" << std::endl;
|
||||||
std::cout << "option name Hash type spin default 8 min 1 max 1024" << std::endl;
|
std::cout << "option name Hash type spin default 8 min 1 max 1024" << std::endl;
|
||||||
std::cout << "option name Threads type spin default 1 min 1 max 1" << std::endl;
|
std::cout << "option name Threads type spin default 1 min 1 max 512" << std::endl;
|
||||||
|
std::cout << "option name UCI_ShowWDL type check default false" << std::endl;
|
||||||
|
|
||||||
#ifdef TUNING
|
#ifdef TUNING
|
||||||
PrintTunablesUCI();
|
PrintTunablesUCI();
|
||||||
@@ -242,16 +338,17 @@ void UCILoop(Board &board) {
|
|||||||
|
|
||||||
// parse UCI "ucinewgame" command
|
// parse UCI "ucinewgame" command
|
||||||
if (input.find("ucinewgame") != std::string::npos) {
|
if (input.find("ucinewgame") != std::string::npos) {
|
||||||
|
StopSearchThreads();
|
||||||
board.SetByFen(StartingFen);
|
board.SetByFen(StartingFen);
|
||||||
|
|
||||||
// Clearing
|
// Clearing
|
||||||
ctx->TT.Clear();
|
ctx->TT->Clear();
|
||||||
|
|
||||||
ctx->killerMoves = {};
|
ctx->killerMoves = {};
|
||||||
ctx->history.Clear();
|
ctx->history.Clear();
|
||||||
ctx->conthist.Clear();
|
ctx->conthist.Clear();
|
||||||
ctx->corrhist.Clear();
|
ctx->corrhist.Clear();
|
||||||
ctx->positionHistory = {};
|
ResetPositionHistory(ctx.get(), board);
|
||||||
ctx->ss = {};
|
ctx->ss = {};
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
@@ -265,14 +362,14 @@ void UCILoop(Board &board) {
|
|||||||
|
|
||||||
// parse UCI "quit" command
|
// parse UCI "quit" command
|
||||||
if (input.find("quit") != std::string::npos) {
|
if (input.find("quit") != std::string::npos) {
|
||||||
|
StopSearchThreads();
|
||||||
// stop the loop
|
// stop the loop
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// parse UCI "stop" command
|
// parse UCI "stop" command
|
||||||
if (input.find("stop") != std::string::npos) {
|
if (input.find("stop") != std::string::npos) {
|
||||||
// stop the loop
|
StopSearchThreads();
|
||||||
ctx->searchStopped = true;
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,12 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include <atomic>
|
||||||
#include "move.h"
|
#include "move.h"
|
||||||
#include "board.h"
|
#include "board.h"
|
||||||
|
|
||||||
inline bool UCIEnabled = false;
|
inline bool UCIEnabled = false;
|
||||||
|
inline bool UCIShowWDL = false;
|
||||||
|
inline int threads = 1;
|
||||||
|
inline std::atomic<bool> searchStopped = false;
|
||||||
|
|
||||||
class SearchParams {
|
class SearchParams {
|
||||||
public:
|
public:
|
||||||
@@ -13,6 +17,8 @@ public:
|
|||||||
int movesToGo = 0;
|
int movesToGo = 0;
|
||||||
int nodes = 0;
|
int nodes = 0;
|
||||||
|
|
||||||
|
int threads = 1;
|
||||||
|
|
||||||
SearchParams(){}
|
SearchParams(){}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,71 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
|
#include "board.h"
|
||||||
|
|
||||||
|
struct WinRateParams {
|
||||||
|
double a;
|
||||||
|
double b;
|
||||||
|
};
|
||||||
|
|
||||||
|
// From SF
|
||||||
|
inline WinRateParams winRateParams(const Board& board) {
|
||||||
|
int material = board.pieces[Pawn].PopCount() +
|
||||||
|
3 * board.pieces[Knight].PopCount() +
|
||||||
|
3 * board.pieces[Bishop].PopCount() +
|
||||||
|
5 * board.pieces[Rook].PopCount() +
|
||||||
|
9 * board.pieces[Queen].PopCount();
|
||||||
|
|
||||||
|
// The fitted model only uses data for material counts in [17, 78], and is anchored at count 58.
|
||||||
|
double m = std::clamp(material, 17, 78) / 58.0;
|
||||||
|
|
||||||
|
// Return a = p_a(material) and b = p_b(material), see github.com/official-stockfish/WDL_model
|
||||||
|
constexpr double as[] = {-977.81543545, 2519.99289616, -2279.71042020, 1197.60065700};
|
||||||
|
constexpr double bs[] = {-224.61509659, 557.03359920, -440.33347428, 226.68436468};
|
||||||
|
|
||||||
|
double a = (((as[0] * m + as[1]) * m + as[2]) * m) + as[3];
|
||||||
|
double b = (((bs[0] * m + bs[1]) * m + bs[2]) * m) + bs[3];
|
||||||
|
|
||||||
|
return {a, b};
|
||||||
|
}
|
||||||
|
|
||||||
|
// The win rate model is 1 / (1 + exp((a - eval) / b)), where a = p_a(material) and b = p_b(material).
|
||||||
|
// It fits the LTC fishtest statistics rather accurately.
|
||||||
|
inline int winRateModel(int v, const Board& board) {
|
||||||
|
auto [a, b] = winRateParams(board);
|
||||||
|
|
||||||
|
// Return the win rate in per mille units, rounded to the nearest integer.
|
||||||
|
return int(0.5 + 1000 / (1 + std::exp((a - static_cast<double>(v)) / b)));
|
||||||
|
}
|
||||||
|
|
||||||
|
struct WDLTriplet {
|
||||||
|
int wins;
|
||||||
|
int draws;
|
||||||
|
int losses;
|
||||||
|
};
|
||||||
|
|
||||||
|
inline WDLTriplet getWDL(int v, const Board& board) {
|
||||||
|
int wins = std::clamp(winRateModel(v, board), 0, 1000);
|
||||||
|
int losses = std::clamp(winRateModel(-v, board), 0, 1000);
|
||||||
|
int draws = 1000 - wins - losses;
|
||||||
|
|
||||||
|
if (draws < 0) {
|
||||||
|
if (wins >= losses) {
|
||||||
|
wins += draws;
|
||||||
|
} else {
|
||||||
|
losses += draws;
|
||||||
|
}
|
||||||
|
|
||||||
|
draws = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {wins, draws, losses};
|
||||||
|
}
|
||||||
|
|
||||||
|
inline int scaleEval(int eval, const Board& board) {
|
||||||
|
auto [a, b] = winRateParams(board);
|
||||||
|
|
||||||
|
return std::round(100 * eval / a);
|
||||||
|
}
|
||||||
Reference in new issue
Block a user