Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does Caffe make copies of the data?

Tags:

caffe

 // Assuming that data are on the CPU initially, and we have a blob.
 const Dtype* foo;
 Dtype* bar;
 foo = blob.gpu_data(); // data copied cpu->gpu.
 foo = blob.cpu_data(); // no data copied since both have up-to-date contents.
 bar = blob.mutable_gpu_data(); // no data copied.
 // ... some operations ...
 bar = blob.mutable_gpu_data(); // no data copied when we are still on GPU.
 foo = blob.cpu_data(); // data copied gpu->cpu, since the gpu side has modified the data
foo = blob.gpu_data(); // no data copied since both have up-to-date contents
//1
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

Why do the last two lines copy data? Don't the GPU and CPU have both up-to-date contents?

http://caffe.berkeleyvision.org/tutorial/net_layer_blob.html

like image 461
user678392 Avatar asked Feb 25 '15 03:02

user678392


1 Answers

.gpu_data and .cpu_data are used in cases were the data is used only as input and will not be modified by the algorithm. .mutable_* is used when the data itself gets updated while running the algorithm.

Whenever a the data is called, it checks whether the previous statement was a mutable_* function call and that too using the same processor (gpu or cpu). If it is using the same processor, data need not be copied. If it is using the other processor, there is a chance that the data might have been updated in the previous .mutable_* call and hence a data copy is required.

Edit 1 Whenever the previous instruction is 'mutable', data copy is to be done before the current instruction IF the current instruction is on a different processor.

In no other case the data copy takes place except for a special initial condition, ie; when no data was present at all in the GPU memory and hence a copy of the data will take place before *_gpu_data() call.

like image 193
Anoop K. Prabhu Avatar answered Sep 22 '22 17:09

Anoop K. Prabhu