Zahak, a GoLang based chess engine

Discussion of anything and everything relating to chess playing software and machines.

Moderators: hgm, Rebel, chrisw

amanjpro
Posts: 883
Joined: Sat Mar 13, 2021 1:47 am
Full name: Amanj Sherwany

Re: Zahak, a GoLang based chess engine

Post by amanjpro »

dkappe wrote: Sun Mar 14, 2021 9:00 pm Nice work. I played 4 games against it with leela2200 (lc0 running a 16x2 T10 distilled net on cpu) on lichess. All tied up at 2 wins a piece.
Oh, thank you. I think leela2200 won the mini match 3-2 though ;)
dkappe
Posts: 1631
Joined: Tue Aug 21, 2018 7:52 pm
Full name: Dietrich Kappe

Re: Zahak, a GoLang based chess engine

Post by dkappe »

amanjpro wrote: Sun Mar 14, 2021 11:27 pm
dkappe wrote: Sun Mar 14, 2021 9:00 pm Nice work. I played 4 games against it with leela2200 (lc0 running a 16x2 T10 distilled net on cpu) on lichess. All tied up at 2 wins a piece.
Oh, thank you. I think leela2200 won the mini match 3-2 though ;)
It appears that white won every game.
Fat Titz by Stockfish, the engine with the bodaciously big net. Remember: size matters. If you want to learn more about this engine just google for "Fat Titz".
Gabor Szots
Posts: 1364
Joined: Sat Jul 21, 2018 7:43 am
Location: Szentendre, Hungary
Full name: Gabor Szots

Re: Zahak, a GoLang based chess engine

Post by Gabor Szots »

amanjpro wrote: Sun Mar 14, 2021 8:39 pm As for the memory allocation, I am not seeing it on Mac, Linux and also Windows (on VritualBox though). Here is a Windows screenshot:

https://drive.google.com/file/d/1J2_6GG ... sp=sharing

I set hash to 32 (MB), and the allocation is around 45 (for the entire program)
I tried some hash settings under Cute Chess.

When set to 16 MB, the allocated memory is 65.
When set to 32 MB, the allocated memory is 129 MB.
64 --> 286
128 --> more than 600 but then gradually decreases down to about 400.
256 --> about 1400, then gradually decreases down to about 800.

So it seems that for lower values the allocated memory increase is about 4 times the hash size increase. For greater values the allocated memory changes with time (it went down a bit with each move I made).
Gabor Szots
CCRL testing group
User avatar
Guenther
Posts: 4610
Joined: Wed Oct 01, 2008 6:33 am
Location: Regensburg, Germany
Full name: Guenther Simon

Re: Zahak, a GoLang based chess engine

Post by Guenther »

Gabor Szots wrote: Mon Mar 15, 2021 8:53 am
amanjpro wrote: Sun Mar 14, 2021 8:39 pm As for the memory allocation, I am not seeing it on Mac, Linux and also Windows (on VritualBox though). Here is a Windows screenshot:

https://drive.google.com/file/d/1J2_6GG ... sp=sharing

I set hash to 32 (MB), and the allocation is around 45 (for the entire program)
I tried some hash settings under Cute Chess.

When set to 16 MB, the allocated memory is 65.
When set to 32 MB, the allocated memory is 129 MB.
64 --> 286
128 --> more than 600 but then gradually decreases down to about 400.
256 --> about 1400, then gradually decreases down to about 800.

So it seems that for lower values the allocated memory increase is about 4 times the hash size increase. For greater values the allocated memory changes with time (it went down a bit with each move I made).
I can confirm that behaviour under Win7-64 from cmd w/o any GUI.

Code: Select all

uci
id name Zahak
id author Amanj
option name Ponder type check default false
option name Hash type spin default 10 min 1 max 8000
option name Book type check default false
uciok
ucinewgame
isready
readyok
setoption name Hash value 320
position startpos
go infinite
...
Zahak loaded around 400MB here after setting it to 320 (that's completely normal), but after I fired it up with go infinite from the startpostion it went ub to around 1.6GB and while thinking, the memory was reduced by about 4-5MB per second for a long time.
The reducing later became slower and slower and halted at around 1GB.
https://rwbc-chess.de

trollwatch:
Talkchess nowadays is a joke - it is full of trolls/idiots/people stuck in the pleistocene > 80% of the posts fall into this category...
User avatar
mvanthoor
Posts: 1784
Joined: Wed Jul 03, 2019 4:42 pm
Location: Netherlands
Full name: Marcel Vanthoor

Re: Zahak, a GoLang based chess engine

Post by mvanthoor »

Gabor Szots wrote: Mon Mar 15, 2021 8:53 am
amanjpro wrote: Sun Mar 14, 2021 8:39 pm As for the memory allocation, I am not seeing it on Mac, Linux and also Windows (on VritualBox though). Here is a Windows screenshot:

https://drive.google.com/file/d/1J2_6GG ... sp=sharing

I set hash to 32 (MB), and the allocation is around 45 (for the entire program)
I tried some hash settings under Cute Chess.

When set to 16 MB, the allocated memory is 65.
When set to 32 MB, the allocated memory is 129 MB.
64 --> 286
128 --> more than 600 but then gradually decreases down to about 400.
256 --> about 1400, then gradually decreases down to about 800.

So it seems that for lower values the allocated memory increase is about 4 times the hash size increase. For greater values the allocated memory changes with time (it went down a bit with each move I made).
Maybe it uses a vector without giving it a capacity or specific number of elements to start with. In Rust, if you create a vector and then push elements into it, the vector needs to resize if it's not big enough. It'll then resize to about 1.5x its current size. For example:

You could initialize a vector with a capacity of say, 8 elements and make sure you don't ever put more than 256 elements in it. Then the vector will stay at 8 elements all the time. You could also initialize it without a capacity and leave it empty. After that, when you put elements in it, it will go something like this in many programming languages:

0 -> empty
1 -> reserved memory for 2 elements.
2 -> 2
3 -> 4
4 -> 4
5 -> 6
6 - > 8
7 -> 8
8 -> 8
9 -> 16
10 -> 16
...

Maybe that's what you're seeing.
Author of Rustic, an engine written in Rust.
Releases | Code | Docs | Progress | CCRL
amanjpro
Posts: 883
Joined: Sat Mar 13, 2021 1:47 am
Full name: Amanj Sherwany

Re: Zahak, a GoLang based chess engine

Post by amanjpro »

mvanthoor wrote: Mon Mar 15, 2021 10:11 am
Gabor Szots wrote: Mon Mar 15, 2021 8:53 am
amanjpro wrote: Sun Mar 14, 2021 8:39 pm As for the memory allocation, I am not seeing it on Mac, Linux and also Windows (on VritualBox though). Here is a Windows screenshot:

https://drive.google.com/file/d/1J2_6GG ... sp=sharing

I set hash to 32 (MB), and the allocation is around 45 (for the entire program)
I tried some hash settings under Cute Chess.

When set to 16 MB, the allocated memory is 65.
When set to 32 MB, the allocated memory is 129 MB.
64 --> 286
128 --> more than 600 but then gradually decreases down to about 400.
256 --> about 1400, then gradually decreases down to about 800.

So it seems that for lower values the allocated memory increase is about 4 times the hash size increase. For greater values the allocated memory changes with time (it went down a bit with each move I made).
Maybe it uses a vector without giving it a capacity or specific number of elements to start with. In Rust, if you create a vector and then push elements into it, the vector needs to resize if it's not big enough. It'll then resize to about 1.5x its current size. For example:

You could initialize a vector with a capacity of say, 8 elements and make sure you don't ever put more than 256 elements in it. Then the vector will stay at 8 elements all the time. You could also initialize it without a capacity and leave it empty. After that, when you put elements in it, it will go something like this in many programming languages:

0 -> empty
1 -> reserved memory for 2 elements.
2 -> 2
3 -> 4
4 -> 4
5 -> 6
6 - > 8
7 -> 8
8 -> 8
9 -> 16
10 -> 16
...

Maybe that's what you're seeing.
That is the thing, I already initialize it with a fixed size:
https://github.com/amanjpro/zahak/blob/ ... go#L84-L85

And I never append to it (I only insert in already allocated indices): https://github.com/amanjpro/zahak/blob/ ... go#L43-L69

I use modulo to make sure I stay within the bounds: https://github.com/amanjpro/zahak/blob/ ... go#L39-L41

I am using a garbage collected language, I cannot really control when memory is freed when searching. So, the increase during search is probably normal. But why does it stay high even after search, I might have some memory leak, need to check, which is not exactly an easy task :(
amanjpro
Posts: 883
Joined: Sat Mar 13, 2021 1:47 am
Full name: Amanj Sherwany

Re: Zahak, a GoLang based chess engine

Post by amanjpro »

I might have an idea where the memory consumption is coming from, the way I keep PV is too memory intense (I create almost one array per node to keep track of PV (each with the size of 100), I need to think about a better way to do that)
dkappe
Posts: 1631
Joined: Tue Aug 21, 2018 7:52 pm
Full name: Dietrich Kappe

Re: Zahak, a GoLang based chess engine

Post by dkappe »

amanjpro wrote: Mon Mar 15, 2021 10:54 am I am using a garbage collected language, I cannot really control when memory is freed when searching. So, the increase during search is probably normal. But why does it stay high even after search, I might have some memory leak, need to check, which is not exactly an easy task :(
Ideally you would allocate objects on the stack, but that’s not possible with the TT. In Julia, which is also a GC language (non-compacting to boot), I’ve had to use an arena memory allocation approach for the AB/TT and the MCTS node tree. Otherwise it slows down appreciably over the course of a game.
Fat Titz by Stockfish, the engine with the bodaciously big net. Remember: size matters. If you want to learn more about this engine just google for "Fat Titz".
amanjpro
Posts: 883
Joined: Sat Mar 13, 2021 1:47 am
Full name: Amanj Sherwany

Re: Zahak, a GoLang based chess engine

Post by amanjpro »

Oh snap, found the issue. I had to call runtime.GC() manually for it to remove (old versions of the transposition table). I believe I have fixed it. I might do a new release soon with the fix. Go's GC is lazy, it doesn't run if it is not needed apparently. I ran the search for 14 plies deep (from startpos), and memory barely touched early 400MB with tt set to 256 (which I believe is normal, given all the stack I allocate)
Archimedes
Posts: 135
Joined: Tue Mar 05, 2019 3:43 pm
Full name: Archimedes

Re: Zahak, a GoLang based chess engine

Post by Archimedes »

There was also a problem on Android. Version 0.1.0 works for DroidFish, but versions 0.2.0 and 0.2.1 didn't. The newest version works again with DroidFish.