2

I'm new to using MPI, and I'm having an issue understanding why my code isn't working correctly.

#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;
    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);
        }
        int index = list_size/slice; // 
        MPI_Bcast(&str_to_search, 10, MPI_CHAR, 0, MPI_COMM_WORLD);
    }

    MPI_Bcast(&str_to_search, 10, MPI_CHAR, 0, MPI_COMM_WORLD);

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

    // Search for string occurs here

    MPI_Finalize();

    return 0;
}

However, I'm finding that when I try to search, only the master task receives some of the slice, the rest is null. All other tasks don't receive anything. I've tried placing MPI_Scatter outside of the if(master task) statement, but I have no luck with this. Also, when the list_size increases, I find the program basically gets stuck at the MPI_Scatter line. What am I doing wrong, and what can I do to correct this?

1 Answers1

2

You should go look up some tutorials on MPI collectives. They require all processes to participate collectively. So if any process calls MPI_Scatter, then all processes must call MPI_Scatter. I'd recommend looking at some sample code and playing with it until you understand what's going on. Then try coming back to your own code and seeing if you can figure out what's going on.

My favorite reference for anything pre-MPI-3 is DeinoMPI. I've never used the implementation, but the documentation is great since it has a complete example for each function in the MPI-2 Spec.

Wesley Bland
  • 8,816
  • 3
  • 44
  • 59
  • Thank you for your help, I understand now why each process calls MPI_Scatter now. However, I find that running the process with ID 0 runs fine, but when I get to other processes, I get a big segmentation fault. All I am trying to do for now is print the first element of the temp_array. In doing MPI_Scatter, the temp_array from each process should have elements from the_array, but they do not and lead to a segmentation fault. Why is this? – casualresearcher May 06 '15 at 19:40
  • I'd have to see the code to help with that. You should ask a new question. – Wesley Bland May 06 '15 at 19:41
  • Here is my new question: http://stackoverflow.com/questions/30086336/c-mpi-array-search-mpi-scatter-segmentation-fault – casualresearcher May 06 '15 at 19:57