Skip to content

Instantly share code, notes, and snippets.

@jeffhammond
Forked from certik/README.md
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffhammond/9746080 to your computer and use it in GitHub Desktop.
Save jeffhammond/9746080 to your computer and use it in GitHub Desktop.

Compile using either of:

./compile_gnu

and you should get the result:

           3 T F
true -> false
           3 F T
false -> true
Done.

Now compile using Intel:

./compile_intel

and you get the result:

           3 T F
true -> true
           3 F T
false -> true
Done.

Which is incorrect. Enable -fpscomp logical in compile_intel, recompile, then you get:

           3 T F
true -> false
           3 F T
false -> true
Done.
module a
use iso_c_binding, only: c_int, c_bool
implicit none
private
public foo
contains
subroutine foo(n, relat, relat_out) bind(c, name="foo")
integer(c_int), intent(in), value :: n
logical(c_bool), intent(in), value :: relat
logical(c_bool), intent(out) :: relat_out
relat_out = .not. relat
print *, n, relat, relat_out
end subroutine
end module
# you can only find Cray compilers on a Cray machine...
module load PrgEnv-cray
module swap PrgEnv-intel PrgEnv-cray
module swap PrgEnv-gnu PrgEnv-cray
module swap PrgEnv-pgi PrgEnv-cray
cc -V
cc -o m.o -c m.c
ftn -o a.o -c a.f90
cc m.o a.o -o cray
gcc -o m.o -c m.c
gfortran -o a.o -c a.f90
gcc m.o a.o -lgfortran -o gnu
xlc -o m.o -c m.c
xlf2003 -o a.o -c a.f90
xlc m.o a.o -lxlf -o ibm
icc -o m.o -c m.c
#ifort -fpscomp nologicals -o a.o -c a.f90
ifort -o a.o -c a.f90
icc m.o a.o -lifcore -o intel
pgcc -o m.o -c m.c
pgf90 -o a.o -c a.f90
pgcc m.o a.o -pgf90libs -o pgi
#include <stdio.h>
#include <stdbool.h>
void foo(int n, bool relat, bool *relat_out);
int main()
{
bool relat_out;
foo(3, true, &relat_out);
printf("true -> %s\n", relat_out ? "true" : "false");
foo(3, false, &relat_out);
printf("false -> %s\n", relat_out ? "true" : "false");
printf("Done.\n");
return 0;
}
@jeffhammond
Copy link
Author

Compiling with Cray:

jhammond@edison02:~/ELEMENTAL/f2c-logical> cat compile_cray 
# you can only find Cray compilers on a Cray machine...
module load PrgEnv-cray || module swap PrgEnv-intel PrgEnv-cray || module swap PrgEnv-gnu PrgEnv-cray || module swap PrgEnv-pgi PrgEnv-cray
cc -o m.o -c m.c
ftn -o a.o -c a.f90
cc m.o a.o -o cray

Results with Cray:

jhammond@edison02:~/ELEMENTAL/f2c-logical> ./cray 
           3 T F
true -> true
           3 F T
false -> true
Done.

@certik
Copy link

certik commented Mar 24, 2014

Thanks! So Cray fails as well. What about IBM?

@DanielCChen
Copy link

With the latest XLF AIX 14.1 compiler and the XLC AIX 12.1 compiler, without any additional option required, it produces

3 T F
true -> false
3 F T
false -> true
Done.

@longb
Copy link

longb commented Mar 24, 2014

Not sure how Jeff got those results. Here is what I get with the current (8.2.5) Cray compiler:

> ftn -c modu.f90
> cc main.c modu.o
> ./a.out
3,  T,  F
true -> false
3,  F,  T
false -> true
Done.

Additionally, I tried a simpler code:

use,intrinsic :: iso_c_binding 
logical(c_bool) :: x
x = .true.
print *, c_bool
write (*,"(g10.8,L5)") transfer (x, 0_1), x
x = .not. x
write (*,"(g10.8,L5)") transfer (x, 0_1), x

end

which results in 

> ftn test.f90
> ./a.out
1
        1    T
        0    F

illustrating that the Cray compiler does use 1 and 0 for true and false, so does have correct interoperability.

@jeffhammond
Copy link
Author

Here's the BGQ release of XL on the POWER7 Linux logins...

[jhammond@vestalac1 f2c-logical]$ xlc -qversion=verbose
IBM XL C/C++ for Blue Gene, V12.1
Version: 12.01.0000.0006
Driver Version: 12.01(C/C++) Level: 131009
C Front End Version: 12.01(C/C++) Level: 131009
C++ Front End Version: 12.01(C/C++) Level: 131023
High-Level Optimizer Version: 12.01(C/C++) and 14.01(Fortran) Level: 131022
Low-Level Optimizer Version: 12.01(C/C++) and 14.01(Fortran) Level: 131009
[jhammond@vestalac1 f2c-logical]$ xlf -qversion=verbose
IBM XL Fortran for Blue Gene, V14.1
Version: 14.01.0000.0006
Driver Version: 14.01(Fortran) Level: 131009
Fortran Front End and Run Time Version: 14.01(Fortran) Level: 131009
Fortran Transformer Version: 14.01(Fortran) Level: 131009
High-Level Optimizer Version: 12.01(C/C++) and 14.01(Fortran) Level: 131022
Low-Level Optimizer Version: 12.01(C/C++) and 14.01(Fortran) Level: 131009
[jhammond@vestalac1 f2c-logical]$ ./ibm 
 3 T F
true -> false
 3 F T
false -> true
Done.
[jhammond@vestalac1 f2c-logical]$ cat compile_ibm 
xlc -o m.o -c m.c
xlf2003 -o a.o -c a.f90
xlf m.o a.o -o ibm

@jeffhammond
Copy link
Author

As I noted through another channel, Bill is correct and my attempts to test Cray's behavior were at first flawed.

@certik
Copy link

certik commented Mar 25, 2014

Thanks everybody for testing this! Conclusion: gfortran, Cray, IBM work. Intel and PGI need an extra option to work.

I've documented this behavior here:

http://www.fortran90.org/src/gotchas.html#c-fortran-interoperability-of-logical

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment