Ashwin Pajankar

Raspberry Pi Supercomputing and Scientific Programming

MPI4PY, NumPy, and SciPy for Enthusiasts

Ashwin Pajankar

Nashik, Maharashtra, India

Any source code or other supplementary materials referenced by the author in this text are available to readers at www.apress.com . For detailed information about how to locate your book’s source code, go to www.apress.com/source-code/ . Readers can also access source code at SpringerLink in the Supplementary Material section for each chapter.

ISBN 978-1-4842-2877-7

e-ISBN 978-1-4842-2878-4

DOI 10.1007/978-1-4842-2878-4

Library of Congress Control Number: 2017943339

© Ashwin Pajankar 2017

This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed.

Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.

While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.

Printed on acid-free paper

Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.

Introduction

“I’m sorry, buddy, you’ve got a C in parallel programming,” my classmate said to me. It was my second semester at IIIT Hyderabad, where I was pursuing my graduate studies towards an MTech in computer science and engineering. To be very honest, I was really not expecting an A in parallel programming. However, I was not expecting a C either. Like most of the folks in my class, I was expecting a B or a B- in the worst case. As I mentioned earlier, it was my second semester and I was getting used to boring and rigorous academics at the Institute. I was also getting used to seeing C and C- grades on my scorecard. I was really starting to wonder if I had what it took to pursue graduate studies at the coveted and hallowed Institute. Little did I know that the worst was yet to come, because exactly one year after that, I saw two Ds on my scorecard.

I wondered if there was a way to make scary subjects like parallel programming more interesting and appealing to undergrad and graduate students. I vividly remember that our prof for the course mentioned that the US Air Force created a cluster of PS3s ( http://phys.org/news/2010-12-air-playstation-3s-supercomputer.html ). You can also check the Wikipedia page ( https://en.wikipedia.org/wiki/PlayStation_3_cluster ) for the same. I fancied owning a small-scale cluster like that. However, the biggest hurdle was and still is the cost associated with individual unit. A PS3 costs around 250 USD and a PS2 costs 100 USD. Moreover, it is a really tedious task to install Linux OS on the PS2 and PS3. I have nothing against Sony or the PS2/PS3, as Playstations are excellent gaming consoles, but I wanted something cheaper and simpler to build the cluster. So I shelved my plans to build a cluster for the time being and got engrossed in academics at the Institute.

Fast forward two years: I saw a bunch of kids booting up a strange-looking PCB (printed circuit board) with Linux and I literally jumped with excitement. When I asked them what it was, they said, “Raspberry Pi.” This is how I was introduced to Raspberry Pi. The occasion was a Hackathon organized at my former workplace for specially abled kids. When I saw those lovely and creative kids using a little computer, I immediately decided to explore it, and after six months my first book on Raspberry Pi ( www.packtpub.com/hardware-and-creative/raspberry-pi-computer-vision-programming ) got published.

It has been a while since my first book got published. Raspberry Pi underwent major revisions. Multiple newer and more powerful models of Pi have been introduced. I explored the world of single board computers in detail as I (correctly) thought that SBCs could be best for cluster. Then I built a cluster with three units of Raspberry Pis for a proof of concept. I went on building the clusters for various organizations, T-Schools, and universities all over India.

Raspberry Pi also serves as an excellent learning platform for scientific programming. We can use it for exploring important concepts in the scientific domains of signal and image processing, or to perform symbolic computations with SymPy.

I have explored the domain of scientific programming in detail in this book. The book has almost 100 coding examples to demonstrate and explore the world of scientific programming.

I hope that this book will help the scientific community and young researchers to use Raspberry Pi and Python as tools to explore scientific frontiers.

Why This Book?

As I said earlier, I found learning and exploring the topics in parallel and scientific programming quite boring and difficult. However, these are some of the most useful areas in computer science, with many applications in the real world. In my past and current jobs, I routinely used various parallel and scientific programming libraries for accomplishing various tasks. I want more people to get interested in this wonderful field. However, when people approach me for guest lectures and talks on this topic, they have a common complaint. They tell me that it’s difficult to get started with Raspberry Pi, parallel and scientific programming due to lack of reliable materials and references over the Internet. There are many tutorials and videos which teach people how to create a small multi-node cluster with Raspberry Pi. But due to unfamiliarity with the notion of single board computers, they found it difficult to grasp the information provided. So I decided to combine the topics of Raspberry Pi setup and supercomputers in a single book. This book teaches readers how to build a Raspberry Pi cluster as well as how to use Python to exploit its computational power for various scientific tasks. The cluster we will build in this book will be very basic one. It will be fit to be deployed in academic and research institutions. We will also learn how to get started with symbolic programming, scientific programming, image processing, and signal processing in this book. The book has within its scope the following:

  • Introduction to single board computers, Python 3, and Raspberry Pi

  • Basic concepts in supercomputing

  • Preparing a node and building an entire cluster

  • Parallel programming in Python

  • Symbolic mathematical programming

  • Scientific programming

  • Image processing

  • Signal processing

  • Visualization with Matplotlib

Who Is This Book For?

This book is for beginners in Raspberry Pi and parallel programming. It is for people who are interested in learning how to create a low-cost supercomputer and get started with scientific programming at very low cost. However, this book is not for people completely new to the world of computer science. I assume that you have a decent knowledge of computers and that you are not new to the most fundamental concepts related to it. The perfect reader of this book may be both a hobbyist and a student, someone who is familiar with computers and wants to learn a more powerful tool to explore computer science further. Makers and hackers will also find this book interesting. Finally, if you have no idea why you are reading this book, you might get interested in Raspberry Pi, supercomputers, scientific and parallel programming. I hope all the readers will enjoy reading this book as much as I enjoyed writing it.

What This Book Is Not

This book is not a book for learning Python 3 programming and syntax from scratch. It is more of a DIY cookbook for Raspberry Pi, scientific programming, and supercomputing. If your understanding of coding is limited or if you are not from a computer science background, then you will find it difficult to follow this book.

How This Book Is Organized

This book has 14 chapters. Here is a sneak peek into the chapters of the book:

Chapter 1 : This chapter introduces the readers to the history and philosophy of single board computers. It explores Raspberry Pi basics, and teaches readers to set up the Raspberry Pi and connect it to a network.

Chapter 2 : This chapter introduces the readers to important Linux commands. It also details the procedure to establish remote connectivity with Raspberry Pi.

Chapter 3 : The aim of this chapter is to introduce the readers to Python history and philosophy. It discusses the differences between Python 2 and Python 3. It also explores various modes of the Python 3 interpreter.

Chapter 4 : This chapter serves to introduce the concept of supercomputing to the readers.

Chapter 5 : This chapter will demonstrate how to install MPI4PY on Raspberry Pi.

Chapter 6 : This chapter teaches the readers how to build a supercomputer of Pis.

Chapter 7 : This chapter teaches the readers how to overclock various models of Pi safely.

Chapter 8 : This detailed chapter introduces readers to MPI4PY programming with Python 3. It explores many important concepts and constructs in parallel programming.

Chapter 9 : This chapter introduces readers to the components of the SciPy stack. It also gets the readers started with symbolic programming using SymPy.

Chapter 10 : This chapter introduces readers to the world of numerical computation with NumPy.

Chapter 11 : This chapter introduces readers to the various modules in the SciPy library.

Chapter 12 : Readers explore the amazing world of signals and signal processing graphically in this chapter.

Chapter 13 : Readers will explore the basics of image processing in this chapter.

Chapter 14 : This chapter provides a brief glimpse into the world of data visualization with Matplotlib in Python.

How to Get The Most Out of This Book

It is easy to leverage the book to gain the maximum by religiously abiding by the following:

  • Read the chapters thoroughly. Do NOT skip any chapter. Perform hands-on by following the step-by-step instructions stated in the code examples. Do NOT skip any code example. If need be, repeat it a second time or until the concept is firmly etched in your mind.

  • Join a Python community or discussion forum.

  • Read online documentation available for MPI4PY, NumPy, and SciPy.

  • Read blogs on single board computers, Raspberry Pi, supercomputers, parallel programming, scientific programming, and Python 3.

Where Next?

I have endeavored to unleash the power of supercomputing and scientific libraries in Python 3 as an aid to the Raspberry Pi community. I recommend you read the book from cover to cover without skipping any chapter, text, code example, note, reference, or exercise.

A Quick Word for Instructors

I have paid attention to the sequence of chapters and the flow of topics within each chapter. This is particularly to assist my fellow instructors and academicians in carving out a syllabus for their training from the table of contents of this book.

I have ensured that each concept discussed in the book is with adequate hands-on content to enable you to teach better and provide ample hands-on practice to your students.

Happy learning, supercomputing, scientific programming, and Pythoning!

Ashwin Pajankar, author

Acknowledgments

The making of the book was a journey that I am glad I undertook. First, I would like to thank my wife, Kavitha, without whose support the extraordinary journey would have never been possible. The journey spanned a few months but the experience will last a lifetime. I had my wife, Kavitha, with me onboard this journey and I wish to express my deepest gratitude to her. Without her unwavering support and affection, I couldn’t have pulled it off.

I am grateful to the community of professionals, students, trainers, and teachers who with their continual bombardment of queries impelled me to learn more, simplify my learnings and findings, and place it neatly in the book. This book is for all of them.

I wish to thank my friends and colleagues—the practitioners from the industry and experts from academia for their good counsel and filling me in with the knowledge on the latest in the fields of single board computers, supercomputing, parallel programming, and Python.

I would like to thank the technical reviewer for his vigilant reviews, suggestions, corrections, and offering his expert opinion.

I consider myself very fortunate for the editorial assistance provided by Apress Media. This is my third book with Apress and the collaboration with them for all the books has been fabulous. I am thankful to Celestin Suresh John, Senior Manager, Editorial Acquisition, Apress and Springer Science and Business Media Company for giving me a long-desired opportunity to collaborate and write with Apress. I wish to acknowledge and appreciate Sanchita Mandal, coordinating editor; Matthew Moodie, development editor; and the team of associates from Apress who adeptly guided me through the entire process of preparation and publication with Apress.

Contents

  1. Chapter 1:​ Introduction to Single Board Computers and Raspberry Pi
    1. Single Board Computers (SBCs)
      1. Differences Between SBCs and Regular Computers
      2. System on Chip
      3. History of SBCs
      4. Popular SBC Families
    2. Raspberry Pi
    3. Raspberry Pi Setup
      1. Hardware required for Raspberry Pi setup
      2. Manual Preparation of the MicroSD Card for Raspberry Pi
      3. Download the Required Free Software
      4. Writing the Raspbian OS Image to the MicroSD Card
      5. Altering the Contents of the config.​txt File for VGA Monitors
      6. Booting up the Pi
      7. Configuring the Pi
    4. Raspbian
    5. config.​txt
    6. Connecting Raspberry Pi to a Network and the Internet
      1. WiFi
      2. Ethernet
    7. Updating the Pi
      1. Updating the Firmware
      2. Updating and Upgrading Raspbian
      3. Updating raspi-config
    8. Shutting Down and Restarting Pi
    9. Conclusion
  2. Chapter 2:​ Important Linux Commands and Remote Connectivity
    1. Important and Useful Linux Commands
      1. Getting Help with Linux Commands
      2. Network-related Commands
      3. System Information Commands
    2. Enabling Pi for SSH from raspi-config
    3. Connecting to the Raspberry Pi Remotely from Windows
      1. Checking the Connectivity with Pi from Another Computer
      2. PuTTY
      3. Accessing Raspberry Pi Desktop Remotely
      4. WinSCP
    4. Connecting to Raspberry Pi Using Linux or macOS
      1. Remote Login with SSH
      2. Forwarding Using SSH
      3. SCP for File Transfer
    5. Conclusion
  3. Chapter 3:​ Introduction to Python
    1. History of Python
    2. Features of Python
      1. Simple
      2. Easy to Learn
      3. Easy to Read
      4. Easy to Maintain
      5. Open Source
      6. High-level Language
      7. Portable
      8. Interpreted
      9. Object-Oriented
      10. Extensible
      11. Extensive Libraries
      12. Robust
      13. Rapid Prototyping
      14. Memory Management
      15. Powerful
      16. Community Support
    3. Python 3
      1. The Differences Between Python 2 and Python 3
      2. Why Use Python 3?​
    4. Python 2 and Python 3 on Raspbian
    5. Running a Python Program and Python Modes
      1. Interactive Mode
      2. Normal Mode
    6. IDEs for Python
      1. IDLE
      2. Geany
    7. Conclusion
  4. Chapter 4:​ Introduction to Supercomputing
    1. Concept of the Supercomputer
    2. Brief history of Supercomputers
    3. Cluster
      1. Heterogenous Cluster
      2. Beowulf Cluster
    4. Parallelism and Concurrency
      1. Parallelism
      2. Concurrency
      3. Parallel Programming
    5. Conclusion
  5. Chapter 5:​ Message Passing Interface
    1. Message Passing Interface
      1. History and Evolution of the MPI Standard
      2. Features of MPI
      3. Implementations of MPI
    2. MPI4PY
      1. Why Use the Python, MPI, and MPI4PY Combination?​
      2. Installing MPI4PY for Python3 on Raspbian
    3. Installing nmap
    4. Conclusion
  6. Chapter 6:​ Building the Supercomputer
    1. Making a Backup of the MicroSD card
    2. Preparing Nodes of the Supercomputer
    3. Networking the Pis
      1. LAN with DHCP
      2. WiFi Network
      3. LAN with Static IP Addresses
    4. Using nmap to Find the IP Addresses of Pis
    5. Running the hostname Command on Multiple Pis with mpirun
      1. Exchanging the ssh-keygen Keys for Automatic Authentication
    6. Organizing the Pis in the Cluster
    7. Conclusion
  7. Chapter 7:​ Overclocking Raspberry Pi
    1. Risks of Overclocking Raspberry Pi
    2. Installing a Heatsink on Pi
      1. Procuring Heatsinks
    3. Overclocking the Pi with raspi-config
    4. Overclocking the Pi with /​boot/​config.​txt
      1. Options in /​boot/​config.​txt
    5. /​boot/​config.​txt Options for the Various Models of Pi
      1. Options for Pi B and Pi B+
      2. Options for Pi 2
      3. Options for Pi 3
    6. Conclusion
  8. Chapter 8:​ Parallel Programming in Python 3
    1. Basics of MPI4PY
    2. Getting Started with MPI4PY
    3. Conditional Statements
    4. Checking the Number of Processes
    5. Sending and Receiving Data
    6. Dynamically Sending and Receiving Data
    7. Data Tagging
    8. Data Broadcasting
    9. Data Scattering
    10. Data Gathering
    11. Conclusion
  9. Chapter 9:​ Introduction to SciPy Stack and Symbolic Programming
    1. The Scientific Python Stack
    2. Installation of the SciPy Stack
    3. SymPy
      1. Getting Started
      2. Symbols
      3. Converting Strings to SymPy Expressions
      4. Sympy’s Printing Functionality
      5. Simplification in SymPy
      6. Calculus
    4. Conclusion
  10. Chapter 10:​ Introduction to NumPy
    1. Basics of NumPy
      1. Jupyter
      2. Jupyter Notebooks
      3. The N-Dimensional Array (ndarray)
      4. ndarray Attributes
      5. Data Types
    2. Array Creation Routines
    3. Matrix and Linear Algebra
    4. Trigonometric Methods
    5. Random Numbers and Statistics
    6. Fourier Transforms
    7. Conclusion
  11. Chapter 11:​ Introduction to SciPy
    1. Scientific and Mathematical Constants in SciPy
    2. Linear algebra
    3. Integration
    4. Interpolation
    5. Conclusion
  12. Chapter 12:​ Signal Processing with SciPy
    1. Waveforms
    2. Window Functions
      1. Mexican Hat Wavelet
      2. Convolution
    3. Conclusion
  13. Chapter 13:​ Image Processing with SciPy
    1. First Image Processing Program
    2. Simple Image Processing
    3. Introduction to NumPy for Image Processing
    4. Matplotlib for Image Processing
    5. Image Channels
    6. Conclusion
  14. Chapter 14:​ Matplotlib
    1. Reading an Image
    2. Colormaps
    3. Colorbar
    4. Matplotlib for Image Processing
    5. Interpolation Methods
    6. Conclusion
    7. Summary of the Book
  15. Index

About the Author and About the Technical Reviewer

About the Author

Ashwin Pajankar is a polymath. He is a science popularizer, a programmer, a maker, an author, and a Youtuber. He graduated from IIIT Hyderabad with an MTech in computer science and engineering. He has a keen interest in the promotion of science, technology, engineering, and mathematics (STEM) education. He has written three books with Packt Publication, six books with Leanpub, two books with Apress, and has also reviewed four books for Packt Publications. This is his third book with Apress Publication and he’s working on a few more books with Apress.

His personal website is

www.AshwinPajankar.com

His LinkedIn profile is

https://in.linkedin.com/in/ashwinpajankar

His Youtube channel is

www.youtube.com/AshwinPajankar

About the Technical Reviewer

A447085_1_En_BookFrontmatter_Figb_HTML.jpg

Lentin Joseph is an author, entrepreneur, electronics engineer, robotics enthusiast, machine vision expert, embedded programmer, and the founder and CEO of Qbotics Labs ( www.qboticslabs.com ). He is from India.

He completed his bachelor’s degree in electronics and communication engineering at the Federal Institute of Science and Technology (FISAT) in Kerala. For his final year engineering project, he made a social robot that can interact with people. The project was a huge success and was mentioned in many forms of visual and print media. The main features of this robot were that it could communicate with people and reply intelligently, and it had some image processing capabilities such as face, motion, and color detection. The entire project was implemented using the Python programming language. His interest in robotics, image processing, and Python started with that project.

After his graduation, for three years he worked at a start-up company focusing on robotics and image processing. In the meantime, he learned to work with famous robotic software platforms such as Robot Operating System (ROS), V-REP, Actin (a robotic simulation tool), and image processing libraries such as OpenCV, OpenNI, and PCL. He also knows about robot 3D designing and embedded programming on Arduino and Tiva Launchpad.

After three years of work experience, he started a new company called Qbotics Labs, which mainly focuses on research to build up some great products in domains such as robotics and machine vision. He maintains a personal website ( www.lentinjoseph.com ) and a technology blog called technolabsz ( www.technolabsz.com ). He publishes his works on his tech blog. He was also a speaker at PyCon2013, India, on the topic of learning robotics using Python.

Lentin is the author of the books Learning Robotics Using Python (see www.learn-robotics.com to find out more) and Mastering ROS for Robotics Programming (see www.mastering-ros.com to find out more) by Packt. The first book was about building an autonomous mobile robot using ROS and OpenCV. This book was launched at ICRA 2015 and was featured in the ROS blog, Robohub, OpenCV, the Python website, and various other such forums. The second book is on mastering Robot Operating System, which was also launched at ICRA 2016, and is one of the bestselling books on ROS. The third book is on ROS Robotics Projects ( www.rosrobots.com ), which is expected to release on April 2017.

He also reviewed one book about Robot Operating System called Effective Robotics Programming Using ROS ( www.packtpub.com/hardware-and-creative/effective-robotics-programming-ros-third-edition )

Lentin and his team were also winners of the HRATC 2016 challenge conducted as a part of ICRA 2016. He was also a finalist in the ICRA 2015 challenge, HRATC ( www.icra2016.org/conference/challenges/ ).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.146.237