使用MPI IO并行输出到单个文件

使用MPI IO并行输出到单个文件,io,parallel-processing,mpi,output,Io,Parallel Processing,Mpi,Output,我有一个非常简单的任务要做,但不知怎么的,我还是被卡住了 我有一个大数据文件(“file_initial.dat”),集群上的所有节点都应该读取它(使用MPI),每个节点将对这个大文件的一部分执行一些操作(file_size/number_of_nodes),最后每个节点将其结果写入一个共享的大文件(“file_final.dat”)。文件的元素数保持不变 通过谷歌搜索,我明白了,最好将数据文件写成二进制文件(我在这个文件中只有十进制数),而不是*.txt”文件。因为没有人会读这个文件,只有计算

我有一个非常简单的任务要做,但不知怎么的,我还是被卡住了

我有一个大数据文件(“file_initial.dat”),集群上的所有节点都应该读取它(使用MPI),每个节点将对这个大文件的一部分执行一些操作(file_size/number_of_nodes),最后每个节点将其结果写入一个共享的大文件(“file_final.dat”)。文件的元素数保持不变

  • 通过谷歌搜索,我明白了,最好将数据文件写成二进制文件(我在这个文件中只有十进制数),而不是*.txt”文件。因为没有人会读这个文件,只有计算机会读

  • 我尝试自己实现(但使用格式化的in/output而不是二进制文件),但我得到了错误的行为

  • 到目前为止,我的代码如下:

    #include <fstream>
    #define NNN 30
    
    int main(int argc, char **argv)
    {   
        ifstream fin;
    
        // setting MPI environment
    
        int rank, nprocs;
        MPI_File file;
        MPI_Init(&argc, &argv);
        MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
        MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    
        // reading the initial file
    
        fin.open("initial.txt");
        for (int i=0;i<NNN;i++)
        {  
            fin  >> res[i];
            cout << res[i] << endl; // to see, what I have in the file
        }  
        fin.close();
    
        // starting position in the "res" array as a function of "rank" of process
        int Pstart = (NNN / nprocs) * rank ;
        // specifying Offset for writing to file
        MPI_Offset offset = sizeof(double)*rank;
        MPI_File file;
        MPI_Status status;
    
        // opening one shared file
        MPI_File_open(MPI_COMM_WORLD, "final.txt", MPI_MODE_CREATE|MPI_MODE_WRONLY,
                              MPI_INFO_NULL, &file);
    
        // setting local for each node array
    
        double * localArray;
        localArray = new double [NNN/nprocs];
    
        // Performing some basic manipulation (squaring each element of array)
        for (int i=0;i<(NNN / nprocs);i++)
        {
            localArray[i] = res[Pstart+i]*res[Pstart+i];
        }
    
        // Writing the result of each local array to the shared final file:
    
        MPI_File_seek(file, offset, MPI_SEEK_SET);
        MPI_File_write(file, localArray, sizeof(double), MPI_DOUBLE, &status);
        MPI_File_close(&file);
    
        MPI_Finalize();
    
        return 0;
    }
    
    #包括
    #定义NNN 30
    int main(int argc,字符**argv)
    {   
    流鳍;
    //设置MPI环境
    国际排名,NPROC;
    MPI_文件;
    MPI_Init(&argc,&argv);
    MPI通信大小(MPI通信世界和NPROC);
    MPI通信等级(MPI通信世界和等级);
    //读取初始文件
    fin.open(“initial.txt”);
    对于(int i=0;i>res[i];
    
    cout您的二进制文件输出几乎正确;但是您对文件中的偏移量和要写入的数据量的计算不正确。您希望偏移量为

    MPI_Offset offset = sizeof(double)*Pstart;
    
    不是

    否则,您将使每个列组覆盖其他列组的数据,因为(
    nprocs=5
    中的第3列开始写入文件中的双倍数字3,而不是(30/5)*3=18)

    另外,您希望每个列组写入
    NNN/nprocs
    double,而不是
    sizeof(double)
    double,这意味着您需要

    MPI_File_write(file, localArray, NNN/nprocs, MPI_DOUBLE, &status);
    
    如何以文本文件的形式编写是一个更大的问题;您必须在内部将数据转换为字符串,然后输出这些字符串,通过仔细设置格式确保您知道每行需要多少字符。本网站中介绍了这一点

    MPI_File_write(file, localArray, NNN/nprocs, MPI_DOUBLE, &status);