Skip to content

GPU TPC: Run extrapolation tracking only on selected tracks - #16210

Open
OliverRietmann wants to merge 10 commits into
AliceO2Group:devfrom
cern-nextgen:PR-extrapolation-tracking
Open

OliverRietmann wants to merge 10 commits into
AliceO2Group:devfrom
cern-nextgen:PR-extrapolation-tracking

Conversation

@OliverRietmann

Copy link
Copy Markdown
Contributor

Changes: For every TPC sector, during GPUTPCTrackletSelector, we write a list of left and right exiting tracks. Then GPUTPCExtrapolationTracking only runs on those tracks. Newly, we do this per pair of sectors, or per edge that they share, respectively. This changes how the sectors have to wait for their neighboring sectors to finish GPUTPCTrackletSelector. While not strictly necessary, we have changed the number of streams and how we assign them to the sectors. Finally, the GPUParameters.csv was updated to fit the new extrapolation tracking.

Remarks:

  1. Since extrapolation tracking is now done per edge rather than per sector, we needed write access via iSector.GetConstantMem()->tpcTrackers[iSectorNeighbor]. This is was actively prohibited before and should thus be looked at.
  2. Since the extrapolation over the left and right edge of a sector now run in different streams, copying to the host is less trivial. The PR implements a redundant copy in GPUChainTracking::ExtrapolationTracking. The two copies are racing, but in a way taht doesn't corrupt the final data (on copy copies a subset of the other). Maybe we can find a better solution for this.

@davidrohr davidrohr left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some general comments, didn't check the kernel in detail, I assume with deterministic reconstruction, it reproduces the previous output exactly?

Comment thread GPU/GPUTracking/Base/GPUProcessor.h Outdated
#endif

GPUd() GPUconstantref() const GPUConstantMem* GetConstantMem() const; // Body in GPUConstantMem.h to avoid circular headers
GPUd() GPUconstantref() GPUConstantMem* GetConstantMem(); // ...

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need a non-const version? In principle, this should be constant memory during kernels?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed it according to your comment below.

void GPUReconstruction::RegisterGPUDeviceProcessor(GPUProcessor* proc, GPUProcessor* slaveProcessor) { proc->InitGPUProcessor(this, GPUProcessor::PROCESSOR_TYPE_DEVICE, slaveProcessor); }
void GPUReconstruction::ConstructGPUProcessor(GPUProcessor* proc) { proc->mConstantMem = proc->mGPUProcessorType == GPUProcessor::PROCESSOR_TYPE_DEVICE ? mDeviceConstantMem : mHostConstantMem.get(); }

uint32_t GPUReconstruction::StreamForSector(uint32_t sector) const

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we move everything that is not pure-GPU related to GPUChainTracking. GPUReconstruction should be generic.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reusable memory in GPUTPCTracker::RegisterMemoryAllocation() relied on the specific way of assigning the sectors to the streams that we had before (round robin). I moved the function to have a "single source of truth" for both GPUTPCTracker and GPUTPCChainTrackingSectorTracker. I can move it back to GPUChainTracking and include the latter in GPUTPCTracker.

AddOption(inKernelParallel, uint8_t, 2, "", 0, "Parallelize with multi-threading inside kernels on the host instead of over TPC sectors: 0 disable, 1 full and exclusive in-kernel parallelization, 2 for nested parallelization over TPC sectors (nTPCClustererLanes thread-groups) and inside kernels")
AddOption(autoAdjustHostThreads, bool, true, "", 0, "Auto-adjust number of OMP threads, decreasing the number for small input data")
AddOption(nStreams, int8_t, 8, "", 0, "Number of GPU streams / command queues")
AddOption(nStreams, int8_t, 6, "", 0, "Number of GPU streams / command queues")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might be wrong, but I belive the TPC clusterization can parallelize over 7 streams at the moment. I thought you wanted to add a nStreamsTPCSectorTracking option, to use only a subset of streams in the sector tracking?

@OliverRietmann OliverRietmann Dec 1, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have rest --nStreams to a deafult of 8 and I added a new option --nStreamsTPCSectorTracking with a default of 6. It gets clamped between 1 and --nStreams here.

GPUhd() GPUglobalref() GPUAtomic(uint32_t) * NExtrapolationCandidatesRight() const { return &mCommonMem->nExtrapolationCandidatesRight; }
GPUhd() GPUglobalref() int32_t* ExtrapolationCandidatesLeft() const { return mExtrapolationCandidatesLeft; }
GPUhd() GPUglobalref() int32_t* ExtrapolationCandidatesRight() const { return mExtrapolationCandidatesRight; }
GPUhd() GPUglobalref() int32_t* ExtrapolationCandidatesLeftDirection() const { return mExtrapolationCandidatesLeftDirection; }

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

int8_t should be enough. In principle, you should be able to store the direction together with the candidateID by using the highest bit. nTracks should never exceed 24 bit anyway.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the two direction arrays and we are now storing the direction in the sign bit. We need this "packing/unpacking" logic in three files: GPUTPCTrackletSelector.cxx, GPUTPCExtrapolationTracking.cxx, and GPUTPCSectorDebugSortKernels.cxx and therefor I have created a dedicated class GPUTPCExtrapolationCandidate. (But maybe this is an overkill?)

@davidrohr

Copy link
Copy Markdown
Collaborator

2. Since the extrapolation over the left and right edge of a sector now run in different streams, copying to the host is less trivial. The PR implements a redundant copy in GPUChainTracking::ExtrapolationTracking. The two copies are racing, but in a way taht doesn't corrupt the final data (on copy copies a subset of the other). Maybe we can find a better solution for this.

I think you can just use GPUMemcpy directly to copy only part of a buffer?

@davidrohr

Copy link
Copy Markdown
Collaborator
  1. Since extrapolation tracking is now done per edge rather than per sector, we needed write access via iSector.GetConstantMem()->tpcTrackers[iSectorNeighbor]. This is was actively prohibited before and should thus be looked at.

I still don't understand what do you need the write access for? You should only write to the arrays, but not to the constant memory itself.
In any case, probably what you want to do instead is to change the processoor type of the processor, so that you get all of them (like

  GPUhdi() static processorType* Processor(GPUConstantMem& processors)
  {
    return &processors;
  }

like the merger and DataCompression kernels do it. Then your kernel already receives ConstantMem and not only the current sector GPUTPCTracker.

{
AllocateAndInitializeLate();
bool reuseCondition = MemoryReuseAllowed();
GPUMemoryReuse reLinks{reuseCondition, GPUMemoryReuse::REUSE_1TO1, GPUMemoryReuse::TrackerDataLinks, (uint16_t)(mISector % mRec->GetProcessingSettings().nStreams)};

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, you refer to this? This seems a bug indeed! I'd do the following:

  • Move StreamForSector back to GPUChainTracking.
  • Include GPUChainTracking.h to GPUTPCTracker.cxx #ifndef GPUCAGPUCODE
  • Add an optional option to StreamForSector overriding the nstreams, and use the new parameter for tpcsectortracking when called from GPUTPCTracker.cxx

@OliverRietmann OliverRietmann Dec 1, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved the function back and added a second argumentGPUChainTracking::StreamForSector(uint32_t sector, const GPUSettingsProcessing& proc). I was not quite sure what you want as default behavior, but StreamForSector now reads the number of streams for sector tracking from the new option --nStreamsTPCSectorTracking to ensure it is always the same in GPUMemoryReuse reLinks and the actual sector tracking.

@OliverRietmann

OliverRietmann commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor Author
  1. Since extrapolation tracking is now done per edge rather than per sector, we needed write access via iSector.GetConstantMem()->tpcTrackers[iSectorNeighbor]. This is was actively prohibited before and should thus be looked at.

I still don't understand what do you need the write access for? You should only write to the arrays, but not to the constant memory itself. In any case, probably what you want to do instead is to change the processoor type of the processor, so that you get all of them (like

  GPUhdi() static processorType* Processor(GPUConstantMem& processors)
  {
    return &processors;
  }

like the merger and DataCompression kernels do it. Then your kernel already receives ConstantMem and not only the current sector GPUTPCTracker.

Yes, this is exactly what I wanted. I changed it to the above.

@OliverRietmann

Copy link
Copy Markdown
Contributor Author

Some general comments, didn't check the kernel in detail, I assume with deterministic reconstruction, it reproduces the previous output exactly?

Yes, we get the exact same GPU.out and CPU.out files.

@OliverRietmann

OliverRietmann commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor Author

I think you can just use GPUMemcpy directly to copy only part of a buffer?

The problem is actually not the two consecutive lines of copies, but the fact that the left and right entering tracks are computed in different streams. Thus we cannot copy when one is done, because we don't know if the other is already done. I have now enqueued the host to device copies at the end of the sector's stream, see here. I am still not using GPUMemcpy because TransferMemoryResourceLinkToHost allowed me to wait for the other side to finish it's extrapolation tracking.

@davidrohr davidrohr left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, looks good. I have one more minor request below.

mNStreams = std::max<int32_t>(GetProcessingSettings().nStreams, 3);
}

if ((int32_t)GetProcessingSettings().nStreamsTPCSectorTracking > GetProcessingSettings().nStreams || GetProcessingSettings().nStreamsTPCSectorTracking < 1) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a function ValidateSettings, where such checks should live.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved the range check to GPUChainTracking::ValidateSettings(), see here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the range check and clamp to NStreams() in StreamForSector, according to our private discussion.

…UReconstruction.cxx to GPUChainTracking::ValidateSettings()
@OliverRietmann

Copy link
Copy Markdown
Contributor Author

Do you want me to clean up the git history after these fixes so that we have just 4 commits again? (requires force-push)

@davidrohr

Copy link
Copy Markdown
Collaborator

Performance-wise, looks very good: On my 5090, the ExtrapolationKernel gets 2x faster, and trackletSelector maintains the same speed.

I asked Claude to do a code review, and it found some issues. Could you have a look:

Bugs:

  1. Wrong shared row cache when the candidates exceed the grid (GPUTPCExtrapolationTracking.cxx:130, GPU only).
    • Each block caches the rows of one target sector, chosen once from iBlock * nThreads < leftToRightCandidatesPadded.
    • The loop then strides by nThreads * nBlocks, so if leftToRightCandidatesPadded + nCandidatesLeft is larger than the grid, a block that started with left→right candidates wraps into right→left ones.
    • Those then track in the left sector with the right sector's rows in smem.mRows, which gives silently wrong results.
    • The grid is 256 blocks × the launch-bound thread count, so about 16k slots with the new [64, 1] entries. That is probably not reached today, but nothing guards it.
    • Fix: give each direction a fixed set of blocks (e.g. first half / second half, each striding only over its own list), or use two launches.
  2. ReleaseEvent on events that were never recorded (GPUChainTrackingSectorTracker.cxx:253).
    • extrapolationTrackingDone[iSector] is released whenever doGPU, but only recorded if rec.tpc.extrapolationTracking is on.
    • With --RECTPCextrapolationTracking 0, OpenCL calls clReleaseEvent on an empty event inside GPUChkErr, which should fail. CUDA is a no-op; I did not check HIP or CPUDevice.
    • Fix: move the release into the if (param().rec.tpc.extrapolationTracking) block.
  3. Two kernels now append to the same sector concurrently.
    • Before, sector s was the only target of its kernel. Now kernels (s-1, s) and (s, s+1) both add tracks to s, on different streams on GPU and different outer-loop threads on CPU.
    • The counters are real atomics on all backends, so this is safe.
    • It may make the order of extrapolated tracks on the CPU non-deterministic. The second sortDeviceDynamic in sectorTracks runs before the extrapolation, so it sorts an empty range and does not fix this.
    • A deterministic CPU vs GPU comparison would settle it.

Suggestions:

  • Dead code: the mRec->IsGPU() transfer loop in the CPU else branch can never do anything, since doGPU is false there.
  • Unconditional work and memory: MemorizeExtrapolationCandidate runs and the two candidate arrays (16 B per max track, in the output resource) are allocated even with extrapolation tracking off.
  • GPUParameters.csv: the commit also retunes unrelated kernels (StartHitsFinder, StartHitsSorter, NeighboursFinder, NEIGHBOURS_FINDER_UNROLL_*, ...) for several architectures. Check that this is intended and not a tuning run on one machine.
  • Style:
    • GPUTPCExtrapolationCandidate.h has no newline at the end of the file.
    • The commented-out GPUInfo lines were carried over.
    • The helper sits in an anonymous namespace and is called with ::; a private static member of GPUTPCTrackletSelector would match the surrounding code.
    • The four near-identical blocks in the helper could be one loop over lower / upper.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants