【发布时间】:2013-12-13 11:48:40
【问题描述】:
我正在并行执行图像处理程序,我有 4 个内核,所以当我在少于 5 个线程上运行程序时,应该比仅在 1 个线程上运行时有所改进,我想没有任何改进,因为图片可能太小,因此线程会产生开销。所以我使用 8142x2175 图像仍然没有任何改进。问题可能出在 Eclipse 上吗?
这是我的图像处理程序的主要部分,我包括其中一个类,如您所见,我正在使用 Java 并在 eclipse 上运行它。
public class Main {
public static void main(String[] args) throws IOException {
final File imageFile= new File ("C:\\Users\\user\\Desktop\\Parallel\\pic8142x2175.jpg");
BufferedImage orgImg= ImageIO.read(imageFile);
// the destination buffered image
BufferedImage destImg = new BufferedImage(orgImg.getWidth(), orgImg.getHeight(),
BufferedImage.TYPE_INT_RGB);
// let the user enter number of thread
System.out.println(" please enter number of threads");
Scanner in = new Scanner(System.in);
int nThreads=in.nextInt();
// create the threads
ExecutorService executor = Executors.newFixedThreadPool(nThreads);
// assign each thread with its own runnable
for (int i=0; i<nThreads;i++){
//Runnable gsr = new GreyscaleRunnable(orgImg, i, nThreads,destImg);
//Runnable pr = new PixelateRunnable(orgImg,20,i,nThreads,destImg);
Runnable fr= new FlipRunnable(orgImg,i,nThreads,destImg);
executor.execute(fr);
}// end of for loop
// This will make the executor accept no new threads
// and finish all existing threads in the queue
executor.shutdown();
// Wait until all threads are finish
}
}
FlipRunnable 类
public class FlipRunnable implements Runnable{
private BufferedImage img;
private BufferedImage dest;
private int my_rank, thread_count;
// constructor of the class
public FlipRunnable(BufferedImage img,int my_rank,int thread_count,BufferedImage dest) {
this.img=img;
this.my_rank=my_rank;
this.thread_count=thread_count;
this.dest=dest;
}
long startTime = System.currentTimeMillis();
@Override
public void run() {
int localRows = img.getHeight() / thread_count;
int myFirstRow = my_rank * localRows;
int myLastRow = (my_rank + 1) * localRows - 1;
for (int i = myFirstRow; i <= myLastRow; i++) {
for (int j = 0; j < img.getWidth(); j++){
int px = img.getRGB(j, i);
dest.setRGB(img.getWidth() - j - 1, i, px);
}// end of j loop
}// end of i loop
String dstName = "C:\\Users\\user\\Desktop\\Parallel\\funFlipped.jpg";
File dstFile = new File(dstName);
try {
ImageIO.write(dest, "jpg", dstFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Output image: " + dstName);
// the total time
long endTime = System.currentTimeMillis();
long totalTime = endTime - startTime;
System.out.println("time is:"+totalTime);
}
}
【问题讨论】:
-
首先,您的每个工人似乎都覆盖了目标图像...
-
另外,
setRGB()是synchronized,所以工人之间肯定有很多争用。似乎你最好将图像复制到普通数组并在那里工作。 -
是的,我希望工作人员相互覆盖,这样我就可以获得整个图像的输出,而不仅仅是线程正在处理的部分。对不起我的英语,我想说的是当我没有让工人覆盖时,我的输出只是图像的一部分,这样我就可以完成整个图像
-
@VictorSorokin 是对的。此外,如果您想真正加快使用踏板的速度,您应该利用您的处理器内核。为此,您应该使用 Fork/Join 框架。如果你不这样做,你最终会在线程之间切换处理器,这会减慢你的操作
-
我认为这个question 已经存在了。