Message Boards Message Boards

Parallel Mathematica Environment on the RaspberryPi using OOP

Posted 8 years ago

My project, Parallel Mathematica Environment on the RaspberryPi using OOP, is a sample application of Object Oriented Programming for the Mathematica cluster computing, implemented with a Mac and three RaspberryPi Zero connected with a USB hub and three USB cables.

Basic idea is to deploy a constructed instance image to calculating servers (RaspberryPi) and send messages to the instance. OOP on the Mathematica is already developed and shown in this community, and further detail is shown on slidesshare titled of "OOP for Mathematica." enter image description here


Preparing for RaspberryPi Zero is as follows using SSH connection from a Mac,

  • naming each Zero as raspberypi,raspberrypi1,raspberrypi2,...
  • set the server program "init" to each RaspberryPi, init is,

    $ cat init
    While[True,
    Run[“nc -l 8000>input”];
    temp=ReleaseHold[<<input];
    temp >>output;
    Run[“nc your-mac-hostname.local 8002<output”]
    ]
    

where, socket numbers must be identical.

  • Run Mathematica manually, and wait the booting Mathematica up.

    $ wolfram <init&
    

Checking each RaspberryPi is useful as,

$ nc -l 8002 >output|nc raspberrypi.local 8000 <<EOF
> 10!
> EOF
$ cat output
3628800

Cluster controller program on a Mac is,

  • set directory

    SetDirectory[NotebookDirectory[]];
    
  • setup socket communication process

    com1="nc -l 8002 >output1 |nc raspberrypi.local 8000 <input1";
    com2="nc -l 9002 >output2 |nc raspberrypi1.local 9000 <input2";
    com3="nc -l 9502 >output3 |nc raspberrypi2.local 9500 <input3";
    
  • set object property

    obj={
       <|"name"->node1,"comm"->com1,"in"->"input1","out"->"output1","p"->{2000,3500}|>,
       <|"name"->node2,"comm"->com2,"in"->"input2","out"->"output2","p"->{3501,4000}|>,
       <|"name"->node3,"comm"->com3,"in"->"input3","out"->"output3","p"->{4000,4500}|>};
    
  • define calculation server class, where is a sample Mersenne prime number calculation

    new[nam_]:=Module[{ps,pe},
       mersenneQ[n_]:=PrimeQ[2^n-1];
       setv[nam[{s_,e_}]]^:={ps,pe}={s,e};
       calc[nam]^:=Select[Range[ps,pe],mersenneQ]
       ];
    
  • construct instances

    Map[new[#name]&,obj];
    
  • deploy instances to calculation servers

    Map[Save[#in,#name]&,obj];
    Map[Run[#comm]&,obj];
    
  • send message to each instance

    Map[Put[Hold@setv[#name[#p]],#in]&,obj];
    Map[Run[#comm]&,obj];
    
  • start calculation

    Map[Put[Hold@calc[#name],#in]&,obj];
    proc=Map[StartProcess[{$SystemShell,"-c",#comm}]&,obj]
    
  • wait for the process termination (mannualy in this sample code)

    Map[ProcessStatus[#]&,proc]
     {Finished,Finished,Finished}
    
  • gather the results

    Map[FilePrint[#out]&,obj];
     {2203, 2281, 3217}
    {}
    {4253, 4423}
    
20 Replies

If all you need is to be able to use multiple Raspberry Pi computers (or any other computer really) for parallel computation, you can do this using built-in functionality.

If you prefer to use the GUI to set this up, go Preferences -> Parallel -> Remote Kernels.

Click Add Host, and fill out the details. First, fill out the hostname. Then, depending on your setup, you may need to modify the launch command template. You will notice that those numerical slots, like `1` in the launch command template. To find out what they are, evaluate

<< SubKernels`RemoteKernels`

?$RemoteCommand

It is important to use a launch command that will not prompt for a username and password. For this, you will need to set up SSH public key authentication (please google for it).

You may also need to tell Mathematica the username on the remote machine. Load << SubKernels`RemoteKernels` then set $RemoteUser.

Personally, I have not used this GUI method. Instead, I configured remote kernels by typing commands.

I did this by loading << SubKernels`RemoteKernels`, then creating a configuration for each remote computer in the form

x = RemoteMachine[hostname, launchcommand]

The launch command can either be a template like in the GUI configuration (i.e. have slots like `1` in it), or just a plain string (no slots). One you have all these, just append them to $ConfiguredKernels

$ConfiguredKernels = Join[$ConfiguredKernels, {x}]

Once everything is set up, you can simply use LaunchKernels[] and submit parallel evaluations as usual (e.g. ParallelSubmit).

I realize that this is a bit vague, but I do not have a Raspberry Pi at hand to give more detailed instructions. I hope it is still helpful.

POSTED BY: Szabolcs Horvát

Parallelization using OOP is certainly an interesting project, and I hope you did not misunderstand my response. I also enjoy tinkering with such things, and re-implementing things that Mathematica already has built-in. The fun is in learning how to implement something like this. Producing something for practical use is something else.

I was simply correcting the misunderstanding that it is not possible to use the built-in parallel tools with a standard Mathematica install. Mathematica includes everything for parallel computing, even on the Raspberry Pi. It is a fun project to create our own parallelization solution, but is is not necessary.

Here are some more straightforward instructions on getting parallelization working on a Raspberry Pi:

First, set up passwordless authentication:

https://www.raspberrypi.org/documentation/remote-access/ssh/passwordless.md

Since parts of this article are not 100% clear, I want to clarify that:

  • the key-pair needs to be created on the computer which is running the main Mathematica kernel (i.e. the master machine)
  • the public key must be copied to the authorized_keys files of the slave machines
  • ssh-agent must be running on the master machine. It may be necessary to add the private key to the agent using ssh-add path-to-your-key. If you set a passphrase for the private key, you will need to enter it at this point.

If the master machine is running Windows (not a Unix-like OS like Linux on the RPi or OS X on a Mac), then you can use the GUI tools from the Putty bundle to do the same. There are many tutorials online for this.

Once all this is set up, it should be possible to connect from the master machine to the slaves using ssh without entering a password (except for entering the passphrase once, when adding your private key to the ssh agent). This means that Mathematica will be able to launch new kernels on the slave machine without prompting you to enter a password. Do verify that this works.

Now suppose that the hostname of a slave machine is slave, and that you will be running the Mathematica kernel under the pi user. Then start Mathematica on the master kernel and run:

AppendTo[$ConfiguredKernels,
    SubKernels`RemoteKernels`RemoteMachine["slave", "ssh -x -f -l pi `1` wolfram -wstp -linkmode Connect `4` -linkname '`2`' -subkernel -noinit -nopaclet", 1, {}]
]

Notice that I changed only one thing compared to the default configuration. I changed -l `3` to -l pi. If Mathematica is running under the same username both on the master and slave machines, then this change not necessary and the default launch command may be kept.

As a reminder, the default launch command is contained within

SubKernels`RemoteKernels`$RemoteCommand

If you use the default (i.e. you are using the same username on the host as on the slave), then all this simplifies to

AppendTo[$ConfiguredKernels, SubKernels`RemoteKernels`RemoteMachine["slave"]]

If you want to use more than one kernel on the same slave (when the slave has a multi-core CPU), then use

AppendTo[$ConfiguredKernels, SubKernels`RemoteKernels`RemoteMachine["slave", SubKernels`RemoteKernels`$RemoteCommand, 4 (* number of kernels *)]]

Now you are ready to go! No further setup is necessary. Just use ParallelTable, ParallelMap, etc. as usual. As a test, you may want to run LaunchKernels[] and see if it succeeds. But under normal use, LaunchKernels[] is called automatically by parallel functions.


This time I did test these instructions by using a Mac as the master computer and a Raspberry Pi as a slave. (Because I do not have two RPi's.)

Once again, all this works with standard Mathematica. Nothing else is needed. It is absolutely not necessary to purchase gridMathematica or the Lightweight Grid. Those products provide you with licenses to run many Mathematica kernels on a non-RPi machine, or automate part of this setup procedure. But they do not provide the parallelization functionality. That is entirely built into standard Mathematica.

The only thing you may need to do to get this to work is load the RemoteKernels package manually:

 <<SubKernels`RemoteKernels`
POSTED BY: Szabolcs Horvát

Great, I was always wondering how to set this up, thanks for sharing! Could you edit your post and add photos of your setup and Raspberry Pis? It would be very nice to see!

POSTED BY: Marina Shchitova

Yes, you must purchase a gridMathematica.

Thanks, but that doesn't square with two "facts on the ground": 1. First, Wolfram Documentation (http://reference.wolfram.com/language/ParallelTools/tutorial/ConnectionMethods.html) strongly suggests that Lightweight Grid Technology is an option, but not a requirement, for setting up a grid. 2. I can't reproduce the behavior (or I wouldn't have posted a query at all), but at some point I got a parallels option menu embedded in a notebook that looked like what one is supposed to get through the menu bar. It evidently generated the following in the file ~/.WolframEngineApplicationData/Parallel/Preferences/Preferences.m:

            {Parallel`Palette`paletteConfig["enabledImplementations"] ->
              {"SubKernels`LocalKernels`", "SubKernels`RemoteKernels`"},
             Parallel`Palette`paletteConfig["Remote Kernels"] ->
              {SubKernels`RemoteKernels`RemoteMachine["RPi1", "ssh -x -f -l `3` `1` \
            wolfram -wstp -linkmode Connect `4` -linkname '`2`' -subkernel -noinit", 4,
                True, False, {}]}, "Version" -> 3.01}

where RPi1 is the first 'slave' machine (I was running from RPi0). So whenever I start Mathematica and ask for configured kernels, it always lists RPi1, but not RPi2 or RPi3, which are listed under Evaluation->Kernel Configuration Options.

I did not manually add that line to Preferences.m, and am still trying to figure out how it got there.

POSTED BY: Bradley Keister

I only have one Raspberry Pi, and I only use it in command line mode, so I cannot test these things, but I would be very very surprised if parallelization did not work with it. It is definitely not necessary to purchase gridMathematica.

Take a look at my first post in this thread on how parallel kernels could be set up.

It is possible that some menu options are removed on the RPi because they are not usually needed there. The Parallel Kernel Configuration menu option just opens a certain tab of the main preferences window, so you can look there.

If the Parallel tab of the main preferences window is missing on the RPi, you can still just manually add elements to $ConfiguredKernels. This is what I would do anyway. (As I said, I do not use a GUI on the RPi because I have no screen for it.)

POSTED BY: Szabolcs Horvát

Though your replies are concerning to the parallel kernel configurations and documentations, my project environment ONLY needs standard Mathematica and "nc" and shell. Key feature of this project is Mathematica OOP.

Yes, as you replied, all parallel setting may be configured in the Mathematica menu of Kernel configuration... and Parallel Kernel configuration... . So, standard Mathematica allows local and remote kernels in total number defined in the license, but that situation is not one I intended.

Hi Rod,

As shown in my article "Parallel computing environment with ZMQ on the Raspberry Pi cluster", a methd with ZMQ running on "Raspberry Pi Zeo 2 W" will be a better choice for multiple node calculation.

"Parallel Computing Environment with ZMQ on the Raspberry Pi cluster" than the 7 year old article "Parallel Mathematica Environment on the RaspberryPi using OOP." will yield more software-stable results.

Regarding your question 1), it may be applicable to the latest Raspberry Pi, 2) If you adopt the latest Raspberry Pi 5 or 4, you need to keep in mind the power supply capacity and network speed, 3) As for the main application/coding, it depends on the user's situation. However, before building a cluster, I would like to recommend to try "Parallel computing and Object Oriented Programming Part 1~3" would be helpful.

enter image description here - you have earned "Featured Contributor" badge, congratulations !

This is a great post and it has been selected for the curated Staff Picks group. Your profile is now distinguished by a "Featured Contributor" badge and displayed on the "Featured Contributor" board.

POSTED BY: EDITORIAL BOARD

Hello,

I'm running Mathematica 11 on a cluster of 4 Raspberry Pi 3s. The Mathematica documentation says that I can configure all the remote kernels through the menu option Evaluate->Parallel Kernel Configuration. That menu option simply isn't there for me. I do have Evaluate->Kernel Configuration Options. That lets me set up remote kernels, but they do not appear as a response to $ConfiguredKernels. From various I can see what Parallel Kernel Configuration looks like - many tabs full of options, which I don't have available. Also I can't find a "Preferences" option and therefore no "Preferences -> Parallel -> Remote Kernels".

I'm clearly missing something.

POSTED BY: Bradley Keister

I configured remote kernels by typing commands.

I did this by loading << SubKernelsRemoteKernels, then creating a configuration for each remote computer in the form

x = RemoteMachine[hostname, launchcommand]

now I 'd like to test new instructions by using a Mac as the master computer and a Raspberry Pi as a slave.
Thanks
http://driverrestore.com/update-drivers-windows-10-upgrade/

POSTED BY: Homes Katosvich
Posted 7 years ago

I enjoyed reading this post

POSTED BY: Philipp Blume
Posted 3 years ago

Will it work with RPI 2 Zero w?

POSTED BY: Rod Young
Posted 1 year ago

Hi, Is this article/content ( 7 years ago - published ) STILL effective on the newest system components( Raspberry Pi 5 (8GB) : 1 Master + 4 Slaves, on Debian 12 Mathmatica 13.3 ) ?

IF Still active - even though major system components are refreshed, PLEASE advice me 1) Any restriction existed ? 2) Any considerations on the latest system build-up ? 3) Any major "Application/coding" on this OOP Mathmatica - do you have/know ??

Thank in advance

POSTED BY: Teiji Takano
Posted 1 year ago

Hello ,,, Very sorry for late response since I need the arrival of Raspberry Pi 5 (8GB) and prepare the new test environment with tis RPI5-8GB.

Though I can start the Trial of this "Parallel Mathematica Environment on the RaspberryPi using OOP" t[ Master (Mother) : RPI5-8GB, Slave( Child) : older RPI4B-4GB] , Today I have the problem ( a Beginner level for Wolfram Language) as below. Pls help me.

1 The status when it occures. - Well done @ Slave: rpi4B Run Mathematica manually, and wait the booting Mathematica up. $ wolfram <init&

  • Then error message has come to me as below @ Master : RPI5-980

tt@RPI5-980:~ $ nc -l 8002 >output|nc rpi4B.local 8000 <<EOF > 10! > EOF bash: nc: コマンドが見つかりません bash: nc: コマンドが見つかりません tt@RPI5-980:~ $

  • What's wrong ? How should I do ? Pls advice me.

Best regards.

POSTED BY: Teiji Takano

Hello Takano-san,

The article you referred to is too old and may not be valid for parallel systems. The newer article "Parallel Computing and Object-Oriented Programming - Parts 1-3" may be better.

This parallel system is based on a local network using USB gadgets and Ethernet. If each raspberryPi is configured in a headless style, a local network is realized with each raspberryPi and a Mac, Linux, Windows or other PC.

A parallel system is built in two steps. First, a single 4-core raspberryPi is prepared. In this step, a parallel system with OOP applied can be realized.

The second step will be a clustering system, composed of two sub-steps. The first sub-step is to combine multiple raspberryPi. Each raspberryPis combined with ZMQ can pass messages from one raspberryPi to another. The second step is to configure a raspberryPi cluster using the OOP'd Mathematica code.

With the above steps, a parallel system can be built. Unfortunately, I don't have a raspberryPi 5 yet, so I can't look into the ZMQ for newest one.

A cluster consisting of raspberryPi 5 and 4 combined with OOP-ized Mathematica would yield interesting results.

The answers to the questions are 1) Are there any limitations? ->Not really. 2) Are there any considerations for building a modern system? -> Not sure if ZMQ will work with 5. 3) Do you know of any major "applications/coding" of this OOP Mathematica? -> I think it will be useful for modeling natural and social phenomena.

Posted 4 months ago

Greetings from Japan,

Since I could JUST prepare the Wolfram Parallel Clustering with Qty:4 of Raspberry Pi ( instead of Mac & qty:3 of raspberry Pi Zero), I would like to retry to examine "this fundamental (but very meaninful ) Wolfram exercise".

I understand this exercise : it is too old ( maybe the difference between the older Wolfram and the latest Wolfram Engine 14.0 makes the some errors ), But this exercise is very simple and useful to understand in detail "mechanism of Parallel Wolfram Clustering on multiple Raspberry Pi 5 s.

Could you please provide the chance for me ?

POSTED BY: Teiji Takano

Hi, friend,

I could not know yours type of Raspberry Pi, however, I tried with my Raspberry Pi Zero W applying to the old my articles. Then I found the new Mac OS version, Ventura, doesn`t support USB OTG now, and a connection with Raspberry Pi Zero W to Mac should be through WiFi.

I suppose you have already realized the connections your RaspberryPi as a server to other RaspberryPis using SSH, which is the first step. As I shown previously, next step is that selecting a mehtod from, using nc command or a ZMQ method. In last year, I tryed a nc command method, but this method was tricky and tedeous, and decided to using ZMQ method may be better.

Next step 1 may be as follows. Using two of ZMQ installed RaspberryPi, you shoud try to connect with each two Wolfram programs. Step 2 may be, Using a RaspberryPi, you should be familiar to OOP to realize concurrent calculations.

I hope you will success.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract