【发布时间】:2025-12-10 16:10:01
【问题描述】:
我尝试编写代码“查找素数”。但是 MPI_Gather 函数无法获取 'c'(素数)的值。即使我声明了其他数组,它也不起作用。我的代码源有什么问题?当我删除 MPI_Gather 函数的一部分时效果很好。我需要使用其他类型的函数来获取值吗?
这是我的代码:
#include<stdio.h>
#include<mpi.h>
#include<stdlib.h>
#define MAXSIZE 1000
int main(int argc, char *argv[]){
int my_rank,p,low,high;
int x,data[MAXSIZE],a=0;
int num,t,result,i=0;
int w=0,b=0,c=0,d=0;
int * results, *resultss;
MPI_Request req;
MPI_Status status;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD, &p);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
if( my_rank == 0){
printf("Input(50~1000) : ");
fflush(stdout);
scanf("%d",&w);
for(i=0; i<w; i++) data[i] = i+1;
}
MPI_Bcast(&w, 1, MPI_INT,0,MPI_COMM_WORLD);
MPI_Bcast(data, w, MPI_INT,0,MPI_COMM_WORLD);
if( my_rank != 0){
x = w/(p-1);
low = (my_rank-1)*x;
high = low+x-1;
for(num = data[low]; num <= data[high];num++){
result = 0;
t=1;
while(num>=t){
if(num%t==0){
result = result +1;
}
t += 1;
}
if(result==2){
if(i%5){
c=num;
// printf("%d\t",c);
}
else{
c=num;
// printf("%d\n",c);
}
}
else {
results = (int *)malloc(p * sizeof(int));
}
}
}
MPI_Gather(&c, 1, MPI_INT, results, 1, MPI_INT, 0, MPI_COMM_WORLD);
int j=0;
for(j=1; j< p; j++){
if(my_rank == 0){
printf("%d\n",results[j]);
}
}
MPI_Finalize();
}
这是错误信息:
* 处理接收到的信号 *
Signal: Segmentation fault: 11 (11)
Signal code: Address not mapped (1)
Failing at address: 0x0
[ 0] 0 libsystem_platform.dylib 0x00007fff8af90b3a _sigtramp + 26
[ 1] 0 ??? 0x000000010cc560cb 0x0 + 4509229259
[ 2] 0 libopen-pal.40.dylib 0x00000001052771a5 non_overlap_copy_content_same_ddt + 885
[ 3] 0 libmpi.40.dylib 0x00000001050cc97c ompi_datatype_sndrcv + 444
[ 4] 0 libmpi.40.dylib 0x00000001051401ff ompi_coll_base_gather_intra_basic_linear + 159
[ 5] 0 libmpi.40.dylib 0x00000001050d9ac4 MPI_Gather + 564
[ 6] 0 prog3 0x0000000105095dfb main + 923
[ 7] 0 libdyld.dylib 0x00007fff8ad81235 start + 1
[ 8] 0 ??? 0x0000000000000001 0x0 + 1
* 错误信息结束 *
主要作业正常终止,但 1 个进程返回非零退出代码。根据用户方向,作业已中止。
mpirun 注意到节点 ****-ui-MacBook Pro 上 PID 为 0 的进程在信号 11 上退出(分段错误:11)。
【问题讨论】:
标签: c parallel-processing mpi