Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Parallel processing 如何通过单个进程读取MPI中的txt文件?为什么我的方法不起作用?_Parallel Processing_Mpi - Fatal编程技术网

Parallel processing 如何通过单个进程读取MPI中的txt文件?为什么我的方法不起作用?

Parallel processing 如何通过单个进程读取MPI中的txt文件?为什么我的方法不起作用?,parallel-processing,mpi,Parallel Processing,Mpi,我是MPI新手。 我试图通过使用标准C++代码读取文本文件如下: int main(int argc, char* argv[] ){ int np, pid, ierr; ierr = MPI_Init(&argc, &argv); ierr = MPI_Comm_size(MPI_COMM_WORLD, &np); ierr = MPI_Comm_rank(MPI_COMM_WORLD, &pid); const int imgWidth = 1000;

我是MPI新手。 我试图通过使用标准C++代码读取文本文件如下:
int main(int argc, char* argv[] ){

int np, pid, ierr;
ierr = MPI_Init(&argc, &argv);
ierr = MPI_Comm_size(MPI_COMM_WORLD, &np);
ierr = MPI_Comm_rank(MPI_COMM_WORLD, &pid);


const int imgWidth = 1000; // the width of the image (count in pixel)
const int imgHeight = 1000; // the height of the image

double* Y;
Y = (double *)malloc(imgHeight*imgWidth*sizeof(double));

if(pid == 0)
{
    string input = "Im.txt";
    readData(input.c_str(), Y);
}
MPI_Bcast(Y, imgHeight*imgWidth, MPI_DOUBLE, 0, MPI_COMM_WORLD);


free(Y);

MPI_Finalize();

return 1;
}

readData函数定义为:

bool readData(const char *fileName, double* Y){

printf("Reading the data file!\n");

ifstream fin(fileName);

int i = 0;
while(fin>>Y[i])
{   
    i++;
};
cout<<"In total, "<<i<<" data are imported."<<endl;
//close the file
fin.close();

return 1;
bool readData(常量字符*文件名,双*Y){
printf(“读取数据文件!\n”);
ifstream-fin(文件名);
int i=0;
而(fin>>Y[i])
{   
i++;
};

cout最后,我发现了问题。我正在使用visual studio在win7下工作。似乎我必须明确指出我的文件的路径。甚至我放了“Im.txt”与源代码文件放在同一个文件夹中,它不起作用。

您是否尝试过在光标读取数据时打印数据?这可以帮助您隔离错误的来源。readData是否返回任何内容?是否可以打开文件开始?是否引发任何异常?我在“if(pid==0)”行中添加了一个断点然后一步一步地执行程序。当程序转到“readData(input.c_str());”行时,程序将打开一个名为“xstring”的文件自动且不会实际转到readData函数。我只是不明白为什么。fin似乎无法打开该文件。我尝试使用C风格的函数fopen和fscanf,但它们仍然无法打开该文件。有人能帮我吗?最后,我发现了问题。我正在使用visual studio在win7下工作。似乎我必须明确指出这是我文件的路径。即使我将“Im.txt”与源代码文件放在同一个文件夹中,它也不起作用。@挖掘,如果您已经回答了自己的问题,请继续,将其作为答案发布,并将其标记为正确,以便关闭此问题。