[英]The output of c language mpi program differ with number of processes
I need to write down a parallel code for this formula我需要为这个公式写下并行代码
I wrote a mpi program in c language for parallel programming我用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;
}
The output should be same whatever the number of process is.无论进程数是多少,output 都应该相同。 But as per my code when I run the program for 1 process the result is different than the number of process 8.
但是根据我的代码,当我为 1 个进程运行程序时,结果与进程 8 的数量不同。
For example, if n= 1, p= 1 the summation is 2.667例如,如果 n= 1,p= 1,则总和为 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但根据我的理解,即使 p= 8 求和的结果也应该是 2.667
There is logical error
in your code.您的代码中存在
logical error
。
Scenario 1: When comm_sz
= 1;场景一:当
comm_sz
= 1时;
For the iteration i = 0 : n=1, so loop condition 0<=1 is true
Value of cal_sum will be 4
For the iteration i = 0 : n=1, so loop condition 1<=1 is true
Value of cal_sum will be 4 + - (4/3) = 2.667
Scenario 2: When comm_sz
= 2;场景二:当
comm_sz
= 2时;
In my_rank = 0
;在
my_rank = 0
;
For the iteration i = 0 : n=0.5, so loop condition 0<=0 is true
So Value of cal_sum will be 4;
For the iteration i = 1 : n=0.5, so loop condition is false
exits the loop with cal_sum as 4;
In my_rank = 1
;在
my_rank = 1
;
For the iteration i = 0 : n=0.5, so loop condition 0<=0 is true
So Value of cal_sum will be 4;
For the iteration i = 1 : n=0.5, so loop condition is false
exits the loop with cal_sum as 4;
Hence the resulting value is comm_sz * 4 = 8
.因此,结果值为
comm_sz * 4 = 8
。
Basically, the same thing happens, even if you have 8 processes, the sum will be 32.基本上,同样的事情会发生,即使你有 8 个进程,总和也会是 32。
The result from your code will always be comm_sz*4
if comm_sz > 1
.如果
comm_sz > 1
,代码的结果将始终为comm_sz*4
。
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
and n=8
, the local_sum
for each process should calculate n/comm_size
iterations but with different starting and ending indexes .提示:假设您有
comm_size=4
和n=8
,则每个进程的local_sum
应该计算n/comm_size
迭代但具有不同的开始和结束索引。 ie rank 0
should calculate sum for i=0 to 1
, rank 1
for i=2 to i=3
... rank 4
should calculate i=6 to i=8
.即
rank 0
应该计算i=0 to 1
的总和, rank 1
为i=2 to i=3
... rank 4
应该计算i=6 to i=8
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.