项目作者: theryanle

项目描述 :
Building a super-computer with Raspberry Pis and MPI4Py
高级语言: Shell
项目地址: git://github.com/theryanle/super-pi.git
创建时间: 2021-01-28T06:07:58Z
项目社区:https://github.com/theryanle/super-pi

开源协议:

下载


Getting Started

Source: https://raspberrytips.com/raspberry-pi-cluster/

Requirements

  • 2 or more Raspberry Pi (the Raspberry Pi 3B is recommended)
  • 2 or more SD Cards
  • 5V power cables
  • A 5-ports gigabit switch to connect all Pis together
  • A Network cable for each Pi (wireless is possible, but not recommended)

Setup

Install Raspian OS

  1. Insert the SD card then run
    1. $ diskutil list
  2. Unmount the target disk
    1. $ diskutil unmountDisk /dev/diskN (where N is the target disk)
  3. Copy the image
    1. $ sudo dd bs=1m if=path/to/your/image.img of=/dev/rdiskN; sync (where N is the target disk)
  4. Eject the SD card
    1. $ sudo diskutil eject /dev/rdiskN (where N is the target disk)

Configure the Raspberry Pi for the first time

  1. Change the hostname
    1. $ raspi-config > System Options > Hostname > (master)
  2. Enable SSH
    1. $ raspi-config > Interface Options > SSH > Enable
  3. Reboot

Install the Master Pi

Note: This installation step can take up to 30-45 minutes to complete

  1. Run the installation script
    1. $ cd scripts/ && chmod +x install_master.sh
    2. $ sudo install_master.sh
  2. Run mpiexec from anywhere, anytime
    1. $ sudo nano .bashrc
    Add these two lines to the bottom of the (bashrc) file
    1. export PATH=$PATH:/opt/mpi/bin
    2. export PYTHONPATH=/home/pi/mpi4py-2.0.0
  3. Reboot

Duplicate the Master Pi

!Remember to change the hostname on each new node

  1. Back up the master image
    1. $ diskutil list
    2. $ sudo dd bs=1m if=/dev/rdiskN of=master-pi.dmg (where N is the target disk)
  2. Restore/Copy image to other sd-card
    1. $ diskutil unmountDisk /dev/diskN
    2. $ sudo dd bs=32m if=master-pi.dmg of=/dev/rdiskN (where N is the target disk)

Create an IP list

  1. Connect and power up all the Pis
  2. Scan for IP addresses from master
    1. $ sudo apt -y install nmap
    2. $ nmap -sP 192.168.X.* (where X is the subnet)
  3. Create a file to store all the Pis IP addresses
    1. $ cd /home/pi
    2. $ nano cluster
  4. Copy all IP addresses to this file (including the masters ip)
    !To keep things in order, setup static IPs on all the nodes
    https://www.raspberrypi.org/documentation/configuration/tcpip/
    1. 192.168.1.2
    2. 192.168.1.3
    3. 192.168.1.4
    4. ...

SSH Keys Exchange

!We need to allow the master node to connect to any other nodes in the cluster via SSH and each node to master (bidirectional) without using passwords. Repeat the following steps for all the nodes (including master)

  1. Generate an SSH key
    1. $ ssh-keygen -t rsa
  2. Copy SSH public key to other nodes
    1. $ scp /home/pi/.ssh/id_rsa.pub pi@192.168.1.N:/home/pi/<piX>.pub (where N is the target IP address, and X is the source node i.e master, pi01, pi02 , ...)
  3. Add the copied public key to authorized_keys file
    3a. Create the .ssh/authorized_keys file if not already exists
    1. $ touch /home/pi/.ssh/authorized_keys
    2. $ chmod 600 /home/pi/.ssh/authorized_keys
    3b. Add other node’s public key
    1. $ cat <piX>.pub >> .ssh/authorized_keys (where X is the public key of the other node i.e master, pi01, pi02 , ...)
  4. Test SSH without password
    !Repeat this step for all the nodes
    From master node
    1. $ ssh pi@192.168.1.N (where N is the target IP address)

Usage

!Run from the master node

  1. Basic command
    !The command below should print out the hostname
    1. $ mpiexec -n 4 hostname
  2. Run a python script
    !Each node needs to have the same script and at the same location
    1. $ mpiexec --hostfile cluster -n 6 python mpi4py-2.0.0/demo/helloworld.py