Advertisement
The_Law

Untitled

Feb 9th, 2022
1,135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.74 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.  
  24.         //process init
  25.         int size, rank;
  26.         MPI_Status status;
  27.         MPI_Init(&argc, &argv);
  28.         MPI_Comm_size(MPI_COMM_WORLD, &size);
  29.         MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  30.        
  31.         set_time(rank);
  32.         int block_size = n / size + (rank < n % size); // difference between procs <= 1 element
  33.         vector< vector<double> > part_matrix(block_size, vector<int> (block_size)); //init from task;
  34.         vector< vector<double> > src_matrix(block_size, vector<int> (block_size)); //copy;
  35.         double *lb = new double[n];
  36.         MPI_Request requests[(rank > 0) * 2 + (rank < size - 1) * 2];
  37.         int req_id = 0;
  38.         if (rank > 0) {
  39.             MPI_Irecv(lb, n, MPI_DOUBLE, rank - 1, 0, MPI_COMM_WORLD, &requests[req_id++]);
  40.             double *s_lb = new double[n];
  41.             for (int i = 0; i < n; ++i) {
  42.                 s_lb[i] = part_matrix[0][i];
  43.             }
  44.             MPI_Isend(s_lb, n, MPI_DOUBLE, rank - 1, 0, MPI_COMM_WORLD, &requests[req_id++]);
  45.         }
  46.         int *rb = new int[n];
  47.         if (rank < size - 1) {
  48.             MPI_Irecv(rb, n, MPI_DOUBLE, rank + 1, 0, MPI_COMM_WORLD, &requests[req_id++]);
  49.             int *r_lb = new int[n];
  50.             for (int i = 0; i < n; ++i) {
  51.                 s_rb[i] = part_matrix[block_size - 1][i];
  52.             }
  53.             MPI_Isend(r_lb, n, MPI_INT, rank + 1, 0, MPI_COMM_WORLD, &requests[req_id++]);
  54.         }
  55.         double s;
  56.         double curr_s = 0;
  57.         for (int i = 0; i < block_size; ++i) {
  58.             for (int j = 0; j < n; ++j) {
  59.                 if (j == 0 || j == n - 1) {
  60.                     curr_sum += src_matrix[i][j];
  61.                     continue;
  62.                 }
  63.                 if (i == 0 && rank != 0 && block_size > 1) {
  64.                     part_matrix[i][j] = (
  65.                         src_matrix[i][j - 1] + src_matrix[i][j + 1] +
  66.                         lb[j] + src_matrix[i + 1][j]
  67.                     ) / 4;
  68.                 } else if (i == 0 && rank != 0 && block_size == 1) {
  69.                     part_matrix[i][j] = (
  70.                         src_matrix[i][j - 1] + src_matrix[i][j + 1] +
  71.                         lb[j] + rb[j]
  72.                     ) / 4;
  73.                 } else if (i == block_size - 1 && rank != size - 1 && block_size > 1) {
  74.                     part_matrix[i][j] = (
  75.                         src_matrix[i][j - 1] + src_matrix[i][j + 1] +
  76.                         src_matrix[i - 1][j] + rb[j]
  77.                     ) / 4;
  78.                 } else {
  79.                     part_matrix[i][j] = (
  80.                         src_matrix[i][j - 1] + src_matrix[i][j + 1] +
  81.                         src_matrix[i - 1][j] + src_matrix[i + 1][j]
  82.                     ) / 4;
  83.                 }
  84.                 curr_sum += part_matrix[i][j];
  85.             }
  86.         }
  87.         MPI_Allreduce(&curr_s, &s, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
  88.         s /= n * n;
  89.         int cnt;
  90.         int curr_cnt = 0;
  91.         for (int i = 0; i < block_size; ++i) {
  92.             for (int j = 0; j < n; ++j) {
  93.                 if (part_matrix[i][j] > s) {
  94.                     ++curr_cnt;
  95.                 }
  96.             }
  97.         }
  98.         MPI_Allreduce(&curr_cnt, &cnt, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
  99.         if (rank == 0)
  100.             cout << cnt;
  101.         MPI_Finalize();
  102.        
  103.     }
  104.    
  105.     return 0;
  106. }
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement