0

I need to some how run one MPI node at a time. I am printing out information relevant to arrays specific to each MPI node. I have formatted print statements for each MPI node according to rank. However, I am having trouble printing out all the arrays without getting either a deadlock, or a mish mash of print formatting as all the nodes print their statements at once.

I am still new to MPI, and the only function I know about right now is MPI Barrier. Here is my code; I have been thinking about it and placing MPI barrier in different places...but, like I said, I seem to get only deadlocks after one or two arrays print out, or just random print statements.

In this current configuration, I am getting print statements in random order; however, there is no deadlock (shifting the bottom barrier up two lines to inside the for loop starting at line 4 would produce one or two properly formatted print statements, but results in a deadlock):

if (debug == 1) {
    if (iterations == 0) {
        MPI_Barrier(MPI_COMM_WORLD);
        for (j = 0; j < mpi_nodes; j++) {
            if (rank == j) {
                printf("\n\nrank %d\n", j);
                printf("\n");
                for (i = 0; i < m2; i++) {
                    j = i / m;
                    k = i % m;
                    if (k == 0) {
                        printf("\n"); //
                    }
                    printf("%1.f ", f[i]);
                }
                printf("\n");
            }
        }
        printf("\n reached.");
        MPI_Barrier(MPI_COMM_WORLD);
        printf("\n passed.\n");
    }
}

How would I implement MPI commands such that the print loop--after the rank test at the 5th line--is run for each MPI node, one at a time (and preferably in order from rank 0 to n).

To get the code to run, it would just take the array,

int m = 5;
int m2 = m*m;
double f[m2] = etc.

But I think this is just a knowledge question...there must be some kind of atomic-esque block for MPI, right?

Chris
  • 28,822
  • 27
  • 83
  • 158
  • Move the barriers inside the `j` loop, "bracketing" the `if` clause. In other words, the first to just before the `if` statement, and the second to after the `if` -closing `}` but before the loop-closing `}`. That way the nodes run the loop in lockstep, and only one node will print in each iteration. – Nominal Animal Dec 16 '16 at 19:05
  • 1
    Running code in certain order by multiple MPI processes is possible, e.g., via token passing. Getting ordered output unless channelled to a single rank is not. – Hristo Iliev Dec 19 '16 at 08:09
  • Thanks--good to know it is not possible. I think I figured out a work around, but I cannot remember what I did after the break. – Chris Jan 13 '17 at 22:00

0 Answers0