问题
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.
来源:https://stackoverflow.com/questions/30086336/c-mpi-array-search-mpi-scatter-segmentation-fault