package myproj;
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();
}
}
cGFja2FnZSBteXByb2o7CmltcG9ydCBtcGkuKjsKcHVibGljIGNsYXNzIGxhYjggewogICAgcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nW10gYXJncykgdGhyb3dzIE1QSUV4Y2VwdGlvbiB7CiAgICAgICAgLy8gSW5pdGlhbGl6ZSBNUEkKICAgICAgICBNUEkuSW5pdChhcmdzKTsKCiAgICAgICAgaW50IE15UmFuayA9IE1QSS5DT01NX1dPUkxELlJhbmsoKTsgIC8vIEdldCB0aGUgcmFuayBvZiB0aGUgY3VycmVudCBwcm9jZXNzCiAgICAgICAgaW50IE51bXByb2NzID0gTVBJLkNPTU1fV09STEQuU2l6ZSgpOyAvLyBHZXQgdGhlIG51bWJlciBvZiBwcm9jZXNzZXMKICAgICAgICBpbnQgdGFnID0gMTAwOwoKICAgICAgICAvLyBNZXNzYWdlIGJ1ZmZlcnMKICAgICAgICBTdHJpbmcgc2VuZF9tZXNzYWdlID0gTXlSYW5rICsgIjogSGVsbG8iOwogICAgICAgIFN0cmluZyByZWN2X21lc3NhZ2UgPSBuZXcgU3RyaW5nKG5ldyBj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