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

查看:28
本文介绍了并行化 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.

我想并行化 for 循环(我的代码在 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天全站免登陆