matt92
Posts: 4
Joined: Sun Feb 19, 2017 3:55 pm

Parallel Programming of Raspberry Pi?

Sun Feb 19, 2017 4:13 pm

Hello everyone I am totally new to Raspberry Pi. I am not a programming expert and am working on a project originally using Arduino. Then I realize that I need multi core CPU to do the work (retrieving multiple sensors values) in parallel. That's why I came to here and wish I could do the job by using quad core Raspberry Pi 2B.

But so far I could only find the way to run a massive for loop in different cores in order to speed up with the use of openMP. It seems to be a usual reason of why using parallel programming. How about running totally different programs at the same time? I would like two programs collecting some IR sensors and Sonar sensors value separately and the values then to be sent to the main running program for analysis. I reckon there should be three program running in parallel and could anyone help me how could I achieve this?

User avatar
topguy
Posts: 6607
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Parallel Programming of Raspberry Pi?

Sun Feb 19, 2017 10:31 pm

Either by actually creating three different programs and run them at the same time. Or use threads.
Type you chosen programming language and "threads" in google and read.

User avatar
Gavinmc42
Posts: 4547
Joined: Wed Aug 28, 2013 3:31 am

Re: Parallel Programming of Raspberry Pi?

Mon Feb 20, 2017 2:32 am

If you are familiar with Arduino and microcontroller programing try example 11
https://github.com/ultibohub/Examples.

There is no OS to get in the way, but you can still do Os type stuff like comms, networking.
In Advanced examples is the Sensormatic3000 code.
DedicatedCPU example will be of interest too.
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

jahboater
Posts: 5924
Joined: Wed Feb 04, 2015 6:38 pm
Location: West Dorset

Re: Parallel Programming of Raspberry Pi?

Mon Feb 20, 2017 7:54 am

matt92 wrote:I would like two programs collecting some IR sensors and Sonar sensors value separately and the values then to be sent to the main running program for analysis. I reckon there should be three program running in parallel and could anyone help me how could I achieve this?
Raspbian (the official Pi operating system) from here https://www.raspberrypi.org/downloads/raspbian/ is fully multi-tasking, it is absolutely normal for many programs to be running in parallel. As noted above, there are multiple ways of doing it, perhaps the simplest being just running three separate programs connected via pipes. Here is a trivial command line example:

cat myfile | more

"cat" is a program that reads a file and sends the contents normally to the screen, but in this case through a pipe (|) to another program called "more" which is a pager and allows you to see the contents of myfile one page at a time. Both cat and more will run in parallel. This example is just to show how normal it is to run more than one program at once. "cat myfile | sort | more" runs three programs in parallel, it shows the file alphabetically sorted. If you run "ps -e" or better, "top" you will likely see more than 100 programs running.

mfa298
Posts: 1386
Joined: Tue Apr 22, 2014 11:18 am

Re: Parallel Programming of Raspberry Pi?

Mon Feb 20, 2017 8:31 am

matt92 wrote:Hello everyone I am totally new to Raspberry Pi. I am not a programming expert and am working on a project originally using Arduino. Then I realize that I need multi core CPU to do the work (retrieving multiple sensors values) in parallel. That's why I came to here and wish I could do the job by using quad core Raspberry Pi 2B.
Do they have to have exactly at the same time, quite often polling multiple sensors immediately after each other is good enough. Depending on the sensors and language you may not get fully parallel access to sensors. If they're on the same bus (e.g. i2c sensors) then the bus will limit you to one sensor at time.

If the sensor data needs to be used by the same software or accessed as close to in parallel as possible then you should probably do it from one application, possibly with the use of threads. You may also need to choose your language carefully. Many higher level languages implement some locking behind the scenes so whilst you may have many threads only one will actually be active at a time.

matt92
Posts: 4
Joined: Sun Feb 19, 2017 3:55 pm

Re: Parallel Programming of Raspberry Pi?

Tue Feb 21, 2017 6:38 am

Thank you all. Looks like parallel programming wasn't that unusual or complicated as I'd thought to be. I do not require the programs starting at the same time. But I would like to have the feedback from different sensors' program on request like:

Normally three programs running in parallel (Main / Infrared receiver sensors / Ultrasonic Sensors), and when the main program calls a function, the other two program will pause and return the latest sensor values to main program. So do I use threads or any other technique to do this? I am writing C++ and if it is too much to explain, please give me some keywords and I will go google it. Thanks to everyone!!

mfa298
Posts: 1386
Joined: Tue Apr 22, 2014 11:18 am

Re: Parallel Programming of Raspberry Pi?

Tue Feb 21, 2017 7:46 am

matt92 wrote:Thank you all. Looks like parallel programming wasn't that unusual or complicated as I'd thought to be. I do not require the programs starting at the same time. But I would like to have the feedback from different sensors' program on request like:

Normally three programs running in parallel (Main / Infrared receiver sensors / Ultrasonic Sensors), and when the main program calls a function, the other two program will pause and return the latest sensor values to main program. So do I use threads or any other technique to do this? I am writing C++ and if it is too much to explain, please give me some keywords and I will go google it. Thanks to everyone!!
Threads probably make more sense than totally separate programs for that.

pthreads are the way generally to create new threads although it can get a bit more interesting in C++, libraries (such as boost) may help wrap up some of the detail for you.

I wrote some C++ with threads a few years back which is on github https://github.com/m1ari/Sandals. You may find the rtty class and sandals.cxx files give some insight into how threads can be used in c++ (I don't promise it's good quality c++ but it seemed to work)

User avatar
topguy
Posts: 6607
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Parallel Programming of Raspberry Pi?

Tue Feb 21, 2017 12:19 pm

Chapter 4 in this freely available book goes into detail about Threads in Linux. Might be an interesting read even if you go for something like boost. Its good to understand the basic concepts.
http://advancedlinuxprogramming.com/alp-folder/

Return to “Troubleshooting”