GalSim-developers/GalSim — pull request #416 by rmjarvis (2013-05-10 21:33:56+00:00)
Title: #340 Speed up HSM code

This PR is for some speed improvements to the HSM code. I concentrated on the unit tests in test_hsm.py to find where the slow parts were.

The main improvements came from abandoning the Numerical-Recipes-inspired fourier transform function and switching to FFTW. As usual, NR code is incredibly slow compared to state of the art alternatives.

There are some other improvements as well, such as turning some loops into a matrix product calculation, which means it is able to leverage the speed of so-called "level 3" BLAS functions rather than direct loops.

I also precalculate the masked image, rather than doing `if mask(x,y)` in the innermost loop. This should make it easier to implement #341, but I didn't do that here. My goal here was merely to speed up the existing functionality, not add any new functionality.

The upshot is that the unit tests in test_hsm.py went from taking about 21 seconds on my laptop down to under 3 seconds.

I kept some of the old code around blocked off by
```
#if 1
< new code >
#else
< old code >
#endif
```
blocks. But if Rachel signs off on the new code, we can probably remove those.

There is one section that I'm not super happy with, which is `fast_convolve_image_1`. I set up the image offsets to produce the same output that the old code did, but they seem kind of screwy. I suspect there are better choices for these that would put all the images in the center of the XTable (which is then fourier transformed) rather than kind of off center.

But I think this will screw up some of the unit tests against pre-computed values, so we'd have to be careful not to mess up the functionality and be sure that the new computed values are really right. I kept getting myself confused when I was trying this originally, so once I got results that passed the unit tests, I stopped messing with it.