Subversion Repositories Games.Chess Giants

Rev

Rev 33 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #include "chess.h"
  2. #include "data.h"
  3. /* last modified 09/16/14 */
  4. /*
  5.  *******************************************************************************
  6.  *                                                                             *
  7.  *   HashProbe() is used to retrieve entries from the transposition table so   *
  8.  *   this sub-tree won't have to be searched again if we reach a position that *
  9.  *   has been searched previously.  A transposition table position contains    *
  10.  *   the following data packed into 128 bits with each item taking the number  *
  11.  *   of bits given in the table below:                                         *
  12.  *                                                                             *
  13.  *     shr  bits   name   description                                          *
  14.  *      55    9    age    search id to identify old trans/ref entries.         *
  15.  *      53    2    type   0->value is worthless; 1-> value represents a        *
  16.  *                        fail-low bound; 2-> value represents a fail-high     *
  17.  *                        bound; 3-> value is an exact score.                  *
  18.  *      32   21    move   best move from the current position, according to    *
  19.  *                        the search at the time this position was stored.     *
  20.  *      17   15    draft  the depth of the search below this position, which   *
  21.  *                        is used to see if we can use this entry at the       *
  22.  *                        current position.                                    *
  23.  *       0   17    value  unsigned integer value of this position + 65536.     *
  24.  *                        this might be a good score or search bound.          *
  25.  *       0   64    key    64 bit hash signature, used to verify that this      *
  26.  *                        entry goes with the current board position.          *
  27.  *                                                                             *
  28.  *   The underlying scheme here is that we use a "bucket" of N entries.  In    *
  29.  *   HashProbe() we simply compare against each of the four entries for a      *
  30.  *   match.  Each "bucket" is carefully aligned to a 64-byte boundary so that  *
  31.  *   the bucket fits into a single cache line for efficiency.  The bucket size *
  32.  *   (N) is currently set to 4.                                                *
  33.  *                                                                             *
  34.  *   Crafty uses the lockless hashing approach to avoid lock overhead in the   *
  35.  *   hash table accessing (reading or writing).  What we do is store the key   *
  36.  *   and the information in two successive writes to memory.  But since there  *
  37.  *   is nothing that prevents another CPU from interlacing its writes with     *
  38.  *   ours, we want to make sure that the bound/draft/etc really goes with the  *
  39.  *   key.  Consider thread 1 trying to store A1 and A2 in two successive 64    *
  40.  *   words, while thread 2 is trying to store B1 and B2.  Since the two cpus   *
  41.  *   are fully independent, we could end up with {A1,A2}, {A1,B2}, {B1,A2} or  *
  42.  *   {B1,B2}.  The two cases with one word of entry A and one word of entry B  *
  43.  *   are problematic since the information part does not belong with the       *
  44.  *   signature part, and a hash hit (signature match) will retrieve data that  *
  45.  *   does not match the position.  Let's assume that the first word is the     *
  46.  *   signature (A1 or B1) and the second word is the data (A2 or B2).  What we *
  47.  *   do is store A1^A2 (exclusive-or the two parts) in the 1 (key) slot of the *
  48.  *   entry, and store A2 in the data part.  Now, before we try to compare the  *
  49.  *   signatures, we have to "un-corrupt" the stored signature by again using   *
  50.  *   xor, since A1^A2^A2 gives us the original A1 signature again.  But if we  *
  51.  *   store A1^A2, and the data part gets replaced by B2, then we try to match  *
  52.  *   against A1^A2^B2 and that won't match unless we are lucky and A2 == B2    *
  53.  *   which means the match is OK anyway.  This eliminates the need to lock the *
  54.  *   hash table while storing the two values, which would be a big performance *
  55.  *   hit since hash entries are probed/stored in almost every node of the tree *
  56.  *   except for the quiescence search.                                         *
  57.  *                                                                             *
  58.  *******************************************************************************
  59.  */
  60. int HashProbe(TREE * RESTRICT tree, int ply, int depth, int side, int alpha,
  61.     int beta, int *value) {
  62.   HASH_ENTRY *htable;
  63.   HPATH_ENTRY *ptable;
  64.   uint64_t word1, word2, temp_hashkey;
  65.   int type, draft, avoid_null = 0, val, entry, i;
  66.  
  67. /*
  68.  ************************************************************
  69.  *                                                          *
  70.  *  All we have to do is loop through four entries to see   *
  71.  *  if there is a signature match.  There can only be one   *
  72.  *  instance of any single signature, so the first match is *
  73.  *  all we need.                                            *
  74.  *                                                          *
  75.  ************************************************************
  76.  */
  77.   tree->hash_move[ply] = 0;
  78.   temp_hashkey = (side) ? HashKey : ~HashKey;
  79.   htable = hash_table + (temp_hashkey & hash_mask);
  80.   for (entry = 0; entry < 4; entry++) {
  81.     word1 = htable[entry].word1;
  82.     word2 = htable[entry].word2 ^ word1;
  83.     if (word2 == temp_hashkey)
  84.       break;
  85.   }
  86. /*
  87.  ************************************************************
  88.  *                                                          *
  89.  *  If we found a match, we have to verify that the draft   *
  90.  *  is at least equal to the current depth, if not higher,  *
  91.  *  and that the bound/score will let us terminate the      *
  92.  *  search early.                                           *
  93.  *                                                          *
  94.  *  We also return an "avoid_null" status if the matched    *
  95.  *  entry does not have enough draft to terminate the       *
  96.  *  current search but does have enough draft to prove that *
  97.  *  a null-move search would not fail high.  This avoids    *
  98.  *  the null-move search overhead in positions where it is  *
  99.  *  simply a waste of time to try it.                       *
  100.  *                                                          *
  101.  *  If this is an EXACT entry, we are going to store the PV *
  102.  *  in a safe place so that if we get a hit on this entry,  *
  103.  *  we can recover the PV and see the complete path rather  *
  104.  *  rather than one that is incomplete.                     *
  105.  *                                                          *
  106.  *  One other issue is to update the age field if we get a  *
  107.  *  hit on an old position, so that it won't be replaced    *
  108.  *  just because it came from a previous search.            *
  109.  *                                                          *
  110.  ************************************************************
  111.  */
  112.   if (entry < 4) {
  113.     if (word1 >> 55 != transposition_age) {
  114.       word1 =
  115.           (word1 & 0x007fffffffffffffull) | ((uint64_t) transposition_age <<
  116.           55);
  117.       htable[entry].word1 = word1;
  118.       htable[entry].word2 = word1 ^ word2;
  119.     }
  120.     val = (word1 & 0x1ffff) - 65536;
  121.     draft = (word1 >> 17) & 0x7fff;
  122.     tree->hash_move[ply] = (word1 >> 32) & 0x1fffff;
  123.     type = (word1 >> 53) & 3;
  124.     if ((type & UPPER) &&
  125.         depth - null_depth - depth / null_divisor - 1 <= draft && val < beta)
  126.       avoid_null = AVOID_NULL_MOVE;
  127.     if (depth <= draft) {
  128.       if (val > 32000)
  129.         val -= ply - 1;
  130.       else if (val < -32000)
  131.         val += ply - 1;
  132.       *value = val;
  133. /*
  134.  ************************************************************
  135.  *                                                          *
  136.  *  We have three types of results.  An EXACT entry was     *
  137.  *  stored when val > alpha and val < beta, and represents  *
  138.  *  an exact score.  An UPPER entry was stored when val <   *
  139.  *  alpha, which represents an upper bound with the score   *
  140.  *  likely being even lower.  A LOWER entry was stored when *
  141.  *  val > beta, which represents alower bound with the      *
  142.  *  score likely being even higher.                         *
  143.  *                                                          *
  144.  *  For EXACT entries, we save the path from the position   *
  145.  *  to the terminal node that produced the backed-up score  *
  146.  *  so that we can complete the PV if we get a hash hit on  *
  147.  *  this entry.                                             *
  148.  *                                                          *
  149.  ************************************************************
  150.  */
  151.       switch (type) {
  152.         case EXACT:
  153.           if (val > alpha && val < beta) {
  154.             SavePV(tree, ply, 1);
  155.             ptable = hash_path + (temp_hashkey & hash_path_mask);
  156.             for (entry = 0; entry < 16; entry++)
  157.               if (ptable[entry].path_sig == temp_hashkey) {
  158.                 for (i = ply;
  159.                     i < Min(MAXPLY - 1, ptable[entry].hash_pathl + ply); i++)
  160.                   tree->pv[ply - 1].path[i] =
  161.                       ptable[entry].hash_path_moves[i - ply];
  162.                 if (ptable[entry].hash_pathl + ply < MAXPLY - 1)
  163.                   tree->pv[ply - 1].pathh = 0;
  164.                 tree->pv[ply - 1].pathl =
  165.                     Min(MAXPLY - 1, ply + ptable[entry].hash_pathl);
  166.                 ptable[entry].hash_path_age = transposition_age;
  167.                 break;
  168.               }
  169.           }
  170.           return HASH_HIT;
  171.         case UPPER:
  172.           if (val <= alpha)
  173.             return HASH_HIT;
  174.           break;
  175.         case LOWER:
  176.           if (val >= beta)
  177.             return HASH_HIT;
  178.           break;
  179.       }
  180.     }
  181.     return avoid_null;
  182.   }
  183.   return HASH_MISS;
  184. }
  185.  
  186. /* last modified 09/16/14 */
  187. /*
  188.  *******************************************************************************
  189.  *                                                                             *
  190.  *   HashStore() is used to store entries into the transposition table so that *
  191.  *   this sub-tree won't have to be searched again if the same position is     *
  192.  *   reached.  We basically store three types of entries:                      *
  193.  *                                                                             *
  194.  *     (1) EXACT.  This entry is stored when we complete a search at some ply  *
  195.  *          and end up with a score that is greater than alpha and less than   *
  196.  *          beta, which is an exact score, which also has a best move to try   *
  197.  *          if we encounter this position again.                               *
  198.  *                                                                             *
  199.  *     (2) LOWER.  This entry is stored when we complete a search at some ply  *
  200.  *          and end up with a score that is greater than or equal to beta.  We *
  201.  *          know know that this score should be at least equal to beta and may *
  202.  *          well be even higher.  So this entry represents a lower bound on    *
  203.  *          the score for this node, and we also have a good move to try since *
  204.  *          it caused the cutoff, although we do not know if it is the best    *
  205.  *          move or not since not all moves were search.                       *
  206.  *                                                                             *
  207.  *     (3) UPPER.  This entry is stored when we complete a search at some ply  *
  208.  *          and end up with a score that is less than or equal to alpha.  We   *
  209.  *          know know that this score should be at least equal to alpha and    *
  210.  *          may well be even lower.  So this entry represents an upper bound   *
  211.  *          on the score for this node.  We have no idea about which move is   *
  212.  *          best in this position since they all failed low, so we store a     *
  213.  *          best move of zero.                                                 *
  214.  *                                                                             *
  215.  *   For storing, we may require three passes.  We make our first pass looking *
  216.  *   for an entry that matches the current hash signature.  If we find a match *
  217.  *   then we are constrained to overwrite that entry regardless of any other   *
  218.  *   considerations.  The second pass looks for entries stored in previous     *
  219.  *   searches (not iterations) and chooses the one with the shallowest draft,  *
  220.  *   if one is found;  Otherwise we make a final pass over the bucket and      *
  221.  *   choose the entry with the shallowest draft, period.                       *
  222.  *                                                                             *
  223.  *******************************************************************************
  224.  */
  225. void HashStore(TREE * RESTRICT tree, int ply, int depth, int side, int type,
  226.     int value, int bestmove) {
  227.   HASH_ENTRY *htable, *replace = 0;
  228.   HPATH_ENTRY *ptable;
  229.   uint64_t word1, temp_hashkey;
  230.   int entry, draft, age, replace_draft, i, j;
  231.  
  232. /*
  233.  ************************************************************
  234.  *                                                          *
  235.  *  "Fill in the blank" and build a table entry from        *
  236.  *  current search information.                             *
  237.  *                                                          *
  238.  ************************************************************
  239.  */
  240.   word1 = transposition_age;
  241.   word1 = (word1 << 2) | type;
  242.   if (value > 32000)
  243.     value += ply - 1;
  244.   else if (value < -32000)
  245.     value -= ply - 1;
  246.   word1 = (word1 << 21) | bestmove;
  247.   word1 = (word1 << 15) | depth;
  248.   word1 = (word1 << 17) | (value + 65536);
  249.   temp_hashkey = (side) ? HashKey : ~HashKey;
  250. /*
  251.  ************************************************************
  252.  *                                                          *
  253.  *  Now we search for an entry to overwrite in three        *
  254.  *  passes.                                                 *
  255.  *                                                          *
  256.  *  Pass 1:  If any signature in the table matches the      *
  257.  *    current signature, we are going to overwrite this     *
  258.  *    entry, period.  It might seem worthwhile to check the *
  259.  *    draft and not overwrite if the table draft is greater *
  260.  *    than the current remaining depth, but after you think *
  261.  *    about it, this is a bad idea.  If the draft is        *
  262.  *    greater than or equal the current remaining depth,    *
  263.  *    then we should never get here unless the stored bound *
  264.  *    or score is unusable because of the current alpha/    *
  265.  *    beta window.  So we are overwriting to avoid losing   *
  266.  *    the current result.                                   *
  267.  *                                                          *
  268.  *  Pass 2:  If any of the entries come from a previous     *
  269.  *    search (not iteration) then we choose the entry from  *
  270.  *    this set that has the smallest draft, since it is the *
  271.  *    least potentially usable result.                      *
  272.  *                                                          *
  273.  *  Pass 3:  If neither of the above two found an entry to  *
  274.  *    overwrite, we simply choose the entry from the bucket *
  275.  *    with the smallest draft and overwrite that.           *
  276.  *                                                          *
  277.  ************************************************************
  278.  */
  279.   htable = hash_table + (temp_hashkey & hash_mask);
  280.   for (entry = 0; entry < 4; entry++) {
  281.     if (temp_hashkey == (htable[entry].word1 ^ htable[entry].word2)) {
  282.       replace = htable + entry;
  283.       break;
  284.     }
  285.   }
  286.   if (!replace) {
  287.     replace_draft = 99999;
  288.     for (entry = 0; entry < 4; entry++) {
  289.       age = htable[entry].word1 >> 55;
  290.       draft = (htable[entry].word1 >> 17) & 0x7fff;
  291.       if (age != transposition_age && replace_draft > draft) {
  292.         replace = htable + entry;
  293.         replace_draft = draft;
  294.       }
  295.     }
  296.     if (!replace) {
  297.       for (entry = 0; entry < 4; entry++) {
  298.         draft = (htable[entry].word1 >> 17) & 0x7fff;
  299.         if (replace_draft > draft) {
  300.           replace = htable + entry;
  301.           replace_draft = draft;
  302.         }
  303.       }
  304.     }
  305.   }
  306. /*
  307.  ************************************************************
  308.  *                                                          *
  309.  *  Now that we know which entry to replace, we simply      *
  310.  *  stuff the values and exit.  Note that the two 64 bit    *
  311.  *  words are xor'ed together and stored as the signature   *
  312.  *  for the "lockless-hash" approach.                       *
  313.  *                                                          *
  314.  ************************************************************
  315.  */
  316.   replace->word1 = word1;
  317.   replace->word2 = temp_hashkey ^ word1;
  318. /*
  319.  ************************************************************
  320.  *                                                          *
  321.  *  If this is an EXACT entry, we are going to store the PV *
  322.  *  in a safe place so that if we get a hit on this entry,  *
  323.  *  we can recover the PV and see the complete path rather  *
  324.  *  rather than one that is incomplete.                     *
  325.  *                                                          *
  326.  ************************************************************
  327.  */
  328.   if (type == EXACT) {
  329.     ptable = hash_path + (temp_hashkey & hash_path_mask);
  330.     for (i = 0; i < 16; i++, ptable++) {
  331.       if (ptable->path_sig == temp_hashkey ||
  332.           ((transposition_age - ptable->hash_path_age) > 1)) {
  333.         for (j = ply; j < tree->pv[ply - 1].pathl; j++)
  334.           ptable->hash_path_moves[j - ply] = tree->pv[ply - 1].path[j];
  335.         ptable->hash_pathl = tree->pv[ply - 1].pathl - ply;
  336.         ptable->path_sig = temp_hashkey;
  337.         ptable->hash_path_age = transposition_age;
  338.         break;
  339.       }
  340.     }
  341.   }
  342. }
  343.  
  344. /* last modified 09/16/14 */
  345. /*
  346.  *******************************************************************************
  347.  *                                                                             *
  348.  *   HashStorePV() is called by Iterate() to insert the PV moves so they will  *
  349.  *   be searched before any other moves.  Normally the PV moves would be in    *
  350.  *   the table, but on occasion they can be overwritten, particularly the ones *
  351.  *   that are a significant distance from the root since those table entries   *
  352.  *   will have a low draft.                                                    *
  353.  *                                                                             *
  354.  *******************************************************************************
  355.  */
  356. void HashStorePV(TREE * RESTRICT tree, int side, int ply) {
  357.   HASH_ENTRY *htable, *replace;
  358.   uint64_t temp_hashkey, word1;
  359.   int entry, draft, replace_draft, age;
  360.  
  361. /*
  362.  ************************************************************
  363.  *                                                          *
  364.  *  First, compute the initial hash address and the fake    *
  365.  *  entry we will store if we don't find a valid match      *
  366.  *  already in the table.                                   *
  367.  *                                                          *
  368.  ************************************************************
  369.  */
  370.   temp_hashkey = (side) ? HashKey : ~HashKey;
  371.   word1 = transposition_age;
  372.   word1 = (word1 << 2) | WORTHLESS;
  373.   word1 = (word1 << 21) | tree->pv[0].path[ply];
  374.   word1 = (word1 << 32) | 65536;
  375. /*
  376.  ************************************************************
  377.  *                                                          *
  378.  *  Now we search for an entry to overwrite in three        *
  379.  *  passes.                                                 *
  380.  *                                                          *
  381.  *  Pass 1:  If any signature in the table matches the      *
  382.  *    current signature, we are going to overwrite this     *
  383.  *    entry, period.  It might seem worthwhile to check the *
  384.  *    draft and not overwrite if the table draft is greater *
  385.  *    than the current remaining depth, but after you think *
  386.  *    about it, this is a bad idea.  If the draft is        *
  387.  *    greater than or equal the current remaining depth,    *
  388.  *    then we should never get here unless the stored bound *
  389.  *    or score is unusable because of the current alpha/    *
  390.  *    beta window.  So we are overwriting to avoid losing   *
  391.  *    the current result.                                   *
  392.  *                                                          *
  393.  *  Pass 2:  If any of the entries come from a previous     *
  394.  *    search (not iteration) then we choose the entry from  *
  395.  *    this set that has the smallest draft, since it is the *
  396.  *    least potentially usable result.                      *
  397.  *                                                          *
  398.  *  Pass 3:  If neither of the above two found an entry to  *
  399.  *    overwrite, we simply choose the entry from the bucket *
  400.  *    with the smallest draft and overwrite that.           *
  401.  *                                                          *
  402.  ************************************************************
  403.  */
  404.   htable = hash_table + (temp_hashkey & hash_mask);
  405.   for (entry = 0; entry < 4; entry++) {
  406.     if ((htable[entry].word2 ^ htable[entry].word1) == temp_hashkey) {
  407.       htable[entry].word1 &= ~((uint64_t) 0x1fffff << 32);
  408.       htable[entry].word1 |= (uint64_t) tree->pv[0].path[ply] << 32;
  409.       htable[entry].word2 = temp_hashkey ^ htable[entry].word1;
  410.       break;
  411.     }
  412.   }
  413.   if (entry == 4) {
  414.     replace = 0;
  415.     replace_draft = 99999;
  416.     for (entry = 0; entry < 4; entry++) {
  417.       age = htable[entry].word1 >> 55;
  418.       draft = (htable[entry].word1 >> 17) & 0x7fff;
  419.       if (age != transposition_age && replace_draft > draft) {
  420.         replace = htable + entry;
  421.         replace_draft = draft;
  422.       }
  423.     }
  424.     if (!replace) {
  425.       for (entry = 0; entry < 4; entry++) {
  426.         draft = (htable[entry].word1 >> 17) & 0x7fff;
  427.         if (replace_draft > draft) {
  428.           replace = htable + entry;
  429.           replace_draft = draft;
  430.         }
  431.       }
  432.     }
  433.     replace->word1 = word1;
  434.     replace->word2 = temp_hashkey ^ word1;
  435.   }
  436. }
  437.