OPM/opm-upscaling — pull request #129 by jorgekva (2014-09-30 12:16:48+00:00)
Title: Refactors to prepare for multithreading.

Rewrites a very C-esque routine to a more value oriented function. Functionally
equivalent, but now exposes decent opportunities for thread-level parallelism.

I was poking about in the source when I noticed this method. By rewriting the algorithm a little bit it boils down to basically three reduce operations - one to build the map*, one to accumulate outflow and one to accumulate outflow.

Honestly I'm not too sure just how expensive these computations are, as I have yet to trace every code path. However, since it does a 3x iteration over a grid the work done could easily be significant.

This pull request is more of a suggestion and a discussion starter. Personally I find my take much more readable and easier to maintain, and has less in-line logical branches to follow. Using threading or MPI or some other technique, most of the work can be done in parallel.

Building the map must be completed before inflow is calculated, but can be done in parallel with outflow. This first draft does not support this, but suggests doing that in one operation (as they mostly rely on the same data). The inflow and outflow calculations can also be done in parallel, as they do not depend on each other - in fact, the procedure is a simple reduce and the accumulation is monoidic.

Regarding the parallelism itself, an obviously simple solution is locally-spawned C++11 threads - however, some other scheme may of course be desired, either global scheduling, MPI or some other technique.

Comments & thoughts?