Skip to content

Commit

Permalink
Grid calculations: Free memory after data has been read
Browse files Browse the repository at this point in the history
When data has been read for a variable, free the related data to make sure the memory footprint is as low as possible.
  • Loading branch information
magnesj committed Jan 19, 2024
1 parent 9ec5304 commit 6ed63c2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
13 changes: 10 additions & 3 deletions ApplicationLibCode/ProjectDataModel/RimGridCalculation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -559,23 +559,30 @@ std::vector<double> RimGridCalculation::getDataForResult( const QString&

auto eclipseCaseData = sourceCase->eclipseCaseData();
auto rigCaseCellResultsData = eclipseCaseData->results( porosityModel );
if ( !rigCaseCellResultsData->ensureKnownResultLoaded( resAddr ) ) return {};
if ( !rigCaseCellResultsData->findOrLoadKnownScalarResultForTimeStep( resAddr, timeStepToUse ) ) return {};

// Active cell info must always be retrieved from the destination case, as the returned vector must be of the same size as number of
// active cells in the destination case.
// active cells in the destination case. Active cells can be different between source and destination case.
auto activeCellInfoDestination = destinationCase->eclipseCaseData()->activeCellInfo( porosityModel );
auto activeReservoirCells = activeCellInfoDestination->activeReservoirCellIndices();

std::vector<double> values( activeCellInfoDestination->activeReservoirCellIndices().size() );

auto resultAccessor = RigResultAccessorFactory::createFromResultAddress( eclipseCaseData, 0, porosityModel, timeStepToUse, resAddr );
size_t gridIndex = 0;
auto resultAccessor =
RigResultAccessorFactory::createFromResultAddress( eclipseCaseData, gridIndex, porosityModel, timeStepToUse, resAddr );

#pragma omp parallel for
for ( int i = 0; i < static_cast<int>( activeReservoirCells.size() ); i++ )
{
values[i] = resultAccessor->cellScalarGlobIdx( activeReservoirCells[i] );
}

auto categoriesToExclude = { RiaDefines::ResultCatType::GENERATED };

sourceCase->results( RiaDefines::PorosityModelType::MATRIX_MODEL )->freeAllocatedResultsData( categoriesToExclude, timeStepToUse );
sourceCase->results( RiaDefines::PorosityModelType::FRACTURE_MODEL )->freeAllocatedResultsData( categoriesToExclude, timeStepToUse );

return values;
}

Expand Down
12 changes: 8 additions & 4 deletions ApplicationLibCode/ReservoirDataModel/RigCaseCellResultsData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -742,10 +742,14 @@ void RigCaseCellResultsData::freeAllocatedResultsData( std::vector<RiaDefines::R
}

auto& dataForTimeStep = m_cellScalarResults[resultIdx][index];
// Using swap with an empty vector as that is the safest way to really get rid of the allocated data in a
// vector
std::vector<double> empty;
dataForTimeStep.swap( empty );

if ( !dataForTimeStep.empty() )
{
// Using swap with an empty vector as that is the safest way to really get rid of the allocated data in a
// vector
std::vector<double> empty;
dataForTimeStep.swap( empty );
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,12 @@ class RigCaseCellResultsData : public cvf::Object
QString makeResultNameUnique( const QString& resultNameProposal ) const;

bool ensureKnownResultLoaded( const RigEclipseResultAddress& resultAddress );

bool findAndLoadResultByName( const QString& resultName, const std::vector<RiaDefines::ResultCatType>& resultCategorySearchOrder );

// Load result for a single time step. This can be used to process data for a single time step
// Other data access functions assume all time steps are loaded at the same time
size_t findOrLoadKnownScalarResultForTimeStep( const RigEclipseResultAddress& resVarAddr, size_t timeStepIndex );

bool hasResultEntry( const RigEclipseResultAddress& resultAddress ) const;
bool isResultLoaded( const RigEclipseResultAddress& resultAddress ) const;
void createResultEntry( const RigEclipseResultAddress& resultAddress, bool needsToBeStored );
Expand Down Expand Up @@ -167,7 +170,6 @@ class RigCaseCellResultsData : public cvf::Object
friend class RigOilVolumeResultCalculator;
friend class RigCellVolumeResultCalculator;
friend class RigCellsWithNncsCalculator;
size_t findOrLoadKnownScalarResultForTimeStep( const RigEclipseResultAddress& resVarAddr, size_t timeStepIndex );

size_t findOrCreateScalarResultIndex( const RigEclipseResultAddress& resVarAddr, bool needsToBeStored );

Expand Down

0 comments on commit 6ed63c2

Please sign in to comment.