Skip to content

Commit 8dc83c1

Browse files
committed
GPU: Add cache folder and file lock options to RTC
1 parent 8387a8c commit 8dc83c1

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

GPU/GPUTracking/Base/cuda/GPUReconstructionCUDAGenRTC.cxx

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
#ifdef GPUCA_HAVE_O2HEADERS
2424
#include "Framework/SHA1.h"
2525
#endif
26+
#include <sys/stat.h>
27+
#include <fcntl.h>
28+
#include <filesystem>
2629

2730
using namespace GPUCA_NAMESPACE::gpu;
2831

@@ -62,11 +65,27 @@ int GPUReconstructionCUDA::genRTC(std::string& filename, unsigned int& nCompile)
6265

6366
nCompile = mProcessingSettings.rtc.compilePerKernel ? kernels.size() : 1;
6467
bool cacheLoaded = false;
68+
int fd = 0;
6569
if (mProcessingSettings.rtc.cacheOutput) {
70+
if (mProcessingSettings.RTCcacheFolder != ".") {
71+
std::filesystem::create_directories(mProcessingSettings.RTCcacheFolder);
72+
}
6673
#ifndef GPUCA_HAVE_O2HEADERS
6774
throw std::runtime_error("Cannot use RTC cache without O2 headers");
6875
#else
69-
FILE* fp = fopen("rtc.cuda.cache", "rb");
76+
if (mProcessingSettings.rtc.cacheMutex) {
77+
mode_t mask = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
78+
fd = open((mProcessingSettings.RTCcacheFolder + "/cache.lock").c_str(), O_RDWR | O_CREAT | O_CLOEXEC, mask);
79+
if (fd == -1) {
80+
throw std::runtime_error("Error opening rtc cache mutex lock file");
81+
}
82+
fchmod(fd, mask);
83+
if (lockf(fd, F_LOCK, 0)) {
84+
throw std::runtime_error("Error locking rtc cache mutex file");
85+
}
86+
}
87+
88+
FILE* fp = fopen((mProcessingSettings.RTCcacheFolder + "/rtc.cuda.cache").c_str(), "rb");
7089
char sharead[20];
7190
if (fp) {
7291
size_t len;
@@ -100,6 +119,7 @@ int GPUReconstructionCUDA::genRTC(std::string& filename, unsigned int& nCompile)
100119
break;
101120
}
102121
GPUSettingsProcessingRTC cachedSettings;
122+
static_assert(std::is_trivially_copyable_v<GPUSettingsProcessingRTC> == true, "GPUSettingsProcessingRTC must be POD");
103123
if (fread(&cachedSettings, sizeof(cachedSettings), 1, fp) != 1) {
104124
throw std::runtime_error("Cache file corrupt");
105125
}
@@ -183,7 +203,7 @@ int GPUReconstructionCUDA::genRTC(std::string& filename, unsigned int& nCompile)
183203
}
184204
#ifdef GPUCA_HAVE_O2HEADERS
185205
if (mProcessingSettings.rtc.cacheOutput) {
186-
FILE* fp = fopen("rtc.cuda.cache", "w+b");
206+
FILE* fp = fopen((mProcessingSettings.RTCcacheFolder + "/rtc.cuda.cache").c_str(), "w+b");
187207
if (fp == nullptr) {
188208
throw std::runtime_error("Cannot open cache file for writing");
189209
}
@@ -218,9 +238,16 @@ int GPUReconstructionCUDA::genRTC(std::string& filename, unsigned int& nCompile)
218238
}
219239
}
220240
fclose(fp);
241+
221242
}
222243
#endif
223244
}
245+
if (mProcessingSettings.rtc.cacheOutput && mProcessingSettings.rtc.cacheMutex) {
246+
if (lockf(fd, F_ULOCK, 0)) {
247+
throw std::runtime_error("Error unlocking RTC cache mutex file");
248+
}
249+
close(fd);
250+
}
224251

225252
#endif
226253
return 0;

GPU/GPUTracking/Definitions/GPUSettingsList.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,15 @@ AddSubConfig(GPUSettingsRecTRD, trd)
191191
AddHelp("help", 'h')
192192
EndConfig()
193193

194+
#ifndef __OPENCL__
194195
// Settings steering the processing once the device was selected, only available on the host
195196
BeginSubConfig(GPUSettingsProcessingRTC, rtc, configStandalone.proc, "RTC", 0, "Processing settings", proc_rtc)
196197
AddOption(cacheOutput, bool, false, "", 0, "Cache RTC compilation results")
197198
AddOption(optConstexpr, bool, true, "", 0, "Replace constant variables by static constexpr expressions")
198199
AddOption(compilePerKernel, bool, true, "", 0, "Run one RTC compilation per kernel")
199200
AddOption(enable, bool, false, "", 0, "Use RTC to optimize GPU code")
200201
AddOption(runTest, int, 0, "", 0, "Do not run the actual benchmark, but just test RTC compilation (1 full test, 2 test only compilation)")
202+
AddOption(cacheMutex, bool, true, "", 0, "Use a file lock to serialize access to the cache folder")
201203
AddHelp("help", 'h')
202204
EndConfig()
203205

@@ -276,11 +278,13 @@ AddOption(tpcSingleSector, int, -1, "", 0, "Restrict TPC processing to a single
276278
AddOption(tpcDownscaledEdx, unsigned char, 0, "", 0, "If != 0, downscale dEdx processing (if enabled) to x %")
277279
AddOption(tpcMaxAttachedClustersPerSectorRow, unsigned int, 51000, "", 0, "Maximum number of TPC attached clusters which can be decoded per SectorRow")
278280
AddOption(tpcUseOldCPUDecoding, bool, false, "", 0, "Enable old CPU-based TPC decoding")
281+
AddOption(RTCcacheFolder, std::string, "./rtccache/", "", 0, "Folder in which the cache file is stored")
279282
AddVariable(eventDisplay, GPUCA_NAMESPACE::gpu::GPUDisplayFrontendInterface*, nullptr)
280283
AddSubConfig(GPUSettingsProcessingRTC, rtc)
281284
AddSubConfig(GPUSettingsProcessingParam, param)
282285
AddHelp("help", 'h')
283286
EndConfig()
287+
#endif // __OPENCL__
284288

285289
#ifndef GPUCA_GPUCODE_DEVICE
286290
// Light settings concerning the event display (can be changed without rebuilding vertices)

GPU/GPUTracking/Standalone/Benchmark/standalone.cxx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,8 @@ int SetupReconstruction()
297297
GPUSettingsGRP grp = rec->GetGRPSettings();
298298
GPUSettingsRec recSet;
299299
GPUSettingsProcessing procSet;
300-
memcpy((void*)&recSet, (void*)&configStandalone.rec, sizeof(GPUSettingsRec));
301-
memcpy((void*)&procSet, (void*)&configStandalone.proc, sizeof(GPUSettingsProcessing));
300+
recSet = configStandalone.rec;
301+
procSet = configStandalone.proc;
302302
GPURecoStepConfiguration steps;
303303

304304
if (configStandalone.eventGenerator) {

GPU/Workflow/src/GPUWorkflowSpec.cxx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,11 +321,11 @@ void GPURecoWorkflowSpec::init(InitContext& ic)
321321
mode_t mask = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
322322
fd = open("/tmp/o2_gpu_memlock_mutex.lock", O_RDWR | O_CREAT | O_CLOEXEC, mask);
323323
if (fd == -1) {
324-
throw std::runtime_error("Error opening lock file");
324+
throw std::runtime_error("Error opening memlock mutex lock file");
325325
}
326326
fchmod(fd, mask);
327327
if (lockf(fd, F_LOCK, 0)) {
328-
throw std::runtime_error("Error locking file");
328+
throw std::runtime_error("Error locking memlock mutex file");
329329
}
330330
}
331331
std::chrono::time_point<std::chrono::high_resolution_clock> start, end;
@@ -342,7 +342,7 @@ void GPURecoWorkflowSpec::init(InitContext& ic)
342342
}
343343
if (mConfParam->mutexMemReg) {
344344
if (lockf(fd, F_ULOCK, 0)) {
345-
throw std::runtime_error("Error unlocking file");
345+
throw std::runtime_error("Error unlocking memlock mutex file");
346346
}
347347
close(fd);
348348
}

0 commit comments

Comments
 (0)