#include "stdafx.h"
#include <mpi.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <cstdlib>
#include <ctime>
#include <cctype>
#include <cstdlib>
#include <ctime>
using namespace std;
int randomize() {
srand(time(0));
int i = 1 + rand() % 3;
return i;
}
int main(int argc, char** argv) {
MPI_Init(&argc, &argv);
int rank, size, tag = 1;
ofstream outfile;
ifstream infile;
bool read = true;
const int max = 10;
char message[max]; char msgS[max]; char msgR[max];
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
outfile.open("in.txt");
cout << "Write messages: " << rank << endl;
do {
cin.getline(message, max);
if (strcmp(message, "stop") == 0)
break;
outfile << message << endl;
} while (strcmp(message, "stop") != 0);
cout << "Messages written to file [in.txt]!" << endl;
outfile.close();
infile.open("in.txt");
outfile.open("out.txt");
if (rank == 0 && read) {
int i = randomize();
if (!infile.eof()) {
infile >> msgS;
MPI_Send(msgS, strlen(msgS), MPI_CHAR, (rank + i), tag, MPI_COMM_WORLD);
read = false;
}
else {
cout << "Messages written to file [out.txt]!" << endl;
infile.close();
outfile.close();
exit(0);
}
}
else if (rank == 1 && !read) {
MPI_Recv(msgR, strlen(msgS), MPI_CHAR, 0, tag, MPI_COMM_WORLD, MPI_STATUSES_IGNORE);
outfile << msgR << endl;
tag++;
read = true;
}
else if (rank == 2 && !read) {
MPI_Recv(msgR, strlen(msgS), MPI_CHAR, 0, tag, MPI_COMM_WORLD, MPI_STATUSES_IGNORE);
outfile << msgR << endl;
tag++;
read = true;
}
else if (rank == 3 && !read) {
MPI_Recv(msgR, strlen(msgS), MPI_CHAR, 0, tag, MPI_COMM_WORLD, MPI_STATUSES_IGNORE);
outfile << msgR << endl;
tag++;
read = true;
}
else {
cout << "Error!!!";
exit(0);
}
MPI_Finalize();
system("PAUSE");
return 0;
}
何か問題があります。コードはMPI_Sendルーチンで停止し、私はその理由を知らない!ファイルから任意のプロセスに読み込み行を送り、受け取ったデータを別のファイルに格納したいだけです。動作していないMPIをデバッグする方法は?
'randomize'が返されます3より大きい数字で、SendとRecvのブロッキングバージョンを使用しているため、 'Recv'と一致しない' Send'になります。 – niceman
各ランクのif文が正当なものかどうかを確認してください。 –
申し訳ありませんが間違っていた、それについて考えてみましょう – niceman