Skip to content
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
9 changes: 8 additions & 1 deletion include/AdePT/integration/HostTrackDataMapper.hh
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,14 @@ public:
{
auto it = gpuToIndex.find(gpuId);
if (it == gpuToIndex.end()) return; // already gone
int idx = it->second;
int idx = it->second;

// As the data of the userTrackInfo is owned by AdePT, it has to be deleted here
if (hostDataVec[idx].userTrackInfo) {
delete hostDataVec[idx].userTrackInfo;
hostDataVec[idx].userTrackInfo = nullptr;
}

int last = int(hostDataVec.size()) - 1;

// unused g4 id
Expand Down
28 changes: 17 additions & 11 deletions src/AdePTGeant4Integration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,12 @@ void AdePTGeant4Integration::ProcessGPUStep(GPUHit const &hit, bool const callUs

// cleanup of the secondary vector that is created in FillG4Step above
fScoringObjects->fG4Step->DeleteSecondaryVector();

// If this was the last step of a track, the hostTrackData of that track can be safely deleted.
// Note: This deletes the AdePT-owned UserTrackInfo data
if (hit.fLastStepOfTrack) {
fHostTrackDataMapper->removeTrack(hit.fTrackID);
}
}

void AdePTGeant4Integration::FillG4NavigationHistory(vecgeom::NavigationState aNavState,
Expand Down Expand Up @@ -794,11 +800,6 @@ void AdePTGeant4Integration::FillG4Step(GPUHit const *aGPUHit, G4Step *aG4Step,
auto *userTrackingAction = evtMgr->GetUserTrackingAction();
if (userTrackingAction) userTrackingAction->PostUserTrackingAction(aTrack);
}

// remove killed tracks from storage to reduce lookup table size
if (aGPUHit->fLastStepOfTrack) {
fHostTrackDataMapper->removeTrack(aGPUHit->fTrackID);
}
}

void AdePTGeant4Integration::ReturnTrack(adeptint::TrackData const &track, unsigned int trackIndex, int debugLevel,
Expand Down Expand Up @@ -938,14 +939,19 @@ void AdePTGeant4Integration::ReturnTrack(adeptint::TrackData const &track, unsig
// Give secondaries to G4
G4EventManager::GetEventManager()->StackTracks(&secondaries);

// Since we do not give back the track to G4, we have to delete it here
// As Gamma-nuclear kills the track, and the track is owned by AdePT, it has to be deleted. This includes:
// 1. deleting the HostTrackData, which also deletes the UserTrackInfo
// 2. Setting the UserInformation pointer in the track to nullptr (as the data was just deleted)
// 3. deleting the track
// Note that it is safe to remove the track and the hostTrackData here, as the hits are always handled
// before the leaks and hits with Gamma-nuclear are not marked as last step.
fHostTrackDataMapper->removeTrack(track.trackId);
// as the UserTrackInfo was just deleted by removeTrack, the pointer must be set to null to avoid double
// deletion
leakedTrack->SetUserInformation(nullptr);
// Now the track and step can be safely deleted
delete leakedTrack;
delete step;

// gamma nuclear kills the track, so we safely remove it from the hostTrackData map, as the hits are always
// handled before the leaked tracks
fHostTrackDataMapper->removeTrack(track.trackId);

} else {
// no gamma nuclear process attached, just give back the track to G4 to put it back on GPU
G4EventManager::GetEventManager()->GetStackManager()->PushOneTrack(leakedTrack);
Expand Down
9 changes: 7 additions & 2 deletions src/AdePTTrackingManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,13 @@ void AdePTTrackingManager::ProcessTrack(G4Track *aTrack)
// The track dies from the point of view of Geant4
aTrack->SetTrackStatus(fStopAndKill);

// After the track has been offloaded to the GPU, it can be deleted on the CPU.
// However, the HostTrackData is now owning and therefore responsible for deleting the TrackUserInfo data
// To avoid deletion of the TrackUserInfo data when the track is deleted, the pointer must be reset.
// Then, the underlying data is either deleted via hostTrackData.removeTrack in case the track is finished on GPU,
// or the ownership is transferred back to G4, when the track is given back to the CPU
aTrack->SetUserInformation(nullptr);

} else { // If the particle is not in a GPU region, track it on CPU
// Track the particle step by step until it dies or enters a GPU region in the (specialized)
// G4HepEmTrackingManager
Expand All @@ -454,8 +461,6 @@ void AdePTTrackingManager::ProcessTrack(G4Track *aTrack)
}

// delete track after finishing offloading to AdePT or finished tracking in G4HepEmTrackingManager
aTrack->SetUserInformation(
nullptr); // set UserInformation to null as its memory would otherwise be deleted when we delete the track
delete aTrack;
}

Expand Down