Automated looping over parameter values for simulations in C


I generally run a fair number of stochastic simulations, where I want to run many simulations as I systematically vary one or more parameters. I typically run anywhere from 5 to 100 replicated simulations for each combination of parameter values.

I used to write Perl scripts which would loop over my parameter values, and for each parameter combination, call my simulation (written in C). The Perl script would gather up the output and save it all to a file. I then do post-processing (basic statistics, visualization, etc.) in Matlab or R.

Eventually, I started to get tired of bouncing between Perl, C, and Matlab/R. I also started running simulations on an OpenMPI cluster, and my Perl script trick wasn’t very useful for that. Instead, I folded the iteration over parameter values back into the C code, and wrote some routines to help automate this. These are the results.

The code here provides a way to conveniently loop over specified values for one or more parameters. You can that specify parameters should go from a to b using s steps, or using an increment of i. You can instead specify an explicit list of values v1v2v3, … for a parameter. The code loops over all possible combinations of the parameters, so if you specify 3 values for one parameter and 8 values for a second, you get a total of 24 combinations of the two parameters. Alternatively, you can link two parameters together. So e.g. you can specify that 4 different parameters each have 3 values, but they are linked, so you get only 3 total parameter combinations rather than 81.

Small example programs are included showing how to use the code. They simply print out each parameter combination as they run. The first compiles with standard gcc. The second should be compiled with mpicc to run under OpenMPI. It prints all output to the files testparmloopMPI.out.nodeX where X varies among 0, 1, 2, ….

Source code



This code is based on work supported by the National Science Foundation under Grant nos. DMS-0718786 and DMS-0746603.