1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
|
/*=========================================================================
*
* Copyright Insight Software Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#include "itkMath.h"
#include "itkDomainThreader.h"
#include "itkThreadedIndexedContainerPartitioner.h"
#include "itkCompensatedSummation.h"
#include <iostream>
#include <iomanip>
/*
* This test demonstrates the variance in output when the same operation is
* performed with different numbers of threads, and the utility of the
* CompensatedSummation class for summing the per-thread output of
* multi-threaded operation to reduce the variance. The variance is a
* result of different floating-point rounding that occurs when
* different numbers of threads are used.
*/
class CompensatedSummationTest2Associate
{
public:
typedef CompensatedSummationTest2Associate Self;
// Nested class holds the domain threader
class TestDomainThreader
: public itk::DomainThreader< itk::ThreadedIndexedContainerPartitioner, Self >
{
public:
typedef TestDomainThreader Self;
typedef itk::DomainThreader< itk::ThreadedIndexedContainerPartitioner, Self > Superclass;
typedef itk::SmartPointer< Self > Pointer;
typedef itk::SmartPointer< const Self > ConstPointer;
typedef Superclass::DomainPartitionerType DomainPartitionerType;
typedef Superclass::DomainType DomainType;
itkNewMacro( Self );
protected:
TestDomainThreader() {};
private:
virtual void BeforeThreadedExecution() ITK_OVERRIDE
{
const itk::ThreadIdType numThreadsUsed = this->GetNumberOfThreadsUsed();
this->m_PerThreadCompensatedSum.resize( numThreadsUsed );
for( itk::ThreadIdType i = 0; i < numThreadsUsed; ++i )
{
this->m_PerThreadCompensatedSum[i].ResetToZero();
}
}
virtual void ThreadedExecution( const DomainType& subdomain,
const itk::ThreadIdType threadId ) ITK_OVERRIDE
{
itk::CompensatedSummation<double> compensatedSum;
for( DomainType::IndexValueType i=subdomain[0]; i <= subdomain[1]; i++ )
{
double value = itk::NumericTraits<double>::OneValue() / 7;
this->m_PerThreadCompensatedSum[threadId].AddElement( value );
}
}
virtual void AfterThreadedExecution() ITK_OVERRIDE
{
this->m_Associate->m_UncompensatedSumOfThreads = itk::NumericTraits<double>::ZeroValue();
this->m_Associate->m_CompensatedSumOfThreads.ResetToZero();
for( itk::ThreadIdType i = 0, numThreadsUsed = this->GetNumberOfThreadsUsed(); i < numThreadsUsed; ++i )
{
double sum = this->m_PerThreadCompensatedSum[i].GetSum();
std::cout << i << ": " << sum << std::endl;
this->m_Associate->m_CompensatedSumOfThreads.AddElement( sum );
this->m_Associate->m_UncompensatedSumOfThreads += sum;
}
}
std::vector< itk::CompensatedSummation<double> > m_PerThreadCompensatedSum;
ITK_DISALLOW_COPY_AND_ASSIGN(TestDomainThreader);
}; // end TestDomainThreader class
CompensatedSummationTest2Associate()
{
m_TestDomainThreader = TestDomainThreader::New();
m_ClassDescriptor = "enclosing class";
m_UncompensatedSumOfThreads = 0.0;
}
double GetCompensatedSumOfThreads()
{
return this->m_CompensatedSumOfThreads.GetSum();
}
double GetUncompensatedSumOfThreads()
{
return this->m_UncompensatedSumOfThreads;
}
TestDomainThreader * GetDomainThreader()
{
return m_TestDomainThreader.GetPointer();
}
void Execute( const TestDomainThreader::DomainType & completeDomain )
{
m_TestDomainThreader->Execute(this, completeDomain);
}
private:
TestDomainThreader::Pointer m_TestDomainThreader;
std::string m_ClassDescriptor;
itk::CompensatedSummation<double> m_CompensatedSumOfThreads;
double m_UncompensatedSumOfThreads;
};
int itkCompensatedSummationTest2(int, char* [])
{
CompensatedSummationTest2Associate enclosingClass;
CompensatedSummationTest2Associate::TestDomainThreader::Pointer domainThreader = enclosingClass.GetDomainThreader();
/* Check # of threads */
std::cout << "GetGlobalMaximumNumberOfThreads: "
<< domainThreader->GetMultiThreader()->GetGlobalMaximumNumberOfThreads()
<< std::endl;
std::cout << "GetGlobalDefaultNumberOfThreads: "
<< domainThreader->GetMultiThreader()->GetGlobalDefaultNumberOfThreads()
<< std::endl;
typedef CompensatedSummationTest2Associate::TestDomainThreader::DomainType DomainType;
DomainType domain;
itk::ThreadIdType maxNumberOfThreads =
domainThreader->GetMultiThreader()->GetGlobalMaximumNumberOfThreads();
domain[0] = 0;
domain[1] = maxNumberOfThreads * 10000;
/* Test with single thread. We should get the same result. */
itk::ThreadIdType numberOfThreads = 1;
domainThreader->SetMaximumNumberOfThreads( numberOfThreads );
std::cout << "Testing with " << numberOfThreads
<< " threads and domain " << domain << " ..." << std::endl;
/* Execute */
enclosingClass.Execute( domain );
/* Did we use as many threads as requested? */
std::cout << "Requested numberOfThreads: " << numberOfThreads << std::endl
<< "actual: threader->GetNumberOfThreadsUsed(): "
<< domainThreader->GetNumberOfThreadsUsed() << "\n\n" << std::endl;
/* Check results */
if( itk::Math::NotAlmostEquals( enclosingClass.GetCompensatedSumOfThreads(), enclosingClass.GetUncompensatedSumOfThreads() ) )
{
std::cerr << std::setprecision(20)
<< "Error. Expected the sum to be the same for compensated and uncompensated."
<< " Instead, got " << enclosingClass.GetCompensatedSumOfThreads() << " and "
<< enclosingClass.GetUncompensatedSumOfThreads() << std::endl
<< "Difference: " << enclosingClass.GetCompensatedSumOfThreads() - enclosingClass.GetUncompensatedSumOfThreads()
<< std::endl;
return EXIT_FAILURE;
}
/* Store result as reference */
double referenceSum = enclosingClass.GetCompensatedSumOfThreads();
/* Test with maximum threads. We need at least three threads to see a difference. */
if( domainThreader->GetMultiThreader()->GetGlobalMaximumNumberOfThreads() > 2 )
{
domainThreader->SetMaximumNumberOfThreads( maxNumberOfThreads );
std::cout << "Testing with " << maxNumberOfThreads
<< " threads and domain " << domain << " ..." << std::endl;
/* Execute */
enclosingClass.Execute( domain );
/* Check number of threads used */
if( domainThreader->GetNumberOfThreadsUsed() != maxNumberOfThreads )
{
std::cerr << "Error: Expected to use " << maxNumberOfThreads
<< "threads, but used " << domainThreader->GetNumberOfThreadsUsed()
<< "." << std::endl;
return EXIT_FAILURE;
}
std::cout << "# of digits precision in double: " << std::numeric_limits< double >::digits10
<< std::endl;
std::cout << std::setprecision(100)
<< "Reference: " << referenceSum << std::endl
<< "Compensated: " << enclosingClass.GetCompensatedSumOfThreads() << std::endl
<< "Uncompensated: " << enclosingClass.GetUncompensatedSumOfThreads()
<< std::endl
<< "Difference: " << enclosingClass.GetCompensatedSumOfThreads() - enclosingClass.GetUncompensatedSumOfThreads()
<< std::endl;
/* Check that the compensated result is not further from reference than
* uncompensated.
* Generally we see the compensated sum closer, but on a few platforms the
* sums are equal. This could be because of differences in compiler
* optimizations that were not handled by the CompensatedSummation class
* pragmas, or perhaps because of differences in math coprocessors, or
* something else. It's not clear. */
if( std::fabs( referenceSum - enclosingClass.GetCompensatedSumOfThreads() ) >
std::fabs( referenceSum - enclosingClass.GetUncompensatedSumOfThreads() ) )
{
std::cerr << "Error. Expected the compensated sum of threads to be closer "
<< "to reference than the uncompensated sum, or the same value. "
<< std::endl;
return EXIT_FAILURE;
}
}
else
{
std::cout << "No multi-threading available, or too few threads available. "
<< std::endl;
}
return EXIT_SUCCESS;
}
|