Java 中的并行方法和顺序方法有什么区别?
what is a difference between parallel and sequential method in Java?
我目前正在尝试编码 Java 8。昨天我找到了下一个 source,其中包含线性代数的示例。在矩阵向量乘法(link 中的最后一个示例)的基础上,我编写了自己的矩阵乘法方法。我的代码在这里:
import java.util.stream.IntStream;
public static void matrixMatrixProduct() {
System.out.println("Matrix matrix mulptiplication");
final int DIM1 = 15;
final int DIM2 = 20;
final int DIM3 = 2;
int[][] a = new int[DIM1][DIM2];
int[][] b = new int[DIM1][DIM3];
int[][] c = new int[DIM3][DIM2];
int counter = 1;
for (int i =0; i < DIM1; i++) {
for (int j =0; j < DIM3; j++) {
b[i][j] = counter++;
}
}
for (int i =0; i < DIM3; i++) {
for (int j =0; j < DIM2; j++) {
c[i][j] = counter++;
}
}
System.out.println("");
System.out.println("Print matrix b");
System.out.println("");
for (int i = 0; i < DIM1; i++) {
for (int j = 0; j < DIM3; j++) {
System.out.print(b[i][j] + " ");
}
System.out.print("\n");
}
System.out.println("");
System.out.println("Print matrix c");
System.out.println("");
for (int i = 0; i < DIM3; i++) {
for (int j = 0; j < DIM2; j++) {
System.out.print(c[i][j] + " ");
}
System.out.print("\n");
}
IntStream.range(0, DIM1)
.parallel()
.forEach( (i) -> {
IntStream.range(0, DIM2)
.sequential()
.forEach( (j) -> {
IntStream.range(0, DIM3)
.parallel()
.forEach( (k) -> {
a[i][j] += b[i][k]*c[k][j];
});
});
});
System.out.println("");
System.out.println("Print matrix a");
System.out.println("");
for (int i = 0; i < DIM1; i++) {
for (int j = 0; j < DIM2; j++) {
System.out.print(a[i][j] + " ");
}
System.out.print("\n");
}
我的问题是,通过IntStream
class的调用,parallel()
和sequential()
方法到底有什么区别? (实际上这是最重要的部分,我在这里明确地进行了乘法运算)。基于这些知识,我想知道,上次 IntStream
调用中的正确用法是什么。目前我已经在这个地方定义了方法 parallel()
,但我不确定这是一个正确的解决方案......实际上,如果我将 parallel()
更改为 sequential()
我看不到输出的任何差异。
并行 操作同时发生。这意味着可以同时处理流的元素。
顺序 操作一次发生一个。
从 sequential()
更改为 parallel()
对结果没有影响,因为您的操作与状态无关,但可能会影响您的 运行 时间。但是,如果您执行的操作会影响以后的操作,那么您应该考虑使用 sequential()
.
我假设你希望矩阵运算并行发生,但我对数学不太熟悉。
Link 到 Javadoc,由 @the8472 引用。
我目前正在尝试编码 Java 8。昨天我找到了下一个 source,其中包含线性代数的示例。在矩阵向量乘法(link 中的最后一个示例)的基础上,我编写了自己的矩阵乘法方法。我的代码在这里:
import java.util.stream.IntStream;
public static void matrixMatrixProduct() {
System.out.println("Matrix matrix mulptiplication");
final int DIM1 = 15;
final int DIM2 = 20;
final int DIM3 = 2;
int[][] a = new int[DIM1][DIM2];
int[][] b = new int[DIM1][DIM3];
int[][] c = new int[DIM3][DIM2];
int counter = 1;
for (int i =0; i < DIM1; i++) {
for (int j =0; j < DIM3; j++) {
b[i][j] = counter++;
}
}
for (int i =0; i < DIM3; i++) {
for (int j =0; j < DIM2; j++) {
c[i][j] = counter++;
}
}
System.out.println("");
System.out.println("Print matrix b");
System.out.println("");
for (int i = 0; i < DIM1; i++) {
for (int j = 0; j < DIM3; j++) {
System.out.print(b[i][j] + " ");
}
System.out.print("\n");
}
System.out.println("");
System.out.println("Print matrix c");
System.out.println("");
for (int i = 0; i < DIM3; i++) {
for (int j = 0; j < DIM2; j++) {
System.out.print(c[i][j] + " ");
}
System.out.print("\n");
}
IntStream.range(0, DIM1)
.parallel()
.forEach( (i) -> {
IntStream.range(0, DIM2)
.sequential()
.forEach( (j) -> {
IntStream.range(0, DIM3)
.parallel()
.forEach( (k) -> {
a[i][j] += b[i][k]*c[k][j];
});
});
});
System.out.println("");
System.out.println("Print matrix a");
System.out.println("");
for (int i = 0; i < DIM1; i++) {
for (int j = 0; j < DIM2; j++) {
System.out.print(a[i][j] + " ");
}
System.out.print("\n");
}
我的问题是,通过IntStream
class的调用,parallel()
和sequential()
方法到底有什么区别? (实际上这是最重要的部分,我在这里明确地进行了乘法运算)。基于这些知识,我想知道,上次 IntStream
调用中的正确用法是什么。目前我已经在这个地方定义了方法 parallel()
,但我不确定这是一个正确的解决方案......实际上,如果我将 parallel()
更改为 sequential()
我看不到输出的任何差异。
并行 操作同时发生。这意味着可以同时处理流的元素。
顺序 操作一次发生一个。
从 sequential()
更改为 parallel()
对结果没有影响,因为您的操作与状态无关,但可能会影响您的 运行 时间。但是,如果您执行的操作会影响以后的操作,那么您应该考虑使用 sequential()
.
我假设你希望矩阵运算并行发生,但我对数学不太熟悉。
Link 到 Javadoc,由 @the8472 引用。