Segmentation fault in MEX files when parallelized with openmp

3 views (last 30 days)
I'm trying to parallelize a mex code written in FORTRAN90 using OpenMP directives. The code works perfectly when I don't use any OpenMP but when I parallelize it I receive segmentation faults. I debugged the code running MATLAB in debugger mode. It reaches to the end of mex function without any problem and it yields this error
"Program received signal SIGSEGV, Segmentation fault. [Switching to Thread 0x7fff7afe5700 (LWP 9631)] 0x0000003c0e210410 in ?? ()"
when passing the result to MATLAB workspace I guess. Does anyone has any idea for a possible reason for such an error? I use R2014a. And here is the code:
subroutine qtest(srcRe,srcIm,nF,nS,freq,srcPos,nDim,lstPos,nL,c0,resp)
use omp_lib
implicit none
integer*8, intent(in) :: nF, nS, nDim, nL
real*8, intent(in) :: srcRe(nF,nS),srcIm(nF,nS),freq(nF)
real*8, intent(in) :: srcPos(nS,nDim), lstPos(nL,nDim), c0
real*8, intent(out) :: resp(nF,nL)
real*8 :: pi, k(nF), r(nS), x2(nS)
complex*16 :: icplx, G(nF), ikr(nF), p(nF)
integer*4 :: i,j,m
icplx = (0.d0,1.d0)
pi = 4.d0*datan(1.d0)
k = 2.d0*pi*freq/c0
resp = 0.d0
G = 0.d0
!$OMP PARALLEL
do i = 1,nL
!$OMP SINGLE
x2 = (lstPos(i,1) - srcPos(:,1))**2.d0
r = x2
do m = 2,nDim
r = r + (lstPos(i,m) - srcPos(:,m))**2.d0
enddo
r = dsqrt(r)
p = 0.d0
!$OMP END SINGLE
!$OMP DO PRIVATE(j,ikr,G) REDUCTION(+:p)
do j = 1,nS
ikr = icplx*k*r(j)
G = cdexp(-ikr)/(4.d0*pi*r(j))
p = p + (srcRe(:,j) + icplx*srcIm(:,j))*G
enddo
!$OMP END DO
!$OMP SINGLE
resp(:,i) = resp(:,i) + 20.d0*dlog10(cdabs(p)/20.d-6)
!$OMP END SINGLE
enddo
!$OMP END PARALLEL
end subroutine qtest
And the mexfunction I use to transfer the variables from/to Matlab workspace is this:
#include "fintrf.h"
!
SUBROUTINE MEXFUNCTION(NLHS, PLHS, NRHS, PRHS)
use mexf90
implicit none
integer*8, intent(in) :: PRHS(*) ! Pointers carrying the input data
integer*8, intent(out) :: PLHS(*) ! Pointers carrying the output data
integer*4, intent(in) :: NLHS,NRHS ! REMAINS THE SAME FOR 64BIT system
!-----------------------------------------------------------------------
integer*8 :: err, nF, nF1, nS, nS1, nDim, nDim1, nL
real*8 :: c0
integer*8, pointer :: srcRe, srcIm, freq, srcPos, lstPos, resp
character(200) :: errMsg
integer*4 :: txt, classId
integer*4, external :: mexprintf
! ASSIGN POINTERS TO THE VARIOUS PARAMETERS
srcRe =>MXGETPR(PRHS(1))
nF = MXGETM(PRHS(1))
nS = MXGETN(PRHS(1))
srcIm =>MXGETPR(PRHS(2))
freq =>MXGETPR(PRHS(3))
nF1 = MXGETM(PRHS(3))
srcPos =>MXGETPR(PRHS(4))
nS1 = MXGETM(PRHS(4))
nDim = MXGETN(PRHS(4))
lstPos =>MXGETPR(PRHS(5))
nL = MXGETM(PRHS(5))
nDim1 = MXGETN(PRHS(5))
c0 = MXGETSCALAR(PRHS(6))
plhs(1) = mxCreateDoubleMatrix(nF,nL,0)
resp =>mxGetPr(plhs(1))
call qtest(srcRe, srcIm,nF,nS,freq,srcPos,nDim,lstPos,nL,c0,resp)
END SUBROUTINE MEXFUNCTION
  1 Comment
James Tursa
James Tursa on 19 Jun 2015
Sorry for the late reply. I just saw this post. I haven't looked at your code in any detail, but will tell you that MATLAB used to be incompatible with Fortran and OpenMP. Perfectly valid code using the simplest OpenMP constructs would crash MATLAB. This was several years ago, so this issue may have been fixed by now and may not be the cause of your current problem.

Sign in to comment.

Answers (0)

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!