The_Law

Untitled

Feb 9th, 2022
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 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.  
  56. for (int i = 0; i < bloc_size; ++i) {
  57. for (int j = 1; j < n - 1; ++j) {
  58. if (i == 0 && rank != 0 && block_size > 1) {
  59. part_matrix[i][j] = (
  60. src_matrix[i][j - 1] + src_matrix[i][j + 1] +
  61. lb[j] + src_matrix[i + 1][j]
  62. ) / 4;
  63. } else 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] + rb[j]
  67. ) / 4;
  68. } else if (i == block_size - 1 && rank != size - 1 && block_size > 1) {
  69. part_matrix[i][j] = (
  70. src_matrix[i][j - 1] + src_matrix[i][j + 1] +
  71. src_matrix[i - 1][j] + rb[j]
  72. ) / 4;
  73. } else {
  74. part_matrix[i][j] = (
  75. src_matrix[i][j - 1] + src_matrix[i][j + 1] +
  76. src_matrix[i - 1][j] + src_matrix[i + 1][j]
  77. ) / 4;
  78. }
  79. }
  80. }
  81.  
  82. }
  83.  
  84. return 0;
  85. }
  86.  
Advertisement
Add Comment
Please, Sign In to add comment