Advertisement
The_Law

Untitled

Jan 20th, 2022
838
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.33 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <sstream>
  4. #include <vector>
  5.  
  6. #include "mpi.h"
  7.  
  8. using namespace std;
  9.  
  10. void set_time(int rank) {
  11.   srand(rank);
  12. }
  13.  
  14. int main(int argc, char** argv)
  15. {
  16.     if (argc != 2) {
  17.         cerr << "Invalid count of arguments";
  18.     } else {
  19.         size_t n;
  20.         stringstream convert1(argv[1]);
  21.         if (!(convert >> n))
  22.             n = DEFAULT_N;
  23.         size_t a;
  24.         stringstream convert1(argv[2]);
  25.         if (!(convert >> a))
  26.             a = DEFAULT_A;
  27.         size_t b;
  28.         stringstream convert1(argv[3]);
  29.         if (!(convert >> b))
  30.             b = DEFAULT_B;
  31.  
  32.         //process init
  33.         int size, rank;
  34.         MPI_Status status;
  35.         MPI_Init(&argc, &argv);
  36.         MPI_Comm_size(MPI_COMM_WORLD, &size);
  37.         MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  38.        
  39.         set_time(rank);
  40.  
  41.         int block_size = n / size + (rank < n % size); // difference between procs <= 1 element
  42.         bool is_wa = true;
  43.         int cnt_in_segment = 0;
  44.  
  45.         bool curr_is_wa = true;
  46.         int curr_cnt_in_segment = 0;
  47.  
  48.         int *arr = new int[block_size];
  49.         for (int i = 0; i < block_size; ++i) {
  50.             arr[i] = rand();
  51.         }
  52.  
  53.         for (int i = 0; i < block_size; ++i) {
  54.             if ((i > 0) && (arr[i - 1] > arr[i])) {
  55.                 curr_is_wa = false;
  56.             }
  57.             if (a <= arr[i] && arr[i] <= b) {
  58.                 ++curr_cnt_in_segment;
  59.             }
  60.         }
  61.  
  62.         int lb;
  63.         MPI_Request requests[(rank > 0) * 2];
  64.         if (rank > 0) {
  65.             MPI_Isend(arr[0], 1, MPI_INT, std::max(rank - 1, 0), 0, MPI_COMM_WORLD, &requests[0]);
  66.             MPI_Irecv(lb, 1, MPI_INT, std::max(rank - 1, 0), 0, MPI_COMM_WORLD, &requests[1]);
  67.             if (lb > arr[0]) {
  68.                 is_wa = false;
  69.             }
  70.         }
  71.  
  72.         delete [] arr;
  73.         MPI_Reduce(&curr_cnt_in_segment, &cnt_in_segment, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
  74.         MPI_Reduce(&curr_is_wa, &is_wa, 1, MPI_BOOL, MPI_LAND, 0, MPI_COMM_WORLD);
  75.         MPI_Finalize();
  76.  
  77.         if (rank == 0) {
  78.             if (is_wa) {
  79.                 cout << "Is weakly ascending. Numbers in [a:b]: " << cnt_in_segment;
  80.             } else {
  81.                 return -1;
  82.             }
  83.         }
  84.     }
  85.    
  86.     return 0;
  87. }
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement