Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebAssembly Support #920

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
27 changes: 18 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,23 @@ if(ELASTIX_USE_OPENCL)
list(APPEND _GPU_depends ITKGPUCommon)
endif()

set(_ITK_io_depends
ITKImageIO
ITKTransformIO
ITKMeshIO
ITKIOImageBase
ITKIOMeshBase
ITKIOMeshOBJ
ITKIOMeta
ITKIOTransformBase
ITKIOXML
)

if(WASI OR EMSCRIPTEN)
# Keep Wasm binaries small
set(_ITK_io_depends)
endif()

# Find ITK.
find_package(ITK 5.3 REQUIRED COMPONENTS
ITKCommon
Expand All @@ -43,12 +60,6 @@ find_package(ITK 5.3 REQUIRED COMPONENTS
ITKImageGrid
ITKImageIntensity
ITKImageStatistics
ITKIOImageBase
ITKIOMeshBase
ITKIOMeshOBJ
ITKIOMeta
ITKIOTransformBase
ITKIOXML
ITKMathematicalMorphology
ITKMesh
ITKOptimizers
Expand All @@ -61,9 +72,7 @@ find_package(ITK 5.3 REQUIRED COMPONENTS
ITKTransform
ITKTransformFactory
${_GPU_depends}
ITKImageIO
ITKTransformIO
ITKMeshIO
${_ITK_io_depends}
)
include(${ITK_USE_FILE})

Expand Down
2 changes: 0 additions & 2 deletions Common/ImageSamplers/itkImageToVectorContainerFilter.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
#ifndef itkImageToVectorContainerFilter_hxx
#define itkImageToVectorContainerFilter_hxx

#include "itkImageToVectorContainerFilter.h"

#include "itkMath.h"

namespace itk
Expand Down
16 changes: 16 additions & 0 deletions Common/Transforms/elxTransformIO.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@

#include <itkTransformBase.h>
#include <itkTransformFactoryBase.h>
#ifndef __wasm32__
#include <itkTransformFileReader.h>
#include <itkTransformFileWriter.h>
#endif

#include <string>

Expand Down Expand Up @@ -191,6 +193,11 @@ elastix::TransformIO::ConvertItkTransformBaseToSingleItkTransform(const itk::Tra
void
elastix::TransformIO::Write(const itk::Object & itkTransform, const std::string & fileName)
{
#ifdef __wasm32__
const std::string message = "File IO not supported in WebAssembly builds.";
log::error(message);
throw std::runtime_error(message);
#else
try
{
const auto writer = itk::TransformFileWriter::New();
Expand All @@ -203,12 +210,20 @@ elastix::TransformIO::Write(const itk::Object & itkTransform, const std::string
{
log::error(std::ostringstream{} << "Error trying to write " << fileName << ":\n" << stdException.what());
}
#endif
}


itk::SmartPointer<itk::TransformBase>
elastix::TransformIO::Read(const std::string & fileName)
{
#ifdef __wasm32__
const std::string message = "File IO not supported in WebAssembly builds.";
log::error(message);
throw std::runtime_error(message);
return nullptr;
#else

const auto reader = itk::TransformFileReader::New();

reader->SetFileName(fileName);
Expand All @@ -221,6 +236,7 @@ elastix::TransformIO::Read(const std::string & fileName)
assert(transformList->size() <= 1);

return transformList->empty() ? nullptr : transformList->front();
#endif
}


Expand Down
2 changes: 2 additions & 0 deletions Common/itkComputeImageExtremaFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ class ITK_TEMPLATE_EXPORT ComputeImageExtremaFilter : public StatisticsImageFilt
PixelType m_ThreadMin{ 1 };
PixelType m_ThreadMax{ 1 };

#ifndef __wasi__
std::mutex m_Mutex{};
#endif
}; // end of class
} // end namespace itk

Expand Down
4 changes: 4 additions & 0 deletions Common/itkComputeImageExtremaFilter.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ ComputeImageExtremaFilter<TInputImage>::ThreadedGenerateDataImageSpatialMask(con
} // end for
} // end if

#ifndef __wasi__
std::lock_guard<std::mutex> mutexHolder(m_Mutex);
#endif
Comment on lines +179 to +181
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does WASI support multi-threading? Specifically, will this ComputeImageExtremaFilter run using multiple threads in parallel?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, the default WASI toolchain does not support multi-threading. This is being improved, but we need these for now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, still not very happy about having #ifndef __wasi__ all over the place. Would it be an idea to have a "dummy" mutex class (that does not do anything) for single-threaded builds.

#ifdef ELX_SINGLE_THREADED_BUILD
  using ElastixMutexType = DummyMutex;  // Mutex-like dummy type that does nothing
#else
  using ElastixMutexType = std::mutex;
#endif 

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I do not see how a dummy mutex type addresses the std::lock_guard and the mutex class members.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::lock_guard should be able to support any "mutex-like" type, that has a lock() and an unlock() member function. So for example, would the following code compile on WASI? https://godbolt.org/z/oP5T6Pn35 Can you please try that out?

#define ELX_SINGLE_THREADED // For WebAssembly Support

#include <mutex>

// Mutex for single-threading, does nothing.
class SingleThreadedMutex
{
  public:
    void lock() {}
    void unlock() {}
};

// The MutexType type alias:
#ifdef ELX_SINGLE_THREADED
using MutexType = SingleThreadedMutex;
#else
using MutexType = std::mutex;
#endif

// Use case:
MutexType mutexVariable;
std::lock_guard<MutexType> lockGuard(mutexVariable);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thewtex Would it be helpful to you if I would implement such a SingleThreadedMutex for elastix, that would replace std::mutex when a CMake option like ELX_SINGLE_THREADED would be ON?

m_ThreadSum += sum;
m_SumOfSquares += sumOfSquares;
m_Count += count;
Expand Down Expand Up @@ -227,7 +229,9 @@ ComputeImageExtremaFilter<TInputImage>::ThreadedGenerateDataImageMask(const Regi
++it;
} // end while

#ifndef __wasi__
std::lock_guard<std::mutex> mutexHolder(m_Mutex);
#endif
m_ThreadSum += sum;
m_SumOfSquares += sumOfSquares;
m_Count += count;
Expand Down
6 changes: 5 additions & 1 deletion Common/itkMeshFileReaderBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@

#include "itkMeshSource.h"
#include "itkMacro.h"
#include "itkMeshFileReader.h" // for MeshFileReaderException
#ifndef __wasm32__
#include "itkMeshFileReaderException.h"
#else
#define MeshFileReaderException ExceptionObject
#endif

namespace itk
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
#include "elxIncludes.h"
#include "itkMissingStructurePenalty.h"

#include "itkMeshFileReader.h"
#include "itkMeshFileWriter.h"

namespace elastix
{

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
#include "elxMissingStructurePenalty.h"
#include "itkTimeProbe.h"

#ifndef __wasm32__
#include "itkMeshFileReader.h"
#include "itkMeshFileWriter.h"
#endif

namespace elastix
{

Expand Down Expand Up @@ -145,6 +150,11 @@ MissingStructurePenalty<TElastix>::BeforeRegistration()
fmeshArgument << ch << metricNumber;
std::string fixedMeshName = this->GetConfiguration()->GetCommandLineArgument(fmeshArgument.str());
typename MeshType::Pointer fixedMesh; // default-constructed (null)
#ifdef __wasm32__
const std::string message = "File IO not supported in WebAssembly builds.";
log::error(message);
itkExceptionMacro(<< message);
#else
if (itksys::SystemTools::GetFilenameLastExtension(fixedMeshName) == ".txt")
{
this->ReadTransformixPoints(fixedMeshName, fixedMesh);
Expand All @@ -153,6 +163,7 @@ MissingStructurePenalty<TElastix>::BeforeRegistration()
{
this->ReadMesh(fixedMeshName, fixedMesh);
}
#endif

meshPointerContainer->SetElement(meshNumber, fixedMesh.GetPointer());
}
Expand Down Expand Up @@ -281,6 +292,12 @@ template <class TElastix>
unsigned int
MissingStructurePenalty<TElastix>::ReadMesh(const std::string & meshFileName, typename FixedMeshType::Pointer & mesh)
{
#ifdef __wasm32__
const std::string message = "File IO not supported in WebAssembly builds.";
log::error(message);
itkExceptionMacro(<< message);
return 0;
#else
/** Read the input mesh. */
auto meshReader = itk::MeshFileReader<MeshType>::New();
meshReader->SetFileName(meshFileName);
Expand All @@ -300,6 +317,7 @@ MissingStructurePenalty<TElastix>::ReadMesh(const std::string & meshFileName, ty
log::info(std::ostringstream{} << " Number of specified input points: " << nrofpoints);

return nrofpoints;
#endif
} // end ReadMesh()


Expand All @@ -311,6 +329,11 @@ template <class TElastix>
void
MissingStructurePenalty<TElastix>::WriteResultMesh(const std::string & filename, MeshIdType meshId)
{
#ifdef __wasm32__
const std::string message = "File IO not supported in WebAssembly builds.";
log::error(message);
itkExceptionMacro(<< message);
#else
/** Setup the pipeline. */

/** Set the points of the latest transformation. */
Expand Down Expand Up @@ -376,7 +399,7 @@ MissingStructurePenalty<TElastix>::WriteResultMesh(const std::string & filename,
// restore celldata as undefined
mappedMesh->SetCellData(nullptr);
}

#endif
} // end WriteResultMesh()


Expand All @@ -389,6 +412,12 @@ unsigned int
MissingStructurePenalty<TElastix>::ReadTransformixPoints(const std::string & filename,
typename MeshType::Pointer & mesh) // const
{
#ifdef __wasm32__
const std::string message = "File IO not supported in WebAssembly builds.";
log::error(message);
itkExceptionMacro(<< message);
return 0;
#else
/*
FB: Majority of the code is copied from elxTransformBase.hxx: TransformPointsSomePoints()
Function to read 2d structures by reading elastix point files (transformix format) and connecting
Expand Down Expand Up @@ -528,6 +557,7 @@ the sequence of points to form a 2d connected polydata contour.
}
}
return nrofpoints;
#endif
} // end ReadTransformixPoints()


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@

//#include "elxMetricBase.h"

#include "itkMeshFileReader.h"
#include "itkMeshFileWriter.h"

namespace elastix
{
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@

#include <typeinfo>

#ifndef __wasm32__
#include "itkMeshFileReader.h"
#include "itkMeshFileWriter.h"
#endif

namespace elastix
{

Expand Down Expand Up @@ -270,6 +275,12 @@ template <class TElastix>
unsigned int
PolydataDummyPenalty<TElastix>::ReadMesh(const std::string & meshFileName, typename FixedMeshType::Pointer & mesh)
{
#ifdef __wasm32__
const std::string message = "File IO not supported in WebAssembly builds.";
log::error(message);
itkExceptionMacro(<< message);
return 0;
#else
/** Read the input mesh. */
auto meshReader = itk::MeshFileReader<MeshType>::New();
meshReader->SetFileName(meshFileName);
Expand All @@ -291,6 +302,7 @@ PolydataDummyPenalty<TElastix>::ReadMesh(const std::string & meshFileName, typen
log::info(std::ostringstream{} << " Number of specified input points: " << nrofpoints);

return nrofpoints;
#endif
} // end ReadMesh()


Expand All @@ -302,6 +314,11 @@ template <class TElastix>
void
PolydataDummyPenalty<TElastix>::WriteResultMesh(const std::string & filename, MeshIdType meshId)
{
#ifdef __wasm32__
const std::string message = "File IO not supported in WebAssembly builds.";
log::error(message);
itkExceptionMacro(<< message);
#else
/** Set the points of the latest transformation. */
const MappedMeshContainerPointer mappedMeshContainer = this->GetModifiableMappedMeshContainer();
FixedMeshPointer mappedMesh = mappedMeshContainer->ElementAt(meshId);
Expand Down Expand Up @@ -360,7 +377,7 @@ PolydataDummyPenalty<TElastix>::WriteResultMesh(const std::string & filename, Me
{
mappedMesh->SetCellData(nullptr);
}

#endif
} // end WriteResultMesh()


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
#include "itkDefaultStaticMeshTraits.h"
#include "itkTransformMeshFilter.h"
#include <itkMesh.h>
#ifndef __wasm32__
#include <itkMeshFileReader.h>
#endif

#include <fstream>
#include <typeinfo>
Expand Down Expand Up @@ -352,6 +354,12 @@ StatisticalShapePenalty<TElastix>::ReadShape(const std::string &
typename PointSetType::Pointer & pointSet,
const typename ImageType::ConstPointer image)
{
#ifdef __wasm32__
const std::string message = "File IO not supported in WebAssembly builds.";
log::error(message);
itkExceptionMacro(<< message);
return 0;
#else
/** Typedef's. \todo test DummyIPPPixelType=bool. */
using DummyIPPPixelType = double;
using MeshTraitsType =
Expand Down Expand Up @@ -380,7 +388,7 @@ StatisticalShapePenalty<TElastix>::ReadShape(const std::string &
pointSet = PointSetType::New();
pointSet->SetPoints(mesh->GetPoints());
return nrofpoints;

#endif
} // end ReadShape()


Expand Down
14 changes: 13 additions & 1 deletion Core/ComponentBaseClasses/elxFixedImagePyramidBase.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@

#include "elxFixedImagePyramidBase.h"
#include "elxDeref.h"

#ifndef __wasm32__
#include "itkImageFileCastWriter.h"
#endif

namespace elastix
{
Expand Down Expand Up @@ -175,21 +178,30 @@ FixedImagePyramidBase<TElastix>::WritePyramidImage(const std::string & filename,

/** Do the writing. */
log::to_stdout(" Writing fixed pyramid image ...");
#ifndef __wasm32__
try
{
itk::WriteCastedImage(*(this->GetAsITKBaseType()->GetOutput(level)), filename, resultImagePixelType, doCompression);
itk::WriteCastedImage(*(this->GetAsITKBaseType()->GetOutput(level)), filename, resultImagePixelType, doCompression);
}
catch (itk::ExceptionObject & excp)
{
#else
// Always throw -- do not include support code or access filesystem with wasm
itk::ExceptionObject excp;
#endif
/** Add information to the exception. */
excp.SetLocation("FixedImagePyramidBase - BeforeEachResolutionBase()");
std::string err_str = excp.GetDescription();
err_str += "\nError occurred while writing pyramid image.\n";
excp.SetDescription(err_str);

/** Pass the exception to an higher level. */
#ifndef __wasm32__
throw;
}
#else
throw excp;
#endif

} // end WritePyramidImage()

Expand Down
Loading