Try to use up the Raspberry Pi 2's 4-core CPU with Parallel Python

Motivation

The newly released Raspberry Pi 2 has a 4-core CPU. It's a good idea, so I'd like to do distributed processing and use up 4 cores to the full.

What i did

I ran a simple distributed processing program using the distributed processing framework Parallel Python and compared the processing speeds of the old model and the new model. We also checked the usage rate of each core with mpstat.

Conclusion

Compared to the old model, when 4 cores are used up, it is ** about 8.5 times **, and even with 1 core alone, it is ** about 2.4 times ** faster. When 4 cores were used up, the result of mpstat was 100% of all cores.

Impressions

I am satisfied because I have used up 4 cores. Also, it turned out that even one core is fast enough. [There is a report that the desktop screen and Chrome browser start up 2.2 to 2.6 times faster](http://itpro.nikkeibp.co.jp/atcl/column/14/565123/020600005/?ST= oss & P = 2) Since the speed improvement rate is close, the performance improvement for one core may have contributed significantly.

Experimental method and results

Execution of distributed processing and measurement of processing time

Given processing

Count an even number of natural numbers from 1 to N. The counting method is determined by finding the remainder divided by 2 one by one. A natural number from 1 to N is divided into k, and each is shared by k cores for distributed processing. It's a problem that can be solved with one shot if you do N / 2, but I apologize to Raspberry Pi first for letting me do a boring job.

This time, N = 10 million.

Source code

pptest.py


# coding: UTF-8

import pp

#Determine if n is a multiple of m
def ismultiples(n,m):
    return n % m == 0

#from n1 to n2(Including n2)Find out how many multiples of m are among the natural numbers of
def sum_multiples(n1,n2,m):
    cnt = 0
    for x in range(n1,n2+1):
        if ismultiples(x,m):
            cnt += 1
    return cnt

#IP address of Raspberry Pi to process
ppservers = ("192.168.1.241","192.168.1.241","192.168.1.241","192.168.1.241",) #Use 4 cores
# ppservers = ("192.168.1.241",) #1 core used

#Maximum value of natural numbers
N = 10000000
#Multiples you want to count
M = 2
#Number of nodes
num_node = len(ppservers)

#Create a server object by registering the connection destination node
job_server = pp.Server(0, ppservers)
#Task generation
jobs = []
for i in range(num_node):
    #Find the range of natural numbers handled by the i-th node
    indSt = N/num_node*i+1
    if (i==num_node-1):
        indEnd = N
    else:
        indEnd = indSt+N/num_node-1
    #Throw a task to a node
    jobs.append(job_server.submit(sum_multiples, (indSt, indEnd, M), (ismultiples,), ("math",)))
    print("task%d args:(%d,%d,%d)" % (i,indSt,indEnd,M))  

#Collect execution results. sum_mutiples()Image to get the return value of.
#If the processing on the node is not finished yet, the processing here will be blocked until it is finished..
result = 0;
for i in range(num_node):
    result += jobs[i]()

#View results
print "%Of natural numbers less than or equal to d%Number of multiples of d= %d" % (N, M, result)
job_server.print_stats()

Points on the source code

Four same IP addresses are described in ppservers. By doing this, you can throw the task divided into 4 on the same Raspberry Pi, and you can fully use 4 cores. If only one core is used, describe only one. If you want to distribute the processing with multiple Raspberry Pis, you can set the IP address of each Raspberry Pi here.

Installing Parallel Python

  1. Download and unzip pp-1.6.4.tar.gz from Parallel Python Site
  2. Move to the unzipped folder
  3. Installation > python setup.py

Script execution

  1. Start the server that accepts processing > ppserver.py &
  2. Execute the written script pptest.py > python pptest.py

Execution result

Execution environment processing time[sec] Speed improvement rate * 1
Old model(Raspberry Pi B+) 48.7 -
New model(Raspberry Pi 2 B)1 core used 20.1 2.4
New model(Raspberry Pi 2 B)Use 4 cores 5.7 8.5

The output of the script is as follows

Old model (Raspberry Pi B +)

task0 args:(1,10000000,2) Multiples of 2 out of natural numbers less than 10000000 = 5000000 Job execution statistics: job count | % of all jobs | job time sum | time per job | job server 1 | 100.00 | 48.4202 | 48.420249 | 192.168.1.241:60000 Time elapsed since server creation 48.7006518841 0 active tasks, 0 cores

New model (Raspberry Pi 2 B) uses only 1 core

task0 args:(1,10000000,2) Multiples of 2 out of natural numbers less than 10000000 = 5000000 Job execution statistics: job count | % of all jobs | job time sum | time per job | job server 1 | 100.00 | 19.6110 | 19.610974 | 192.168.1.241:60000 Time elapsed since server creation 20.0955970287 0 active tasks, 0 cores

New model (Raspberry Pi 2 B) with 4 cores

task0 args:(1,2500000,2) task1 args:(2500001,5000000,2) task2 args:(5000001,7500000,2) task3 args:(7500001,10000000,2) Multiples of 2 out of natural numbers less than 10000000 = 5000000 Job execution statistics: job count | % of all jobs | job time sum | time per job | job server 4 | 100.00 | 20.6152 | 5.153800 | 192.168.1.241:60000 Time elapsed since server creation 5.68198180199 0 active tasks, 0 cores

Let's see the CPU usage of each core with mpstat

Installation

sudo apt-get install sysstat

Run

When measuring 10 times at 1-second intervals, do as follows mpstat -P ALL 1 10

I tried running the above script while running mpstat in another terminal.

Execution result

Old model (Raspberry Pi B +)

Since there is only one core, the CPU number is only 0. % usr is 100.

22:30:56 CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle 22:30:57 all 100.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 22:30:57 0 100.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00

New model (Raspberry Pi 2 B) uses only 1 core

Since there are 4 cores, there are CPU numbers from 0 to 3. Only CPU3 is 100%, and the others are 0%. All is 1/4, which is 25.0%.

22:35:54 CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle 22:35:55 all 25.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 75.00 22:35:55 0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00 22:35:55 1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00 22:35:55 2 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00 22:35:55 3 100.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00

New model (Raspberry Pi 2 B) with 4 cores

All 4 cores are 100%. Satisfied.

22:22:11 CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle 22:22:12 all 100.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 22:22:12 0 100.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 22:22:12 1 100.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 22:22:12 2 100.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 22:22:12 3 100.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00

Recommended Posts

Try to use up the Raspberry Pi 2's 4-core CPU with Parallel Python
Use python on Raspberry Pi 3 to light the LED with switch control!
Try to visualize the room with Raspberry Pi, part 1
How to use the Raspberry Pi relay module Python
Use vl53l0x with Raspberry Pi (python)
Use python on Raspberry Pi 3 to illuminate the LED (Hello World)
How to use Raspberry Pi pie camera Python
Try to solve the man-machine chart with Python
Specify the Python executable to use with virtualenv
The easiest way to use OpenCV with python
Get CPU information of Raspberry Pi with Python
Connect to MySQL with Python on Raspberry Pi
Measure CPU temperature of Raspberry Pi with Python
From setting up Raspberry Pi to installing Python environment
Try to solve the programming challenge book with python3
Try debugging Python on Raspberry Pi with Visual Studio.
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
[Python] I want to use the -h option with argparse
Try to bring up a subwindow with PyQt5 and Python
Try to automate the operation of network devices with Python
Try to decipher the garbled attachment file name with Python
Connect Raspberry Pi to Alibaba Cloud IoT Platform with Python
It was great to edit the Python file in the Raspberry Pi with Atom's remote function
Python: How to use async with
Try to operate Facebook with Python
Try L Chika with raspberry pi
How to use FTP with Python
Try moving 3 servos with Raspberry Pi
Use boto3 to mess with S3
[Raspberry Pi] Changed Python default to Python3
Control the motor with a motor driver using python on Raspberry Pi 3!
I tried to automate the watering of the planter with Raspberry Pi
[September 2020 version] Explains the procedure to use Gmail API with Python
I want to run the Python GUI when starting Raspberry Pi
[Python] Explains how to use the format function with an example
Try to solve the shortest path with Python + NetworkX + social data
About the error I encountered when trying to use Adafruit_DHT from Python on a Raspberry Pi
Try logging in to qiita with Python
Put Cabocha 0.68 on Windows and try to analyze the dependency with Python
Use Raspberry Pi to solve the problem of insufficient mobile Wi-Fi connection
[Python] Explains how to use the range function with a concrete example
[Python] Round up with just the operator
[Introduction to Python] Let's use foreach with Python
Try fishing for smelt with Raspberry Pi
Use the Grove sensor on the Raspberry Pi
I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)
Try Object detection with Raspberry Pi 4 + Coral
Python amateurs try to summarize the list ①
I connected the thermo sensor to the Raspberry Pi and measured the temperature (Python)
Use PIR motion sensor with raspberry Pi
Connect to s3 with AWS Lambda Python
Read the data of the NFC reader connected to Raspberry Pi 3 with Python and send it to openFrameworks with OSC
[Cloudian # 5] Try to list the objects stored in the bucket with Python (boto3)
The road to compiling to Python 3 with Thrift
Working with GPS on Raspberry Pi 3 Python
Try to solve the traveling salesman problem with a genetic algorithm (Python code)
Periodically notify the processing status of Raspberry Pi with python → Google Spreadsheet → LINE
Try to detect an object with Raspberry Pi ~ Part 1: Comparison of detection speed ~
Use python on Raspberry Pi 3 and turn on the LED when it gets dark!
Use Raspberry Pi Python to TMP36 analog temperature sensor and MCP3008 AD converter
Try to solve the fizzbuzz problem with Keras