无法实现具有特定线程数的矩阵乘法。
程序不会停止并显示
线程“ Thread-####”中的异常java.lang.NullPointerException
public Matrix multiply(Matrix a, Matrix b) {
if (a.cols() != b.rows()) {
return null;
}
threads = new Thread[a.cols()][b.rows()];
MatrixMultiplication[] vmmworker = new MatrixMultiplication[this.numThreads];
Thread[] vmmThreads = new Thread[this.numThreads];
barriere = new CyclicBarrier(this.numThreads, new Runnable() {
public void run() {
}});
Matrix c = new Matrix(a.rows(), b.cols());
int n = a.cols();
for (int f = 0; f < this.numThreads; f++) {
int blocksize = (int) Math.ceil((double) a.cols() / this.numThreads ) ;
int start = f * blocksize;
int end = Math.min((f+1)*blocksize, n);
for (int i = start; i < end; i++) {
for (int j = 0; j<b.cols(); j++) {
threads[i][j] = new Thread(new MatrixMultiplication(a,b,c,start,end));
threads[i][j].start();
}
}
vmmworker[f] = new MatrixMultiplication(a, b, c, start, end);
vmmThreads[f] = new Thread(vmmworker[f]);
vmmThreads[f].start();
}
public void run() {
for (int i = start; i < end; i++) {
for (int j = 0; j<b.cols(); j++) {
for (int k = 0; k < a.cols(); k++) {
double cij = c.get(i, j);
double aik = a.get(i, k);
double bkj = b.get(k, j);
c.set(i, j, cij + aik * bkj);
}
}
try { barriere.await(); }
catch (InterruptedException e) { e.printStackTrace(); }
catch (BrokenBarrierException e) { e.printStackTrace(); }}
我想创建ijk
循环,并以其开头和结尾。