Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <mpi.h>
- #include <random>
- int main(int argc, char** argv) {
- int proc_num, rank;
- double start_time;
- int received_message;
- int flag = 0;
- MPI_Init(&argc, &argv);
- MPI_Comm_rank(MPI_COMM_WORLD, &rank);
- MPI_Comm_size(MPI_COMM_WORLD, &proc_num);
- start_time = MPI_Wtime();
- while (MPI_Wtime() - start_time < 1) {
- if (rank % 2 == 1) {
- int address = 1;
- while (address % 2 == 1) {
- std::random_device rand_value;
- address = rand_value() % proc_num;
- }
- if (MPI_Wtime() - start_time < 0.00005) {
- MPI_Send(&rank, 1, MPI_INT, address, 0, MPI_COMM_WORLD);
- }
- MPI_Iprobe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &flag, MPI_STATUS_IGNORE);
- if (flag) {
- MPI_Recv(&received_message, 1, MPI_INT, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
- printf("Process %d received a message from process% d\n", rank, received_message);
- }
- }
- else {
- MPI_Iprobe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &flag, MPI_STATUS_IGNORE);
- if (flag) {
- MPI_Recv(&received_message, 1, MPI_INT, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
- printf("Process %d received a message from process %d\n", rank, received_message);
- }
- int address = 0;
- while (address % 2 == 0) {
- std::random_device rand_value;
- address = rand_value() % proc_num;
- }
- if (MPI_Wtime() - start_time < 0.00005) {
- MPI_Send(&rank, 1, MPI_INT, address, 0, MPI_COMM_WORLD);
- }
- }
- }
- printf("Process %d finished \n", rank);
- MPI_Finalize();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement