MPI: Send & Recv: Difference between revisions
Appearance
Created page with "<code>MPI_Send</code> dan <code>MPI_Recv</code> ada "point-to-point" function untuk komunikasi antar proses." |
No edit summary |
||
| Line 1: | Line 1: | ||
<code>MPI_Send</code> dan <code>MPI_Recv</code> ada "point-to-point" function untuk komunikasi antar proses. | <code>MPI_Send</code> dan <code>MPI_Recv</code> ada "point-to-point" function untuk komunikasi antar proses. | ||
<syntaxhighlight lang=cpp> | |||
#include <mpi.h> | |||
#include <iostream> | |||
int main(int argc, char** argv) { | |||
MPI_Init(NULL, NULL); | |||
int world_rank; | |||
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); | |||
int world_size; | |||
MPI_Comm_size(MPI_COMM_WORLD, &world_size); | |||
if (world_size < 2) { | |||
printf("Minimum 2 processes required\n"); | |||
MPI_Abort(MPI_COMM_WORLD, 1); | |||
} | |||
int number; | |||
if (world_rank == 0) { | |||
number = 777; | |||
MPI_Send( | |||
&number, // data | |||
1, // count | |||
MPI_INT, // datatype | |||
1, // destination | |||
0, // tag | |||
MPI_COMM_WORLD // communicator | |||
); | |||
printf("Process 0 send number %d to Process 1\n", number); | |||
} else if (world_rank == 1) { | |||
MPI_Recv( | |||
&number, // data | |||
1, // count | |||
MPI_INT, // datatype | |||
0, // source | |||
0, // tag | |||
MPI_COMM_WORLD, // communicator | |||
MPI_STATUS_IGNORE | |||
); | |||
printf("Process 1 succesfully retrieve number %d from Process 0!\n", number); | |||
} | |||
MPI_Finalize(); | |||
return 0; | |||
} | |||
</syntaxhighlight> | |||
Latest revision as of 07:07, 13 March 2026
MPI_Send dan MPI_Recv ada "point-to-point" function untuk komunikasi antar proses.
#include <mpi.h>
#include <iostream>
int main(int argc, char** argv) {
MPI_Init(NULL, NULL);
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
if (world_size < 2) {
printf("Minimum 2 processes required\n");
MPI_Abort(MPI_COMM_WORLD, 1);
}
int number;
if (world_rank == 0) {
number = 777;
MPI_Send(
&number, // data
1, // count
MPI_INT, // datatype
1, // destination
0, // tag
MPI_COMM_WORLD // communicator
);
printf("Process 0 send number %d to Process 1\n", number);
} else if (world_rank == 1) {
MPI_Recv(
&number, // data
1, // count
MPI_INT, // datatype
0, // source
0, // tag
MPI_COMM_WORLD, // communicator
MPI_STATUS_IGNORE
);
printf("Process 1 succesfully retrieve number %d from Process 0!\n", number);
}
MPI_Finalize();
return 0;
}