OTB  9.0.0
Orfeo Toolbox
otbStreamingManager.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2022 Centre National d'Etudes Spatiales (CNES)
3  *
4  * This file is part of Orfeo Toolbox
5  *
6  * https://www.orfeo-toolbox.org/
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #ifndef otbStreamingManager_hxx
22 #define otbStreamingManager_hxx
23 
24 #include "otbStreamingManager.h"
26 #include "itkExtractImageFilter.h"
27 
28 namespace otb
29 {
30 
31 template <class TImage>
32 StreamingManager<TImage>::StreamingManager() : m_ComputedNumberOfSplits(0), m_DefaultRAM(0)
33 {
34 }
35 
36 template <class TImage>
38 {
39 }
40 
41 template <class TImage>
43 {
44  return m_Splitter;
45 }
46 
47 template <class TImage>
49 {
50  MemoryPrintType availableRAMInBytes = availableRAMInMB * 1024 * 1024;
51 
52  if (availableRAMInBytes == 0)
53  {
54  if (m_DefaultRAM != 0)
55  {
56  availableRAMInBytes = 1024 * 1024 * m_DefaultRAM;
57  }
58  else
59  {
60  // Retrieve it from the configuration
61  availableRAMInBytes = 1024 * 1024 * ConfigurationManager::GetMaxRAMHint();
62  }
63  }
64  return availableRAMInBytes;
65 }
66 
67 template <class TImage>
68 unsigned int StreamingManager<TImage>::EstimateOptimalNumberOfDivisions(itk::DataObject* input, const RegionType& region, MemoryPrintType availableRAM,
69  double bias)
70 {
71  MemoryPrintType availableRAMInBytes = GetActualAvailableRAMInBytes(availableRAM);
72 
73  otb::PipelineMemoryPrintCalculator::Pointer memoryPrintCalculator;
74  memoryPrintCalculator = otb::PipelineMemoryPrintCalculator::New();
75 
76  // Trick to avoid having the resampler compute the whole
77  // displacement field
78  double regionTrickFactor = 1;
79  ImageType* inputImage = dynamic_cast<ImageType*>(input);
80 
81  MemoryPrintType pipelineMemoryPrint;
82  if (inputImage)
83  {
84 
85  typedef itk::ExtractImageFilter<ImageType, ImageType> ExtractFilterType;
86  typename ExtractFilterType::Pointer extractFilter = ExtractFilterType::New();
87  extractFilter->SetInput(inputImage);
88 
89  // Define a small region to run the memory footprint estimation,
90  // around the image center, 100 pixels wide in each dimension
91  SizeType smallSize;
92  smallSize.Fill(100);
93  IndexType index;
94  index[0] = region.GetIndex()[0] + region.GetSize()[0] / 2 - 50;
95  index[1] = region.GetIndex()[1] + region.GetSize()[1] / 2 - 50;
96 
97  RegionType smallRegion;
98  smallRegion.SetSize(smallSize);
99  smallRegion.SetIndex(index);
100 
101  // In case the image is smaller than 100 pixels in a direction
102  smallRegion.Crop(region);
103 
104  extractFilter->SetExtractionRegion(smallRegion);
105 
106  bool smallRegionSuccess = smallRegion.Crop(region);
107 
108  if (smallRegionSuccess)
109  {
110  // the region is well behaved, inside the largest possible region
111  memoryPrintCalculator->SetDataToWrite(extractFilter->GetOutput());
112 
113  regionTrickFactor = static_cast<double>(region.GetNumberOfPixels()) / static_cast<double>(smallRegion.GetNumberOfPixels());
114 
115  memoryPrintCalculator->SetBiasCorrectionFactor(regionTrickFactor * bias);
116  }
117  else
118  {
119  // the region is not well behaved
120  // use the full region
121  memoryPrintCalculator->SetDataToWrite(input);
122  memoryPrintCalculator->SetBiasCorrectionFactor(bias);
123  }
124 
125  memoryPrintCalculator->Compute();
126 
127  pipelineMemoryPrint = memoryPrintCalculator->GetMemoryPrint();
128 
129  if (smallRegionSuccess)
130  {
131  // remove the contribution of the ExtractImageFilter
132  MemoryPrintType extractContrib = memoryPrintCalculator->EvaluateDataObjectPrint(extractFilter->GetOutput());
133 
134  pipelineMemoryPrint -= extractContrib;
135  }
136  }
137  else
138  {
139  // Use the original object to estimate memory footprint
140  memoryPrintCalculator->SetDataToWrite(input);
141  memoryPrintCalculator->SetBiasCorrectionFactor(1.0);
142 
143  memoryPrintCalculator->Compute();
144 
145  pipelineMemoryPrint = memoryPrintCalculator->GetMemoryPrint();
146  }
147 
148  unsigned int optimalNumberOfDivisions = otb::PipelineMemoryPrintCalculator::EstimateOptimalNumberOfStreamDivisions(pipelineMemoryPrint, availableRAMInBytes);
149 
150  otbLogMacro(Info, << "Estimated memory for full processing: " << pipelineMemoryPrint * otb::PipelineMemoryPrintCalculator::ByteToMegabyte
151  << "MB (avail.: " << availableRAMInBytes * otb::PipelineMemoryPrintCalculator::ByteToMegabyte
152  << " MB), optimal image partitioning: " << optimalNumberOfDivisions << " blocks");
153 
154  return optimalNumberOfDivisions;
155 }
156 
157 template <class TImage>
159 {
160  return m_ComputedNumberOfSplits;
161 }
162 
163 template <class TImage>
165 {
166  typename StreamingManager<TImage>::RegionType region(m_Region);
167  m_Splitter->GetSplit(i, m_ComputedNumberOfSplits, region);
168  return region;
169 }
170 
171 } // End namespace otb
172 
173 #endif
otb::StreamingManager< TInputImage >::ImageType
TInputImage ImageType
Definition: otbStreamingManager.h:62
otb::PipelineMemoryPrintCalculator::ByteToMegabyte
static const double ByteToMegabyte
Definition: otbPipelineMemoryPrintCalculator.h:120
otb::StreamingManager::StreamingManager
StreamingManager()
Definition: otbStreamingManager.hxx:32
otb::PipelineMemoryPrintCalculator::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbPipelineMemoryPrintCalculator.h:79
otbLogMacro
#define otbLogMacro(level, msg)
Definition: otbMacro.h:52
otb::StreamingManager
This class handles the streaming process used in the writers implementation.
Definition: otbStreamingManager.h:53
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::StreamingManager< TInputImage >::IndexType
RegionType::IndexType IndexType
Definition: otbStreamingManager.h:65
otb::StreamingManager< TInputImage >::SizeType
RegionType::SizeType SizeType
Definition: otbStreamingManager.h:66
otbStreamingManager.h
otb::StreamingManager::MemoryPrintType
otb::PipelineMemoryPrintCalculator::MemoryPrintType MemoryPrintType
Definition: otbStreamingManager.h:69
otb::PipelineMemoryPrintCalculator::EstimateOptimalNumberOfStreamDivisions
static unsigned long EstimateOptimalNumberOfStreamDivisions(MemoryPrintType memoryPrint, MemoryPrintType availableMemory)
otb::StreamingManager::AbstractSplitterType
itk::ImageRegionSplitterBase AbstractSplitterType
Definition: otbStreamingManager.h:70
otb::PipelineMemoryPrintCalculator::New
static Pointer New()
otbConfigurationManager.h
otb::StreamingManager< TInputImage >::RegionType
ImageType::RegionType RegionType
Definition: otbStreamingManager.h:64