高斯消除多线程

时间:2018-07-01 17:08:59

标签: java multithreading gaussian

我正在尝试为Java高斯消除程序编写并行程序。

我在开始时制作了两个随机矩阵A和B,并且我没有使用旋转。

我创建线程时的代码是:

  GaussianElimination threads[] = new GaussianElimination[T];
        long startTime = System.currentTimeMillis();

    for (int k = 0; k < N; k++) {


        /**This for statement creates threads that behave like objects
         * With the start() method we execute the run() proccess .And with
         * Join() the main thread wait for all the other threads to finish
         */
        for (int i = 0; i < T; i++) {
            threads[i] = new GaussianElimination(T, k, i, A, B);

            threads[i].start();
        }
        for (int i = 0; i < T; i++) {
            try {
                threads[i].join();
            } catch (InterruptedException e) {
                System.err.println("this should not happen");
            }
        }
    }


    long endTime = System.currentTimeMillis();
    float time = (endTime - startTime) / 1000.0f;
    System.out.println("Computation time: " + time);

在此之后,run方法是:

class GaussianElimination extends Thread {
    private int myid;
    private double[] B;
    private double[][] A;
    int k;
    int threads;


GaussianElimination(int threads, int k, int myid, double[][] A, double[] B) {
    this.A = A;//Matrix A
    this.B = B;//Matrix B
    this.myid = myid;//Id of thread
    this.k = k;//k value from exterior loop
    this.threads = threads; //size of threads
}

/**Method run() where the threads are running .
 *
 * The distribution of the data are in cyclic mode.
 * e.g. For 3 threads the operation of each thread will be distribute like this:
 * thread 1 = 1,4,7,10...
 * thread 2= 2,5,8,11...
 * thread 3 =3,6,9,12...
 */
public void run() {
    int N = B.length;
    long startTime = System.currentTimeMillis();//Clock starts
    for (int i = myid + k + 1; i < N; i += threads) {
        double factor = A[i][k] / A[k][k];
        B[i] -= factor * B[k];
        for (int j = k; j < N; j++)
            A[i][j] -= factor * A[k][j];
        long endTime = System.currentTimeMillis();
        float time = (endTime - startTime) ;//clock ends
        System.out.println("Computation time of thread: " + time);
    }
  }
}

之后,我要进行反向替换序列,然后打印解决方案。

因此程序正在运行,但不是并行运行。 我试图检查每个线程之间的时间,但没有找到解决方法。

我在Java中找不到很多类似问题的例子,所以我在这里问。

架构和逻辑是否不好,或者程序中是否存在任何编码错误?

这也是我使用的https://www.sanfoundry.com/java-program-gaussian-elimination-algorithm/

的序列号

感谢您的合作!

0 个答案:

没有答案