0
다음 코드를 작성했습니다. 그것은 콘솔에서 번호를 읽고 (data
변수로) 다른 모든 프로세스로 보냅니다. 하지만 cin >> data
은 무시되었습니다. 이미 cin.clear()
및 cin.ignore()
시도했습니다MPI가 cin을 무시합니다.
From which process do you want to transfer data?
2
Process #2 waiting data to send. Please enter.
Process 1 received data (322) from process #2
Process 3 received data (322) from process #2
:
#include <mpi.h>
#include <iostream>
#include <stdio.h>
using namespace std;
int main(int argc, char* argv[]) {
int rank, n;
int i;
MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &n);
int data = 322; // magic number 322 just for initialisation
if (rank == 0)
{
cout << "From which process do you want to transfer data?" << endl;
cin >> i;
MPI_Send(&i, 1, MPI_INT, rank+1, 0, MPI_COMM_WORLD);
}
else
{
MPI_Recv(&i, 1, MPI_INT, rank-1, 0, MPI_COMM_WORLD, &status);
if (rank < n - 1)
MPI_Send(&i, 1, MPI_INT, rank+1, 0, MPI_COMM_WORLD);
if(rank == i) {
cout << "Process #"<< rank <<" waiting data to send. Please enter." << endl;
cin >> data; //doesn't work
for(int j = 0; j < n; j++)
if(j != i)
MPI_Send(&data, 1, MPI_INT, j, 7, MPI_COMM_WORLD);
}
else {
int pata;
MPI_Recv(&pata, 1, MPI_INT, i, 7, MPI_COMM_WORLD, &status);
cout << "Process "<< rank <<" received data (" << pata << ") from process #" << i << endl;
}
}
MPI_Finalize();
}
는 콘솔과 같이 보입니다.
저는 openMPI를 사용하고 있습니다. 당신의 대답 덕분에 나는 문서에서 이것을 발견했다 : '-stdin, --stdin stdin을받는 프로세스의 MPI_COMM_WORLD 순위. 기본값은 stdin을 MPI_COMM_WORLD 순위 0으로 전달하지만이 옵션은 stdin을 모든 프로세스로 전달하는 데 사용할 수 있습니다. none을 지정해도 프로세스가 stdin을받을 수 없음을 나타낼 수 있습니다. ' 불행히도 모든 순위로 리디렉션 할 수있는 옵션이 없습니다. 나는 이것이 내가 원하는 것을 할 것이라고 생각한다. 어쨌든, 지금 무슨 일이 일어 났는지 압니다. 고맙습니다. –