Marcus Johnson
Published © GPL3+

Raspberry Pi Supercomputer with MPI

This project shows communication between Raspberry pi nodes in a cluster using the message passing interface library standard.

IntermediateFull instructions provided8,965
Raspberry Pi Supercomputer with MPI

Things used in this project

Hardware components

Raspberry Pi 2 Model B
Raspberry Pi 2 Model B
I used 3 Pis for my project but as long as you have at least 2, you're fine.
×3
LED (generic)
LED (generic)
×3
Resistor 221 ohm
Resistor 221 ohm
×3

Story

Read more

Schematics

LED Circuit

Mpi

Code

installMPI.sh

SH
Shell script that installs dependency files for mpi4py and mpi4py itself
#!/bin/sh

#Download all necessary dependencies and mpi4py
sudo apt-get install python-dev
sudo apt-get install python-mpi4py

#Create directory for source/demo files for mpi4py
mkdir mpi4py
cd mpi4py

#Download tarball file and extract source/demo files for mpi4py
curl -k -O https://mpi4py.googlecode.com/files/mpi4py-1.3.1.tar.gz

#MPI will already be installed at this point. This is only for having the source code, documentation, and demos for your convenience
tar xvfz mpi4py-1.3.1.tar.gz 

MachineFile example

Plain text
This is an example of how the machinefile should look for MPI. It should be placed on all nodes.
192.168.1.123
192.168.1.124
192.168.1.126

ledDemo.py

Python
"""
LED Demo
""" 

from mpi4py import MPI
import RPi.GPIO as GPIO

# Set up GPIO
GPIO.cleanup()
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT) #master - Green LED
GPIO.setup(18, GPIO.OUT) #Slave 1 - Purple LED
GPIO.setup(23, GPIO.OUT) #Slave 2 - Red LED 

# MPI variables for communication, rank, size, and name of the node
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()
name = MPI.Get_processor_name()

total_requests = 2500 

# This is the master node.
if rank == 0:
    request = 0
    process = 1

    # Send the first batch of processes to the nodes.
    while process < size and request < total_requests:
        comm.send(request, dest=process, tag=1)
        print "Sending request",request,"to process",process
        request += 1
        process += 1

    # Wait for the data to come back
    received_processes = 0
    while received_processes < total_requests:
        process = comm.recv(source=MPI.ANY_SOURCE, tag=1)
        print "Received data from process", process 
        if (process == 1):
	    GPIO.output(18, True)
	    GPIO.output(23, False)
	elif (process == 2):
	    GPIO.output(23, True)
	    GPIO.output(18, False)
        received_processes += 1

        if request < total_requests:
            comm.send(request, dest=process, tag=1)
            print "Sending request",request,"to process", process 
            GPIO.output(17, True)
            request += 1

    # Send the shutdown signal
    for process in range(1,size):
        comm.send(-1, dest=process, tag=1)
    print "End"

# These are the slave nodes, where rank > 0. They receive the request from the master node and simply send a response back 
else:
    while True:
        start = comm.recv(source=0, tag=1)
        if start == -1: break
        comm.send(rank, dest=0, tag=1)


#Clean up GPIO activities after all processing takes place
GPIO.cleanup()

Credits

Marcus Johnson

Marcus Johnson

8 projects • 22 followers
Software engineer with professional experience creating, maintaining, integrating, and testing software in Windows and Unix environments.
Contact

Comments