0

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.

Encipher
  • 1,370
  • 1
  • 14
  • 31
  • Please point out where you think `my_rank` is used/referenced and in each case where you think it is declared beforehand. Also explain what you think this line `void Get_input(int my_rank, int comm_sz, int *n){` does in the later middle of `main()`. – Yunnosch Feb 25 '22 at 06:15
  • 1
    Does this answer your question? [Nested function in C](https://stackoverflow.com/questions/2608158/nested-function-in-c) – Yunnosch Feb 25 '22 at 06:19
  • The duplicate I propose explains that you cannot define a function within another function, only declare. But that is pointless. Move the function definition outside of `main()`. Most easily probably to just move it before. Then you will notice that its parameter is unknown within `main()` and will easily find a way to fix that. – Yunnosch Feb 25 '22 at 06:21
  • "I am not looking for C programming syntax" Yet your problem has nothing to do with MPI and is completely a problem of you not using C correctly. – Victor Eijkhout Feb 25 '22 at 12:53

1 Answers1

1

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.

j23
  • 3,139
  • 1
  • 6
  • 13
  • Hi, You can see I know how to declare a function, how to call the function. I did that for `double sum()` function in my code. But for `void Get_input()` where should I declare the the prototype? Before function main()? and how could I pass the parameters? In `double sum()` I passed the parameter `local_sum=sum(local_n);`. So I need to pass the parameter within main function and declare the function body before main for `Void Get_input`. But it is not clear in An introduction to parallel programming by Matthew Malensek – Encipher Feb 25 '22 at 17:01
  • 1
    I have solve the problem – Encipher Feb 26 '22 at 05:29
  • That’s good to hear. The general rule of thumb to approach MPI during our learning phase is to write the sequential program first. Make sure it works as needed. Then you can move on to parallelizing it. This is relevant because if there are any issues with correctness of result or errors, we can be confident (not always) that our MPI implementation broke the code and hence easy to debug. – j23 Feb 26 '22 at 06:53
  • Additionally, you should think of MPI program as independent programs (it is indeed like that) running in parallel different machines and you are only using MPI to transfer data between these different programs. No data is shared between different mpi processes. For example, beginners might assume that global variables are shared like in threads - but no. If anything need to be shared, you have to use MPI calls. – j23 Feb 26 '22 at 06:54
  • Thanks for explanation. I have implemented serial program, point to point communication and broadcasting. If the process number is 1 serial program, point to point and broadcasting gave same result. If the the process is 8 I got same result for point to point and broadcasting. Is my implementation for point to point and broadcasting ok? – Encipher Feb 26 '22 at 06:57
  • 1
    General Suggestions: After you have a working sequential program, then you can think about how to parallelize it. For example, what all info (send minimum data as possible) needs to be shared and when. If it is configuration then at the beginning. If we need to aggregate the results, then at the end. So, create a mental model of how to approach your problem. After that, think about what should be the changes in the code. If it is a parallel sum, then your some function should work on local sum with elements Total_size/world_size. So modify it accordingly. – j23 Feb 26 '22 at 07:04
  • 1
    @Encipher as a rule of thumb, if your mpi program gives the same output as sequential program it is correct. So try with 1,2,4, 6, 8 etc and see you are getting same results. For most of the problems, correctness is tested with the result from the sequential programs. – j23 Feb 26 '22 at 07:06
  • What about the same result of point to point communication and broadcasting if the number of processes same say, 8? Is it alright? – Encipher Feb 26 '22 at 07:13