Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Par_prog_lab1.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- #include <cmath>
- #include <mpi.h>
- #include <ctime>
- #include <vector>
- #include <algorithm>
- #include <iostream>
- #include <string>
- #include <fstream>
- using namespace std;
- int main(int argc, char* argv[])
- {
- int n;
- int worldSize;
- int worldRank;
- MPI_Init(&argc, &argv);
- MPI_Comm_size(MPI_COMM_WORLD, &worldSize);
- MPI_Comm_rank(MPI_COMM_WORLD, &worldRank);
- MPI_Status status;
- fstream file;
- if (worldRank == 0) {
- // file.open("input_stupid.txt", fstream::in | fstream::out | fstream::app);
- // file.open("input_no_ans.txt" , fstream::in | fstream::out | fstream::app);
- file.open("input_big.txt", fstream::in | fstream::out | fstream::app);
- }
- if (worldRank == 0) {
- file >> n;
- for (int i = 1; i < worldSize; ++i) {
- MPI_Send(&n, 1, MPI_INT, i, 98, MPI_COMM_WORLD);
- }
- }
- else {
- MPI_Recv(&n, 1, MPI_INT, 0, 98, MPI_COMM_WORLD, &status);
- }
- vector<string> v(n);
- for (int i = 0; i < n; ++i) {
- char tmp[10000];
- if (worldRank == 0) {
- file >> tmp;
- for(int j = 1 ; j < worldSize ; ++j)
- MPI_Send(&tmp[0], 10000 , MPI_CHAR, j, 99, MPI_COMM_WORLD);
- }
- else {
- MPI_Recv(&tmp[0], 10000, MPI_CHAR, 0, 99, MPI_COMM_WORLD, &status);
- }
- v[i] = string(tmp);
- }
- if (worldRank == 0)
- file.close();
- long long start = clock();
- int ans = 1e7;
- for (int i = worldRank ; i < n; i += worldSize) {
- int cur = 0;
- for (int j = 0; j < n; ++j) {
- string tmp = v[j] + v[j];
- size_t p = tmp.find(v[i]);
- if (p == string::npos) {
- ans = -1;
- }
- else
- {
- cur += p;
- }
- }
- ans = min(ans, cur);
- }
- if (worldRank == 0) {
- int proc_ans;
- for (int i = 1; i < worldSize; ++i) {
- MPI_Recv(&proc_ans, 1, MPI_INT, i, 100, MPI_COMM_WORLD, &status);
- ans = min(ans, proc_ans);
- }
- }
- else {
- MPI_Send(&ans, 1, MPI_INT, 0, 100, MPI_COMM_WORLD);
- }
- if (worldRank == 0) {
- cout << ans << endl;
- long long end = clock();
- cout << (double)(end - start) / CLOCKS_PER_SEC << " sec " << endl;
- system("pause");
- }
- MPI_Finalize();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement