diff --git a/src/Core/Common/CMakeLists.txt b/src/Core/Common/CMakeLists.txt index 98df75470..f5529bb9b 100644 --- a/src/Core/Common/CMakeLists.txt +++ b/src/Core/Common/CMakeLists.txt @@ -204,3 +204,5 @@ compare_to_baseline(EXAMPLE_NAME OutOfBoundsPixelsReturnConstValue BASELINE_PREFIX OutputBaseline ) endif() + +add_example(SetDefaultNumberOfThreads) diff --git a/src/Core/Common/SetDefaultNumberOfThreads/CMakeLists.txt b/src/Core/Common/SetDefaultNumberOfThreads/CMakeLists.txt new file mode 100644 index 000000000..576f8e14b --- /dev/null +++ b/src/Core/Common/SetDefaultNumberOfThreads/CMakeLists.txt @@ -0,0 +1,36 @@ +cmake_minimum_required(VERSION 3.10.2) + +project(SetDefaultNumberOfThreads) + +find_package(ITK REQUIRED) +include(${ITK_USE_FILE}) + +add_executable(SetDefaultNumberOfThreads Code.cxx) +target_link_libraries(SetDefaultNumberOfThreads ${ITK_LIBRARIES}) + +install(TARGETS SetDefaultNumberOfThreads + DESTINATION bin/ITKExamples/Core/Common + COMPONENT Runtime +) + +install(FILES Code.cxx CMakeLists.txt Code.py + DESTINATION share/ITKExamples/Code/Core/Common/SetDefaultNumberOfThreads + COMPONENT Code +) + +enable_testing() + +set(test_options 3) + +add_test(NAME SetDefaultNumberOfThreadsTest + COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/SetDefaultNumberOfThreads + ${test_options} +) + +if(ITK_WRAP_PYTHON) + find_package(PythonInterp REQUIRED) + add_test( NAME SetDefaultNumberOfThreadsTestPython + COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/Code.py + ${test_options} + ) +endif() diff --git a/src/Core/Common/SetDefaultNumberOfThreads/Code.cxx b/src/Core/Common/SetDefaultNumberOfThreads/Code.cxx new file mode 100644 index 000000000..53a47135b --- /dev/null +++ b/src/Core/Common/SetDefaultNumberOfThreads/Code.cxx @@ -0,0 +1,53 @@ +/*========================================================================= + * + * 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 "itkMultiThreaderBase.h" +#include "itkMedianImageFilter.h" + +int +main(int argc, char * argv[]) +{ + if (argc != 2) + { + std::cerr << "Usage: " << std::endl; + std::cerr << argv[0]; + std::cerr << " "; + std::cerr << std::endl; + return EXIT_FAILURE; + } + + const auto numberOfThreads = std::atoi(argv[1]); + + itk::MultiThreaderBase::SetGlobalDefaultNumberOfThreads(numberOfThreads); + + constexpr unsigned int Dimension = 2; + using PixelType = float; + using ImageType = itk::Image; + using FilterType = itk::MedianImageFilter; + FilterType::Pointer filter = FilterType::New(); + + const auto filterDefaultThreads = filter->GetMultiThreader()->GetGlobalDefaultNumberOfThreads(); + std::cout << "Filter's default number of threads: " << filterDefaultThreads << std::endl; + + if (filterDefaultThreads != numberOfThreads) + { + std::cerr << "Filter does not have expected default number of threads." << std::endl; + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} diff --git a/src/Core/Common/SetDefaultNumberOfThreads/Code.py b/src/Core/Common/SetDefaultNumberOfThreads/Code.py new file mode 100755 index 000000000..86e17cb3a --- /dev/null +++ b/src/Core/Common/SetDefaultNumberOfThreads/Code.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python + +# 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. + +import argparse +import itk + +parser = argparse.ArgumentParser(description='Set the Default Number Of Threads.') +parser.add_argument('number_of_threads', type=int) +args = parser.parse_args() + +# Create a MultiThreaderBase instance to get access to its static methods +threader = itk.MultiThreaderBase.New() +threader.SetGlobalDefaultNumberOfThreads(args.number_of_threads) + +filt = itk.MedianImageFilter.New() +filter_default_threads = filt.GetMultiThreader().GetGlobalDefaultNumberOfThreads() + +print("Filter's default number of threads: {}".format(filter_default_threads)) + +assert(filter_default_threads == args.number_of_threads) diff --git a/src/Core/Common/SetDefaultNumberOfThreads/Documentation.rst b/src/Core/Common/SetDefaultNumberOfThreads/Documentation.rst new file mode 100644 index 000000000..56c109f77 --- /dev/null +++ b/src/Core/Common/SetDefaultNumberOfThreads/Documentation.rst @@ -0,0 +1,46 @@ +Set the Default Number Of Threads +================================= + +.. index:: + single: MultiThreaderBase + +Synopsis +-------- + +Set the default number of threads for multi-threading. + +The default number of threads can also be set via the environment variable, +`ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS`. + + +Results +------- + +Output:: + + Filter's default number of threads: 3 + + + +Code +---- + +C++ +... + +.. literalinclude:: Code.cxx + :language: c++ + :lines: 18- + +Python +...... + +.. literalinclude:: Code.py + :language: python + :lines: 1,18- + + +Classes demonstrated +-------------------- + +.. breathelink:: itk::MultiThreaderBase diff --git a/src/Core/Common/index.rst b/src/Core/Common/index.rst index 828b9a334..8bdbad10b 100644 --- a/src/Core/Common/index.rst +++ b/src/Core/Common/index.rst @@ -110,4 +110,5 @@ Common StoreNonPixelDataInImage/Documentation.rst CheckIfModuleIsPresent/Documentation.rst DisplayImage/Documentation.rst - DirectWarningToFile/Documentation.rst \ No newline at end of file + DirectWarningToFile/Documentation.rst + SetDefaultNumberOfThreads/Documentation.rst