Morpho-lang / Morpho-lang/morpho

[Bug] Modifying global variables inside integrands of Functionals segfaults when run in parallel

Open
#262 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug enhancement
Dominant language
C
Stars
153
Forks
22
Avg merge
9d 10h
Merged PRs (30d)
1

Description

The fix for this is already planned, as mentioned in #259, but I am documenting it here as an open issue for posterity.

Describe the bug
An integrand function of, say, an AreaIntegral, is not allowed to modify state (see #259). This results in a Segmentation Fault if run in multithreaded setting.

To Reproduce
Run the following code using morpho6 -w4

import constants
import meshtools

var mb = MeshBuilder()
mb.addvertex([0,0])
mb.addvertex([1,0])
mb.addvertex([0,1])
mb.addface([0,1,2])

var m = mb.build()

var f = Field(m, fn (x,y) Matrix([x,2*y]))
var r

fn integrand(x, n) {
    var g = grad(n)
    r = []
    for (u in g) r.append(u.clone())
    return 0
}

var total = AreaIntegral(integrand, f).total(m)

print r[0]

Note that r is defined in the global scope and being modified inside the integrand.

Expected behavior
Some clear error pointing out that the variable r cannot be modified inside the integrand.

Currently, this generates a segmentation fault.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided reproducer with morpho6 -w4 and read the related discussion in #259. Trace how the AreaIntegral integrand handles modification of global variable r during multithreaded execution. Done means the segmentation fault is replaced by a clear error explaining that state cannot be modified inside the integrand.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.