In the context of the development of a finite element code, I have a dictionary that is formed after reading an input data file containing the finite element mesh (that is, nodal coordinates and element connectivity). I am in the process of deciding how I am going to unpack the finite element mesh from the dictionary to perform the long computation. Here, I present two approaches to perform the long computation. The codes look something like these:
Approach 1:
function main1()
file = "inputdata.txt"
data = read_input(file) # data is a dictionary containing a finite element mesh (nodal coordinates, element connectivity, etc.)
solve1(data)
end
function solve1(data)
# some auxiliary inexpensive computations
someoutputs = do_some_short_computations()
# long computation that depends on data
loop_over_elements1(data)
# additional auxiliary inexpensive computations
do_some_other_short_computations()
end
function loop_over_elements1(data)
# this is an array of a "struct" type containing the data of an element
elements = data["elements"]
# the loop
@inbounds for e = 1:length(elements)
element = elements[e] # the struct containing the information of the current element
nodes = element.nodes # this is the nodal connectivity of the current element
do_some_stuff_with_nodes(nodes)
end
end
Approach 2:
function main2()
file = "inputdata.txt"
data = read_input(file) # data is a dictionary containing a finite element mesh (nodal coordinates, element connectivity, etc.)
solve2(data)
end
function solve2(data)
# some auxiliary inexpensive computations
someoutputs = do_some_short_computations()
# long computation
elements = data["elements"] # this is an array of a "struct" type containing the data of an element
loop_over_elements2(elements)
# additional auxiliary inexpensive computations
do_some_other_short_computations()
end
function loop_over_elements2(elements)
@inbounds for e = 1:length(elements)
element = elements[e] # the struct containing the information of the current element
nodes = element.nodes # this is the nodal connectivity of the current element
do_some_stuff_with_nodes(nodes)
end
end
What do you think it is a better (in term of performance) approach? to pass the data dictionary inside the “loop_over_elements” function and unpack the elements inside this function (Approach 1) or to unpack the array of elements before entering the “loop_over_elements” function (Approach 2)?