2021-04-16 12:53 CEST

View Issue Details Jump to Notes ]
IDProjectCategoryView StatusLast Update
0001042OTB-libGeneralpublic2015-05-20 14:16
ReporterLuc Hermitte 
Assigned Torashadkm 
PriorityhighSeverityminorReproducibilityalways
StatusclosedResolutionwon't fix 
Summary0001042: Optimization of Image to VectorImage Cast Filter (from 47M cycles to 7M cycles)
DescriptionUseless reallocation were done in OTB 3.16.

Here is a patch that makes the use of Image to VectorImage Cast Filter goes down from 47M cycles 7M cycles (measured by callgrind on a program compiled with -O2 -g -DNDEBUG)
Additional InformationThe idea behind the patch is to avoid to allocate and free memory to store a VectorPixel of size 1 at each iteration.

Instead the output pixel is cached and its value is updated at each iteration. Moreover the pixel is now returned through a const reference.

NB: The end user code shall not remember references on the returned pixels.
TagsNo tags attached.
Attached Files
  • patch file icon 0001-cots-OTB-Optimization-of-Image-to-VectorImage-Cast-F.patch (1,608 bytes) 2015-05-04 15:43 -
    From b9e0cc410c66bafba8ead826ee97e25cac971dac Mon Sep 17 00:00:00 2001
    From: Luc Hermitte <luc.hermitte@c-s.fr>
    Date: Wed, 8 Apr 2015 16:14:21 +0200
    Subject: [PATCH] [cots/OTB] Optimization of Image to VectorImage Cast Filter (from 47M cycles to 7M cycles)
    
    ---
     .../Code/Common/otbImageToVectorImageCastFilter.h  |   22 ++++++++++++++-----
     1 files changed, 16 insertions(+), 6 deletions(-)
    
    diff --git a/cots/otb/OrfeoToolbox-3.16.0/Code/Common/otbImageToVectorImageCastFilter.h b/cots/otb/OrfeoToolbox-3.16.0/Code/Common/otbImageToVectorImageCastFilter.h
    index 80caff2..8e46ed7 100644
    --- a/cots/otb/OrfeoToolbox-3.16.0/Code/Common/otbImageToVectorImageCastFilter.h
    +++ b/cots/otb/OrfeoToolbox-3.16.0/Code/Common/otbImageToVectorImageCastFilter.h
    @@ -37,7 +37,10 @@ class VectorCast
     {
     public:
       typedef typename TOutput::ValueType OutputValueType;
    -  VectorCast() {}
    +  VectorCast()
    +      : m_CachedOutput(1)
    +  {
    +  }
       virtual ~VectorCast() {}
     
       bool operator !=(const VectorCast&) const
    @@ -48,13 +51,20 @@ public:
       {
         return (*this != other);
       }
    -  inline TOutput operator ()(const TInput& A)
    +  TOutput const& operator ()(const TInput& A)
       {
    -    TOutput output;
    -    output.SetSize(1);
    -    output[0] = static_cast<OutputValueType>(A);
    -    return output;
    +#if 0
    +      TOutput output;
    +      output.SetSize(1);
    +      output[0] = static_cast<OutputValueType>(A);
    +      return output;
    +#else
    +      m_CachedOutput[0] = static_cast<OutputValueType>(A);
    +      return m_CachedOutput;
    +#endif
       }
    +private:
    +  TOutput m_CachedOutput;
     };
     } // End namespace Functor
     /**
    -- 
    1.7.1
    
    

-Relationships
+Relationships

-Notes

~0002880

rashadkm (developer)

This patch is breaking some tests. Hence cannot be applied now. Need to investiage the issue more.

The following tests are failing:

1160 - coTvImageToVectorImageCastFilter (Failed)


ctest -VV -R coTvImageToVectorImageCastFilter

1160: Test command: /home/mkanavat/build/otb/bin/otbImageManipulationTestDriver "--compare-image" "0.0" "/home/mkanavat/sources/orfeo/OTB-Data/Baseline/OTB/Images/coImageToVectorImageCastFilterOutput.tif" "/home/mkanavat/build/otb/Testing/Temporary/coImageToVectorImageCastFilterOutput.tif" "otbImageToVectorImageCastFilter" "/home/mkanavat/sources/orfeo/OTB-Data/Input/poupees.hdr" "/home/mkanavat/build/otb/Testing/Temporary/coImageToVectorImageCastFilterOutput.tif"
1160: Test timeout computed to be: 1500
1160: -> Test EXIT SUCCESS.
1160: ------------- Start control baseline tests -------------
1160: Number of baseline images: 1
1160: Testing non-regression on image: /home/mkanavat/sources/orfeo/OTB-Data/Baseline/OTB/Images/coImageToVectorImageCastFilterOutput.tif
1160: <DartMeasurement name="ImageError 1" type="numeric/double">[1.84929e+06]</DartMeasurement>
1160: <DartMeasurement name="NumberOfPixelsWithDifferences 1" type="numeric/integer">20696</DartMeasurement>
1160: <DartMeasurement name="ToleranceDiffPixelImage" type="numeric/double">0</DartMeasurement>
1160: WARNING: In /home/mkanavat/sources/orfeo/OTB/Modules/IO/ImageIO/include/otbImageFileWriter.txx, line 543
1160: ImageFileWriter (0x285df20): The ImageFactory selected for the image file </home/mkanavat/build/otb/Testing/Temporary/coImageToVectorImageCastFilterOutput.tif.diff.png> does not support streaming.
1160:
1160: <DartMeasurementFile name="DifferenceImage 1" type="image/png">/home/mkanavat/build/otb/Testing/Temporary/coImageToVectorImageCastFilterOutput.tif.diff.png</DartMeasurementFile>
1160: WARNING: In /home/mkanavat/sources/orfeo/OTB/Modules/IO/ImageIO/include/otbImageFileWriter.txx, line 543
1160: ImageFileWriter (0x285df20): The ImageFactory selected for the image file </home/mkanavat/build/otb/Testing/Temporary/coImageToVectorImageCastFilterOutput.tif.base.png> does not support streaming.
1160:
1160: <DartMeasurementFile name="BaselineImage 1" type="image/png">/home/mkanavat/build/otb/Testing/Temporary/coImageToVectorImageCastFilterOutput.tif.base.png</DartMeasurementFile>
1160: WARNING: In /home/mkanavat/sources/orfeo/OTB/Modules/IO/ImageIO/include/otbImageFileWriter.txx, line 543
1160: ImageFileWriter (0x285df20): The ImageFactory selected for the image file </home/mkanavat/build/otb/Testing/Temporary/coImageToVectorImageCastFilterOutput.tif.test.png> does not support streaming.
1160:
1160: <DartMeasurementFile name="TestImage 1" type="image/png">/home/mkanavat/build/otb/Testing/Temporary/coImageToVectorImageCastFilterOutput.tif.test.png</DartMeasurementFile>
1160: ------------- End control baseline tests -------------
1/1 Test 0001160: coTvImageToVectorImageCastFilter ...***Failed 0.73 sec

~0002901

rashadkm (developer)

Caching pixel values using m_CachedOutput is not working in a multi-threaded environment. I had tested with number of threads = 1. see below


[mkanavat@localhost otb]$ ctest -R coTvImageToVectorImageCast
Test project /home/mkanavat/build/otb
    Start 1160: coTvImageToVectorImageCastFilter
1/1 Test 0001160: coTvImageToVectorImageCastFilter ...***Failed 0.70 sec

0% tests passed, 1 tests failed out of 1

Label Time Summary:
OTBImageManipulation = 0.70 sec

Total Test time (real) = 0.83 sec

The following tests FAILED:
    1160 - coTvImageToVectorImageCastFilter (Failed)
Errors while running CTest
[mkanavat@localhost otb]$ export ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS=1
[mkanavat@localhost otb]$ ctest -R coTvImageToVectorImageCast
Test project /home/mkanavat/build/otb
    Start 1160: coTvImageToVectorImageCastFilter
1/1 Test 0001160: coTvImageToVectorImageCastFilter ... Passed 0.24 sec

100% tests passed, 0 tests failed out of 1

Label Time Summary:
OTBImageManipulation = 0.24 sec

Total Test time (real) = 0.36 sec
[mkanavat@localhost otb]$


This patch could not be safe with recent OTB or maybe in a OTB with multi-threaded execution.

Luc, could you check if the coTvImageToVectorImageCastFilter test is passing with your patch on OTB 3.16. I think, test will also fail there.

Thanks

~0002907

rashadkm (developer)

This patch cannot be applied in a multi-threaded context. functors are applied with every pixel hence the proposed optimization cannot be applied.
+Notes

-Issue History
Date Modified Username Field Change
2015-05-04 15:43 Luc Hermitte New Issue
2015-05-04 15:43 Luc Hermitte File Added: 0001-cots-OTB-Optimization-of-Image-to-VectorImage-Cast-F.patch
2015-05-13 16:23 rashadkm Note Added: 0002880
2015-05-19 11:06 julien Assigned To => rashadkm
2015-05-19 11:06 julien Status new => assigned
2015-05-19 11:06 julien Priority normal => high
2015-05-19 14:11 rashadkm Note Added: 0002901
2015-05-20 14:16 rashadkm Note Added: 0002907
2015-05-20 14:16 rashadkm Status assigned => closed
2015-05-20 14:16 rashadkm Resolution open => won't fix
+Issue History