转载自:http://blog.csdn.net/qq_16055159/article/details/45099547
主要参考:linger
Reshape(const int num, const int channels, const int height, const int width)
功能:改变一个blob的大小
步骤:1.读入num_,channels_,height_,width_的大小
2.计算count_:count_ = num_ * channels_ * height_ * width_;
3.如果count_不为0,则重新为data_和diff_分配一块空间
如果count为0,则都初始化为NULL
输入:num,channels,height,width
输出:无
ReshapeLike(const Blob<Dtype>& other)
功能:为data_和diff_ 重新分配一块空间,大小和另一个blob的一样
输入:Bolb类型的other
输出:无
Blob<Dtype>::Blob(const int num, const int channels, const int height, const int width)
功能:简单的构造函数
输入:num,channels,height,width
set_cpu_data(Dtype* data)
功能:改变CPU的数据
<code class="hljs bash has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">const D<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">type</span>* Blob<D<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">type</span>>::cpu_data()
const D<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">type</span>* Blob<D<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">type</span>>::cpu_diff()
const D<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">type</span>* Blob<D<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">type</span>>::gpu_data()
const D<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">type</span>* Blob<D<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">type</span>>::gpu_diff()</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>
功能:以上四个函数,前两个调用to_cpu(),返回cpu_ptr;第一个对于data对象,第二个对于diff对象
后两个 调用to_gpu(),返回gpu_ptr;第一个对于data对象,第二个对于diff对象
<code class="hljs bash has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">D<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">type</span>* Blob<D<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">type</span>>::mutable_cpu_data()
D<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">type</span>* Blob<D<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">type</span>>::mutable_gpu_data()
D<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">type</span>* Blob<D<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">type</span>>::mutable_cpu_diff()
D<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">type</span>* Blob<D<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">type</span>>::mutable_gpu_diff()</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>
功能:下面是gpu_data和mutable_gpu_data函数的对比,可以看出第二个函数
只是多改变了head_的值。
<code class="hljs cs has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>* SyncedMemory::gpu_data() {
to_gpu();
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>*)gpu_ptr_;
}
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>* SyncedMemory::mutable_gpu_data() {
to_gpu();
head_ = HEAD_AT_GPU;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> gpu_ptr_;
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li></ul>
<code class="hljs cs has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> Blob<Dtype>::ShareData(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> Blob& other)
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> Blob<Dtype>::ShareDiff(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> Blob& other)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>
功能:从other的blob复制data和diff的值
Update()
功能:更新data_的数据,就是减去diff_的数据。
步骤:1.判断blob的位置(HEAD_AT_CPU/HEAD_AT_GPU/SYNCED/UNINITIALIZED)
1)调用caffe_axpy:在math_functions.cpp可以找到该函数的实现,其实这函数也是封装了mkl的函数。这里调用是为了实现了两个向量的减法。
2)调用caffe_gpu_axpy:在math_functions.cpp可以找到该函数的实现,其实这函数也是封装了cublas的函数。这里调用是为了实现了两个向量的减法。
CopyFrom(const Blob& source, bool copy_diff, bool reshape)
功能:从source拷贝数据。copy_diff作为标志来区分是拷贝data还是拷贝diff。
步骤:1.如果是GPU:
如果是拷贝diff:调用cudaMemcpy函数将source的diff拷贝过来
否则拷贝data
2.如果是CPU:
如果是拷贝diff:调用memcpy函数将source的diff拷贝过来
否则拷贝data
FromProto(const BlobProto& proto)
功能:从proto读数据进来,其实就是反序列化
步骤:1.先把blob的大小改变一下
2.得到cpu中数据的地址
3.用proto中的data覆盖blob中的data
4.用proto中的diff覆盖blob中的diff
ToProto(BlobProto* proto, bool write_diff)
功能:把blob数据保存到proto中