Advertisement
maxim_shlyahtin

MPI

Oct 15th, 2024
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.87 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <mpi.h>
  4. #include <random>
  5. int main(int argc, char** argv) {
  6.     int proc_num, rank;
  7.     double start_time;
  8.     int received_message;
  9.     int flag = 0;
  10.     MPI_Init(&argc, &argv);
  11.     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  12.     MPI_Comm_size(MPI_COMM_WORLD, &proc_num);
  13.     start_time = MPI_Wtime();
  14.     while (MPI_Wtime() - start_time < 1) {
  15.         if (rank % 2 == 1) {
  16.             int address = 1;
  17.             while (address % 2 == 1) {
  18.                 std::random_device rand_value;
  19.                 address = rand_value() % proc_num;
  20.             }
  21.             if (MPI_Wtime() - start_time < 0.00005) {
  22.                 MPI_Send(&rank, 1, MPI_INT, address, 0, MPI_COMM_WORLD);
  23.             }
  24.             MPI_Iprobe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &flag, MPI_STATUS_IGNORE);
  25.             if (flag) {
  26.                 MPI_Recv(&received_message, 1, MPI_INT, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  27.                 printf("Process %d received a message from process% d\n", rank, received_message);
  28.             }
  29.         }
  30.         else {
  31.             MPI_Iprobe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &flag, MPI_STATUS_IGNORE);
  32.             if (flag) {
  33.                 MPI_Recv(&received_message, 1, MPI_INT, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  34.                 printf("Process %d received a message from process %d\n", rank, received_message);
  35.             }
  36.             int address = 0;
  37.             while (address % 2 == 0) {
  38.                 std::random_device rand_value;
  39.                 address = rand_value() % proc_num;
  40.             }
  41.             if (MPI_Wtime() - start_time < 0.00005) {
  42.                 MPI_Send(&rank, 1, MPI_INT, address, 0, MPI_COMM_WORLD);
  43.             }
  44.         }
  45.     }
  46.     printf("Process %d finished \n", rank);
  47.     MPI_Finalize();
  48.     return 0;
  49. }
  50.  
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement