简体   繁体   English

Java运行并行程序

[英]Java running parallel programs

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. 我有两个预定义的100 * 100矩阵,我想执行它们的乘法运算。 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( ie multiplications of rows) and then join these three different results into a single matrix. 我想要的是,不是简单的乘法,但我想把它分成三个独立的程序,第一个程序将执行前33行的乘法,第二个程序将执行从33到64的乘法,第三个将乘以64到100的乘法现在我想并行运行这些程序并收集它们的结果(即行的乘法),然后将这三个不同的结果连接成一个矩阵。 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. 我会非常感谢你们。

Java has this sort of thing built-in. Java内置了这种东西。 The documentation is http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/Executors.html 该文档是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. 这是基本概要。

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 ) . 对于这种情况,我会使用已经存在的库,例如EJML( http://code.google.com/p/efficient-java-matrix-library/ )或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 希望有所帮助

Here's a parallel java library that I use in college for most of my parallel java projects. 是我在大学里用于大多数并行java项目的并行java库

It was written by my Parallel Programming professor, Alan Kaminsky , who is awesome (a few students have contributed to it as well). 它由我的并行编程教授艾伦·卡明斯基Alan Kaminsky )撰写,他很棒(一些学生也为此做出了贡献)。

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). 在下载页面上需要注意的重要事项:用法部分,文档部分和系统要求(特别是关于java 5如何比java 6或7更好地执行的部分)。

A good starting point might be edu.rit.pj.test.Test13 , which performs a matrix operation. 一个好的起点可能是edu.rit.pj.test.Test13 ,它执行矩阵运算。

A class very important to this implementation is edu.rit.pj.Comm , which is responsible for most passing of data between threads. 对此实现非常重要的一个类是edu.rit.pj.Comm ,它负责线程之间的大多数数据传递。 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. 请务必阅读本课程的文档,特别注意broadcast()gather() ,这应该是您首次将矩阵分发到节点的方式,然后最终收集并合并结果。

It's a lot to take in, I know. 我知道,要吸收很多东西。 I'll try to write an example later. 我稍后会尝试写一个例子。

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. 我不相信100乘100是非常大的并且当并行完成时你不会看到大幅加速,这将是预优化的典型例子,特别是如果你的操作系统很忙。

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. jblas http://mikiobraun.github.io/jblas/被认为是java最快的BLAS库。
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 但无论如何,我会向您推荐一个完成任务的答案,这些任务依赖于使用java并发 api的其他任务的输出: 在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. 这将为您提供另一个以您描述的方式解决此问题的示例,但我认为该方法比使用jblas慢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM