For Monte Carlo simulation with same code same algorithm, how fast is Julia compared with Fortran?

I’ve followed your various threads with interest, and having done some learning and porting myself, I have a few suggestions.

  1. The first line-by-line translation should be for testing, not performance. You’ve got your Monte Carlo sim working, which is great. Think of it a ground truth to test an eventual port, but not the basis for truly useful and performant code.
  2. The eventual port should probably not be a direct translation from Fortran. I understand why you set up your globals, which is fine for getting things to work, but now you can sit back and consider how you should really take advantage of Julia, which probably means organizing the data differently. In the big picture, there are lots of options like DataFrames, AxisArrays, StructArrays, NamedTuples, etc. Almost certainly there is something far better than the current globals.
  3. People usually recommend profiling. You may have a lot of unnecessary allocations going on, and could get astonishing gains just by stamping those out. There may be only a few low-level functions in a hot loop. Since you have the first port working, try profiling to find out where time and memory is being spent.
  4. Once I have my initial port going, I also like to build bottom-up. The low-level functions are often just math, and relatively straightforward to get big gains, say by broadcasting and avoiding allocations. If you’re lucky, you can progress on these and benchmark them, without having too much effect on the top-down organization. Unit-testing is easy because you have a working version to compare against. You can go bottom-up in parallel with thinking top-down, although I think bottom-up will only get you on par with Fortran, not usually better.
  5. I’m tempted to think it’s not necessary to reproduce all aspects of your former Fortran other than the computations. It takes a lot of effort to get the formatting exactly right, is it critical? I used to spend a lot of time in printf land, and I’m gradually learning that people don’t do that anymore. Nowadays there are PrettyTables.jl, csv, json, etc. Complicated output is more likely to be plotted, or read by machine, than by human.

To address your overall question, I fully expect that a line-by-line translation will get you Fortran-like performance, but that should not be the goal. Rather, aim to get something that’s easier to maintain, more readable by people, uses modern array/data storage and output, and is backed by good unit-tests.

I fully support this trick as the way to get started, but I’d think seriously about moving on to better approaches than this:

Best of luck, I look forward to your further posts!

10 Likes