Speeding up force calculations and mutable structs

  • I used @code_warntype on compute_pair_interaction! and got no type issues. I also did the same for a simplified version where I removed all for loops and if statements (basically chose one particle and one neighbor and computed the force instead of looping over all of them). @code_warntype didn’t output anything bad. I’m not entirely sure this means my code is type-stable because there was an incident in the past where @code_warntype came back fine but it turned out that there was a statement like one_of_many_structs_grouped_under_abstract_type.shared_field that needed to be rewritten as ....shared_field::Float64 in order to have performant code.

  • @use_threads is a macro that becomes Threads.@threads if lj.multithreaded is true; otherwise it becomes a normal for loop without threads (see post). With three threads, this gives about a x2 boost to speed compare to a single thread. Note that if I replace @use_threads lj.multithreaded with Threads.@threads, the performance is nearly identical.

  • There are about 17 allocations (2.7 KiB) per function call. 16 of those allocations comes from the threading regardless if I use @use_threads or Threads.@threads. I haven’t quite found where that remaining allocation comes from, though it seems insignificant. Is it possible for @time or @btime to miss some allocations in a function?

Edit: I will try Duane_Wilson’s suggestion with Setfield and see if switching all my structs to immutable has any effect.