Skip to content

Commit

Permalink
vrtprocesseddataset.cpp: factor code common to 2 recent pull requests
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Jan 6, 2025
1 parent 615fe1e commit 44ff97a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 44 deletions.
1 change: 1 addition & 0 deletions autotest/gdrivers/vrtprocesseddataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1514,5 +1514,6 @@ def test_vrtprocesseddataset_RasterIO(tmp_vsimem):

with gdal.config_option("VRT_PROCESSED_DATASET_ALLOWED_RAM_USAGE", "96"):
ds = gdal.Open(vrt_content)
assert ds.GetRasterBand(1).GetBlockSize() == [1, 1]
with pytest.raises(Exception):
ds.ReadAsArray()
70 changes: 26 additions & 44 deletions frmts/vrt/vrtprocesseddataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -538,32 +538,39 @@ CPLErr VRTProcessedDataset::Init(const CPLXMLNode *psTree,
return CE_Failure;
}

int nLargestInDTSizeTimesBand = 1;
int nLargestOutDTSizeTimesBand = 1;
for (const auto &oStep : m_aoSteps)
{
const int nInDTSizeTimesBand =
GDALGetDataTypeSizeBytes(oStep.eInDT) * oStep.nInBands;
nLargestInDTSizeTimesBand =
std::max(nLargestInDTSizeTimesBand, nInDTSizeTimesBand);
const int nOutDTSizeTimesBand =
GDALGetDataTypeSizeBytes(oStep.eOutDT) * oStep.nOutBands;
nLargestOutDTSizeTimesBand =
std::max(nLargestOutDTSizeTimesBand, nOutDTSizeTimesBand);
}
m_nWorkingBytesPerPixel =
nLargestInDTSizeTimesBand + nLargestOutDTSizeTimesBand;

// Use only up to 40% of RAM to acquire source bands and generate the output
// buffer.
const auto nMaxMemAllowed = CPLGetUsablePhysicalRAM() / 10 * 4;
if (nMaxMemAllowed > 0)
m_nAllowedRAMUsage = CPLGetUsablePhysicalRAM() / 10 * 4;
// Only for tests now
const char *pszMAX_RAM = "VRT_PROCESSED_DATASET_ALLOWED_RAM_USAGE";
if (const char *pszVal = CPLGetConfigOption(pszMAX_RAM, nullptr))
{
int nLargestInDTSizeTimesBand = 1;
int nLargestOutDTSizeTimesBand = 1;
for (const auto &oStep : m_aoSteps)
{
const int nInDTSizeTimesBand =
GDALGetDataTypeSizeBytes(oStep.eInDT) * oStep.nInBands;
nLargestInDTSizeTimesBand =
std::max(nLargestInDTSizeTimesBand, nInDTSizeTimesBand);
const int nOutDTSizeTimesBand =
GDALGetDataTypeSizeBytes(oStep.eOutDT) * oStep.nOutBands;
nLargestOutDTSizeTimesBand =
std::max(nLargestOutDTSizeTimesBand, nOutDTSizeTimesBand);
}

const int nPerPixelSize =
nLargestInDTSizeTimesBand + nLargestOutDTSizeTimesBand;
CPL_IGNORE_RET_VAL(
CPLParseMemorySize(pszVal, &m_nAllowedRAMUsage, nullptr));
}

if (m_nAllowedRAMUsage > 0)
{
bool bBlockSizeModified = false;
while ((m_nBlockXSize >= 2 || m_nBlockYSize >= 2) &&
static_cast<GIntBig>(m_nBlockXSize) * m_nBlockYSize >
nMaxMemAllowed / nPerPixelSize)
m_nAllowedRAMUsage / m_nWorkingBytesPerPixel)
{
if ((m_nBlockXSize == nRasterXSize ||
m_nBlockYSize >= m_nBlockXSize) &&
Expand Down Expand Up @@ -670,31 +677,6 @@ CPLErr VRTProcessedDataset::Init(const CPLXMLNode *psTree,

m_oXMLTree.reset(CPLCloneXMLTree(psTree));

int nLargestInDTSizeTimesBand = 1;
int nLargestOutDTSizeTimesBand = 1;
for (const auto &oStep : m_aoSteps)
{
const int nInDTSizeTimesBand =
GDALGetDataTypeSizeBytes(oStep.eInDT) * oStep.nInBands;
nLargestInDTSizeTimesBand =
std::max(nLargestInDTSizeTimesBand, nInDTSizeTimesBand);
const int nOutDTSizeTimesBand =
GDALGetDataTypeSizeBytes(oStep.eOutDT) * oStep.nOutBands;
nLargestOutDTSizeTimesBand =
std::max(nLargestOutDTSizeTimesBand, nOutDTSizeTimesBand);
}
m_nWorkingBytesPerPixel =
nLargestInDTSizeTimesBand + nLargestOutDTSizeTimesBand;

m_nAllowedRAMUsage = CPLGetUsablePhysicalRAM() / 10 * 4;
// Only for tests now
const char *pszMAX_RAM = "VRT_PROCESSED_DATASET_ALLOWED_RAM_USAGE";
if (const char *pszVal = CPLGetConfigOption(pszMAX_RAM, nullptr))
{
CPL_IGNORE_RET_VAL(
CPLParseMemorySize(pszVal, &m_nAllowedRAMUsage, nullptr));
}

return CE_None;
}

Expand Down

0 comments on commit 44ff97a

Please sign in to comment.