0

I tried to reproduce the question raised in this post. Below is my code. According to the answer, since every process runs independently, the global_variable should be 0 in process 1. However, process 1 also prints 1000. So in my understanding, the processes are spawned in MPI_Init, so if the global variables are defined before MPI_Init, the created processes will get the same value, right? Do I misunderstood that post?

#include <stdio.h>
#include <mpi.h>

static int global_variable;

main(int argc, char **argv)
{
    int ierr, num_procs, my_id;

    global_variable = 1000;

    ierr = MPI_Init(&argc, &argv);

    /* find out MY process ID, and how many processes were started. */

    ierr = MPI_Comm_rank(MPI_COMM_WORLD, &my_id);
    ierr = MPI_Comm_size(MPI_COMM_WORLD, &num_procs);

    if( my_id == 0 ) {
        printf("%d\n", global_variable);
    }
    else if( my_id == 1 ) {
        printf("%d\n", global_variable);
    }

    ierr = MPI_Finalize();
}
Community
  • 1
  • 1
HuangJie
  • 1,488
  • 1
  • 16
  • 33
  • Just wondering, why are you storing all those `ierr` return values if you're not using them? – Kusalananda Jul 30 '16 at 06:42
  • 1
    There is a point in the comment by @Kusalananda. Moreover, the default error handler for MPI calls not related to file I/O terminates the entire job, therefore those `MPI_...` routines will either return `MPI_SUCCESS` or not return at all. Unless the `MPI_ERRORS_RETURN` error handler has been **explicitly** set, that is. – Hristo Iliev Jul 30 '16 at 07:26

1 Answers1

1

This is what independent means:

#include <stdio.h>
#include <mpi.h>

static int global_variable;

int main(int argc, char **argv)
{
    int ierr, num_procs, my_id;
    ierr = MPI_Init(&argc, &argv);

    ierr = MPI_Comm_rank(MPI_COMM_WORLD, &my_id);
    ierr = MPI_Comm_size(MPI_COMM_WORLD, &num_procs);

    if (my_id == 0) {
        global_variable = 1000;
    }
    MPI_Barrier(MPI_COMM_WORLD);
    printf("%d\n", global_variable);

    ierr = MPI_Finalize();
}

Here, only process 0 changes global_variable. In your example the global_variable = 1000; line isn't specific to any one process, and all processes will execute it.

a3f
  • 8,517
  • 1
  • 41
  • 46
  • Thanks for your reply. I had misunderstanding before. I thought the newly created processes will execute the program from right after MPI_Init. However, according to your answer, "all processes will execute global_variable = 1000", this means every process runs the program from the beginning of main(), am I right? Many thanks. – HuangJie Jul 30 '16 at 07:10
  • 2
    @HuangJie, virtually all production-level MPI implementations use separate OS processes for each MPI rank, which means that the ranks execute the program code in its entirety starting from `main()`. Still, such behaviour is not mandated by the MPI standard and implementations that use threads instead with all the consequences such as shared global variables exist. – Hristo Iliev Jul 30 '16 at 07:20