I have a simple FORTRAN code
#define DTYPE(a) a/**/_s
TYPE,EXTENDS(DTYPE(type1)) :: DTYPE(type2)
CLASS(DTYPE(type1)_), POINTER :: x => NULL()
CONTAINS
PROCEDURE :: comp => DTYPE(comp1)
END TYPE DTYPE(type2)
#undef DTYPE
Using GNU preprocessor I will get the correct behavior which I expect, while using CLANG preprocessor it will behave completely strange.
using cpp-4.9 (GNU)
cpp-4.9 -traditional-cpp -P test.f
:
TYPE,EXTENDS(type1_s) :: type2_s
CLASS(type1_s_), POINTER :: x => NULL()
CONTAINS
PROCEDURE :: comp => comp1_s
END TYPE type2_s
using cpp (CLANG)
cpp -traditional-cpp -P test.f
:
TYPE,EXTENDS(type1 _s) :: type2 _s
CLASS(type1 _s _), POINTER :: x => NULL()
CONTAINS
PROCEDURE :: comp => comp1 _s
END TYPE type2 _s
using cpp (CLANG) there is an extra space which will cause compiler error. Does anyone know what is the problem?
Related
I have a C program that has a function named get_name. This function returns a string (i.e. char *) and changes the argument size (passed to it) with the size of the string:
char *get_name(int &size)
{
*size = strlen(name); // name is a C global variable declared as a char *
return name;
}
I have created the following Fortran module to be able to call the C function get_name:
MODULE X
USE, INTRINSIC :: iso_c_binding, ONLY: c_intptr_t
IMPLICIT NONE
INTERFACE
TYPE(c_ptr) FUNCTION get_name_(size) BIND(C, name = "get_name")
USE, INTRINSIC :: iso_c_binding, ONLY: c_int, c_ptr
INTEGER(c_int), INTENT(OUT) :: size
END FUNCTION
END INTERFACE
CONTAINS
FUNCTION get_name()
USE, INTRINSIC :: iso_c_binding, ONLY: c_int, c_char, c_f_pointer, c_ptr, c_associated
!DEC$ ATTRIBUTES DLLEXPORT :: get_name
CHARACTER(LEN = :), ALLOCATABLE :: get_name
INTEGER(c_int) :: size
TYPE(c_ptr) :: c_string
c_string = get_name_(size)
IF (c_associated(c_string)) THEN
BLOCK
CHARACTER(KIND = c_char, LEN = size), POINTER :: f_string
CALL c_f_pointer(c_string, f_string)
get_name = f_string
END BLOCK
ELSE
get_name = ""
END IF
END FUNCTION
END MODULE
I can successfully compile this Fortran module using IFORT 2016 in Windows, IFORT 2016 in Linux, and gfortran in Linux.
To test things, I have created a short Fortran program:
PROGRAM Test
USE X
WRITE(*, *) "Name: ", get_name()
END PROGRAM
I can successfully compile this Fortran program using IFORT 2016 in Windows, IFORT 2016 in Linux, and gfortran in Linux.
Now, when running the program it works well in IFORT 2016 for Linux, gfortran in Linux, but not in IFORT 2016 for Windows. It actually gives the following error:
forrtl: severe (157): Program Exception - access violation
Any idea how this error can be solved?
I assume your C function is char *get_name(int *size) instead of using a C++ reference.
Then this code works here on Windows and Linux with Ifort 16 and gcc-6.3.1 or Visual Studio 2015.
Please add your used C compiler and command lines to compile the code. I used:
cl -c testc.c && ifort -c testf.f /extend-source:132 && ifort testm.f testf.obj testc.obj /extend-source:132 && testm.exe
Btw: If you are using this function in parallel code I would suggest to avoid the block construct - I had bad experience using this in parallel (OpenMP) code with Ifort 16.
I have some code that passes an internal function of the main program as an argument to a function: when the function that been passed is eventually called it causes a segmentation fault. This only occurs when I use Windows Subsystem for Linux (I'm using Ubuntu 16 on WSL); running on native Linux or Mac machines this does not occur.
A minimal example that crashes:
module test1
implicit none
contains
subroutine x(ff,y)
interface
real function ff(y)
real, intent(in) :: y
end function ff
end interface
real, intent(in) :: y
integer z
z=ff(y)
end subroutine x
end module test1
program tester
use test1
implicit none
call x(f,1.0)
contains
real function f(y)
real, intent(in) :: y
write(*,*) y
f=y*y
end function f
end program tester
Compiled with:
gfortran-7 -ggdb test_fun_passing.f90 -o test
The backtrace, gdb output:
(gdb) bt
#0 0x00007ffffffde320 in ?? ()
#1 0x0000000000400734 in test1::x (ff=0x7ffffffde320, y=1) at test_fun_passing.f90:17
#2 0x0000000000400829 in tester () at test_fun_passing.f90:31
#3 0x0000000000400860 in main (argc=1, argv=0x7ffffffde64f) at test_fun_passing.f90:27
#4 0x00007ffffec70830 in __libc_start_main (main=0x40082c <main>, argc=1, argv=0x7ffffffde448, init=<optimized out>, fini=<optimized out>, rtld_fini=<optimized out>, stack_end=0x7ffffffde438) at ../csu/libc-start.c:291
#5 0x0000000000400669 in _start ()
This one does work (moving f into its own module but still passing as an argument) so it's something about f being contained in the program.
module test1
implicit none
contains
subroutine x(ff,y)
interface
real function ff(y)
real, intent(in) :: y
end function ff
end interface
real, intent(in) :: y
integer z
z=ff(y)
end subroutine x
end module test1
module test2
implicit none
contains
real function f(y)
real, intent(in) :: y
write(*,*) y
f=y*y
end function f
end module test2
program tester
use test1
use test2
implicit none
call x(f,1.0)
end program tester
gfortran-7 -ggdb test_fun_passing.f90 -o test && ./test
1.00000000
Is passing f in this way valid Fortran, or have I been relaying on some non-standard feature on native Linux?
Looks like I'm running into this:
https://github.com/Microsoft/WSL/issues/3083 and https://github.com/Microsoft/WSL/issues/286
WSL has a non-executable stack. Running:
excestack -c test
on native linux, to remove the execstack from the binary, triggers the same error message as I have on WSL. Clearing/setting the execstack (with -c/-s) on WSL does nothing. From the github bug report it seems its unlikely to be fixed.
Edit:
It seems moving to WSL version 2 fixes this issue
I have an legacy Fortran code that I want to mix with a new C/C++ program.
The Fortran subroutine allocates dynamically some arrays that I want to pass to c program. I will only get the size of these arrays after running the Fortran code.
After getting some tips here in this forum I arrived to the following code that I thought it would best compile, link and run.
Actually I can compile my C code and my Fortran code separately, but it doesn't link giving the following errors:
undefined reference to _gfortran_runtime_error
undefined reference to _gfortran_os_error
I'm using g++ and GFortran compilers version 5.4.0, and linking both .o files with g++ and the option -lg fortran.
fortran code:
subroutine test_allocation(outp) bind(c)
use iso_c_binding
implicit none
type (c_ptr), value :: outp
integer, target ::b(2)
integer(c_int), pointer :: a(:)
b(1)=1
b(2)=2
allocate(a(2))
a=>b
call c_f_pointer(outp, a,[2])
end subroutine
c code:
#include <iostream>
using namespace std;
extern "C" void test_allocation(int ** ptr);
int main ()
{
int* ptr;
test_allocation(&ptr);
}
EDIT:
As Vladimir F said on comments there was a mistake in my compiler option. The correct is -lgfortran.
Now it's linking but the results is not what I expect. I change a little my code to show this:
Fortran code:
subroutine test_allocation(outp) bind(c)
use iso_c_binding
implicit none
type (c_ptr), value :: outp
integer, target ::b(2)
integer(c_int), pointer :: a(:)
b(1)=1
b(2)=2
allocate(a(2))
a=>b
print*, "a(1) in Fortran: ", a(1)
print*, "a(2) in Fortran: ", a(2)
call c_f_pointer(outp, a,[2])
print*, "outp after c_f_pointer: ", outp
end subroutine
C code:
#include <iostream>
using namespace std;
extern "C" void test_allocation(int** ptr);
int main ()
{
int* ptr;
test_allocation(&ptr);
cout<<"ptr[0] in C: "<< ptr[0]<<endl;
cout<<"ptr[1] in C: "<< ptr[1]<<endl;
}
The output is:
a(1) in fortran: 1
a(2) in fortran: 2
outp after c_f_pointer: 140726088663920
ptr[0] in C: 1447122753
ptr[1] in C: 1107265857
I also tried changing the declaration of extern function to the following and it still does not work:
extern "C" void test_allocation(int*& ptr);
...
test_allocation(ptr);
The output is:
a(1) in fortran: 1
a(2) in fortran: 2
outp after c_f_pointer: 140729541703872
ptr[0] in C: 1447122753
ptr[1] in C: 1107265857
Now it works. Instead of using C_F_POINTER I used the function C_LOC.
I also removed the parameter VALUE from the TYPE(C_PTR) declaration.
Here is the code:
subroutine test_allocation(outp) bind(c)
use iso_c_binding
implicit none
type (c_ptr) :: outp
integer ::b(2)
integer,dimension(:), pointer :: a
b(1)=1
b(2)=2
allocate(a(2))
a=b
print*, "a(1) in fortran: ", a(1)
print*, "a(2) in fortran: ", a(2)
outp=c_loc(a)
print*, "outp after c_loc: ", outp
end subroutine
and it's output:
a(1) in fortran: 1
a(2) in fortran: 2
outp after c_loc: 36866928
ptr[0] in C: 1
ptr[1] in C: 2
The following link helped me a lot although I'm not using intel compiler:
https://software.intel.com/en-us/forums/intel-fortran-compiler-for-linux-and-mac-os-x/topic/269660
I am trying to compile a fortran module with f2py. It is the following code
module my_log_mod
implicit none
interface my_log
module procedure my_log_array
module procedure my_log_vector
end interface my_log
private ! hides items not listed on public statement
public :: my_log
contains
subroutine my_log_array(a,res)
double precision, dimension (:,:), intent (in) :: a
double precision, dimension (:,:), intent (out) :: res
where (a>1.0)
res = log(a)
else where
res = 0.D0
end where
end subroutine
subroutine my_log_vector(a,res)
double precision, dimension (:), intent (in) :: a
double precision, dimension (:), intent (out) :: res
where (a>1.0)
res = log(a)
else where
res = 0.D0
end where
end subroutine
end module my_log_mod
that I compile with the following command
f2py.py -c -m my_log_mod_comp my_log_mod.f90
and it results in the following error
C:\Users\weisshau\AppData\Local\Temp\tmpf0apqa7s\src.win32-3.6\my_log_mod_comp-f2pywrappers2.f90:7:28:
use my_log_mod, only : my_log_array
1
Error: Symbol 'my_log_array' referenced at (1) not found in module 'my_log_mod'
C:\Users\weisshau\AppData\Local\Temp\tmpf0apqa7s\src.win32-3.6\my_log_mod_comp-f2pywrappers2.f90:18:28:
use my_log_mod, only : my_log_vector
1
Error: Symbol 'my_log_vector' referenced at (1) not found in module 'my_log_mod'
I don't really know much about fortran and f2py, so I don't have any idea what is happening. If i use the module in pure fortran it works nicely
F2py appears to be creating another wrapper code which uses the subroutines in your module.
But it calls directly the subroutines my_log_vector and my_log_array. It seems f2py does not support private. I would delete the private.
Also be prepared that you won't be able to use the generic my_log in Python. This concept of generics is alien to Python. You may need to delete the generic interface if deleting private does not make it compilable. You should definitely delete the public :: my_log.
Unfortunately, f2py does not support all features of modern Fortran.
The code I tested:
module my_log_mod
implicit none
interface my_log
module procedure my_log_array
module procedure my_log_vector
end interface my_log
contains
subroutine my_log_array(a,res)
double precision, dimension (:,:), intent (in) :: a
double precision, dimension (:,:), intent (out) :: res
where (a>1.0)
res = log(a)
else where
res = 0.D0
end where
end subroutine
subroutine my_log_vector(a,res)
double precision, dimension (:), intent (in) :: a
double precision, dimension (:), intent (out) :: res
where (a>1.0)
res = log(a)
else where
res = 0.D0
end where
end subroutine
end module my_log_mod
compilation:
f2py -c -m my_log_mod_comp my_log_mod.f90
...
Post-processing...
Block: my_log_mod_comp
Block: my_log_mod
Block: my_log
Block: my_log_array
Block: my_log_vector
Post-processing (stage 2)...
Block: my_log_mod_comp
Block: unknown_interface
Block: my_log_mod
Block: my_log_array
Block: my_log_vector
Building modules...
Building module "my_log_mod_comp"...
Constructing F90 module support for "my_log_mod"...
Creating wrapper for Fortran subroutine "my_log_array"
res = my_log_array(a)
res = my_log_array(a)
Creating wrapper for Fortran subroutine "my_log_vector"("my_log_vector")...
Constructing wrapper function "my_log_mod.my_log_vector"...
res = my_log_vector(a)
Wrote C/API module "my_log_mod_comp" to file "/tmp/tmp7e5v0u/src.linux-x86_64-2.7/my_log_mod_compmodule.c"
Fortran 90 wrappers are saved to "/tmp/tmp7e5v0u/src.linux-x86_64-2.7/my_log_mod_comp-f2pywrappers2.f90"
...
gfortran:f90: /tmp/tmp7e5v0u/src.linux-x86_64-2.7/my_log_mod_comp-f2pywrappers2.f90
/usr/bin/gfortran -Wall -g -Wall -g -shared /tmp/tmp7e5v0u/tmp/tmp7e5v0u/src.linux-x86_64-2.7/my_log_mod_compmodule.o /tmp/tmp7e5v0u/tmp/tmp7e5v0u/src.linux-x86_64-2.7/fortranobject.o /tmp/tmp7e5v0u/my_log_mod.o /tmp/tmp7e5v0u/tmp/tmp7e5v0u/src.linux-x86_64-2.7/my_log_mod_comp-f2pywrappers2.o -L/usr/lib64 -lpython2.7 -lgfortran -o ./my_log_mod_comp.so
Removing build directory /tmp/tmp7e5v0u
f2py gives an error when I try compile FORTRAN subroutine, from which I call another FORTRAN function passing array to it.
I simplified code to leave the problem only.
SUBROUTINE MAS (matrix, a)
IMPLICIT NONE
INTERFACE
LOGICAL FUNCTION LTRY(input_array)
IMPLICIT NONE
INTEGER*4 :: input_array(:,:)
END FUNCTION LTRY
END INTERFACE
INTEGER*4 :: matrix (:,:)
!f2py INTENT(INOUT) :: matrix(:,:)
INTEGER*4 a
!f2py INTENT(INOUT) :: a
a = 1
IF ( LTRY (matrix)) a = 2
END SUBROUTINE
LOGICAL FUNCTION LTRY(input_array)
IMPLICIT NONE
INTEGER*4 :: input_array (:,:)
IF ( ANY(input_array == 0)) LTRY = .FALSE.
END FUNCTION LTRY
After I try to compile this using
f2py -c -m ptest ptest.f90
I get a tremendous number of errors. What's wrong?
Using Ubuntu and gfortran compiler.
Here is compiler output (part with error reports):
gfortran:f77: /tmp/tmpSZHMCR/src.linux-i686-2.7/ptest-f2pywrappers.f
/tmp/tmpSZHMCR/src.linux-i686-2.7/ptest-f2pywrappers.f:16.35:
function ltry(input) ! in :ptest:ptest.f90:mas:unkno
1
Error: Unexpected junk after function declaration at (1)
/tmp/tmpSZHMCR/src.linux-i686-2.7/ptest-f2pywrappers.f:18.72:
integer, dimension(:,:) :: input
1
Error: Unexpected data declaration statement in INTERFACE block at (1)
/tmp/tmpSZHMCR/src.linux-i686-2.7/ptest-f2pywrappers.f:19.72:
logical :: ltry
1
Error: Unexpected data declaration statement in INTERFACE block at (1)
/tmp/tmpSZHMCR/src.linux-i686-2.7/ptest-f2pywrappers.f:20.17:
end function ltry
1
Error: Expecting END INTERFACE statement at (1)
/tmp/tmpSZHMCR/src.linux-i686-2.7/ptest-f2pywrappers.f:40.32:
ltryf2pywrap = .not.(.not.ltry(input_array))
1
Error: Operand of .not. operator at (1) is INTEGER(4)
/tmp/tmpSZHMCR/src.linux-i686-2.7/ptest-f2pywrappers.f:16.35:
function ltry(input) ! in :ptest:ptest.f90:mas:unkno
1
Error: Unexpected junk after function declaration at (1)
/tmp/tmpSZHMCR/src.linux-i686-2.7/ptest-f2pywrappers.f:18.72:
integer, dimension(:,:) :: input
1
Error: Unexpected data declaration statement in INTERFACE block at (1)
/tmp/tmpSZHMCR/src.linux-i686-2.7/ptest-f2pywrappers.f:19.72:
logical :: ltry
1
Error: Unexpected data declaration statement in INTERFACE block at (1)
/tmp/tmpSZHMCR/src.linux-i686-2.7/ptest-f2pywrappers.f:20.17:
end function ltry
1
Error: Expecting END INTERFACE statement at (1)
/tmp/tmpSZHMCR/src.linux-i686-2.7/ptest-f2pywrappers.f:40.32:
ltryf2pywrap = .not.(.not.ltry(input_array))
1
Error: Operand of .not. operator at (1) is INTEGER(4)
error: Command "/usr/bin/gfortran -Wall -ffixed-form -fno-second-underscore -fPIC -O3 -funroll-loops -I/tmp/tmpSZHMCR/src.linux-i686-2.7 -I/usr/lib/python2.7/dist-packages/numpy/core/include -I/usr/include/python2.7 -c -c /tmp/tmpSZHMCR/src.linux-i686-2.7/ptest-f2pywrappers.f -o /tmp/tmpSZHMCR/tmp/tmpSZHMCR/src.linux-i686-2.7/ptest-f2pywrappers.o" failed with exit status 1
Compilation failed.
Some errors look quite strange. For example compiler considers logical function as an integer.
Unfortunately, you have not indicated which type of error you get.
I have tried to compile it on my system, and I do get error messages:
/tmp/tmpLKy0jV/src.linux-x86_64-2.7/ptest-f2pywrappers.f:16:35:
function ltry(input) ! in :ptest:ptest.f90:mas:unkno
1
Error: Unexpected junk after function declaration at (1)
/tmp/tmpLKy0jV/src.linux-x86_64-2.7/ptest-f2pywrappers.f:18:72: Error: Unexpected data declaration statement in INTERFACE block at (1)
/tmp/tmpLKy0jV/src.linux-x86_64-2.7/ptest-f2pywrappers.f:19:72: Error: Unexpected data declaration statement in INTERFACE block at (1)
/tmp/tmpLKy0jV/src.linux-x86_64-2.7/ptest-f2pywrappers.f:20:17:
end function ltry
1
Error: Expecting END INTERFACE statement at (1)
/tmp/tmpLKy0jV/src.linux-x86_64-2.7/ptest-f2pywrappers.f:16:35:
function ltry(input) ! in :ptest:ptest.f90:mas:unkno
1
Error: Unexpected junk after function declaration at (1)
/tmp/tmpLKy0jV/src.linux-x86_64-2.7/ptest-f2pywrappers.f:18:72: Error: Unexpected data declaration statement in INTERFACE block at (1)
/tmp/tmpLKy0jV/src.linux-x86_64-2.7/ptest-f2pywrappers.f:19:72: Error: Unexpected data declaration statement in INTERFACE block at (1)
/tmp/tmpLKy0jV/src.linux-x86_64-2.7/ptest-f2pywrappers.f:20:17:
end function ltry
1
Error: Expecting END INTERFACE statement at (1)
Looking into the wrapped file, we see that the wrapper has made a very dubious decision: It has added comments at the end of the line, but then line-wrapped the comment without declaring it a comment in the second line:
C -*- fortran -*-
C This file is autogenerated with f2py (version:2)
C It contains Fortran 77 wrappers to fortran functions.
subroutine f2pywrapmas (matrix, a, f2py_matrix_d0, f2py_matr
&ix_d1)
integer*4 a
integer f2py_matrix_d0
integer f2py_matrix_d1
integer*4 matrix(f2py_matrix_d0,f2py_matrix_d1)
interface
subroutine mas(matrix,a)
integer*4, dimension(:,:) :: matrix
integer*4 :: a
interface ! in :ptest:ptest.f90:mas
function ltry(input) ! in :ptest:ptest.f90:mas:unkno
&wn_interface
integer, dimension(:,:) :: input
logical :: ltry
end function ltry
end interface
end subroutine mas
end interface
call mas(matrix, a)
end
I don't know how to help you, though. But I do suggest you post your actual error messages in the question.