How to build a Raspberry Pi cluster
The instructions below show how to build a Raspberry Pi cluster with six Raspberry Pis (1× RPi 4, 3× RPi 3 B+, and 2× RPi 3 v. 1.2).
Sources
These notes are based on many excellent guides, including (but not limited to):
- Rasim Muratovic videos (Video 1, Video 2, Video 3, Video 4) and website
- The MagPi
- University of Southampton by Simon Cox
- Multiple Raspberry Pi Cluster builds by Element14
- Building a Raspberry Pi Cluster by Razvan Coloja
- Raspberry Pi Supercomputer Cluster by Gary Explains
- How to build your first Raspberry Pi cluster? by Patrick Fromaget
Installing and setting up Raspbian
- Download the Raspbian image from the Raspberry Pi website.
- Download and install Win32 Disk Imager from its SourceForge website.
- Burn the Raspbian image to the MicroSD card using Win32 Disk Imager (I used a 32 GB MicroSD).
- Enable SSH by creating an extension-less file named
sshin the root of the boot partition. - Insert the MicroSD card (with Raspbian burned on it) into the Raspberry Pi.
- Connect a network cable from the Raspberry Pi to your router/switch (the same router your PC is connected to).
- Connect a Micro USB cable from the Raspberry Pi to a power source.
- Download PuTTY from its website.
- Open a command prompt (type
cmdin the Windows search box). - Type
ping raspberrypiand hit Enter (write down your Pi’s IP address). - Open PuTTY, type the Pi’s IP address, and click Open.
- The login is pi and the password is raspberry.
- Type
sudo raspi-configand hit Enter. - Select “Update”.
- Expand the file system.
- Advanced options.
- Set the hostname to “Pi01”, or type
sudo nano /etc/hostname. - Exit raspi-config.
Installing analytical programs (Mathematica and R)
sudo apt-get -y update
sudo apt-get -y upgrade
sudo apt-get -y install wolfram-engine
sudo apt-get -y install r-base
To check how much space is left on the SD card, type df -h.
Installing MPICH
The MPICH 3.2 source is at http://www.mpich.org/static/downloads/3.2/mpich-3.2.tar.gz.
sudo mkdir mpich2
cd ~/mpich2
sudo wget http://www.mpich.org/static/downloads/3.2/mpich-3.2.tar.gz
sudo tar xfz mpich-3.2.tar.gz
sudo mkdir /home/rpimpi/
sudo mkdir /home/rpimpi/mpi-install
sudo mkdir /home/pi/mpi-build
sudo apt-get -y install gfortran
sudo /home/pi/mpich2/mpich-3.2/configure -prefix=/home/rpimpi/mpi-install
sudo make
sudo make install
cd ..
nano .bashrc
Paste the following line at the bottom of the file:
PATH=$PATH:/home/rpimpi/mpi-install/bin
I also added two optional commands to .bashrc after the PATH: df -h (to show the space left on the MicroSD) and htop (to show the interactive process viewer).
Then hold CTRL and press “6” “x”, let go of CTRL, and press “y” and Enter.
sudo reboot
mpiexec -n 1 hostname
Installing MPI4PY
The mpi4py 2.0.0 source is at https://bitbucket.org/mpi4py/mpi4py/downloads/mpi4py-2.0.0.tar.gz.
wget https://bitbucket.org/mpi4py/mpi4py/downloads/mpi4py-2.0.0.tar.gz
sudo tar -zxf mpi4py-2.0.0.tar.gz
cd mpi4py-2.0.0
sudo apt-get -y install python-dev
python setup.py build
sudo python setup.py install
export PYTHONPATH=/home/pi/mpi4py-2.0.0
mpiexec -n 5 python demo/helloworld.py
Installing nmap
sudo apt-get -y update
sudo apt-get -y install nmap
Creating an image file of Pi01 and duplicating it to the other MicroSDs
- Remove the MicroSD from the Raspberry Pi.
- Create an image using Win32 Disk Imager.
- Copy it to the other MicroSDs.
- Insert the duplicate MicroSDs into the additional Raspberry Pi nodes.
Getting the current IP
ifconfig
Scanning the subnet for Pi0x
sudo nmap -sn 192.168.1.*
Changing the Pi0x names
Let’s assume that the IP addresses are:
- Pi01: 192.168.1.1
- Pi02: 192.168.1.2
- Pi03: 192.168.1.3
- Pi04: 192.168.1.4
- Pi05: 192.168.1.5
- Pi06: 192.168.1.6
Do the following for Pi02, Pi03, Pi04, Pi05, and Pi06 (the password is raspberry):
ssh pi@192.168.1.2
sudo nano /etc/hostname
or
sudo raspi-config
- Scroll down to Advanced and hit Enter.
- Scroll down to Hostname and hit Enter.
- Change the name (Pi01, Pi02, Pi03, Pi04) and hit Enter.
- Press Tab until Finish is selected, then hit Enter.
Editing the nodes file
Change the IP addresses below to reflect the IP addresses of the Pi0x, and enter them in the nodes file:
nano nodes
192.168.1.1
192.168.1.2
192.168.1.3
192.168.1.4
192.168.1.5
192.168.1.6
Hold CTRL, press 6, x, press y, hit Enter (or CTRL+O and CTRL+X).
Testing the nodes
mpiexec -n 1 hostname
mpiexec -f nodes -n 6 hostname
You should get errors on the second command, because Pi01 does not have access to the other nodes. So, do the following.
Adding keys to all Pi0x
Pi01
ssh-keygen
cd ~
cd .ssh
cp id_rsa.pub Pi01
Pi02
ssh pi@192.168.1.2
ssh-keygen
cd .ssh
cp id_rsa.pub Pi02
scp 192.168.1.1:/home/pi/.ssh/Pi01 .
cat Pi01 >> authorized_keys
exit
Pi03
ssh pi@192.168.1.3
ssh-keygen
cd .ssh
cp id_rsa.pub Pi03
scp 192.168.1.1:/home/pi/.ssh/Pi01 .
cat Pi01 >> authorized_keys
exit
Pi04
ssh pi@192.168.1.4
ssh-keygen
cd .ssh
cp id_rsa.pub Pi04
scp 192.168.1.1:/home/pi/.ssh/Pi01 .
cat Pi01 >> authorized_keys
exit
Pi05
ssh pi@192.168.1.5
ssh-keygen
cd .ssh
cp id_rsa.pub Pi05
scp 192.168.1.1:/home/pi/.ssh/Pi01 .
cat Pi01 >> authorized_keys
exit
Pi06
ssh pi@192.168.1.6
ssh-keygen
cd .ssh
cp id_rsa.pub Pi06
scp 192.168.1.1:/home/pi/.ssh/Pi01 .
cat Pi01 >> authorized_keys
exit
Appending the authorized keys to Pi01
From Pi01, do the following:
scp 192.168.1.2:/home/pi/.ssh/Pi02 .
cat Pi02 >> authorized_keys
scp 192.168.1.3:/home/pi/.ssh/Pi03 .
cat Pi03 >> authorized_keys
scp 192.168.1.4:/home/pi/.ssh/Pi04 .
cat Pi04 >> authorized_keys
scp 192.168.1.5:/home/pi/.ssh/Pi05 .
cat Pi05 >> authorized_keys
scp 192.168.1.6:/home/pi/.ssh/Pi06 .
cat Pi06 >> authorized_keys
Checking the new nodes
cd ~
mpiexec -f nodes -n 6 hostname
Running Python code on the cluster
- Create the
prime.pyfile (listed below) in/home/piof Pi01. - Then copy it to the other nodes as follows:
scp prime.py 192.168.1.2:/home/pi
scp prime.py 192.168.1.3:/home/pi
scp prime.py 192.168.1.4:/home/pi
scp prime.py 192.168.1.5:/home/pi
scp prime.py 192.168.1.6:/home/pi
mpiexec -f nodes -n 4 python prime.py
Then copy primality_cluster_test1.py (see below) to all nodes, and run the following commands on Pi01:
scp primality_cluster_test1.py 192.168.1.2:/home/pi
scp primality_cluster_test1.py 192.168.1.3:/home/pi
scp primality_cluster_test1.py 192.168.1.4:/home/pi
scp primality_cluster_test1.py 192.168.1.5:/home/pi
scp primality_cluster_test1.py 192.168.1.6:/home/pi
This command runs only on node 1 (Pi01), using all four cores (-n 4). It took 7.738 seconds to find 109926 primes.
time mpiexec -n 4 python primality_cluster_test1.py
This one runs on all nodes, using all twenty-four cores (-n 24). It took 5.952 seconds to find 109926 primes. Remember that Pi01 (node 1) is a Raspberry Pi 4. When tested on Pi06 (node 6), which is a Raspberry Pi 3 v. 1.2, it took 25.4 seconds! So combining old and new versions of the Raspberry Pi may not be the ideal approach for building a cluster.
time mpiexec -hostfile nodes -n 24 python primality_cluster_test1.py
Use htop to view the processes on each node.
Sample Python code (prime.py)
Copy it exactly as shown below.
def is_prime(n):
status = True
if n < 2:
status = False
else:
for i in range(2,n):
if n % i == 0:
status = False
return status
for n in range(1,101):
if is_prime(n):
if n==97:
print n
else:
print n,",",
How a Raspberry Pi cluster should work, by Gary Explains
https://www.youtube.com/watch?v=VzcarXuVUvU&t=639s
The file primality_cluster_test1.py is here.
Adding an external hard disk
Some additional information by Alasdair Allan and Coobird’s Lab (Part 1, Part 2, Part 3).