fortran - Array in derived type and OpenMP cause segmentation fault -
this question has answer here:
i have simple code.
program test_example use iso_c_binding, only: c_double, c_int implicit none integer, parameter :: nelems = 500000 integer, parameter :: np = 16, nvar = 4, nflux = 16 type mesh2d real(c_double) :: u(np, nelems) real(c_double) :: uflux(nflux, nelems) real(c_double) :: ucommon(nflux, nelems) end type mesh2d type(mesh2d) :: mesh integer(c_int) :: i, j, k !$omp parallel j = 1, nelems k = 1, np mesh%u(k, j) = j+k end end !$end parallel end program test_example
i compile using
gfortran -g temp.f90 -o main.exe -fopenmp
and gives me segmentation fault. same code runs fine if instead of using derived type used array.
is bug or doing wrong.
i ran segfault conundrum on laptop, code ran without hitch on powerful desktop machine. nelems = 500000
requires heap access. following @vladimir f suggestion obtained following:
this file compiled gcc version 5.4.0 20160609 using options -cpp -imultiarch x86_64-linux-gnu -d_reentrant -mtune=generic -march=x86-64 -g -fopenmp
from
module type_mesh2d use iso_c_binding, only: & wp => c_double, & ip => c_int ! explicit typing implicit none ! private unless stated otherwise private public :: wp, ip public :: nelems, np, nvar, nflux public :: mesh2d integer (ip), parameter :: nelems = 500000 integer (ip), parameter :: np = 16, nvar = 4, nflux = 16 type, public :: mesh2d real (wp), dimension (:,:), allocatable :: u, uflux, ucommon end type mesh2d interface mesh2d module procedure allocate_arrays module procedure default_allocate_arrays end interface mesh2d contains pure function allocate_arrays(n, m, k) result (return_value) ! dummy arguments integer (ip), intent (in) :: n, m, k type (mesh2d) :: return_value allocate( return_value%u(n, m) ) allocate( return_value%uflux(k, m) ) allocate( return_value%ucommon(k, m) ) end function allocate_arrays pure function default_allocate_arrays() result (return_value) ! dummy arguments type (mesh2d) :: return_value return_value = allocate_arrays(np, nelems, nflux) end function default_allocate_arrays end module type_mesh2d program test_example use iso_fortran_env, only: & compiler_version, compiler_options use type_mesh2d ! explicit typing implicit none type (mesh2d) :: mesh integer (ip) :: i, j, k ! allocate memory mesh = mesh2d() !$omp parallel j = 1, nelems k = 1, np mesh%u(k, j) = j + k end end !$end parallel print '(4a)', & 'this file compiled ', compiler_version(), & ' using options ', compiler_options() end program test_example
Comments
Post a Comment