Day 05 Geant4多线程 Multithreading --------以B1为例
Day 05 Geant4多线程 Multithreading --------以B1为例
**fEdep:**用于在不同层级上统计能量进行加和,其中,EventAction中的fEdep是在每个event结束后,每一个step都会把能量沉积加和给runAction中的fEdep;RunAction中把每一个event结束后,把每一个event的fEdep给runAction的fEdep。
如何理解多线程 Multithreading(G4中的多线程是什么样子的):
在exampleB1.cc中将代码变成顺序执行,就是注释掉if …else…endif。
假如有10个gamma粒子要发射,
顺序执行下:
先判断每一个step是不是有效step;然后根据每一个step获得的getStepedEnergy得到有效step的能量沉积;把能量沉积加和给当前eventAction的fEdep;在当前event结束后将eventAction的fEdep加和给runAction的fEdep,这就得到了当前粒子的能量沉积。再继续发射第二个(下一个)粒子。
多线程执行下:
G4可能将10个gamma分成多组,一组几个。对于其中的一个组(线程)来说,就会变成上面的顺序执行,且对于每个线程来讲,有该线程自己的RunAction.fEdep。对于整个项目而言,要对各自线程的runAction的fEdep进行加和
# RunAction.cc文件中
private:G4Accumulable<G4double> fEdep;G4Accumulable<G4double> fEdep2;G4Accumulable<G4double> fEdep3;...
其中fEdep:
# 首先最开始进行初始化,RunAction.cc文件中
fEdep(0.),
fEdep2(0.),
...
# 然后,定义的两个量注册到AccumulableManager上进行自行管理并最终进行merge,初始化放在beginofAction
G4AccumulableManager* accumulableManager = G4AccumulableManager::Instance();
accumulableManager->RegisterAccumulable(fEdep);
accumulableManager->RegisterAccumulable(fEdep2);
...
顺序模式得到的fEdep就是我们需要的;但是使用的是类模板的话,就要使用GetValue()来得到真正的fEdep
# 在此之前要先merge,在endofRunAction中
G4AccumulableManager* accumulableManager = G4AccumulableManager::Instance();
accumulableManager->Merge();G4double edep = fEdep.GetValue();
G4double edep2 = fEdep2.GetValue();
对于G4类编程类应用,需要看多,读多,写多