Segmentation fault happens in vtkAlgorithm::Update when calling vtkXMLPStructuredGridWriter Update()

I have a VTS Writer using vtkXMLPStructuredGridWriter. The program crash and reports Segmentation fault in Update().

Most of my code comes from https://stackoverflow.com/questions/24123432/composing-vtk-file-from-multiple-mpi-outputs, and his code run well. I don’t know what is the difference to make thing crashs.

Here is part of my code where the error happens.

    // Create the parallel writer and call some functions
    auto parallel_writer = vtkSmartPointer<vtkXMLPStructuredGridWriter>::New();
    parallel_writer->SetInputConnection(programmable_filter->GetOutputPort());
    parallel_writer->SetController(contr);
    parallel_writer->SetFileName((filename + ".pvts").c_str());
    parallel_writer->SetNumberOfPieces(mpi_size);
    parallel_writer->SetStartPiece(mpi_rank);
    parallel_writer->SetEndPiece(mpi_rank);
    parallel_writer->SetDataModeToAppended();
    parallel_writer->SetCompressorTypeToNone();
    parallel_writer->EncodeAppendedDataOff();
    parallel_writer->Update();
    parallel_writer->Write();

Minimal reproduction is published to github.

https://github.com/CheapMeow/VTSWriter

Here is all of gdb report. I don’t know the exact meaning of VTK source code.

[0,1]   Program received signal SIGSEGV, Segmentation fault.
[0]     0x00007f2d656a00af in vtkAlgorithm::GetOutputInformation (this=0x959020, port=0) at <my_home>/VTK-9.1.0/Common/ExecutionModel/vtkAlgorithm.cxx:1321
[1]     0x00007f46e3ebc0af in vtkAlgorithm::GetOutputInformation (this=0x2387020, port=0) at <my_home>/VTK-9.1.0/Common/ExecutionModel/vtkAlgorithm.cxx:1321
[0,1]   1321      return this->GetExecutive()->GetOutputInformation(port);
[0,1] (mpigdb) 
[0]     #0  0x00007f2d656a00af in vtkAlgorithm::GetOutputInformation (this=0x959020, 
[1]     #0  0x00007f46e3ebc0af in vtkAlgorithm::GetOutputInformation (this=0x2387020, 
[0,1]       port=0)
[0,1]       at <my_home>/VTK-9.1.0/Common/ExecutionModel/vtkAlgorithm.cxx:1321
[0]     #1  0x0000000000402d59 in VTSWriterMPI::VTSWriterMPI(int, int, int, double, int, char**)::{lambda(void*)#1}::operator()(void*) const (arg=0x7ffd02746170, 
[1]     #1  0x0000000000402d59 in VTSWriterMPI::VTSWriterMPI(int, int, int, double, int, char**)::{lambda(void*)#1}::operator()(void*) const (arg=0x7ffde52c3920, 
[0,1]       __closure=0x0)
[0,1]       at <my_home>/ljc/VTSWriter/src/solver/io/vts_writer_mpi.h:56
[0,1]   #2  VTSWriterMPI::VTSWriterMPI(int, int, int, double, int, char**)::{lambda(void*)#1}::_FUN(void*) ()
[0,1]       at <my_home>/ljc/VTSWriter/src/solver/io/vts_writer_mpi.h:63
[0]     #3  0x00007f2d65a30733 in vtkProgrammableFilter::RequestData (this=0x92bee0, 
[1]     #3  0x00007f46e424c733 in vtkProgrammableFilter::RequestData (this=0x2359ee0, 
[0]         inputVector=0x957a00, outputVector=0x956bc0)
[1]         inputVector=0x2385a00, outputVector=0x2384bc0)
[0,1]       at <my_home>/VTK-9.1.0/Filters/Programmable/vtkProgrammableFilter.cxx:254
[0]     #4  0x00007f2d656dc46c in vtkPassInputTypeAlgorithm::ProcessRequest (
[1]     #4  0x00007f46e3ef846c in vtkPassInputTypeAlgorithm::ProcessRequest (
[0]         this=0x92bee0, request=0x958890, inputVector=0x957a00, 
[1]         this=0x2359ee0, request=0x2386890, inputVector=0x2385a00, 
[0]         outputVector=0x956bc0)
[1]         outputVector=0x2384bc0)
[0,1]       at <my_home>/VTK-9.1.0/Common/ExecutionModel/vtkPassInputTypeAlgorithm.cxx:161
[0]     #5  0x00007f2d656ba630 in vtkExecutive::CallAlgorithm (this=0x9567d0, 
[1]     #5  0x00007f46e3ed6630 in vtkExecutive::CallAlgorithm (this=0x23847d0, 
[0]         request=0x958890, direction=1, inInfo=0x957a00, outInfo=0x956bc0)
[1]         request=0x2386890, direction=1, inInfo=0x2385a00, outInfo=0x2384bc0)
[0,1]       at <my_home>/VTK-9.1.0/Common/ExecutionModel/vtkExecutive.cxx:746
[0]     #6  0x00007f2d656b2d5b in vtkDemandDrivenPipeline::ExecuteData (this=0x9567d0, 
[1]     #6  0x00007f46e3eced5b in vtkDemandDrivenPipeline::ExecuteData (
[0]         request=0x958890, inInfo=0x957a00, outInfo=0x956bc0)
[1]         this=0x23847d0, request=0x2386890, inInfo=0x2385a00, outInfo=0x2384bc0)
[0,1]       at <my_home>/VTK-9.1.0/Common/ExecutionModel/vtkDemandDrivenPipeline.cxx:462
[0]     #7  0x00007f2d656a87f9 in vtkCompositeDataPipeline::ExecuteData (
[1]     #7  0x00007f46e3ec47f9 in vtkCompositeDataPipeline::ExecuteData (
[0]         this=0x9567d0, request=0x958890, inInfoVec=0x957a00, outInfoVec=0x956bc0)
[1]         this=0x23847d0, request=0x2386890, inInfoVec=0x2385a00, 
[0]         at <my_home>/VTK-9.1.0/Common/ExecutionModel/vtkCompositeDataPipeline.cxx:162
[1]         outInfoVec=0x2384bc0)
[0]     #8  0x00007f2d656b248f in vtkDemandDrivenPipeline::ProcessRequest (
[1]         at <my_home>/VTK-9.1.0/Common/ExecutionModel/vtkCompositeDataPipeline.cxx:162
[0]         this=0x9567d0, request=0x958890, inInfoVec=0x957a00, outInfoVec=0x956bc0)
[1]     #8  0x00007f46e3ece48f in vtkDemandDrivenPipeline::ProcessRequest (
[0]         at <my_home>/VTK-9.1.0/Common/ExecutionModel/vtkDemandDrivenPipeline.cxx:261
[1]         this=0x23847d0, request=0x2386890, inInfoVec=0x2385a00, 
[0]     #9  0x00007f2d65765142 in vtkStreamingDemandDrivenPipeline::ProcessRequest (
[1]         outInfoVec=0x2384bc0)
[0]         this=0x9567d0, request=0x958890, inInfoVec=0x957a00, outInfoVec=0x956bc0)
[1]         at <my_home>/VTK-9.1.0/Common/ExecutionModel/vtkDemandDrivenPipeline.cxx:261
[0]         at <my_home>/VTK-9.1.0/Common/ExecutionModel/vtkStreamingDemandDrivenPipeline.cxx:343
[1]     #9  0x00007f46e3f81142 in vtkStreamingDemandDrivenPipeline::ProcessRequest (
[0]     #10 0x00007f2d656aaefe in vtkCompositeDataPipeline::ForwardUpstream (
[1]         this=0x23847d0, request=0x2386890, inInfoVec=0x2385a00, 
[0]         this=0x957fe0, request=0x958890)
[1]         outInfoVec=0x2384bc0)
[0]         at <my_home>/VTK-9.1.0/Common/ExecutionModel/vtkCompositeDataPipeline.cxx:727
[1]         at <my_home>/VTK-9.1.0/Common/ExecutionModel/vtkStreamingDemandDrivenPipeline.cxx:343
[0]     #11 0x00007f2d656b236b in vtkDemandDrivenPipeline::ProcessRequest (
[1]     #10 0x00007f46e3ec6efe in vtkCompositeDataPipeline::ForwardUpstream (
[0]         this=0x957fe0, request=0x958890, inInfoVec=0x957680, outInfoVec=0x957950)
[1]         this=0x2385fe0, request=0x2386890)
[0]         at <my_home>/VTK-9.1.0/Common/ExecutionModel/vtkDemandDrivenPipeline.cxx:247
[1]         at <my_home>/VTK-9.1.0/Common/ExecutionModel/vtkCompositeDataPipeline.cxx:727
[0]     #12 0x00007f2d65765142 in vtkStreamingDemandDrivenPipeline::ProcessRequest (
[1]     #11 0x00007f46e3ece36b in vtkDemandDrivenPipeline::ProcessRequest (
[0]         this=0x957fe0, request=0x958890, inInfoVec=0x957680, outInfoVec=0x957950)
[1]         this=0x2385fe0, request=0x2386890, inInfoVec=0x2385680, 
[0]         at <my_home>/VTK-9.1.0/Common/ExecutionModel/vtkStreamingDemandDrivenPipeline.cxx:343
[1]         outInfoVec=0x2385950)
[0]     #13 0x00007f2d656b2bb7 in vtkDemandDrivenPipeline::UpdateData (this=0x957fe0, 
[1]         at <my_home>/VTK-9.1.0/Common/ExecutionModel/vtkDemandDrivenPipeline.cxx:247
[0]         outputPort=-1)
[1]     #12 0x00007f46e3f81142 in vtkStreamingDemandDrivenPipeline::ProcessRequest (
[0]         at <my_home>/VTK-9.1.0/Common/ExecutionModel/vtkDemandDrivenPipeline.cxx:420
[1]         this=0x2385fe0, request=0x2386890, inInfoVec=0x2385680, 
[0]     #14 0x00007f2d657654c6 in vtkStreamingDemandDrivenPipeline::Update (
[1]         outInfoVec=0x2385950)
[0]         this=0x957fe0, port=-1, requests=0x0)
[1]         at <my_home>/VTK-9.1.0/Common/ExecutionModel/vtkStreamingDemandDrivenPipeline.cxx:343
[0]         at <my_home>/VTK-9.1.0/Common/ExecutionModel/vtkStreamingDemandDrivenPipeline.cxx:417
[1]     #13 0x00007f46e3ecebb7 in vtkDemandDrivenPipeline::UpdateData (this=0x2385fe0, 
[0]     #15 0x00007f2d65765352 in vtkStreamingDemandDrivenPipeline::Update (
[1]         outputPort=-1)
[0]         this=0x957fe0, port=-1)
[1]         at <my_home>/VTK-9.1.0/Common/ExecutionModel/vtkDemandDrivenPipeline.cxx:420
[0]         at <my_home>/VTK-9.1.0/Common/ExecutionModel/vtkStreamingDemandDrivenPipeline.cxx:380
[1]     #14 0x00007f46e3f814c6 in vtkStreamingDemandDrivenPipeline::Update (
[0]     #16 0x00007f2d656a086e in vtkAlgorithm::Update (this=0x957bf0, port=-1)
[1]         this=0x2385fe0, port=-1, requests=0x0)
[0]         at <my_home>/VTK-9.1.0/Common/ExecutionModel/vtkAlgorithm.cxx:1421
[1]         at <my_home>/VTK-9.1.0/Common/ExecutionModel/vtkStreamingDemandDrivenPipeline.cxx:417
[0]     #17 0x00007f2d656a0839 in vtkAlgorithm::Update (this=0x957bf0)
[1]     #15 0x00007f46e3f81352 in vtkStreamingDemandDrivenPipeline::Update (
[0]         at <my_home>/VTK-9.1.0/Common/ExecutionModel/vtkAlgorithm.cxx:1415
[1]         this=0x2385fe0, port=-1)
[0]     #18 0x0000000000403b7e in VTSWriterMPI::write_to_file (this=<optimized out>, 
[1]         at <my_home>/VTK-9.1.0/Common/ExecutionModel/vtkStreamingDemandDrivenPipeline.cxx:380
[0]         filename=...)
[1]     #16 0x00007f46e3ebc86e in vtkAlgorithm::Update (this=0x2385bf0, port=-1)
[0]         at <my_home>/ljc/VTSWriter/src/solver/io/vts_writer_mpi.cpp:26
[1]         at <my_home>/VTK-9.1.0/Common/ExecutionModel/vtkAlgorithm.cxx:1421
[0]     #19 0x000000000040295f in main (argc=<optimized out>, argv=<optimized out>)
[1]     #17 0x00007f46e3ebc839 in vtkAlgorithm::Update (this=0x2385bf0)
[0]         at <my_home>/ljc/VTSWriter/src/manual_test/test.cpp:20
[1]         at <my_home>/VTK-9.1.0/Common/ExecutionModel/vtkAlgorithm.cxx:1415
[1]     #18 0x0000000000403b7e in VTSWriterMPI::write_to_file (this=<optimized out>, 
[1]         filename=...)
[1]         at <my_home>/ljc/VTSWriter/src/solver/io/vts_writer_mpi.cpp:26
[1]     #19 0x000000000040295f in main (argc=<optimized out>, argv=<optimized out>)
[1]         at <my_home>/ljc/VTSWriter/src/manual_test/test.cpp:20

Thanks you for your reply!

Please update to the last release of VTK, v9.3.1

I link to VTK-9.3.1 but there is link error.

Release/libSolver.a(vts_writer_mpi.cpp.o): In function `vtkAlgorithm::GetErrorCode()':
/public/home/jzc/VTK-9.3.1/Common/ExecutionModel/vtkAlgorithm.h:280: undefined reference to `vtksys::SystemTools::GetFilenameName(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
Release/libSolver.a(vts_writer_mpi.cpp.o): In function `vtkXMLPDataObjectWriter::SetStartPiece(int)':
/public/home/jzc/VTK-9.3.1/IO/ParallelXML/vtkXMLPDataObjectWriter.h:44: undefined reference to `vtksys::SystemTools::GetFilenameName(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
Release/libSolver.a(vts_writer_mpi.cpp.o): In function `vtkXMLPDataObjectWriter::SetNumberOfPieces(int)':
/public/home/jzc/VTK-9.3.1/IO/ParallelXML/vtkXMLPDataObjectWriter.h:36: undefined reference to `vtksys::SystemTools::GetFilenameName(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
Release/libSolver.a(vts_writer_mpi.cpp.o): In function `vtkXMLWriterBase::SetFileName(char const*)':
/public/home/jzc/VTK-9.3.1/IO/XML/vtkXMLWriterBase.h:114: undefined reference to `vtksys::SystemTools::GetFilenameName(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
Release/libSolver.a(vts_writer_mpi.cpp.o): In function `vtkXMLWriterBase::SetDataMode(int)':
/public/home/jzc/VTK-9.3.1/IO/XML/vtkXMLWriterBase.h:182: undefined reference to `vtksys::SystemTools::GetFilenameName(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
Release/libSolver.a(vts_writer_mpi.cpp.o):/public/home/jzc/VTK-9.3.1/IO/ParallelXML/vtkXMLPDataObjectWriter.h:46: more undefined references to `vtksys::SystemTools::GetFilenameName(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)' follow
collect2: error: ld returned 1 exit status

My CMakeLists.txt is

cmake_minimum_required(VERSION 3.10)
project(MyProject)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_CXX_FLAGS_DEBUG "-g -O0 -lm -DDEBUG")
set(CMAKE_CXX_FLAGS_RELEASE "-g -O3 -lm -DRELEASE")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-g -O3 -lm -DRELWITHDEBINFO")

# set output
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/$<CONFIG>)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/$<CONFIG>)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/$<CONFIG>)

# find 3rdparty
find_package(MPI REQUIRED)
find_package(VTK COMPONENTS 
  CommonCore
  CommonMisc
  CommonDataModel
  IOXML
  IOParallelXML
  ParallelMPI
  FiltersProgrammable
)
if (NOT VTK_FOUND)
  message(FATAL_ERROR "Solver: Unable to find the VTK build folder.")
endif()

# ================ Solver ================

add_library(Solver STATIC
	src/solver/io/vts_writer.h
	src/solver/io/vts_writer_mpi.cpp
	src/solver/io/vts_writer.cpp
	src/solver/io/vts_writer_mpi.h
	src/solver/fluid_grid/fluid_grid_3d.h
	src/solver/fluid_grid/field3.hpp
)

target_include_directories(Solver PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}/src/solver
    ${MPI_CXX_INCLUDE_DIRS})
target_link_libraries(Solver PUBLIC
    MPI::MPI_CXX
    ${VTK_LIBRARIES})

# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS VTSWriter
  MODULES ${VTK_LIBRARIES}
)

# ================ EXE ================

# Manual Tests

add_executable(test src/manual_test/test.cpp)
target_link_libraries(test PUBLIC Solver)

set_target_properties(test PROPERTIES
    RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/$<CONFIG>
    ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/$<CONFIG>
    LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/$<CONFIG>
)

My build command is

mkdir build
cd build
cmake -DVTK_USE_MPI=ON ..
make -j52

Seems like I lack libsys, but it should be link in vtk library. It should be passed to my cmake project by interface. Is there anything missing? I use the CMakeLists.txt to link VTK-9.1.0 before, and there was no link error.

Final solution is put vtkProgrammableFilter handling into every write. Still using VTK-9.1.0.

Maybe vtkProgrammableFilter has some hidden usage requirement?

void VTSWriterMPI::write_to_file(const std::string& filename)
{
    // Create a vtkProgrammableFilter
    programmable_filter = vtkSmartPointer<vtkProgrammableFilter>::New();

    // Initialize an instance of Args
    Args args;
    args.programmable_filter = programmable_filter;
    for (int i = 0; i < 6; ++i)
        args.local_extent[i] = local_extent[i];

    programmable_filter->SetExecuteMethod(
        [](void* arg) {
            Args*              args       = reinterpret_cast<Args*>(arg);
            auto               info       = args->programmable_filter->GetOutputInformation(0);
            auto               output_tmp = args->programmable_filter->GetOutput();
            auto               input_tmp  = args->programmable_filter->GetInput();
            vtkStructuredGrid* output     = dynamic_cast<vtkStructuredGrid*>(output_tmp);
            vtkStructuredGrid* input      = dynamic_cast<vtkStructuredGrid*>(input_tmp);
            output->ShallowCopy(input);
            output->SetExtent(args->local_extent);
        },
        &args);
        
    programmable_filter->SetInputData(vtk_structured_grid);

    vtk_structured_grid->SetExtent(global_extent);
    vtk_structured_grid->SetPoints(points);

    vtk_structured_grid->GetPointData()->AddArray(velocity);
    vtk_structured_grid->GetPointData()->AddArray(pressure);

    std::cout << "Writing vts: " << filename + ".vts" << std::endl;

    // Create the parallel writer and call some functions
    auto parallel_writer = vtkSmartPointer<vtkXMLPStructuredGridWriter>::New();
    parallel_writer->SetInputConnection(programmable_filter->GetOutputPort());
    parallel_writer->SetController(contr);
    parallel_writer->SetFileName((filename + ".pvts").c_str());
    parallel_writer->SetNumberOfPieces(mpi_size);
    parallel_writer->SetStartPiece(mpi_rank);
    parallel_writer->SetEndPiece(mpi_rank);
    parallel_writer->SetDataModeToAppended();
    parallel_writer->SetCompressorTypeToNone();
    parallel_writer->EncodeAppendedDataOff();
    parallel_writer->Update();
    parallel_writer->Write();

    if (parallel_writer->GetErrorCode() != vtkErrorCode::NoError)
    {
        std::cout << "Writing to file failed: " << filename + ".vts" << std::endl;
    }
}