m3g / CellListMap.jl

Flexible implementation of cell lists to map the calculations of particle-pair dependent functions, such as forces, energies, neighbor lists, etc.
https://m3g.github.io/CellListMap.jl/
MIT License
87 stars 4 forks source link

Control number of threads #64

Closed jgreener64 closed 2 years ago

jgreener64 commented 2 years ago

We are currently looking at implementing replica exchange methods in Molly (https://github.com/JuliaMolSim/Molly.jl/pull/64), which involves running a number of simulations in parallel. In the case of 4 replicas you might want to use 4 cores per replica if you had 16 cores, with the neighbour lists for each replica being separate.

It seems in CellListMap.jl that you can set parallel=true and this will use a number of cores up to Threads.nthreads():

https://github.com/m3g/CellListMap.jl/blob/38dc3137c06332d1d18a9dad218fdc7db5fb9aef/src/CellLists.jl#L226

Is there a way to say not just parallel=true but to set a maximum number of threads? If not, how easily could this feature be added? This would make it easier to use CellListMap.jl with multiple replica simulations.

lmiq commented 2 years ago

Yes, the parameter is nbatches from the CellList constructor (see here).

Basically, the CellList object has to be constructed passing the number of batches desired with:

julia> cl = CellList(x,box,nbatches=(1,4))
CellList{3, Float64}
  1000000 real particles.
  1000 cells with real particles.
  1727449 particles in computing box, including images.

where (1,4) are the number of batches that will be launched in the cell list construction step, and in the function computation step. Thus, for example, if you want to limit the number of batches used by CellListMap to 4, you can use nbatches=(4,4). (The number of batches of the cell list construction step is better not be greater than more or less 8, because that step does not scale very well).

We call that batches, because that is the nomenclature used by TKF in his implementations of parallelization. One important thing is that from start it is a good idea to decouple the number of batches from the effective number of threads (as it is now in CellListMap, you can also set nbatches > nthreads, and sometimes this is actually a good option for performance, particularly if the batches are not very homogeneous, when the faster batches end, new ones are launched and no processor gets idle).

jgreener64 commented 2 years ago

Great, thanks for that.