I recently created a Raspberry Pi cluster using 4 Raspberry Pi mini computers together to run the Stockfish chess engine. Here is what I learned.

Introduction

This page gives a high-level overview of the whole process. I have a series of posts about the cluster and stockfish including:

Why?

First off, I wanted a remote server I could connect to when analyzing chess games. I could just run stockfish on my laptop, where I do most of my work, but the heavy CPU usage of stockfish would drain my battery pretty fast.

Since I had some Raspberry Pi computers lying around, and I always wanted to try to create a cluster with them, I thought this was the perfect chance.

My goal was to create a cluster that would be at least as fast as my laptop, which is can process about 3 million nodes/second. That’s 3 million chess positions analyzed.

Setting Up the Raspberry Pis

Once I had all the parts, it was time to do the initial setup of configuring the raspberry pis. There are plenty of guides out there for that. However, the basic steps I did for each pi were:

  1. Install RaspbianOS Lite (no desktop environment)
  2. Replace and delete the default pi user
  3. Add ssh keys so I could log into each pi easily from my laptop
  4. Update all the packages

Since I was using a mix of old pis, I needed to check some specs like memory. If you need to do that, you could use these commands:

free -h
cat /proc/cpuinfo

Get Them Talking

Next I needed a way for each node (raspberry pi) to talk to each other. Actually, I only really needed the master node to be able to talk to each worker node.

Create an SSH Key

On each node, do this command, accepting all the defaults:

ssh-keygen -t rsa

Copy the Keys

Then you need to install that key to each node you want to talk to. You do not need to have each worker know about each other.

  • master node to each worker node (3 times)
  • 3 worker nodes to the master

To do this, use versions of this command:

ssh-copy-id matt@stockfishworker1

Build Open MPI

MPI is the standard for turning 4 raspberry pis into 1 “super” computer (not all that super). I built my own version of this to get the latest:

Building Open MPI

Build Stockfish

Finally, something about chess. Like OpenMPI, using the apt-get version of stockfish left with with an old version (version 9 instead of 14). But that isn’t the worst of it. To run stockfish on the cluster, you need to use speicifc code that isn’t available in the normal execution. So, again I had to build my own:

Building Stockfish

Create a Host File

This is all done only on the master node.

First, you need to create a file that tells OpenMPI where all the hosts are. I called it cluster_hosts and it looks like this:

127.0.0.1
192.168.1.91
192.168.1.92
192.168.1.93

The master node should just be 127.0.0.1.

If you want to run more than one stockfish process on each node, you could have a file that looks like this:

127.0.0.1 slots=3
192.168.1.91 slots=3
192.168.1.92 slots=3
192.168.1.93 slots=3

Where slots is the maximum number of processes to run. In testing performance, and from the documentation, I found that one process with more threads was more performant than multiple processes. My guess is that the communication between the processes adds too much overhead.

Run The Cluster

Now, we are finally ready to get this cluster running stockfish. Here is the command I use:

/home/matt/bin/mpirun --hostfile /home/matt/cluster_hosts -map-by node -np 4 /home/matt/bin/stockfish
  • --hostfile is the definition of hosts
  • -map-by node says to do 1 node at a time (0, 1, 2, 3, 0, 1, 2, 3) instead of filling up one node (0, 0, 0, 1, 1, 1). As I said above, I’m only running 1 process per node anyway, but this is set just in case I change my mind.
  • -np 4 The number of processes to start
  • /home/matt/bin/stockfish is the command to execute across the cluster

Hopefully you will see the standard welcome message:

Stockfish 14.1 by the Stockfish developers (see AUTHORS file)

Now, when you run commands (try bench), it will have all 4 nodes of the cluster handle the load.

Running Remotely

If you plan to run this from a remote computer, there are a couple more tips. First, I would create a executable script that you can call. I created one called cluster that looks like this:

#!/bin/sh
/home/matt/bin/mpirun --hostfile /home/matt/cluster_hosts -map-by node -np 4 /home/matt/bin/stockfish

I would be sure to include the full paths for everything. I know I ran into problems without that.

Then, from a remote computer, you can just run something like this:

ssh -i ~/.ssh/id_rsa matt@11.222.33.444 cluster

If you want to try to connect with Chessbase or another program, I’d recommend you checkout my post on SshEngine, which is a utility I wrote to handle this.

Follow Up

If you have any questions, find me on twitter