PERF: Make ImageGridSampler multi-threaded#1017
Merged
Conversation
Member
Author
|
Update: just did a performance benchmark with mask: using PixelType = int;
enum
{
// ImageSizeValue = 200U,
// Dimension = 3U
ImageSizeValue = 4096U,
Dimension = 2U
};
using ImageType = itk::Image<PixelType>;
using SamplerType = itk::ImageGridSampler<ImageType>;
constexpr auto imageSize = ImageType::SizeType::Filled(ImageSizeValue);
const auto image = CreateImage<PixelType>(imageSize);
using MaskSpatialObjectType = itk::ImageMaskSpatialObject<Dimension>;
const auto maskImage = CreateImage<MaskSpatialObjectType::PixelType>(ImageDomain(*image));
unsigned int i{};
for (std::uint8_t & maskValue : itk::ImageBufferRange(*maskImage))
{
maskValue = (i % 3U == 0) ? std::uint8_t{ 1 } : std::uint8_t{ 0 };
++i;
}
const auto maskSpatialObject = MaskSpatialObjectType::New();
maskSpatialObject->SetImage(maskImage);
maskSpatialObject->Update();
const auto generateSamples = [image, maskSpatialObject](const bool useMultiThread) {
elx::DefaultConstruct<SamplerType> sampler{};
sampler.SetInput(image);
sampler.SetMask(maskSpatialObject);
sampler.SetUseMultiThread(useMultiThread);
sampler.SetSampleGridSpacing(itk::MakeFilled<SamplerType::SampleGridSpacingType>(2));
sampler.SetInput(image);
using namespace std::chrono;
const auto timePoint = high_resolution_clock::now();
sampler.Update();
std::cout << (useMultiThread ? "MT" : "ST")
<< " Duration: " << duration_cast<duration<double>>(high_resolution_clock::now() - timePoint).count()
<< " seconds" << std::endl;
return std::move(DerefRawPointer(sampler.GetOutput()).CastToSTLContainer());
};
for (int i{}; i < 5; ++i)
{
generateSamples(true);
generateSamples(false);
}
Output (VS2019 Release): So this case also supports using multi-threading 😃 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Initial benchmark (4096x4096 image, 2x2 grid spacing, no mask) suggests a performance improvement by a factor of ~2 (VS2019 Release build, 6 cores, 12 logical processors, 48 work units).
Benchmark code:
Output (2D, 4096x4096):
Output for 3D (200x200x200):