Closed marcnunezc closed 3 years ago
FYI @inigolcanalejo
Ah true... I should have thought about that. It is not the first time that I had this problem. As far as I understand if one initializes the variable, the memory gets allocated and the result is not random when accessing its values in parallel. If one doesn't initialize the variable, accessing its value gives random behavior in parallel. @jcotela and @sunethwarna explained it to me a few years back.
no clue TBH why this is happing. I checked the code several times in detail, it seems fine to me.
As you wrote this should be fine since you lock it properly Also I don't understand why the initialization changes it, this should not matter IMO (and it is designed to work without)
Anyway, the @KratosMultiphysics/technical-committee is aware, thx for reporting
When did you add this code? It looks quite standard to me, I am pretty sure there are many more places where this is used.
When did you add this code? It looks quite standard to me, I am pretty sure there are many more places where this is used.
This was added in https://github.com/KratosMultiphysics/Kratos/pull/3641 when first creating the element.
hi Guys, is this problem solved once you initialize the variables outside of the parallel loop? looking into the discussion we have the impression it is now solved, however if it is not it would be important to create a small mockup test (self contained, no elements etc) which reproduces the same type of behaviour
The problem is completely solved if the variables are initialized outside of the parallel loop.
I left the issue open due to the open question:
Is it expected that a race condition occurs with SetValue in a SetLock/UnsetLock operation?
(inside a parallel loop)
If it is expected then we can close this.
@marcnunezc you mean to initialize them in a separate parallel loop, correct? We @KratosMultiphysics/technical-committee would give as a feedback that it is always a good practice to do that before as miximing initialization and parallel operations is normally a very bad idea.
In any case we will close as there is no way to have that level of threadsafety without adding a lock before every get
@marcnunezc you mean to initialize them in a separate parallel loop, correct?
Yes, for me at least initializing them in a separate previous parallel loop solves the problem.
We @KratosMultiphysics/technical-committee would give as a feedback that it is always a good practice to do that before as miximing initialization and parallel operations is normally a very bad idea.
In any case we will close as there is no way to have that level of threadsafety without adding a lock before every get
:thumbsup: Is this written somewhere in the wiki? If not, it might be a good idea to add it so that future users and developers are aware of this. This gave me a lot of problems in the past, I think know I finally got that the variables need to be initialized before doing parallel operations :sweat_smile:
Thanks @roigcarlo also for his detailed explanation! https://github.com/KratosMultiphysics/Kratos/issues/8924#issuecomment-878903178
Description A few days ago a potential flow test was failing in the CI. The error trace was something like this:
https://github.com/KratosMultiphysics/Kratos/blob/7c86ef788ebd32a25591f8a364be9eb57a974593/applications/CompressiblePotentialFlowApplication/custom_utilities/potential_flow_utilities.cpp#L41
which is called in here in ComputeVelocity():
https://github.com/KratosMultiphysics/Kratos/blob/7c86ef788ebd32a25591f8a364be9eb57a974593/applications/CompressiblePotentialFlowApplication/custom_elements/compressible_potential_flow_element.cpp#L852-L858
Which is called just after ComputePotentialJump()
https://github.com/KratosMultiphysics/Kratos/blob/7c86ef788ebd32a25591f8a364be9eb57a974593/applications/CompressiblePotentialFlowApplication/custom_elements/compressible_potential_flow_element.cpp#L154-L158 which does some operation doing a Lock(): https://github.com/KratosMultiphysics/Kratos/blob/7c86ef788ebd32a25591f8a364be9eb57a974593/applications/CompressiblePotentialFlowApplication/custom_elements/compressible_potential_flow_element.cpp#L836-L848
This has been solved in #8904 by removing the conflicting code from the element (which was needed to be done anyway due to code duplication and the fact that it is a postprocess quantity).
After further debugging with @roigcarlo it turns out that the issue would have been solved as well by properly initializing the POTENTIAL_JUMP value before starting the analysis, e.g:
VariableUtils().SetNonHistoricalVariableToZero(POTENTIAL_JUMP, root_model_part.Nodes());
By adding this line at some point before the start of the analysis, the test works fine.
The point to discuss is:
Is it expected that a race condition occurs with SetValue in a SetLock/UnsetLock operation? We would expect that the SetLock/UnsetLock would prevent the race condition from happening even if the variable was not set beforehand. Any thoughts?
Scope Where in Kratos is this bug happening. E.g.
To Reproduce Compile
cps/ci-test-1
branch and run test_CompressiblePotentialFlowApplication.py tests with as many threads as possible.Expected behavior To not get a race condition if the locks are used.
Environment Please specify your operating system, kratos branch, python version or any other library versions that you consider relevant. E.g.: