I am coding a program in C++ MPI but when passing a large file as stdin
I am facing the problem that the threads are not seeing the same stdin
information.
More elaborated, I am passing as standard input a list of input files, which is then stored in a vector<string>
:
MPI_Init(NULL,NULL);
int CORES, thread;
MPI_Comm_size(MPI_COMM_WORLD,&CORES);
MPI_Comm_rank(MPI_COMM_WORLD,&thread);
stringstream tline;
int count = 0;
for (std::string line; std::getline(std::cin, line);){
tline << line << " ";
count++;
}
vector<string> args(count,"");
for(int i = 0; i < count; i++)
tline >> args[i];
cout << thread << " " << count << endl; //each thread outputs the number of input files it received
My problem is that this gives different numbers for different threads. For instance, after passing a file of 10 000 lines, I get:
5 9464
6 9464
3 9464
4 9464
1 9554
2 9554
0 10000
7 9464
Is it because of some overheading? How can I avoid that?
答案 0 :(得分:1)
好的,所以基本上你的问题是你所有的线程都在消耗来自 cin 的线并且他们竞争。尽管 cin 通常会为线程安全性提供一些保证,但您并不总是确定自己会得到什么。检查此主题:How do scanf(), std::cin behave on multithreaded environment?
解决方案:不要使用CIN?使用文件并让每个线程使用文件句柄自己打开文件。如果你真的想使用 cin ,那么让一个来自MPI的线程读取 CIN 并将其广播到其他线程,然后他们可以随心所欲地使用它。