locked
MS MPI and scanf RRS feed

  • Question

  • I tried to run the following program on Windows HPC Server R2
    http://www.mcs.anl.gov/research/projects/mpi/usingmpi/examples/simplempi/cpi_c.htm
    Unfortunately the program is not able to run in parallel.

    I can run it sequentially. But the command mpiexec -n 2 program doesn't work.
    I tried to compile it using mpich2 library for windows from
    http://www.mcs.anl.gov/research/projects/mpich2/

    Unfortunately it is not working as well.

    Is there any specific problem with that program on windows platform or it is a problem with my configuration. I tried that code on different computers with the same results. 
    I tested my hardware and software using other programs and everything is working good.

    The program acctually works if I remove "scanf("%d",&n);".
    Does "scanf" work on Windows version of MPI (HPC Server)?
    I run the program on Linux and everything was working fine.

    The code

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

    int main( int argc, char *argv[] )
    {
    int n, myid, numprocs, i;
    double PI25DT = 3.141592653589793238462643;
    double mypi, pi, h, sum, x;
    MPI_Init(&argc,&argv);
    MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
    MPI_Comm_rank(MPI_COMM_WORLD,&myid);

    while (1) {
    if (myid == 0) {
    printf("Enter the number of intervals: (0 quits) ");
    scanf("%d",&n);
    }

    MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
    if (n == 0)
    break;
    else {
    h = 1.0 / (double) n;
    sum = 0.0;
    for (i = myid + 1; i <= n; i += numprocs) {
    x = h * ((double)i - 0.5);
    sum += (4.0 / (1.0 + x*x));
    }
    mypi = h * sum;
    MPI_Reduce(&mypi, &pi, 1, MPI_DOUBLE, MPI_SUM, 0,
    MPI_COMM_WORLD);
    if (myid == 0)
    printf("pi is approximately %.16f, Error is %.16f\n",
    pi, fabs(pi - PI25DT));
    }
    }
    MPI_Finalize();
    return 0;
    }

    Saturday, September 11, 2010 12:24 AM

Answers

  • It is the right behavior if you press ctrl+c.

    Could you please add fflush(stdout) (it is used to flush out the buffer) after the printf statement and try again?

    Thanks,

    James

    • Marked as answer by Jedrek2004 Monday, September 13, 2010 7:55 PM
    Monday, September 13, 2010 6:12 PM

All replies

  • Hello,

    I've tried to compile and run the program with ms-mpi without any problem. I was wondering what's the error you saw with mpiexec -n 2 program in your case?

    Another suggestion, you can add fflush(stdout) after the printf() so you can get your printf message displayed right away. 

    The output I got:

    >mpiexec -n 4 pi.exe
    Enter the number of intervals: (0 quits) 2
    pi is approximately 3.1623529411764704, Error is 0.0207602875866773
    Enter the number of intervals: (0 quits) 4
    pi is approximately 3.1468005183939427, Error is 0.0052078648041496
    Enter the number of intervals: (0 quits) 8
    pi is approximately 3.1428947295916889, Error is 0.0013020760018958
    Enter the number of intervals: (0 quits) 16
    pi is approximately 3.1419181743085600, Error is 0.0003255207187669
    Enter the number of intervals: (0 quits) 0

    Thanks,

    James

    Monday, September 13, 2010 2:26 PM
  • No error message. Just black screen.
    After I pressed CTRL+C I see:
    -----------------------------------------------
    mpiexec aborting job...

    job aborted:
    [ranks] message

    [0-1] process exited without calling finalize

    ---- error analysis -----

    [0-1] on utephpc1
    ParallelPiC-oryginal.exe ended prematurely and may have crashed. exit code -1

    ---- error analysis -----

    Monday, September 13, 2010 4:53 PM
  • It is the right behavior if you press ctrl+c.

    Could you please add fflush(stdout) (it is used to flush out the buffer) after the printf statement and try again?

    Thanks,

    James

    • Marked as answer by Jedrek2004 Monday, September 13, 2010 7:55 PM
    Monday, September 13, 2010 6:12 PM
  • Now everything is working ok.

    Thank you very much for your help.

    This is the code which acctually works.

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

    int main( int argc, char *argv[] )
    {
     int n, myid, numprocs, i;
     double PI25DT = 3.141592653589793238462643;
     double mypi, pi, h, sum, x;
     MPI_Init(&argc,&argv);
     MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
     MPI_Comm_rank(MPI_COMM_WORLD,&myid);

     while (1) {
      if (myid == 0) {
       printf("Enter the number of intervals: (0 quits) ");
       fflush( stdout );
       scanf("%d",&n);
      }

      MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
      if (n == 0)
       break;
      else {
       h = 1.0 / (double) n;
       sum = 0.0;
       for (i = myid + 1; i <= n; i += numprocs) {
        x = h * ((double)i - 0.5);
        sum += (4.0 / (1.0 + x*x));
       }
       mypi = h * sum;
       MPI_Reduce(&mypi, &pi, 1, MPI_DOUBLE, MPI_SUM, 0,
        MPI_COMM_WORLD);
       if (myid == 0)
       {
        printf("pi is approximately %.16f, Error is %.16f\n",pi, fabs(pi - PI25DT));
        fflush( stdout );
       }
      }
     }
     MPI_Finalize();
     return 0;
    }

     

     

     

    Monday, September 13, 2010 7:54 PM