1
import mpi.*   
public class Sum {

    public static void main(String[] args) throws Exception {
          String[] userArgs  = MPI.Init(args);
          int sum =0; 
          int [] partial_sum = new int [1];
          int n;
          int[] array = new int[100];
          int[] array2 = new int[100];

          int i;
          int num_rows = 0;
          int num_procs;
          int an_id;
          int num_rows_to_receive = 0;
          int avg_rows_per_process = 0;
          int num_rows_received;
          int start_row;
          int end_row;
          int num_rows_to_send;

          /* Now replicte this process to create parallel processes.
           * From this point on, every process executes a seperate copy
           * of this program */


          /* find out MY process ID, and how many processes were started. */

          int myrank = MPI.COMM_WORLD.Rank();
          int size = MPI.COMM_WORLD.Size(); 
          int input_size = Integer.parseInt(userArgs[0]);
          if(myrank == 0) {
              avg_rows_per_process = input_size / size;
              n = input_size;
              /* initialize an array */
              for(i = 0; i < n; i++){
                  array[i] = i + 1;  
              }
             System.out.println(n);
             /* distribute a portion of the vector to each child process */

             for(an_id = 1; an_id < size; an_id++) {
                 start_row = an_id*avg_rows_per_process + 1;
                 end_row   = (an_id + 1)*avg_rows_per_process;
                 if((num_rows - end_row) < avg_rows_per_process) end_row = num_rows - 1;
                 num_rows_to_send = end_row - start_row + 1;
                 MPI.COMM_WORLD.Send(array, start_row, num_rows_to_send, MPI.INT, an_id, 0);   
             }
             MPI.COMM_WORLD.Barrier();
             /* and calculate the sum of the values in the segment assigned
              * to the root process */
             sum = 0;
             for(i = 0; i < avg_rows_per_process + 1; i++) {
                 sum += array[i];   
                 } 
             System.out.println("Sum calculated by root process" + sum);

             MPI.COMM_WORLD.Barrier();
             /* and, finally, I collect the partial sums from the slave processes, 
              * print them, and add them to the grand sum, and print it */

             for(an_id = 1; an_id < size; an_id++){
                 MPI.COMM_WORLD.Recv(partial_sum, 0, 1, MPI.INT, 0, 0);
                 System.out.println("Partial sum " + partial_sum[0] + " returned from process " + an_id);
                 sum += partial_sum[0];
             }

             System.out.println("The grand total is: " + sum);
          }
          MPI.COMM_WORLD.Barrier();


             /* I must be a slave process, so I must receive my array segment,
              * storing it in a "local" array, array1. */
          if (myrank!=0){
              array2[0] =0;           
              MPI.COMM_WORLD.Recv(array2, 0, num_rows_to_receive, MPI.INT, 0, 1);
              num_rows_received = num_rows_to_receive;
             /* Calculate the sum of my portion of the array */
             partial_sum[0] = 0;
             for(i = 0; i < num_rows_received; i++) {
                partial_sum[0] += array2[i];
             }
             System.out.println("partial sum from process " + myrank + " is " + partial_sum[0]);
             /* and finally, send my partial sum to the root process */

             MPI.COMM_WORLD.Send(partial_sum, 0 ,1 , MPI.INT, 0, 1);
          }

          MPI.Finalize();
    }
}

I wrote this java code summing an array using MPI. I am new to this. I am little bit confused with where to give my barriers. and this code is with many errors. please give me some advice on this.

here is the error log:

MPJ Express (0.38) is started in the multicore configuration
5
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at runtime.starter.MulticoreStarter$1.run(MulticoreStarter.java:289)
    at java.lang.Thread.run(Unknown Source)
Caused by: mpi.MPIException: java.lang.NullPointerException
    at mpi.BasicType.createWriteBuffer(BasicType.java:192)
    at mpi.Comm.send(Comm.java:362)
    at mpi.Comm.Send(Comm.java:332)
    at Sum.main(Sum.java:67)
    ... 6 more
Caused by: java.lang.NullPointerException
    at mpjbuf.Buffer.putSectionHeader(Buffer.java:628)
    at mpi.BasicType.createWriteBuffer(BasicType.java:189)
    ... 9 more

thanks!

djacob
  • 110
  • 1
  • 9

0 Answers0