尝试将C程序转换为MPI程序,但出现错误 [英] Try to convert C program to mpi program but error occur

查看:42
本文介绍了尝试将C程序转换为MPI程序,但出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为这个方程式写了一个c程序,

我正在尝试为上面的公式写一个MPI编程。

#include <stdio.h>
#include <math.h>
#include<mpi.h>
double sum(int n);

int main(void){
int my_rank,comm_sz, n=4;
double local_sum, total_sum;
int source;
MPI_Init(NULL,NULL);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
MPI_Comm_size(MPI_COMM_WORLD, &comm_sz);

local_sum=sum(n);

if ( my_rank != 0) {
 MPI_Send (&local_sum , 1, MPI_DOUBLE , 0, 0, MPI_COMM_WORLD ) ;
}

else{
 total_sum = local_sum;
 for(source=1;source<comm_sz;source++){
 MPI_Recv (&local_sum , 1, MPI_DOUBLE , source , 0, MPI_COMM_WORLD , MPI_STATUS_IGNORE ) ;
 total_sum+=local_sum;
}
}
MPI_Finalize();
return 0;

}

double sum(int n){
int i;
double cal_sum=0;
for (i =0;i <= n;i++) {
  cal_sum = cal_sum + 4*(pow (-1, i))/((2*i)+1);
}
return cal_sum;
}

运行时错误:

[myid@node070 foldername]$ mpirun -c 4 ./mpi_sum
[node070:100103] *** An error occurred in MPI_Recv
[node070:100103] *** reported by process [617349121,0]
[node070:100103] *** on communicator MPI_COMM_WORLD
[node070:100103] *** MPI_ERR_RANK: invalid rank
[node070:100103] *** MPI_ERRORS_ARE_FATAL (processes in this 
communicator will now abort,
 [node070:100103] ***    and potentially your MPI job)

‘ 如有任何帮助,我们将不胜感激

推荐答案

我认为您可以通过阅读错误消息并检查代码中的相应部分来推断错误。

错误消息:

[node070:100103] *** An error occurred in MPI_Recv
[node070:100103] *** MPI_ERR_RANK: invalid rank
查看代码中的MPI_Recv,您可以看到您正在从rank n接收。没有秩数n。当大小nn-1时,您可以获得的最大秩数为n-1

MPI_Recv (&sum , 1, MPI_DOUBLE , n , 0, MPI_COMM_WORLD , MPI_STATUS_IGNORE ) ;

您应该将MPI_Recv中的n替换为i

这篇关于尝试将C程序转换为MPI程序,但出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆