!$OMP and segmentation faults

Numerical methods and mathematical models of Elmer
Post Reply
spacedout
Posts: 177
Joined: 30 Mar 2020, 23:27
Antispam: Yes

!$OMP and segmentation faults

Post by spacedout »

Hi

If I see that a solver contains !$OMP directives and I add my own subroutine to it, how I can ensure segmentation fault errors will not occur in that subroutine when I call it ?

Regards
spacedout
Posts: 177
Joined: 30 Mar 2020, 23:27
Antispam: Yes

Re: !$OMP and segmentation faults

Post by spacedout »

I am sure this will happen to somebody else so I will answer my own question:

When you add your routines in the CONTAINS clause of your modified Elmer solver, then the following code will avoid memory corruption after you call your routines (denoted by MyTest1 and MyTest2 )


.....

MyVar1 = 1

!$OMP PARALLEL &
!$OMP SHARED( MyVar1, MyVar3 ) &
!$OMP PRIVATE(MyVar2) &

.............

MyVar2 = 1

MyVar3 = 1

CALL MyTest1 (MyVar2)

CALL MyTest2 ()

............

!$OMP END PARALLEL

First, note that if you did not declare MyVar1 as SHARED above but as PRIVATE then MyVar 1 would be undefined within the !$OMP directives.
Next, PRIVATE variable MyVar2 has to be passed as a parameter to MyTest1. Otherwise it would undefined inside routine MyTest1.
Finally, MyTest2 does not need to be passed MyVar3 because it has been declared SHARED.

I hope this will clarify matters.
Thank you for viewing
Post Reply