Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29236 Diskussionen

Segfault with OpenMP and combination of Fortran/C

sonicato
Einsteiger
1.039Aufrufe
I have an error by executing a combination Fortran/C program with OpenMP pragma.

C source code:

#include
#include

extern void fentry_( int * );

int main(){
int level=4, num;

num = omp_get_max_threads();
printf("omp threads = %d ",num);
fflush(stderr);
fflush(stdout);

fentry_( &level );
return 0;
}

Fortran source code:

module fe
use omp_lib
implicit none

type matrixStruct
real(kind=8), dimension(:,:), allocatable :: matrix
end type matrixStruct

contains

subroutine f1(level)
integer(kind=4) :: level, i, j, m
type(matrixStruct), dimension(:), allocatable :: struct

allocate( struct(1:level) )

do i=1, level
m = ((2**i)+1)**2
allocate( struct(i)%matrix(1:m,1:m) )

write(*,*) "matrix ", i, " befor, size=", size(struct(i)%matrix(:,1)), size(struct(i)%matrix(1,:))
do j=1, m
write(*,*) struct(i)%matrix(:,j)
end do

!$omp parallel do shared(m, struct) private(i, j)
do j=1, m
struct(i)%matrix(:,j) = 0
end do
end do

do i=1, level
m = ((2**i)+1)**2
write(*,*) "matrix ", i, " after, size=", size(struct(i)%matrix(:,1)), size(struct(i)%matrix(1,:))
do j=1, m
write(*,*) struct(i)%matrix(:,j)
end do
end do

do i=1, level
deallocate( struct(i)%matrix )
end do
deallocate( struct )
end subroutine f1
end module fe

subroutine fentry(level)
use fe
integer(kind=4) :: level

call f1(level)
end subroutine fentry

The source code was compiled with the flags "-g -O2 -mcpu=itanium2-p9000 -mtune=itanium2-p9000 -openmp" and the compiler "INTEL C/C++ Compiler Version 10.1 Build 015" and "INTEL Fortran Compiler Version 10.1 Build 015".

Running the program end in a segfault.

I ruled out all usual suspects.

Any help would be appreciated.
Thanks in advance,
Jens

0 Kudos
6 Antworten
TimP
Geehrter Beitragender III
1.039Aufrufe

I can't get a properly formatted copy of your source code to try it.

As I would take your comment about having tried all evident solutions to include such measures as setting thread stack size by kmp_stacksize (environment variable or function call), it seems you have excluded anyone from commenting.

sonicato
Einsteiger
1.039Aufrufe
I have added an attachment with the source code, copy the 2 parts in the .txt file to a .c file and a .f90 file.

Yes, I have tried to set KMP_STACKSIZE and also to compile with "-heap-arrays 0", but no changes.

The program ends after printing the first matrix in the first loop.
jimdempseyatthecove
Geehrter Beitragender III
1.039Aufrufe

Jens,

OpenMP in FORTRAN require an !$OMP END xxx at the end of parallel regions. In this case you are missing

!$omp end parallel do

after the statement that ends the do loop that was parallized

 !$omp parallel do shared(m, struct) private(i, j)
do j=1, m
struct(i)%matrix(:,j) = 0
end do
!$omp end parallel do

Jim Dempsey

sonicato
Einsteiger
1.038Aufrufe
I am not sure, if this "!$omp end ..." statement is realy necessary,
but the error also occurs with this statement.
sonicato
Einsteiger
1.038Aufrufe
I think, I have the solution.
The privat(i) is wrong, firstprivate(i) seems to be correct, else i has not the value like befor the omp pragma.
jimdempseyatthecove
Geehrter Beitragender III
1.038Aufrufe

Sonicato,

In looking at your parallelloop you want shared(i)

The parallel loop is inside the do loop iterating on i. i must be passed into the parallel region (and not modified in there).

Jim Dempsey

Antworten