Reputation: 41
I am implementing the following tutorial (matrix multiplication using Mpi_Scatter and Mpi_Gather) for MPI-based matrix multiplication using Scatter and Gather methods. However, I need to multiply the two integers arrays (1D) using the same scatter and gather method in java programming. Here is my edited code
import java.util.*;
import mpi.*;
public class MPIArrayMultiplication {
public static void main(String[] args) {
// TODO Auto-generated method stub
MPI.Init(args);
int me = MPI.COMM_WORLD.Rank();
int size = MPI.COMM_WORLD.Size();
int chunk =4;
int process =4;
int i=0;
int j=0;
int k =0;
int tag=99;
int blksz=0;
int mul=0;
int n=16;
int Array1[]=new int[] {11,22,3,44,65,60,71,18,90,101,121,112,13,14,10,25}; // First Input Array
int Array2[]=new int[] {11,22,3,44,65,60,71,18,90,101,121,112,13,14,10,25}; // Second Input Array
int[] tempArray = new int[n];
int[] tempArray1 = new int[n];
int[] tempArray2 = new int[n];
MPI.COMM_WORLD.Scatter(Array1, 0, n/size, MPI.INT, tempArray1, 0, n/size, MPI.INT, 0);
System.out.println("Process " + me + " data " + Arrays.toString(tempArray1));
MPI.COMM_WORLD.Bcast(Array2, 0, n, MPI.INT, 0);
for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
{
mul=mul+tempArray1[j]*Array2[j];
}
tempArray2[i] = mul ;
mul =0;
}
MPI.COMM_WORLD.Gather(tempArray2, 0, n/size, MPI.INT, tempArray, 0, n/size, MPI.INT, 0);
MPI.Finalize();
}
}
Upvotes: 0
Views: 239