Why don't files open when I add MPI?

大城市里の小女人 提交于 2020-05-17 07:46:26

问题


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);
    ...
}

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.


回答1:


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

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

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



来源:https://stackoverflow.com/questions/61752531/why-dont-files-open-when-i-add-mpi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!