|
| 1 | +/*========================================================================= |
| 2 | + * |
| 3 | + * Copyright NumFOCUS |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * https://www.apache.org/licenses/LICENSE-2.0.txt |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + *=========================================================================*/ |
| 18 | +#include "itkSingleMultiThreader.h" |
| 19 | +#include "itkNumericTraits.h" |
| 20 | +#include "itkProcessObject.h" |
| 21 | +#include "itkTotalProgressReporter.h" |
| 22 | + |
| 23 | +namespace itk |
| 24 | +{ |
| 25 | + |
| 26 | +SingleMultiThreader::SingleMultiThreader() |
| 27 | +{ |
| 28 | + m_NumberOfWorkUnits = 1; |
| 29 | + m_MaximumNumberOfThreads = 1; |
| 30 | +} |
| 31 | + |
| 32 | +SingleMultiThreader::~SingleMultiThreader() = default; |
| 33 | + |
| 34 | +void |
| 35 | +SingleMultiThreader::SetSingleMethod(ThreadFunctionType f, void * data) |
| 36 | +{ |
| 37 | + m_SingleMethod = std::move(f); |
| 38 | + m_SingleData = data; |
| 39 | +} |
| 40 | + |
| 41 | +void |
| 42 | +SingleMultiThreader::SetMaximumNumberOfThreads(ThreadIdType) |
| 43 | +{ |
| 44 | + m_MaximumNumberOfThreads = 1; |
| 45 | +} |
| 46 | + |
| 47 | +void |
| 48 | +SingleMultiThreader::SetNumberOfWorkUnits(ThreadIdType) |
| 49 | +{ |
| 50 | + m_NumberOfWorkUnits = 1; |
| 51 | +} |
| 52 | + |
| 53 | +void |
| 54 | +SingleMultiThreader::SingleMethodExecute() |
| 55 | +{ |
| 56 | + if (!m_SingleMethod) |
| 57 | + { |
| 58 | + itkExceptionStringMacro("No single method set!"); |
| 59 | + } |
| 60 | + |
| 61 | + WorkUnitInfo workUnitInfo; |
| 62 | + workUnitInfo.WorkUnitID = 0; |
| 63 | + workUnitInfo.NumberOfWorkUnits = 1; |
| 64 | + workUnitInfo.UserData = m_SingleData; |
| 65 | + workUnitInfo.ThreadFunction = m_SingleMethod; |
| 66 | + workUnitInfo.ThreadExitCode = WorkUnitInfo::ThreadExitCodeEnum::SUCCESS; |
| 67 | + |
| 68 | + // Invoke the method directly on the calling thread. |
| 69 | + // Since no thread boundary is crossed, exceptions propagate naturally |
| 70 | + // to the caller, preserving the original exception type and message. |
| 71 | + m_SingleMethod(&workUnitInfo); |
| 72 | +} |
| 73 | + |
| 74 | +void |
| 75 | +SingleMultiThreader::ParallelizeArray(SizeValueType firstIndex, |
| 76 | + SizeValueType lastIndexPlus1, |
| 77 | + ArrayThreadingFunctorType aFunc, |
| 78 | + ProcessObject * filter) |
| 79 | +{ |
| 80 | + if (!this->GetUpdateProgress()) |
| 81 | + { |
| 82 | + filter = nullptr; |
| 83 | + } |
| 84 | + |
| 85 | + if (firstIndex + 1 < lastIndexPlus1) |
| 86 | + { |
| 87 | + const SizeValueType count = lastIndexPlus1 - firstIndex; |
| 88 | + TotalProgressReporter reporter(filter, count); |
| 89 | + for (SizeValueType i = firstIndex; i < lastIndexPlus1; ++i) |
| 90 | + { |
| 91 | + reporter.CheckAbortGenerateData(); |
| 92 | + aFunc(i); |
| 93 | + reporter.CompletedPixel(); |
| 94 | + } |
| 95 | + } |
| 96 | + else if (firstIndex + 1 == lastIndexPlus1) |
| 97 | + { |
| 98 | + // Match MultiThreaderBase::ParallelizeArray: progress reaches 1.0 on RAII exit. |
| 99 | + const ProgressReporter progress(filter, 0, 1); |
| 100 | + aFunc(firstIndex); |
| 101 | + } |
| 102 | + // else: empty or reversed range — nothing to execute |
| 103 | +} |
| 104 | + |
| 105 | +void |
| 106 | +SingleMultiThreader::ParallelizeImageRegion(unsigned int dimension, |
| 107 | + const IndexValueType index[], |
| 108 | + const SizeValueType size[], |
| 109 | + ThreadingFunctorType funcP, |
| 110 | + ProcessObject * filter) |
| 111 | +{ |
| 112 | + (void)dimension; // The full region is passed directly; dimension is implicit in index/size. |
| 113 | + if (!this->GetUpdateProgress()) |
| 114 | + { |
| 115 | + filter = nullptr; |
| 116 | + } |
| 117 | + |
| 118 | + const ProgressReporter progress(filter, 0, 1); |
| 119 | + |
| 120 | + funcP(index, size); |
| 121 | +} |
| 122 | + |
| 123 | +void |
| 124 | +SingleMultiThreader::PrintSelf(std::ostream & os, Indent indent) const |
| 125 | +{ |
| 126 | + Superclass::PrintSelf(os, indent); |
| 127 | +} |
| 128 | + |
| 129 | +} // namespace itk |
0 commit comments