4

I am a new julia user about to write my first script. I use R, Matlab and python for data analysis and I have yet to have to worry about concerns people in c or java would have with memory allocation and more advacned programming I geuss. I want to use julia to simulate some biological data and in python these types of programs can get really slow. So as I was reading the performance tips in the part on memory allocation caught my eye. As some one who kind of lacks a background in more advanced languages like C what should I be looking for? I read this stack post here but he had python code he had compiled and then run in Julia so I don't feel it is pertinent.

How will I know when I have unusual memory allocation, and conversely what does "good" memory allocation in Julia look like? Is there a way to identify what an unacceptable level of memory demand is based on what I am running?

In addition to the @time macro what other proofing tips do you have?

Angus Campbell
  • 563
  • 4
  • 19

1 Answers1

4

To understand Julia performance issues very carefully read https://docs.julialang.org/en/v1/manual/performance-tips/ Running yourself all examples in that page is a must.

Usually, memory allocation is not the major bottleneck. From my experience when people are starting to use Julia the type stability and using global variables are a major source of all performance problems.

The above document has also a section on memory allocation:

"Unexpected memory allocation is almost always a sign of some problem with your code, usually a problem with type-stability or creating many small temporary arrays."

You will find there detailed discussion about addressing such issues.

Regarding code profiling you have the following tools:

  • @time macro (note that the first time you run it for a given methods you are actually mostly measuring compile time rather than run time. Hence usually BenchmarkTools.jl is recommended
  • BenchmarkTools.jl with its @btime macro (among others - the first choice to understand what is going on
  • inbuilt profiling (https://docs.julialang.org/en/v1/manual/profile/) together with ProfileView.jl and ProfileSVG.jl packages to visualize profiling data in standalone and Jupyter environments respectively
  • in most complicated cases you can view the code at various compiling stages with @code_lowered, @code_typed and @code_native

These are the basic and most useful tools for a great start to performant Julia code.

Przemyslaw Szufel
  • 40,002
  • 3
  • 32
  • 62
  • I'm planning on running dynamic simulations with julia. Is it ok to make parameters global variables and then keep passing an array through the functions to make transformations of the data or is that a bad idea? May be this explanation is a bit to vague. – Angus Campbell Feb 01 '21 at 13:52
  • Once you pass a global variable to a function as parameter than this is OK. – Przemyslaw Szufel Feb 01 '21 at 14:05
  • But I want to be avoiding making lots of temporary array's unless I'm debugging correct? In general once my code is done the only arrays should be my input and my data of interest correct? Edit: I geuss thats what the @code_typed and other macros are for! – Angus Campbell Feb 01 '21 at 14:15
  • Contrary to languages like R, passing objects to functions in Julia does not mean copying the data in memory. Have a look a this post: https://stackoverflow.com/questions/58150295/how-to-pass-an-object-by-reference-and-value-in-julia – Przemyslaw Szufel Feb 01 '21 at 14:36
  • I think I've seen code written like that. Basically most of my code should just be functions right? At the start will be all the stuff I need to happen in my workflow and then at the end ther will be one function that calls all the stuff above in the order it needs right? And for simulations i geuss I can even have an initial geenration function for my inital conditions so i don't even really need input, like in the example you posted with the empty array that gets filled in the function. Is that correct? – Angus Campbell Feb 01 '21 at 15:41
  • In simulations I keep parameters in `struct`s with default values. Those `struct`s are then passed to functions - eg. see https://www.youtube.com/watch?v=wybG7ha3fdA – Przemyslaw Szufel Feb 01 '21 at 18:24
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/228127/discussion-between-angus-campbell-and-przemyslaw-szufel). – Angus Campbell Feb 01 '21 at 21:47