Orfeo Toolbox  4.2
otbPipelineMemoryPrintCalculator.cxx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ORFEO Toolbox
4  Language: C++
5  Date: $Date$
6  Version: $Revision$
7 
8 
9  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
10  See OTBCopyright.txt for details.
11 
12 
13  This software is distributed WITHOUT ANY WARRANTY; without even
14  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  PURPOSE. See the above copyright notices for more information.
16 
17 =========================================================================*/
18 
20 
21 #include "otbMath.h"
22 #include "otbImage.h"
23 #include "otbVectorImage.h"
24 #include "itkFixedArray.h"
25 #include "otbImageList.h"
26 
27 namespace otb
28 {
29 const double PipelineMemoryPrintCalculator::ByteToMegabyte = 1./vcl_pow(2.0, 20);
30 const double PipelineMemoryPrintCalculator::MegabyteToByte = vcl_pow(2.0, 20);
31 
34  : m_MemoryPrint(0),
35  m_DataToWrite(NULL),
36  m_BiasCorrectionFactor(1.),
37  m_VisitedProcessObjects()
38 {}
39 
42 {}
43 
44 // [static]
45 unsigned long
48 {
49  unsigned long divisions;
50  divisions = vcl_ceil(static_cast<double>(memoryPrint)
51  / availableMemory);
52  return divisions;
53 }
54 
55 void
57 ::PrintSelf(std::ostream& os, itk::Indent indent) const
58 {
59  // Call superclass implementation
60  Superclass::PrintSelf(os, indent);
61 
62  // Display parameters
63  os<<indent<<"Data to write: "<<m_DataToWrite<<std::endl;
64  os<<indent<<"Memory print of whole pipeline: "<<m_MemoryPrint * ByteToMegabyte <<" Mb"<<std::endl;
65  os<<indent<<"Bias correction factor applied: "<<m_BiasCorrectionFactor<<std::endl;
66 }
67 
68 void
71 {
72  // Clear the visited process objects set
73  m_VisitedProcessObjects.clear();
74 
75  // Dry run of pipeline synchronisation
76  m_DataToWrite->UpdateOutputInformation();
77  m_DataToWrite->SetRequestedRegionToLargestPossibleRegion();
78  m_DataToWrite->PropagateRequestedRegion();
79 
80  // Get the source process object
81  ProcessObjectType * source = m_DataToWrite->GetSource();
82 
83  // Check if source exists
84  if(source)
85  {
86  // Call the recursive memory print evaluation
87  m_MemoryPrint = EvaluateProcessObjectPrintRecursive(source);
88  }
89  else
90  {
91  // Get memory print for this data only
92  m_MemoryPrint = EvaluateDataObjectPrint(m_DataToWrite);
93  }
94 
95  // Apply bias correction factor
96  m_MemoryPrint *= m_BiasCorrectionFactor;
97 
98 }
99 
103 {
104  otbMsgDevMacro(<< "EvaluateMemoryPrint for " << process->GetNameOfClass() << " (" << process << ")")
105  // This variable will store the final print
106  MemoryPrintType print = 0;
107 
108  // Check if this process object has already been visited
109  if(m_VisitedProcessObjects.count(process))
110  {
111  return print;
112  }
113  // Else register it as a visited process object
114  else
115  {
116  m_VisitedProcessObjects.insert(process);
117  }
118 
119  // Retrieve the array of inputs
121  // First, recurse on each input source
122  for(unsigned int i = 0; i < process->GetNumberOfInputs(); ++i)
123  {
124  // Retrieve the data object
125  DataObjectType * input = inputs[i];
126 
127  if( input )
128  {
129  // Retrieve possible source
130  ProcessObjectType * source = input->GetSource();
131 
132  // If data object has a source
133  if(source)
134  {
135  print += this->EvaluateProcessObjectPrintRecursive(source);
136  }
137  else
138  {
139  MemoryPrintType localPrint = this->EvaluateDataObjectPrint(input);
140  print += localPrint;
141  }
142  }
143  }
144 
145  // Retrieve the output array
147 
148  // Now, evaluate the current object print
149  for(unsigned int i = 0; i < process->GetNumberOfOutputs(); ++i)
150  {
151  MemoryPrintType localPrint = this->EvaluateDataObjectPrint(outputs[i]);
152  print += localPrint;
153  }
154 
155  // Finally, return the total print
156  return print;
157 }
158 
162 {
163  otbMsgDevMacro(<< "EvaluateMemoryPrint for " << data->GetNameOfClass() << " (" << data << ")")
164 
165 #define OTB_IMAGE_SIZE_BLOCK(type) \
166  if(dynamic_cast<itk::Image<type, 2> *>(data) != NULL) \
167  { \
168  itk::Image<type, 2> * image = dynamic_cast<itk::Image<type, 2> *>(data); \
169  return image->GetRequestedRegion().GetNumberOfPixels() \
170  * image->GetNumberOfComponentsPerPixel() * sizeof(type); \
171  } \
172  if(dynamic_cast<itk::VectorImage<type, 2> * >(data) != NULL) \
173  { \
174  itk::VectorImage<type, 2> * image = dynamic_cast<itk::VectorImage<type, 2> *>(data); \
175  return image->GetRequestedRegion().GetNumberOfPixels() \
176  * image->GetNumberOfComponentsPerPixel() * sizeof(type); \
177  } \
178  if(dynamic_cast<ImageList<Image<type, 2> > *>(data) != NULL) \
179  { \
180  ImageList<Image<type, 2> > * imageList = dynamic_cast<otb::ImageList<otb::Image<type, 2> > *>(data); \
181  MemoryPrintType print(0); \
182  for(ImageList<Image<type, 2> >::ConstIterator it = imageList->Begin(); \
183  it != imageList->End(); ++it) \
184  { \
185  print += it.Get()->GetRequestedRegion().GetNumberOfPixels() \
186  * it.Get()->GetNumberOfComponentsPerPixel() * sizeof(type); \
187  } \
188  return print; \
189  } \
190  if(dynamic_cast<ImageList<VectorImage<type, 2> > *>(data) != NULL) \
191  { \
192  ImageList<VectorImage<type, 2> > * imageList = dynamic_cast<otb::ImageList<otb::VectorImage<type, 2> > *>(data); \
193  MemoryPrintType print(0); \
194  for(ImageList<VectorImage<type, 2> >::ConstIterator it = imageList->Begin(); \
195  it != imageList->End(); ++it) \
196  { \
197  print += it.Get()->GetRequestedRegion().GetNumberOfPixels() \
198  * it.Get()->GetNumberOfComponentsPerPixel() * sizeof(type); \
199  } \
200  return print; \
201  } \
202 
203 
204  // Call the macro for each pixel type
205  OTB_IMAGE_SIZE_BLOCK(unsigned char)
207  OTB_IMAGE_SIZE_BLOCK(unsigned short)
208  OTB_IMAGE_SIZE_BLOCK(short)
209  OTB_IMAGE_SIZE_BLOCK(unsigned int)
211  OTB_IMAGE_SIZE_BLOCK(unsigned long)
213  OTB_IMAGE_SIZE_BLOCK(float)
214  OTB_IMAGE_SIZE_BLOCK(double)
215  OTB_IMAGE_SIZE_BLOCK(std::complex<float>)
216  OTB_IMAGE_SIZE_BLOCK(std::complex<double>)
217  typedef itk::FixedArray<float, 2> FloatFixedArray2Type;
218  typedef itk::FixedArray<float, 2> DoubleFixedArray2Type;
219  OTB_IMAGE_SIZE_BLOCK(FloatFixedArray2Type)
220  OTB_IMAGE_SIZE_BLOCK(DoubleFixedArray2Type)
221 
222  // If we are still here, none of the macro call succeed
223  return 0;
224 }
225 
226 } // End namespace otb

Generated at Sat Aug 30 2014 16:19:14 for Orfeo Toolbox with doxygen 1.8.3.1