Advertisement
Nevarkir

Untitled

Dec 20th, 2019
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.87 KB | None | 0 0
  1. #include <mpi.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include <math.h>
  6.  
  7. int main(int argc, char** argv) {
  8.     MPI_Init(&argc, &argv);
  9.     int world_size;
  10.     MPI_Comm_size(MPI_COMM_WORLD, &world_size);
  11.     int world_rank;
  12.     MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
  13.    
  14.     int n;
  15.     int *a, *b;
  16.    
  17.     if (world_rank == 0) {
  18.         bool f = false;
  19.         while (!f) {
  20.             //printf("n = ");
  21.             scanf("%i", &n);
  22.             //printf("\n");
  23.             if(n % world_size == 0) {
  24.                 f = true;
  25.             }        
  26.         }
  27.  
  28.         srand(time(NULL));        
  29.        
  30.         a = new int[n*n];
  31.         b = new int[n];
  32.        
  33.         printf("a =\n");
  34.        
  35.         // generating matrix a
  36.  
  37.         bool converge = false;
  38.         while (!converge) {
  39.             for (int i = 0; i < n; i++) {
  40.                 for (int j = 0; j < n; j++) {
  41.                     if (i == j) {
  42.                         a[i*n + j] = 3*n + rand() % 10;
  43.                     }
  44.                     else {                
  45.                         a[i*n + j] = rand() % 4;
  46.                     }
  47.                 }
  48.             }
  49.            
  50.             converge = true;
  51.             for (int i = 0; i < n; i++) {
  52.                 int sum = 0;
  53.                 for (int j = 0; j < n; j++) {
  54.                     if (i != j) {
  55.                         sum += abs(a[i*n + j]);
  56.                     }
  57.                 }
  58.                 if (abs(a[i*n + i]) < sum) {
  59.                     converge = false;
  60.                     break;                
  61.                 }
  62.             }
  63.         }
  64.  
  65.         for (int i = 0; i < n; i++) {
  66.             for (int j = 0; j < n; j++) {
  67.                 printf("%2i ", a[i*n + j]);
  68.             }
  69.             printf("\n");
  70.         }
  71.         printf("\n");  
  72.        
  73.         // vector b
  74.  
  75.         printf("b =\n");
  76.         for (int i = 0; i < n; i++) {
  77.             b[i] = rand() % 10;
  78.             printf("%i ", b[i]);
  79.         }
  80.         printf("\n");
  81.     }  
  82.  
  83.     MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
  84.    
  85.     int *ra = new int[n*n / world_size];
  86.     int *rb = new int[n / world_size];
  87.  
  88.     MPI_Scatter(a, n * n / world_size, MPI_INT, ra, n * n / world_size, MPI_INT, 0, MPI_COMM_WORLD);        
  89.     MPI_Scatter(b, n / world_size, MPI_INT, rb, n / world_size, MPI_INT, 0, MPI_COMM_WORLD);        
  90.    
  91.     double *x0 = new double[n];    
  92.     double *x1 = new double[n];
  93.     double *rx1 = new double[n / world_size];
  94.    
  95.     for (int i = 0; i < n; i++) {
  96.         x0[i] = 0;    
  97.     }
  98.  
  99.     int end = 1;
  100.     int endend = 1;
  101.     double eps = 0.001;
  102.  
  103.     do {
  104.         // calculating next iteration
  105.         for (int i = 0; i < n / world_size; i++) {
  106.    
  107.             int ind = world_rank * n / world_size + i;
  108.          
  109.             double sum = 0;
  110.             for (int j = 0; j < n; j++) {
  111.                 if (ind != j) {
  112.                     sum += ra[i*n + j] * x0[j];
  113.                 }
  114.             }
  115.  
  116.             rx1[i] = 1.0 / ra[i*n + ind] * (rb[i] - sum);
  117.         }      
  118.        
  119.         // checking end condition
  120.         end = 1;
  121.         int tmp = world_rank * n / world_size;
  122.         double maxdiff = fabs(rx1[0] - x0[tmp]);
  123.        
  124.         x0[tmp] = rx1[0];
  125.        
  126.          for (int i = 1; i < n / world_size; i++) {
  127.             if (fabs(rx1[i] - x0[tmp + i]) > maxdiff) {
  128.                 maxdiff = fabs(rx1[i] - x0[tmp + i]);
  129.             }
  130.             x0[tmp + i] = rx1[i];
  131.         }
  132.         if (maxdiff > eps) {
  133.             end = 0;
  134.         }
  135.  
  136.         int *res = new int[world_size];
  137.  
  138.         MPI_Gather(&end, 1, MPI_INT, res, 1, MPI_INT, 0, MPI_COMM_WORLD);
  139.         MPI_Gather(rx1, n / world_size, MPI_DOUBLE, x0, n / world_size, MPI_DOUBLE, 0, MPI_COMM_WORLD);
  140.        
  141.         if (world_rank == 0) {
  142.             endend = 1;
  143.             for (int i = 0; i < world_size; i++) {
  144.                 if (res[i] == 0) {
  145.                     endend = 0;
  146.                     break;
  147.                 }
  148.             }
  149.  
  150.             /*            
  151.             end = 1;
  152.             double maxdiff = fabs(x1[0] - x0[0]);            
  153.             for (int i = 1; i < n; i++) {
  154.                 if (fabs(x1[i] - x0[i]) > maxdiff) {
  155.                     maxdiff = fabs(x1[i] - x0[i]);
  156.                 }
  157.             }
  158.             if (maxdiff > eps) {
  159.                 end = 0;
  160.             }
  161.             */
  162.             //printf("x1[0] = %lf\n", x1[0]);
  163.             //printf("maxdiff = %lf\n", maxdiff);
  164.             //printf("\nx =\n");
  165.         }
  166.  
  167.         MPI_Bcast(&endend, 1, MPI_INT, 0, MPI_COMM_WORLD);
  168.         MPI_Bcast(x0, n, MPI_DOUBLE, 0, MPI_COMM_WORLD);
  169.     }
  170.     while (!endend);
  171.    
  172.     if (world_rank == 0) {
  173.         printf("\nx =\n");
  174.         for (int i = 0; i < n; i++) {
  175.             printf("%lf ", x0[i]);
  176.         }
  177.         printf("\n");
  178.     }
  179.  
  180.     MPI_Finalize();
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement