import mpi.*;
public class lab8 {
public static void main(String[] args) throws MPIException {
// Initialize MPI
MPI.Init(args);
int MyRank = MPI.COMM_WORLD.Rank(); // Get the rank of the current process
int Numprocs = MPI.COMM_WORLD.Size(); // Get the number of processes
int tag = 100;
// Message buffers
String send_message = MyRank + ": Hello";
String recv_message = new String(new char[50]); // Buffer to receive message
// Send message to all other processes
for (int i = 0; i < Numprocs; i++) {
if (MyRank != i) {
// Send message to process i
MPI.COMM_WORLD.Send(send_message.getBytes(), send_message.length(), MPI.BYTE, i, tag);
}
}
// Receive message from all other processes
for (int i = 0; i < Numprocs; i++) {
if (MyRank != i) {
// Receive message from process i
MPI.COMM_WORLD.Recv(recv_message.getBytes(), 50, MPI.BYTE, i, tag);
System.out.println("Process " + MyRank + " received message from process " + i + ": " + new String(recv_message));
}
}
// Finalize MPI
MPI.Finalize();
}
}
aW1wb3J0IG1waS4qOwpwdWJsaWMgY2xhc3MgbGFiOCB7CiAgICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKSB0aHJvd3MgTVBJRXhjZXB0aW9uIHsKICAgICAgICAvLyBJbml0aWFsaXplIE1QSQogICAgICAgIE1QSS5Jbml0KGFyZ3MpOwoKICAgICAgICBpbnQgTXlSYW5rID0gTVBJLkNPTU1fV09STEQuUmFuaygpOyAgLy8gR2V0IHRoZSByYW5rIG9mIHRoZSBjdXJyZW50IHByb2Nlc3MKICAgICAgICBpbnQgTnVtcHJvY3MgPSBNUEkuQ09NTV9XT1JMRC5TaXplKCk7IC8vIEdldCB0aGUgbnVtYmVyIG9mIHByb2Nlc3NlcwogICAgICAgIGludCB0YWcgPSAxMDA7CgogICAgICAgIC8vIE1lc3NhZ2UgYnVmZmVycwogICAgICAgIFN0cmluZyBzZW5kX21lc3NhZ2UgPSBNeVJhbmsgKyAiOiBIZWxsbyI7CiAgICAgICAgU3RyaW5nIHJlY3ZfbWVzc2FnZSA9IG5ldyBTdHJpbmcobmV3IGNoYXJbNTBdK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