Skip to content

Commit

Permalink
Merge pull request #1511 from alicevision/dev/texturingLimitMemory
Browse files Browse the repository at this point in the history
add memory use constraint in texturing
  • Loading branch information
cbentejac authored Aug 18, 2023
2 parents d6a3a33 + d3111d7 commit 9541da0
Show file tree
Hide file tree
Showing 11 changed files with 21 additions and 11 deletions.
1 change: 0 additions & 1 deletion src/aliceVision/cmdline/cmdline.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "cmdline.hpp"

#include <aliceVision/system/cpu.hpp>
#include <aliceVision/system/MemoryInfo.hpp>
#include <aliceVision/alicevision_omp.hpp>

namespace aliceVision {
Expand Down
5 changes: 2 additions & 3 deletions src/aliceVision/mesh/Texturing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "UVAtlas.hpp"

#include <aliceVision/system/Logger.hpp>
#include <aliceVision/system/MemoryInfo.hpp>
#include <aliceVision/image/io.hpp>
#include <aliceVision/image/pixelTypes.hpp>
#include <aliceVision/numeric/numeric.hpp>
Expand Down Expand Up @@ -285,6 +284,7 @@ void Texturing::updateAtlases()

void Texturing::generateTextures(const mvsUtils::MultiViewParams& mp,
const boost::filesystem::path& outPath,
size_t memoryAvailable,
image::EImageFileType textureFileType)
{
// Ensure that contribution levels do not contain 0 and are sorted (as each frequency band contributes to lower bands).
Expand Down Expand Up @@ -312,15 +312,14 @@ void Texturing::generateTextures(const mvsUtils::MultiViewParams& mp,
ALICEVISION_LOG_INFO("Images loaded from cache with: " + ECorrectEV_enumToString(texParams.correctEV));

//calculate the maximum number of atlases in memory in MB
system::MemoryInfo memInfo = system::getMemoryInfo();
const std::size_t imageMaxMemSize =
mp.getMaxImageWidth() * mp.getMaxImageHeight() * sizeof(image::RGBfColor) / std::pow(2,20); //MB
const std::size_t imagePyramidMaxMemSize = texParams.nbBand * imageMaxMemSize;
const std::size_t atlasContribMemSize =
texParams.textureSide * texParams.textureSide * (sizeof(image::RGBfColor)+sizeof(float)) / std::pow(2,20); //MB
const std::size_t atlasPyramidMaxMemSize = texParams.nbBand * atlasContribMemSize;

const int availableRam = int(memInfo.availableRam / std::pow(2,20));
const int availableRam = int(memoryAvailable / std::pow(2,20));
const int availableMem = availableRam - 2 * (imagePyramidMaxMemSize + imageMaxMemSize); // keep some memory for the 2 input images in cache and one laplacian pyramid

const int nbAtlas = _atlases.size();
Expand Down
1 change: 1 addition & 0 deletions src/aliceVision/mesh/Texturing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ struct Texturing
/// Generate texture files for all texture atlases
void generateTextures(const mvsUtils::MultiViewParams& mp,
const bfs::path &outPath,
size_t memoryAvailable,
image::EImageFileType textureFileType = image::EImageFileType::PNG);

/// Generate texture files for the given sub-set of texture atlases
Expand Down
10 changes: 10 additions & 0 deletions src/aliceVision/system/hardwareContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,14 @@ unsigned int HardwareContext::getMaxThreads() const
return count;
}

size_t HardwareContext::getMaxMemory() const
{
auto meminfo = system::getMemoryInfo();

size_t ret = meminfo.availableRam;
ret = std::min(ret, _maxUserMemoryAvailable);

return ret;
}

}
8 changes: 7 additions & 1 deletion src/aliceVision/system/hardwareContext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class HardwareContext

void setUserMaxMemoryAvailable(size_t val)
{
_maxUserCoresAvailable = val;
_maxUserMemoryAvailable = val;
}

unsigned int getUserMaxCoresAvailable() const
Expand All @@ -44,6 +44,12 @@ class HardwareContext

unsigned int getMaxThreads() const;

/**
* @brief compute the maximum memory available
* @return the size in bytes
*/
size_t getMaxMemory() const;

private:
/**
* @brief This is the maximum memory available
Expand Down
1 change: 0 additions & 1 deletion src/software/pipeline/main_imageSegmentation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include <aliceVision/image/imageAlgo.hpp>

// System
#include <aliceVision/system/MemoryInfo.hpp>
#include <aliceVision/system/Logger.hpp>

// Reading command line options
Expand Down
1 change: 0 additions & 1 deletion src/software/pipeline/main_panoramaCompositing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include <aliceVision/image/imageAlgo.hpp>

// System
#include <aliceVision/system/MemoryInfo.hpp>
#include <aliceVision/system/Logger.hpp>

// Reading command line options
Expand Down
1 change: 0 additions & 1 deletion src/software/pipeline/main_panoramaMerging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include <aliceVision/image/imageAlgo.hpp>

// System
#include <aliceVision/system/MemoryInfo.hpp>
#include <aliceVision/system/Logger.hpp>

// Reading command line options
Expand Down
1 change: 0 additions & 1 deletion src/software/pipeline/main_panoramaPostProcessing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <aliceVision/image/all.hpp>

// System
#include <aliceVision/system/MemoryInfo.hpp>
#include <aliceVision/system/Logger.hpp>

// Reading command line options
Expand Down
1 change: 0 additions & 1 deletion src/software/pipeline/main_panoramaSeams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include <aliceVision/image/imageAlgo.hpp>

// System
#include <aliceVision/system/MemoryInfo.hpp>
#include <aliceVision/system/Logger.hpp>

// Reading command line options
Expand Down
2 changes: 1 addition & 1 deletion src/software/pipeline/main_texturing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ int aliceVision_main(int argc, char* argv[])
if(!inputMeshFilepath.empty() && !sfmDataFilename.empty() && texParams.textureFileType != image::EImageFileType::NONE)
{
ALICEVISION_LOG_INFO("Generate textures.");
mesh.generateTextures(mp, outputFolder, texParams.textureFileType);
mesh.generateTextures(mp, outputFolder, hwc.getMaxMemory(), texParams.textureFileType);
}


Expand Down

0 comments on commit 9541da0

Please sign in to comment.