Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding comment to describe relative_rank. #1

Closed
wants to merge 1 commit into from
Closed

Adding comment to describe relative_rank. #1

wants to merge 1 commit into from

Conversation

zardav
Copy link

@zardav zardav commented Jun 18, 2014

That's can save time for someone who try to understand it.
No functional change.

@lucasart
Copy link

I am on holiday without access to my computer. Joona, Gary: can anyone test replacing the current code by a simple

return c == WHITE ? r : 7 - r;

and run speed measurement with perft. My guess is that despite the extra branch there will be no measurable difference (multiplication is costly if not by a power of 2).

Better have self-documenting code without comments, than clever code with comments, if possible.

@zardav
Copy link
Author

zardav commented Jun 19, 2014

I assumed the current code is 'Upgrade' of this.
Anyway, I think 7 - r should be RANK_8 - r.
Where can I find instructions to run speed measurement?

@lucasart lucasart closed this Jun 23, 2014
@zardav
Copy link
Author

zardav commented Jun 23, 2014

Why did you close this request without do anything about this?
If you not change the function, at least add comment....

@zamar zamar reopened this Jun 25, 2014
@zamar
Copy link

zamar commented Jun 25, 2014

I think we can merge this. Clearly the current code is cryptic and needs to be clarified by a comment.

@lucasart
Copy link

@zamar: Comments should explain what the code does, not how it does it, unless it is particularly subtle:

  • what: the name of the function is already documenting what it does
  • how: I think this code is not that complicated to deserve a comment on how it works. If we comment that we will need to be consistent and comment a lot of slightly subtle bit operations elsewhere in SF.

Anyway, that was my reasoning for closing the pull request. It sparked no interest form you and gary, so I assumed you didn't want to merge it.

@zamar
Copy link

zamar commented Jun 26, 2014

@lucas: Okay, your reasoning is fine with me.

@zamar zamar closed this Jun 26, 2014
@zardav
Copy link
Author

zardav commented Jun 26, 2014

I created this request, because when I tried to understand the "RelativeRank" mean, I didn't understand until I read the code and understand it. So, in my opinion, comment may help.

Hasimir pushed a commit to Hasimir/Stockfish that referenced this pull request Nov 14, 2014
@lantonov lantonov mentioned this pull request Feb 9, 2015
pb00068 referenced this pull request in pb00068/Stockfish Sep 24, 2016
vondele referenced this pull request in vondele/Stockfish Jul 26, 2017
Fixes a race when ucinewgame is issued 'after' a search, as seen for a thread-sanitized binary issuing :

```
go depth 5
[wait for bestmove ..]
ucinewgame
```
leading to

```
WARNING: ThreadSanitizer: data race (pid=5148)
  Write of size 4 at 0x7fbf5dcf6af8 by main thread:
    #0 __gnu_cxx::__enable_if<!std::__is_scalar<Move>::__value, void>::__type std::__fill_a<Move*, Move>(Move*, Move*, Move const&) <null> (stockfish+0x000000462884)
    #1 void std::fill<Move*, Move>(Move*, Move*, Move const&) /usr/include/c++/5/bits/stl_algobase.h:747 (stockfish+0x0000004618cc)
    #2 StatBoards<16, 64, Move>::fill(Move const&) src/movepick.h:36 (stockfish+0x00000046084f)
    #3 Search::clear() src/search.cpp:194 (stockfish+0x000000451732)
    #4 newgame src/uci.cpp:145 (stockfish+0x0000004738a0)
    #5 UCI::loop(int, char**) src/uci.cpp:200 (stockfish+0x000000473d7d)
    #6 main src/main.cpp:47 (stockfish+0x000000433322)

  Previous write of size 4 at 0x7fbf5dcf6af8 by thread T1:
    #0 update_stats src/search.cpp:1417 (stockfish+0x000000453ecb)
    #1 search<(<unnamed>::NodeType)0u> src/search.cpp:1114 (stockfish+0x00000045c4c6)
    #2 search<(<unnamed>::NodeType)1u> src/search.cpp:997 (stockfish+0x000000457658)
    #3 search<(<unnamed>::NodeType)1u> src/search.cpp:1019 (stockfish+0x000000457a50)
    #4 search<(<unnamed>::NodeType)1u> src/search.cpp:1019 (stockfish+0x000000457a50)
    #5 Thread::search() src/search.cpp:402 (stockfish+0x000000452e28)
    #6 MainThread::search() src/search.cpp:264 (stockfish+0x000000451c32)
    #7 Thread::idle_loop() src/thread.cpp:114 (stockfish+0x000000468c90)

```

The solution adopted is to split the uci commands in those that can be issued during search (quit, stop, ponderhit, isready, uci) and those that should not run during search (ucinewgame, setoption, .. ).  Waiting for search to finish was previously done only for a 'go'.

This fixes the above race.

There are two side effects, one possibly negative, depending on how a gui deals with uci (or a user at the cli).
- crashes are avoided (e.g. resizing hash during search).
- as earlier commands can block on the search to be completed, 'stop' can be non-effective. This behavior is already present on master :

```
go depth 20
go depth 5
stop
```

will wait for depth 20 to be completed before stopping depth 5. This behavior now shows for other commands as well. As such some gui testing of this patch would be useful.

No functional change.
vondele referenced this pull request in vondele/Stockfish Jul 27, 2017
limits.ponder was used as a signal between uci and search threads, but is not an atomic variable,
leading to the following race as flagged by a sanitized binary.

Expect input:
```
 spawn  ./stockfish
 send "uci\n"
 expect "uciok"
 send "setoption name Ponder value true\n"
 send "go wtime 4000 btime 4000\n"
 expect "bestmove"
 send "position startpos e2e4 d7d5\n"
 send "go wtime 4000 btime 4000 ponder\n"
 sleep 0.01
 send "ponderhit\n"
 expect "bestmove"
 send "quit\n"
 expect eof
```

Race:
```
WARNING: ThreadSanitizer: data race (pid=7191)
  Read of size 4 at 0x0000005c2260 by thread T1:
    #0 MainThread::check_time() src/search.cpp:1488 (stockfish+0x000000454471)
    #1 search<(<unnamed>::NodeType)0u> src/search.cpp:566 (stockfish+0x0000004594e0)
    #2 search<(<unnamed>::NodeType)0u> src/search.cpp:997 (stockfish+0x00000045bfb6)
    #3 search<(<unnamed>::NodeType)0u> src/search.cpp:1006 (stockfish+0x00000045c1a3)
    #4 search<(<unnamed>::NodeType)1u> src/search.cpp:997 (stockfish+0x000000457658)
    #5 Thread::search() src/search.cpp:402 (stockfish+0x000000452e28)
    #6 MainThread::search() src/search.cpp:264 (stockfish+0x000000451c32)
    #7 Thread::idle_loop() src/thread.cpp:114 (stockfish+0x000000468c90)

  Previous write of size 4 at 0x0000005c2260 by main thread:
    #0 UCI::loop(int, char**) src/uci.cpp:193 (stockfish+0x000000473c9b)
    #1 main src/main.cpp:47 (stockfish+0x000000433322)

  Location is global 'Search::Limits' of size 88 at 0x0000005c2220 (stockfish+0x0000005c2260)
```

The fix is to add an atomic bool to the threads structure to signal the ponder status, letting Search::Limits to reflect just what was passed to 'go'.

No functional change.
vondele referenced this pull request in vondele/Stockfish Aug 2, 2017
limits.ponder was used as a signal between uci and search threads, but is not an atomic variable,
leading to the following race as flagged by a sanitized binary.

Expect input:
```
 spawn  ./stockfish
 send "uci\n"
 expect "uciok"
 send "setoption name Ponder value true\n"
 send "go wtime 4000 btime 4000\n"
 expect "bestmove"
 send "position startpos e2e4 d7d5\n"
 send "go wtime 4000 btime 4000 ponder\n"
 sleep 0.01
 send "ponderhit\n"
 expect "bestmove"
 send "quit\n"
 expect eof
```

Race:
```
WARNING: ThreadSanitizer: data race (pid=7191)
  Read of size 4 at 0x0000005c2260 by thread T1:
    #0 MainThread::check_time() src/search.cpp:1488 (stockfish+0x000000454471)
    #1 search<(<unnamed>::NodeType)0u> src/search.cpp:566 (stockfish+0x0000004594e0)
    #2 search<(<unnamed>::NodeType)0u> src/search.cpp:997 (stockfish+0x00000045bfb6)
    #3 search<(<unnamed>::NodeType)0u> src/search.cpp:1006 (stockfish+0x00000045c1a3)
    #4 search<(<unnamed>::NodeType)1u> src/search.cpp:997 (stockfish+0x000000457658)
    #5 Thread::search() src/search.cpp:402 (stockfish+0x000000452e28)
    #6 MainThread::search() src/search.cpp:264 (stockfish+0x000000451c32)
    #7 Thread::idle_loop() src/thread.cpp:114 (stockfish+0x000000468c90)

  Previous write of size 4 at 0x0000005c2260 by main thread:
    #0 UCI::loop(int, char**) src/uci.cpp:193 (stockfish+0x000000473c9b)
    #1 main src/main.cpp:47 (stockfish+0x000000433322)

  Location is global 'Search::Limits' of size 88 at 0x0000005c2220 (stockfish+0x0000005c2260)
```

The fix is to add an atomic bool to the threads structure to signal the ponder status, letting Search::Limits to reflect just what was passed to 'go'.

No functional change.
protonspring referenced this pull request in protonspring/Stockfish Apr 11, 2018
protonspring referenced this pull request in protonspring/Stockfish Apr 22, 2018
protonspring referenced this pull request in ElbertoOne/Stockfish Jan 9, 2019
@FauziAkram FauziAkram mentioned this pull request Jun 4, 2019
mstembera pushed a commit to mstembera/Stockfish that referenced this pull request Jul 15, 2020
@gekkehenker gekkehenker mentioned this pull request Jul 18, 2020
@mattginsberg mattginsberg mentioned this pull request Jan 20, 2021
@gvreuls gvreuls mentioned this pull request Jun 3, 2021
locutus2 referenced this pull request in locutus2/Stockfish Jun 28, 2023
AYESDIE added a commit to AYESDIE/Stockfish that referenced this pull request Feb 8, 2025
AYESDIE added a commit to AYESDIE/Stockfish that referenced this pull request Feb 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants