How can I write vtkStructuredGrid data in Parallel by using MPI?

I have a C++ program that use MPI for parallelization. I’m trying to write my output as vtkStructuredGrid format in parallel. In fact, I have this function to write the vtk files:

void vtkParallelWriter(int argc, char *argv[],std::vector<double*>colors, std::vector<char*> names, int LX, int LY, int LZ, double x_min, double x_max, double y_min, double y_max, double z_min, double z_max, double local_origin_x, double local_origin_y, double local_origin_z, int nn, int timesnapshot) {
  int dims[3] = {LX, LY, LZ};
  int global_extent[6] = {x_min, x_max, y_min, y_max, z_min, z_max};
  int local_extent[6] = {local_origin_x, local_origin_x+LX-1, local_origin_y, local_origin_y+LY-1, local_origin_z, local_origin_z+LZ-1};

  // Create and Initialize vtkMPIController
  auto contr = vtkSmartPointer<vtkMPIController>::New();
  contr->Initialize(&argc, &argv, 1);
  int nranks = contr->GetNumberOfProcesses();
  int rank   = contr->GetLocalProcessId();

  // Create grid points, allocate memory and Insert them
  auto points = vtkSmartPointer<vtkPoints>::New();
  points->Allocate(dims[0]*dims[1]*dims[2]);
  int iterator = 0;
  for (int k=0; k<dims[2]; ++k) {
    for (int j=0; j<dims[1]; ++j) {
      for (int i=0; i<dims[0]; ++i) {
        points->InsertPoint(iterator,
                            local_origin_x+i, local_origin_y+j, local_origin_z+k);
        iterator++;

   }
   }
   }

  // Create a density field. Note that the number of cells is always less than
  // number of grid points by an amount of one so we use dims[i]-1
  std::vector<vtkSmartPointer<vtkFloatArray>> vtkColors;
  for (int iterator = 0; iterator < colors.size(); iterator++) {
  vtkColors[iterator]->SetNumberOfComponents(1);
  vtkColors[iterator]->SetNumberOfTuples(dims[0]*dims[1]*dims[2]);
  vtkColors[iterator]->SetName(names[iterator]);
  int index = 0;
  int LXP = nn+LX+nn;
  int LYP = nn+LY+nn;
  int LZP = nn+LZ+nn;
  for (int k=0; k<dims[2]; ++k) {
        int K = k + nn;
    for (int j=0; j<dims[1]; ++j) {
                int J = j + nn;
      for (int i=0; i<dims[0]; ++i) {
                        int I = i + nn;
                                int IDflattened = I+J*LXP+K*LXP*LYP;
        vtkColors[iterator]->SetValue(index, colors[iterator][IDflattened]);
        index++;
      }
      }
      }
  }

  // Create a vtkProgrammableFilter
  auto pf = vtkSmartPointer<vtkProgrammableFilter>::New();

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

  pf->SetExecuteMethod(execute, &args);

  // Create a structured grid and assign point data and cell data to it
  auto structuredGrid = vtkSmartPointer<vtkStructuredGrid>::New();
  structuredGrid->SetExtent(global_extent);
  pf->SetInputData(structuredGrid);
  structuredGrid->SetPoints(points);
  for (int iterator = 0; iterator < vtkColors.size(); iterator++) {
  structuredGrid->GetCellData()->AddArray(vtkColors[iterator]);
  }

  std::string fileName = std::string("./out/output_") + std::to_string(timesnapshot) + ".pvts";

  // Create the parallel writer and call some functions
  auto parallel_writer = vtkSmartPointer<vtkXMLPStructuredGridWriter>::New();
  parallel_writer->SetInputConnection(pf->GetOutputPort());
  parallel_writer->SetController(contr);
  parallel_writer->SetFileName(fileName.c_str());
  parallel_writer->SetNumberOfPieces(nranks);
  parallel_writer->SetStartPiece(rank);
  parallel_writer->SetEndPiece(rank);
  parallel_writer->SetDataModeToBinary();
  parallel_writer->Update();
  parallel_writer->Write();

  contr->Finalize();

}

Unfortunately, when I run this function, it shows me this error:

==== backtrace (tid:  34166) ====
 0 0x0000000000409d63 vtkParallelWriter()  ???:0
 1 0x000000000040b3f1 main()  ???:0
 2 0x0000000000022495 __libc_start_main()  ???:0
 3 0x00000000004083a9 _start()  ???:0

Any idea what’s wrong with my parallel vtk writer function? I guess the problem is about vtkMPIController, cause I have a MPI topology in my program but I don’t know how to pass this topology to vtkMPIController.