Reputation: 540
I have a requirement and i am not sure if that can be fulfilled by using threads. I have two predefined 100*100 matrices and i want to perform their multiplication. What i want is, not simple multiplication but i want to divide this into three separate programs , first program will do multiplication of first 33 rows, second program will do multiplication of rows from 33 to 64 and third will do multiplication of rows 64 to 100. Now i want to run these programs in parallel and collect their results( i.e. multiplications of rows) and then join these three different results into a single matrix. Now, i have developed three such programs or classes but i am not able to find any way to run them parallel. In threads, i read that we can not be sure which thread will execute first and which will execute later.
Can anyone please give me any idea which technique i can use for my problem?
I will be very thankful to you guys.
Upvotes: 1
Views: 2377
Reputation: 8137
I do not believe that a 100 by 100 is very large and you wont be seeing a massive speed up when completed in parallel, this and would be a classic example of pre-optimisation, especially if your OS is busy.
Secondly why are you trying to re-invent the wheel?
jblas
http://mikiobraun.github.io/jblas/ is considered to be the fastest BLAS
library for java.
Also how can we tell you what's wrong with your code if you don't post it?
But anyway I will refer you to a answer I gave for completing tasks which are dependent on the output of other tasks using the java concurrency api: Executing Dependent tasks in parallel in Java
Please check that out.
That will give you another example of doing this problem in the way you described however I think the approach would be slower than using jblas.
Upvotes: 0
Reputation: 149
For this situation, I would use a library already out there, such as EJML (http://code.google.com/p/efficient-java-matrix-library/) or la4j (http://la4j.org) . In all likely hood the library will produce results much faster. You could also move to a different language more suited for mathematics, however I believe that is out of the scope of this question. If you are adamant on using threads, since these are all run in parallel, whomever completes first would be of no consequence, you would just have to make sure they all complete and then reassemble.
Hope that helps
Upvotes: 1
Reputation: 2030
Java has this sort of thing built-in. The documentation is http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/Executors.html
Create each object, throw it on thread pool, and then run them all.
ExecutorService service = Executors.newFixedThreadPool(3);
service.submit(matrixMult1);
service.submit(matrixMult2); // the objects that do the work
service.submit(matrixMult3);
service.shutdown();
service.awaitTermination(1, TimeUnit.HOURS);
This is the basic outline.
Upvotes: 2
Reputation: 13196
Here's a parallel java library that I use in college for most of my parallel java projects.
It was written by my Parallel Programming professor, Alan Kaminsky, who is awesome (a few students have contributed to it as well).
Important things to notice on the download page: the usage section, the documentation section, and the system requirements (especially the part about how java 5 tends to perform better than java 6 or 7).
A good starting point might be edu.rit.pj.test.Test13
, which performs a matrix operation.
A class very important to this implementation is edu.rit.pj.Comm
, which is responsible for most passing of data between threads. Make sure you read the documentation for this class, paying special attention to the sections for broadcast()
and gather()
, which should be the way you first distribute your matrix to your nodes, and then finally collect and combine the result.
It's a lot to take in, I know. I'll try to write an example later.
Upvotes: 1