C语言mpi程序的输出随进程数的不同而不同

发布于 2025-01-12 14:28:06 字数 1350 浏览 2 评论 0原文

我需要写下这个公式的并行代码

在此处输入图像描述

我用 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

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

春风十里 2025-01-19 14:28:06

您的代码中存在逻辑错误

场景一:
当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

场景2:
当comm_sz = 2时;

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;

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;

因此,结果值为 comm_sz * 4 = 8

基本上,同样的事情也会发生,即使您有 8 个进程,总和也将是 32。

如果 comm_sz > ,则代码的结果将始终是 comm_sz*4。 1..

我希望这将帮助您理解代码中的问题。

您还应该考虑如何在不同的进程之间分配任务。

提示:假设您有 comm_size=4n=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;

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;

In 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;

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.

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 if comm_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 and n=8, the local_sum for each process should calculate n/comm_size iterations but with different starting and ending indexes. i.e 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文