SHARE
TWEET

Untitled

a guest Dec 6th, 2019 96 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <mpi.h>
  2. #include <stdio.h>
  3. #include <iostream>
  4. #include <cmath>
  5.  
  6. using namespace std;
  7.  
  8. const double eps = 0.001;
  9.  
  10. int main(int argc, char** argv) {
  11.     srand(time(0));
  12.     int size;
  13.     MPI_Init(&argc,  &argv);
  14.     int world_size;
  15.     MPI_Comm_size(MPI_COMM_WORLD, &world_size);
  16.     int world_rank;
  17.     MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
  18.     if(world_rank == 0) {
  19.         std::cout<< "Size: ";
  20.         std::cin >> size;
  21.     }
  22.     MPI_Bcast(&size, 1, MPI_INT, 0, MPI_COMM_WORLD);
  23.     double *A = new double[size];
  24.     double *F = new double[size];
  25.     double *X = new double[size];
  26.     if(world_rank == 0) {
  27.         for (int i = 0; i < size; i++) {
  28.             for (int j = 0; j < size; j++) {
  29.                 A[i*size + j] = rand()%10;
  30.             }
  31.             for (int j = 1; j < size; j++) {
  32.                 A[i*size + i] += A[i*size + j];
  33.             }
  34.             for (int j = 0; j < size; j++) {
  35.                 cout << A[i*size + j] << " ";
  36.             }
  37.             cout << endl;
  38.             F[i] = rand()%10;
  39.             X[i] = F[i];
  40.         }
  41.         cout << endl;
  42.         for(int i = 0; i < size; i++) {
  43.             cout << F[i] << " ";
  44.         }
  45.         cout << endl;
  46.     }
  47.     int localSize = (size * size)/world_size;
  48.     double *localA = new double[localSize];
  49.     MPI_Scatter(A, localSize, MPI_DOUBLE, localA, localSize, MPI_DOUBLE, 0, MPI_COMM_WORLD);
  50.     MPI_Bcast(F, size, MPI_DOUBLE, 0, MPI_COMM_WORLD);
  51.     double *TempX = new double[size];
  52.     double norm;
  53.     double *allNorms = new double[world_size];
  54.     double *allX = new double[size];
  55.     int count = 0;
  56.     do {
  57.         count++;
  58.         for (int i = 0; i < localSize/size; i++) {
  59.             TempX[i] = F[i];
  60.             for (int j = 0; j < size; j++) {
  61.                 if (i != j) {
  62.                     TempX[i] -= localA[i*size+j] * X[j];
  63.                 }
  64.             }
  65.             TempX[i] /= localA[i*size+i];
  66.         }
  67.         MPI_Gather(TempX, localSize/size, MPI_DOUBLE, TempX, localSize/size, MPI_DOUBLE, 0, MPI_COMM_WORLD);
  68.         MPI_Bcast(TempX, size, MPI_DOUBLE, 0, MPI_COMM_WORLD);
  69.         norm = fabs(X[0] - TempX[0]);
  70.         for (int i = 0; i < localSize/size; i++) {
  71.             if (fabs(X[i] - TempX[i]) > norm) {
  72.                 norm = fabs(X[i] - TempX[i]);
  73.             }
  74.             X[i] = TempX[i];
  75.         }
  76.         MPI_Gather(&norm, 1, MPI_DOUBLE, allNorms, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
  77.         if(world_rank == 0) {
  78.             for (int i = 0; i < world_size; i++) {
  79.                 if(allNorms[i]>norm) {
  80.                     norm = allNorms[i];
  81.                 }
  82.             }
  83.         }
  84.         MPI_Bcast(&norm, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
  85.  
  86.     } while (norm > eps);
  87.     MPI_Gather(X, localSize/size, MPI_DOUBLE, allX, localSize/size, MPI_DOUBLE, 0, MPI_COMM_WORLD);
  88.     if(world_rank == 0) {
  89.         cout << "iter: " << count << endl;
  90.         for (int i = 0; i < size; i++) {
  91.             cout << "X[" << i << "] = " << allX[i] << endl;
  92.         }
  93.     }
  94.     MPI_Finalize();
  95. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top