简体   繁体   English

Java中for循环的并行化

[英]Parallelization of a for loop in Java

I have a long doubly-nested for loop to go over for many trials. 我有一个很长的双嵌套for循环来进行许多试验。 I want to do this in parallel because these trials are independent of one another. 我想同时这样做,因为这些试验是相互独立的。 How do I implement this efficiently in Java similar to OpenMP in C++? 如何在Java中有效地实现这一点,类似于C ++中的OpenMP? I would be running this on a node with 64 processors, so I want each core to do one measure. 我会在64个处理器的节点上运行它,所以我希望每个核心都做一个测量。

Relevant code: 相关代码:

//I want each measure to perform the doubly nested loop at the same time.

for (int i : measures) {

  for (int j = 0; j < N; j++) {
    for (int k = 0; k < N; k++) {
    array[i*N*N + j*N + k] = someFunc(i,j,k);
    }
  }

}

Edit: Still having issues: 编辑:仍有问题:

//sim is a 1D array of type double
//gM is an array of type SMconf
//gene[foo].annot is a LinkedHashSet of URIs.
//Javadoc http://www.semantic-measures-library.org/sml/docs/apidocs/

Arrays.parallelSetAll( sim, i -> {
    try {
        engine.compare( gM[i/(N*N)], gene[(i/N)%N].annot, gene[i % N].annot );
    }
    catch (SLIB_Ex_Critic ex) {
        Logger.getLogger(Exp2.class.getName()).log(Level.SEVERE, null, ex);
    }
});

Error: 错误:

在此输入图像描述

    int N=5;
    int array[]=new int[200];
    int [] measures={1,2,3,4,5};

    Arrays.stream(measures).parallel().forEach(i->{
        IntStream.range(0, N).parallel().forEach(j->{
            IntStream.range(0, N).parallel().forEach(k->{
                array[i*N*N+j*N + k]= someFunc(i,j,k);
            });
        });
    });


    Arrays.stream(array).forEach(System.out::println);

Considering measures is an array and not an ArrayList . 考虑measures是一个数组而不是ArrayList You may want to put locks while writing to array[] . 您可能希望在写入array[]放置锁。 I hope you are not using array as a variable name. 我希望你没有使用array作为变量名。

Probably it would be moreless efficient to use Java-8 method Arrays.parallelSetAll : 使用Java-8方法Arrays.parallelSetAll可能会更有效率:

Arrays.parallelSetAll(array, idx -> someFunc(idx/(N*N), (idx/N)%N, idx % N));

This sets the array elements independently in parallel. 这将独立地并行设置数组元素。 While you may have an overhead for division, it's probably much less than overhead for creating nested parallel streams as in @pallavt answer. 虽然你可能有一个划分开销,但它可能比创建嵌套并行流的开销要小得多,就像@pallavt回答一样。 Though it may depend on problem size. 虽然它可能取决于问题的大小。

If your someFunc throws checked exception, rethrow it as unchecked one: 如果someFunc抛出已检查的异常,请将其重新抛出为未选中的异常:

Arrays.parallelSetAll(array, idx -> {
    try {
        return someFunc(idx/(N*N), (idx/N)%N, idx % N);
    }
    catch(MyCheckedException ex) {
        throw new RuntimeException(ex);
    }
});

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

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