并行化一个for循环 [英] Parallelizing a for loop

查看:237
本文介绍了并行化一个for循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个for循环,迭代计算 i 不依赖于前面迭代的计算。

I have a for loop where the computation at iteration i does not depend on the computations done in the previous iterations.

我想为循环(我的代码是在java中)并行化,这样多次迭代的计算就可以在多个处理器上同时运行。我应该创建一个线程来计算每个迭代,即要创建的线程数等于迭代次数(for循环中的迭代次数很多)?如何做到这一点?

I want to parallelize the for loop(my code is in java) so that the computation of multiple iterations can be run concurrently on multiple processors. Should I create a thread for the computation of each iteration, i.e. number of threads to be created is equal to the number of iterations(number of iterations are large in the for loop)? How to do this?

推荐答案

下面是一个小例子,您可能会发现对并行化入门很有帮助。它假设:

Here's a small example that you might find helpful to get started with parallelization. It assumes that:


  1. 创建一个 Input 对象,其中包含每个对象的输入迭代您的计算。
  2. 创建一个 Output 对象,其中包含计算每个迭代输入的输出。

  3. 您希望传入一个输入列表并一次返回所有输出列表。
  4. 您的输入是一个合理的工作要做,所以开销不是太高。
  1. You create an Input object that contains the input for each iteration of your computation.
  2. You create an Output object that contains the output from computing the input of each iteration.
  3. You want to pass in a list of inputs and get back a list of outputs all at once.
  4. Your input is a reasonable chunk of work to do, so overhead isn't too high.

如果您的计算非常简单,那么您可能需要考虑分批处理它们。你可以通过在每个输入中输入100来做到这一点。它使用与系统中的处理器一样多的线程。如果你正在处理纯粹的CPU密集型任务,那么这可能是你想要的数字。如果他们被阻止等待其他事情(磁盘,网络,数据库等),你会想要更高。

If your computation is really simple then you'll probably want to consider processing them in batches. You could do that by putting say 100 in each input. It uses as many threads as there are processors in your system. If you're dealing with purely CPU intensive tasks then that's probably the number you want. You'd want to go higher if they're blocked waiting for something else (disk, network, database, etc.)

public List<Output> processInputs(List<Input> inputs)
        throws InterruptedException, ExecutionException {

    int threads = Runtime.getRuntime().availableProcessors();
    ExecutorService service = Executors.newFixedThreadPool(threads);

    List<Future<Output>> futures = new ArrayList<Future<Output>>();
    for (final Input input : inputs) {
        Callable<Output> callable = new Callable<Output>() {
            public Output call() throws Exception {
                Output output = new Output();
                // process your input here and compute the output
                return output;
            }
        };
        futures.add(service.submit(callable));
    }

    service.shutdown();

    List<Output> outputs = new ArrayList<Output>();
    for (Future<Output> future : futures) {
        outputs.add(future.get());
    }
    return outputs;
}

这篇关于并行化一个for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆