Advertisement
osipyonok

ppl2

Apr 24th, 2017
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.18 KB | None | 0 0
  1. // Par_prog_lab1.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <cmath>
  7. #include <mpi.h>
  8. #include <ctime>
  9. #include <vector>
  10. #include <algorithm>
  11. #include <iostream>
  12. #include <string>
  13. #include <fstream>
  14.  
  15. using namespace std;
  16.  
  17.  
  18. int main(int argc, char* argv[])
  19. {
  20.  
  21.     int n;
  22.  
  23.     int worldSize;
  24.     int worldRank;
  25.  
  26.     MPI_Init(&argc, &argv);
  27.     MPI_Comm_size(MPI_COMM_WORLD, &worldSize);
  28.     MPI_Comm_rank(MPI_COMM_WORLD, &worldRank);
  29.     MPI_Status status;
  30.     fstream file;
  31.  
  32.     if (worldRank == 0) {
  33. //      file.open("input_stupid.txt", fstream::in | fstream::out | fstream::app);
  34. //      file.open("input_no_ans.txt" , fstream::in | fstream::out | fstream::app);
  35.         file.open("input_big.txt", fstream::in | fstream::out | fstream::app);
  36.     }
  37.  
  38.     if (worldRank == 0) {
  39.         file >> n;
  40.         for (int i = 1; i < worldSize; ++i) {
  41.             MPI_Send(&n, 1, MPI_INT, i, 98, MPI_COMM_WORLD);
  42.         }
  43.     }
  44.     else {
  45.         MPI_Recv(&n, 1, MPI_INT, 0, 98, MPI_COMM_WORLD, &status);
  46.     }
  47.  
  48.     vector<string> v(n);
  49.  
  50.     for (int i = 0; i < n; ++i) {
  51.         char tmp[10000];
  52.         if (worldRank == 0) {
  53.             file >> tmp;
  54.             for(int j = 1 ; j < worldSize ; ++j)
  55.                 MPI_Send(&tmp[0], 10000 , MPI_CHAR, j, 99, MPI_COMM_WORLD);
  56.         }
  57.         else {
  58.            
  59.             MPI_Recv(&tmp[0], 10000, MPI_CHAR, 0, 99, MPI_COMM_WORLD, &status);
  60.         }
  61.         v[i] = string(tmp);
  62.     }
  63.  
  64.     if (worldRank == 0)
  65.         file.close();
  66.  
  67.  
  68.     long long start = clock();
  69.  
  70.     int ans = 1e7;
  71.  
  72.     for (int i = worldRank ; i < n; i += worldSize) {
  73.         int cur = 0;
  74.         for (int j = 0; j < n; ++j) {
  75.             string tmp = v[j] + v[j];
  76.             size_t p = tmp.find(v[i]);
  77.             if (p == string::npos) {
  78.                 ans = -1;
  79.             }
  80.             else
  81.             {
  82.                 cur += p;
  83.             }
  84.         }
  85.         ans = min(ans, cur);
  86.     }
  87.  
  88.     if (worldRank == 0) {
  89.         int proc_ans;
  90.         for (int i = 1; i < worldSize; ++i) {
  91.             MPI_Recv(&proc_ans, 1, MPI_INT, i, 100, MPI_COMM_WORLD, &status);
  92.             ans = min(ans, proc_ans);
  93.         }
  94.     }
  95.     else {
  96.         MPI_Send(&ans, 1, MPI_INT, 0, 100, MPI_COMM_WORLD);
  97.     }
  98.  
  99.     if (worldRank == 0) {
  100.         cout << ans << endl;
  101.  
  102.         long long end = clock();
  103.  
  104.         cout << (double)(end - start) / CLOCKS_PER_SEC << " sec " << endl;
  105.  
  106.         system("pause");
  107.     }
  108.    
  109.     MPI_Finalize();
  110.     return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement