Advertisement
Guest User

Untitled

a guest
Sep 30th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.82 KB | None | 0 0
  1. #include "mpi.h"
  2. #include <iostream>
  3. #include <ctime>
  4. using namespace std;
  5.  
  6. #define MAX_VALUE 10
  7. #define VECTOR_SIZE 10
  8. const int MAIN_PROCESS = 0;
  9.  
  10. class MPI_Process
  11. {
  12. private:
  13.     int rank;
  14.     int processCount;
  15.  
  16. public:
  17.     MPI_Process(int argc, char *argv[])
  18.     {
  19.         MPI_Init(&argc, &argv);
  20.         MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  21.         MPI_Comm_size(MPI_COMM_WORLD, &processCount);
  22.     }
  23.  
  24.     ~MPI_Process()
  25.     {
  26.         MPI_Finalize();
  27.     }
  28.  
  29.     int getRank()
  30.     {
  31.         return rank;
  32.     }
  33.  
  34.     int getProcessCount()
  35.     {
  36.         return processCount;
  37.     }
  38.  
  39.     bool isMain()
  40.     {
  41.         return (rank == MAIN_PROCESS);
  42.     }
  43. };
  44.  
  45. void createVector(double *, int, int);
  46. void printVector(double *, int);
  47.  
  48. int main(int argc, char *argv[])
  49. {
  50.     srand((unsigned)time(0));
  51.  
  52.     MPI_Process process(argc, argv);
  53.  
  54.     int     sendSize = VECTOR_SIZE / process.getProcessCount();
  55.     int     leftSize = VECTOR_SIZE % process.getProcessCount();
  56.     double  *vector1 = nullptr;
  57.     double  *vector2 = nullptr;
  58.     int     recvSize = process.getRank() >= leftSize ? sendSize : sendSize + 1;
  59.     double  *buffer1 = new double[recvSize];
  60.     double  *buffer2 = new double[recvSize];
  61.     int     *sendCounts = nullptr;
  62.     int     *displs = nullptr;
  63.    
  64.     if (process.isMain())
  65.     {
  66.         vector1 = new double[VECTOR_SIZE];
  67.         vector2 = new double[VECTOR_SIZE];
  68.  
  69.         createVector(vector1, VECTOR_SIZE, MAX_VALUE);
  70.         createVector(vector2, VECTOR_SIZE, MAX_VALUE);
  71.  
  72.         cout << "Vector 1 = ";
  73.         printVector(vector1, VECTOR_SIZE);
  74.         cout << "Vector 2 = ";
  75.         printVector(vector2, VECTOR_SIZE);
  76.  
  77.         sendCounts = new int[process.getProcessCount()];
  78.         displs = new int[process.getProcessCount()];
  79.  
  80.         for (int i = 0; i < process.getProcessCount(); i++)
  81.         {
  82.             sendCounts[i] = i >= leftSize ? sendSize : sendSize + 1;
  83.             if (i == 0) {
  84.                 displs[i] = 0;
  85.             }
  86.             else {
  87.                 displs[i] = displs[i - 1] + sendCounts[i - 1];
  88.             }
  89.         }
  90.     }
  91.  
  92.     MPI_Scatterv(vector1, sendCounts, displs, MPI_DOUBLE, buffer1, recvSize, MPI_DOUBLE, MAIN_PROCESS, MPI_COMM_WORLD);
  93.     MPI_Scatterv(vector2, sendCounts, displs, MPI_DOUBLE, buffer2, recvSize, MPI_DOUBLE, MAIN_PROCESS, MPI_COMM_WORLD);
  94.     for (int i = 0; i < recvSize; i++) {
  95.         buffer1[i] -= buffer2[i];
  96.     }
  97.     delete[] buffer2;
  98.  
  99.     MPI_Gatherv(buffer1, recvSize, MPI_DOUBLE, vector1, sendCounts, displs, MPI_DOUBLE, MAIN_PROCESS, MPI_COMM_WORLD);
  100.     delete[] buffer1;
  101.     delete[] sendCounts;
  102.     delete[] displs;
  103.     if (process.isMain())
  104.     {
  105.         cout << "Vector1 - Vector2 = ";
  106.         printVector(vector1, VECTOR_SIZE);
  107.         delete[] vector2;
  108.         delete[] vector1;
  109.     }
  110.     return 0;
  111. }
  112.  
  113. void createVector(double *out, int size, int maxValue)
  114. {
  115.     for (int i = 0; i < size; i++) {
  116.         out[i] = (double)(rand() % (200 * maxValue + 1) - maxValue * 100) / 100;
  117.     }
  118. }
  119.  
  120. void printVector(double *vec, int size)
  121. {
  122.     cout << "(";
  123.     for (int i = 0; i < size - 1; i++) {
  124.         cout << vec[i] << ", ";
  125.     }
  126.     cout << vec[size - 1] << ")\n";
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement