Error while reading from a file in MPI C++ programming

五迷三道 提交于 2019-12-11 15:48:22

问题


I`m trying to write a code in C++ and using MPI.

In my code, I want to read from a file. I just want the master processor reads the data and later on, I will scatter it to the others. The goal is reading a graph from a file and then scatter its adjacency matrix row-wise.

The issue is when I want to open the file. If I generate a matrix it would scatter it nicely ( I used the code from here and changed it based on my need), so I don`t have any issues with scattering as I tested it several times. The issue is when I want to open and read from a file, and the weird thing is that even opening the file gives me errors, I am not very familiar with MPI and versions and its compatibility with C++. I assume it is something related to C++ or how I compile it.

Here is my code:

#include <iostream>
#include <climits>
#include <fstream>
#include <vector>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <list>
#include <queue>
#include <sstream>
#include <mpi.h>
#include <string>


#define BLOCK_LOW(id,p,n) ((id)*(n)/(p))
#define BLOCK_HIGH(id,p,n) ((id+1)*(n)/(p) - 1)
#define BLOCK_SIZE(id,p,n) ((id+1)*(n)/(p) - (id)*(n)/(p))
#define BLOCK_OWNER(index,p,n) (((p)*((index)+1)-1)/(n))

using namespace std;

void matrix_print(double **M, size_t m, size_t n,int my_rank);

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

    int comm_size, my_rank, root = 0, nVertex = 6, rows, i, j, *sendcounts, *displs,nEdges;
    double **adjArray, **adjPart, *stripdata;
    double *Adata;

    MPI_Status status;
    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD,&comm_size);
    MPI_Comm_rank(MPI_COMM_WORLD,&my_rank);


    if (my_rank == 0){

        ifstream myfile;
        myfile.open("path/intsance.gr");
        if(!myfile)
        {
            cout << "Error: file could not be opened"  << endl;
            exit (EXIT_FAILURE);
        }

      /*
        static const int max_line = 65536;
        string s;
        myfile.ignore(max_line, '\n');
        myfile>>s; // Nodes;
        myfile>>nVertex;
        myfile>>s; // Edges;
        myfile>>nEdges;
        cout<<"-----File is read..."<<endl;
        cout<<"------------------------------------------------------------------------------------------"<<endl;
        */
    }

   // MPI_Barrier(MPI_COMM_WORLD);


    nVertex=6;

    if (my_rank == 0){

        Adata = (double *)malloc(sizeof(double)*nVertex*nVertex);
        adjArray = (double **)malloc(sizeof(double*)*nVertex);
        for(int i = 0; i < nVertex; i++)
            adjArray[i] = &(Adata[i*nVertex]);


        int k=0;
        for (int i=0; i<nVertex; i++) {
            for (int j=0; j<nVertex; j++) {
                adjArray[i][j]=k;
                k++;
            }
        }

        cout<<"---Adjacancy Matrix:"<<endl;

        for (int i=0; i<nVertex; i++) {
            for (int j=0; j<nVertex; j++) {
                if(adjArray[i][j]==INT_MAX)
                {
                    cout<< " - ";
                }else
                {
                    cout<< adjArray[i][j]<<" ";
                }
            }
            cout<<endl;
        }
        cout<<"----------------------------------------------------"<<endl;

    }




    // Array containing the numbers of rows for each process
    sendcounts =  (int *)malloc(nVertex * sizeof(int));
    // Array containing the displacement for each data chunk
    displs =  (int *)malloc(nVertex * sizeof(int));
    // For each process ...
    for (j = 0; j < comm_size; j++) {
        // Sets each number of rows
        sendcounts[j] = BLOCK_SIZE(j, comm_size, nVertex)*nVertex;
        // Sets each displacement
        displs[j] = BLOCK_LOW(j, comm_size, nVertex)*nVertex;
    }
    // Each process gets the number of rows that he is going to get
    rows = sendcounts[my_rank]/nVertex;
    // Creates the empty matrixes for the parts of M
   // adjPart = (double**)matrix_create(rows, nVertex, sizeof(double));


    stripdata = (double *)malloc(sizeof(double)*rows*nVertex);
    adjPart = (double **)malloc(sizeof(double*)*rows);
    for(int i= 0; i< rows; i++) {
        adjPart[i] = &(stripdata[i*nVertex]);
    }


    // Scatters the matrix parts through all the processes
    MPI_Scatterv(&adjArray[0][0], sendcounts, displs, MPI_DOUBLE, &adjPart[0][0], sendcounts[my_rank], MPI_DOUBLE, root, MPI_COMM_WORLD);

    // This is where I get the Segmentation Fault
    matrix_print(adjPart, rows, nVertex,my_rank);


    MPI_Finalize();
    return 0;
}


void matrix_print(double **M, size_t m, size_t n,int my_rank) {
    size_t i,j;
    cout<<"Rank:  "<<my_rank<<endl;
    for(i=0; i<m; ++i) {
        printf("\n  ");
        for(j=0; j<n; ++j)
            printf("%f  ",M[i][j]);
    }
    cout<<endl;
}

Here is how I compile and run it:

mpic++ file.cpp -o outPut
mpirun -np 4 ./outPut

And I get the following result, which I don`t understand where is the issue:

---Adjacancy Matrix:
0 1 2 3 4 5 
6 7 8 9 10 11 
12 13 14 15 16 17 
18 19 20 21 22 23 
24 25 26 27 28 29 
30 31 32 33 34 35 
----------------------------------------------------
[S-MacBook-Pro:25734] *** Process received signal ***
[S-MacBook-Pro:25734] Signal: Segmentation fault: 11 (11)
[S-MacBook-Pro:25734] Signal code: Address not mapped (1)
[S-MacBook-Pro:25734] Failing at address: 0x0
[S-MacBook-Pro:25734] [ 0] 0   libsystem_platform.dylib            0x00007fff7221ff5a _sigtramp + 26
[S-MacBook-Pro:25734] [ 1] 0   ???                                 0x000000010a630b5a 0x0 + 4469230426
[S-MacBook-Pro:25734] [ 2] 0   libdyld.dylib                       0x00007fff71f9e115 start + 1
[S-MacBook-Pro:25734] *** End of error message ***
Rank:  0

  0.000000  1.000000  2.000000  3.000000  4.000000  5.000000  
[S-MacBook-Pro:25732] *** Process received signal ***
[S-MacBook-Pro:25732] Signal: Segmentation fault: 11 (11)
[S-MacBook-Pro:25732] Signal code: Address not mapped (1)
[S-MacBook-Pro:25732] Failing at address: 0x0
[S-MacBook-Pro:25732] [ 0] 0   libsystem_platform.dylib            0x00007fff7221ff5a _sigtramp + 26
[S-MacBook-Pro:25732] [ 1] 0   ???                                 0x000000010e9cfb5a 0x0 + 4540136282
[S-MacBook-Pro:25732] [ 2] 0   libdyld.dylib                       0x00007fff71f9e115 start + 1
[S-MacBook-Pro:25732] *** End of error message ***
[S-MacBook-Pro:25733] *** Process received signal ***
[S-MacBook-Pro:25733] Signal: Segmentation fault: 11 (11)
[S-MacBook-Pro:25733] Signal code: Address not mapped (1)
[S-MacBook-Pro:25733] Failing at address: 0x0
[S-MacBook-Pro:25733] [ 0] 0   libsystem_platform.dylib            0x00007fff7221ff5a _sigtramp + 26
[S-MacBook-Pro:25733] [ 1] 0   ???                                 0x00000001100acb5a 0x0 + 4564110170
[S-MacBook-Pro:25733] [ 2] 0   libdyld.dylib                       0x00007fff71f9e115 start + 1
[S-MacBook-Pro:25733] *** End of error message ***
--------------------------------------------------------------------------
mpirun noticed that process rank 3 with PID 25734 on node S-MacBook-Pro exited on signal 11 (Segmentation fault: 11).

Note that since I didn`t know where the issue comes from, I kept the matrix as I generate it in my code, for now I just want to know why it gives me errors by opening the file. Any help would be appreciated!

来源:https://stackoverflow.com/questions/49579791/error-while-reading-from-a-file-in-mpi-c-programming

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