# Code parallelization for Brownian Dynamics simulation of hard spheres

**URL:** <https://discourse.julialang.org/t/code-parallelization-for-brownian-dynamics-simulation-of-hard-spheres/64584>\
**Category:** Performance\
**Tags:** parallel\
**Created:** [July 13, 2021, 3:44pm UTC](https://discourse.julialang.org/t/code-parallelization-for-brownian-dynamics-simulation-of-hard-spheres/64584 "2021-07-13T15:44:28Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [July 13, 2021, 7:02pm UTC](https://discourse.julialang.org/t/code-parallelization-for-brownian-dynamics-simulation-of-hard-spheres/64584/3 "2021-07-13T19:02:14Z")

</div>

Note that most of your code can be written in a dimension-independent manner. But, in particular, change

> [@Amir\_Abbasi](#):
>
> particle.f = SVector{2,Float64}(particle.f .+ [f\_x,f\_y])

to:

```julia
particle.f = particle.f + SVector{2,Float64}(f_x,f_y)

```

and that will remove the allocation of the `[f_x,f_y]` intermediate vector. Removing this allocations will speedup the code in general, and may have implications for the scaling at the end.

To improve scaling you will need to improve the distribution of tasks, for example by splitting the tasks in `nthreads` chuncks of similar size. I have done a cell list implementation that does that reasonably here: [https://github.com/m3g/CellListMap.jl/blob/1629b7ac8c621d002eeb6294385f65f9c8d4eb17/src/CellListMap.jl#L740](https://github.com/m3g/CellListMap.jl/blob/1629b7ac8c621d002eeb6294385f65f9c8d4eb17/src/CellListMap.jl#L740). But that is not ideal yet, probably.

@Skoffer gave a lecture on how to that properly here: [Nbabel nbody integrator speed up - #32 by Skoffer](https://discourse.julialang.org/t/nbabel-nbody-integrator-speed-up/51712/32)

---

_[View the full topic](https://discourse.julialang.org/t/code-parallelization-for-brownian-dynamics-simulation-of-hard-spheres/64584)._
