Morpho-lang / morpho

The Morpho language 🦋. Morpho is a small embeddable language for scientific computing applications.
MIT License
30 stars 10 forks source link

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

Open joshichaitanya3 opened 4 months ago

joshichaitanya3 commented 4 months ago

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.