我的MPI代码一直在运行所有非主任务的分段错误。
#include "mpi.h" #include #include 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 %sn", 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是正确的。
我假设发生这种情况是因为只有主任务接收切片的一部分,但其他任务没有收到任何内容。
以上就是c/c++开发分享C MPIarrays搜索(MPI_Scatter分段故障)相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/522908.html