Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Core/Common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,5 @@ compare_to_baseline(EXAMPLE_NAME OutOfBoundsPixelsReturnConstValue
BASELINE_PREFIX OutputBaseline
)
endif()

add_example(SetDefaultNumberOfThreads)
36 changes: 36 additions & 0 deletions src/Core/Common/SetDefaultNumberOfThreads/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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()
53 changes: 53 additions & 0 deletions src/Core/Common/SetDefaultNumberOfThreads/Code.cxx
Original file line number Diff line number Diff line change
@@ -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 << " <NumberOfThreads>";
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<PixelType, Dimension>;
using FilterType = itk::MedianImageFilter<ImageType, ImageType>;
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;
}
33 changes: 33 additions & 0 deletions src/Core/Common/SetDefaultNumberOfThreads/Code.py
Original file line number Diff line number Diff line change
@@ -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)
46 changes: 46 additions & 0 deletions src/Core/Common/SetDefaultNumberOfThreads/Documentation.rst
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion src/Core/Common/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,5 @@ Common
StoreNonPixelDataInImage/Documentation.rst
CheckIfModuleIsPresent/Documentation.rst
DisplayImage/Documentation.rst
DirectWarningToFile/Documentation.rst
DirectWarningToFile/Documentation.rst
SetDefaultNumberOfThreads/Documentation.rst