stockfish
8 years agoClear TT before running 'bench'
Marco Costalba [Thu, 26 Apr 2012 18:01:13 +0000 (19:01 +0100)]
Clear TT before running 'bench'

Now that we can call bench multiple times
from command prompt we need to ensure searched
nodes remain constant across different runs.

Spotted by B├ílint Pfliegel.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoRetire "Active reparenting"
Marco Costalba [Sun, 22 Apr 2012 16:52:31 +0000 (17:52 +0100)]
Retire "Active reparenting"

After 6K games at 60" + 0.1 on QUAD with 4 threads
this implementation fails to show a measurable increase,
result is well within error bar.

Perhaps with 8 or more threads resut is better but we
don't have the hardware to test. So retire for now and
in case re-add in the future if it proves good on big
machines.

The only good news is that we don't have a regression and
implementation is stable and bug-free, so could be reused
somewhere in the future.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoFix endless reaparenting loop
Marco Costalba [Tue, 17 Apr 2012 07:11:34 +0000 (09:11 +0200)]
Fix endless reaparenting loop

The check for detecting when a split point has all the
slaves still running is done with:

   slavesMask == allSlavesMask

When a thread reparents, slavesMask is increased, then, if
the same thread finishes, because there are no more moves,
slavesMask returns to original value and the above condition
returns to be true. So that the just finished thread immediately
reparents again with the same split point, then starts and
then immediately exits in a tight loop that ends only when a
second slave finishes, so that slavesMask decrements and the
condition becomes false. This gives a spurious and anomaly
high number of faked reparents.

With this patch, that rewrites the logic to avoid this pitfall,
the reparenting success rate drops to a more realistical 5-10%
for 4 threads case.

As a side effect note that now there is no more the limit of
maxThreadsPerSplitPoint when reparenting.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoImproved cutoff check when reparenting
Marco Costalba [Mon, 16 Apr 2012 12:19:22 +0000 (14:19 +0200)]
Improved cutoff check when reparenting

Check for a cutoff occurred also high in
the tree and not only at current split
point.

This avoids some more wasted reparenting.

No functional chnage.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoUse more_than_one() instead of single_bit()
Marco Costalba [Sat, 14 Apr 2012 08:16:34 +0000 (09:16 +0100)]
Use more_than_one() instead of single_bit()

It is more correct given what the function does. In
particular single_bit() returns true also in case of
empty bitboards.

Of course also the usual renaming while there :-)

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoReparent to latest
Marco Costalba [Fri, 13 Apr 2012 05:32:55 +0000 (06:32 +0100)]
Reparent to latest

Instead of reparenting to oldest split point, try to reparent
to latest. The nice thing here is that we can use the YBWC
helpful master condition to allow the reparenting of a split
point master as long as is reparented to one of its slaves.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoDon't reparent if a cutoff is pending
Marco Costalba [Tue, 10 Apr 2012 18:48:57 +0000 (19:48 +0100)]
Don't reparent if a cutoff is pending

And update master->splitPointsCnt under lock
protection. Not stricly necessary because
single_bit() condition takes care of false
positives anyhow, but it is a bit tricky and
moving under lock is the most natural thing
to do to avoid races with "reparenting".

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoActive Reparenting
Marco Costalba [Tue, 10 Apr 2012 17:00:31 +0000 (18:00 +0100)]
Active Reparenting

In Young Brothers Wait Concept (YBWC) available slaves are
booked by the split point master, then start to search below
the assigned split point and, once finished, return in idle
state waiting to be booked by another master.

This patch introduces "Active Reparenting" so that when a
slave finishes its job on the assigned split point, instead
of passively waiting to be booked, searches a suitable active
split point and reprents itselfs to that split point. Then
immediately starts to search below the split point in exactly
the same way of the others split point's slaves. This reduces
to zero the time waiting in idle loop and should increase
scalability especially whit many (8 or more) cores.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoSync compute_xxx implementations
Marco Costalba [Mon, 9 Apr 2012 10:35:47 +0000 (11:35 +0100)]
Sync compute_xxx implementations

Also refactored Position::pos_is_ok() while there.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoMove Tempo to evaluation
Marco Costalba [Mon, 9 Apr 2012 09:34:17 +0000 (10:34 +0100)]
Move Tempo to evaluation

Apart from the semplification it is now more clear that
the actual Tempo added was half of the indicated score.
This is becuase at start compute_psq_score() added half
Tempo unit and in do_move() white/black were respectively
adding/subtracting one Tempo unit.

Now we have directly halved Tempo constant and everything
is more clear.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoMicro-optimize do_castle_move()
Marco Costalba [Mon, 9 Apr 2012 08:23:52 +0000 (09:23 +0100)]
Micro-optimize do_castle_move()

Use the same tables update trick used in do_move().

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoDisable book during analysis
Marco Costalba [Mon, 9 Apr 2012 07:09:02 +0000 (08:09 +0100)]
Disable book during analysis

It is still enabled during fixed limit search so to
use it during fixed depth/nodes/time matches.

Bug reported by Daylen.

No functional changes.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoIntroduce make_castle_right() helper
Marco Costalba [Sun, 8 Apr 2012 16:07:56 +0000 (17:07 +0100)]
Introduce make_castle_right() helper

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoShrink castlePath[] and castleRookSquare[] sizes
Marco Costalba [Sun, 8 Apr 2012 10:32:01 +0000 (11:32 +0100)]
Shrink castlePath[] and castleRookSquare[] sizes

Shrinking from [16] to [2][2] is able to speedup
perft of start position of almost 5% !

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoReduce stack usage
Marco Costalba [Sun, 8 Apr 2012 07:16:37 +0000 (08:16 +0100)]
Reduce stack usage

Shrink dimensions of the biggest stack consumers arrays.
In particular movesSearched[] can be safely shrinked
without any impact on strenght or risk of crashing.
Also MAX_PLY can be reverted to 100 with almost no impact
so to limit search recursion and hence stack allocation.

A different case is for MAX_MOVES (used by Movepicker's
moves[]), because we know that do exsist some artificial
position with about 220 legal moves, so in those cases SF
will crash. Anyhow these cases are never found in games.
An open risk remains perft, especially run above handcrafted
positions.

This patch originates from a report by Daylen that found
SF crashing on his Mac OS X 10.7.3 while in deep analysys
on the following position:

8/3Q1pk1/5p2/4r3/5K2/8/8/8 w - - 0 1

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoFix 'bench' for Chess960 case
Marco Costalba [Sat, 7 Apr 2012 13:06:42 +0000 (14:06 +0100)]
Fix 'bench' for Chess960 case

Now a fen file with Chess960 positions is
correctly parsed. But it is mandatory to set
"UCI_Chess960" option _before_ to call bench.

Note that this was not needed/possible before
adding the possibility to call 'bench' from
command prompt.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoUse bench to implement UI 'perft' command
Marco Costalba [Sat, 7 Apr 2012 12:23:43 +0000 (13:23 +0100)]
Use bench to implement UI 'perft' command

Now that we can call bench on current position
we can directly use it to perform our perft.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoTeach 'bench' to run current position
Marco Costalba [Sat, 7 Apr 2012 11:04:03 +0000 (12:04 +0100)]
Teach 'bench' to run current position

Now that we can call bench from command prompt
has a sense to teach bench to run the current
set position. To do this is enough to call bench
with 'current' as fen source parameter.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoDon't need to wait after a "ponderhit"
Marco Costalba [Sat, 7 Apr 2012 10:47:37 +0000 (11:47 +0100)]
Don't need to wait after a "ponderhit"

It is enough to wake up main thread. This is
a better fix than d033d5e06a604f.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoRevert "Penalty for undefended rook"
Marco Costalba [Sat, 7 Apr 2012 10:34:22 +0000 (11:34 +0100)]
Revert "Penalty for undefended rook"

After extensive test Gary says:

"So, after 16k games at 10"+1" on an i7, the undefended rook test
looks to be not good (albeit by a very small margin).
3063 - 3093 - 9844 (-1).

I doubt that is causing the regression, but even so, it looks like
it's not worth keeping, and we can go back to the simpler undefended
minors check."

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoReplace Position::copy()
Marco Costalba [Fri, 6 Apr 2012 18:13:48 +0000 (19:13 +0100)]
Replace Position::copy()

With assignment operator. And fix Position::flip().

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoRevert thread_local stuff
Marco Costalba [Fri, 6 Apr 2012 17:36:46 +0000 (18:36 +0100)]
Revert thread_local stuff

Unfortunatly accessing thread local variable
is much slower than object data (see previous
patch log msg), so we have to revert to old code
to avoid speed regression.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoUse thread_local compiler specifics
Marco Costalba [Fri, 6 Apr 2012 16:01:41 +0000 (17:01 +0100)]
Use thread_local compiler specifics

Much faster then pthread_getspecific() but still a
speed regression against the original code.

Following are the nps on a bench:

Position
454165
454838
455433

tls
441046
442767
442767

ms (Win)
450521
447510
451105

ms (pthread)
422115
422115
424276

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoFix a (theoretical) race leading to a crash
Marco Costalba [Fri, 6 Apr 2012 14:22:02 +0000 (15:22 +0100)]
Fix a (theoretical) race leading to a crash

After we release the SplitPoint lock the master, suppose
is main thread, can safely return and if a "quit" command
is pending, main thread exits and associated Thread object
is freed. So when we access master->is_searching a crash
occurs.

I have never found such a race that is of course very rare
becuase assumes that from lock releasing we go to sleep for
a time long enough for the main thread to end the search and
return. But you can never know, and anyhow a race is a race.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoTeach UI thread to use main thread resources
Marco Costalba [Fri, 6 Apr 2012 13:17:08 +0000 (14:17 +0100)]
Teach UI thread to use main thread resources

So to avoid a crash when setting the moves in
UCI "position startpos moves ...." command.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoDon't store Thread info in Position
Marco Costalba [Fri, 6 Apr 2012 12:46:53 +0000 (13:46 +0100)]
Don't store Thread info in Position

But use the newly introduced local storage
for this. A good code semplification and also
the correct way to go.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoIntroduce thread local storage
Marco Costalba [Fri, 6 Apr 2012 10:39:07 +0000 (12:39 +0200)]
Introduce thread local storage

Use thread local storage to store a pointer to the thread we
are running on. This will allow to remove thread info from
Position class.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoRewrite pop_1st_bit() to be endian independent
Marco Costalba [Fri, 6 Apr 2012 09:37:07 +0000 (11:37 +0200)]
Rewrite pop_1st_bit() to be endian independent

With this change sources are fully endianess
independent, so we can simplify the Makefile.

Somewhat surprisingly we don't have any speed
regression !

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoUse a Thread instead of an array index
Marco Costalba [Wed, 4 Apr 2012 06:54:02 +0000 (07:54 +0100)]
Use a Thread instead of an array index

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoBig Position renaming
Marco Costalba [Mon, 2 Apr 2012 17:21:17 +0000 (18:21 +0100)]
Big Position renaming

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoMicro-optimize last_1() for 32bits
Marco Costalba [Wed, 4 Apr 2012 05:52:07 +0000 (06:52 +0100)]
Micro-optimize last_1() for 32bits

Verified assembly it is a bit simpler.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoFix bench with fen files regression
Marco Costalba [Mon, 2 Apr 2012 17:38:50 +0000 (18:38 +0100)]
Fix bench with fen files regression

Erroneusly adds default positions to the fens
loaded from external file.

Bug introduced in adb71b8096436a

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoSimplify Endgames::probe()
Marco Costalba [Mon, 2 Apr 2012 06:41:26 +0000 (07:41 +0100)]
Simplify Endgames::probe()

With this API change we simplify both function and caller site.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoUse polymorphism to resolve map() overloading
Marco Costalba [Sun, 1 Apr 2012 14:12:39 +0000 (15:12 +0100)]
Use polymorphism to resolve map() overloading

The 2 overload functions map() accept a pointer to
EndgameBase<Value> or a pointer to EndgameBase<ScaleFactor>.

Because Endgame<E> is derived from one of them we can
directly use a pointer to this class to resolve the
overload as is needed in Endgames::add().

Also made class Endgames fully parametrized and no more
hardcoded to the types (Value or ScaleFactor) of endgames
stored.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoUse a std::vector to store searchMoves
Marco Costalba [Sun, 1 Apr 2012 11:26:14 +0000 (12:26 +0100)]
Use a std::vector to store searchMoves

A std::set (that is a rb_tree) seems really
overkill to store at most a handful of moves
and nothing in the common case.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoRetire platform specifics include in misc.cpp
Marco Costalba [Sun, 1 Apr 2012 11:00:15 +0000 (12:00 +0100)]
Retire platform specifics include in misc.cpp

Now that platform.h is included in types.h we
don't need this stuff anymore.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoBetter document square flipping helpers
Marco Costalba [Sun, 1 Apr 2012 10:30:54 +0000 (11:30 +0100)]
Better document square flipping helpers

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoIntroduce Bitboards namespace
Marco Costalba [Sun, 1 Apr 2012 09:46:38 +0000 (10:46 +0100)]
Introduce Bitboards namespace

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoProcess 'bench' also from SF prompt
Marco Costalba [Sun, 1 Apr 2012 08:25:07 +0000 (09:25 +0100)]
Process 'bench' also from SF prompt

It is possible to start with 'stockfish', then from
command prompt type 'bench' and SF will do what you expect.
Old behaviour is anyhow preserved. As a bonus we can now
start from command line any UCI command understood by
Stockfish. The difference is that after execution of a
command from arguments SF quits, while at the end of the
same command from prompt SF stays in UCI loop.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoUse std::vector to implement HashTable
Marco Costalba [Sat, 31 Mar 2012 11:15:57 +0000 (12:15 +0100)]
Use std::vector to implement HashTable

Allows some code semplification and avoids directly
allocation and managing heap memory.

Also the usual renaming while there.

No functional change and no speed regression.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoRename Materials and Pawns hash stuff
Marco Costalba [Sat, 31 Mar 2012 08:43:16 +0000 (09:43 +0100)]
Rename Materials and Pawns hash stuff

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoComplete the renaming in Search::LimitsType
Marco Costalba [Sat, 31 Mar 2012 08:16:09 +0000 (09:16 +0100)]
Complete the renaming in Search::LimitsType

This completes the job started with revision
4124c94583c8f61.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoReset search time as early as possible
Marco Costalba [Sat, 31 Mar 2012 09:01:31 +0000 (10:01 +0100)]
Reset search time as early as possible

In particualr before to wake up main thread that
could take some random time. Until we don't reset
search time we are not able to correctly track
the elapsed search time and this can be dangerous
under extreme time pressure.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoRevert "Call wait_for_search_finished() only when quitting"
Marco Costalba [Fri, 30 Mar 2012 10:44:31 +0000 (12:44 +0200)]
Revert "Call wait_for_search_finished() only when quitting"

We need to wake up main thread if it is sleeping
waiting for stop or ponderhit, so we cannot skip
calling wait_for_search_finished().

Found by Othello1984.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoLast touches to pawns shelter code
Marco Costalba [Thu, 29 Mar 2012 05:04:44 +0000 (06:04 +0100)]
Last touches to pawns shelter code

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoFix an hang when max depth is reached
Marco Costalba [Thu, 29 Mar 2012 19:04:27 +0000 (20:04 +0100)]
Fix an hang when max depth is reached

In this case SF stop searching and goes sleeping
waiting for a stop / ponderhit before to return
best move. So when a "stop" arrives we need to wake
up the main thread again.

Another regression introduced by 3aa471f2a9cb,
hopefully the last one.

Thanks to Otello1984 to reporting this.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoRefactor pawns shelter and storm
Marco Costalba [Wed, 28 Mar 2012 10:44:41 +0000 (12:44 +0200)]
Refactor pawns shelter and storm

Renamed stuff and added comments. The aim is to make more
readable, at least by me ;-) , this newly added part of code.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoRestore MS1BTable[]
Marco Costalba [Wed, 28 Mar 2012 18:01:37 +0000 (19:01 +0100)]
Restore MS1BTable[]

Incredible typo from my side!

The 2 tables are completely different, one counts 1s the
other returns the msb position. Even more incredible
the 'stockfish bench' command returns the same number
of nodes!!!

Spotted by Justin Blanchard.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoSilently handle "ucinewgame" command
Marco Costalba [Wed, 28 Mar 2012 17:22:40 +0000 (18:22 +0100)]
Silently handle "ucinewgame" command

Avoid returning "Unknown command", it seems some
GUI are misguided by this.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoReplace MS1BTable[] with BitCount8Bit[]
Marco Costalba [Wed, 28 Mar 2012 09:03:06 +0000 (11:03 +0200)]
Replace MS1BTable[] with BitCount8Bit[]

We already have the necessary infrastructure
in place.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoMerge pull request #11 from glinscott/squash
Marco Costalba [Wed, 28 Mar 2012 05:40:17 +0000 (22:40 -0700)]
Merge pull request #11 from glinscott/squash

Add more detailed pawn shelter/storm evaluation

After 10670 games at 10"+0.05
Mod vs Orig 2277 - 1941 - 6452 ELO +11 !!!

The first real increase since 2.2.2, congratulations Gary !!!

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoSet do_sleep out of lock protection
Marco Costalba [Tue, 27 Mar 2012 17:47:41 +0000 (18:47 +0100)]
Set do_sleep out of lock protection

Fixes a not so rare crash (once every 100 games)
newly introduced. Unfortunatly I am still not
able to figure out why :-(

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoUse UCI names in Search::LimitsType
Marco Costalba [Tue, 27 Mar 2012 10:27:57 +0000 (12:27 +0200)]
Use UCI names in Search::LimitsType

There is no need to "invent" different names
from the original UCI parameters.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoMerge pull request #9 from glinscott/master
Marco Costalba [Mon, 26 Mar 2012 18:55:32 +0000 (11:55 -0700)]
Merge pull request #9 from glinscott/master

Penalty for undefended rook

Almost no change at longer TC, but perhaps there
is a tiny increase....

After 17522 games at 10"+0.05
Mod vs Orig 3064 - 2967 - 11491 ELO +2

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoSimplify start_searching() signature
Marco Costalba [Mon, 26 Mar 2012 17:56:48 +0000 (18:56 +0100)]
Simplify start_searching() signature

Retire the "sync" behaviour that now is up to
the caller to honour.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoCall wait_for_search_finished() only when quitting
Marco Costalba [Mon, 26 Mar 2012 17:35:23 +0000 (18:35 +0100)]
Call wait_for_search_finished() only when quitting

When quitting we should avoid RootPosition to be
destroyed while threads are still running, leading
to a crash. In case of a "stop" or "ponderhit"
command there is no need for the UI thread to wait.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoIntroduce and use wait_for_search_finished()
Marco Costalba [Mon, 26 Mar 2012 15:19:37 +0000 (17:19 +0200)]
Introduce and use wait_for_search_finished()

Helper function that allows us to simplify
the code.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoAdd more detailed pawn shelter/storm evaluation
Gary Linscott [Mon, 26 Mar 2012 11:52:10 +0000 (07:52 -0400)]
Add more detailed pawn shelter/storm evaluation

8 years agoMove ThreadsManager::exit() to d'tor
Marco Costalba [Mon, 26 Mar 2012 06:58:10 +0000 (07:58 +0100)]
Move ThreadsManager::exit() to d'tor

And add final touches to this long patch series.

All the series has been verified against regression with
20K games at fast TC.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoFix compile error with gcc
Marco Costalba [Sun, 25 Mar 2012 22:08:35 +0000 (23:08 +0100)]
Fix compile error with gcc

We have a clash with start_fn defined both as a
Thread memeber and as a function pointer type in
pthread_create().

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoReset Thread::maxPly before a new search
Marco Costalba [Sun, 25 Mar 2012 15:44:55 +0000 (16:44 +0100)]
Reset Thread::maxPly before a new search

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoFix race in ThreadsManager::sleep()
Marco Costalba [Sun, 25 Mar 2012 15:31:50 +0000 (16:31 +0100)]
Fix race in ThreadsManager::sleep()

We cannot set do_sleep flag of main thread before
"bestmove" is sent to GUI, otherwise GUI could send
immediately the next "go" command that triggers
start_thinking() and because do_sleep is set UI
thread resets the flag to launch a new search. But
when shortly after main thread returns to main_loop()
flag is incorrectly reset and main thread goes to sleep
hanging the engine.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoMerge remote-tracking branch 'upstream/master'
Gary Linscott [Sun, 25 Mar 2012 14:18:29 +0000 (10:18 -0400)]
Merge remote-tracking branch 'upstream/master'

8 years agoTry to mimic std::thread API
Marco Costalba [Sun, 25 Mar 2012 11:01:56 +0000 (12:01 +0100)]
Try to mimic std::thread API

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoUse std::vector<Thread*> to store threads
Marco Costalba [Sat, 24 Mar 2012 20:36:33 +0000 (21:36 +0100)]
Use std::vector<Thread*> to store threads

We store pointers instead of Thread objects because
Thread is not copy-constructible nor copy-assignable
and default ones are not suitable. So we cannot store
directly in a std::vector.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoRefactor Thread class
Marco Costalba [Sat, 24 Mar 2012 19:10:13 +0000 (20:10 +0100)]
Refactor Thread class

Associate platform OS thread to the Thread class instead of
creating it from ThreadsManager.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoRefactor ThreadsManager::set_size() functionality
Marco Costalba [Sat, 24 Mar 2012 18:29:12 +0000 (19:29 +0100)]
Refactor ThreadsManager::set_size() functionality

Split the data allocation, now done (mostly once)
in read_uci_options(), from the wake up and sleeping
of the slave threads upon entering/exiting the search.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoRevert "Don't sync with C library I/O buffers"
Marco Costalba [Sun, 25 Mar 2012 09:23:16 +0000 (10:23 +0100)]
Revert "Don't sync with C library I/O buffers"

It seems is the cause of strange and rare hangs
reported by some users where Stockfish stops
responding to GUI. It is not clear why but for
the moment revert the patch.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoDon't ceil cpu_count()
Marco Costalba [Sun, 25 Mar 2012 08:54:20 +0000 (09:54 +0100)]
Don't ceil cpu_count()

It is already done at calling site where it is
more appropiate.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoFix a MSVC warning
Marco Costalba [Sat, 24 Mar 2012 09:14:21 +0000 (10:14 +0100)]
Fix a MSVC warning

Not correct warning about use of an uninitialized
variable. The warning is not correct becuase we can
never reach the warned code when in SpNode, anyhow
the fix is simple.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoRename lock.h to platform.h
Marco Costalba [Sat, 24 Mar 2012 08:52:41 +0000 (09:52 +0100)]
Rename lock.h to platform.h

And move some more platform specific code there.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoRemove last platform specific code form thread.cpp
Marco Costalba [Fri, 23 Mar 2012 12:12:26 +0000 (13:12 +0100)]
Remove last platform specific code form thread.cpp

A somewhat tricky function pointer cast allows us
to move the platform specifics to lock.h, the cast
is tricky because return type is not the same of the
casted function in Linux (for Windows return type is
a DWORD that is a long) but this should not be a
problem as long as the size is the same;

From: http://stackoverflow.com/questions/188839/function-pointer-cast-to-different-signature

"OpenSSL was only casting functions pointers to
other function types taking and returning the same
number of values of the same exact sizes, and this
(assuming you're not dealing with floating-point)
happens to be safe across all the platforms and
calling conventions I know of. However, anything
else is potentially unsafe."

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoMerge two loops in ThreadsManager::init()
Marco Costalba [Thu, 22 Mar 2012 21:39:10 +0000 (22:39 +0100)]
Merge two loops in ThreadsManager::init()

In analogy with ThreadsManager::exit()

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoUse a local copy of tte->value()
Marco Costalba [Tue, 20 Mar 2012 10:45:27 +0000 (11:45 +0100)]
Use a local copy of tte->value()

This should avoid some aliasing issues
with TT table access.

After 3913 games at 10"+0.05
Mod vs Orig 662 - 651 - 2600  ELO +0 (+- 6.4)

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoPenalty for undefended rook as well
Gary Linscott [Thu, 22 Mar 2012 11:33:47 +0000 (07:33 -0400)]
Penalty for undefended rook as well

8 years agoMerge pull request #8 from glinscott/master
mcostalba [Thu, 22 Mar 2012 06:39:53 +0000 (23:39 -0700)]
Merge pull request #8 from glinscott/master

Optimize undefended minor check. Little editing by
me, no change even at assembly level.

No regression after 8K games at fast TC on a 64bit CPU.

8 years agoOptimize undefended minor check.
Gary Linscott [Wed, 21 Mar 2012 12:19:21 +0000 (08:19 -0400)]
Optimize undefended minor check.

8 years agoPenalize undefended minors
Gary Linscott [Wed, 21 Mar 2012 06:26:46 +0000 (07:26 +0100)]
Penalize undefended minors

Even if not under attack. This seems to be good
especially on openings.

After 12112 games at 10"+0.05
Mod vs Orig 2175 - 1997 - 7940 ELO +5 (+- 3.7)

[Patch series from Gary, little edited by me]

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoFix Logger under MSVC iostream libraries
Marco Costalba [Tue, 20 Mar 2012 19:50:24 +0000 (20:50 +0100)]
Fix Logger under MSVC iostream libraries

We need splitted Tie classes because MSVC stream library
takes a lock on buffer both on reading and on writing and
this causes an hang because, while searching, the I/O
thread is locked on getline() and when main thread is
trying to std::cout() something it blocks on the same
lock waiting for I/O thread getting some input and
releasing the lock.

The solution is to use separated streambuf objects for
cin and cout.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoRemove cruft from Logger class
Marco Costalba [Tue, 20 Mar 2012 13:40:09 +0000 (14:40 +0100)]
Remove cruft from Logger class

A big code simplification and cruft removing, make
Logger class a singleton and fully self conteined.
Also add direction indicators (">>" and "<<") to
better differentiate input and output lines in the
log file.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoBetter on_change() argument name
Marco Costalba [Tue, 20 Mar 2012 06:09:58 +0000 (07:09 +0100)]
Better on_change() argument name

Using "o" as a parameter with the on_xxx(const UICOption& o)
functions is a bit dangerous because of confusion with "0".

Suggested by Rein Halbersma.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoAdd also logging of std::cin
Marco Costalba [Mon, 19 Mar 2012 20:36:25 +0000 (21:36 +0100)]
Add also logging of std::cin

Some trial was needed to find the correct recipe but now
we log both stdin and stdout to file "io_log.txt".

Link http://spec.winprog.org/streams/ was very useful
to understand the details of iostreams implementation.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoAdd (smart) logging facility
Marco Costalba [Sun, 18 Mar 2012 02:20:43 +0000 (03:20 +0100)]
Add (smart) logging facility

By means of "Use Debug Log" UCI option it is possible to toggle
the logging of std::cout to file "out.txt" while preserving
the usual output to stdout. There is zero overhead when logging
is disabled and we achieved this without changing a single line
of exsisting code, in particular we still use std::cout as usual.

The idea and part of the code comes from this article:
http://groups.google.com/group/comp.lang.c++/msg/1d941c0f26ea0d81

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoShow startup messages immediately
Marco Costalba [Sun, 18 Mar 2012 22:08:37 +0000 (23:08 +0100)]
Show startup messages immediately

In particular before initialization. So that SF
seems more snappy at startup.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoRevert to byTypeBB[0] storing occupied squares
Marco Costalba [Sun, 18 Mar 2012 10:33:54 +0000 (11:33 +0100)]
Revert to byTypeBB[0] storing occupied squares

As it was in Glaurung times. Also rearranged order
so that byTypeBB[0] is accessed before byTypeBB[x]
to be more cache friendly. It seems there is even
a small speedup.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoRename occupied_squares() to pieces()
Marco Costalba [Sun, 18 Mar 2012 10:10:12 +0000 (11:10 +0100)]
Rename occupied_squares() to pieces()

Also some microoptimizations, were there from ages
but hidden: the renaming suddendly made them visible!

This is a good example of how better naming lets you write
better code. Naming is really a kind of black art!

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoUCI buttons don't need a value
Marco Costalba [Sat, 17 Mar 2012 20:18:02 +0000 (21:18 +0100)]
UCI buttons don't need a value

Take advantage of this to further simplify the code.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoDon't use "OwnBook" by default
Marco Costalba [Sat, 17 Mar 2012 12:16:23 +0000 (13:16 +0100)]
Don't use "OwnBook" by default

Stick to UCI protocol that says:

* by default all the opening book handling is done by the GUI,
  but there is an option for the engine to use its own book
  ("OwnBook" option, see below)

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoRetire "ucinewgame" UCI option
Marco Costalba [Sat, 17 Mar 2012 10:24:19 +0000 (11:24 +0100)]
Retire "ucinewgame" UCI option

UCI protocol it is not clear about what the engine
should be supposed to do when "ucinewgame" is
received. Stockfish simply sets the position to
start FEN, but it is redundant becuase the GUI always
resends the position after "ucinewgame" command, so
it seems we can safely ignore that command.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoFix UCI 'button' options
Marco Costalba [Sat, 17 Mar 2012 09:29:33 +0000 (10:29 +0100)]
Fix UCI 'button' options

When a button fires UCIOption::operator=() is called and from
there the on_change() function. Now it happens that in case of
a button the on_change() function resets option's value to
"false" triggering again UCIOption::operator=() that calls again
on_change() and so on in an endless loop that is experienced
by the user as an application hang.

Rework the button logic to fix the issue and also be more clear
about how button works.

Reported by several people working with Scid and tracked down
to the "Clear Hash" UCI button by Steven Atkinson.

Bug recently introduced by 2ef5b4066e649.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoDon't sync with C library I/O buffers
Marco Costalba [Mon, 12 Mar 2012 14:21:54 +0000 (15:21 +0100)]
Don't sync with C library I/O buffers

Now we are forced to just use C++ iostream becuase
buffers are independent and using C library functions
like printf() or scanf() could yield to issues.

Speed up of about 1%.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoFix time_to_msec() precision
Marco Costalba [Sat, 10 Mar 2012 18:38:56 +0000 (19:38 +0100)]
Fix time_to_msec() precision

Result of t.time * 1000 should be a 64 bit value, not an int.

Bug reported by several users.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoIntroduce Eval namespace
Marco Costalba [Tue, 6 Mar 2012 09:09:37 +0000 (10:09 +0100)]
Introduce Eval namespace

Wrap evaluation related stuff and reshuffle
a bit the code.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoDouble pinner bonus
Marco Costalba [Sat, 25 Feb 2012 11:45:34 +0000 (12:45 +0100)]
Double pinner bonus

Fine tune newly introduced pinner bonus score:

After 34696 games at 2"+0.05
Mod vs Orig 7474 - 7087 - 20135 ELO +3 (+- 2.4)

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoConvert init of eval to async option
Marco Costalba [Mon, 5 Mar 2012 18:24:59 +0000 (19:24 +0100)]
Convert init of eval to async option

So to be done only once at startup and in the (unlikely)
cases that a relevant UCI parameter is changed, instead
of doing it at the beginning of each search.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoAsync UCI options actions
Marco Costalba [Sun, 4 Mar 2012 16:57:01 +0000 (17:57 +0100)]
Async UCI options actions

Introduce 'on change' actions that are triggered as soon as
an UCI option is changed by the GUI. This allows to set hash
size before to start the game, helpful especially on very fast
TC and big TT size.

As a side effect remove the 'button' type option, that now
is managed as a 'check' type.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoUse new Time class in timed_wait()
Marco Costalba [Sat, 3 Mar 2012 17:53:37 +0000 (18:53 +0100)]
Use new Time class in timed_wait()

And simplify the code.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoIntroduce single_bit() helper
Marco Costalba [Sun, 4 Mar 2012 22:26:08 +0000 (23:26 +0100)]
Introduce single_bit() helper

Self-documenting code instead of a tricky
bitwise tweak, not known by everybody.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoIntroduce pinning bonus
Marco Costalba [Sat, 25 Feb 2012 11:45:34 +0000 (12:45 +0100)]
Introduce pinning bonus

Add a bonus if a slider is pinning an enemy piece.
Idea from Critter.

After 27443 games at 2"+0.05
Mod vs Orig 5900 - 5518 - 16025 ELO +4 (+- 2.7)

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
8 years agoRewrite time measurement code
Marco Costalba [Sat, 3 Mar 2012 08:35:56 +0000 (09:35 +0100)]
Rewrite time measurement code

Introduce and use a new Time class designed after
QTime, from Qt framework. Should be a more clear and
self documented code.

As an added benefit we now use 64 bits internally to get
millisecs from system time. This avoids to wrap around
to 0 every 2^32 milliseconds, which is 49.71 days.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>