如何解决mpi C
我用 C 语言用 MPI 编写了这个程序。问题是在特定的点上程序崩溃了,我不明白为什么,因为我只增加了一个变量。
for(int tmp=0;tmp<sizeof(how_much_word)/sizeof(how_much_word[0])-1;tmp++){
cum_sum += how_much_word[tmp];
printf("Cumulative sum: %d \n",cum_sum);
printf("AFTER CUM SUM");
if(cum_sum > lowerbound && partition > 0 ){
strcpy(path,"/myfolder");
strcat(path,file_name_and_number_words[tmp]);
printf("name of file: %s \n",path);
file = fopen(path,"r");
printf("opening the file: %s \n",file_name_and_number_words[tmp]);
start = how_much_word[tmp] - (cum_sum-lowerbound);
if (start < 0){
start = 0;
}
printf("Start reading: %d \n",cum_sum);
while((ch = fgetc(file)) != EOF){
if(isalnum(ch)!=0 ){
in_word =1;
} else{
if ((ch == ' ' || ch == '\t' || ch== '\n') && (in_word==1)){
word_count++;
in_word=0;
if(word_count >= start){
partition--;
}
}/*end if tabulazione*/
} /*end else*/
} /*end while*/
printf("While is over with partiton: %d",partition);
} /*end if*/
fclose(file);
}/*end for*/
数组有 2 个元素,file_name_and_number_words 有 "mytxt.txt" 和 "mytxt2.txt",数组 how_much_word 有这些文件的字数,所以在这种情况下是 6 和 18。 程序在 printf“AFTER CUM SUM”之前崩溃,但我不明白出了什么问题。 崩溃的输出是:
Cumulative sum: 6
[979208abf4bc:26041] *** Process received signal ***
[979208abf4bc:26041] Signal: Segmentation fault (11)
[979208abf4bc:26041] Signal code: Address not mapped (1)
[979208abf4bc:26041] Failing at address: (nil)
[979208abf4bc:26041] [ 0] /lib/x86_64-linux-gnu/libc.so.6(+0x3ef20)[0x7f49630daf20]
[979208abf4bc:26041] [ 1] /lib/x86_64-linux-gnu/libc.so.6(fclose+0x4)[0x7f496311a254]
[979208abf4bc:26041] [ 2] Compiled/partitioningtest.out(+0x1950)[0x555784fc1950]
[979208abf4bc:26041] [ 3] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xe7)[0x7f49630bdb97]
[979208abf4bc:26041] [ 4] Compiled/partitioningtest.out(+0xcca)[0x555784fc0cca]
[979208abf4bc:26041] *** End of error message ***
文件路径有问题(我无法验证,因为在cumsum增量后我无法做任何事情)?变量路径在 main 中声明(在等级为 0 的进程之外)。声明是字符路径[2100]。我也在master内部使用了这个变量,但我认为这不是问题,因为每个进程应该有不同的内存区域。
已编辑:数组已由秩为 0 的进程发送并以这种方式接收 MPI_Recv(how_much_word,sizeof(how_much_word)/sizeof(how_much_word[0]),MPI_INT,1,MPI_COMM_WORLD,&status);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。