1

My MPI code keeps running into a segmentation fault for all non-master tasks.

#include "mpi.h"
#include <stdio.h>
#include <string.h>
int main(int argc, char* argv[]) {
    int list_size = 1000
    int threads;
    int th_nums;
    int slice;
    int index;
    char* the_array[list_size];
    char* temp_array[list_size];
    char str_to_search[10];

    FILE *in = fopen("inputfile", "r");

    char parse[10];

    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &threads);
    MPI_Comm_size(MPI_COMM_WORLD, &th_nums);

    if (threads == 0) { // if master task
        fgets(parse, 10, in);
        slice = atoi(parse); // How many slices to cut the array into

        fgets(parse, 10, in);
        sscanf(parse, "%s", search); // gives us the string we want to search

        int i;
        for (i = 0; i < list_size; i++) {
            char temp[10];
            fgets(parse, 10, in);
            sscanf(parse, "%s", temp);
            the_array[i] = strdup(temp);
        }
        index = list_size/slice; // 
        MPI_Bcast(&str_to_search, 10, MPI_CHAR, 0, MPI_COMM_WORLD);
        MPI_Bcast(&index, 15, MPI_INT, 0, MPI_COMM_WORLD);
    }

    MPI_Bcast(&str_to_search, 10, MPI_CHAR, 0, MPI_COMM_WORLD);
    MPI_Bcast(&index, 15, MPI_INT, 0, MPI_COMM_WORLD);

    MPI_Scatter(the_array, index, MPI_CHAR, temp_array, index, 0, MPI_COMM_WORLD);

    // TESTING IF THE TEMP_ARRAY RECEIVED DATA
    printf("Thread %i's temp_array[0] is %s\n", threads, temp_array[0]);

    // Search for string occurs here

    MPI_Finalize();

    return 0;
}

The error I'm receiving is this:

  [katya-kazanova:03096] *** Process received signal ***
  [katya-kazanova:03096] Signal: Segmentation fault (11)
  [katya-kazanova:03096] Signal code: Address not mapped (1)
  [katya-kazanova:03096] Failing at address: 0x220e6d0
  [katya-kazanova:03096] [ 0] /lib64/libpthread.so.0() [0x359fc0f710]
  [katya-kazanova:03096] [ 1] /lib64/libc.so.6(_IO_vfprintf+0x3e5c) [0x359f047e2c]
  [katya-kazanova:03096] [ 2] /lib64/libc.so.6(_IO_printf+0x9a) [0x359f04f18a]
  [katya-kazanova:03096] [ 3] ./test(main+0x31b) [0x400eaf]
  [katya-kazanova:03096] [ 4] /lib64/libc.so.6(__libc_start_main+0xfd) [0x359f01ed5d]
  [katya-kazanova:03096] [ 5] ./test() [0x400ad9]
  [katya-kazanova:03096] *** End of error message ***
  [katya-kazanova:03097] *** Process received signal ***
  [katya-kazanova:03098] *** Process received signal ***
  [katya-kazanova:03098] Signal: Segmentation fault (11)
  [katya-kazanova:03098] Signal code: Address not mapped (1)
  [katya-kazanova:03098] Failing at address: 0x223f410
  [katya-kazanova:03098] [ 0] /lib64/libpthread.so.0() [0x359fc0f710]
  [katya-kazanova:03097] Signal: Segmentation fault (11)
  [katya-kazanova:03097] Signal code: Address not mapped (1)
  [katya-kazanova:03097] Failing at address: 0x2226d70
  [katya-kazanova:03098] [ 1] /lib64/libc.so.6(_IO_vfprintf+0x3e5c) [0x359f047e2c]
  [katya-kazanova:03098] [ 2] /lib64/libc.so.6(_IO_printf+0x9a) [0x359f04f18a]
  [katya-kazanova:03098] [ 3] ./test(main+0x31b) [0x400eaf]
  [katya-kazanova:03098] [ 4] /lib64/libc.so.6(__libc_start_main+0xfd) [0x359f01ed5d]
  [katya-kazanova:03098] [ 5] ./test() [0x400ad9]
  [katya-kazanova:03098] *** End of error message ***
  [katya-kazanova:03097] [ 0] /lib64/libpthread.so.0() [0x359fc0f710]
  [katya-kazanova:03097] [ 1] /lib64/libc.so.6(_IO_vfprintf+0x3e5c) [0x359f047e2c]
  [katya-kazanova:03097] [ 2] /lib64/libc.so.6(_IO_printf+0x9a) [0x359f04f18a]
  [katya-kazanova:03097] [ 3] ./test(main+0x31b) [0x400eaf]
  [katya-kazanova:03097] [ 4] /lib64/libc.so.6(__libc_start_main+0xfd) [0x359f01ed5d]
  [katya-kazanova:03097] [ 5] ./test() [0x400ad9]
  [katya-kazanova:03097] *** End of error message ***

And I always receive

Thread 0's temp_array[0] is aaa

Which is correct for Thread 0.

I'm assuming this occurs because only the master task receives part of the slice, but other tasks don't receive anything.

  • We'd need to get some sample input to be able to tell you what specifically is wrong, but at first glance, rank 0 will do two more `MPI_Bcast` calls than everyone else (the two in its specific block plus the two below it). – Wesley Bland May 06 '15 at 20:03
  • This has been discussed here countless times: MPI does not support array of pointers. You have to "flatten" the memory into a single contiguous block. – Hristo Iliev May 06 '15 at 21:21
  • 1
    possible duplicate of [MPI\_Scatter - not working as expected](http://stackoverflow.com/questions/20891830/mpi-scatter-not-working-as-expected) – Hristo Iliev May 06 '15 at 21:29
  • Interesting, thank you, now I see that the issue most likely comes from the 2-D array issue. But how would I flatten out this array? I see in the link you posted how they did it with integers, but because I'm working with char*, wouldn't flattening a 2-D char array that contains array[0] = 'hi', array[1] = 'hello', array[2] = 'goodbye' into 'hihellogoodbye'? – casualresearcher May 06 '15 at 21:52
  • 1
    Strings can be easily concatenated by separating them with NUL (`\0`) characters. One should also use `MPI_Scatterv` because the chunks will most likely be of different size. Another option would be to have each string occupy the same amount of space, e.g. 64 bytes, by padding with NULs. – Hristo Iliev May 07 '15 at 12:50
  • `MPI_Bcast()` and most MPI functions needs pointer to the data to be send. By writing `MPI_Bcast(&str_to_search, 10, MPI_CHAR, 0, MPI_COMM_WORLD);`, a pointer to an array of char is given to `MPI_Bcast()`. But it should be a pointer to the first item of the array. So `MPI_Bcast(str_to_search, 10, MPI_CHAR, 0, MPI_COMM_WORLD);` – francis May 07 '15 at 19:54

0 Answers0