I have a CFD Algorithm that I implemented first in MATLAB (slow), then in C++ (blazing fast), and now I am trying to recreate it in Julia. I have already achieved much faster performance than MATLAB but there seem to be some final hurdles before I can get to ~C++ speeds in Julia.
Currently, my code works like this. I have a script which pre-allocates empty arrays of size eg 33x33, and then runs a function called mainLoop()
which does all the calculations. I currently test my code by using the @time
macro during the call to the mainLoop
function.
Scenario 1:
# include some header files which are later used by mainLoop etc.
# other code, not relevant here
# pre-allocate about 10-15 arrays. not all are shown here.
u = zeros(nc); v = zeros(nc);
oldu = zeros(nc); oldv = zeros(nc);
deltau = zeros(nc); deltav = zeros(nc);
# call the main function which does the calculations.
@time mainLoop(deltaVL2Norm);
Doing the above returns a time for the mainLoop function of about 4.5 seconds.
If instead, I notice that once I have run this script once, my 10-15 arrays such as u,v etc. have all already been defined (initial guesses donât really matter much), I can just erase the part of my script which does the pre-allocation and simply run mainLoop()
in the script.
Scenario 2:
# include some header files which are later used by mainLoop etc.
# other code, not relevant here
# don't do any pre-allocation since this script was already called once, so
# the arrays already exist.
# u = zeros(nc); v = zeros(nc);
# oldu = zeros(nc); oldv = zeros(nc);
# deltau = zeros(nc); deltav = zeros(nc);
# call the main function which does the calculations.
@time mainLoop(deltaVL2Norm);
When I do this, the @time
macro gives me 0.5 seconds for mainLoop
!
I realize I may be making some basic mistakes in how I think about functions, scripts, and so on but I still think there should be a way for me to get this ~0.5 second performance without the antics I have described here.
The behavior is identical if I put all my array declarations into a function and call that function in this script.
Whatâs particularly puzzling is that I am not timing the entire script - only the call to mainLoop
, which I didnât even change at all!