我有一个很长的双重嵌套的for循环来进行许多试验。我想同时做到这一点,因为这些试验是相互独立的。我如何在Java中像使用C++的OpenMP一样高效地实现它?我将在具有64个处理器的节点上运行此操作,因此我希望每个内核都可以采取一项措施。在Java中for循环的并行化
相关代码:
//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);
}
}
}
编辑:问题仍然没有解决:
//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);
}
});
错误:
了解线程:https://docs.oracle.com/javase/tutorial/essential/concurrency/ – Michael
你可以尝试使用[并行流](https://docs.oracle.com/javase/tutorial/collections/streams/parallelism.html#executing_streams_in_parallel) - 尽管您可能需要更改'someFunc'签名以直接接受数组项,而不是其数组项在阵列中的位置。 –