Loading [MathJax]/extensions/tex2jax.js
PISM, A Parallel Ice Sheet Model 2.2.1-cd005eec8 committed by Constantine Khrulev on 2025-03-07
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
label_components_serial.cc
Go to the documentation of this file.
1/* Copyright (C) 2019, 2020, 2021, 2022, 2023 PISM Authors
2 *
3 * This file is part of PISM.
4 *
5 * PISM is free software; you can redistribute it and/or modify it under the
6 * terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 3 of the License, or (at your option) any later
8 * version.
9 *
10 * PISM is distributed in the hope that it will be useful, but WITHOUT ANY
11 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with PISM; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#include "label_components.hh"
22
23#include "pism/util/array/Scalar.hh"
24#include "pism/util/error_handling.hh"
25#include "pism/util/petscwrappers/Vec.hh"
26
27namespace pism {
28
29namespace connected_components {
30
31void label_serial(double *image, int nrows, int ncols, bool mark_isolated_components, int reachable,
32 int min_label) {
33
34 using array = details::CArray<double>;
35
36 array output(image, nrows, ncols);
37
38 bool assign_final_labels = true;
39 label(details::Mask<array>(output, reachable), mark_isolated_components, min_label,
40 assign_final_labels, output);
41}
42
43/*!
44 * Label connected components in a mask stored in an array::Scalar.
45 *
46 * This function allocates a copy on rank 0 and so should not be used if that is a
47 * problem.
48 */
49void label_serial(array::Scalar &mask, bool mark_isolated_components, int reachable) {
50 auto mask_p0 = mask.allocate_proc0_copy();
51
52 mask.put_on_proc0(*mask_p0);
53
54 auto grid = mask.grid();
55
56 ParallelSection rank0(grid->com);
57 try {
58 if (grid->rank() == 0) {
59 petsc::VecArray array(*mask_p0);
60 int min_label = 1;
61 connected_components::label_serial(array.get(), static_cast<int>(grid->My()),
62 static_cast<int>(grid->Mx()), mark_isolated_components,
63 reachable, min_label);
64 }
65 } catch (...) {
66 rank0.failed();
67 }
68 rank0.check();
69
70 mask.get_from_proc0(*mask_p0);
71}
72
73} // namespace connected_components
74
75} // end of namespace pism
void failed()
Indicates a failure of a parallel section.
void get_from_proc0(petsc::Vec &onp0)
Gets a local Array2 from processor 0.
Definition Array.cc:1058
std::shared_ptr< const Grid > grid() const
Definition Array.cc:131
std::shared_ptr< petsc::Vec > allocate_proc0_copy() const
Definition Array.cc:926
void put_on_proc0(petsc::Vec &onp0) const
Puts a local array::Scalar on processor 0.
Definition Array.cc:1015
Adds "foregrdound" and "attached" concepts to an "array".
double * get()
Definition Vec.cc:54
Wrapper around VecGetArray and VecRestoreArray.
Definition Vec.hh:44
void label_serial(double *image, int nrows, int ncols, bool mark_isolated_components, int reachable, int min_label)