Parallel search in Javascript question

Discussion of chess software programming and technical issues.

Moderators: hgm, Dann Corbit, Harvey Williamson

User avatar
maksimKorzh
Posts: 771
Joined: Sat Sep 08, 2018 5:37 pm
Location: Ukraine
Full name: Maksim Korzh

Parallel search in Javascript question

Post by maksimKorzh »

Hi guys

I'm currently working on summarizing of all of my previous work into a single javascript chess library intended to provide an embeddable chess board (so one can include it into his website) empowered by chess engine analysis. And yeah, I'm NOT covering it in youtube series believe it or not (this was sarcasm). Despite normal people would most likely separate GUI and analysis (literally engine) parts into 2 files (instances) me, as a code monkey king bundle it all together. But obviously we can't reuse the single board instance for both - GUI and searching in a non-blocking manner, so I need to instantiate them in separate somehow. The solution I've considered is inspired by the encapsulation technique introduced in chess.js https://github.com/jhlywa/chess.js/blob/master/chess.js
The reason why I consider it is because I can leave my "all global variables" engine structure, which most people hate, but encapsulate it within a single object. The snippet below shows the architecture:

Code: Select all

// library
var Chess = function() {
  // global to this object/function scope board array
  var board = new Array(128);
  ...
  // render board to HTML
  render_board() { ... }
  
  // search position
  function search() { ... }
  
  // public API
  return {
    render_board: function() { return render_board(); }
    search: function() { return search(); }
  }
}
Now the main issue is that obviously we don't want board we use for GUI to be affected by search, so here's the solution:

Code: Select all

// create board instance
var board = new Chess()  // it still can search but used only for handling GUI, move validation, etc.

// create engine instance
var engine = new Chess() // it still can work with GUI for instance is the same as above, but this one is used for search

// render board
board.render_board()   // draws chess board on HTML page

// async position search
var best_move = new Promise(function() { engine.search() });  // searches for best move without blocking GUI thread

// interact with GUI when promise returns
best_move.then(/* interact with board object when search is done */)
So what I do above is simple - just creating 2 instances of the same object and then use one for GUI purposes and another for search purposes and interact between them (it's not implemented yet, just an idea)

Now the QUESTION ARISES:
Can I instantiate the number of Chess() object instances equal to the number of moves in the root position and search in parallel in this way?
User avatar
hgm
Posts: 27700
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Parallel search in Javascript question

Post by hgm »

That would be a very inefficient way to parallelize a chess program. Usually engines only spend 50-60% of their thinking time on the non-best moves. I.e., they take only 1 to 1.5 times as much effort as the PV move. If you had 30 moves in the root, and searched them all independently, it would require 30 times as much effort as the PV move.
User avatar
maksimKorzh
Posts: 771
Joined: Sat Sep 08, 2018 5:37 pm
Location: Ukraine
Full name: Maksim Korzh

Re: Parallel search in Javascript question

Post by maksimKorzh »

hgm wrote: Thu Nov 26, 2020 7:09 pm That would be a very inefficient way to parallelize a chess program. Usually engines only spend 50-60% of their thinking time on the non-best moves. I.e., they take only 1 to 1.5 times as much effort as the PV move. If you had 30 moves in the root, and searched them all independently, it would require 30 times as much effort as the PV move.
Crystal clear now. Thank you.