简体   繁体   English

为什么在使用MPJ Express的程序中发生异常?

[英]Why does an exception occurs in the program which used MPJ Express?

There is a program that multiplies matrix and vector using MPJ Express. 有一个程序可以使用MPJ Express将矩阵和向量相乘。 The matrix is divided by rows. 矩阵按行划分。 But there is an exception occurs while processing. 但是在处理时会发生异常。 That am I doing wrong? 那我做错了吗?

import java.util.Random;

import mpi.Comm;
import mpi.MPI;

public class Main {
    private static final int rootProcessorRank = 0;
    private static Comm comunicator;
    private static int processorsNumber;
    private static int currentProcessorRank;


    public static void main(String[] args) {
        MPI.Init(args);
        comunicator = MPI.COMM_WORLD;
        currentProcessorRank = comunicator.Rank();
        processorsNumber = comunicator.Size();

        if (currentProcessorRank == rootProcessorRank) {
            rootProcessorAction();
        } else {
            notRootProcessorAction();
        }

        MPI.Finalize();

    }

    public static void rootProcessorAction() {
        int[] matrixVectorSize = new int[] {5};
        int[][] matrix = createAndInitMatrix(matrixVectorSize[0]);
        int[] vector = createAndInitVector(matrixVectorSize[0]);

        for (int i = 1; i < processorsNumber; i++) {
            comunicator.Isend(matrixVectorSize, 0, 1, MPI.INT, i, MPI.ANY_TAG);
            System.out.println("Proc: " + currentProcessorRank + ", send matrixVectorSize");

            comunicator.Isend(vector, 0, vector.length, MPI.INT, i, MPI.ANY_TAG);
            System.out.println("Proc: " + currentProcessorRank + ", send vector");
        }

        int averageRowsPerProcessor = matrix.length / (processorsNumber - 1);
        int[] rowsPerProcessor = new int[processorsNumber];
        int notDistributedRowsNumber = matrix.length;
        for (int i = 1; i < rowsPerProcessor.length; i++) {
            if (i == rowsPerProcessor.length - 1) {
                rowsPerProcessor[i] = notDistributedRowsNumber;
            } else {
                rowsPerProcessor[i] = averageRowsPerProcessor;
                notDistributedRowsNumber -= averageRowsPerProcessor;
            }
        }

        int offset = 0;
        // the processorRows[0] always will be '0'
        for (int i = 1; i < rowsPerProcessor.length; i++) {
            int[] processorRows = new int[1];
            processorRows[0] = rowsPerProcessor[i];
            comunicator.Isend(processorRows, 0, 1, MPI.INT, i, MPI.ANY_TAG);
            comunicator.Isend(matrix, offset, processorRows[0], MPI.OBJECT, i, MPI.ANY_TAG);
            offset += rowsPerProcessor[i];
        }

        // there will be a code that receive a subRecults from all processes.
    }

    public static void notRootProcessorAction() {
        int[] matrixVectorSize = new int[1];
        int[] rowsNumber = new int[1];
        int[] vector = null;
        int[][] subMatrix = null;

        comunicator.Probe(rootProcessorRank, MPI.ANY_SOURCE);
        comunicator.Recv(matrixVectorSize, 0, 1, MPI.INT, rootProcessorRank, MPI.ANY_TAG);
        System.out.println("Proc: " + currentProcessorRank + ", receive matrixVectorSize");

        vector = new int[matrixVectorSize[0]];
        comunicator.Probe(rootProcessorRank, MPI.ANY_SOURCE);
        comunicator.Recv(vector, 0, vector.length, MPI.INT, rootProcessorRank, MPI.ANY_TAG);
        System.out.println("Proc: " + currentProcessorRank + ", receive vector");

        comunicator.Probe(rootProcessorRank, MPI.ANY_SOURCE);
        comunicator.Recv(rowsNumber, 0, 1, MPI.INT, rootProcessorRank, MPI.ANY_TAG);
        System.out.println("Proc: " + currentProcessorRank + ", receive rowsNumber");
        subMatrix = new int[rowsNumber[0]][rowsNumber[0]];

        comunicator.Probe(rootProcessorRank, MPI.ANY_SOURCE);
        comunicator.Recv(subMatrix, 0, subMatrix.length, MPI.OBJECT, rootProcessorRank, MPI.ANY_TAG);
        System.out.println("Proc: " + currentProcessorRank + ", receive subMatrix");

        int[] result = new int[rowsNumber[0]];
        multiplyMatrixVector(subMatrix, vector, result);

        comunicator.Send(result, 0, result.length, MPI.INT, rootProcessorRank, MPI.ANY_TAG);
    }

    private static void multiplyMatrixVector(int[][] matrix, int[] vector, int[] result) {
        for (int i = 0; i < matrix.length; i++) {
            int summ = 0;
            for (int j = 0; j < matrix[i].length; j++) {
                summ += matrix[i][j] * vector[j];
            }
            result[i] = summ;
        }
    }

    private static int[][] createAndInitMatrix(int size) {
        int[][] matrix = new int[size][size];
        Random random = new Random();
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix.length; j++) {
                matrix[i][j] = random.nextInt(100);
            }
        }
        return matrix;
    }

    private static int[] createAndInitVector(int size) {
        int[] vector = new int[size];
        Random random = new Random();
        for (int i = 0; i < vector.length; i++) {
            vector[i] = random.nextInt(100);
        }
        return vector;
    }
}

Here is an exception: 这是一个例外:

MPJ Express (0.44) is started in the multicore configuration java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at runtime.starter.MulticoreStarter$1.run(MulticoreStarter.java:281) at java.lang.Thread.run(Thread.java:745) Caused by: mpi.MPIException: xdev.XDevException: java.lang.NullPointerException at mpi.Comm.isend(Comm.java:944) at mpi.Comm.Isend(Comm.java:885) at Main.rootProcessorAction(Main.java:35) at Main.main(Main.java:20) ... 6 more Caused by: xdev.XDevException: java.lang.NullPointerException at xdev.smpdev.SMPDevice.isend(SMPDevice.java:104) at mpjdev.javampjdev.Comm.isend(Comm.java:1019) at mpi.Comm.isend(Comm.java:941) ... 9 more Caused by: java.lang.NullPointerException at xdev.smpdev.SMPDeviceImp MPJ Express(0.44)在sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)的多核配置java.lang.reflect.InvocationTargetException中启动。在运行时的java.lang.reflect.Method.invoke(Method.java:606)处的DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)。在java.lang.Thread处的start.starter.MulticoreStarter $ 1.run(MulticoreStarter.java:281)。 run(Thread.java:745)原因:mpi.MPIException:xdev.XDevException:mpi.Comm.isend(Comm.java:944)上的java.lang.NullPointerException mpi.Comm.Isend(Comm.java:885)上在Main.main(Main.java:20)的Main.rootProcessorAction(Main.java:35)处...其他6个原因:xdev.smpdev.SMPDevice.isend(SMPDevice。 mpjdev.javampjdev.Comm.isend(Comm.java:1019)处的mpi.Comm.isend(Comm.java:941)处的java:104)...另外9个原因由xdev.smpdev处的java.lang.NullPointerException引起。 SMPDeviceImp l$SendQueue.add(SMPDeviceImpl.java:930) at xdev.smpdev.SMPDeviceImpl$SendQueue.add(SMPDeviceImpl.java:909) at xdev.smpdev.SMPDeviceImpl.isend(SMPDeviceImpl.java:330) at xdev.smpdev.SMPDevice.isend(SMPDevice.java:101) ... 11 more xdev.XDevException: java.lang.NullPointerException at xdev.smpdev.SMPDevice.recv(SMPDevice.java:162) xdev.smpdev上的l $ SendQueue.add(SMPDeviceImpl.java:930)xdev.smpdev.SMPDevice上的xdev.smpdev.SMPDeviceImpl.isend(SMPDeviceImpl.java:330)上的$ SendQueue.add(SMPDeviceImpl.java:909) .isend(SMPDevice.java:101)...还有11个xdev.XDevException:xdev.smpdev.SMPDevice.recv处的java.lang.NullPointerException(SMPDevice.java:162)

In my experience with mpj express, try to avoid using the constants MPI.ANY_SOURCE and MPI.ANY_TAG. 根据我对mpj express的经验,请尝试避免使用常量MPI.ANY_SOURCE和MPI.ANY_TAG。 Set your own tag and the source and you should be fine. 设置自己的标签和源,您应该没问题。 When i was using this constants in my program, sometimes i got random crashes with the xDev.xDevException caused by a null pointer and sometimes it run just fine. 当我在程序中使用此常量时,有时由于空指针而导致xDev.xDevException出现随机崩溃,有时运行正常。

Here a list of the internal constants of the mpj express you should not use as tag aswell, i am showing only the constants that are integers: 这是mpj内部常量列表,表示您也不应将其用作标记,我只显示了整数常量:

public static final int mpi.MPI.NUM_OF_PROCESSORS = 4
public static int mpi.MPI.UNDEFINED = -1
public static int mpi.MPI.THREAD_SINGLE = 1
public static int mpi.MPI.THREAD_FUNNELED = 2
public static int mpi.MPI.THREAD_SERIALIZED = 3
public static int mpi.MPI.THREAD_MULTIPLE = 4
public static int mpi.MPI.ANY_SOURCE = -2
public static int mpi.MPI.ANY_TAG = -2
public static int mpi.MPI.PROC_NULL = -3
public static int mpi.MPI.BSEND_OVERHEAD = 0
public static int mpi.MPI.SEND_OVERHEAD = 0
public static int mpi.MPI.RECV_OVERHEAD = 0
public static final int mpi.MPI.IDENT = 0
public static final int mpi.MPI.CONGRUENT = 3
public static final int mpi.MPI.SIMILAR = 1
public static final int mpi.MPI.UNEQUAL = 2
public static int mpi.MPI.GRAPH = 1
public static int mpi.MPI.CART = 2
public static int mpi.MPI.TAG_UB = 0
public static int mpi.MPI.HOST = 0
public static int mpi.MPI.IO = 0

cheers. 干杯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM