I was inspired by a Technology blogger Gary Sims to build my own (small) super computer using Raspberry Pi’s and a message passing interface library. Following his Prime Numbers
example the key concepts are:
MPI library - Message passing interface, this is a standard way of sending blocks of data into a cluster from one node to another. For the examples below I used mpiexec which is free and open source.
Scatter - the MPI program will see each core in your cluster as something it can ask to do work. It will scatter the numbers accross the nodes to check if they are prime.
Gather - the MPI program will then gather the results. This is done on the master node that you started the command from.
In this video Gary explains what a Raspberry Pi Supercomputer Cluster
is and the key concepts of the message passing interface.
Setup Hardware / OS
Follow the OS Steps (For Cluster), then call them
node1
node2
The distro come with most applications already installed, some of the applications I used were git
, nano
and ssh
. Additional installs:
1 | sudo apt-get install git python-mpi4py keychain ansible |
Setup Passwordless SSH Access
See the Keychain
section on this passwordless SSH access post.
Hostfile
When running mpiexec python python_script.py
it can be run with the switch -hostfile HOSTFILENAME
, so the full command is then mpiexec -hostfile hostfile python python_script.py
The content of the hostfile is either the DNS name or IP address of all the nodes in the cluster.
1 | node1 |
Work Load
The PI’s need some problems to solve! Clone https://github.com/carlpaton/python-hoon to the same dir on both PI’s. Its important that the directorys are the same as the MASTER node (using MPI) will look in these directorys for the script to run.
Check mpi4py
The script prints hostnames and ranks (process id in MPI) from each MPI process in a sequential manner.
1 | cd python-hoon/mpiexec |
This script was copied from chainermn.readthedocs.io
Prime Numbers
Use parallel computing to find prime numbers in the range 0
to 10000
. This is a Python 2 script cloned from github.com/garyexplains
1 | cd python-hoon/mpiexec |
The results below show the time taken to complete the script primenumbers27.py
using 8 cores, 4 cores and finally just 1 core. The results should probably be averaged out or something but running just once like the below clearly shows speed improvements. Using Wifi and the speed of my SD cards would also come into play.
What Are Prime Numbers?
- Greater than 1
- Whole number with exactly two factors, itself and 1.
Some examples of prime numbers: 2,3,5,7,11,13,17,19
The number 4 is not a prime number because it can be divided evenly by 4, 2, and 1. The number 5 is a prime number because it cannot be divided evenly by any other numbers except for 5 and 1.
1 | 2 / 1 = 2 |
References
- https://github.com/garyexplains/examples
- http://www.circuitbasics.com/how-to-write-and-run-a-python-program-on-the-raspberry-pi/
- https://www.raspberrypi.org/documentation/usage/python/
- https://www.open-mpi.org/doc/v3.0/man1/mpiexec.1.php
- https://chainermn.readthedocs.io/en/v1.0.0b2_a/installation/troubleshooting.html