FlexMonkey/SwiftCFD — pull request #1 by josephlord (2014-09-09 00:00:20+00:00)
Title: Optimisation experiment1 CFD Solve 0.025 (was about 0.45)

Aproximately 18 time speedup. Largely by removing the `.0` on the `let i1 = i0 + 1` lines in both advect functions.

Generally I don't really like the code structure and there is more improvement possible. If nothing else there are places the Accelerate framework could be used here.

Something went wrong when I went through trying to fix up the coordinate systems for the correct getIndex with border so there is a trail off to the bottom right corner.

LARGELY FIXED
Overuse of shared global state. Now there is a FluidDynamicSolver that owns the data. I would in principle prefer a class as it a shared thing to keep a reference to. Also only the main fluidDynamicsStep accesses the data directly and it passes arguments to all the functions that require it (which are now global functions and could be private). Part of the reason for making them global was to get the compiler's help to indicate which variables they accessed and needed to be passed as arguments now. Each function can be tested separately with its own test data this way too.

Swaps have been optimised out. With array structs these would be full array copies not reference/pointer copies as in the paper. Wrap in a class if you want to be able to use as references. I've switched to pure functions passing arguments and getting return values. In places optimising with inout args may be worthwhile. I did it in the GrayScott thing to
PARTIALLY FIXED
Coordinate system (one pixel boundary stuff).

NOT FIXED

1) ViewController updates d in non-threadsafe manner. You need to create a custom serial queue and ensure all calls that mutate an object occur on the same thread. That includes solver steps and UI modifications to the data. This is why touch data is only intermittently received. This is the same bug the opposite way round as in the GrayScottRenderer when you couldn't update the image reliably.

2) Drift of the image - something went wrong in the Coordinate fixup.

3) The resolve dispatch closures capture the ViewController. Weakself dance or similar move required.

4) setBoundary is basically unimplemented.

5) dOld/uOld/vOld were basically unused/thrownaway. I have optimised by not retaining them between calls matching previous behaviour. I'm not sure if this actually matches the algorithm in the paper but I think in this regard it does match the behaviour of your version.