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?