Jump to content
  • Advertisement
Sign in to follow this  
Cacks

[java] Threading math operations

This topic is 3886 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys, I want to thread my matrix multiplication. My code would be something like: public Matrix4x4 mult(Matrix4x4 matrix4x4) { element[0] = row1.dotProduct(matrix4x4.row1); element[1] = row1.dotProduct(matrix4x4.row2); element[2] = row1.dotProduct(matrix4x4.row3); ... etc } Is there anyway in Java that I can do all the dotProducts simultaneously & exit the method knowing that all the dotProducts have been completed?

Share this post


Link to post
Share on other sites
Advertisement
This isn't an answer to your question - but I believe the amount of time taken to create multiple threads and waiting for them to finish is going to be much larger than the time taken to do a few dot-products.
I recommend profiling (timing) the threaded and the non-threaded version of this function to see which is faster.

Share this post


Link to post
Share on other sites
Quote:
Original post by Cacks
Is there anyway in Java that I can do all the dotProducts simultaneously & exit the method knowing that all the dotProducts have been completed?


Yes, it's the same as in any language.

- Create a thread pool
- Partition the array between threads (for n threads and m elements, let each thread compute m/n elements)
- Wait till all threads report completion

Since Java uses lightweight threads, they don't map directly to system threads, meaning efficiency of such parallelism will be poor. You will also gain no benefit unless you're using multi-core machine.

To wait for threads, wait on a lock in a while loop with a counter. Let threads call notify on it. Once all threads have called notify, you can exit the while loop.

There might be better synchronization primitives available in Java 6, but I haven't looked into them in detail yet.

Share this post


Link to post
Share on other sites
Hi guys,

good point, the threading of this would make performance slower. The code would get very complicated & I would have loads of threads all fighting one another to be executed. Just going to stick with the single thread approach.

Cheers

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!