#include <petscdm.h>
#include <petscdmplex.h>
#include <petscdmlabel.h>

int main(int argc, char *argv[]) {

    DM       dm;
    PetscInt n_labels;

    PetscFunctionBegin;
    PetscCall(PetscInitialize(&argc, &argv, NULL, NULL));

    PetscCall(DMCreate(PETSC_COMM_WORLD, &dm));
    PetscCall(DMSetType(dm, DMPLEX));
    PetscCall(DMSetFromOptions(dm));
    PetscCall(DMViewFromOptions(dm, NULL, "-dm_view"));

    PetscCall(DMGetNumLabels(dm, &n_labels));
    for (PetscInt n = 2; n < n_labels; n++) {   // skip celltype & depth
        DMLabel     label;
        PetscInt    pStart, pEnd;
        const char *labelName;

        PetscCall(DMGetLabelByNum(dm, n, &label));
        PetscCall(DMGetLabelName(dm, n, &labelName));
        //PetscCall(DMPlexLabelComplete(dm, label));
        PetscCall(DMLabelGetBounds(label, &pStart, &pEnd));
        PetscCall(PetscSynchronizedPrintf(PETSC_COMM_WORLD, "%s : s / e : %" PetscInt_FMT " / %" PetscInt_FMT "\n", labelName, pStart, pEnd));
        PetscCall(PetscSynchronizedFlush(PETSC_COMM_WORLD, NULL));
        PetscCall(DMLabelViewFromOptions(label, NULL, "-label_view"));
    }

    PetscCall(DMDestroy(&dm));
    PetscCall(PetscFinalize());
    return 0;
}

/*
 *
 * mpirun -n 2 --tag-output ./test -dm_plex_filename mesh.msh -dm_plex_gmsh_use_regions -dm_plex_gmsh_mark_vertices -dm_plex_gmsh_multiple_tags
 *
 */
