Advertisement
The_Law

Untitled

Jan 20th, 2022
605
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.     delete [] arr;
  62.     MPI_Reduce(&curr_cnt_in_segment, &cnt_in_segment, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
  63.     MPI_Reduce(&curr_is_wa, &is_wa, 1, MPI_BOOL, MPI_LAND, 0, MPI_COMM_WORLD);
  64.     MPI_Finalize();
  65.  
  66.     if (rank == 0) {
  67.         if (is_wa) {
  68.             cout << "Is weakly ascending. Numbers in [a:b]: " << cnt_in_segment;
  69.         } else {
  70.             cout << -1;
  71.         }
  72.     }
  73.    
  74.     return 0;
  75. }
  76.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement