找不到 mpi 函数声明和定义的正确位置

发布于 2025-01-10 02:06:51 字数 2319 浏览 4 评论 0原文

我喜欢使用广播方法来实现 MPI 程序。我从教科书中找到了一些广播方法的代码块,并尝试修改我的 此代码

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

int main(void){

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

      void Get_input(int my_rank, int comm_sz, int *n){
              if(my_rank==0){
                    printf("Enter the value for n:");
                    scanf("%d",n);
           }
      MPI_Bcast(n,1,MPI_INT,0,MPI_COMM_WORLD);
 }
 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;
 }

现在我得到了这样的错误:

mpi_sumB.c: In function ‘main’:
mpi_sumB.c:13:34: error: ‘my_rank’ undeclared (first use in this function)
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
                              ^
mpi_sumB.c:13:34: note: each undeclared identifier is reported only once for each function it appears in
mpi_sumB.c:14:35: error: ‘comm_sz’ undeclared (first use in this function)
MPI_Comm_size(MPI_COMM_WORLD, &comm_sz);
                               ^
mpi_sumB.c:25:12: error: ‘n’ undeclared (first use in this function)
local_n= n/comm_sz;

现在很明显我得到了这个错误,因为我知道我在哪里写下了 void Get_input(int my_rank, int comm_sz, int *n) 可能这是不正确的这个功能的地方。但我没有得到任何明确的指导来定义这个函数。

感谢您的任何帮助

编辑:我不是在寻找 C 编程语法或嵌套函数。这是一个用 c 语言实现的 mpi 程序,更具体地说是广播问题。我想知道一般在 mpi 程序中我们需要定义广播。

I like to implement an MPI program using broadcasting method. I find out some code block for broadcasting method from text book and try to modify my this code.

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

int main(void){

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

      void Get_input(int my_rank, int comm_sz, int *n){
              if(my_rank==0){
                    printf("Enter the value for n:");
                    scanf("%d",n);
           }
      MPI_Bcast(n,1,MPI_INT,0,MPI_COMM_WORLD);
 }
 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;
 }

Now I got error like this:

mpi_sumB.c: In function ‘main’:
mpi_sumB.c:13:34: error: ‘my_rank’ undeclared (first use in this function)
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
                              ^
mpi_sumB.c:13:34: note: each undeclared identifier is reported only once for each function it appears in
mpi_sumB.c:14:35: error: ‘comm_sz’ undeclared (first use in this function)
MPI_Comm_size(MPI_COMM_WORLD, &comm_sz);
                               ^
mpi_sumB.c:25:12: error: ‘n’ undeclared (first use in this function)
local_n= n/comm_sz;

Now it is obvious I got this errors because I know where I write down the void Get_input(int my_rank, int comm_sz, int *n) may be this is not the right place for this function. But I did not get any clear guidance where I need to define this function.

Thank you for any help

Edit: I am not looking for C programming syntax or nested function. This is an mpi program implementing in c, more specifically broadcasting problem. I would like to know generally in mpi program where we need to define broadcasting.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

似狗非友 2025-01-17 02:06:51

我想知道在 mpi 程序中我们需要在哪里定义
广播。

应用程序的上下文很重要。如果您需要广播,例如配置信息(例如,在您的情况下,输入大小),则需要在您的领先排名(例如 rank 0)拥有相应信息后进行广播。因此,您可以在此之后立即调用MPI_Bcast

如果您询问定义函数的位置,即在 main 之前或 main 之后。没关系。文件内函数的顺序是任意的。如果将函数一放在文件顶部并将函数二放在底部,反之亦然,这并不重要。注意:为了让一个函数“看到”(使用)另一个函数,必须在使用之前在文件中看到该函数的“原型”。 查看此内容

I would like to know generally in mpi program where we need to define
broadcasting.

The context of the application matters. If you need to broadcast, say configuration information (for example, like in your case, the input size), you need to do the broadcast once your lead rank (say rank 0) has the corresponding information. So, you can call MPI_Bcast immediately after that.

If you are asking about the place to define the function i.e before the main or after the main. It doesn't matter. The order of functions inside a file is arbitrary. It does not matter if you put function one at the top of the file and function two at the bottom, or vice versa. Caveat: In order for one function to "see" (use) another function, the "prototype" of the function must be seen in the file before the usage. See this.

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