user2113035
user2113035

Reputation: 33

Segmentation fault in MPI_Bcast

I am new to Open MPI and I'm trying use it to run a brute force password cracker using a dictionary attack (I'm not actually cracking passwords, it's just an exercise). The dictionary I'm using is a vector<char> where the words are separated by null terminators.

Using MPI_Barriers, I've determined that the error occurs in the second MPI_Bcast (the MPI_Bcast that broadcasts dictionary). I've verified that the contents of buffer (the sizes of the vectors) are successfully broadcasted.

The error I get is a segmentation fault (11) with code "Address not mapped".

For some reason this always occurs in the rank 2 process when run with > 2 processes. If I run with only 2 processes, it occurs in the rank 1 process. I don't know if it's always caught in the rank 2 process due to chance, due to scheduling, or if it's actually only a problem in the rank 2 process (which I find least likely given that it occurs with 2 processes as well).

In other similar questions, the problem lies in how the buffer is passed. The normal problem is that the user was manually allocating memory in a c program, passing the address of the pointer to the array instead of the pointer itself. That's not the problem here (I even tried it out of desperation, but it gets caught as a compiler error).

I'm am now at a loss as to what the problem may be. It could be related to passing the buffer, or something else entirely. If more code is needed, let me know.

Relevant code:

void BroadCast_Receive(vector<char> &dictionary, vector<char> &passwords){

    int buffer[2];

    buffer[0] = dictionary.size(); 
    buffer[1] = passwords.size();

    MPI_Bcast(buffer,2,MPI_INT,0,MPI_COMM_WORLD);

    dictionary.resize(buffer[0]);
    passwords.resize(buffer[1]);    

    // seg faults here
    MPI_Bcast(&dictionary[0],buffer[0],MPI_INT,0,MPI_COMM_WORLD);

    MPI_Bcast(&passwords[0],buffer[1],MPI_INT,0,MPI_COMM_WORLD);
}

Upvotes: 3

Views: 861

Answers (1)

Patrick
Patrick

Reputation: 920

Your problem is that your MPI_Datatype is not matching. You are using MPI_INT, whereas your dictionary and passwords are std::vectors of type char. Use MPI_CHAR instead.

Moreover, as PaulMcKenzie pointed out, &dictionary[0] will still fail if the vector has size 0. For clean code, you should either check for this condition or assert it.

The following code should work for you:

void BroadCast_Receive(vector<char> &dictionary, vector<char> &passwords){

    int buffer[2];

    buffer[0] = dictionary.size(); 
    buffer[1] = passwords.size();

    MPI_Bcast(buffer,2,MPI_INT,0,MPI_COMM_WORLD);

    if (buffer[0] > 0) {
        dictionary.resize(buffer[0]);
        MPI_Bcast(&dictionary[0],buffer[0],MPI_CHAR,0,MPI_COMM_WORLD);
    }

    if (buffer[1] > 0) {
        passwords.resize(buffer[1]);    
        MPI_Bcast(&passwords[0],buffer[1],MPI_CHAR,0,MPI_COMM_WORLD);
    }
}

Upvotes: 1

Related Questions