Improve jpeg conversion

This commit is contained in:
2026-04-16 10:59:28 +10:00
parent 6c72751a14
commit 8bb4f49b09
2 changed files with 39 additions and 10 deletions

View File

@@ -246,7 +246,25 @@ namespace ANSCENTER
return jpegStr;
}
// ── NvJpegPool: fixed pool of 4 GPU encoders, lock-free acquire ──
// ── NvJpegPool: VRAM-scaled pool of GPU encoders, lock-free acquire ──
int NvJpegPool::detectPoolSize() {
// Query VRAM via CUDA and scale: 1 encoder per 2 GB, min 1
int deviceCount = 0;
if (cudaGetDeviceCount(&deviceCount) != cudaSuccess || deviceCount <= 0)
return 0;
cudaDeviceProp prop{};
if (cudaGetDeviceProperties(&prop, 0) != cudaSuccess)
return 0;
size_t vramGB = prop.totalGlobalMem / (1024ULL * 1024ULL * 1024ULL);
int pool = static_cast<int>(vramGB / 2);
if (pool < 1) pool = 1;
ANS_DBG("ANSCV", "NvJpegPool: GPU=%s, VRAM=%zuGB, poolSize=%d", prop.name, vramGB, pool);
return pool;
}
NvJpegPool& NvJpegPool::Instance() {
static NvJpegPool instance;
@@ -256,7 +274,13 @@ namespace ANSCENTER
NvJpegPool::NvJpegPool() {
if (!anscv_vendor_gate::IsNvidiaGpuAvailable()) return;
for (int i = 0; i < kPoolSize; ++i) {
_poolSize = detectPoolSize();
if (_poolSize <= 0) return;
_encoders.resize(_poolSize);
_inUse = std::make_unique<std::atomic<bool>[]>(_poolSize);
for (int i = 0; i < _poolSize; ++i) {
_inUse[i].store(false, std::memory_order_relaxed);
_encoders[i] = std::make_unique<NvJpegCompressor>();
if (!_encoders[i]->isValid()) {
@@ -264,16 +288,18 @@ namespace ANSCENTER
}
}
// Pool is available if at least one encoder initialized
for (int i = 0; i < kPoolSize; ++i) {
for (int i = 0; i < _poolSize; ++i) {
if (_encoders[i]) { _available = true; break; }
}
ANS_DBG("ANSCV", "NvJpegPool: initialized %d encoder(s), available=%d", _poolSize, _available ? 1 : 0);
}
std::string NvJpegPool::tryCompress(const cv::Mat& image, int quality) {
if (!_available) return "";
// Lock-free slot acquisition: try each slot with compare_exchange
for (int i = 0; i < kPoolSize; ++i) {
for (int i = 0; i < _poolSize; ++i) {
if (!_encoders[i]) continue;
bool expected = false;
if (_inUse[i].compare_exchange_strong(expected, true, std::memory_order_acquire)) {