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
225 225 option @pxref{Invoking GNU Go}. 226 226 227 227 The hash table is created once and for all at the beginning of 228 the game by the function @code{ hashtable_new()}. Although hash228 the game by the function @code{reading_cache_init()}. Although hash 229 229 memory is thus allocated only once in the game, the table is 230 230 reinitialized 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()}. 232 232 233 233 @menu 234 234 * Hash Calculation:: Calculation of the hash value 235 235 * Hash Organization:: Organization of the hash table 236 * Hash Structures:: Structures in @file{hash.h}237 236 @end menu 238 237 239 238 @node Hash Calculation … … 249 248 @enumerate 250 249 @item First we define a @dfn{go position}. This positions consists of 251 250 @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 as251 @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 254 253 the empty point where the last single stone was situated before 255 254 it was captured. 256 255 @end itemize 257 256 257 In addition to that we include some more information into the hash code, 258 that 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 262 the 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 258 267 It is not necessary to specify the color to move (white or black) 259 268 as part of the position. The reason for this is that read results 260 269 are stored separately for the various reading functions such as 261 270 @code{attack3}, and it is implicit in the calling function which 262 271 player is to move. 263 272 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) 274 we generate tables with random numbers for each: function, komaster 275 status or position on a board. 271 276 These random numbers are generated once at initialization time and 272 277 then used throughout the life time of the hash table. 273 278 274 279 @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). 280 which are applicable for the position. If extra information is needed 281 it is passed by a function as a hash code which is XORed into the 282 resulting hash key. 277 283 @end enumerate 278 284 279 285 @node Hash Organization 280 286 @subsection Organization of the hash table 281 287 282 The hash table consists of 3 parts:283 284 @cindex Hash node285 @cindex Read result286 287 @itemize @bullet288 @item An area which contains so called @dfn{Hash Nodes}. Each hash node289 contains:290 @itemize @minus291 @item A go position as defined above.292 @item A computed hash value for the position293 @item A pointer to Read Results (see below)294 @item A pointer to another hash node.295 @end itemize296 297 @item An area with so called Read Results. These are used to store298 which function was called in the go position, which string was299 under attack or to be defended, and the result of the reading.300 301 Each Read Result contains:302 @itemize @minus303 @item the function ID (an int between 0 and 255), the position of the304 string under attack and a depth value, which is used to305 determine how deep the search was when it was made, packed into306 one 32 bit integer.307 @item The result of the search (a numeric value) and a position to308 play to get the result packed into one 32 bit integer.309 @item A pointer to another Read Result.310 @end itemize311 312 @item An array of pointers to hash nodes. This is the hash table313 proper.314 315 @end itemize316 317 When the hash table is created, these 3 areas are allocated using318 @code{malloc()}. When the hash table is populated, all contents are taken319 from the Hash nodes and the Read results. No further allocation is320 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 totally322 emptied, at which point it can be used again as if newly initialized.323 324 @findex hashtable_search325 When a function wants to use the hash table, it looks up the current326 position using @code{hashtable_search()}. If the position doesn't already327 exist there, it can be entered using328 329 @findex hashtable_enter_position330 @code{hashtable_enter_position()}.331 332 @findex hashtable_enter_position333 Once the function has a pointer to the hash node containing a334 function, it can search for a result of a previous search using335 @code{hashnode_search()}. If a result is found, it can be used, and336 if not, a new result can be entered after a search using337 @findex hashnode_new_result338 @code{hashnode_new_result()}.339 340 Hash nodes which hash to the same position in the hash table341 (collisions) form a simple linked list. Read results for the same342 position, created by different functions and different attacked or343 defended strings also form a linked list.344 345 This is deemed sufficiently efficient for now, but the representation346 of collisions could be changed in the future. It is also not347 determined what the optimum sizes for the hash table, the number of348 positions and the number of results are.349 350 @node Hash Structures351 @subsection Hash Structures352 353 288 The 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} 362 290 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. 291 The hash table consists of an array of pointers to hash entries and 292 variable that defines number of the entries. 366 293 367 294 @example 368 295 typedef 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; 381 299 382 struct read_result_t *next; 383 @} Read_result; 300 Transposition_table ttable; 384 301 @end example 385 302 386 The data1 field packs into 32 bits the following fields: 303 @cindex Hash entry 387 304 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: 305 The @dfn{Hash Entry} consists of two @dfn{Hash Nodes}. One is the 306 most reliable node and the second is the newest. The reliability is 307 computed based on cost of the function, which created a node and 308 depth of analysis for that node (the lower depth the more reliable 309 results). 399 310 400 311 @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 312 typedef struct @{ 313 Hashnode most_reliable; 314 Hashnode newest; 315 @} Hashentry; 408 316 @end example 409 317 410 The @code{komaster} and @code{(kom_pos)} field are 411 documented in @xref{Ko}. 318 @cindex Hash node 412 319 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. 320 Each hash node contains a computed hash value (as defined above) 321 and @dfn{Read Results}. 419 322 420 323 @example 421 typedef struct hashnode_t @{ 422 Hash_data key; 423 Read_result * results; 424 struct hashnode_t * next; 324 typedef struct @{ 325 Hash_data key; 326 HASHNODE_DATATYPE data; /* 32-bit value */ 425 327 @} Hashnode; 426 328 @end example 427 329 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 431 331 432 There is also a pointer to another hash node which is used when 433 the nodes are sorted into hash buckets (see below). 332 Read Results are used to store which function was called in the go 333 position, which string was under attack or to be defended, and the 334 result of the reading. The result is packed into one 32 bit integer. 335 Each Read Result contains: 336 @itemize @minus 337 @item a remaining (to the max depth) depth value, which is used to 338 determine 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 341 numeric value) (4 bits); 342 @item a move to play to get the result (10 bits). 343 @end itemize 434 344 435 345 @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 439 353 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. */ 354 When the hash table is created, the hash entries are allocated using 355 @code{malloc()}. After that, no further allocation is 356 done and when all nodes or results are used, the hash table is full. 357 Nothing is deleted, instead nodes are replaced by newer or more 358 reliable nodes (when collision occurs). 443 359 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 361 When a function wants to use the hash table, it looks up the current 362 position using @code{tt_get()}. If the position doesn't already 363 exist there, it can be entered using 449 364 450 The hash table consists of three parts: 365 @findex tt_update 366 @code{tt_update()}. 451 367 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 369 There are several macros which store the result and return it from 370 a 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}. 461 374 462 375 @node Persistent Cache 463 376 @section Persistent Reading Cache
