Repository navigation
Full Metal optimization stack #16136
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
base: dev
Are you sure you want to change the base?
Changes from all commits
661638f
cc125bb
53b3d9e
687be8e
aa77dc3
e929987
6c767cc
31f0a89
4ef5f46
6d52586
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,10 @@ | |
|
|
||
| /// \file GPUReconstructionMetalKernelsSpecialize.inc | ||
|
|
||
| #include "GPUTPCGMMergerSortComp.h" | ||
| #include <tbb/parallel_for.h> | ||
| #include <chrono> | ||
|
|
||
| template <> | ||
| inline void GPUReconstructionMetal::runKernelBackend<GPUMemClean16, 0>(const krnlSetupTime& _xyz, void* const& ptr, uint64_t const& size) | ||
| { | ||
|
|
@@ -30,3 +34,158 @@ inline void GPUReconstructionMetal::runKernelBackend<GPUMemClean16, 0>(const krn | |
| RecordMarker(_xyz.z.ev, _xyz.x.stream); | ||
| } | ||
| } | ||
|
|
||
| // The sorts that CUDA and HIP run with thrust (GPUCA_SPECIALIZE_THRUST_SORTS) | ||
| // run on a single GPU thread otherwise. The memory is shared with the host, so | ||
| // sort there, with the same std::sort as the CPU backend, which also orders | ||
| // the elements that compare equal as on the CPU. The number of elements is | ||
| // read once the stream is drained, from the device copy of the counters, | ||
| // since the host copy may still be on its way. | ||
| template <class T> | ||
| inline T* GPUReconstructionMetal::HostPointer(T* p) | ||
| { | ||
| const size_t offset = (const char*)p - (const char*)mDeviceMemoryBase; | ||
| return offset < mDeviceMemorySize ? (T*)((char*)mInternals->mem_gpu.contents + offset) : p; | ||
| } | ||
|
|
||
| template <class T, class N, class S> | ||
| inline void GPUReconstructionMetal::SortOnHost(const krnlSetupTime& _xyz, T* begin, const N& count, const S& comp) | ||
| { | ||
| if (_xyz.z.evList) { | ||
| StreamWaitForEvents(_xyz.x.stream, _xyz.z.evList, _xyz.z.nEvents); | ||
| } | ||
| DrainStream(_xyz.x.stream); | ||
| T* hostBegin = HostPointer(begin); | ||
| GPUCommonAlgorithm::sortDeviceDynamic(hostBegin, hostBegin + count(), comp); | ||
| if (_xyz.z.ev) { | ||
| RecordMarker(_xyz.z.ev, _xyz.x.stream); | ||
| } | ||
| } | ||
|
|
||
| // Kernels that are a long serial chain per GPU thread, with little parallelism, | ||
| // run on the host instead, on the shared memory, as the CPU backend runs them. | ||
| // The processors in mProcessorsShadow hold GPU addresses: the kernel runs on a | ||
| // private copy of mProcessorsShadow, whose processors point at the host view of | ||
| // the same memory, with the host's parameters and calibration objects, and | ||
| // which is their constant memory. SetDevicePointers relocates the pointers in | ||
| // place, so they are copied and the shadow is restored before the kernel runs: | ||
| // the kernel, on many host threads, never touches the shadow. | ||
| struct GPUMetalHostProcessor : public GPUProcessor { | ||
| static const GPUConstantMem*& ConstantMem(GPUProcessor& p) { return p.*&GPUMetalHostProcessor::mConstantMem; } | ||
| }; | ||
|
|
||
| template <class T, int32_t I, typename... Args> | ||
| inline void GPUReconstructionMetal::RunOnHost(const krnlSetupTime& _xyz, typename T::processorType* procs, uint32_t nProcs, uint32_t nBlocks, const Args&... args) | ||
| { | ||
| if (_xyz.z.evList) { | ||
| StreamWaitForEvents(_xyz.x.stream, _xyz.z.evList, _xyz.z.nEvents); | ||
| } | ||
| DrainStream(_xyz.x.stream); | ||
| const auto start = std::chrono::steady_clock::now(); | ||
| const auto shadow = std::make_unique<char[]>(sizeof(GPUConstantMem)); | ||
| static_assert(alignof(GPUConstantMem) <= __STDCPP_DEFAULT_NEW_ALIGNMENT__); | ||
| const auto hostMem = std::make_unique<char[]>(sizeof(GPUConstantMem)); | ||
| GPUConstantMem* host = (GPUConstantMem*)hostMem.get(); | ||
| memcpy(shadow.get(), (void*)mProcessorsShadow, sizeof(GPUConstantMem)); | ||
| for (const auto& res : mMemoryResources) { | ||
| if (res.PtrDevice()) { | ||
| res.SetDevicePointers(HostPointer(res.PtrDevice())); | ||
| } | ||
| } | ||
| memcpy((void*)host, (void*)mProcessorsShadow, sizeof(GPUConstantMem)); | ||
| for (const auto& res : mMemoryResources) { | ||
| if (res.PtrDevice()) { | ||
| res.SetDevicePointers(res.PtrDevice()); | ||
| } | ||
| } | ||
| memcpy((void*)mProcessorsShadow, shadow.get(), sizeof(GPUConstantMem)); | ||
| host->param = param(); | ||
| host->calibObjects = processors()->calibObjects; | ||
| host->errorCodes.setMemory(HostPointer(const_cast<uint32_t*>(host->errorCodes.getErrorPtr()) - 1)); | ||
| auto* hostProcs = (typename T::processorType*)((char*)host + ((char*)procs - (char*)mProcessorsShadow)); | ||
| for (uint32_t i = 0; i < nProcs; i++) { | ||
| GPUMetalHostProcessor::ConstantMem(hostProcs[i]) = host; | ||
| } | ||
| tbb::parallel_for<uint32_t>(0, nBlocks, [&](uint32_t iBlock) { | ||
| typename T::GPUSharedMemory smem; | ||
| T::template Thread<I>(nBlocks, 1, iBlock, 0, smem, hostProcs[_xyz.y.index], args...); | ||
| }); | ||
| if (GetProcessingSettings().debugLevel >= 1 && memcmp(shadow.get(), (void*)mProcessorsShadow, sizeof(GPUConstantMem))) { | ||
| GPUFatal("Metal: mProcessorsShadow changed while %s ran on the host", GetKernelName<T, I>()); | ||
| } | ||
| _xyz.t = std::chrono::duration<double>(std::chrono::steady_clock::now() - start).count(); | ||
| if (_xyz.z.ev) { | ||
| RecordMarker(_xyz.z.ev, _xyz.x.stream); | ||
| } | ||
| } | ||
|
|
||
| template <> | ||
| inline void GPUReconstructionMetal::runKernelBackend<GPUTPCGMMergerSortTracks, 0>(const krnlSetupTime& _xyz) | ||
| { | ||
| // The fit runs one track per thread, in this order. Tracks of similar length | ||
| // keep the threads of a SIMD group busy for as long as each other; within a | ||
| // length bucket of 16 clusters, tracks of the same sector and side then read | ||
| // the same pages of the cluster, transformation and calibration data, which | ||
| // the Metal GPU, bound by its address translation in the fit, needs more than | ||
| // the exact length order. | ||
| const GPUTPCGMMergedTrack* trk = HostPointer(mProcessorsShadow->tpcMerger.MergedTracks()); | ||
| auto comp = [trk](const int32_t aa, const int32_t bb) { | ||
| const GPUTPCGMMergedTrack& a = trk[aa]; | ||
| const GPUTPCGMMergedTrack& b = trk[bb]; | ||
| if (a.OK() != b.OK()) { | ||
| return a.OK(); | ||
| } | ||
| if (a.CCE() != b.CCE()) { | ||
| return a.CCE() > b.CCE(); | ||
| } | ||
| if (a.NClusters() / 16 != b.NClusters() / 16) { | ||
| return a.NClusters() / 16 > b.NClusters() / 16; | ||
| } | ||
| const bool sideA = a.GetParam().GetZ() < 0, sideB = b.GetParam().GetZ() < 0; | ||
| if (sideA != sideB || a.GetAlpha() != b.GetAlpha()) { | ||
| return sideA != sideB ? sideA < sideB : a.GetAlpha() < b.GetAlpha(); | ||
| } | ||
| return aa < bb; | ||
| }; | ||
| SortOnHost(_xyz, mProcessorsShadow->tpcMerger.TrackOrderProcess(), [&] { return HostPointer(mProcessorsShadow->tpcMerger.Memory())->nMergedTracks; }, comp); | ||
| } | ||
|
|
||
| template <> | ||
| inline void GPUReconstructionMetal::runKernelBackend<GPUTPCGMMergerSortTracksQPt, 0>(const krnlSetupTime& _xyz) | ||
| { | ||
| SortOnHost(_xyz, mProcessorsShadow->tpcMerger.TrackSort(), [&] { return HostPointer(mProcessorsShadow->tpcMerger.Memory())->nMergedTracks; }, internal::GPUTPCGMMergerSortTracksQPt_comp(HostPointer(mProcessorsShadow->tpcMerger.MergedTracks()))); | ||
| } | ||
|
|
||
| template <> | ||
| inline void GPUReconstructionMetal::runKernelBackend<GPUTPCGMMergerMergeLoopers, 1>(const krnlSetupTime& _xyz) | ||
| { | ||
| SortOnHost(_xyz, mProcessorsShadow->tpcMerger.LooperCandidates(), [&] { return HostPointer(mProcessorsShadow->tpcMerger.Memory())->nLooperMatchCandidates; }, internal::GPUTPCGMMergerMergeLoopers_comp()); | ||
| } | ||
|
|
||
| template <> | ||
| inline void GPUReconstructionMetal::runKernelBackend<GPUTPCGMO2Output, GPUTPCGMO2Output::sort>(const krnlSetupTime& _xyz) | ||
| { | ||
| SortOnHost(_xyz, mProcessorsShadow->tpcMerger.TrackSortO2(), [&] { return HostPointer(mProcessorsShadow->tpcMerger.Memory())->nO2Tracks; }, internal::GPUTPCGMO2OutputSort_comp()); | ||
| } | ||
|
|
||
| template <> | ||
| inline void GPUReconstructionMetal::runKernelBackend<GPUTPCGMMergerMergeBorders, 3>(const krnlSetupTime& _xyz, gputpcgmmergertypes::GPUTPCGMBorderRange* const& range, int32_t const& N, int32_t const& cmpMax) | ||
| { | ||
| if (cmpMax) { | ||
| SortOnHost(_xyz, range, [&] { return N; }, internal::MergeBorderTracks_compMax()); | ||
| } else { | ||
| SortOnHost(_xyz, range, [&] { return N; }, internal::MergeBorderTracks_compMin()); | ||
| } | ||
| } | ||
|
|
||
| template <> | ||
| inline void GPUReconstructionMetal::runKernelBackend<GPUTPCGMMergerMergeBorders, 2>(const krnlSetupTime& _xyz, int32_t const& iSector, uint8_t const& mergeMode) | ||
| { | ||
| RunOnHost<GPUTPCGMMergerMergeBorders, 2>(_xyz, &mProcessorsShadow->tpcMerger, 1, 1, iSector, mergeMode); | ||
| } | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. GPU Metal: run the extrapolation tracking on the host: |
||
| template <> | ||
| inline void GPUReconstructionMetal::runKernelBackend<GPUTPCExtrapolationTracking, 0>(const krnlSetupTime& _xyz) | ||
| { | ||
| RunOnHost<GPUTPCExtrapolationTracking, 0>(_xyz, mProcessorsShadow->tpcTrackers, GPUTPCGeometry::NSECTORS, getNKernelHostThreads(false)); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -114,4 +114,5 @@ MERGER_INTERPOLATION_ERROR_TYPE,"""half""","""float""",,,,,,,,,,,,,,,,, | |
| COMP_GATHER_KERNEL,4,0,4,4,,,,,,4,4,4,4,,4,,4,4, | ||
| COMP_GATHER_MODE,3,0,3,3,,,,,,3,3,3,3,,3,,3,3, | ||
| CF_SCAN_WORKGROUP_SIZE,512,0,,,,,,,,224,,992,448,,1024,,,448, | ||
| CF_CHECK_PAD_BASELINE_SEGMENTED,0,0,,,,,,,,,,,,,,,,,1 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if you add a parameter here, you should also add it to kernels.cmake. I am actually wondering how it works at all without being part of kernels.cmake. Probably we should add a check that all parameters are listed when parsing the json in the CMake :). |
||
| MERGER_SPLIT_LOOP_INTERPOLATION,,,,,,,,,,,,,,,1,,,1, | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -121,10 +121,23 @@ int32_t GPUChainTracking::RunTPCTrackingMerger(bool synchronizeOutput) | |
| if (GetProcessingSettings().deterministicGPUReconstruction) { | ||
| runKernel<GPUTPCGlobalDebugSortKernels, GPUTPCGlobalDebugSortKernels::clearIds>(GetGridAuto(0, deviceType), 1); | ||
| } | ||
| // Sectors only share the atomic track counter, so they can be refit concurrently, but then the sector track infos are not ordered by sector. | ||
| // Only the deterministic sort and the debug dumps need that order (SectorTrackInfoFirst / Last), so keep the serial order for them. | ||
| const uint32_t nRefitStreams = doGPU && !GetProcessingSettings().deterministicGPUReconstruction ? mRec->NStreams() : 1; | ||
| RecordMarker(&mEvents->single, 0); | ||
| for (uint32_t i = 0; i < NSECTORS; i++) { | ||
| runKernel<GPUTPCGMMergerUnpackSaveNumber>({{1, -WarpSize(), 0, deviceType}}, i); | ||
| runKernel<GPUTPCGMMergerUnpackResetIds>(GetGridAuto(0, deviceType), i); | ||
| runKernel<GPUTPCGMMergerSectorRefit>(GetGridAuto(0, deviceType), i); // TODO: Why all in stream 0? | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. GPU: refit the sector tracks of the merger on all streams: |
||
| const int32_t stream = i % nRefitStreams; | ||
| runKernel<GPUTPCGMMergerUnpackSaveNumber>({{1, -WarpSize(), stream, deviceType}, krnlRunRangeNone, {nullptr, stream && i < nRefitStreams ? &mEvents->single : nullptr}}, i); | ||
| runKernel<GPUTPCGMMergerUnpackResetIds>(GetGridAuto(stream, deviceType), i); | ||
| runKernel<GPUTPCGMMergerSectorRefit>(GetGridAuto(stream, deviceType), i); | ||
| } | ||
| ReleaseEvent(mEvents->single); | ||
| if (nRefitStreams > 1) { // Stream 0 must wait for all streams before unpacking the extrapolated tracks | ||
| const int32_t ne = std::min<int32_t>(NSECTORS, nRefitStreams) - 1; | ||
| for (int32_t j = 0; j < ne; j++) { | ||
| RecordMarker(&mEvents->sector[j], j + 1); | ||
| } | ||
| StreamWaitForEvents(0, &mEvents->sector[0], ne); | ||
| } | ||
| if (GetProcessingSettings().deterministicGPUReconstruction) { | ||
| runKernel<GPUTPCGMMergerUnpackSaveNumber>({{1, -WarpSize(), 0, deviceType}}, NSECTORS); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -63,84 +63,7 @@ using namespace o2::gpu; | |
| using namespace o2::tpc; | ||
| using namespace gputpcgmmergertypes; | ||
|
|
||
| namespace o2::gpu::internal | ||
| { | ||
| struct MergeLooperParam { | ||
| float refz; | ||
| float x; | ||
| float y; | ||
| uint32_t id; | ||
| }; | ||
|
|
||
| struct MergeBorderTracks_compMax { | ||
| GPUd() bool operator()(const GPUTPCGMBorderRange& a, const GPUTPCGMBorderRange& b) | ||
| { | ||
| return GPUCA_DETERMINISTIC_CODE((a.fMax != b.fMax) ? (a.fMax < b.fMax) : (a.fId < b.fId), a.fMax < b.fMax); | ||
| } | ||
| }; | ||
| struct MergeBorderTracks_compMin { | ||
| GPUd() bool operator()(const GPUTPCGMBorderRange& a, const GPUTPCGMBorderRange& b) | ||
| { | ||
| return GPUCA_DETERMINISTIC_CODE((a.fMin != b.fMin) ? (a.fMin < b.fMin) : (a.fId < b.fId), a.fMin < b.fMin); | ||
| } | ||
| }; | ||
|
|
||
| struct GPUTPCGMMergerSortTracks_comp { | ||
| const GPUTPCGMMergedTrack* const mCmp; | ||
| GPUhd() GPUTPCGMMergerSortTracks_comp(GPUTPCGMMergedTrack* cmp) : mCmp(cmp) {} | ||
| GPUd() bool operator()(const int32_t aa, const int32_t bb) | ||
| { | ||
| const GPUTPCGMMergedTrack& GPUrestrict() a = mCmp[aa]; | ||
| const GPUTPCGMMergedTrack& GPUrestrict() b = mCmp[bb]; | ||
| if (a.OK() != b.OK()) { | ||
| return a.OK(); | ||
| } | ||
| if (a.CCE() != b.CCE()) { | ||
| return a.CCE() > b.CCE(); | ||
| } | ||
| GPUCA_DETERMINISTIC_CODE( // clang-format off | ||
| if (a.NClusters() != b.NClusters()) { | ||
| return a.NClusters() > b.NClusters(); | ||
| } if (CAMath::Abs(a.GetParam().GetQPt()) != CAMath::Abs(b.GetParam().GetQPt())) { | ||
| return CAMath::Abs(a.GetParam().GetQPt()) > CAMath::Abs(b.GetParam().GetQPt()); | ||
| } if (a.GetParam().GetY() != b.GetParam().GetY()) { | ||
| return a.GetParam().GetY() > b.GetParam().GetY(); | ||
| } | ||
| return aa > bb; | ||
| , // !GPUCA_DETERMINISTIC_CODE | ||
| return a.NClusters() > b.NClusters(); | ||
| ) // clang-format on | ||
| } | ||
| }; | ||
|
|
||
| struct GPUTPCGMMergerSortTracksQPt_comp { | ||
| const GPUTPCGMMergedTrack* const mCmp; | ||
| GPUhd() GPUTPCGMMergerSortTracksQPt_comp(GPUTPCGMMergedTrack* cmp) : mCmp(cmp) {} | ||
| GPUd() bool operator()(const int32_t aa, const int32_t bb) | ||
| { | ||
| const GPUTPCGMMergedTrack& GPUrestrict() a = mCmp[aa]; | ||
| const GPUTPCGMMergedTrack& GPUrestrict() b = mCmp[bb]; | ||
| GPUCA_DETERMINISTIC_CODE( // clang-format off | ||
| if (CAMath::Abs(a.GetParam().GetQPt()) != CAMath::Abs(b.GetParam().GetQPt())) { | ||
| return CAMath::Abs(a.GetParam().GetQPt()) > CAMath::Abs(b.GetParam().GetQPt()); | ||
| } if (a.GetParam().GetY() != b.GetParam().GetY()) { | ||
| return a.GetParam().GetY() > b.GetParam().GetY(); | ||
| } | ||
| return a.GetParam().GetZ() > b.GetParam().GetZ(); | ||
| , // !GPUCA_DETERMINISTIC_CODE | ||
| return CAMath::Abs(a.GetParam().GetQPt()) > CAMath::Abs(b.GetParam().GetQPt()); | ||
| ) // clang-format on | ||
| } | ||
| }; | ||
|
|
||
| struct GPUTPCGMMergerMergeLoopers_comp { | ||
| GPUd() bool operator()(const MergeLooperParam& a, const MergeLooperParam& b) | ||
| { | ||
| return GPUCA_DETERMINISTIC_CODE(CAMath::Abs(a.refz) != CAMath::Abs(b.refz) ? CAMath::Abs(a.refz) < CAMath::Abs(b.refz) : a.id < b.id, CAMath::Abs(a.refz) < CAMath::Abs(b.refz)); | ||
| } | ||
| }; | ||
|
|
||
| } // namespace o2::gpu::internal | ||
| #include "GPUTPCGMMergerSortComp.h" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. GPU: put the orderings of the merger's sorts in a header + GPU Metal: run the merger's sorts on the host: But after thinking about it, there might be a better solution: You can call the host sort already in runKernel via the I'd add a setting GPU_UNIFIED_MEM_SORT_ON_HOST to GPUParameters.csv and kernels.cmake, you can move your SortOnHost function to GPUReconstructionDevice.cxx, to be available for all backends, and then switch the krnlExec::device similarly to e.g. |
||
|
|
||
| using namespace o2::gpu::internal; | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GPU Metal: fit the tracks grouped by sector within length buckets
I believe this breaks the deterministic mode, if 2 tracks have the same z (That is why struct GPUTPCGMMergerSortTracks_comp { in GPUTPCGMMerger.cxx checks both GetY and GetQPt, to have multiple float values (and even that is not save).
Generally, I don't like to specialize here, since it is code copy and paste, and can break if the normal sort function is changed.
From what I understand, you add a check for TPC side and sector, which makes sense, and will also not hurt other backends. So I'd just inset the part
before the final
GPUCA_DETERMINISTIC_CODE( // clang-format off