Astro 585 discussion 4


10 February 2014 astro585

Basic optimization techniques for serial code

Ideas for simple optimizations
  • Don't call your functions too often

  • Pass pointers rather than by reference

  • Know what your computer is doing - what is going on under the hood?

  • Providing types at compile-time

  • If using an interpreted language: Vectorize!

  • Reduce branching

Loops

Compilers can be smart, and can speed up one-dimensional loops. They have a harder time to figure out of how to make nested loops faster.

Difference between unit tests and regression tests

Unit tests

A test that makes sure that one function in your code (or one small, and very well defined part of your code) works properly.

Regression tests

Bigger scale tests, that usually combine many functions, and to check if they are working correctly together. For example: Calculating the Planck spectrum and then write the results to an external ASCII file.

Using _Try-Catch

Its a way to see if the program says: "Hey! There is a problem here!" - then if you don't have a Try-Catch clause your program will crash. An IF statement can also work if you know how it can fail. However, the advantage of Try-Catch is that you can catch some errors that are buried deep down maybe. Still, its a much more expensive way to deal with a problem, rather than using an IF statement. Its for things that don't have a clear order. It doesn't really happen too often in scientific programming. Good places to use would be in I/O, and networks.

Q. Should we optimize serial code first, before we parallelize it?

It kind of depends on your problem. They key is to think about: "How are you gonna parallelize it?" It can be embarrassingly parallel , and you can then basically distribute the serial code to a lot of workers and gain speedups that way. If not, then you can also go into the idea of parallelizing at multiple levels.


Ergonomically handwritten code, with a little help from my friends; vim, jekyll and twitter bootstrap