BVLC/caffe — pull request #318 by jeffdonahue (2014-04-12 02:34:42+00:00)
Title: add Adopt{Data,Diff} methods to blobs to enable "virtual" copies

This PR adds methods `AdoptData` and `AdoptDiff` so that explicit O(N) copies by value can be replaced with O(1) copies by reference using the `=` operator on `shared_ptr`s which hold the blob data.

This makes `FlattenLayer`s essentially free and `SplitLayer`s substantially cheaper -- only needing to do an `axpy` in the backward pass (which is mathematically necessary).

On my machine, this speeds up `FlattenLayerTest` from ~20s to ~12s*, with the vast majority of the speedup due to TestGPUGradient since FlattenLayer no longer actually does any GPU computation. The `SplitLayerTest` timing does not change much because the copies in the unit test are so small to begin with, but when I do the test on a somewhat realistic input size (change `test_split_layer` line 27 to `blob_bottom_(new Blob(1000, 3, 200, 200))` instead of `(2, 3, 6, 5)`) and run using `./build/test/test_split_layer.testbin --gtest_filter="-*Gradient*"` (suppresses gradient checking tests because they become absurdly slow with this blob size), I see a speedup from ~176s to ~171s. Not huge, but basically a free improvement.

*These unit tests (which I wrote) are way too slow even with this improvement, but that's a problem to address in another PR...