首页 > 解决方案 > 从 MPI 中的文本文件中读取

问题描述

我正在尝试从 txt 文件中读取 int 和 double 。当我在控制台中运行该进程时,一切正常,但是当我使用单个进程通过 MPI 工作时,它给了我一个错误:

$ ./a.out
job aborted:
rank: node: exit code [: error message]
0: DESKTOP-QUJB667: -1073740791: process 0 exited without calling finalize

我只使用了两个命令从文件中读取:

FILE * file = fopen ("pixel.txt", "r");
fscanf (file, "% d", & numOfPixelsInFile);

从文件读取的问题只是我必须做的更广泛任务的初始阶段。

#include <mpi.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h> 

struct Pixel 
{
    int id;
    int x;
    int y;
    float rgb[3];
};

int main(int argc, char *argv[])
{
    int  namelen, numprocs, myid;
    char processor_name[MPI_MAX_PROCESSOR_NAME];

    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &myid);
    MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
    MPI_Get_processor_name(processor_name, &namelen);

    if (myid == 0) 
    {       
        FILE* file = fopen("pixel.txt", "r");

        //Pixel arguments
        int id, x, y;
        float rgb1;
        float rgb2;
        float rgb3;

        int numOfPixelsInFile = 0;
        fscanf(file, "%d", &numOfPixelsInFile);
        printf("Num of pixels: %d\n", numOfPixelsInFile);

        for (int i = 0; i < numOfPixelsInFile; i++)
        {
            fscanf(file, "%d %d %d %e %e %e", &id,
                &x, &y, &rgb1, &rgb2, &rgb3);
            printf("%d %d %d %e %e %e\n", id, x, y, rgb1, rgb2, rgb3);
        }

        fclose(file);       
    }
    else 
    {
    }

    MPI_Finalize();
    return 0;
}

标签: c

解决方案


推荐阅读