Thursday, April 12, 2012

Evaluation Output

The latest changes to iCE did not gain any ELO or made things worse so I threw them away. To see a bit of progress I decided to spend some time into something that does not affect playing strength so I can keep it for sure. I had a reformatting of the evaluation output on my todo list for some time already and I now decided to get it done. The eval output of the stockfish engine looks really nice and helpful so I decided to mirror that format just using the evaluation terms of iCE instead of the stockfish ones of course.

So it now looks like this and I hope it will me help to fine tune eval in the later development of iCE.


position fen r2q1rk1/pp2np2/5P2/3bp2Q/3pN2b/1B1P4/PPP3PP/R4RK1 w
Total eval of the position : -52
Evaluation:
===========

            Eval term |    White    |    Black    |     Total
                      |   MG    EG  |   MG    EG  |   MG     EG
 ---------------------+-------------+-------------+--------------
      Static Material |   ---   --- |   ---   --- |   141    141
     Dynamic Material |    36    36 |     2     2 |    38     38
             Mobility |   -73   -73 |    66    66 |    -7     -7
   Pattern Recognizer |     0     0 |     0     0 |     0      0
        King Pressure |   -20   -20 |     1     1 |   -19    -19
   Static Pawn Struct |   ---   --- |   ---   --- |     6      0
 Dynamic. Pawn Struct |   -33   -38 |    -4    -4 |   -37    -42
               Pieces |   -10   -10 |    -8     0 |   -18    -10
              Threats |     0     0 |   -52   -87 |   -52    -87
 ---------------------+-------------+-------------+--------------
                Total |   ---   --- |   ---   --- |    52     14

Scaling : 100% MG (52)  0% EG (0)  = 52
NegaMax Adjustment for Side To Move (White): -52


In order to be able to gather the sub terms of eval I had to rewrite parts of it and a minor functionality change related to the sub scores rounding (which is not done anymore) was unavoidable. But tests show it does not seem to hurt. In theory it should even help.

But it now shows also a weakness of iCE. In this position Black is in big trouble but static eval does not indicated that. The king pressure evaluation definitely needs some adjustment. iCE finds its way through anyway once it reaches that position (... but it might never get so far)

info depth 11 seldepth 26 time 2734 nodes 7865064 pv h5h6 e7f5 f1f5 h4f6 e4f6 d8f6 h6f6 f8d8 f5h5 d5b3 h5h8  nps 2876760 score mate 6 hashfull 133 tbhits 0
bestmove h5h6 ponder e7f5

Saturday, March 17, 2012

Murphys law

As I now do some code fine tuning in iCE I run long test series to see that the changes does not make the engine weaker. I started to see iCE losing some games because of illegal moves.

I was absolutely dumbfounded by that. The move generator of iCE passes all perft test, I use a 64 bit hash signature to make undetected collisions very unlikely and as an additional safety belt I verify each move from the hash again for legality. So illegal moves should just be not possible.

After some investigation I found that is was always the same illegal move in the same position and it actually came from the new opening book of iCE. So when I assembled the book I generated an illegal move and put it in the book.


In this position the move to play was specified as 5. bxa6. The iCE book creator translated that move into Pawn from b2 to a6. So when it looked what pawn actually captures on a6 it picked the wrong one. The reason was an uninitialized bitboard and this was a quick fix.

But it still did not explain why iCE actually used that move and did not detect it as illegal. After debugging I found that the move validator had also a small bug (a bracket was closed at the wrong spot) that just effected white pawn captures and so the move went ok through validation.

I fixed that bug too and corrected the books but it showed "whenever something can go wrong it will".

Sunday, March 4, 2012

Another reference match

In my efforts to establish a performance baseline I ran another reference match against a strong engine. This time I picked gaviota and decided to run a few more games at a slightly longer time control to get a more accurate result.

600 games at a TC of 100 moves in 10 sec + 0.5 sec increment per move, ponder off, own book

Score of gaviota v0.85 vs iCE 0.3 v2394 : 392 - 111 - 97  [0.73] 600
ELO difference: 176

which seems about petty correct. So this is the level I want to improve my engine from.

Saturday, March 3, 2012

Windows TimeResolution Trouble

In recent tournaments iCE 0.2 sometimes lost on time which is a very bad thing. Its time control is implemented aggressively so it is using all available time on the last move before the time control but it should not overstep it from the way it was implemented, but it did. As I wanted to improve the time allocation anyway I introduced a soft and a hard time limit. Under certain conditions the engine might continue to search beyond the soft but never beyond the hard limit. The hard limit is verified to always be less than the available remaining time. On the last move before the time control a small safety buffer is used in addition. So I was pretty sure time loss are now a thing of the past.

I was wrong. In a little private reference tournament at very fast time controls (1 sec + 0.1 sec increment per move) iCE was losing almost every game on time. After debugging the GUI and the Engine logs it showed that the engine was assuming to spend less time on a move as the GUI did. So the engine thought it has calculated for 90 ms where the GUI recorded 120 ms for a move. So the initial buffer was getting smaller and smaller with each move until there was almost no buffer anymore and the engine overstepped it.

I investigated the issue a bit and found 2 root causes for this behavior
  • it seems that the Windows XP time resolution has a default quantum of about 10 ms, so the time related functions have a smallest resolution of 10 ms. So for 9 ms the engine thinks it did spend no time at all and 1 ms later it spent already 10.
  • the c++ Sleep(int wait) function is only specifying the minimum time the OS scheduler does not schedule the thread that called Sleep. The actual time the thread is suspended can be much greater
If the engine is idle iCE is sending the worker thread to sleep until the IO thread is signaling new input. Otherwise and idle engine would consume a whole CPU core just by looping over a hasNewInput flag. The Sleep interval is actually pretty small, I was using 10 ms but as this is not reliable the GUI might have sent a search command and started the GUI clock while the engine was still sleeping for some ms. When the engine finally woke up realized the new command and started its clock the GUI clock was already ticking for some ms.

Those 2 facts introduced a significant error margin in short TC games. I investigated some alternatives to either lower the time resolution of the OS (there is an API call for that) or eliminate the Sleep call in the main thread but all had its Cons. At the end I decided to introduce a TIME_RESOLUTION_ERROR_MARGIN of 25 ms. All time limits assume that the engine in reality is using 25 ms more than it thinks and the internal limits are adjusted for that. This works pretty well and eliminates almost all time losses even in very fast TC games. The downside is that the engine is sometimes using some ms less time than it could. But nothing is perfect.

All this is only relevant in very short TC games anyway. In real tournaments the engine has much more time and 25 ms don't make a difference at all. But for engine tuning and change evaluation a lot of games are required and to finish 1000+ games in a reasonable amount of time you have to limit the time per move very much.

With the new TC I ran a quick match against a pair of engines to establish a baseline which I measure engine changes against. iCE was actually doing not so bad, but I think this is TC related. At longer TCs the other engines would probably be stronger. For instance I wasn't able to include the engine Aristarch 4.50 at all because it is very weak at short TCs. It was mated in 9 out of 10 games by iCE (no time losses but real mates). At longer TCs Aristarch seems much stronger.

Those are the results of iCE against 5 engines with 200 games each at a TC of 100 moves in 3 sec + 0.3 sec per move. All engines 32 bits and 1 core, PonderOff, own book where existing.

Rank Name                        ELO   Games   Score   Draws
   1 Quazar 0.4 w32              282     200     84%     16%
   2 cheng3 1.07 JA               96     200     64%     12%
   3 iCE 0.3 v2394                19    1000     53%     17%
   4 Abrok 5.0                   -92     200     37%     24%
   5 Ufim 8.02                  -177     200     26%     18%
   6 Eeyore 1.52 UCI            -186     200     26%     15%
Finished match


Congratulations to Martin Sedlak. It's cheng engine is really very strong and greatly improved in Version 1.07.

Saturday, February 4, 2012

Speeding up the popcount stuff

A recent profile of my little engine revealed that one of the biggest single hotspots in the entire engine is the "popcount" instruction . This instruction returns the number of set "1" bits in a 64 bit integer. Especially the position evaluation is using this instruction a lot. Questions like
  • How many pawns are on a file (double or triple pawn detection)
  • How many squares can the queen attack next move
  • How mobile are the bishops
  • How many pawns are protecting the king in its king shelter
  • How many squares has the knight to move that are not attacked by pawns of the enemy
are all finally answered by counting the bits in a 64 bit integer. As iCE evaluates about 1 million positions per second this instruction is called many many million times. So even a tiny speed improvement can make a big difference.

Intel introduces with its Nehalem architecture in i7 cores a popcount processor instruction but the new CPUs are not so wide spread yet and I don't own one either so for the immediate future popcount still has to be calculated in software. So far I used a stable algorithm that uses shift and multiply operations to do that. This algorithm is here called C-shift (as it is my old shifting implementation in C)

int popCount_C_shift(int64 B)
{
        B = B - ((B >> 1) & 0x5555555555555555ULL);
        B = (B & 0x3333333333333333ULL) +
            ((B >> 2) & 0x3333333333333333ULL);
        B = (B + (B >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
        return (B * 0x0101010101010101ull) >> 56;
}

A new approach implements a loop that deletes 1 bit at at time and counts how often it loops. This is very fast when only a few bits are set (C-sparse)

int popCount_C_sparse(int64 b)
{
   int count = 0;
   while (b != 0)
   {
       count++;
       b &= b - 1; // reset LS1B
   }
   return count;
}


And as 3rd alternative I decided to implement the above C algorithm directly in Assembler using 32 bit instructions to maybe reduce some overhead introduced by the compiler.

__asm
    {
            xor ecx,ecx
            mov edx, dword ptr [b]
            and edx, edx
            jz L1
       
        L0: inc ecx   
            lea eax, [edx-1]
            and edx, eax
            jnz L0
       
        L1: mov edx, dword ptr [b + 04h]
            and edx, edx
            jz L3

        L2: inc ecx   
            lea eax, [edx-1]
            and edx, eax
            jnz L2

        L3: mov eax, ecx
    } 

I compiled the code using the Microsoft C compiler, the Intel C Compiler and the gnu C compiler and let each of the algorithms count the 1 bits in a predefined large set of random numbers with an increasing population count and measured the execution time.

Here are the results














The old C shift based implementation is stable, it runs always the same time independent of the number of set bits. Both looping algorithms outperform it with lesser bits. The break even is with about 7 bits. The MSVC compiler code was slightly faster as my handcrafted assembler.

A similar picture is achieved using the Intel compiler














The shift C code is stable and runs slightly faster that the same algorithm compiled with MSVC. The looping algorithms outperform it but are a bit slower than the MSVC code. No profile guided optimization has been used (PGO). Maye that would level the execution speed compared with MSVC but was not tested.

Finally I compiled the source with the gnu C compiler using the gnu C builtin popcount instruction instead of my assembler code as the gnu C compiler does not understand inline assembler in Intel Syntax.












It shows that the native gcc popcount instruction is probably implemented as a shifting algorithm and surprisingly it is the worst performing of all 3. The looping algorithm gets a performance hit between 2 and 3 bits set but it still outperforms the other 2 until 6 bits are set.

So I took the architectural decision to drop my handcrafted assembler code and introduce the C version of the looping algorithm into my engine. I replace all popcount calls where the number of set bits is expected to be smaller than 7 (like number of pawns on the 7th rank) with the new popcount code and keep the old one for the remaining calls (e.g. number of squares a queen can attack).

I got a nice speedup of more than 10% in execution speed, which was well worth the exercise.

Sunday, January 22, 2012

Fixing the repetition detection hash table

Currently I try to speed up iCE a bit and investigate where iCE spends its time, yeah I'm profiling it a bit. One of the functions that used more than it should was the calculateRepetitions() function.

This function is used to determine whether this position was encountered before so a 3fold repetition is likely. The function has to parts, whenever a new position is entered it increments for this position (zobrist key & size_of_hash -1) a counter in a small hash table by 1 and then checks the value of the counter. If it is now 1 this means it was 0 before and we have never seen this position before. We can be sure this position is not repeated and exit. If the counter is 2 or more we might have a repetition or a hash key collision (we don't know). We now scan through the list of previous positions up to the latest non reversible move. This takes a bit longer.

When we unmake a move we decrement the counter in the hash table. This means the hash table is always almost empty. It only takes 1 slot for every real move on the board + 1 slot for every ply of search.

To see why this function takes so long despite its hash early exit I measured how many early exits I take and how often I scan through the list of keys after all. To my surprise I only had an early exit rate of at most 20% and in longer searches it went down to 10%.

How could that be ?

I looked at the content of the table and it contained negative values, it was full of them. This should not have happened. It is of course not possible to encounter a position less than never.

It turned out that I introduced an inconsistency when I added Null Move. I did not increment the hash slot when doing a Null Move but I decremented it when I undid the Null Move. So after a while most of the slots contained negative values and provided no value anymore.

After fixing that my early exit rate went up to 99,95% and calculateRepetitions() went almost into non existence in my function profile list.

Saturday, January 21, 2012

iCE is a winner in the local newspaper chess study challenge

Our local newspaper organizes once a year a local chess challenge with 1 chess study a week (5 in total) of increasing difficulty. Me and my engine were taking part this year and with a bit of luck we were the lucky winners of the 3rd challenge, a really nice Mate in 4 problem.

Mate in 4, Study by Frank Fiedler

White must take care not to stalemate the black king, so the rook must be blocked in its mobility for a moment. The correct solution is 1. Rh4 !

iCE 0.3 v2058 by Thomas Petzke
position fen 2b5/1p1p4/1P1P4/ppBP1B2/k2p1R2/N2P4/K1P1P3/8 w - - 0 1
go depth 10
info depth 1 seldepth 7 time 0 nodes 72 pv c5d4  nps 71999 score cp 1526
info depth 2 seldepth 8 time 0 nodes 186 pv c5d4 b5b4  nps 185999 score cp 1510
info depth 3 seldepth 10 time 0 nodes 813 pv c5d4 b5b4 nps 812999 score cp 1518
info depth 4 seldepth 12 time 16 nodes 1894 pv c5d4 b5b4 nps 118375 score cp 1507 

info depth 5 seldepth 18 time 15 nodes 6381 pv c5d4 b5b4  nps 425400 score cp 1531 
info depth 6 seldepth 20 time 63 nodes 13244 pv c5d4 b5b4 nps 210222 score cp 1629 
info depth 7 seldepth 28 time 94 nodes 53632 pv c2c4 b5b4 nps 570553 score cp 1660 
info depth 8 seldepth 41 time 156 nodes 289838 pv f5g4 b5b4 nps 1857935 score mate 5 
info depth 9 seldepth 9 time 125 nodes 375331 pv f4h4 b5b4 f5g4 b4a3 c2c4 d4c3 g4d7  nps 3002648 score mate 4
bestmove f4h4 ponder b5b4


So as iCE really needs a hardware upgrade, I'm running and developing it on a 6 year old desktop PC which must be replaced urgently the prize money of 30€ might earn it an additional GB of RAM in the new system.