Advertisement
The_Law

Untitled

Jan 20th, 2022
825
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.48 KB | None | 0 0
  1. #include <vector>
  2.  
  3. #include "mpi.h"
  4.  
  5. using namespace std;
  6.  
  7. int main(int argc, char** argv)
  8. {
  9.     int n, a, b;
  10.     n = atoi(argv[1]);
  11.     a = atoi(argv[2]);
  12.     b = atoi(argv[3]);
  13.  
  14.     int size, rank;
  15.     MPI_Status status;
  16.     MPI_Init(&argc, &argv);
  17.     MPI_Comm_size(MPI_COMM_WORLD, &size);
  18.     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  19.    
  20.     srand(rank);
  21.     int blocksize = n / size;
  22.     if (rank < n % size)
  23.         ++blocksize;
  24.    
  25.     bool flag = true;
  26.     bool proc_flag = true;
  27.     int res = 0;
  28.     int proc_res = 0;
  29.  
  30.     int arr[blocksize];
  31.     for (int i = 0; i < block_size; ++i) {
  32.         arr[i] = rand();
  33.         if ((i > 0) && (arr[i - 1] > arr[i])) {
  34.             proc_falg = false;
  35.         }
  36.         if (a <= arr[i] && arr[i] <= b) {
  37.             ++proc_res;
  38.         }
  39.     }
  40.  
  41.     int bound;
  42.     MPI_Request request1;
  43.     MPI_Request request2;
  44.     if (rank > 0) {
  45.         MPI_Isend(arr[0], 1, MPI_INT, std::max(rank - 1, 0), 0, MPI_COMM_WORLD, &request1);
  46.         MPI_Irecv(lb, 1, MPI_INT, std::max(rank - 1, 0), 0, MPI_COMM_WORLD, &request2);
  47.         if (bound > arr[0]) {
  48.             proc_flag = false;
  49.         }
  50.     }
  51.  
  52.     MPI_Reduce(&proc_res, &res, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
  53.     MPI_Reduce(&proc_flag, &flag, 1, MPI_BOOL, MPI_LAND, 0, MPI_COMM_WORLD);
  54.     MPI_Finalize();
  55.  
  56.     if (rank == 0) {
  57.         if (flag) {
  58.             cout << res;
  59.         } else {
  60.             return -1;
  61.         }
  62.     }
  63.    
  64.     return 0;
  65. }
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement