gpt4 book ai didi

Java 任务 - 内存不足

转载 作者:行者123 更新时间:2023-12-01 12:55:08 26 4
gpt4 key购买 nike

我有这个程序(矩阵相乘并不重要得到结果):

package serie06_ExecutorFramework;

import java.util.Random;

public class Sequenziale {
public static final int NUM_OPERATIONS = 100000;
public static final int MATRIX_SIZE = 64;

public static void main(final String[] args) {
final Random rand = new Random();
System.out.println("Simulazione iniziata");
for (int operation = 0; operation < NUM_OPERATIONS; operation++) {
// Crea matrici
final int[][] m0 = new int[MATRIX_SIZE][MATRIX_SIZE];
final int[][] m1 = new int[MATRIX_SIZE][MATRIX_SIZE];
final int[][] m2 = new int[MATRIX_SIZE][MATRIX_SIZE];

// Inizializza gli array con numeri random
for (int i = 0; i < MATRIX_SIZE; i++)
for (int j = 0; j < MATRIX_SIZE; j++) {
m0[i][j] = rand.nextInt(10);
m1[i][j] = rand.nextInt(10);
}

// Moltiplica matrici
for (int i = 0; i < m0[0].length; i++)
for (int j = 0; j < m1.length; j++)
for (int k = 0; k < m0.length; k++)
m2[i][j] += m0[i][k] * m1[k][j];
}
System.out.println("Simulazione terminata");
}
}

对于一个处理该任务并使用多核的程序,最后我必须绘制该程序的加速,其中

speed-up= sequency time/parallel time

我是这样编写程序的:
任务

package pcepS6;

import java.util.Random;

public class MatrixMoltiplication implements Runnable {
private final int[][] m0;
private final int[][] m1;

public MatrixMoltiplication(int[][] m0, int[][] m1) {
this.m0 = m0;
this.m1 = m1;
}

@Override
public void run() {
final Random rand = new Random();
// Inizializza gli array con numeri random
for (int i = 0; i < S6Esercizio1.MATRIX_SIZE; i++)
for (int j = 0; j < S6Esercizio1.MATRIX_SIZE; j++) {
m0[i][j] = rand.nextInt(10);
m1[i][j] = rand.nextInt(10);
}

// Moltiplica matrici
final int[][] m2 = new int[m0.length][m1.length];
for (int i = 0; i < m0[0].length; i++)
for (int j = 0; j < m1.length; j++)
for (int k = 0; k < m0.length; k++)
m2[i][j] += m0[i][k] * m1[k][j];
}
}

主要

package pcepS6;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class S6Esercizio1 {
public static final int NUM_OPERATIONS = 100000;
public static final int MATRIX_SIZE = 64;
private static int numThreads = 5;
private static final ExecutorService myExec = Executors.newFixedThreadPool(numThreads);


public static void main(final String[] args) {

System.out.println("Simulazione iniziata");
System.out.println("Submit delle tasks ai threads");

for (int operation=0; operation<NUM_OPERATIONS; operation++) {
int[][] a = new int[MATRIX_SIZE][MATRIX_SIZE];
int[][] b = new int[MATRIX_SIZE][MATRIX_SIZE];
myExec.execute( new MatrixMoltiplication(a, b) );
}

//l'executor non accetta piu nessuna ulteriore tasks
myExec.shutdown();
System.out.println("Tutte le tasks sono state inviate ai threads");

while ( !myExec.isTerminated() ) {}
System.out.println("Tutti i threads hanno finito");

}
}

问题是我不能只使用 1 个线程来获取执行时间,在 2 个线程之后获取新时间...直到 20 个线程,所以最后绘制所有时间的加速。
我不能这样做,因为如果我使用少量线程,则会出现以下错误:

Exception in thread "main" java.lang.OutOfMemoryError: GC overhead
limit exceeded at pcepS6.S6Esercizio1.main(S6Esercizio1.java:39)
Exception in thread "pool-1-thread-6" java.lang.OutOfMemoryError: GC
overhead limit exceeded at
pcepS6.MatrixMoltiplication.run(MatrixMoltiplication.java:25) at
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at
java.lang.Thread.run(Unknown Source)


我知道我可以使用newCachedThreadPool方法代替Executors.newFixedThreadPool,但这样我就无法获得特定数量线程的时间。
有可能分配完全错误吗?

最佳答案

你同时只运行5个任务,那你为什么要分配100000个空间呢?仅为正在运行的任务分配矩阵就足够了。

此外,没有理由将矩阵注入(inject) MatrixMoltiplication,因为您不对这些矩阵执行任何操作,只是分配它们。所以你可以改变这个

new MatrixMoltiplication(a, b) 

对此:

new MatrixMoltiplication()

并且仅当执行器调度它们时才分配矩阵:

@Override
public void run() {
a = new int[MATRIX_SIZE][MATRIX_SIZE];
b = new int[MATRIX_SIZE][MATRIX_SIZE];

// rest of the runnable

与您的问题无关的另一条注释:

您可以一步完成,而不是使用 [0-9] 区间中的元素创建两个随机矩阵(然后将它们相乘):

for (int i = 0; i < S6Esercizio1.MATRIX_SIZE; i++)
for (int j = 0; j < S6Esercizio1.MATRIX_SIZE; j++) {
m0[i][j] = rand.nextInt(10) * rand.nextInt(10);
}

它与两个[0-9]随机矩阵的乘积具有相同的性质。

此外,值得注意的是,您不能使用 rand.nextInt(82),因为与 rand.nextInt(10) * rand.nextInt(10) 相比,它的分布不同。

关于Java 任务 - 内存不足,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23971938/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com