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

Parallel polydata reader #634

Merged
merged 18 commits into from
Aug 8, 2023
Merged
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
3 changes: 3 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ AC_CONFIG_LINKS([test/testfiles/test_vtk_cube.vtp:test/testfiles/test_vtk_cube.v
AC_CONFIG_LINKS([test/testfiles/test_parallel_file.pvtu:test/testfiles/test_parallel_file.pvtu])
AC_CONFIG_LINKS([test/testfiles/test_parallel_file_0.vtu:test/testfiles/test_parallel_file_0.vtu])
AC_CONFIG_LINKS([test/testfiles/test_parallel_file_1.vtu:test/testfiles/test_parallel_file_1.vtu])
AC_CONFIG_LINKS([test/testfiles/test_polydata.pvtp:test/testfiles/test_polydata.pvtp])
AC_CONFIG_LINKS([test/testfiles/test_polydata_0.vtp:test/testfiles/test_polydata_0.vtp])
AC_CONFIG_LINKS([test/testfiles/test_polydata_1.vtp:test/testfiles/test_polydata_1.vtp])
AC_CONFIG_LINKS([example/IO/cmesh/gmsh/circlesquare_hybrid_hole.msh:example/IO/cmesh/gmsh/circlesquare_hybrid_hole.msh])
AC_CONFIG_FILES([tutorials/features/t8_features_curved_meshes_generate_cmesh.geo:tutorials/features/t8_features_curved_meshes_generate_cmesh.geo])
AC_CONFIG_FILES([tutorials/features/t8_features_curved_meshes_generate_cmesh_2d.geo:tutorials/features/t8_features_curved_meshes_generate_cmesh_2d.geo])
Expand Down
9 changes: 6 additions & 3 deletions example/IO/cmesh/vtk/t8_cmesh_read_from_vtk.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,9 @@ main (int argc, char **argv)
"If set, partition the cmesh uniformly.");
sc_options_add_int (opt, 't', "type_of_file", &vtk_file_type_int, -1,
"Set the type of the data in the file.\n"
"\t\t\t\t\t0 for vtkUnstructuredGrid \n"
"\t\t\t\t\t1 for vtkPolyData\n"
"\t\t\t\t\t2 for pvtu.");
"\t\t\t\t\t0 for vtkUnstructuredGrid,\n"
"\t\t\t\t\t1 for vtkPolyData,\n"
"\t\t\t\t\t2 for pvtu,\n" "\t\t\t\t\t3 for pvtp.");
parsed =
sc_options_parse (t8_get_package_id (), SC_LP_ERROR, opt, argc, argv);

Expand All @@ -190,6 +190,9 @@ main (int argc, char **argv)
case 2:
vtk_file_type = VTK_PARALLEL_UNSTRUCTURED_FILE;
break;
case 3:
vtk_file_type = VTK_PARALLEL_POLYDATA_FILE;
break;
default:
vtk_file_type = VTK_FILE_ERROR;
break;
Expand Down
3 changes: 2 additions & 1 deletion scripts/t8indent_custom_datatypes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ t8_shmem_array_t
t8_stash_attribute_struct_t
t8_stash_t
t8_vtk_data_field_t
vtk_file_type_t
vtk_file_type_t
vtk_read_success_t
111 changes: 92 additions & 19 deletions src/t8_vtk/t8_vtk_parallel.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,26 @@ along with t8code; if not, write to the Free Software Foundation, Inc.,

#if T8_WITH_VTK
#include <vtkUnstructuredGrid.h>
#include <vtkXMLPDataReader.h>
#include <vtkXMLPUnstructuredGridReader.h>
#include <vtkXMLPPolyDataReader.h>
#include <vtkAppendFilter.h>
#include <vtkAppendPolyData.h>

vtk_read_success_t
t8_read_parallel (const char *filename, vtkSmartPointer < vtkDataSet > grid,
sc_MPI_Comm comm)
/**
* Setup the reader on each process
*
* \param[in] filename The filename of the parallel file to read
* \param[in, out] reader On input a reader, on output a reader linked to \a filename
* \param[in, out] first_piece Arbitrary on input, the index of the first piece to read on output
* \param[in, out] last_piece Arbitrary on input, the (non-included) index of the last piece to read on output
* \param[in] comm The Communicator to use.
* \return vtk_read_success_t
*/
static vtk_read_success_t
setup_reader (const char *filename,
vtkSmartPointer < vtkXMLPDataReader > reader,
int *first_piece, int *last_piece, sc_MPI_Comm comm)
{
/* Check if we can open the parallel file */
FILE *first_check;
Expand All @@ -39,9 +53,7 @@ t8_read_parallel (const char *filename, vtkSmartPointer < vtkDataSet > grid,
return read_failure;
}
fclose (first_check);
/* Setup parallel reader. */
vtkSmartPointer < vtkXMLPUnstructuredGridReader > reader =
vtkSmartPointer < vtkXMLPUnstructuredGridReader >::New ();

if (!reader->CanReadFile (filename)) {
t8_errorf ("Unable to read file.\n");
return read_failure;
Expand All @@ -63,36 +75,97 @@ t8_read_parallel (const char *filename, vtkSmartPointer < vtkDataSet > grid,
SC_CHECK_MPI (mpiret);

/* Setup number of pieces to read on this proc. */
int last_piece = -1;
int first_piece = 0;
*last_piece = -1;
*first_piece = 0;

if (mpisize >= total_num_pieces) {
/* The first n-procs read a piece each. */
first_piece = mpirank;
last_piece = first_piece + ((mpirank < mpisize) ? 1 : 0);
*first_piece = mpirank;
*last_piece = *first_piece + ((mpirank < mpisize) ? 1 : 0);
}
else {
first_piece = total_num_pieces / mpisize * mpirank;
last_piece = first_piece;
*first_piece = total_num_pieces / mpisize * mpirank;
*last_piece = *first_piece;
const int prev_proc_first_piece =
total_num_pieces / mpisize * (mpirank - 1);
last_piece +=
first_piece == prev_proc_first_piece ? 0 : total_num_pieces / mpisize;
if (first_piece == total_num_pieces / mpisize * (mpisize - 1)) {
*last_piece +=
*first_piece == prev_proc_first_piece ? 0 : total_num_pieces / mpisize;
if (*first_piece == total_num_pieces / mpisize * (mpisize - 1)) {
/* Read the last chunk of data */
last_piece = total_num_pieces - first_piece;
*last_piece = total_num_pieces - *first_piece;
}
}
return read_success;
}

vtk_read_success_t
t8_read_parallel_polyData (const char *filename,
vtkSmartPointer < vtkDataSet > grid,
sc_MPI_Comm comm)
{
/* Setup parallel reader. */
vtkSmartPointer < vtkXMLPPolyDataReader > reader =
vtkSmartPointer < vtkXMLPPolyDataReader >::New ();

vtk_read_success_t read_status = read_failure;

/* Read the pieces if there are any pieces to read on this proc. */
int first_piece = 0;
int last_piece = -1;
read_status =
setup_reader (filename, reader, &first_piece, &last_piece, comm);
if (read_status == read_failure) {
return read_status;
}
/* Read the pieces if there are any pieces to read on this proc.
* Merge the output of multiple pieces into one grid */
if (first_piece < last_piece) {
vtkNew < vtkAppendFilter > append;
vtkNew < vtkAppendPolyData > append;
const int total_num_pieces = last_piece - first_piece + 1;
for (int ipiece = first_piece; ipiece < last_piece; ipiece++) {
reader->UpdatePiece (ipiece, total_num_pieces, 0);
append->AddInputData (reader->GetOutput ());
}
/* Merge all read grids together */
append->Update ();
grid->ShallowCopy (append->GetOutput ());
}
else {
/* Initialize the grid, but don't construct any cells.
* simplifies further processing of the grid on multiple procs. */
grid->Initialize ();
}
return read_success;
}

vtk_read_success_t
t8_read_parallel_unstructured (const char *filename,
vtkSmartPointer < vtkDataSet > grid,
sc_MPI_Comm comm)
{
/* Setup parallel reader. */
vtkSmartPointer < vtkXMLPUnstructuredGridReader > reader =
vtkSmartPointer < vtkXMLPUnstructuredGridReader >::New ();

vtk_read_success_t read_status = read_failure;

int first_piece = 0;
int last_piece = -1;
read_status =
setup_reader (filename, reader, &first_piece, &last_piece, comm);
if (read_status == read_failure) {
return read_status;
}
/* Read the pieces if there are any pieces to read on this proc.
* Merge the output of multiple pieces into one grid */
if (first_piece < last_piece) {
vtkNew < vtkAppendFilter > append;
const int total_num_pieces = last_piece - first_piece + 1;
for (int ipiece = first_piece; ipiece < last_piece; ipiece++) {
reader->UpdatePiece (ipiece, total_num_pieces, 0);
append->AddInputData (reader->GetOutputAsDataSet ());
}
/* Merge all read grids together */
append->MergePointsOn ();
append->Update ();
grid->ShallowCopy (append->GetOutput ());
}
else {
Expand Down
26 changes: 21 additions & 5 deletions src/t8_vtk/t8_vtk_parallel.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,32 @@ along with t8code; if not, write to the Free Software Foundation, Inc.,

/**
* Given a filename to a parallel vtk file (for example .pvtu) and its data files,
* read a piece of the data files (like .vtu, .vtp, ...).
* read a piece of the data files (like .vtu, ...).
*
* \param[in] filename The name of a parallel vtk file (.pvtu for example)
* \param[in] filename The name of a parallel vtk file to a distributed vtkUnstructuredGrid
* \param[in] grid On input a vtkSmartPointer, that will hold the grid described
* by the pieces read on this proc.
* \returns non-zero on success, zero if the reading failed.
*/
vtk_read_success_t t8_read_parallel (const char *filename,
vtkSmartPointer < vtkDataSet > grid,
sc_MPI_Comm comm);
/* *INDENT-OFF* */
vtk_read_success_t t8_read_parallel_unstructured (const char *filename,
vtkSmartPointer
< vtkDataSet > grid,
sc_MPI_Comm comm);
/* *INDENT-ON* */

/**
* Given a filename to a parallel vtk file (for example .pvtp) and its data files,
* read a piece of the data files (like .vtp, ...).
*
* \param[in] filename The name of a parallel vtk file to a distributed vtkPolyData
* \param[in] grid On input a vtkSmartPointer, that will hold the grid described
* by the pieces read on this proc.
* \returns non-zero on success, zero if the reading failed.
*/
vtk_read_success_t t8_read_parallel_polyData (const char *filename,
vtkSmartPointer < vtkDataSet >
grid, sc_MPI_Comm comm);

#endif /* T8_WITH_VTK */
#endif /* T8_VTK_PARALLEL_HXX */
17 changes: 15 additions & 2 deletions src/t8_vtk/t8_vtk_polydata.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ along with t8code; if not, write to the Free Software Foundation, Inc.,
#include <vtkPolyDataReader.h>
#include <vtkSTLReader.h>
#include <vtkXMLPolyDataReader.h>
#include <vtkXMLPPolyDataReader.h>
#include <vtkTriangleFilter.h>
#include <vtkSmartPointer.h>

static vtk_read_success_t
static vtk_read_success_t
t8_read_poly_ext (const char *filename, vtkSmartPointer < vtkPolyData > grid)
{
char tmp[BUFSIZ];
Expand Down Expand Up @@ -105,6 +106,18 @@ t8_read_poly_ext (const char *filename, vtkSmartPointer < vtkPolyData > grid)
grid->ShallowCopy (vtkDataSet::SafeDownCast (reader->GetOutput ()));
return read_failure;
}
else if (strcmp (extension, "pvtp") == 0) {
vtkSmartPointer < vtkXMLPPolyDataReader > reader =
vtkSmartPointer < vtkXMLPPolyDataReader >::New ();
if (!reader->CanReadFile (filename)) {
t8_errorf ("Unable to read file.\n");
return read_failure;
}
reader->SetFileName (filename);
reader->Update ();
grid->ShallowCopy (vtkDataSet::SafeDownCast (reader->GetOutput ()));
return read_success;
}
else {
/* Return NULL if the reader is not used correctly. */
t8_global_errorf ("Please use .ply, .vtp, .obj, .stl, .vtk or .g file\n");
Expand All @@ -113,7 +126,7 @@ t8_read_poly_ext (const char *filename, vtkSmartPointer < vtkPolyData > grid)
}

vtk_read_success_t
t8_read_poly (const char *filename, vtkDataSet * grid)
t8_read_polyData (const char *filename, vtkDataSet * grid)
{
vtkSmartPointer < vtkPolyData > poly_data =
vtkSmartPointer < vtkPolyData >::New ();
Expand Down
3 changes: 2 additions & 1 deletion src/t8_vtk/t8_vtk_polydata.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ along with t8code; if not, write to the Free Software Foundation, Inc.,
* \returns non-zero on success, zero if the reading failed.
*
*/
vtk_read_success_t t8_read_poly (const char *filename, vtkDataSet * grid);
vtk_read_success_t t8_read_polyData (const char *filename,
vtkDataSet * grid);

#endif /* T8_WITH_VTK */
#endif /* T8_CMESH_VTK_POLYDATA */
23 changes: 19 additions & 4 deletions src/t8_vtk/t8_vtk_reader.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ along with t8code; if not, write to the Free Software Foundation, Inc.,
#include <vtkUnstructuredGrid.h>
#include <vtkUnstructuredGridReader.h>
#include <vtkXMLUnstructuredGridReader.h>
#include <vtkXMLPUnstructuredGridReader.h>
#include <vtkXMLPPolyDataReader.h>
#include <vtkPolyData.h>
#include <vtkBYUReader.h>
#include <vtkOBJReader.h>
Expand Down Expand Up @@ -137,15 +139,25 @@ t8_file_to_vtkGrid (const char *filename,
main_proc_read_successful = t8_read_unstructured (filename, vtkGrid);
break;
case VTK_POLYDATA_FILE:
main_proc_read_successful = t8_read_poly (filename, vtkGrid);
main_proc_read_successful = t8_read_polyData (filename, vtkGrid);
break;
case VTK_PARALLEL_UNSTRUCTURED_FILE:
if (!partition) {
main_proc_read_successful = t8_read_unstructured (filename, vtkGrid);
}
else {
main_proc_read_successful =
t8_read_parallel (filename, vtkGrid, comm);
t8_read_parallel_unstructured (filename, vtkGrid, comm);
break;
}
break;
case VTK_PARALLEL_POLYDATA_FILE:
if (!partition) {
main_proc_read_successful = t8_read_polyData (filename, vtkGrid);
}
else {
main_proc_read_successful =
t8_read_parallel_polyData (filename, vtkGrid, comm);
break;
}
break;
Expand Down Expand Up @@ -214,8 +226,8 @@ t8_get_dimension (vtkSmartPointer < vtkDataSet > vtkGrid)
* the vtkGrid. Each cell in the vtkDataSet becomes a tree in the cmesh. This
* function constructs a cmesh on a single process.
*
* \param[in] vtkGrid The vtkGrid that gets translated
* \param[in, out] cmesh An empty cmesh that is filled with the data.
* \param[in] vtkGrid The vtkGrid that gets translated
* \param[in, out] cmesh An empty cmesh that is filled with the data.
* \param[in] first_tree The global id of the first tree. Will be the global id of the first tree on this proc.
* \param[in] comm A communicator.
* \return The number of elements that have been read by the process.
Expand Down Expand Up @@ -473,6 +485,9 @@ t8_vtk_reader (const char *filename, const int partition,
case VTK_PARALLEL_UNSTRUCTURED_FILE:
vtkGrid = vtkSmartPointer < vtkUnstructuredGrid >::New ();
break;
case VTK_PARALLEL_POLYDATA_FILE:
vtkGrid = vtkSmartPointer < vtkPolyData >::New ();
break;
default:
t8_errorf ("Filetype is not supported.\n");
break;
Expand Down
Loading