2017-12-31
Cluster Computing with some Raspberry Pis
Please, skim the whole article first, at least read the headers. Work your way through, step by step, reading the next chapter once before you continue :)
What you need:
two or more Raspberry Pis + SD Cards + Power + Ethernet Cables
a Router and/or a Switch
Preparing a Master Image
Current Raspbian (2017-11) ships with Python 3.5. All we need to do now, is set up an MPI. Download Raspbian
Set some time aside for the following - if concurrency isn't your thing and you need to sit in front of your screen ;-) (a quality, class 10 SD card can plough through the setup process)
Setup a fresh SD with Raspbian Lite.
when finished, enable SSH by navigate to the SD and add a plain file called
ssh.boot and ssh into your Raspberry Pi
$ sudo apt update && sudo apt upgrade$ sudo raspi-configset localization and stuff if you wantoptional/fyi: limiting the dumped image size when using dd can be done by passing the count argument.
dd: limit image size withcount=3700__.In my case dumping looks like: (how to backup your Raspberry Pi):
sudo dd bs=1m if=/dev/rdiskX of=RaspbianLitePresetImageDE_8GB_PiMaster.img count=3700Install mpich and python3-dev
$ sudo apt update && sudo apt upgrade && sudo reboot now$ sudo apt-get install libssl-dev libcr-dev python3-dev python3-pip mpich libatlas-base-dev libatlas-dev libatlas3-base cython && sudo apt update --fix-missing && sudo reboot now
Download the mpi4py package and unpack.
$ wget https://bitbucket.org/mpi4py/mpi4py/downloads/mpi4py-3.0.0.tar.gzif the download link is broken google (this: mpi4py source)
$ tar zxf mpi4py-3.0.0.tar.gz$ cd mpi4py-3.0.0/$ sudo python3 setup.py build --mpicc=/usr/bin/mpicc$ sudo python3 setup.py install
Add Pandas. It's more fun with Pandas. Pandas brings Numpy.
$ python3 -m pip install --user pandas && sudo reboot now
set a new password, so your cluster will be protected and has the same one over all:
$ passwd
Cloning
shutdown and clone the SD Card to your workstation (help)
load the image to a second SD card, then cable up and boot both Raspberry Pis
Master
ssh into one of the Pi - this will be your master node from now on.
change hostname from raspberrypi to pimaster:
$ sudo nano /etc/hostnamereboot, reconnect (with new hostname)
create an ssh key without password (default path, no password)
$ ssh-keygen -t rsa -b 4096copy your public key
$ cat ~/.ssh/id_rsa.puband save it temporarily in a word file on your workstationoptional: shutdown and backup SD card (you might need to replace your master when something breaks)
Slave/Node
ssh into your slave machine
$ mkdir .sshpaste your master's public keys into
/home/pi/.ssh/authorized_keys:$ nano .ssh/autorized_keyschange hostname to pinode00
$ sudo nano /etc/hostnameshutdown and backup SD card, as this is the "master clone" for all nodes/slaves to come
put the SD back in the Slave Raspberry and boot up
change hostname to pinode01
$ sudo nano /etc/hostname
Connect Master and Slave
ssh to master then run $ ssh pi@pinode01 making the machines known to each
other
Adding Nodes
You can add further slaves, using the node image you created earlier. Be sure to add the nodes one by one.
ssh into master, and from there ssh to pi@pinode00
rename your new slave's hostname from pinode00 to the next number that makes sense :-)
$ sudo nano /etc/hostnamereboot slave
Run Your Cluster
to utilize all members of the cluster, ssh to your master and add a hostfile to the directory your script is in, looking like this for one master and one slave:
# content of hostfile
pimaster
pinode01
# add all cluster member with their hostnameDemo / Test
navigate to
/home/pi/mpi4py-3.0.0/demoon your mastercreate your hostfile (as above)
$ mpiexec --hostfile hostfile python3 helloworld.py
all your nodes should say hello :D
Errors / Help / Network
If mpiexec throws proxy or connection errors, you might need to check your router for duplicate hostnames in its network settings. This caused major hickups in my run network (AVM Fritz Box 7490).
← Previous Post | Next Post →