cpu_data()和mutable_cpu_data() 区别:
mutable_cpu_data()多了一句head_ = HEAD_AT_CPU;是因为它返回的cpu_ptr_是可以被修改的,不像cpu_data()中返回(const void*)cpu_ptr_不可修改,这样,在下一次cpu或者gpu数据同步的时候,会知道这里的数据可能被修改过。
const void* SyncedMemory::cpu_data() {
check_device();
to_cpu();
return (const void*)cpu_ptr_;
}
void* SyncedMemory::mutable_cpu_data() {
check_device();
to_cpu();
head_ = HEAD_AT_CPU;
return cpu_ptr_;
}
SyncedHead
hand_ 数据状态,有四种状态:
UNINITIALIZED(未初始化), HEAD_AT_CPU(最新数据在内存中), HEAD_AT_GPU(最新数据在内存中), SYNCED(内存显存数据已同步)
to_cpu()用来将显存数据同步到内存,to_gpu()用来将内存数据同步到显存
内存显存数据不同步时,并不会立马调用to_cpu()或者to_gpu()使数据同步,只改变数据状态
hand_。只有在获取数据的时候才会先去同步数据,再返回数据。
状态转换图如下:最先在这里看到这个图片:https://blog.csdn.net/u010414386/article/details/52346192(作者写的很好啊,可惜后来没有更了)
![](https://i-blog.csdnimg.cn/blog_migrate/3637b0ffe524b893a7a50be58540705a.png)
// Assuming that data are on the CPU initially, and we have a blob.
//首先假设初始化时数据在cpu中(内存),此时状态为HEAD_AT_CPU
const Dtype* foo;
Dtype* bar;
foo = blob.gpu_data(); // data copied cpu->gpu.
const Dtype* foo;
Dtype* bar;
foo = blob.gpu_data(); // data copied cpu->gpu.
//这时调用to_gpu()会将数据复制至gpu,因为之前状态是状态HEAD_AT_CPU,此时变为SYNCED
foo = blob.cpu_data(); // no data copied since both have up-to-date contents.
foo = blob.cpu_data(); // no data copied since both have up-to-date contents.
//这时调用to_cpu()因为之前状态是SYNCED,所以不会复制数据,状态不变
bar = blob.mutable_gpu_data(); // no data copied.
bar = blob.mutable_gpu_data(); // no data copied.
//这时调用to_gpu()因为之前状态是SYNCED,所以不会复制数据,但状态变为HEAD_AT_GPU
// ... some operations ...
bar = blob.mutable_gpu_data(); // no data copied when we are still on GPU.
// ... some operations ...
bar = blob.mutable_gpu_data(); // no data copied when we are still on GPU.
//这时调用to_gpu()因为之前状态是HEAD_AT_GPU,所以不会复制数据,但状态仍为HEAD_AT_GPU
foo = blob.cpu_data(); // data copied gpu->cpu, since the gpu side has modified the data
foo = blob.cpu_data(); // data copied gpu->cpu, since the gpu side has modified the data
//这时调用to_cpu()因为之前状态是HEAD_AT_GPU,所以会复制数据,状态改为SYNCED
foo = blob.gpu_data(); // no data copied since both have up-to-date contents
foo = blob.gpu_data(); // no data copied since both have up-to-date contents
//这就跟前面类似了
bar = blob.mutable_cpu_data(); // still no data copied.
bar = blob.mutable_gpu_data(); // data copied cpu->gpu.
bar = blob.mutable_cpu_data(); // data copied gpu->cpu.
bar = blob.mutable_cpu_data(); // still no data copied.
bar = blob.mutable_gpu_data(); // data copied cpu->gpu.
bar = blob.mutable_cpu_data(); // data copied gpu->cpu.