Page 1 of 1

!$OMP and segmentation faults

Posted: 09 Nov 2023, 22:54
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

Re: !$OMP and segmentation faults

Posted: 17 Nov 2023, 20:06
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