Я делаю умножение целочисленных массивов в программировании MPI с использованием метода разброса и сбора. У меня есть два целочисленных массива, каждый из которых состоит из 16 элементов. Я успешно умножил разбросанные массивы и получил выходные данные, но позволил объединить их как выходные данные в последовательности. Мне нужно объединить или объединить разбросанные массивы, но я не могу найти код, который объединяет n массивов.
import java.util.stream.Stream;
import mpi.*;
public class Test {
public static void main(String[] args) {
// TODO InputArray1uto-generInputArray1ted method stub
MPI.Init(args); // InitiInputArray1lize MPI setup and functionalities
int me = MPI.COMM_WORLD.Rank(); // My process ID (or Rank)
int size = MPI.COMM_WORLD.Size(); // Total number of MPI processors
int chunk = 4;
int process =4;
int mul=0;
int n =16;
int[] InputArray1 = new int[16]; // Input Array 1
int[] InputArray2= new int[16]; // Input Array 2
int[] tempArray = new int[chunk];
int[] ProcessArray1 = new int[chunk]; // Process Array
int[] ProcessArray2= new int[chunk]; // Process Array
if (me==0)
{
InputArray1[0] =1;
InputArray1[1] =2;
InputArray1[2] =3;
InputArray1[3] =4;
InputArray1[4] =5;
InputArray1[5] =6;
InputArray1[6] =7;
InputArray1[7] =8;
InputArray1[8] =9;
InputArray1[9] =10;
InputArray1[10] =11;
InputArray1[11] =12;
InputArray1[12] =13;
InputArray1[13] =14;
InputArray1[14] =15;
InputArray1[15] =16;
InputArray2[0] =1;
InputArray2[1] =2;
InputArray2[2] =3;
InputArray2[3] =4;
InputArray2[4] =5;
InputArray2[5] =6;
InputArray2[6] =7;
InputArray2[7] =8;
InputArray2[8] =9;
InputArray2[9] =10;
InputArray2[10] =11;
InputArray2[11] =12;
InputArray2[12] =13;
InputArray2[13] =14;
InputArray2[14] =15;
InputArray2[15] =16;
}
MPI.COMM_WORLD.Scatter(InputArray1, 0, chunk, MPI.INT, ProcessArray1, 0, chunk, MPI.INT, 0);
System.out.println("Process " + me + " data " + Arrays.toString(ProcessArray1));
MPI.COMM_WORLD.Scatter(InputArray2, 0, chunk, MPI.INT, ProcessArray2, 0, chunk, MPI.INT, 0);
System.out.println("Process " + me + " data " + Arrays.toString(ProcessArray2));
//MPI.COMM_WORLD.Bcast(InputArray2, 0, n/size, MPI.INT, 0);
//System.out.println("Process " + me + " data " + Arrays.toString(ProcessInputArray2));
// Array Multiplication
for (int i = 0; i < chunk; i++)
tempArray[i] = ProcessArray1[i]*ProcessArray1[i];
System.out.println("Partial Multiplication is "+ Arrays.toString(tempArray));
MPI.COMM_WORLD.Gather(tempArray, 0, 1, MPI.INT, tempArray, 0, 1, MPI.INT, 0);
int[] result = merge(tempArray);
for ( int x: result )
System.out.print( String.format("%3d", x) );
MPI.Finalize();
}
// Merge the Arrays
final static
public int[] merge(final int[] ...arrays ) {
int size = 0;
for ( int[] a: arrays )
size += a.length;
int[] res = new int[size];
int destPos = 0;
for ( int i = 0; i < arrays.length; i++ ) {
if ( i > 0 ) destPos += arrays[i-1].length;
int length = arrays[i].length;
System.arraycopy(arrays[i], 0, res, destPos, length);
}
return res;
}
}
The output of the folloing code is
MPJ Express (0.44) is started in the multicore configuration
Process 3 data [13, 14, 15, 16]
Process 2 data [9, 10, 11, 12]
Process 0 data [1, 2, 3, 4]
Process 3 data [13, 14, 15, 16]
Partial Multiplication is [169, 196, 225, 256]
Process 2 data [9, 10, 11, 12]
Partial Multiplication is [81, 100, 121, 144]
81100 81144
Process 1 data [5, 6, 7, 8]
Process 0 data [1, 2, 3, 4]
Partial Multiplication is [1, 4, 9, 16]
Process 1 data [5, 6, 7, 8]
Partial Multiplication is [25, 36, 49, 64]
1 25 81169169196 81169 25 25 49 64
'''