GeMA
The GeMA main application
Shared code blocks

Working with multiple threads to parallelize the simulation is mostly acomplished by the processes called from the orchestration script. Nevertheless, if your Lua orchestration code includes some heavy processing, such as loops over mesh nodes, cells or Gauss points to initialize data, or even to do some aditional calculations, it can be interesting to also parallelize those loops.

This can be accomplished by turning those loops into auxiliary Lua functions, each representing a task that operates over a part of the problem (mesh) domain. Those functions can then be called in parallel from multiple threads. The actual mechanics of how this parallel calling is done in the orchestration is explained in the parallel calls section.

Since a Lua environment can not be accessed from several threads in parallel, GeMA creates one Lua environment per worker thread, and the auxiliary Lua functions containing the per thread code, as described above, have to be defined in each of those Lua environments. Rather than loading all the model in each environment, which would be a huge waste of space and time, GeMA only replicates in each one the code present inside shared code blocks.

Shared code blocks are then chunks of Lua code inside a region, started by a SharedCodeBegin{} statement and ended by a corresponding SharedCodeEnd{} statement, defining the set of Lua functions that are available to be called inside a parallel call. Its contents are loaded in the Lua environment of each worker thread. It can contain several functions. If a global variable is declared in a shared block, it will have a per thread value, and not a single one. The set of values can be managed by functions described in the parallel calls section.

Shared code blocks can appear in any file loaded by the simulation, including the master file, the model file and/or the simulation file. There can be several shared code blocks in the model, but they can not be nested. Also, model objects such as Mesh, StateVar, etc are not allowed inside a shared code block, the only exception being the declaration of user functions, which must also appear in each worker thread environment, and so also define an implicit shared code block inf not already inside one.

Important: Due to implementation details, Lua long comments are not correctly handled when GeMA parses the model files searching for shared code blocks. The practical implication of that is that you should not use long comments to disable a shared code block. Use single line coments instead.

Example

--
-- Shared functions
--
SharedCodeBegin{}
-- Task for checking results
function nodeCheck(nodeTask, xyAc, svAc)
for node in nodeTask() do
local v = svAc:value(node)
local xy = xyAc:value(node)
assert(equal(v, xy(1) + xy(2)))
end
return true
end
SharedCodeEnd{}
--
-- User function
--
SharedCodeBegin{}
function h(x, y) return x + y end
NodeFunction{
id = 'nodef',
parameters = {{src = 'coordinate', dim = 1},
{src = 'coordinate', dim = 2},
},
method = h,
}
SharedCodeEnd{}