Reading, Filtering and Writing in multiple threads

Keyn34
Fri, 15 Oct 2021 11:23:41 +0000
Beginner Questions

Hello everyone ( again :D),

Are there any resources or tips on running a function like this

void multiStaple(std::vector<std::string> inputfiles)
{
        // itk::MultiLabelSTAPLEImageFilter Type
	FilterType::Pointer filter = FilterType::New();
	filter->SetLabelForUndecidedPixels(0);
	filter->SetMaximumNumberOfIterations(1);

	for (size_t i = 0; i < inputfiles.size(); i++)
	{
		ReaderType::Pointer reader = ReaderType::New();
		reader->SetFileName(inputfiles[i]);

		try {
			reader->SetNumberOfThreads(1);
			reader->Update();
		}
		catch (itk::ExceptionObject& error) {
			mu.lock();
			std::cerr << "Error: " << error << std::endl;
			mu.unlock();
		}

		filter->SetInput(i, reader->GetOutput());
	}

	try {
		filter->SetNumberOfThreads(1);
		filter->Update();
	}
	catch (itk::ExceptionObject& error) {
		mu.lock();
		std::cerr << "Error: " << error << std::endl;
		mu.unlock();
	}

	WriterType::Pointer writer = WriterType::New();
	std::string outputFilename = inputfiles[0];
	std::string outputFilepath = std::filesystem::path(outputFilename).parent_path().string() + "\\" + outputFilename + "_filtered.nii";

	mu.lock();
	std::cout << "Writing: " << outputFilepath << std::endl;
	mu.unlock();

	writer->SetFileName(outputFilepath);
	writer->SetInput(filter->GetOutput());

	try {
		writer->SetNumberOfThreads(1);
		writer->Update();
	}
	catch (itk::ExceptionObject& error)
	{
		mu.lock();
		std::cerr << "Error: " << error << std::endl;
		mu.unlock();
	}
}

in multiple std::threads like

for (int i = 0; i < maxNumberOfThreads; ++i) {
	threadPool.emplace_back(std::thread(multiStaple, filesets[i]));
}

for (std::thread& th : threadPool) {
	th.join();
}

Because, as far as I know now, the itk::MultiLabelSTAPLEImageFilter does not have an implementation of ThreadedGenerateData() (taken from ImageToImageFilter). Therefore it should be theoretically possible to start multiple threads working on different image data, or am I wrong?

When I let it run with a single thread, everything works fine. If I start more than one thread, I get ITK Exceptions during execution. Are there any multithreaded accesses I am not aware of the reader, filter, and writer? In theory, there are no shared resources between the threads, as I only need to run the StapleFilter on multiple different data sets. I could not find any examples of letting filters run in parallel. I would be very grateful if you could show me some, given there are some.

Any suggestions are highly appreciated, as always!

4 posts - 3 participants