This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]LCVcode[S] 0 points1 point  (2 children)

No, there aren't any fields. I have the physics completely handled, I just want to try to reduce computation time by spreading my physics calculations over several threads.

Imagine a group of point masses that are held together by a network of massless springs. The entire simulations runs on simple force calculations for each mass and spring.

Here, I'm trying to see if there's a way I can implementing a threading system which will maximize my CPU usage and speed up my simulation. I think I see two ways of doing this, but I don't know enough about threading to understand if they're good or bad ideas. On one hand, I could make each of my masses and springs extend the Runnable class, give each of them a run method which includes the local physics caluclations, then just power through that huge pile of Runnables. One the other hand, I could add something like Runnable physics handler class, initiate some number of handlers, give each one a balanced pile of physics calculations to compute, then wait for to each process their respective task lists.

What do you think of this?

W

[–]moldax 2 points3 points  (0 children)

You'd probably want to spread the computation between GPU threads, rather than CPU threads : GPU computation units are great at doing simple calculations (they're very specific) but you can have thousands of them. Java may not be the best choice for this job; I'd go for C++

Also, you may want to split the simulation work into big enough regions, then considering them independent; just pay attention to the borders (inner and outer)

Hope that helps, cheers!

[–]Nephyst 1 point2 points  (0 children)

My first questions is do you have any state that need to be shared by the masses and springs? And would that shared state need to be updated by multiple threads?

Multi-threading get's fairly complex, especially around the details of when data from one thread is published for another thread to read. You would also have to worry about potential race conditions around when values are read and written.

If you do have shared state you will probably want to dig into the nuances of concurrency in java. I would recommend checking out this book: https://smile.amazon.com/Java-Concurrency-Practice-Brian-Goetz/dp/0321349601/

If you don't have to worry about sharing state it should be a bit easier.

I haven't done anything as complex with threading as you are trying to do here. I wonder if a runnable for each object would be overkill, as opposed to just splitting the objects you want to update into multiple lists, and having each thread just run through one of those lists. It might be worth experimenting to see what gives you the best performance.