Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring the new Async mode additions #321

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,11 @@ endif()
#----------------------------------------------------------------------------#
set(ADEPT_G4_INTEGRATION_SRCS
src/AdePTTrackingManager.cc
src/AdePTTrackingManager.cu
src/AdePTPhysics.cc
src/HepEMPhysics.cc
src/AdePTGeant4Integration.cpp
src/AdePTConfigurationMessenger.cc
src/AdePTConfiguration.cc
)

add_library(CopCore INTERFACE)
Expand All @@ -180,8 +180,6 @@ target_include_directories(CopCore

add_library(AdePT_G4_integration SHARED
${ADEPT_G4_INTEGRATION_SRCS}
src/AdePTTransport.cc
src/AdePTTransport.cu
)
target_include_directories(AdePT_G4_integration
PUBLIC
Expand Down
9 changes: 5 additions & 4 deletions include/AdePT/core/AdePTConfiguration.hh
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ std::shared_ptr<AdePTTransportInterface> AdePTTransportFactory(unsigned int nThr
///
class AdePTConfiguration {
public:
AdePTConfiguration();
~AdePTConfiguration();
AdePTConfiguration() : fAdePTConfigurationMessenger{new AdePTConfigurationMessenger(this)} {}
~AdePTConfiguration() {}
void SetNumThreads(int numThreads) { fNumThreads = numThreads; }
void SetTrackInAllRegions(bool trackInAllRegions) { fTrackInAllRegions = trackInAllRegions; }
void AddGPURegionName(std::string name) { fGPURegionNames.push_back(name); }
void SetAdePTActivation(bool activateAdePT) { fAdePTActivated = activateAdePT; }
Expand All @@ -42,6 +43,7 @@ public:

bool GetTrackInAllRegions() { return fTrackInAllRegions; }
bool IsAdePTActivated() { return fAdePTActivated; }
int GetNumThreads() { return fNumThreads; };
int GetVerbosity() { return fVerbosity; };
int GetTransportBufferThreshold() { return fTransportBufferThreshold; }
int GetCUDAStackLimit() { return fCUDAStackLimit; }
Expand All @@ -50,14 +52,13 @@ public:
double GetMillionsOfHitSlots() { return fMillionsOfHitSlots; }
std::vector<std::string> *GetGPURegionNames() { return &fGPURegionNames; }

std::shared_ptr<AdePTTransportInterface> CreateAdePTInstance(unsigned int nThread);

// Temporary
std::string GetVecGeomGDML() { return fVecGeomGDML; }

private:
bool fTrackInAllRegions{false};
bool fAdePTActivated{true};
int fNumThreads;
int fVerbosity{0};
int fTransportBufferThreshold{200};
int fCUDAStackLimit{0};
Expand Down
7 changes: 4 additions & 3 deletions include/AdePT/core/AdePTTransport.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "CommonStruct.h"
#include <AdePT/core/AdePTScoringTemplate.cuh>
#include <AdePT/core/HostScoringStruct.cuh>
#include <AdePT/core/AdePTConfiguration.hh>

class G4Region;
struct GPUstate;
Expand All @@ -32,7 +33,7 @@ class AdePTTransport : public AdePTTransportInterface {
using TrackBuffer = adeptint::TrackBuffer;
using VolAuxArray = adeptint::VolAuxArray;

AdePTTransport() = default;
AdePTTransport(AdePTConfiguration &configuration);

~AdePTTransport() { delete fScoring; }

Expand Down Expand Up @@ -76,8 +77,8 @@ class AdePTTransport : public AdePTTransportInterface {

private:
static inline G4HepEmState *fg4hepem_state{nullptr}; ///< The HepEm state singleton
static inline int fCapacity{1024 * 1024}; ///< Track container capacity on GPU
static inline int fHitBufferCapacity{1024 * 1024}; ///< Capacity of hit buffers
int fCapacity{1024 * 1024}; ///< Track container capacity on GPU
int fHitBufferCapacity{1024 * 1024}; ///< Capacity of hit buffers
int fNthreads{0}; ///< Number of cpu threads
int fMaxBatch{0}; ///< Max batch size for allocating GPU memory
int fNumVolumes{0}; ///< Total number of active logical volumes
Expand Down
16 changes: 16 additions & 0 deletions include/AdePT/core/AdePTTransport.icc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ void ShowerGPU(IntegrationLayer &integration, int event, TrackBuffer &buffer, GP

} // namespace adept_impl

template <typename IntegrationLayer>
AdePTTransport<IntegrationLayer>::AdePTTransport(AdePTConfiguration &configuration)
{
fDebugLevel = 0;
fBufferThreshold = configuration.GetTransportBufferThreshold();
fMaxBatch = 2 * configuration.GetTransportBufferThreshold();
fTrackInAllRegions = configuration.GetTrackInAllRegions();
fGPURegionNames = configuration.GetGPURegionNames();
fCUDAStackLimit = configuration.GetCUDAStackLimit();
fCapacity = 1024 * 1024 * configuration.GetMillionsOfTrackSlots() / configuration.GetNumThreads();
fHitBufferCapacity = 1024 * 1024 * configuration.GetMillionsOfHitSlots() / configuration.GetNumThreads();

printf( "AdePT Allocated track capacity: %d tracks\n", fCapacity);
printf( "AdePT Allocated step buffer capacity: %d tracks\n", fHitBufferCapacity);
}

template <typename IntegrationLayer>
bool AdePTTransport<IntegrationLayer>::InitializeField(double bz)
{
Expand Down
3 changes: 2 additions & 1 deletion include/AdePT/integration/AdePTTrackingManager.hh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "globals.hh"
#include <AdePT/core/AdePTTransportInterface.hh>
#include <AdePT/core/AdePTTransport.h>
#include "AdePT/copcore/SystemOfUnits.h"
#include <AdePT/integration/AdePTGeant4Integration.hh>
#include <AdePT/core/AdePTConfiguration.hh>
Expand Down Expand Up @@ -49,9 +50,9 @@ private:
/// @brief Steps a track using the Generic G4TrackingManager until it enters a GPU region or stops
void StepInHostRegion(G4Track *aTrack);

static inline int fNumThreads{0};
std::set<G4Region const *> fGPURegions{};
int fVerbosity{0};

std::shared_ptr<AdePTTransportInterface> fAdeptTransport;
AdePTConfiguration *fAdePTConfiguration;
unsigned int fTrackCounter{0};
Expand Down
29 changes: 0 additions & 29 deletions src/AdePTConfiguration.cc

This file was deleted.

1 change: 0 additions & 1 deletion src/AdePTGeant4Integration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,6 @@ void AdePTGeant4Integration::ReturnTrack(adeptint::TrackData const &track, unsig
G4Track *secondary = new G4Track(dynamique, track.globalTime, posi);
secondary->SetLocalTime(track.localTime);
secondary->SetProperTime(track.properTime);
secondary->SetTrackID(kAdePTTrackID);
secondary->SetParentID(track.parentID);

G4EventManager::GetEventManager()->GetStackManager()->PushOneTrack(secondary);
Expand Down
22 changes: 10 additions & 12 deletions src/AdePTTrackingManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,25 @@ AdePTTrackingManager::~AdePTTrackingManager()

void AdePTTrackingManager::InitializeAdePT()
{
const auto num_threads = G4RunManager::GetRunManager()->GetNumberOfThreads();

// Check if this is a sequential run
G4RunManager::RMType rmType = G4RunManager::GetRunManager()->GetRunManagerType();
bool sequential = (rmType == G4RunManager::sequentialRM);

// One thread initializes common elements
auto tid = G4Threading::G4GetThreadId();
if (tid < 0) {
// Only the master thread knows the actual number of threads, the worker threads will return "1"
// This value is stored here by the master in a static variable, and used by each thread to pass the
// correct number to their AdePTConfiguration instance
fNumThreads = G4RunManager::GetRunManager()->GetNumberOfThreads();
fAdePTConfiguration->SetNumThreads(fNumThreads);

// Load the VecGeom world in memory
AdePTGeant4Integration::CreateVecGeomWorld(fAdePTConfiguration->GetVecGeomGDML());

// Create an instance of an AdePT transport engine. This can either be one engine per thread or a shared engine for
// all threads.
fAdeptTransport = fAdePTConfiguration->CreateAdePTInstance(num_threads);
fAdeptTransport = std::make_shared<AdePTTransport<AdePTGeant4Integration>>(*fAdePTConfiguration);

// Initialize common data:
// G4HepEM, Upload VecGeom geometry to GPU, Geometry check, Create volume auxiliary data
Expand All @@ -54,7 +58,8 @@ void AdePTTrackingManager::InitializeAdePT()
} else {
// Create an instance of an AdePT transport engine. This can either be one engine per thread or a shared engine for
// all threads.
fAdeptTransport = fAdePTConfiguration->CreateAdePTInstance(num_threads);
fAdePTConfiguration->SetNumThreads(fNumThreads);
fAdeptTransport = std::make_shared<AdePTTransport<AdePTGeant4Integration>>(*fAdePTConfiguration);
// Initialize per-thread data
fAdeptTransport->Initialize();
}
Expand Down Expand Up @@ -190,10 +195,8 @@ void AdePTTrackingManager::ProcessTrack(G4Track *aTrack)
// Check if the particle is in a GPU region
const bool isGPURegion = trackInAllRegions || fGPURegions.find(region) != fGPURegions.end();

if (isGPURegion &&
aTrack->GetTrackID() != AdePTGeant4Integration::kAdePTTrackID /*track didn't come back from AdePT*/) {
if (isGPURegion) {
// If the track is in a GPU region, hand it over to AdePT

auto particlePosition = aTrack->GetPosition();
auto particleDirection = aTrack->GetMomentumDirection();
G4double energy = aTrack->GetKineticEnergy();
Expand Down Expand Up @@ -240,16 +243,11 @@ void AdePTTrackingManager::StepInHostRegion(G4Track *aTrack)
G4TrackingManager *trackManager = eventManager->GetTrackingManager();
G4SteppingManager *steppingManager = trackManager->GetSteppingManager();
G4Region const *previousRegion = aTrack->GetVolume()->GetLogicalVolume()->GetRegion();
G4UserSteppingAction *steppingAction = steppingManager->GetUserAction();
const bool stayOnHost = aTrack->GetParentID() == AdePTGeant4Integration::kAdePTTrackID;

// Track the particle Step-by-Step while it is alive and outside of a GPU region
while ((aTrack->GetTrackStatus() == fAlive || aTrack->GetTrackStatus() == fStopButAlive)) {
aTrack->IncrementCurrentStepNumber();
steppingManager->Stepping();
if (steppingAction) steppingAction->UserSteppingAction(aTrack->GetStep());

if (stayOnHost) continue;

if (aTrack->GetTrackStatus() != fStopAndKill) {
// Switch the touchable to update the volume, which is checked in the
Expand Down
File renamed without changes.
39 changes: 0 additions & 39 deletions src/AdePTTransport.cc

This file was deleted.

Loading