If inside your loop you have a function that is from a package, say the function `pwr.t.test` from the ‘pwr’ package, make sure to include the package name in the call.

So instead of putting this inside the loop:
pwr.t.test(n=20, d=.5), you put this:
pwr::pwr.t.test(n=20,d=.5)

The reason is that the packages you load with ‘library’ or ‘groundhog.library’ are not sent to the parallel processors, so you need tell R running in those other processors where to find pwr.t.test [4].

I often run montecarlo simulations from within a custom function, and inside that function I put the parallel loop. So the code looks like this:

montecarlo_function <- function(N) {

    res=foreach(…{… yada yada yada )
       }
return(res) }

If you have any objects that are referenced in that function (e.g., other functions written on that script), they will not be sent to the parallel cores and you will get an error when your code executes, for a called on function will not be found. So you just need to tell R to send those other objects to every parallel processor.

See the example below, where a variable created outside the custom function, the vector n=c(100,500,1000), is explicitly referenced in the foreach(), with the .export() option, so that it is sent to the parallel processors.

If you delete the `.export=’n’ option, you will get an error saying ‘n’ was not found by the parallel processors. If you delete the pwr:: from pwr.t.test, you will get an error saying that the function pwr.t.test does not exist.

Fig 4. Intermediate level parallel loops. Putting loop in a function, and calling functions from packages

With serial loops I always use a simple counter that prints on the console the loop # every, say, 100 loops.

With parallel loops that will not work, because every parallel processor has its own instance of R open.
Instead, I have a text file that is saved to from the script, and I open the text file in R Studio. Whichever processor now happens to run the simulation that is a multiple of 100 saves to that text file, and R Studio refreshes when the file is saved, showing the loop number overall.

Here is the code that saves to a text file counter (I opened the text file in R Studio so it is now a tab)

 

And here is what you see if you click on the counter tab, e.g., there I was 1200 loops in.


Step 2. Running parallel loops on a virtual desktop
Your laptop probably has at most 8 cores, limiting the benefits of parallel loops. If you run them on a virtual computer on the cloud with more cores, things will naturally go even faster. I will show you how to setup a virtual desktop with ~50 cores. You have to pay, but it is cheap, <$1 (one dollar) per hour of computations. I first used Amazon’s AWS to run R on the cloud, but I recently came across a way better alternative kamatera.com.

If you haven’t learned how to use AWS, do not bother.
If you have learned it, you probably will prefer Kamatera, it is much easier to use, way faster to set up, and flexible (e.g., you can easily update R and R Studio in it).

To use it, go to http://kamatera.com  and then:

1. Create an account
(sorry, life is hard sometimes)

2. My cloud→Servers→Create New Desktop

3. Choose any location and presumably Windows machine.

4. Select the specs for your virtual computer
You probably want type=’burstable’

I am choosing 48 cores here, the key component, and some RAM and HD space which are usually not too relevant for loops.

Kamatera sets a limit to the specs based on how much it would cost to leave the virtual machine on for a full month. That limit is about $350. if you don’t modify that cap (which you can do by emailing support) you will have to choose fewer than 50 cores. [5].

5 Choose a name and password for the virtual computer.

Note: Make sure to choose ‘hourly billing’ so you don’t get charged when you don’t use it

It takes about 5 literal minutes for the virtual computer to be created. So don’t worry if it does not immediately appear.
Kamatera actually emails you once they have it ready.

 

6. Then start the virtual computer clicking on “Action->power on”

7. Connect with remote desktop app in your computer, entering the IP address shown in the Kamatera site

Look at the arrow to see what to use as user name

Whem prompted for the password, enter the one you just set above.

Mac user? connect to the windows machine from your Mac with:
https://apps.apple.com/us/app/microsoft-remote-desktop/id1295203466?mt=12

8. Now you have a fully functional virtual windows machine.

Once you are connected this is your virtual machine, you can do everything in it you do on your pc. To install R Studio, for example, just open the browser and download it. You can also copy-paste code from your computer to the virtual one and vice versa.

9. Bonus: github
If you use github, that’s by far the easiest way to send files back and forth. Install it in the virtual machine, and just pull before you work and push after you do. Code and results will be synced.

Wide logo


Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Footnotes.

  1. There are several blog posts and tutorials on running parallel loops (see e.g., blasbenitoesciencecenter | jbhender).  I thought there was room for an attempt at a more intuitive and focused-on-simulations coverage[]
  2. there are always fixed overhead cost, so not 56 times. Also, disclaimer: individual results may vary[]
  3. This example only considered uniform distributions, but it is actually fine more generally[]
  4. there are many ways to achieve this outcome, including the package name in the loop is by far the easiest[]
  5. Note that as you change the specs the cost of keeping the machine on for the full month is displayed, and there default is a $350 cap, so you have to play around to get what you want and stick to under $350, or email support to get the cap increased.  To be clear, you will spend WAY WAY less than $500, because that is the cost of keeping it on all month, you will probably use it for a few hours[]