BVLC/caffe — pull request #250 by kloudkl (2014-03-22 11:47:13+00:00)
Title: Make SyncedMem and Blob capacity aware

Currently, whenever the Blob reshapes, the underlying SyncedMem is deallocated and reallocated. In contrast, std::vector, cv::Mat/GpuMat, thrust::host_vector/device_vector and many other memory container classes are aware of data size and container capacity. The size is the actual number of elements and the capacity is the max number of elements. Changing the size will cause the capacity to increase to be large enough if necessary but never decrease. While changing the capacity does not change the size.

To address the problem of [repeated deallocation and reallocation of memory](https://github.com/BVLC/caffe/issues/119#issuecomment-36268330), this PR delegates the memory management tasks to thrust::host_vector/device_vector to save the trouble of reinvention. When the Blob reshapes and the capacity is not enough, only the extra amount of memory will be allocated. At the same time, memory allocation and reallocation are kept as lazy as possible to avoid wasting memory.