Registered Member
|
I am writing a container class for particles to be used in a N-body simulation. In the code below (C++11 flavor), particle data (positions, charge, mass etc.) are stored in a slightly expanded Eigen matrix with an interface that makes it easy to replace an already existing particle container. My question is if such a design is expected to give optimal performance when looping over the particles? One could alternatively split the major matrix into matrices for each property, but I suspect this data partitioning could cause cache misses. Any tips are greatly appreciated!
|
Moderator
|
it's ok, but it might be better to store the particles into a rowmajor matrix with accessors to the vector of charges and positions in Particles. This way you could remove one loop, and exploit vectorization:
for (int i=0; i<p.size()-1; ++i) u += p[i].charge() * (p.charges() .cwiseQuotient( (p.positions().rowwise() - p[i]).rowwise().norm() )).sum(); |
Registered Member
|
Thanks, but would this solution not evaluate N^2 pairs while I need only N^2/2 ?
|
Moderator
|
oh yes, you can truncate the computations:
(....).tail(p.size()-i-1).sum(); |
Registered Member
|
Aha, I see. So if I get it correctly, you suggest the following matrix layout
where each row corresponds to a property (x,y,z,q,...), tightly linked in memory? |
Moderator
|
The key point is to provide accessors to the set of charges, positions, etc, replace for loops with Eigen expressions, and then you can easily change between a SoA and AoS by switching between row-major and column major to see what's best.
|
Registered users: Bing [Bot], Google [Bot], Sogou [Bot]