Open MPI logo

Open MPI User's Mailing List Archives

  |   Home   |   Support   |   FAQ   |   all Open MPI User's mailing list

Subject: Re: [OMPI users] printf and scanf problem of C code compiled with Open MPI
From: Meilin Bai (meilin.bai_at_[hidden])
Date: 2011-03-30 04:31:23


You remind me. I now realize that it is not an matter of compiler, but an
issue of C language. The printf() function in C doesn't print messages onto
the standard ouput immediately, but instead stores them in a buffer. Only in
some cases does the standard output work, defined in standard C:
    1. The buffer is full or freshed compulsively (like use fflush()).
    2. When a newline comes (\n).
    3. Needs reading from buffer (such as scanf()).

Some compilers may deal with this function, and compiled by Mpich2 this code
seems be well-off.

Thanks very much.

On Wed, Mar 30, 2011 at 3:39 PM, Pascal Deveze <Pascal.Deveze_at_[hidden]>wrote:

> Maybe this could solve your problem: Just add \n in the string you want to
> display:
> printf("Please give N= \n");
>
> Of course, this will return, but the string is displayed. This run by me
> without the fflush().
>
> On the other hand, do you really observe that the time of the scanf () and
> the time to enter "N" be insignificant ?
>
> Pascal
>
> Meilin Bai a écrit :
>
> So it means that MPI doesn't suit to interactive programming? Though we can
> really use fflush(stdout) to get the right order, it takes too more time,
> and it's said that using fflush() is not a good progrmming style in C.
>
> On the other hand, in Fortran language, this situation won't exist. Maybe
> it is because I/O implement is a built-in part of Fortran, while in C/C++ it
> is realized only through function like scanf, printf, et al?
>
>
>
> On Wed, Mar 30, 2011 at 2:38 AM, Prentice Bisbal <prentice_at_[hidden]> wrote:
>
>> On 03/29/2011 01:29 PM, Meilin Bai wrote:
>> > Dear open-mpi users:
>> >
>> > I come across a little problem when running a MPI C program compiled
>> > with Open MPI 1.4.3. A part of codes as follows:
>> >
>> > MPI_Init(&argc, &argv);
>> > MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
>> > MPI_Comm_rank(MPI_COMM_WORLD, &myid);
>> > MPI_Get_processor_name(processor_name, &namelen);
>> > if (myid == 0) {
>> > printf("Please give N= ");
>> > //fflush(stdout);
>> > scanf("%d", &n);
>> > startwtime = MPI_Wtime();
>> > }
>> >
>> > If comment out the sentence of "fflush(stdout);", it doesn't print out
>> > the message till I input an integer n. And if I add the fflush function
>> > between them, it works as expected, though comsumming time obviously.
>> >
>> > However, when I compiled it with Mpich2-1.3.2p1, without fflush function
>> > in the code, it works correctly.
>> >
>> > Can anyone know what the matter is.
>> >
>>
>> The Open MPI Developers (Jeff, Ralph, etc) can confirm this:
>>
>> The MPI standard doesn't have a lot of strict requirements for I/O
>> behavior like this, so implementations are allowed to buffer I/O if they
>> want. There is nothing wrong with requiring fflush(stdout) in order to
>> get the behavior you want. In fact, if you check some text books on MPI
>> programming, I'm pretty sure they recommend using fflush to minimize
>> this problem.
>>
>> MPICH behaves differently because its developers made different design
>> choices.
>>
>> Neither behavior is "wrong".
>>
>> --
>> Prentice
>> _______________________________________________
>> users mailing list
>> users_at_[hidden]
>> http://www.open-mpi.org/mailman/listinfo.cgi/users
>>
>
>
>
> --
> Meilin Bai
>
> ------------------------------
>
> _______________________________________________
> users mailing listusers_at_[hidden]http://www.open-mpi.org/mailman/listinfo.cgi/users
>
>
>
> _______________________________________________
> users mailing list
> users_at_[hidden]
> http://www.open-mpi.org/mailman/listinfo.cgi/users
>

-- 
Meilin Bai
School of Electronic Engineering and Computer Science, Peking University
Beijing 100871, China
E-Mail: meilin.bai_at_[hidden]
           meilin_at_[hidden]
MSN: meilin.bai_at_[hidden]
Cellular: +86-1342-619-8430