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=1;
double local_sum, total_sum;
int source;
int local_n;
MPI_Init(NULL,NULL);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
MPI_Comm_size(MPI_COMM_WORLD, &comm_sz);
local_n= n/comm_sz;
local_sum=sum(local_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;
}
}
if(my_rank==0){
printf("Sum is=%lf",total_sum);
printf("\n");
}
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;
}
无论进程数是多少,输出都应该相同。但是根据我的代码,当我运行 1 个进程的程序时,结果与进程数 8 不同。
例如,如果 n= 1,p= 1,总和为 2.667
Whereas, if n= 1, p= 8 the summation is 32.00
但根据我的理解,即使 p= 8求和结果应为 2.667
I need to write down a parallel code for this formula
I wrote a mpi program in c language for parallel programming
#include <stdio.h>
#include <math.h>
#include<mpi.h>
double sum(int n);
int main(void){
int my_rank,comm_sz, n=1;
double local_sum, total_sum;
int source;
int local_n;
MPI_Init(NULL,NULL);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
MPI_Comm_size(MPI_COMM_WORLD, &comm_sz);
local_n= n/comm_sz;
local_sum=sum(local_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;
}
}
if(my_rank==0){
printf("Sum is=%lf",total_sum);
printf("\n");
}
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;
}
The output should be same whatever the number of process is. But as per my code when I run the program for 1 process the result is different than the number of process 8.
For example, if n= 1, p= 1 the summation is 2.667
Whereas, if n= 1, p= 8 the summation is 32.00
But as per my understanding even if p= 8 the result of summation should be 2.667
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码中存在
逻辑错误
。场景一:
当comm_sz = 1时;
场景2:
当comm_sz = 2时;
在
my_rank = 0
中;在
my_rank = 1
中;因此,结果值为
comm_sz * 4 = 8
。基本上,同样的事情也会发生,即使您有 8 个进程,总和也将是 32。
如果
comm_sz > ,则代码的结果将始终是
.comm_sz*4
。 1.我希望这将帮助您理解代码中的问题。
您还应该考虑如何在不同的进程之间分配任务。
提示:假设您有
comm_size=4
和n=8
,则每个进程的local_sum
应计算 < code>n/comm_size 次迭代,但具有不同的开始和结束索引。即rank 0
应该计算i=0 到 1
的总和,rank 1
计算i=2 到 i=3
的总和> ...rank 4
应该计算i=6 到 i=8
。There is
logical error
in your code.Scenario 1:
When
comm_sz
= 1;Scenario 2:
When
comm_sz
= 2;In
my_rank = 0
;In
my_rank = 1
;Hence the resulting value is
comm_sz * 4 = 8
.Basically, the same thing happens, even if you have 8 processes, the sum will be 32.
The result from your code will always be
comm_sz*4
ifcomm_sz > 1
.I hope this will assist you to understand the issue in your code.
You should also consider how to distribute the tasks across different processes.
Hint: Say if you have
comm_size=4
andn=8
, thelocal_sum
for each process should calculaten/comm_size
iterations but with different starting and ending indexes. i.erank 0
should calculate sum fori=0 to 1
,rank 1
fori=2 to i=3
...rank 4
should calculatei=6 to i=8
.