Code three definitions
class P2PSync : public GPUParams<Dtype>, public Solver<Dtype>::Callback, InternalThread {
class BasePrefetchingDataLayer : publicpublic InternalThread {
publicpublic: forfor parallelism virtualboolShareInParallelconstreturnfalseprotected: protected: is created per source classpublic InternalThread {
Start thread
src/caffe/parallel.cpp: syncs[i]->StartInternalThread();
src/caffe/layers/base_data_layer.cpp: StartInternalThread();
src/caffe/data_reader.cpp: StartInternalThread();
Specific analysis
With 4 gpu:gpu0, GPU1, GPU2, GPU3
Run Root_solver thread here called the main thread (GPU0),
After the main thread enters sync.run(gpus)
this function,
for (int1; i < syncs.size(); ++i) { //主线程生成了3个新线程(boost thread): bt1,bt2,bt3. //GPU1,2,3 //这三个线程就会先分别初始化自己的solver->net->layer, //然后前传后传啥的 syncs[i]->StartInternalThread(); } // Run root solver on current thread //主线程() //已经初始化完了solver->net->layer,准备开始前传后传了 solver_->Solve();
Imagedatalayer
//1.继承layer->BaseDataLayerBaseDataLayer, InternalThread->BasePrefetchingDataLayer->ImageLayer//2.-> true
When the above 4 threads initialize the Imagelayer:
The main thread is called in a BasePrefetchingDataLayer
function that layersetup
StartInternalThread
generates a new thread of prefetch (Boost thread):bt_data1
Because of the Imagelayer shareinparallel -> true
, so the remaining three boost thread (bt1,bt2,bt3) will not have the necessary to Imagelayer layersetup
.
So it can be said that there is only one imagelayer
DataLayer
class DataLayer: public baseprefetchingdatalayer<dtype> {public ://DataLayer uses DataReader instead for sharing for parallelism Span class= "Hljs-keyword" >virtual inline bool Shareinparallel () const {return false ; } protected : DataReader reader_;}; Class DataReader {protected ://A single body is Created per source class Body: public Internalthread {
Unlike Imagelayer, DataLayer ShareInParallel
is false
,
So,4 threads will have their own datalayer (total of 4)
However, the body (4 threads public), Reader_ (main thread private), when the main thread initializes its datalayer
That is, the main thread after the new body, the remaining three threads just call this body is good, no more new.
And this body corresponds to a boost thread: bt_data2
Summary
Both Imagelayer and datalayer ensure that only one thread is reading the data.
[Caffe]thread