C MPI阵列搜索(MPI_Scatter分段故障)

时间:2015-05-06 19:56:03

标签: c arrays search mpi scatter

我的MPI代码一直在为所有非主要任务遇到分段错误。

#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;
}

我收到的错误是:

  [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 ***

我总是收到

Thread 0's temp_array[0] is aaa

对于线程0来说是正确的。

我假设发生这种情况是因为只有主任务接收切片的一部分,但其他任务没有收到任何内容。

0 个答案:

没有答案