0

Im trying to create a distributed MPI class in java which will do some computations for me. Unfortunately, MPI.Init seems to need argv,argc which are in main class. Would there be a way to initialize MPI in this class?

public class distributed {

    public distributed(int mat[][],int n,int m) throws MPIException {

        MPI.Init(argv); // issue is here,i dont have "argv" to initialize with,causing null pointer exceptions in variables like "myrank"

        final int myrank = MPI.COMM_WORLD.Rank();
        final int size = MPI.COMM_WORLD.Size();

        final int rows = n;
        final int rowChunk = (rows+size-1)/size;

        final int startRow = myrank *rowChunk;
        int endRow = (myrank+1)*rowChunk;

        int[] newRow = new int[m];
           }
}

1 Answers1

0

Why not initializing myrank and size in your main method. Then pass them as parameters of your Distributed class constructor ?

public class Distributed {

    public Distributed(int mat[][],int n,int m, int myrank, int size) {
        // These two variables are now parameters of the constructor
        // final int myrank = MPI.COMM_WORLD.Rank();
        // final int size = MPI.COMM_WORLD.Size();

        final int rows = n;
        final int rowChunk = (rows+size-1)/size;

        final int startRow = myrank *rowChunk;
        int endRow = (myrank+1)*rowChunk;

        int[] newRow = new int[m];
    }

    public static void main (String [] args) {
        MPI.Init(args, args.length); 
        final int myrank = MPI.COMM_WORLD.Rank();
        final int size = MPI.COMM_WORLD.Size();

        Distributed d = new Distributed (..., myrank, size);
    }
}
Patrick
  • 1,458
  • 13
  • 27