I'm making in OpenGL a 2D newtonian gravity simulation with lot of particles following the mouse, changing velocities in one loop that iterates over all particles.
It works fine, but the performance isn't that good, I can get just 60 fps average with 2 milions particles(I have an i7 6700k and a gtx 970).
So I thought multi-threading was the best way to improve that.
To do this, I used OpenMP 2.0(I'm on Visual Studio).
The update loop then becomes:
#pragma omp parallel for
for (int i = 0; i < count; i++)
float vertX = WIDTH/2 * (vertices[i*2]+1);
float vertY = -HEIGHT/2*(vertices[i *2+ 1]+1)+HEIGHT;
float fact = (mouseX - vertX)*(mouseX - vertX) + (mouseY - vertY)*(mouseY - vertY) + 120;
glm::vec2 acc = 3.f / fact*(glm::vec2(mouseX, mouseY) - glm::vec2(vertX, vertY)) * (float)bPressed;
acc.y *= -1;
speed[i*2] += acc.x - speed[i*2]/200;
speed[i *2+ 1] += acc.y - speed[i *2+ 1] / 200;
vertices[i*2] += speed[i*2]*dt;
vertices[i *2+ 1] += speed[i*2+1]*dt;
Your code looks good, there is nothing immediate to improve, but your expectation is too high.
Any additional optimizations would heavily depend on
count and the rest of the code. If you want specific suggestions you would have to provide the code as a distilled [mcve].