I compiled this code with ifort 17.0 and it gives segfault.
It works with gfortran 6.2 but ifort fails. Is this ifort's bug?
This is traceback
#0 0x0000003a06a7a96c in free () from /lib64/libc.so.6
#1 0x0000000000563e7b in for_dealloc_allocatable ()
#2 0x00000000004057ca in MAIN__ ()
#3 0x0000000000403ade in main ()
#4 0x0000003a06a1ecdd in __libc_start_main () from /lib64/libc.so.6
#5 0x0000000000403969 in _start ()
gnu libc version status
ldd (GNU libc) 2.12
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Written by Roland McGrath and Ulrich Drepper.
ifort version status
ifort (IFORT) 17.0.0 20160721
Copyright (C) 1985-2016 Intel Corporation. All rights reserved.
FFTW version 3.3.5 compiled with --enable-avx --enable-openmp
I have this issue with ifort 12.1 as well as 17.0
I think this is your bug. The description of the fftw_alloc_xxx routines say:
Data allocated by
fftw_malloc must be deallocated by
fftw_free and not by the ordinary
In other words, don't use Fortran DEALLOCATE on storage allocated by fftw.
You cannot use a Fortran deallocate using a pointer that was not directly allocated. e.g.
ALLOCATE(yourPointer(nX, nY, nZ))
IOW, if the pointer were initializes with => or with c_f_pointer, you must not DEALLOCATE.
If initialized with =OtherPointer (that was directly allocated) then you are walking on thin ice.
In the sample code, you should be calling fftw_free(p_ffttest), fftw_free(p_ffttest_c) and remember to nullify the pointers (both Fortran and C_PTR).
Don't be embarrassed. It is a common mistake in multi-language programming that allocation/deallocation are freely interchangeable between languages. While this may have been the case some time ago, it is not necessarily true as languages develop. C++ may have dtor's to call, C# has garbage collection, Fortran may have Final routines (dtor-like), etc...