Discussion:
problem with fprintf and OpenMP
Siegmar Gross
2014-05-15 06:01:13 UTC
Permalink
Hi,

I'm using gcc-4.9.0 and have a problem with the following program.
I have reported the problem allready on gcc-help some days ago,
but didn't get any replies. Perhaps somebody in this list knows,
if the behaviour is intended.

#include <stdio.h>
#include <stdlib.h>
#include <omp.h>

int main (void)
{
#pragma omp parallel default (none)
fprintf (stderr, "Hello!\n");
return EXIT_SUCCESS;
}


I get the following error, when I try to compile the program
on "Solaris 10 Sparc".

tyr OpenMP 116 \gcc -fopenmp omp_fprintf.c
In file included from /usr/include/stdio.h:66:0,
from omp_fprintf.c:38:
omp_fprintf.c: In function 'main':
omp_fprintf.c:45:12: error: '__iob' not specified in enclosing parallel
fprintf (stderr, "Hello!\n");
^
omp_fprintf.c:44:11: error: enclosing parallel
#pragma omp parallel default (none)
^
tyr OpenMP 117


I can solve the problem if I add "shared(__iob)" to "#pragma",
but then the program isn't portable any longer.

tyr OpenMP 118 grep stderr /usr/include/iso/stdio_iso.h
#define stderr (&__iob[2])
#define stderr (&_iob[2])
tyr OpenMP 119


I have a similar problem with Linux and Mac OS X, which need
a different solution, because "stderr" is defined in a different
way.

In my opinion the compiler knows that I use OpenMP and that
I use "default(none)" so that it can automatically add all
necessary internal variables to the "shared clause" or that
it only reports errors for variables which are not part of
system header files. I don't know how many macros or variables
will cause such an error. If their number is small, the compiler
can maintain a small table with these names. Otherwise it may
be a little bit more complicated. Furthermore I don't know if
their might be a reason to have a private copy of an internal
variable (although I cannot see any reason for such a thing,
because an application program shouldn't know about internal
variables or how system macros expand to internal variables).

I would be happy, if it would be possible to write operating
system independend code if I use standards (C11, OpenMP,
pthreads, ...) and that the compiler would solve all operating
system dependend problems if I only use standardized constructs.
Perhaps somebody knows, if it is possible to fulfill my wish in
an upcoming release of gcc.


Kind regards

Siegmar
Leif Leonhardy
2014-05-15 08:09:35 UTC
Permalink
Post by Siegmar Gross
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
int main (void)
{
#pragma omp parallel default (none)
fprintf (stderr, "Hello!\n");
return EXIT_SUCCESS;
}
I get the following error, when I try to compile the program
on "Solaris 10 Sparc".
tyr OpenMP 116 \gcc -fopenmp omp_fprintf.c
In file included from /usr/include/stdio.h:66:0,
omp_fprintf.c:45:12: error: '__iob' not specified in enclosing parallel
fprintf (stderr, "Hello!\n");
^
omp_fprintf.c:44:11: error: enclosing parallel
#pragma omp parallel default (none)
^
tyr OpenMP 117
I can solve the problem if I add "shared(__iob)" to "#pragma",
but then the program isn't portable any longer.
tyr OpenMP 118 grep stderr /usr/include/iso/stdio_iso.h
#define stderr (&__iob[2])
#define stderr (&_iob[2])
tyr OpenMP 119
I have a similar problem with Linux and Mac OS X, which need
a different solution, because "stderr" is defined in a different
way.
To get around this, you can add

FILE *fp=stderr;

and use fp instead of stderr, also adding shared(fp).


-leif
Jakub Jelinek
2014-05-15 08:17:35 UTC
Permalink
Post by Siegmar Gross
I'm using gcc-4.9.0 and have a problem with the following program.
I have reported the problem allready on gcc-help some days ago,
but didn't get any replies. Perhaps somebody in this list knows,
if the behaviour is intended.
Yes, this is what OpenMP standard requires.
Post by Siegmar Gross
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
int main (void)
{
#pragma omp parallel default (none)
fprintf (stderr, "Hello!\n");
return EXIT_SUCCESS;
}
I can solve the problem if I add "shared(__iob)" to "#pragma",
but then the program isn't portable any longer.
Or don't use default (none), or don't use stderr directly in the
default(none) parallel region (use a temporary variable, call
a function that implicitly uses stderr, etc.).
Post by Siegmar Gross
In my opinion the compiler knows that I use OpenMP and that
I use "default(none)" so that it can automatically add all
necessary internal variables to the "shared clause" or that
Please define what is internal variable. __iob is for the compiler
not an internal variable of any kind, stderr is a macro like any other.
Note that e.g. on Linux stderr is a macro and a variable
(#define stderr stderr), treating in that case stderr as an internal
variable would be really weird.

Jakub

Loading...