为什么在添加MPI时文件没有打开? [英] Why don't files open when I add MPI?

查看:57
本文介绍了为什么在添加MPI时文件没有打开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的程序没有MPI时,打开文件就可以了,但是当我添加MPI时,文件不会打开.为什么会这样?
我的代码:

When my program works without MPI, then everything is fine with opening files, but when I add MPI, the files do not open. Why is that?
My code:

void fileEntry(string path, int n) {

    ofstream fout;

    fout.open(path);
    if (!fout.is_open()) {
        cout << "File open error";
    }
    else {
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < n; j++) {
                fout << rand() % 100 << " ";
            }
            fout << "\n";
        }
    }

    fout.close();
}

int main(int argc, char** argv) {

    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &WORLD_RANK);
    MPI_Comm_size(MPI_COMM_WORLD, &WORLD_SIZE);

    if (WORLD_RANK == 0) {
        // размерность
        int dimension = 0;
        cout << "Введите размерность матрицы:\n";
        cin >> dimension;

        // записываем данные в файлы
        fileEntry("MatrixA.txt", dimension);
        fileEntry("MatrixB.txt", dimension);
    ...
}

此代码给出两次:文件打开错误.
我以7个进程启动该项目,但这没关系,因为我只在一个0进程中专门打开文件.我不需要打开文件7次,我需要打开1次.
因此,不仅是开头,而且通常是处理文件.

This code gives twice: File open error.
I start the project in 7 processes, but it doesn't matter, because I do opening files specifically in only one 0 process. I don’t need the file to open 7 times, I need 1 time.
And so not only with the opening, but in general with any work with files.

推荐答案

我没有足够的声誉来发表评论,因此请在此处发布.

I didn't have enough reputation to comment, so posting here.

根据您的评论, errno 13 代表权限被拒绝",而 errno 2 -没有这样的文件或目录"

According to you comments, errno 13 stands for 'permission denied' and errno 2 - 'No such file or directory'

首先,我建议尝试使用完整的 filepath ,然后尝试为文件或目录提供正确的权限.

First i would suggest to try use full filepath, and then try to provide right permission to the file or directory.

这篇关于为什么在添加MPI时文件没有打开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆