What is typically the best way to multi thread the assembly loop in a finite element code? Does anyone have experience with implementing this, that they can share? I can think of a couple of ways of doing it:

  1. Divide the mesh into different cell sets, where the elements in each set does not share dofs, such that the threads can add element-stiffness-matrices to the global stiffness matrix directly (without worrying about them accessing the same data).

  2. Divide the mesh into cell sets and create a separate global stiffness matrix for each thread, and then combine the stiffness-matrices at the end.

  3. Dont divide into cellsets, but rather spawn a new thread/tasks for each cell (this can be good for load balancing, if some of the elements are more expensive than others, while some are still elastic).

  • 2
    $\begingroup$ Option 4: don't overthink it. use a thread-safe container to add your sparse matrix triplets in a parallel loop, in whatever order you please. that got my assembly time down to less than 1% of the total runtime. revisit the issue if it becomes a bottleneck! $\endgroup$
    – Charlie S
    Oct 12, 2021 at 1:14

1 Answer 1


It turns out that I have just the right paper for you on this subject: https://www.math.colostate.edu/~bangerth/publications/2013-pattern.pdf

  • $\begingroup$ Thanks, I will definitely read it. Can you summarise what the best approach was? $\endgroup$
    – lijas
    Oct 12, 2021 at 7:42
  • $\begingroup$ If I understood everything correctly, I think that my option 1 was what gave best scalability in your paper. You use some sort of coloring algorithm such that you avoid the need for reduction. $\endgroup$
    – lijas
    Oct 12, 2021 at 11:51
  • $\begingroup$ @iljas No :-) That's what abstract, introduction, and conclusions of the paper are there for :-) $\endgroup$ Oct 12, 2021 at 11:51
  • $\begingroup$ And yes, coloring is what makes it all work. $\endgroup$ Oct 12, 2021 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.