1

I'm making a chess engine and I want to split the task of calculating all the different moves and positions to multiple cpu cores instead of just one but I can't get it to work. This is the code which is supposed to do it.

with concurrent.futures.ThreadPoolExecutor() as executor:
    #allChessPieces is an array of every possible chess piece the AI can move in the current board position
    for chessPiece in allChessPieces:
      executor.submit(engine,chessPiece,chessBoard)

The engine-function calculates every possible move that can be made with the given piece in the given board position and how the opponent can respond to that move and how the AI can respond to that move and so on. It gives an value for each move it calculates and stores the move alongside with its value in an array.

I want to be able to do these calculation for different pieces at the same time using mulitple cpu cores but right now I'm seeing python using only 14% of my cpu which is only 1 core.

Kruutteri
  • 25
  • 5
  • 2
    You're running into the [Global Interpreter Lock](https://wiki.python.org/moin/GlobalInterpreterLock). Python is essentially single-threaded for CPU-bound code which is written in Python. – Nick ODell Oct 21 '22 at 16:16
  • I see. Do you know if it would be possible to bypass GIL by switching from pythons built-in arrays to numpy arrays? (the board is now a 2d python array) – Kruutteri Oct 21 '22 at 16:29
  • 1
    use a `ProcessPoolExecutor` to achieve multiprocessing in Python. – Louis Lac Oct 21 '22 at 16:47

0 Answers0