mfem: Evaluation of grid function coefficient leads to memory corruption

Hello,

I have an issue with the evaluation of VectorGridFunctionCoefficient.

I am trying to solve a simple waveguide problem: $$\vec{\nabla} \times (\vec{\nabla} \times \vec{E} ) - k_0^2 \vec{E} = 0 $$, where $\vec{E}$ is the electric field, and $k_0$ is the wavevector. The geometry is a simple elongated box with Dirichlet boundary conditions on the side walls and a specific Robin boundary conditions at the ends which represent the input and output ports: $$\hat{n} \times (\vec{\nabla} \times \vec{E}) = -i \beta \vec{E} + [\hat{n} \times (-2i\beta E_i \hat{x})]$$, where $\hat{n}$ is the unit vector normal, $\beta$ is the propagation constant, $E_i(x,y)$ is the incident field and $\hat{x}$ is the unit vector along the $x$ coordinate.

As you can see, the Robin boundary condition in this problem depends on the incident field $E_i$. To implement such boundary condition I use a subclass of VectorCoefficient where I hard coded the incident field $E_i$. However, in future I would like to define $E_i$ outside and pass it into the Robin bc class using VectorGridFunctionCoefficient. However, on the way to implementing this approach, I encountered a very strange problem. When I try to evaluate the corresponding VectorGridFunctionCoefficient inside the Robin bc class (without even using the results of this evaluation) I see that the otherwise correct solution becomes distorted. Thus, a simple evaluation of an arbitrary VectorGridFunctionCoefficient leads to some memory corruption. Can you please help me to understand what is going on.

In the attachment I put the MWE (the code, the mesh, and a makefile template to build, run and visualize the solution) which demonstrates the problem. To fix the distorted solution issue, you just need to comment out line 62 in the gfcoef_issue_mwe.cpp. The following figures show the correct solution and the distorted one:

Distorted solution:

Correct solution:

MWE files: gfcoef_issue_mwe.zip

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 17 (10 by maintainers)

Most upvoted comments

Returning DofTransformation by value from GetElementVDofs(int i, Array<int> &vdofs) has unnecessary overhead when used as

  doftrans = fes->GetElementVDofs(i, vdofs);

This involves constructing a temporary DofTransformation on the stack and then copy-assigning that to doftrans.

This should be a move-assignment rather than a copy-assignment.

If used (inside the loop) as

  DofTransformation doftrans = fes->GetElementVDofs(i, vdofs);

the copy will be avoided but construction, initialization (e.g. heap allocation for the Array<int> data member), and destruction costs will be incurred on each iteration.

Yes, this cost would be incurred if returning by value and doesn’t seem avoidable. It would be interesting to see if this is non-negligible (that’s why I asked about performance regression tests).

On the other hand, the call (with doftrans constructed outside the loop)

  fes->GetElementVDofs(i, vdofs, doftrans);

avoids the new object construction, the heap re-allocation, and the copy-assignment without any drawbacks – at least I don’t see any drawbacks.

The drawback is that, unless the two-argument version is changed to no longer return a pointer to the internal object, it’s still easy to encounter bugs like the one reported in this issue.

This is not a drawback of the the method I’m suggesting but of the existing 2-argument version.

As a solution to the issue with the current two-argument version, we can simply make it trigger an error if used in a case that requires the use of DofTransformation with an explanation to switch to using the new 3-argument version that I’m proposing. This will change current behavior, so to preserve backward-compatibility for some time, before introducing the error message, we can just issue a warning instead.

Yes, I think that approach should work.

Even better (but a breaking change) would be to just return the DofTransformation by value in all cases — the pattern of returning a reference/pointer to an internal object that can be invalidated mysteriously in the future seems to cause us a number of problems. Do we have performance regression tests to measure if the performance impact of such a change would be meaningful? (Maybe timing the sample runs, but maybe we have something more systematic).

Dear Socratis,

Your hack works. Will wait for a more permanent solution. Thank you.

Hi @vladimir-yu-fedorov, as @pazner said this is a bit of a complicated issue and we will discuss it with the rest of the team to decide on a fix. In the meantime you can fix it by adding this line

doftrans = fes -> GetBdrElementVDofs (i, vdofs);

just before

https://github.com/mfem/mfem/blob/9a9087e20b214cfb74f684429bd912e21b9381b3/fem/linearform.cpp#L263-L266

This will recompute and overwrite the doftrans again after it was overwritten inside the integrator. This is not a nice fix, more like a hack, until with figure something else out.

I hope this helps.

Best,

Socratis