Concurrency in the Civis R Client

Just like most functions in R, all functions in civis block. This means that each function in a program must complete before the next function runs. For instance,

This program takes 6 seconds to complete, since it takes 1 second for the first nap, 2 for the second and 3 for the last. This program is easy to reason about because each function is sequentially executed. Usually, that is how we want our programs to run.

There are some exceptions to this rule. Sequentially executing each function might be inconvenient if each nap took 30 minutes instead of a few seconds. In that case, we might like our program to perform all 3 naps simultaneously. In the above example, running all 3 naps simultaneously would take 3 seconds (the length of the longest nap) rather than 6 seconds.

As all function calls in civis block, civis relies on the mature R ecosystem for parallel programming to enable multiple simultaneous tasks. The three packages we introduce are future, foreach, and parallel (included in base R). For all packages, simultaneous tasks are enabled by starting each task in a separate R process. Examples for building several models in parallel with different libraries are included below. The libraries have strengths and weaknesses and choosing which library to use is often a matter of preference.

It is important to note that when calling civis functions, the computation required to complete the task takes place in Platform. For instance, during a call to civis_ml, Platform builds the model while your laptop waits for the task to complete. This means that you don’t have to worry about running out of memory or cpu cores on your laptop when training dozens of models, or when scoring a model on a very large population. The task being parallized in the code below is simply the task of waiting for Platform to send results back to your laptop.

Operating System / Environment Specific Errors

Differences in operating systems and R environments may cause errors for some users of the parallel libraries listed above. In particular, mclapply does not work on Windows and may not work in RStudio on certain operating systems. future may require plan(multisession) on certain operating systems. If you encounter an error parallelizing functions in civis, we recommend first trying more than one method listed above. While we will address errors specific to civis with regards to parallel code, the technicalities of parallel libraries in R across operating systems and enviroments prevent us from providing more general support for issues regarding parallelized code in R.