SMP, game struct, thread struct and MakeMove

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

Michael Sherwin
Posts: 3196
Joined: Fri May 26, 2006 3:00 am
Location: WY, USA
Full name: Michael Sherwin

SMP, game struct, thread struct and MakeMove

Post by Michael Sherwin »

In my single thread engine I chose to have a single struct for both the game history and the search history. That seems wasteful for a multithreaded engine. So in my new multithreaded engine I have a game history and multiple search histories. So, do I also have to have two make move functions?
If you are on a sidewalk and the covid goes beep beep
Just step aside or you might have a bit of heat
Covid covid runs through the town all day
Can the people ever change their ways
Sherwin the covid's after you
Sherwin if it catches you you're through
Dann Corbit
Posts: 12541
Joined: Wed Mar 08, 2006 8:57 pm
Location: Redmond, WA USA

Re: SMP, game struct, thread struct and MakeMove

Post by Dann Corbit »

If your functions are re-entrant, you should not need more than one.
Taking ideas is not a vice, it is a virtue. We have another word for this. It is called learning.
But sharing ideas is an even greater virtue. We have another word for this. It is called teaching.
Michael Sherwin
Posts: 3196
Joined: Fri May 26, 2006 3:00 am
Location: WY, USA
Full name: Michael Sherwin

Re: SMP, game struct, thread struct and MakeMove

Post by Michael Sherwin »

Dann Corbit wrote: Fri Apr 19, 2019 2:49 am If your functions are re-entrant, you should not need more than one.
Sorry Dann, I do not know what a re-entrant function means.
If you are on a sidewalk and the covid goes beep beep
Just step aside or you might have a bit of heat
Covid covid runs through the town all day
Can the people ever change their ways
Sherwin the covid's after you
Sherwin if it catches you you're through
Dann Corbit
Posts: 12541
Joined: Wed Mar 08, 2006 8:57 pm
Location: Redmond, WA USA

Re: SMP, game struct, thread struct and MakeMove

Post by Dann Corbit »

Michael Sherwin wrote: Fri Apr 19, 2019 3:02 am
Dann Corbit wrote: Fri Apr 19, 2019 2:49 am If your functions are re-entrant, you should not need more than one.
Sorry Dann, I do not know what a re-entrant function means.
There are functions that carry state from the caller and rely on that behavior or that keep an internal object and return a pointer to it or things of that nature. An example is strtok() for parsing strings. The strtok() function is not re-entrant, but strtok_r() is re-entrant.

If all the variables in your function are auto variables (no static or global variables in use) and the function is stateless (does not need to memorize something from the previous call) then as many threads as you want can call it simultaneously without collision
Taking ideas is not a vice, it is a virtue. We have another word for this. It is called learning.
But sharing ideas is an even greater virtue. We have another word for this. It is called teaching.
Michael Sherwin
Posts: 3196
Joined: Fri May 26, 2006 3:00 am
Location: WY, USA
Full name: Michael Sherwin

Re: SMP, game struct, thread struct and MakeMove

Post by Michael Sherwin »

Dann Corbit wrote: Fri Apr 19, 2019 7:31 am
Michael Sherwin wrote: Fri Apr 19, 2019 3:02 am
Dann Corbit wrote: Fri Apr 19, 2019 2:49 am If your functions are re-entrant, you should not need more than one.
Sorry Dann, I do not know what a re-entrant function means.
There are functions that carry state from the caller and rely on that behavior or that keep an internal object and return a pointer to it or things of that nature. An example is strtok() for parsing strings. The strtok() function is not re-entrant, but strtok_r() is re-entrant.

If all the variables in your function are auto variables (no static or global variables in use) and the function is stateless (does not need to memorize something from the previous call) then as many threads as you want can call it simultaneously without collision
Thank you Dann, But that was not my question. Do I need a separate Make Move for the game history record? I understand about thread safety for the search histories. Or maybe I should only use search histories and start every search at game ply. It's okay I'll figure it out.
If you are on a sidewalk and the covid goes beep beep
Just step aside or you might have a bit of heat
Covid covid runs through the town all day
Can the people ever change their ways
Sherwin the covid's after you
Sherwin if it catches you you're through
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: SMP, game struct, thread struct and MakeMove

Post by Sven »

Michael Sherwin wrote: Fri Apr 19, 2019 3:27 pm Do I need a separate Make Move for the game history record?
In Jumbo the game history is simply part of the Board object (so "Board" is more like "Game with current position and history"). The small memory overhead is negligible but the gain of simplicity is significant in my view. Each search thread, as well as the game-playing part of the engine (ChessPlayer), has its own copy of the Board and therefore of the game history. And making a move during search is also a change of the game history - the reason is that I mainly use the game history for two purposes during search: 1) repetition check, 2) access to the last and possibly next-to-last move (e.g. for null move or counter move). So I do not need two different makeMove() functions. Whether you would need it might depend on your data structures.
Sven Schüle (engine author: Jumbo, KnockOut, Surprise)
Michael Sherwin
Posts: 3196
Joined: Fri May 26, 2006 3:00 am
Location: WY, USA
Full name: Michael Sherwin

Re: SMP, game struct, thread struct and MakeMove

Post by Michael Sherwin »

Sven wrote: Sat Apr 20, 2019 10:37 pm
Michael Sherwin wrote: Fri Apr 19, 2019 3:27 pm Do I need a separate Make Move for the game history record?
In Jumbo the game history is simply part of the Board object (so "Board" is more like "Game with current position and history"). The small memory overhead is negligible but the gain of simplicity is significant in my view. Each search thread, as well as the game-playing part of the engine (ChessPlayer), has its own copy of the Board and therefore of the game history. And making a move during search is also a change of the game history - the reason is that I mainly use the game history for two purposes during search: 1) repetition check, 2) access to the last and possibly next-to-last move (e.g. for null move or counter move). So I do not need two different makeMove() functions. Whether you would need it might depend on your data structures.
Thanks Sven! You do what I do in RomiChess for just one thread. So if you say that the additional memory to include the game history in every thread is negligible then I'll do the same.
If you are on a sidewalk and the covid goes beep beep
Just step aside or you might have a bit of heat
Covid covid runs through the town all day
Can the people ever change their ways
Sherwin the covid's after you
Sherwin if it catches you you're through
Michael Sherwin
Posts: 3196
Joined: Fri May 26, 2006 3:00 am
Location: WY, USA
Full name: Michael Sherwin

Re: SMP, game struct, thread struct and MakeMove

Post by Michael Sherwin »

Turns out that I accidently did it another way. From my move list array, just for debugging, I made the move directly from there and unmade the move from there. So in the search no moves are stored in history. I forgot I had done it that way so my entered moves from the command line would not work. I had to make a game history first. And it seems to be working just fine. Sometimes it pays to have a terrible memory, lol.
If you are on a sidewalk and the covid goes beep beep
Just step aside or you might have a bit of heat
Covid covid runs through the town all day
Can the people ever change their ways
Sherwin the covid's after you
Sherwin if it catches you you're through