2
votes

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

1 Answers

2
votes

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.