Skip to content

Commit 281505d

Browse files
committed
GPU: use ptrDiff function
1 parent 6730e74 commit 281505d

File tree

4 files changed

+28
-28
lines changed

4 files changed

+28
-28
lines changed

GPU/GPUTracking/Base/GPUParam.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ void GPUParam::LoadClusterErrors(bool Print)
267267

268268
void GPUParamRTC::setFrom(const GPUParam& param)
269269
{
270-
memcpy((char*)this, (char*)&param, sizeof(param));
270+
memcpy((void*)this, (void*)&param, sizeof(param));
271271
}
272272

273273
std::string GPUParamRTC::generateRTCCode(const GPUParam& param, bool useConstexpr)

GPU/GPUTracking/Base/GPUParam.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,12 @@ struct GPUParam : public internal::GPUParam_t<GPUSettingsRec, GPUSettingsParam>
101101
return 0.174533f + par.dAlpha * iSlice;
102102
}
103103
GPUd() float GetClusterErrorSeeding(int32_t yz, int32_t type, float zDiff, float angle2, float unscaledMult) const;
104-
GPUd() void GetClusterErrorsSeeding2(char sector, int32_t row, float z, float sinPhi, float DzDs, float time, float& ErrY2, float& ErrZ2) const;
104+
GPUd() void GetClusterErrorsSeeding2(uint8_t sector, int32_t row, float z, float sinPhi, float DzDs, float time, float& ErrY2, float& ErrZ2) const;
105105
GPUd() float GetSystematicClusterErrorIFC2(float trackX, float trackY, float z, bool sideC) const;
106-
GPUd() float GetSystematicClusterErrorC122(float trackX, float trackY, char sector) const;
106+
GPUd() float GetSystematicClusterErrorC122(float trackX, float trackY, uint8_t sector) const;
107107

108108
GPUd() float GetClusterError2(int32_t yz, int32_t type, float zDiff, float angle2, float unscaledMult, float scaledAvgInvCharge, float scaledInvCharge) const;
109-
GPUd() void GetClusterErrors2(char sector, int32_t row, float z, float sinPhi, float DzDs, float time, float avgInvCharge, float invCharge, float& ErrY2, float& ErrZ2) const;
109+
GPUd() void GetClusterErrors2(uint8_t sector, int32_t row, float z, float sinPhi, float DzDs, float time, float avgInvCharge, float invCharge, float& ErrY2, float& ErrZ2) const;
110110
GPUd() void UpdateClusterError2ByState(int16_t clusterState, float& ErrY2, float& ErrZ2) const;
111111
GPUd() float GetUnscaledMult(float time) const;
112112

GPU/GPUTracking/Base/GPUParam.inc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ GPUdi() void MEM_LG(GPUParam)::Global2Slice(int32_t iSlice, float X, float Y, fl
4747
#ifdef GPUCA_TPC_GEOMETRY_O2
4848

4949
MEM_CLASS_PRE()
50-
GPUdi() void MEM_LG(GPUParam)::GetClusterErrorsSeeding2(char sector, int32_t iRow, float z, float sinPhi, float DzDs, float time, float& ErrY2, float& ErrZ2) const
50+
GPUdi() void MEM_LG(GPUParam)::GetClusterErrorsSeeding2(uint8_t sector, int32_t iRow, float z, float sinPhi, float DzDs, float time, float& ErrY2, float& ErrZ2) const
5151
{
5252
const int32_t rowType = tpcGeometry.GetROC(iRow);
5353
z = CAMath::Abs(tpcGeometry.TPCLength() - CAMath::Abs(z));
@@ -119,7 +119,7 @@ GPUdi() float MEM_LG(GPUParam)::GetSystematicClusterErrorIFC2(float x, float y,
119119
}
120120

121121
MEM_CLASS_PRE()
122-
GPUdi() float MEM_LG(GPUParam)::GetSystematicClusterErrorC122(float x, float y, char sector) const
122+
GPUdi() float MEM_LG(GPUParam)::GetSystematicClusterErrorC122(float x, float y, uint8_t sector) const
123123
{
124124
const float dx = x - 83.f;
125125
if (dx > occupancyTotal * rec.tpc.sysClusErrorC12Box) {
@@ -143,7 +143,7 @@ GPUdi() float MEM_LG(GPUParam)::GetClusterErrorSeeding(int32_t yz, int32_t type,
143143
}
144144

145145
MEM_CLASS_PRE()
146-
GPUdi() void MEM_LG(GPUParam)::GetClusterErrorsSeeding2(char sector, int32_t iRow, float z, float sinPhi, float DzDs, float time, float& ErrY2, float& ErrZ2) const
146+
GPUdi() void MEM_LG(GPUParam)::GetClusterErrorsSeeding2(uint8_t sector, int32_t iRow, float z, float sinPhi, float DzDs, float time, float& ErrY2, float& ErrZ2) const
147147
{
148148
int32_t rowType = tpcGeometry.GetROC(iRow);
149149
z = CAMath::Abs(tpcGeometry.TPCLength() - CAMath::Abs(z));
@@ -179,15 +179,15 @@ GPUdi() float MEM_LG(GPUParam)::GetSystematicClusterErrorIFC2(float trackX, floa
179179
}
180180

181181
MEM_CLASS_PRE()
182-
GPUdi() float MEM_LG(GPUParam)::GetSystematicClusterErrorC122(float trackX, float trackY, char sector) const
182+
GPUdi() float MEM_LG(GPUParam)::GetSystematicClusterErrorC122(float trackX, float trackY, uint8_t sector) const
183183
{
184184
return 0;
185185
}
186186

187187
#endif // !GPUCA_TPC_GEOMETRY_O2
188188

189189
MEM_CLASS_PRE()
190-
GPUdi() void MEM_LG(GPUParam)::GetClusterErrors2(char sector, int32_t iRow, float z, float sinPhi, float DzDs, float time, float avgInvCharge, float invCharge, float& ErrY2, float& ErrZ2) const
190+
GPUdi() void MEM_LG(GPUParam)::GetClusterErrors2(uint8_t sector, int32_t iRow, float z, float sinPhi, float DzDs, float time, float avgInvCharge, float invCharge, float& ErrY2, float& ErrZ2) const
191191
{
192192
const int32_t rowType = tpcGeometry.GetROC(iRow);
193193
z = CAMath::Abs(tpcGeometry.TPCLength() - CAMath::Abs(z));

GPU/GPUTracking/Base/GPUReconstruction.cxx

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ int32_t GPUReconstruction::Init()
166166
for (uint32_t i = 0; i < mSlaves.size(); i++) {
167167
mSlaves[i]->mDeviceMemoryBase = mDeviceMemoryPermanent;
168168
mSlaves[i]->mHostMemoryBase = mHostMemoryPermanent;
169-
mSlaves[i]->mDeviceMemorySize = mDeviceMemorySize - ((char*)mSlaves[i]->mDeviceMemoryBase - (char*)mDeviceMemoryBase);
170-
mSlaves[i]->mHostMemorySize = mHostMemorySize - ((char*)mSlaves[i]->mHostMemoryBase - (char*)mHostMemoryBase);
169+
mSlaves[i]->mDeviceMemorySize = mDeviceMemorySize - ptrDiff(mSlaves[i]->mDeviceMemoryBase, mDeviceMemoryBase);
170+
mSlaves[i]->mHostMemorySize = mHostMemorySize - ptrDiff(mSlaves[i]->mHostMemoryBase, mHostMemoryBase);
171171
mSlaves[i]->mHostMemoryPoolEnd = mHostMemoryPoolEnd;
172172
mSlaves[i]->mDeviceMemoryPoolEnd = mDeviceMemoryPoolEnd;
173173
if (mSlaves[i]->InitDevice()) {
@@ -437,7 +437,7 @@ void GPUReconstruction::WriteConstantParams()
437437
{
438438
if (IsGPU()) {
439439
const auto threadContext = GetThreadContext();
440-
WriteToConstantMemory((char*)&processors()->param - (char*)processors(), &param(), sizeof(param()), -1);
440+
WriteToConstantMemory(ptrDiff(&processors()->param, processors()), &param(), sizeof(param()), -1);
441441
}
442442
}
443443

@@ -491,7 +491,7 @@ void GPUReconstruction::ComputeReuseMax(GPUProcessor* proc)
491491
resMain.mOverrideSize = 0;
492492
for (uint32_t i = 0; i < re.res.size(); i++) {
493493
GPUMemoryResource& res = mMemoryResources[re.res[i]];
494-
resMain.mOverrideSize = std::max<size_t>(resMain.mOverrideSize, (char*)res.SetPointers((void*)1) - (char*)1);
494+
resMain.mOverrideSize = std::max<size_t>(resMain.mOverrideSize, ptrDiff(res.SetPointers((void*)1), (char*)1));
495495
}
496496
}
497497
}
@@ -545,7 +545,7 @@ size_t GPUReconstruction::AllocateRegisteredMemoryHelper(GPUMemoryResource* res,
545545
GPUError("Invalid reuse ptr (%s)", res->mName);
546546
throw std::bad_alloc();
547547
}
548-
size_t retVal = (char*)((res->*setPtr)(ptr)) - (char*)(ptr);
548+
size_t retVal = ptrDiff((res->*setPtr)(ptr), ptr);
549549
if (retVal > mMemoryResources[res->mReuse].mSize) {
550550
GPUError("Insufficient reuse memory %lu < %lu (%s) (%s)", mMemoryResources[res->mReuse].mSize, retVal, res->mName, device);
551551
throw std::bad_alloc();
@@ -561,31 +561,31 @@ size_t GPUReconstruction::AllocateRegisteredMemoryHelper(GPUMemoryResource* res,
561561
}
562562
size_t retVal;
563563
if ((res->mType & GPUMemoryResource::MEMORY_STACK) && memorypoolend) {
564-
retVal = (char*)((res->*setPtr)((char*)1)) - (char*)(1);
564+
retVal = ptrDiff((res->*setPtr)((char*)1), (char*)(1));
565565
memorypoolend = (void*)((char*)memorypoolend - GPUProcessor::getAlignmentMod<GPUCA_MEMALIGN>(memorypoolend));
566566
if (retVal < res->mOverrideSize) {
567567
retVal = res->mOverrideSize;
568568
}
569569
retVal += GPUProcessor::getAlignment<GPUCA_MEMALIGN>(retVal);
570570
memorypoolend = (char*)memorypoolend - retVal;
571571
ptr = memorypoolend;
572-
retVal = std::max<size_t>((char*)((res->*setPtr)(ptr)) - (char*)ptr, res->mOverrideSize);
572+
retVal = std::max<size_t>(ptrDiff((res->*setPtr)(ptr), ptr), res->mOverrideSize);
573573
} else {
574574
ptr = memorypool;
575575
memorypool = (char*)((res->*setPtr)(ptr));
576-
retVal = (char*)memorypool - (char*)ptr;
576+
retVal = ptrDiff(memorypool, ptr);
577577
if (retVal < res->mOverrideSize) {
578578
retVal = res->mOverrideSize;
579579
memorypool = (char*)ptr + res->mOverrideSize;
580580
}
581581
memorypool = (void*)((char*)memorypool + GPUProcessor::getAlignment<GPUCA_MEMALIGN>(memorypool));
582582
}
583-
if (memorypoolend ? (memorypool > memorypoolend) : ((size_t)((char*)memorypool - (char*)memorybase) > memorysize)) {
584-
std::cerr << "Memory pool size exceeded (" << device << ") (" << res->mName << ": " << (memorypoolend ? (memorysize + ((char*)memorypool - (char*)memorypoolend)) : (char*)memorypool - (char*)memorybase) << " < " << memorysize << "\n";
583+
if (memorypoolend ? (memorypool > memorypoolend) : ((size_t)ptrDiff(memorypool, memorybase) > memorysize)) {
584+
std::cerr << "Memory pool size exceeded (" << device << ") (" << res->mName << ": " << (memorypoolend ? (memorysize + ptrDiff(memorypool, memorypoolend)) : ptrDiff(memorypool, memorybase)) << " < " << memorysize << "\n";
585585
throw std::bad_alloc();
586586
}
587587
if (mProcessingSettings.allocDebugLevel >= 2) {
588-
std::cout << "Allocated (" << device << ") " << res->mName << ": " << retVal << " - available: " << (memorypoolend ? ((char*)memorypoolend - (char*)memorypool) : (memorysize - ((char*)memorypool - (char*)memorybase))) << "\n";
588+
std::cout << "Allocated (" << device << ") " << res->mName << ": " << retVal << " - available: " << (memorypoolend ? ptrDiff(memorypoolend, memorypool) : (memorysize - ptrDiff(memorypool, memorybase))) << "\n";
589589
}
590590
return retVal;
591591
}
@@ -633,7 +633,7 @@ void GPUReconstruction::AllocateRegisteredMemoryInternal(GPUMemoryResource* res,
633633
if (control->allocator) {
634634
res->mSize = std::max((size_t)res->SetPointers((void*)1) - 1, res->mOverrideSize);
635635
res->mPtr = control->allocator(CAMath::nextMultipleOf<GPUCA_BUFFER_ALIGNMENT>(res->mSize));
636-
res->mSize = std::max<size_t>((char*)res->SetPointers(res->mPtr) - (char*)res->mPtr, res->mOverrideSize);
636+
res->mSize = std::max<size_t>(ptrDiff(res->SetPointers(res->mPtr), res->mPtr), res->mOverrideSize);
637637
if (mProcessingSettings.allocDebugLevel >= 2) {
638638
std::cout << "Allocated (from callback) " << res->mName << ": " << res->mSize << "\n";
639639
}
@@ -701,12 +701,12 @@ void* GPUReconstruction::AllocateUnmanagedMemory(size_t size, int32_t type)
701701
char* retVal;
702702
GPUProcessor::computePointerWithAlignment(pool, retVal, size);
703703
if (pool > poolend) {
704-
GPUError("Insufficient unmanaged memory: missing %lu bytes", (size_t)((char*)pool - (char*)poolend));
704+
GPUError("Insufficient unmanaged memory: missing %ld bytes", ptrDiff(pool, poolend));
705705
throw std::bad_alloc();
706706
}
707707
UpdateMaxMemoryUsed();
708708
if (mProcessingSettings.allocDebugLevel >= 2) {
709-
std::cout << "Allocated (unmanaged " << (type == GPUMemoryResource::MEMORY_GPU ? "gpu" : "host") << "): " << size << " - available: " << ((char*)poolend - (char*)pool) << "\n";
709+
std::cout << "Allocated (unmanaged " << (type == GPUMemoryResource::MEMORY_GPU ? "gpu" : "host") << "): " << size << " - available: " << ptrDiff(poolend, pool) << "\n";
710710
}
711711
return retVal;
712712
}
@@ -723,12 +723,12 @@ void* GPUReconstruction::AllocateVolatileDeviceMemory(size_t size)
723723
char* retVal;
724724
GPUProcessor::computePointerWithAlignment(mDeviceMemoryPool, retVal, size);
725725
if (mDeviceMemoryPool > mDeviceMemoryPoolEnd) {
726-
GPUError("Insufficient volatile device memory: missing %lu", (size_t)((char*)mDeviceMemoryPool - (char*)mDeviceMemoryPoolEnd));
726+
GPUError("Insufficient volatile device memory: missing %ld", ptrDiff(mDeviceMemoryPool, mDeviceMemoryPoolEnd));
727727
throw std::bad_alloc();
728728
}
729729
UpdateMaxMemoryUsed();
730730
if (mProcessingSettings.allocDebugLevel >= 2) {
731-
std::cout << "Allocated (volatile GPU): " << size << " - available: " << ((char*)mDeviceMemoryPoolEnd - (char*)mDeviceMemoryPool) << "\n";
731+
std::cout << "Allocated (volatile GPU): " << size << " - available: " << ptrDiff(mDeviceMemoryPoolEnd, mDeviceMemoryPool) << "\n";
732732
}
733733

734734
return retVal;
@@ -757,15 +757,15 @@ void GPUReconstruction::ResetRegisteredMemoryPointers(int16_t ires)
757757
GPUMemoryResource* res = &mMemoryResources[ires];
758758
if (!(res->mType & GPUMemoryResource::MEMORY_EXTERNAL) && (res->mType & GPUMemoryResource::MEMORY_HOST)) {
759759
void* basePtr = res->mReuse >= 0 ? mMemoryResources[res->mReuse].mPtr : res->mPtr;
760-
size_t size = (char*)res->SetPointers(basePtr) - (char*)basePtr;
760+
size_t size = ptrDiff(res->SetPointers(basePtr), basePtr);
761761
if (basePtr && size > std::max(res->mSize, res->mOverrideSize)) {
762762
std::cerr << "Updated pointers exceed available memory size: " << size << " > " << std::max(res->mSize, res->mOverrideSize) << " - host - " << res->mName << "\n";
763763
throw std::bad_alloc();
764764
}
765765
}
766766
if (IsGPU() && (res->mType & GPUMemoryResource::MEMORY_GPU)) {
767767
void* basePtr = res->mReuse >= 0 ? mMemoryResources[res->mReuse].mPtrDevice : res->mPtrDevice;
768-
size_t size = (char*)res->SetDevicePointers(basePtr) - (char*)basePtr;
768+
size_t size = ptrDiff(res->SetDevicePointers(basePtr), basePtr);
769769
if (basePtr && size > std::max(res->mSize, res->mOverrideSize)) {
770770
std::cerr << "Updated pointers exceed available memory size: " << size << " > " << std::max(res->mSize, res->mOverrideSize) << " - GPU - " << res->mName << "\n";
771771
throw std::bad_alloc();
@@ -806,7 +806,7 @@ void GPUReconstruction::ReturnVolatileDeviceMemory()
806806
mVolatileMemoryStart = nullptr;
807807
}
808808
if (mProcessingSettings.allocDebugLevel >= 2) {
809-
std::cout << "Freed (volatile GPU) - available: " << ((char*)mDeviceMemoryPoolEnd - (char*)mDeviceMemoryPool) << "\n";
809+
std::cout << "Freed (volatile GPU) - available: " << ptrDiff(mDeviceMemoryPoolEnd, mDeviceMemoryPool) << "\n";
810810
}
811811
}
812812

0 commit comments

Comments
 (0)