Import Geant4 10.7.0.beta source tree
This commit is contained in:
@@ -30,10 +30,8 @@
|
||||
#include "B2bDetectorConstruction.hh"
|
||||
#include "B2ActionInitialization.hh"
|
||||
|
||||
//G4-TBB interfaces
|
||||
#include "tbbUserWorkerInitialization.hh"
|
||||
#include "SimpleTbbMasterRunManager.hh"
|
||||
#include "G4Threading.hh"
|
||||
#include "G4TaskRunManager.hh"
|
||||
|
||||
#include "G4UImanager.hh"
|
||||
#include "FTFP_BERT.hh"
|
||||
@@ -44,63 +42,26 @@
|
||||
#include "G4VisExecutive.hh"
|
||||
#include "G4UIExecutive.hh"
|
||||
|
||||
//TBB includes
|
||||
#include <tbb/task_scheduler_init.h>
|
||||
#include <tbb/task.h>
|
||||
|
||||
|
||||
//This function is very simple: it just start tbb work.
|
||||
//This is done in a seperate thread, because for G4 the
|
||||
//master cannot live in the same thread where workers are
|
||||
//Starting tbb work in a thread guarantees that no workers
|
||||
//are created where the master lives (the main thread)
|
||||
//Clearly a separate solution is to create and configure master
|
||||
//in a separate thread. But this is much simpler.
|
||||
G4ThreadFunReturnType startWork(G4ThreadFunArgType arg)
|
||||
{
|
||||
tbb::task_list* tasks = static_cast<tbb::task_list*>(arg);
|
||||
//We assume at least one /run/beamOn was executed, thus the tasklist is now filled,
|
||||
//lets start TBB
|
||||
try {
|
||||
std::cout<<"Now calling 'tbb::task::spawn_work_and_wait' "<<std::endl;
|
||||
tbb::task::spawn_root_and_wait( *tasks );
|
||||
} catch(std::exception& e) {
|
||||
std::cerr<<"Error occurred. Error info is:\""<<e.what()<<"\""<<std::endl;
|
||||
}
|
||||
return static_cast<G4ThreadFunReturnType>(0);
|
||||
}
|
||||
|
||||
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
|
||||
|
||||
int main(int argc,char** argv)
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
// Instantiate G4UIExecutive if there are no arguments (interactive mode)
|
||||
G4UIExecutive* ui = 0;
|
||||
if ( argc == 1 ) {
|
||||
if(argc == 1)
|
||||
{
|
||||
ui = new G4UIExecutive(argc, argv);
|
||||
}
|
||||
|
||||
// Choose the Random engine
|
||||
|
||||
G4Random::setTheEngine(new CLHEP::RanecuEngine);
|
||||
|
||||
unsigned int numCoresAvailable= G4Threading::G4GetNumberOfCores();
|
||||
unsigned int numberOfCoresToUse= (numCoresAvailable > 1 ) ? 2 : 1 ;
|
||||
//=== TBB engine initialization
|
||||
tbb::task_scheduler_init init( numberOfCoresToUse );
|
||||
tbb::task_list tasks;
|
||||
|
||||
SimpleTbbMasterRunManager* runManager = new SimpleTbbMasterRunManager;
|
||||
|
||||
//Set TBB specific data to run-manager, 1 event per tbb::task (e.g. Nevents == N tasks)
|
||||
//Note that a /run/beamOn command will just create tasks and add them to tasks
|
||||
runManager->SetNumberEventsPerTask(1); //Not needed since 1 is however default
|
||||
runManager->SetTaskList(&tasks);
|
||||
//Set user-initialization that specify threading model, in this case TBB.
|
||||
//This overwrites default that uses pthreads
|
||||
runManager->SetUserInitialization(new tbbUserWorkerInitialization );
|
||||
|
||||
//==== Geant4 specific stuff, from now up to END-G4 comment is copy from MT example
|
||||
|
||||
auto runManager =
|
||||
G4RunManagerFactory::CreateRunManager(G4RunManagerType::TBB);
|
||||
|
||||
//==== Geant4 specific stuff, from now up to END-G4 comment is copy from MT
|
||||
// example
|
||||
// Set mandatory initialization classes
|
||||
|
||||
runManager->SetUserInitialization(new B2bDetectorConstruction());
|
||||
@@ -108,48 +69,37 @@ int main(int argc,char** argv)
|
||||
G4VModularPhysicsList* physicsList = new FTFP_BERT;
|
||||
physicsList->RegisterPhysics(new G4StepLimiterPhysics());
|
||||
runManager->SetUserInitialization(physicsList);
|
||||
|
||||
|
||||
// Set user action classes
|
||||
|
||||
runManager->SetUserInitialization(new B2ActionInitialization());
|
||||
|
||||
|
||||
// Initialize G4 kernel
|
||||
|
||||
runManager->Initialize();
|
||||
|
||||
|
||||
// Get the pointer to the User Interface manager
|
||||
G4UImanager* UImanager = G4UImanager::GetUIpointer();
|
||||
|
||||
if (!ui) // batch mode
|
||||
{
|
||||
G4String command = "/control/execute ";
|
||||
G4String fileName = argv[1];
|
||||
UImanager->ApplyCommand(command+fileName);
|
||||
}
|
||||
if(!ui) // batch mode
|
||||
{
|
||||
G4String command = "/control/execute ";
|
||||
G4String fileName = argv[1];
|
||||
UImanager->ApplyCommand(command + fileName);
|
||||
}
|
||||
else
|
||||
{ // interactive mode : define UI session
|
||||
#if 1
|
||||
G4int nEvents= 50;
|
||||
runManager->BeamOn(nEvents);
|
||||
#else
|
||||
UImanager->ApplyCommand("/control/execute init.mac");
|
||||
if (ui->IsGUI())
|
||||
UImanager->ApplyCommand("/control/execute gui.mac");
|
||||
ui->SessionStart();
|
||||
delete ui;
|
||||
#endif
|
||||
}
|
||||
//END-G4
|
||||
G4Thread* aThread = new G4Thread;
|
||||
G4THREADCREATE(aThread, startWork, static_cast<G4ThreadFunArgType>(&tasks));
|
||||
|
||||
//Wait for work to be finised
|
||||
if(aThread)
|
||||
aThread->join();
|
||||
|
||||
delete runManager;
|
||||
{
|
||||
// interactive mode : define UI session
|
||||
UImanager->ApplyCommand("/control/execute init.mac");
|
||||
if(ui->IsGUI())
|
||||
UImanager->ApplyCommand("/control/execute gui.mac");
|
||||
ui->SessionStart();
|
||||
delete ui;
|
||||
}
|
||||
|
||||
return 0;
|
||||
delete runManager;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.....
|
||||
|
||||
@@ -1,83 +0,0 @@
|
||||
//
|
||||
// ********************************************************************
|
||||
// * License and Disclaimer *
|
||||
// * *
|
||||
// * The Geant4 software is copyright of the Copyright Holders of *
|
||||
// * the Geant4 Collaboration. It is provided under the terms and *
|
||||
// * conditions of the Geant4 Software License, included in the file *
|
||||
// * LICENSE and available at http://cern.ch/geant4/license . These *
|
||||
// * include a list of copyright holders. *
|
||||
// * *
|
||||
// * Neither the authors of this software system, nor their employing *
|
||||
// * institutes,nor the agencies providing financial support for this *
|
||||
// * work make any representation or warranty, express or implied, *
|
||||
// * regarding this software system or assume any liability for its *
|
||||
// * use. Please see the license in the file LICENSE and URL above *
|
||||
// * for the full disclaimer and the limitation of liability. *
|
||||
// * *
|
||||
// * This code implementation is the result of the scientific and *
|
||||
// * technical work of the GEANT4 collaboration. *
|
||||
// * By using, copying, modifying or distributing the software (or *
|
||||
// * any work based on the software) you agree to acknowledge its *
|
||||
// * use in resulting scientific publications, and indicate your *
|
||||
// * acceptance of all terms of the Geant4 Software license. *
|
||||
// ********************************************************************
|
||||
//
|
||||
//
|
||||
// Class description:
|
||||
//
|
||||
// This class implements the master model run manager for TBB bases
|
||||
// application.
|
||||
// It is instantiated by user main (or equivalent function) instead
|
||||
// of G4[MT]RunManager. It controls the creation of tbb::tasks.
|
||||
// See G4MTRunManager for documentation of methods relative to base
|
||||
// class. Only class specific methods are documented here.
|
||||
//
|
||||
// Equivalent in traditional MT:
|
||||
// G4MTRunManager
|
||||
//
|
||||
// History:
|
||||
// Nov 4th, 2013 A. Dotti - First Implementation
|
||||
|
||||
#ifndef SIMPLETBBMASTERRUNMANAGER_HH
|
||||
#define SIMPLETBBMASTERRUNMANAGER_HH
|
||||
|
||||
#include "G4MTRunManager.hh"
|
||||
#include <tbb/task.h>
|
||||
|
||||
class SimpleTbbMasterRunManager : public G4MTRunManager {
|
||||
public:
|
||||
SimpleTbbMasterRunManager(); // G4RunManagerKernel* existingRMK=0);
|
||||
// Default constructor,
|
||||
// tasklist is the tbb::task_list to which the created tasks will be added.
|
||||
// nEvents is the number of events for which each tbb::task is responsible
|
||||
//
|
||||
virtual ~SimpleTbbMasterRunManager();
|
||||
virtual void RunTermination();
|
||||
void SetTaskList( tbb::task_list* tl ) { theTasks = tl; }
|
||||
//Set a reference to the output task list where new tasks will
|
||||
//be added to
|
||||
void SetNumberEventsPerTask( G4int nt ) { nEvtsPerTask = nt; }
|
||||
//Specify number of events that each simulation task is responsible
|
||||
//for
|
||||
protected:
|
||||
virtual void CreateAndStartWorkers();
|
||||
virtual void TerminateWorkers();
|
||||
virtual void CreateTask(G4int id,G4int evts);
|
||||
//Creates a concrete tbb::task with index id
|
||||
//responsible for evts events
|
||||
protected:
|
||||
//Barriers mechanism for TBB is non existing
|
||||
virtual void WaitForReadyWorkers() {}
|
||||
virtual void WaitForEndEventLoopWorkers() {}
|
||||
virtual void ThisWorkerReady() {}
|
||||
virtual void ThisWorkerEndEventLoop() {}
|
||||
virtual WorkerActionRequest ThisWorkerWaitForNextAction()
|
||||
{ return WorkerActionRequest::UNDEFINED; }
|
||||
virtual void NewActionRequest( WorkerActionRequest /*newRequest*/ ) {}
|
||||
private:
|
||||
tbb::task_list* theTasks;
|
||||
G4int nEvtsPerTask;
|
||||
};
|
||||
|
||||
#endif //TBBMASTERRUNMANAGER_HH
|
||||
@@ -1,49 +0,0 @@
|
||||
//
|
||||
// ********************************************************************
|
||||
// * License and Disclaimer *
|
||||
// * *
|
||||
// * The Geant4 software is copyright of the Copyright Holders of *
|
||||
// * the Geant4 Collaboration. It is provided under the terms and *
|
||||
// * conditions of the Geant4 Software License, included in the file *
|
||||
// * LICENSE and available at http://cern.ch/geant4/license . These *
|
||||
// * include a list of copyright holders. *
|
||||
// * *
|
||||
// * Neither the authors of this software system, nor their employing *
|
||||
// * institutes,nor the agencies providing financial support for this *
|
||||
// * work make any representation or warranty, express or implied, *
|
||||
// * regarding this software system or assume any liability for its *
|
||||
// * use. Please see the license in the file LICENSE and URL above *
|
||||
// * for the full disclaimer and the limitation of liability. *
|
||||
// * *
|
||||
// * This code implementation is the result of the scientific and *
|
||||
// * technical work of the GEANT4 collaboration. *
|
||||
// * By using, copying, modifying or distributing the software (or *
|
||||
// * any work based on the software) you agree to acknowledge its *
|
||||
// * use in resulting scientific publications, and indicate your *
|
||||
// * acceptance of all terms of the Geant4 Software license. *
|
||||
// ********************************************************************
|
||||
//
|
||||
#ifndef TBBTASK_HH
|
||||
#define TBBTASK_HH
|
||||
|
||||
#include <tbb/task.h>
|
||||
#include <tbb/concurrent_queue.h>
|
||||
#include "G4Types.hh"
|
||||
|
||||
class G4Run;
|
||||
|
||||
class tbbTask : public tbb::task {
|
||||
public:
|
||||
tbbTask(G4int anId, tbb::concurrent_queue<const G4Run*>* output=0 , G4int nEvts = 1 );
|
||||
virtual ~tbbTask();
|
||||
tbb::task* execute();
|
||||
|
||||
unsigned int GetSlotId(); //
|
||||
private:
|
||||
G4int m_nEvents;
|
||||
G4int m_taskID;
|
||||
tbb::concurrent_queue<const G4Run*>* m_output;
|
||||
G4bool m_beamOnCondition;
|
||||
};
|
||||
|
||||
#endif //TBBTASK_HH
|
||||
@@ -1,51 +0,0 @@
|
||||
//
|
||||
// ********************************************************************
|
||||
// * License and Disclaimer *
|
||||
// * *
|
||||
// * The Geant4 software is copyright of the Copyright Holders of *
|
||||
// * the Geant4 Collaboration. It is provided under the terms and *
|
||||
// * conditions of the Geant4 Software License, included in the file *
|
||||
// * LICENSE and available at http://cern.ch/geant4/license . These *
|
||||
// * include a list of copyright holders. *
|
||||
// * *
|
||||
// * Neither the authors of this software system, nor their employing *
|
||||
// * institutes,nor the agencies providing financial support for this *
|
||||
// * work make any representation or warranty, express or implied, *
|
||||
// * regarding this software system or assume any liability for its *
|
||||
// * use. Please see the license in the file LICENSE and URL above *
|
||||
// * for the full disclaimer and the limitation of liability. *
|
||||
// * *
|
||||
// * This code implementation is the result of the scientific and *
|
||||
// * technical work of the GEANT4 collaboration. *
|
||||
// * By using, copying, modifying or distributing the software (or *
|
||||
// * any work based on the software) you agree to acknowledge its *
|
||||
// * use in resulting scientific publications, and indicate your *
|
||||
// * acceptance of all terms of the Geant4 Software license. *
|
||||
// ********************************************************************
|
||||
//
|
||||
// Class Descrition:
|
||||
// This class implements TBB specific worker initialization.
|
||||
// It is a sub-class of G4UserWorkerThreadInitialization.
|
||||
// Its role is to instantiate a tbbWorkerRunManager to be used by
|
||||
// tbb tasks.
|
||||
//
|
||||
// Equivalent in traditional MT:
|
||||
// G4UserWorkerThreadInitialization
|
||||
//
|
||||
// History:
|
||||
// Oct 31st, 2013 A. Dotti - First Implementation
|
||||
|
||||
#ifndef TBBUSERWORKERINITIALIZATION_HH
|
||||
#define TBBUSERWORKERINITIALIZATION_HH
|
||||
|
||||
#include "G4UserWorkerThreadInitialization.hh"
|
||||
|
||||
class tbbUserWorkerInitialization : public G4UserWorkerThreadInitialization {
|
||||
public:
|
||||
virtual ~tbbUserWorkerInitialization();
|
||||
virtual G4WorkerRunManager* CreateWorkerRunManager() const;
|
||||
virtual void JoinWorker(G4Thread*);
|
||||
virtual G4Thread* CreateAndStartWorker(G4WorkerThread*);
|
||||
};
|
||||
|
||||
#endif //TBBUSERWORKERINITIALIZATION_HH
|
||||
@@ -1,55 +0,0 @@
|
||||
//
|
||||
// ********************************************************************
|
||||
// * License and Disclaimer *
|
||||
// * *
|
||||
// * The Geant4 software is copyright of the Copyright Holders of *
|
||||
// * the Geant4 Collaboration. It is provided under the terms and *
|
||||
// * conditions of the Geant4 Software License, included in the file *
|
||||
// * LICENSE and available at http://cern.ch/geant4/license . These *
|
||||
// * include a list of copyright holders. *
|
||||
// * *
|
||||
// * Neither the authors of this software system, nor their employing *
|
||||
// * institutes,nor the agencies providing financial support for this *
|
||||
// * work make any representation or warranty, express or implied, *
|
||||
// * regarding this software system or assume any liability for its *
|
||||
// * use. Please see the license in the file LICENSE and URL above *
|
||||
// * for the full disclaimer and the limitation of liability. *
|
||||
// * *
|
||||
// * This code implementation is the result of the scientific and *
|
||||
// * technical work of the GEANT4 collaboration. *
|
||||
// * By using, copying, modifying or distributing the software (or *
|
||||
// * any work based on the software) you agree to acknowledge its *
|
||||
// * use in resulting scientific publications, and indicate your *
|
||||
// * acceptance of all terms of the Geant4 Software license. *
|
||||
// ********************************************************************
|
||||
//
|
||||
//
|
||||
// Class description:
|
||||
//
|
||||
// This class implements the worker model run manager for TBB based
|
||||
// application.
|
||||
// It is instantiated by tbbUserWorkerInitialization and used by
|
||||
// SimpleTbbMasterRunManager.
|
||||
// See G4WorkerRunManager for documentation of methods relative to
|
||||
// base class. Only class specific methods are documented here.
|
||||
//
|
||||
// Equivalent in traditional MT:
|
||||
// G4WorkerRunManager
|
||||
//
|
||||
// History:
|
||||
// Oct 31st, 2013 A. Dotti - First Implementation
|
||||
// May 12th, 2014 J. Apostolakis - Additional methods/functionality
|
||||
|
||||
#ifndef TBBWORKERRUNMANAGER_HH
|
||||
#define TBBWORKERRUNMANAGER_HH
|
||||
|
||||
#include "G4WorkerRunManager.hh"
|
||||
|
||||
class tbbWorkerRunManager : public G4WorkerRunManager {
|
||||
public:
|
||||
tbbWorkerRunManager();
|
||||
virtual ~tbbWorkerRunManager();
|
||||
virtual void ConstructScoringWorlds();
|
||||
virtual void MergePartialResults();
|
||||
};
|
||||
#endif
|
||||
@@ -1,86 +0,0 @@
|
||||
//
|
||||
// ********************************************************************
|
||||
// * License and Disclaimer *
|
||||
// * *
|
||||
// * The Geant4 software is copyright of the Copyright Holders of *
|
||||
// * the Geant4 Collaboration. It is provided under the terms and *
|
||||
// * conditions of the Geant4 Software License, included in the file *
|
||||
// * LICENSE and available at http://cern.ch/geant4/license . These *
|
||||
// * include a list of copyright holders. *
|
||||
// * *
|
||||
// * Neither the authors of this software system, nor their employing *
|
||||
// * institutes,nor the agencies providing financial support for this *
|
||||
// * work make any representation or warranty, express or implied, *
|
||||
// * regarding this software system or assume any liability for its *
|
||||
// * use. Please see the license in the file LICENSE and URL above *
|
||||
// * for the full disclaimer and the limitation of liability. *
|
||||
// * *
|
||||
// * This code implementation is the result of the scientific and *
|
||||
// * technical work of the GEANT4 collaboration. *
|
||||
// * By using, copying, modifying or distributing the software (or *
|
||||
// * any work based on the software) you agree to acknowledge its *
|
||||
// * use in resulting scientific publications, and indicate your *
|
||||
// * acceptance of all terms of the Geant4 Software license. *
|
||||
// ********************************************************************
|
||||
//
|
||||
//
|
||||
// Class description:
|
||||
//
|
||||
// This class implements the worker model run manager for TBB based
|
||||
// application.
|
||||
// It is instantiated by tbbUserWorkerInitialization and used by
|
||||
// SimpleTbbMasterRunManager.
|
||||
// See G4WorkerRunManager for documentation of methods relative to
|
||||
// base class. Only class specific methods are documented here.
|
||||
//
|
||||
// Equivalent in traditional MT:
|
||||
// G4WorkerRunManager
|
||||
//
|
||||
// History:
|
||||
// Oct 31st, 2013 A. Dotti - First Implementation
|
||||
|
||||
#include "SimpleTbbMasterRunManager.hh"
|
||||
#include "tbbTask.hh"
|
||||
|
||||
SimpleTbbMasterRunManager::SimpleTbbMasterRunManager() :
|
||||
G4MTRunManager(),
|
||||
theTasks(static_cast<tbb::task_list*>(nullptr)),
|
||||
nEvtsPerTask(1)
|
||||
{
|
||||
}
|
||||
|
||||
SimpleTbbMasterRunManager::~SimpleTbbMasterRunManager()
|
||||
{
|
||||
}
|
||||
|
||||
void SimpleTbbMasterRunManager::TerminateWorkers()
|
||||
{
|
||||
//For TBB based example this should be empty
|
||||
}
|
||||
|
||||
void SimpleTbbMasterRunManager::CreateAndStartWorkers()
|
||||
{
|
||||
//Instead of pthread based workers, create tbbTask
|
||||
G4int ntasks = numberOfEventToBeProcessed/nEvtsPerTask;
|
||||
G4int remn = numberOfEventToBeProcessed % nEvtsPerTask;
|
||||
for ( G4int nt = 0 ; nt < ntasks ; ++nt )
|
||||
{
|
||||
G4int evts= nEvtsPerTask;
|
||||
if ( nt == ntasks - 1 ) evts+=remn;
|
||||
CreateTask(nt,evts);
|
||||
}
|
||||
}
|
||||
|
||||
void SimpleTbbMasterRunManager::CreateTask(G4int id,G4int evts)
|
||||
{
|
||||
tbbTask& task = * new(tbb::task::allocate_root())
|
||||
tbbTask( id , nullptr , evts ); //Add output for merging
|
||||
theTasks->push_back( task );
|
||||
}
|
||||
|
||||
void SimpleTbbMasterRunManager::RunTermination()
|
||||
{
|
||||
// Reduce results ....
|
||||
G4MTRunManager::RunTermination();
|
||||
}
|
||||
|
||||
@@ -1,233 +0,0 @@
|
||||
//
|
||||
// ********************************************************************
|
||||
// * License and Disclaimer *
|
||||
// * *
|
||||
// * The Geant4 software is copyright of the Copyright Holders of *
|
||||
// * the Geant4 Collaboration. It is provided under the terms and *
|
||||
// * conditions of the Geant4 Software License, included in the file *
|
||||
// * LICENSE and available at http://cern.ch/geant4/license . These *
|
||||
// * include a list of copyright holders. *
|
||||
// * *
|
||||
// * Neither the authors of this software system, nor their employing *
|
||||
// * institutes,nor the agencies providing financial support for this *
|
||||
// * work make any representation or warranty, express or implied, *
|
||||
// * regarding this software system or assume any liability for its *
|
||||
// * use. Please see the license in the file LICENSE and URL above *
|
||||
// * for the full disclaimer and the limitation of liability. *
|
||||
// * *
|
||||
// * This code implementation is the result of the scientific and *
|
||||
// * technical work of the GEANT4 collaboration. *
|
||||
// * By using, copying, modifying or distributing the software (or *
|
||||
// * any work based on the software) you agree to acknowledge its *
|
||||
// * use in resulting scientific publications, and indicate your *
|
||||
// * acceptance of all terms of the Geant4 Software license. *
|
||||
// ********************************************************************
|
||||
//
|
||||
#include "tbbTask.hh"
|
||||
#include "tbbWorkerRunManager.hh"
|
||||
#include "G4Threading.hh"
|
||||
#include "G4String.hh"
|
||||
#include "G4WorkerRunManager.hh"
|
||||
#include "G4MTRunManager.hh"
|
||||
#include "G4UImanager.hh"
|
||||
#include "G4UserWorkerThreadInitialization.hh"
|
||||
#include "G4WorkerThread.hh"
|
||||
#include "G4MTRunManagerKernel.hh"
|
||||
#include "G4AutoLock.hh"
|
||||
#include "G4UserWorkerInitialization.hh"
|
||||
#include "G4VUserActionInitialization.hh"
|
||||
|
||||
//Equivalent to G4MTRunManagerKernel::StartThread
|
||||
|
||||
tbbTask::tbbTask(G4int anId,
|
||||
tbb::concurrent_queue<const G4Run*>* out,
|
||||
G4int nevts)
|
||||
: m_nEvents(nevts),
|
||||
m_taskID(anId),
|
||||
m_output(out),
|
||||
m_beamOnCondition(false)
|
||||
{
|
||||
}
|
||||
|
||||
tbbTask::~tbbTask()
|
||||
{
|
||||
}
|
||||
|
||||
#include <tbb/atomic.h>
|
||||
namespace {
|
||||
tbb::atomic<int> counter;
|
||||
}
|
||||
|
||||
tbb::task* tbbTask::execute()
|
||||
{
|
||||
// In tbb we do not have anymore the concept of thread:
|
||||
// tasks run "somewhere"; but there is no control over where.
|
||||
// This somewhere is a thread, created, controlled and 'switched'
|
||||
// to our task by the TBB runtime system.
|
||||
//
|
||||
// The "pedantic" way to proceed is: recreate the "context"
|
||||
// from scratch every time (context=local run manager,
|
||||
// geometry, etc)
|
||||
// This would be a clear waste of resources: there is no
|
||||
// need to initialize multiple times, when the same thread
|
||||
// is used several times, ie for several tasks.
|
||||
|
||||
// In the current version to avoid this we initialize only
|
||||
// i) the first time we run on each thread, and
|
||||
// ii) at the start of a new run on each thread (reinitialization).
|
||||
// We are confident that this works because TBB co-works with TLS.
|
||||
// In addition this ensures that minimal changes needed to G4 code base.
|
||||
//
|
||||
// Note 1: that this "thread" is responsible for 1 or more TBB task,
|
||||
// e.g. at least one event.
|
||||
// Note 2: In this first example, we do not care about memory usage:
|
||||
// The resources required depend only on the total number of
|
||||
// threads used for at least one event during the simulation.
|
||||
// Note 3: It is possible to do better - this is left for another example.
|
||||
// Here is a sketch how:
|
||||
// If there is a set maximum <N> threads which will be doing
|
||||
// simulation at any point, and that this is smaller than the
|
||||
// number of TBB tasks which can run simulataneously.
|
||||
// What would need to be done is to "acquire" a resource (workspace)
|
||||
// which holds all the memory required for a running task/thread,
|
||||
// and release it at the end of the task to be re-used by other tasks
|
||||
// possibly on different threads. This is demonstrated in another
|
||||
// example.
|
||||
|
||||
static G4ThreadLocal tbbWorkerRunManager* localRM = 0;
|
||||
|
||||
|
||||
//Is this task running for the first time?
|
||||
//How to re-initialize between runs????
|
||||
if (! localRM ) {
|
||||
//It's a new thread, basically repeat what is being done in
|
||||
//G4MTRunManagerKernel::StarThread with an
|
||||
//important difference, do not process data or wait for master to
|
||||
//communicate what to do, it will
|
||||
//not happen!
|
||||
G4MTRunManager* masterRM = G4MTRunManager::GetMasterRunManager();
|
||||
//============================
|
||||
//Step-0: Thread ID
|
||||
//============================
|
||||
//Initliazie per-thread stream-output
|
||||
//The following line is needed before we actually do I/O initialization
|
||||
//because the constructor of UI manager resets the I/O destination.
|
||||
G4int thisId = counter.fetch_and_increment();
|
||||
G4Threading::G4SetThreadId( thisId );
|
||||
G4UImanager::GetUIpointer()->SetUpForAThread( thisId );
|
||||
|
||||
//============================
|
||||
//Step-1: Random number engine
|
||||
//============================
|
||||
//RNG Engine needs to be initialized by "cloning" the master one.
|
||||
const CLHEP::HepRandomEngine* masterEngine =
|
||||
masterRM->getMasterRandomEngine();
|
||||
masterRM->GetUserWorkerThreadInitialization()->SetupRNGEngine(masterEngine);
|
||||
|
||||
//============================
|
||||
//Step-2: Initialize worker thread
|
||||
//============================
|
||||
if(masterRM->GetUserWorkerInitialization())
|
||||
masterRM->GetUserWorkerInitialization()->WorkerInitialize();
|
||||
if(masterRM->GetUserActionInitialization()) {
|
||||
G4VSteppingVerbose* sv =
|
||||
masterRM->GetUserActionInitialization()->InitializeSteppingVerbose();
|
||||
if (sv) G4VSteppingVerbose::SetInstance(sv);
|
||||
}
|
||||
//Now initialize worker part of shared objects (geometry/physics)
|
||||
G4WorkerThread::BuildGeometryAndPhysicsVector();
|
||||
localRM = static_cast<tbbWorkerRunManager*>(
|
||||
masterRM->GetUserWorkerThreadInitialization()->CreateWorkerRunManager()
|
||||
);
|
||||
G4cout<<localRM<<G4endl;
|
||||
//localRM->SetWorkerThread(wThreadContext);
|
||||
// G4AutoLock wrmm(&workerRMMutex);
|
||||
// G4MTRunManagerKernel::workerRMvector->push_back(localRM); //<<<<?????? ANDREA TBB
|
||||
// wrmm.unlock();
|
||||
|
||||
//================================
|
||||
//Step-3: Setup worker run manager
|
||||
//================================
|
||||
// Set the detector and physics list to the worker thread. Share with master
|
||||
const G4VUserDetectorConstruction* detector =
|
||||
masterRM->GetUserDetectorConstruction();
|
||||
localRM->G4RunManager::SetUserInitialization(
|
||||
const_cast<G4VUserDetectorConstruction*>(detector));
|
||||
const G4VUserPhysicsList* physicslist = masterRM->GetUserPhysicsList();
|
||||
localRM->SetUserInitialization(const_cast<G4VUserPhysicsList*>(physicslist));
|
||||
|
||||
//================================
|
||||
//Step-4: Initialize worker run manager
|
||||
//================================
|
||||
if(masterRM->GetUserActionInitialization())
|
||||
{ masterRM->GetNonConstUserActionInitialization()->Build(); }
|
||||
if(masterRM->GetUserWorkerInitialization())
|
||||
{ masterRM->GetUserWorkerInitialization()->WorkerStart(); }
|
||||
localRM->Initialize();
|
||||
|
||||
//Problem at this point is if there is more than one run...
|
||||
// Execute UI commands stored in the masther UI manager
|
||||
std::vector<G4String> cmds = masterRM->GetCommandStack();
|
||||
G4UImanager* uimgr = G4UImanager::GetUIpointer(); //TLS instance
|
||||
std::vector<G4String>::const_iterator it = cmds.begin();
|
||||
for(;it!=cmds.end();it++)
|
||||
{ uimgr->ApplyCommand(*it); }
|
||||
//Start this run
|
||||
G4String macroFile = masterRM->GetSelectMacro();
|
||||
G4int numSelect = masterRM->GetNumberOfSelectEvents();
|
||||
if ( macroFile == "" || macroFile == " " )
|
||||
{
|
||||
localRM->BeamOn(m_nEvents,0,numSelect);
|
||||
}
|
||||
else
|
||||
{
|
||||
localRM->BeamOn(m_nEvents,macroFile,numSelect);
|
||||
}
|
||||
//======= NEW TBB SPECIFIC ===========
|
||||
//Step-5: Initialize and start run
|
||||
//====================================
|
||||
// bla-bla-bla-bla
|
||||
// This is basically BeamOn
|
||||
m_beamOnCondition = localRM->ConfirmBeamOnCondition();
|
||||
if (m_beamOnCondition) {
|
||||
localRM->SetNumberOfEventsToBeProcessed( m_nEvents );
|
||||
localRM->ConstructScoringWorlds();
|
||||
localRM->RunInitialization();
|
||||
//Register this G4Run in output
|
||||
//Note: the idea is that we are going to accumulate everything in
|
||||
//G4Run or derived class. We let the kernel know this is the object
|
||||
//where the output is accumulated for the tbb::tasks that run on
|
||||
//this thread.
|
||||
if ( m_output ) {}
|
||||
}
|
||||
}
|
||||
assert(localRM!=0);
|
||||
if ( m_beamOnCondition ) {
|
||||
localRM->DoEventLoop( m_nEvents );
|
||||
//localRM->RunTermination(); //<<<< How to call this??? ANDREA TBB
|
||||
}
|
||||
|
||||
//Cannot be done here since thread can be re-used by other task
|
||||
//===============================
|
||||
//Step-6: Terminate worker thread
|
||||
//===============================
|
||||
// if(masterRM->GetUserWorkerInitialization())
|
||||
// { masterRM->GetUserWorkerInitialization()->WorkerStop(); }
|
||||
|
||||
// wrmm.lock();
|
||||
// std::vector<G4WorkerRunManager*>::iterator itrWrm = workerRMvector->begin();
|
||||
// for(;itrWrm!=workerRMvector->end();itrWrm++)
|
||||
// {
|
||||
// if((*itrWrm)==wrm)
|
||||
// {
|
||||
// workerRMvector->erase(itrWrm);
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// wrmm.unlock();
|
||||
|
||||
// wThreadContext->DestroyGeometryAndPhysicsVector();
|
||||
// wThreadContext = 0;
|
||||
|
||||
return static_cast<tbb::task*>(NULL);
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
//
|
||||
// ********************************************************************
|
||||
// * License and Disclaimer *
|
||||
// * *
|
||||
// * The Geant4 software is copyright of the Copyright Holders of *
|
||||
// * the Geant4 Collaboration. It is provided under the terms and *
|
||||
// * conditions of the Geant4 Software License, included in the file *
|
||||
// * LICENSE and available at http://cern.ch/geant4/license . These *
|
||||
// * include a list of copyright holders. *
|
||||
// * *
|
||||
// * Neither the authors of this software system, nor their employing *
|
||||
// * institutes,nor the agencies providing financial support for this *
|
||||
// * work make any representation or warranty, express or implied, *
|
||||
// * regarding this software system or assume any liability for its *
|
||||
// * use. Please see the license in the file LICENSE and URL above *
|
||||
// * for the full disclaimer and the limitation of liability. *
|
||||
// * *
|
||||
// * This code implementation is the result of the scientific and *
|
||||
// * technical work of the GEANT4 collaboration. *
|
||||
// * By using, copying, modifying or distributing the software (or *
|
||||
// * any work based on the software) you agree to acknowledge its *
|
||||
// * use in resulting scientific publications, and indicate your *
|
||||
// * acceptance of all terms of the Geant4 Software license. *
|
||||
// ********************************************************************
|
||||
//
|
||||
|
||||
#include "tbbUserWorkerInitialization.hh"
|
||||
#include "G4Threading.hh"
|
||||
#include "tbbWorkerRunManager.hh"
|
||||
|
||||
tbbUserWorkerInitialization::~tbbUserWorkerInitialization()
|
||||
{}
|
||||
|
||||
void tbbUserWorkerInitialization::JoinWorker(G4Thread*)
|
||||
{
|
||||
//Not needed for TBB
|
||||
}
|
||||
|
||||
G4Thread* tbbUserWorkerInitialization::CreateAndStartWorker(G4WorkerThread*)
|
||||
{
|
||||
//Not needed for TBB
|
||||
return static_cast<G4Thread*>(0);
|
||||
}
|
||||
|
||||
G4WorkerRunManager* tbbUserWorkerInitialization::CreateWorkerRunManager() const
|
||||
{
|
||||
return new tbbWorkerRunManager();
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
//
|
||||
// ********************************************************************
|
||||
// * License and Disclaimer *
|
||||
// * *
|
||||
// * The Geant4 software is copyright of the Copyright Holders of *
|
||||
// * the Geant4 Collaboration. It is provided under the terms and *
|
||||
// * conditions of the Geant4 Software License, included in the file *
|
||||
// * LICENSE and available at http://cern.ch/geant4/license . These *
|
||||
// * include a list of copyright holders. *
|
||||
// * *
|
||||
// * Neither the authors of this software system, nor their employing *
|
||||
// * institutes,nor the agencies providing financial support for this *
|
||||
// * work make any representation or warranty, express or implied, *
|
||||
// * regarding this software system or assume any liability for its *
|
||||
// * use. Please see the license in the file LICENSE and URL above *
|
||||
// * for the full disclaimer and the limitation of liability. *
|
||||
// * *
|
||||
// * This code implementation is the result of the scientific and *
|
||||
// * technical work of the GEANT4 collaboration. *
|
||||
// * By using, copying, modifying or distributing the software (or *
|
||||
// * any work based on the software) you agree to acknowledge its *
|
||||
// * use in resulting scientific publications, and indicate your *
|
||||
// * acceptance of all terms of the Geant4 Software license. *
|
||||
// ********************************************************************
|
||||
//
|
||||
#include "tbbWorkerRunManager.hh"
|
||||
#include "G4Run.hh"
|
||||
#include "G4MTRunManager.hh"
|
||||
#include "G4UserWorkerInitialization.hh"
|
||||
#include "G4UserRunAction.hh"
|
||||
#include "G4SDManager.hh"
|
||||
|
||||
tbbWorkerRunManager::tbbWorkerRunManager() :
|
||||
G4WorkerRunManager()
|
||||
{}
|
||||
|
||||
tbbWorkerRunManager::~tbbWorkerRunManager()
|
||||
{}
|
||||
|
||||
void tbbWorkerRunManager::MergePartialResults()
|
||||
{
|
||||
//TBB ??????
|
||||
}
|
||||
|
||||
void tbbWorkerRunManager::ConstructScoringWorlds()
|
||||
{
|
||||
//Forward call to protected method, that's all
|
||||
G4WorkerRunManager::ConstructScoringWorlds();
|
||||
}
|
||||
@@ -15,6 +15,9 @@ track of all tags.
|
||||
* Reverse chronological order (last date on top), please *
|
||||
----------------------------------------------------------
|
||||
|
||||
28th May 2020 J. Madsen (tbbex-V10-06-00)
|
||||
- Updated TBB example to use new tasking system which is fully compatible with TBB
|
||||
|
||||
25th Oct 2018 J. Madsen (tbbex-V10-04-01)
|
||||
- Updated TBB example to work with migration from POSIX threads to C++11 threads
|
||||
|
||||
|
||||
Reference in New Issue
Block a user