MapReduce operations allow parallelization of tasks taking advantage of aditional available cpus. However, one might want to use processors across several nodes in a computing cluster and while several options exist to perform this (with very different aims and scallability options), I didn’t feel like there was an option which would allow doing this interactively (for example during a IPython session) in a Slurm cluster and without requiring diving into lots of documentation. So obviously, here’s my custom solution.
The strategy I followed splits input in pools which are submitted in parallel through jobs to the cluster, each one of them is further processed in parallel using the multiprocessing library. This is a middle term between mapping all inputs to different jobs (clogging the cluster) and using only the CPUs available in one machine/node, by controlling the number of jobs that are submitted to the cluster and the size of each pool submitted. This approach was inspired by conversations with Michael Schuster and Nathan Sheffield in my lab.
I create an object to manage tasks which can include huge amounts of independent data to process the same way. Each task’s input is split in equal(ish)-sized pools and submitted to Slurm as jobs when wanted. For now I take care of tasks using a dict by I will expand this to make a Task(object) class, which would take care of them.
I use subprocess to keep track of the job IDs Slurm gives to the jobs and this way I can track if they’re finished or still running.
Now the task going to be called is written in a separate script that is called by the Slurm job.
The basic usage would be something like this:
This would submit 20 jobs per task, which would each take further advantage of parallel processing.
The essential code for the class is here:
In a second level of parallelization, a regular map-reduce operation is also employed. Here I request the help of the parmap module (a wrapper to multiprocessing), since multiprocessing.map() does not allow several arguments passed to the function:
Also, collections.Counter objects are really usefull and one can reduce them by summation.
I illustrate the complete implementation of the Class with an example which takes several genomic regions (combinations of H3K4me3 or H3K27me3 peaks) and compute an output (coverage, density, etc…) under those peaks.
I add more functions to the main Object to perform tasks such as removal of temporary files (pickles, sh file, logs…) and to check if job is finished and output is of the right form.