Uri Blass wrote:I have problems to post in the chess computer club because of changing the server but I read claims by tord that it can be easy to implement parallel search in toga simply by copy and paste parts of glaurung and I simply do not understand how people think that it is possible to do it so fast.
Toga has global variables and I understood from Dann corbit that in order to use parallel search you need to get rid of parallel variables.
You don't necessarily have to remove all global variables. The problem is only variables which change during the search. The most obvious example is the variable(s) holding the board data structure, a single global board clearly won't work if there are several CPUs searching simultaneously.
There are also problems with things like pawn hash tables: If two threads accidentally try to simultaneously update the same pawn hash table entry, this will almost certainly cause the contents of this pawn hash table entry to be corrupted. This can cause non-reproducible bugs which can be tricky to find. There are two ways to solve such problems: To have a separate pawn hash table for each thread, or to use locks to prevent two threads from writing to the pawn hash table simultaneously. The former solution consumes more space, but the latter will probably be slower.
"Pseudo-constants", i.e. global variables which never change during a search, can remain global.
If I recall correctly, Fruit doesn't have a lot of global variables which are not pseudo-constants, so adding Glaurung's parallel search should be easy. In some other programs (Phalanx, for instance), it would take more work.
I do not know a way to do something like that in few hours and in case that it is possible to do it then I would like to get step by step instructions how to do it.
OK, I'll try:
- Find all the global variables in the code, and see if there are some that are modified during the search, in such a way that there could be problems if several threads tries to modify the contents at the same time. For each such variable, either make the variable an array indexed by the number of threads, or protect it with a lock.
- Add a new integer parameter named threadID to the search and evaluation function, and to all other functions which for some reason needs to know which thread it is called from. The evaluation function needs to know this because it looks up the current position in the pawn and material hash tables, and you will probably want to have separate pawn and material hash tables for each thread. The search function needs to know which thread it is called from because it calls the evaluation function, and therefore must be able to tell the evaluation function its threadID.
- Copy the Thread and SplitPoint types from Glaurung (thread_t and split_point_t in Glaurung 2). In the Thread type, remove the variable printCurrentLine, because Fruit doesn't support the UCI_ShowCurrLine feature. In SplitPoint, modify the contents to match the parameters and local variables in Fruit's search function.
- Copy the rest of Glaurung's parallel search code. Modify the split() function to work with your modified SplitPoint structs, and sp_search/sp_search_pv to look like Fruit's regular search function. Because Fruit, unlike Glaurung 2, uses the same search function for PV and non-PV nodes, you can probably combine split and split_pv to one function, and similarly with sp_search and sp_search_pv.
bob wrote:No way it is "easy" to add a parallel search to any engine.
I'd say it is - at least as long as you don't care much about how well the search scales on more than 4 CPUs (I don't care about this before machines with 8 CPUs or more become common and affordable).
Writing a parallel search used to be very hard, but thanks to the efforts of pioneers like you, the problem has become so well understood that it is now rather straightforward to implement. It is just like calculus, which was understood only by the mathematical elite back in the days of Newton and Leibniz, but which is successfully taught to millions of young students with average mathematical skills today.
bob wrote:But cutting/pasting parts of glaurung would certainly be far easier than writing one from scratch.
Easier, but also less fun.
I should also warn that Glaurung's parallel search can only be used in programs released under the GNU General Public License. For Toga, this is obviously not a problem.
Tord