Ticket #145: reading.texi.patch

File reading.texi.patch, 11.6 kB (added by draqo, 2 years ago)
  • gnugo/doc/reading.texi

    RCS file: /sources/gnugo/gnugo/doc/reading.texi,v
    retrieving revision 1.20
    diff -u -r1.20 reading.texi
     
    225225option @pxref{Invoking GNU Go}. 
    226226 
    227227The hash table is created once and for all at the beginning of 
    228 the game by the function @code{hashtable_new()}. Although hash 
     228the game by the function @code{reading_cache_init()}. Although hash 
    229229memory is thus allocated only once in the game, the table is 
    230230reinitialized at the beginning of each move by a call to 
    231 @code{hashtable_clear()} from @code{genmove()}. 
     231@code{reading_cache_clear()} from @code{genmove()}. 
    232232 
    233233@menu 
    234234* Hash Calculation::            Calculation of the hash value 
    235235* Hash Organization::           Organization of the hash table 
    236 * Hash Structures::             Structures in @file{hash.h} 
    237236@end menu 
    238237 
    239238@node Hash Calculation 
     
    249248@enumerate 
    250249@item First we define a @dfn{go position}.  This positions consists of 
    251250@itemize @bullet 
    252 @item the actual board, i.e. the locations and colors of the stones 
    253 @item A @dfn{ko point}, if a ko is going on.  The ko point is defined as 
     251@item the actual board, i.e. the locations and colors of the stones; 
     252@item a @dfn{ko point}, if a ko is going on; the ko point is defined as 
    254253the empty point where the last single stone was situated before 
    255254it was captured. 
    256255@end itemize 
    257256 
     257In addition to that we include some more information into the hash code, 
     258that describes stored analisys results: 
     259@itemize @bullet 
     260@item a @dfn{komaster} and @dfn{kom_pos}, @xref{Ko}, 
     261@item an ID (an int between 0 and 255) of a function used to calculate 
     262the results; 
     263@item a target of analisys (up to two positions on a board); 
     264@item sometimes some extra information used by various functions. 
     265@end itemize 
     266 
    258267It is not necessary to specify the color to move (white or black) 
    259268as part of the position. The reason for this is that read results 
    260269are stored separately for the various reading functions such as 
    261270@code{attack3}, and it is implicit in the calling function which 
    262271player is to move. 
    263272 
    264 @item For each location on the board we generate random numbers: 
    265 @itemize @bullet 
    266 @item A number which is used if there is a white stone on this location 
    267 @item A number which is used if there is a black stone on this location 
    268 @item A number which is used if there is a ko on this location 
    269 @end itemize 
    270  
     273@item For each of the above information (excluding extra information) 
     274we generate tables with random numbers for each: function, komaster 
     275status or position on a board. 
    271276These random numbers are generated once at initialization time and 
    272277then used throughout the life time of the hash table. 
    273278 
    274279@item The hash key for a position is the XOR of all the random numbers 
    275 which are applicable for the position (white stones, black stones, and 
    276 ko position). 
     280which are applicable for the position. If extra information is needed 
     281it is passed by a function as a hash code which is XORed into the 
     282resulting hash key. 
    277283@end enumerate 
    278284 
    279285@node Hash Organization 
    280286@subsection Organization of the hash table 
    281287 
    282 The hash table consists of 3 parts: 
    283  
    284 @cindex Hash node 
    285 @cindex Read result 
    286  
    287 @itemize @bullet 
    288 @item An area which contains so called @dfn{Hash Nodes}. Each hash node 
    289 contains: 
    290 @itemize @minus 
    291 @item A go position as defined above. 
    292 @item A computed hash value for the position 
    293 @item A pointer to Read Results (see below) 
    294 @item A pointer to another hash node. 
    295 @end itemize 
    296  
    297 @item An area with so called Read Results.  These are used to store 
    298 which function was called in the go position, which string was 
    299 under attack or to be defended, and the result of the reading. 
    300  
    301 Each Read Result contains:  
    302 @itemize @minus 
    303 @item the function ID (an int between 0 and 255), the position of the 
    304 string under attack and a depth value, which is used to 
    305 determine how deep the search was when it was made, packed into 
    306 one 32 bit integer.  
    307 @item The result of the search (a numeric value) and a position to 
    308 play to get the result packed into one 32 bit integer.  
    309 @item A pointer to another Read Result. 
    310 @end itemize 
    311  
    312 @item An array of pointers to hash nodes.  This is the hash table 
    313 proper. 
    314  
    315 @end itemize 
    316  
    317 When the hash table is created, these 3 areas are allocated using 
    318 @code{malloc()}.  When the hash table is populated, all contents are taken 
    319 from the Hash nodes and the Read results. No further allocation is 
    320 done and when all nodes or results are used, the hash table is full. 
    321 Nothing is deleted from the hash table except when it is totally 
    322 emptied, at which point it can be used again as if newly initialized. 
    323  
    324 @findex hashtable_search 
    325 When a function wants to use the hash table, it looks up the current 
    326 position using @code{hashtable_search()}. If the position doesn't already 
    327 exist there, it can be entered using 
    328  
    329 @findex hashtable_enter_position 
    330 @code{hashtable_enter_position()}.   
    331  
    332 @findex hashtable_enter_position 
    333 Once the function has a pointer to the hash node containing a 
    334 function, it can search for a result of a previous search using 
    335 @code{hashnode_search()}.  If a result is found, it can be used, and 
    336 if not, a new result can be entered after a search using  
    337 @findex hashnode_new_result 
    338 @code{hashnode_new_result()}. 
    339  
    340 Hash nodes which hash to the same position in the hash table 
    341 (collisions) form a simple linked list.  Read results for the same 
    342 position, created by different functions and different attacked or 
    343 defended strings also form a linked list. 
    344  
    345 This is deemed sufficiently efficient for now, but the representation 
    346 of collisions could be changed in the future.  It is also not 
    347 determined what the optimum sizes for the hash table, the number of 
    348 positions and the number of results are. 
    349  
    350 @node Hash Structures 
    351 @subsection Hash Structures 
    352  
    353288The basic hash structures are declared in @file{engine/hash.h} and 
    354 @file{engine/cache.c} 
    355  
    356 @example 
    357 typedef struct hashposition_t @{ 
    358   Compacttype  board[COMPACT_BOARD_SIZE]; 
    359   int          ko_pos; 
    360 @} Hashposition; 
    361 @end example 
     289@file{engine/cache.h} 
    362290 
    363 Represents the board and optionally the location of a ko, 
    364 which is an illegal move. The player whose move is next 
    365 is not recorded. 
     291The hash table consists of an array of pointers to hash entries and 
     292variable that defines number of the entries. 
    366293 
    367294@example 
    368295typedef struct @{ 
    369   Hashvalue     hashval; 
    370   Hashposition  hashpos; 
    371 @} Hash_data; 
    372 @end example 
    373  
    374 Represents the return value of a function (@code{hashval}) and 
    375 the board state (@code{hashpos}). 
    376  
    377 @example 
    378 typedef struct read_result_t @{ 
    379   unsigned int data1;    
    380   unsigned int data2; 
     296  unsigned int num_entries; 
     297  Hashentry *entries; 
     298@} Transposition_table; 
    381299 
    382   struct read_result_t *next; 
    383 @} Read_result; 
     300Transposition_table ttable; 
    384301@end example 
    385302 
    386 The data1 field packs into 32 bits the following fields: 
     303@cindex Hash entry 
    387304 
    388 @example 
    389  
    390 komaster:  2 bits (EMPTY, BLACK, WHITE, or GRAY) 
    391 kom_pos : 10 bits (allows MAX_BOARD up to 31) 
    392 routine :  4 bits (currently 10 different choices) 
    393 str1    : 10 bits 
    394 stackp  :  5 bits 
    395  
    396 @end example 
    397  
    398 The data2 field packs into 32 bits the following fields: 
     305The @dfn{Hash Entry} consists of two @dfn{Hash Nodes}. One is the 
     306most reliable node and the second is the newest. The reliability is 
     307computed based on cost of the function, which created a node and 
     308depth of analysis for that node (the lower depth the more reliable 
     309results). 
    399310 
    400311@example 
    401  
    402 status :   2 bits (0 free, 1 open, 2 closed) 
    403 result1:   4 bits 
    404 result2:   4 bits 
    405 move   :  10 bits 
    406 str2   :  10 bits 
    407  
     312typedef struct @{ 
     313  Hashnode most_reliable; 
     314  Hashnode newest; 
     315@} Hashentry; 
    408316@end example 
    409317 
    410 The @code{komaster} and @code{(kom_pos)} field are 
    411 documented in @xref{Ko}. 
     318@cindex Hash node 
    412319 
    413 When a new result node is created, 'status' is set to 1 'open'. 
    414 This is then set to 2 'closed' when the result is entered. The main 
    415 use for this is to identify open result nodes when the hashtable is 
    416 partially cleared. Another potential use for this field is to 
    417 identify repeated positions in the reading, in particular local 
    418 double or triple kos. 
     320Each hash node contains a computed hash value (as defined above) 
     321and @dfn{Read Results}. 
    419322 
    420323@example 
    421 typedef struct hashnode_t @{ 
    422   Hash_data            key; 
    423   Read_result        * results; 
    424   struct hashnode_t  * next; 
     324typedef struct @{ 
     325  Hash_data key; 
     326  HASHNODE_DATATYPE data; /* 32-bit value */ 
    425327@} Hashnode; 
    426328@end example 
    427329 
    428 The hash table consists of hash nodes.  Each hash node consists of 
    429 The hash value for the position it holds, the position itself and 
    430 the actual information which is purpose of the table from the start. 
     330@cindex Read result 
    431331 
    432 There is also a pointer to another hash node which is used when 
    433 the nodes are sorted into hash buckets (see below). 
     332Read Results are used to store which function was called in the go 
     333position, which string was under attack or to be defended, and the 
     334result of the reading. The result is packed into one 32 bit integer. 
     335Each Read Result contains:  
     336@itemize @minus 
     337@item a remaining (to the max depth) depth value, which is used to 
     338determine how deep the search was when node was stored (5 bits);  
     339@item the results of the search (two numeric values - 2 x 4 bits); 
     340@item a cost of a function which stored the result (an arbitrary 
     341numeric value) (4 bits); 
     342@item a move to play to get the result (10 bits). 
     343@end itemize 
    434344 
    435345@example 
    436 typedef struct hashtable @{ 
    437   size_t         hashtablesize; /* Number of hash buckets */ 
    438   Hashnode    ** hashtable;     /* Pointer to array of hashnode lists */ 
     346#define hn_create_data(remaining_depth, value1, value2, move, cost) \ 
     347    ((((value1)         & 0x0f)  << 23) \ 
     348   | (((value2)         & 0x0f)  << 19) \ 
     349   | (((move)           & 0x3ff) <<  9) \ 
     350   | (((cost)           & 0x0f)  <<  5) \ 
     351   | (((remaining_depth & 0x1f)/*<<  0*/))) 
     352@end example 
    439353 
    440   int            num_nodes;     /* Total number of hash nodes */ 
    441   Hashnode     * all_nodes;     /* Pointer to all allocated hash nodes. */ 
    442   int            free_node;     /* Index to next free node. */ 
     354When the hash table is created, the hash entries are allocated using 
     355@code{malloc()}. After that, no further allocation is 
     356done and when all nodes or results are used, the hash table is full. 
     357Nothing is deleted, instead nodes are replaced by newer or more 
     358reliable nodes (when collision occurs). 
    443359 
    444   int            num_results;   /* Total number of results */ 
    445   Read_result  * all_results;   /* Pointer to all allocated results. */ 
    446   int            free_result;   /* Index to next free result. */ 
    447 @} Hashtable; 
    448 @end example 
     360@findex tt_get 
     361When a function wants to use the hash table, it looks up the current 
     362position using @code{tt_get()}. If the position doesn't already 
     363exist there, it can be entered using 
    449364 
    450 The hash table consists of three parts: 
     365@findex tt_update 
     366@code{tt_update()}.   
    451367 
    452 @itemize @bullet 
    453 @item The hash table proper: a number of hash buckets with collisions 
    454 being handled by a linked list. 
    455 @item The hash nodes.  These are allocated at creation time and are  
    456 never removed or reallocated in the current implementation. 
    457 @item The results of the searches.  Since many different searches can 
    458 be done in the same position, there should be more of these than 
    459 hash nodes. 
    460 @end itemize 
     368@findex tt_update 
     369There are several macros which store the result and return it from 
     370a searching functions. They are named @code{READ_RETURN0}, 
     371@code{READ_RETURN}, @code{READ_RETURN_SEMEAI}, @code{READ_RETURN_CONN}, 
     372@code{READ_RETURN_HASH} and @code{READ_RETURN2} and can be found in 
     373@file{engine/cache.h}. 
    461374 
    462375@node Persistent Cache 
    463376@section Persistent Reading Cache